STM32配置CH375B成HID Host模式读取自定义HID设备的数据 ——STM32端口初始化

原创 2017年04月07日 09:49:03

        最近产品需要一个USB主机测试治具,所以需要做一个USB HOST去读取HID设备的数据,由于以前也没做过USB方面的项目,对这一块也不是很熟悉,因此遇到了很多困难,所幸的是经过两天半的努力,最终完成了CH375B的调试。得意不多废话,先上一张我调试的MCU管脚分配图

 

头文件

#ifndef __BSP_INIT_H__
#define __BSP_INIT_H__

#include "stm32f0xx_hal.h"
#include "config.h"

typedef struct
{
	struct
	{
		uint16_t USB_BaseTime;
		uint8_t  USB_Flag;
	}USB_Heart;
}Sys_Heart_Def;

typedef struct
{
	Sys_Heart_Def   Sys_Heart_Info;
	uint8_t         USB_Connect_Status;
}Sys_Param_Def;

extern Sys_Param_Def      SysParam_Info;
extern UART_HandleTypeDef HAL_CH375_USART;

void Error_Handler(void);
void Bsp_Peripherals_Init(void);

#endif


详细的代码如下:

#include "bsp_init.h"

Sys_Param_Def      SysParam_Info;
/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef HAL_CH375_USART;

void Bsp_SystemClock_Init(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct;
  RCC_ClkInitTypeDef RCC_ClkInitStruct;
  RCC_PeriphCLKInitTypeDef PeriphClkInit;

  /* Initializes the CPU, AHB and APB busses clocks */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = 16;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
  RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /* Initializes the CPU, AHB and APB busses clocks */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                                |RCC_CLOCKTYPE_PCLK1;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  {
    Error_Handler();
  }

  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
  PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK1;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }

  /* Configure the Systick interrupt time */
  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  /* Configure the Systick */
  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  /* SysTick_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}

void Bsp_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct;

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(CH375_D0_GPIO_Port, CH375_D0_Pin, GPIO_PIN_SET);
  HAL_GPIO_WritePin(CH375_D1_GPIO_Port, CH375_D1_Pin, GPIO_PIN_SET);
  HAL_GPIO_WritePin(CH375_D2_GPIO_Port, CH375_D2_Pin, GPIO_PIN_SET);
  HAL_GPIO_WritePin(CH375_D3_GPIO_Port, CH375_D3_Pin, GPIO_PIN_SET);
  HAL_GPIO_WritePin(CH375_D4_GPIO_Port, CH375_D4_Pin, GPIO_PIN_SET);
  HAL_GPIO_WritePin(CH375_D5_GPIO_Port, CH375_D5_Pin, GPIO_PIN_SET);
  HAL_GPIO_WritePin(CH375_D6_GPIO_Port, CH375_D6_Pin, GPIO_PIN_SET);
  HAL_GPIO_WritePin(CH375_D7_GPIO_Port, CH375_D7_Pin, GPIO_PIN_SET);
  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(CH375_WR_GPIO_Port, CH375_WR_Pin, GPIO_PIN_SET);
  HAL_GPIO_WritePin(CH375_RD_GPIO_Port, CH375_RD_Pin, GPIO_PIN_SET);
  HAL_GPIO_WritePin(CH375_CS_GPIO_Port, CH375_CS_Pin, GPIO_PIN_SET);
  HAL_GPIO_WritePin(CH375_A0_GPIO_Port, CH375_A0_Pin, GPIO_PIN_SET);	
	
  /*Configure GPIO pin : CH375_INT_Pin */
  GPIO_InitStruct.Pin = CH375_INT_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  HAL_GPIO_Init(CH375_INT_GPIO_Port, &GPIO_InitStruct);

  /*Configure GPIO pins : CH375_D0_Pin CH375_D1_Pin CH375_D2_Pin CH375_D3_Pin 
                           CH375_D4_Pin CH375_D5_Pin CH375_D6_Pin CH375_D7_Pin */
  GPIO_InitStruct.Pin = CH375_D0_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  HAL_GPIO_Init(CH375_D0_GPIO_Port, &GPIO_InitStruct);	
	
  GPIO_InitStruct.Pin = CH375_D1_Pin;
  HAL_GPIO_Init(CH375_D1_GPIO_Port, &GPIO_InitStruct);
	
  GPIO_InitStruct.Pin = CH375_D2_Pin;
  HAL_GPIO_Init(CH375_D2_GPIO_Port, &GPIO_InitStruct);
	
  GPIO_InitStruct.Pin = CH375_D3_Pin;
  HAL_GPIO_Init(CH375_D3_GPIO_Port, &GPIO_InitStruct);
	
  GPIO_InitStruct.Pin = CH375_D4_Pin;
  HAL_GPIO_Init(CH375_D4_GPIO_Port, &GPIO_InitStruct);
	
  GPIO_InitStruct.Pin = CH375_D5_Pin;
  HAL_GPIO_Init(CH375_D5_GPIO_Port, &GPIO_InitStruct);
	
  GPIO_InitStruct.Pin = CH375_D6_Pin;
  HAL_GPIO_Init(CH375_D6_GPIO_Port, &GPIO_InitStruct);
	
  GPIO_InitStruct.Pin = CH375_D7_Pin;
  HAL_GPIO_Init(CH375_D7_GPIO_Port, &GPIO_InitStruct);
	
  /*Configure GPIO pins : CH375_WR_Pin CH375_RD_Pin CH375_CS_Pin CH375_A0_Pin */
  GPIO_InitStruct.Pin = CH375_WR_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  HAL_GPIO_Init(CH375_WR_GPIO_Port, &GPIO_InitStruct);
	
  GPIO_InitStruct.Pin = CH375_RD_Pin;
  HAL_GPIO_Init(CH375_RD_GPIO_Port, &GPIO_InitStruct);
	
  GPIO_InitStruct.Pin = CH375_CS_Pin;
  HAL_GPIO_Init(CH375_CS_GPIO_Port, &GPIO_InitStruct);
	
  GPIO_InitStruct.Pin = CH375_A0_Pin;
  HAL_GPIO_Init(CH375_A0_GPIO_Port, &GPIO_InitStruct);
	
  HAL_NVIC_SetPriority(EXTI4_15_IRQn,0,0);
  HAL_NVIC_DisableIRQ(EXTI4_15_IRQn);
}

