stm32f429-discovery +cubemx + USB转虚拟串口 研究

系统管理员 2022-09-24 04:26 476阅读 0赞

在实验过stm32f302的USB转串口之后,仍对缺少上拉电阻或者说不能配置上拉的GPIO感到不安。

所以此次直接更新为stm32f429的开发板,贵就是有贵的好处,配置方法不拖泥带水。

按步来吧,先介绍版本:stm32cubemx version 4.15.1 STM32Cube v1.0 ; Firmware Package for family STM32F4 1.12.0; Keil 4.73

以下配置情况仅供参考,意外情况可以提问。

1.配置管脚,一开始用USART2,应该是引脚复用,结果该口没法用,改用USART1就OK. 还开了两个LED作为通讯指示。

Center

Center 1

2.配置时钟

Center 2

3.串口1的配置

Center 3

Center 4

使用DMA作为发送模式妥妥的省资源。

20160725153352097

4.USB底层配置

20160725153429988

5.中断配置,需要打开DMA2中断

20160725153417175

6.USB中间层配置

20160725153443211

20160725153456957

20160725153508660

7.代码生成配置

20160725153519728

20160725153529926

8.关键代码示例

main函数中的关键定义及引用。 csdn 的代码好坑 本来都是标红的 都被

  1. <span style="font-size:18px;color:#ff0000;"><span style="color:#ff0000;"> <span style="font-family: Arial, Helvetica, sans-serif;"></span> 包围了。自己小心看吧</span></span>
  2. /* Includes ------------------------------------------------------------------*/
  3. #include "stm32f4xx_hal.h"
  4. #include "dma.h"
  5. #include "usart.h"
  6. #include "usb_device.h"
  7. #include "gpio.h"
  8. /* USER CODE BEGIN Includes */
  9. <span style="color:#ff0000;">#include "usbd_cdc_if.h"</span>
  10. /* USER CODE END Includes */
  11. /* Private variables ---------------------------------------------------------*/
  12. /* USER CODE BEGIN PV */
  13. /* Private variables ---------------------------------------------------------*/
  14. uint8_t aRxBuffer[1];
  15. uint8_t aTxBuffer[16];
  16. //extern uint8_t UserRxBufferFS[APP_RX_DATA_SIZE];
  17. /* USER CODE END PV */
  18. /* Private function prototypes -----------------------------------------------*/
  19. void SystemClock_Config(void);
  20. void Error_Handler(void);
  21. /* USER CODE BEGIN PFP */
  22. /* Private function prototypes -----------------------------------------------*/
  23. /* USER CODE END PFP */
  24. /* USER CODE BEGIN 0 */
  25. /* USER CODE END 0 */
  26. int main(void)
  27. {
  28. /* USER CODE BEGIN 1 */
  29. /* USER CODE END 1 */
  30. /* MCU Configuration----------------------------------------------------------*/
  31. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  32. HAL_Init();
  33. /* Configure the system clock */
  34. SystemClock_Config();
  35. /* Initialize all configured peripherals */
  36. MX_GPIO_Init();
  37. MX_DMA_Init();
  38. MX_USB_DEVICE_Init();
  39. MX_USART2_UART_Init();
  40. MX_USART1_UART_Init();
  41. /* USER CODE BEGIN 2 */
  42. HAL_Delay(500);
  43. /* USER CODE END 2 */
  44. /* Infinite loop */
  45. /* USER CODE BEGIN WHILE */
  46. while (1)
  47. {
  48. /* USER CODE END WHILE */
  49. /* USER CODE BEGIN 3 */
  50. <span style="color:#ff0000;">if(HAL_UART_Receive_IT(&huart1,&aRxBuffer[0], 1)== HAL_OK)
  51. {
  52. HAL_GPIO_TogglePin(LED_GREEN_GPIO_Port,LED_GREEN_Pin);//灯儿,不想用的屏蔽掉就OK
  53. CDC_Transmit_FS(&aRxBuffer[0],1);
  54. }</span>
  55. // HAL_UART_Transmit(&huart1, test, sizeof(test), 100);
  56. // HAL_Delay(500);
  57. }
  58. /* USER CODE END 3 */
  59. }
  1. usbd_cdc_if.c文件中的关键配置,标红部分为新加配置。
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "usbd_cdc_if.h"
  3. /* USER CODE BEGIN INCLUDE */
  4. /* USER CODE END INCLUDE */
  5. /** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY
  6. * @{
  7. */
  8. /** @defgroup USBD_CDC
  9. * @brief usbd core module
  10. * @{
  11. */
  12. /** @defgroup USBD_CDC_Private_TypesDefinitions
  13. * @{
  14. */
  15. /* USER CODE BEGIN PRIVATE_TYPES */
  16. extern UART_HandleTypeDef huart2; //这个没启用成功,还赖在这
  17. <span style="color:#ff0000;">extern UART_HandleTypeDef huart1;</span>
  18. /* The following structures groups all needed parameters to be configured for the
  19. ComPort. These parameters can modified on the fly by the host through CDC class
  20. command class requests. */
  21. <span style="color:#ff0000;">typedef struct
  22. {
  23. uint32_t bitrate;
  24. uint8_t format;
  25. uint8_t paritytype;
  26. uint8_t datatype;
  27. }LINE_CODING;
  28. </span>
  29. //mch settings for usb vcp
  30. <span style="color:#ff0000;">LINE_CODING linecoding =
  31. {
  32. 9600, /* baud rate*/
  33. 0x00, /* stop bits-1*/
  34. 0x00, /* parity - none*/
  35. 0x08 /* nb. of bits 8*/
  36. };</span>
  37. <span style="color:#ff0000;">extern uint8_t aRxBuffer[16];</span>
  38. extern uint8_t aTxBuffer[16];
  39. /* USER CODE END PRIVATE_TYPES */
  40. /**
  41. * @}
  42. */
  43. /** @defgroup USBD_CDC_Private_Defines
  44. * @{
  45. */
  46. /* USER CODE BEGIN PRIVATE_DEFINES */
  47. /* Define size for the receive and transmit buffer over CDC */
  48. /* It's up to user to redefine and/or remove those define */
  49. <span style="color:#ff0000;">#define APP_RX_DATA_SIZE 254
  50. #define APP_TX_DATA_SIZE 254</span>
  51. /* USER CODE END PRIVATE_DEFINES */
  52. /**
  53. * @}
  54. */
  55. /** @defgroup USBD_CDC_Private_Macros
  56. * @{
  57. */
  58. /* USER CODE BEGIN PRIVATE_MACRO */
  59. /* USER CODE END PRIVATE_MACRO */
  60. /**
  61. * @}
  62. */
  63. /** @defgroup USBD_CDC_Private_Variables
  64. * @{
  65. */
  66. /* Create buffer for reception and transmission */
  67. /* It's up to user to redefine and/or remove those define */
  68. /* Received Data over USB are stored in this buffer */
  69. uint8_t UserRxBufferFS[APP_RX_DATA_SIZE];
  70. /* Send Data over USB CDC are stored in this buffer */
  71. uint8_t UserTxBufferFS[APP_TX_DATA_SIZE];
  72. /* USER CODE BEGIN PRIVATE_VARIABLES */
  73. /* USER CODE END PRIVATE_VARIABLES */
  74. /**
  75. * @}
  76. */
  77. /** @defgroup USBD_CDC_IF_Exported_Variables
  78. * @{
  79. */
  80. extern USBD_HandleTypeDef hUsbDeviceFS;
  81. /* USER CODE BEGIN EXPORTED_VARIABLES */
  82. /* USER CODE END EXPORTED_VARIABLES */
  83. /**
  84. * @}
  85. */
  86. /** @defgroup USBD_CDC_Private_FunctionPrototypes
  87. * @{
  88. */
  89. static int8_t CDC_Init_FS (void);
  90. static int8_t CDC_DeInit_FS (void);
  91. static int8_t CDC_Control_FS (uint8_t cmd, uint8_t* pbuf, uint16_t length);
  92. static int8_t CDC_Receive_FS (uint8_t* pbuf, uint32_t *Len);
  93. /* USER CODE BEGIN PRIVATE_FUNCTIONS_DECLARATION */
  94. /* USER CODE END PRIVATE_FUNCTIONS_DECLARATION */
  95. /**
  96. * @}
  97. */
  98. USBD_CDC_ItfTypeDef USBD_Interface_fops_FS =
  99. {
  100. CDC_Init_FS,
  101. CDC_DeInit_FS,
  102. CDC_Control_FS,
  103. CDC_Receive_FS
  104. };
  105. /* Private functions ---------------------------------------------------------*/
  106. /**
  107. * @brief CDC_Init_FS
  108. * Initializes the CDC media low layer over the FS USB IP
  109. * @param None
  110. * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
  111. */
  112. static int8_t CDC_Init_FS(void)
  113. {
  114. /* USER CODE BEGIN 3 */
  115. /* Set Application Buffers */
  116. USBD_CDC_SetTxBuffer(&hUsbDeviceFS, UserTxBufferFS, 0);
  117. USBD_CDC_SetRxBuffer(&hUsbDeviceFS, UserRxBufferFS);
  118. return (USBD_OK);
  119. /* USER CODE END 3 */
  120. }
  121. /**
  122. * @brief CDC_DeInit_FS
  123. * DeInitializes the CDC media low layer
  124. * @param None
  125. * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
  126. */
  127. static int8_t CDC_DeInit_FS(void)
  128. {
  129. /* USER CODE BEGIN 4 */
  130. return (USBD_OK);
  131. /* USER CODE END 4 */
  132. }
  133. /**
  134. * @brief CDC_Control_FS
  135. * Manage the CDC class requests
  136. * @param cmd: Command code
  137. * @param pbuf: Buffer containing command data (request parameters)
  138. * @param length: Number of data to be sent (in bytes)
  139. * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
  140. */
  141. static int8_t CDC_Control_FS (uint8_t cmd, uint8_t* pbuf, uint16_t length)
  142. {
  143. /* USER CODE BEGIN 5 */
  144. switch (cmd)
  145. {
  146. case CDC_SEND_ENCAPSULATED_COMMAND:
  147. break;
  148. case CDC_GET_ENCAPSULATED_RESPONSE:
  149. break;
  150. case CDC_SET_COMM_FEATURE:
  151. break;
  152. case CDC_GET_COMM_FEATURE:
  153. break;
  154. case CDC_CLEAR_COMM_FEATURE:
  155. break;
  156. /*******************************************************************************/
  157. /* Line Coding Structure */
  158. /*-----------------------------------------------------------------------------*/
  159. /* Offset | Field | Size | Value | Description */
  160. /* 0 | dwDTERate | 4 | Number |Data terminal rate, in bits per second*/
  161. /* 4 | bCharFormat | 1 | Number | Stop bits */
  162. /* 0 - 1 Stop bit */
  163. /* 1 - 1.5 Stop bits */
  164. /* 2 - 2 Stop bits */
  165. /* 5 | bParityType | 1 | Number | Parity */
  166. /* 0 - None */
  167. /* 1 - Odd */
  168. /* 2 - Even */
  169. /* 3 - Mark */
  170. /* 4 - Space */
  171. /* 6 | bDataBits | 1 | Number Data bits (5, 6, 7, 8 or 16). */
  172. /*******************************************************************************/
  173. <span style="color:#ff0000;"> case CDC_SET_LINE_CODING:
  174. linecoding.bitrate = (uint32_t)(pbuf[0] | (pbuf[1] << 8) | (pbuf[2] << 16) | (pbuf[3] << 24));
  175. linecoding.format = pbuf[4];
  176. linecoding.paritytype = pbuf[5];
  177. linecoding.datatype = pbuf[6];
  178. huart1.Init.BaudRate = linecoding.bitrate;
  179. switch (linecoding.format)
  180. {
  181. case 0 : huart1.Init.StopBits = UART_STOPBITS_1; break;
  182. case 1 : huart1.Init.StopBits = UART_STOPBITS_2; break;
  183. // case 2 : huart2.Init.StopBits = UART_STOPBITS_2; break;//有些位吧429库里没发现 ,奇怪
  184. }
  185. switch (linecoding.paritytype)
  186. {
  187. case 0 : huart1.Init.Parity = UART_PARITY_NONE; break;
  188. case 1 : huart1.Init.Parity = UART_PARITY_ODD; break;
  189. case 2 : huart1.Init.Parity = UART_PARITY_EVEN; break;
  190. }
  191. //UART_WORDLENGTH_7B
  192. switch (linecoding.datatype)
  193. {
  194. case 9 : huart1.Init.WordLength = UART_WORDLENGTH_9B; break;
  195. case 8 : huart1.Init.WordLength = UART_WORDLENGTH_8B; break;
  196. }
  197. HAL_UART_Init(&huart1) ;
  198. /* HAL_RS485Ex_Init(&huart2, UART_DE_POLARITY_HIGH, 0, 0); //这个地方就露馅了,本来是给485用的,后来就改成串口了,当然有兴趣的童鞋可以继续配置成485模式,记得引脚等。详细信息可以参考 http://www.stm32cube.com/article/120 */
  199. break;
  200. case CDC_GET_LINE_CODING:
  201. pbuf[0] = (uint8_t)(linecoding.bitrate);
  202. pbuf[1] = (uint8_t)(linecoding.bitrate >> 8);
  203. pbuf[2] = (uint8_t)(linecoding.bitrate >> 16);
  204. pbuf[3] = (uint8_t)(linecoding.bitrate >> 24);
  205. pbuf[4] = linecoding.format;
  206. pbuf[5] = linecoding.paritytype;
  207. pbuf[6] = linecoding.datatype;
  208. break;</span>
  209. case CDC_SET_CONTROL_LINE_STATE:
  210. break;
  211. case CDC_SEND_BREAK:
  212. break;
  213. default:
  214. break;
  215. }
  216. return (USBD_OK);
  217. /* USER CODE END 5 */
  218. }
  219. /**
  220. * @brief CDC_Receive_FS
  221. * Data received over USB OUT endpoint are sent over CDC interface
  222. * through this function.
  223. *
  224. * @note
  225. * This function will block any OUT packet reception on USB endpoint
  226. * untill exiting this function. If you exit this function before transfer
  227. * is complete on CDC interface (ie. using DMA controller) it will result
  228. * in receiving more data while previous ones are still not sent.
  229. *
  230. * @param Buf: Buffer of data to be received
  231. * @param Len: Number of data received (in bytes)
  232. * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
  233. */
  234. static int8_t CDC_Receive_FS (uint8_t* Buf, uint32_t *Len)
  235. {
  236. /* USER CODE BEGIN 6 */
  237. USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &Buf[0]);
  238. USBD_CDC_ReceivePacket(&hUsbDeviceFS);
  239. <span style="color:#ff0000;"> HAL_UART_Transmit_DMA(&huart1, Buf, *Len);
  240. HAL_GPIO_TogglePin(LED_RED_GPIO_Port,LED_RED_Pin);//灯儿</span>
  241. return (USBD_OK);
  242. /* USER CODE END 6 */
  243. }
  244. /**
  245. * @brief CDC_Transmit_FS
  246. * Data send over USB IN endpoint are sent over CDC interface
  247. * through this function.
  248. * @note
  249. *
  250. *
  251. * @param Buf: Buffer of data to be send
  252. * @param Len: Number of data to be send (in bytes)
  253. * @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL or USBD_BUSY
  254. */
  255. uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len)
  256. {
  257. uint8_t result = USBD_OK;
  258. /* USER CODE BEGIN 7 */
  259. USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*)hUsbDeviceFS.pClassData;
  260. if (hcdc->TxState != 0){
  261. return USBD_BUSY;
  262. }
  263. USBD_CDC_SetTxBuffer(&hUsbDeviceFS, Buf, Len);
  264. result = USBD_CDC_TransmitPacket(&hUsbDeviceFS);
  265. /* USER CODE END 7 */
  266. return result;
  267. }

最后说一下,在115200波特率的时候会丢帧,可能跟一开始 的72MHz时钟有关。未验证
上一张实物图吧,光说不练假把式

20160725160135516

20160725160541706

小玩至此结束,有疑问请留言。

发表评论

表情:
评论列表 (有 0 条评论,476人围观)

还没有评论,来说两句吧...

相关阅读

    相关 stm32f429学习记录

    1.串口uart包含三个知识点: (1)物理层(电气层:接口决定 (2)数据格式(数据层:芯片决定) (3)通信协议(协议层:程序决定,) -----------