基质喷涂
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
3.0 KiB

  1. //
  2. // Created by iflyt on 2025/3/19.
  3. //
  4. #include "uart_cmd.h"
  5. UART_HandleTypeDef huart2;
  6. DMA_HandleTypeDef hdma_usart2_rx;
  7. DMA_HandleTypeDef hdma_usart2_tx;
  8. void CMD_UART_Init() {
  9. __HAL_RCC_USART2_CLK_ENABLE();
  10. __HAL_RCC_GPIOD_CLK_ENABLE();
  11. huart2.Instance = USART2;
  12. huart2.Init.BaudRate = 115200;
  13. huart2.Init.WordLength = UART_WORDLENGTH_8B;
  14. huart2.Init.StopBits = UART_STOPBITS_1;
  15. huart2.Init.Parity = UART_PARITY_NONE;
  16. huart2.Init.Mode = UART_MODE_TX_RX;
  17. huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  18. huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  19. if (HAL_UART_Init(&huart2) != HAL_OK)
  20. {
  21. Error_Handler();
  22. }
  23. GPIO_InitTypeDef GPIO_InitStruct = {0};
  24. /**USART2 GPIO Configuration
  25. PD5 ------> USART2_TX
  26. PD6 ------> USART2_RX
  27. */
  28. GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6;
  29. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  30. GPIO_InitStruct.Pull = GPIO_NOPULL;
  31. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  32. GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
  33. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  34. /* USART2 DMA Init */
  35. /* USART2_RX Init */
  36. hdma_usart2_rx.Instance = DMA1_Stream5;
  37. hdma_usart2_rx.Init.Channel = DMA_CHANNEL_4;
  38. hdma_usart2_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
  39. hdma_usart2_rx.Init.PeriphInc = DMA_PINC_DISABLE;
  40. hdma_usart2_rx.Init.MemInc = DMA_MINC_ENABLE;
  41. hdma_usart2_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  42. hdma_usart2_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  43. hdma_usart2_rx.Init.Mode = DMA_NORMAL;
  44. hdma_usart2_rx.Init.Priority = DMA_PRIORITY_LOW;
  45. hdma_usart2_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  46. if (HAL_DMA_Init(&hdma_usart2_rx) != HAL_OK)
  47. {
  48. Error_Handler();
  49. }
  50. __HAL_LINKDMA(&huart2,hdmarx,hdma_usart2_rx);
  51. /* USART2_TX Init */
  52. hdma_usart2_tx.Instance = DMA1_Stream6;
  53. hdma_usart2_tx.Init.Channel = DMA_CHANNEL_4;
  54. hdma_usart2_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
  55. hdma_usart2_tx.Init.PeriphInc = DMA_PINC_DISABLE;
  56. hdma_usart2_tx.Init.MemInc = DMA_MINC_ENABLE;
  57. hdma_usart2_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  58. hdma_usart2_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  59. hdma_usart2_tx.Init.Mode = DMA_NORMAL;
  60. hdma_usart2_tx.Init.Priority = DMA_PRIORITY_LOW;
  61. hdma_usart2_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  62. if (HAL_DMA_Init(&hdma_usart2_tx) != HAL_OK)
  63. {
  64. Error_Handler();
  65. }
  66. __HAL_LINKDMA(&huart2,hdmatx,hdma_usart2_tx);
  67. /* USART2 interrupt Init */
  68. HAL_NVIC_SetPriority(USART2_IRQn, 5, 0);
  69. HAL_NVIC_EnableIRQ(USART2_IRQn);
  70. }
  71. void CMD_DMA_Init() {
  72. /* DMA controller clock enable */
  73. __HAL_RCC_DMA1_CLK_ENABLE();
  74. /* DMA interrupt init */
  75. /* DMA1_Stream5_IRQn interrupt configuration */
  76. HAL_NVIC_SetPriority(DMA1_Stream5_IRQn, 5, 0);
  77. HAL_NVIC_EnableIRQ(DMA1_Stream5_IRQn);
  78. /* DMA1_Stream6_IRQn interrupt configuration */
  79. HAL_NVIC_SetPriority(DMA1_Stream6_IRQn, 5, 0);
  80. HAL_NVIC_EnableIRQ(DMA1_Stream6_IRQn);
  81. }