void Bsp_CH375_USART_Init(void)
{

  HAL_CH375_USART.Instance = USART1;
  HAL_CH375_USART.Init.BaudRate = 115200;
  HAL_CH375_USART.Init.WordLength = UART_WORDLENGTH_8B;
  HAL_CH375_USART.Init.StopBits = UART_STOPBITS_1;
  HAL_CH375_USART.Init.Parity = UART_PARITY_NONE;
  HAL_CH375_USART.Init.Mode = UART_MODE_TX_RX;
  HAL_CH375_USART.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  HAL_CH375_USART.Init.OverSampling = UART_OVERSAMPLING_16;
  HAL_CH375_USART.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  HAL_CH375_USART.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&HAL_CH375_USART) != HAL_OK)
  {
    Error_Handler();
  }
  __HAL_UART_ENABLE(&HAL_CH375_USART);
}

void Bsp_Peripherals_Init(void)
{
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
  /* Configure the system clock */
  Bsp_SystemClock_Init();
  Bsp_GPIO_Init();
  Bsp_CH375_USART_Init();
}

/**
  * @brief  This function is executed in case of error occurrence.
  * @param  None
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler */
  /* User can add his own implementation to report the HAL error return state */
  while(1) 
  {
  }
  /* USER CODE END Error_Handler */ 
}

/***/


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

RecyclerView实现悬浮吸顶

使用RecyclerView实现悬浮吸顶效果

第四十六篇:MAC下使用 Node.js 实现一个 WebServer 服务器

引序: Node.js 采用一系列“非阻塞”库来支持事件循环的方式。Node.js是一个服务器端 JavaScript 解释器 , 也就是说 Node.js 是一个支持 JavaScript 语法编写...

异步新书书讯-8月特刊

8月异步新书,大咖书集中出版,《深度学习》中文版领衔经典著作不断,异步小编整理了13本新书目录: 深度学习 奔跑吧 Linux内核 爱上Android Android Gradle权威指南 机器学习W...

XAMPP各个版本配置

XAMPP这集成环境也很不错 如果你的PHP代码有加密部分,而Zend Guard 5.2加密的代码不能用5.3解密, 只有下载最后一个PHP5.2版本的xampp 1.7.1,并改动...

PHPWAMP自启异常,服务器重启后Apache等服务不会自动重启的原因分析

PHPWAMP是我开发的一款集成了VC的纯绿色集成环境,支持自定义设置php版本、多版本同时运行、强制修改任何环境数据库密码、一键去除域名非80端口、强制卸载任意环境、强制解除环境阻碍、自动定期任务系...
  • Lccee
  • Lccee
  • 2017-02-16 11:28
  • 364727

夕拾朝花——我的2016

夕拾朝花——我的2016走在北航的林间小道上,两边稀稀落落侥幸残留几片枯叶在冷风中瑟瑟抖动,北京 的冬日就是这样,异显凄凉。我常想在纷扰中寻出一点闲静来,然而委实不容易。趁着圣诞的间隙,从快节奏的生活...
  • yzzst
  • yzzst
  • 2016-12-24 23:53
  • 13384

Xamarin android如何调用百度地图入门示例(一)

Xamarin android如何调用百度地图api 进行定位的显示 新建项目 Binging Labrary(android) 下载百度地图 --Android定位SDK Binging L...

xamarin android异步更新UI线程

UI线程简单了解 一些从事web开发的同学,可能对UI线程没有这个概念,没办法,毕竟“UI线程”这个概念只存在一些客户端(window客户端软件、app等)。其实android在子线程中更新UI线程,...

程序员的八重境界

看到一篇有趣的文章The Eight Levels of Programmers。以前似乎看过不少这种程序员的多少个级别、境界,但这篇语言很风趣,而且分类比较细化,让人觉得挺合情合理、无法反驳的。绝大...
  • dc_726
  • dc_726
  • 2017-08-31 04:58
  • 26197

机器学习故事汇-梯度下降

机器学习故事汇-梯度下降【咱们的目标】系列算法讲解旨在用最简单易懂的故事情节帮助大家掌握晦涩无趣的机器学习,适合对数学很头疼的同学们,小板凳走起!今天咱们的故事继续上一次线性回归来说,还不熟悉的小伙伴...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)