训练营PLSR题目
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

186 Zeilen
5.1 KiB

  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file usart.c
  5. * @brief This file provides code for the configuration
  6. * of the USART instances.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * Copyright (c) 2025 STMicroelectronics.
  11. * All rights reserved.
  12. *
  13. * This software is licensed under terms that can be found in the LICENSE file
  14. * in the root directory of this software component.
  15. * If no LICENSE file comes with this software, it is provided AS-IS.
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "usart.h"
  22. /* USER CODE BEGIN 0 */
  23. #ifdef USE_printf
  24. #include <stdio.h>
  25. #ifdef __GNUC__
  26. #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  27. #else
  28. #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  29. #endif
  30. PUTCHAR_PROTOTYPE
  31. {
  32. HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 100);
  33. return ch;
  34. }
  35. #endif
  36. uint8_t RX1DataTemp = 0;
  37. uint8_t RX1Data[RX_BUFFER_SIZE] = {0};
  38. uint8_t RX1Flag = 0;
  39. volatile uint16_t RX1Count = 0;
  40. /* USER CODE END 0 */
  41. UART_HandleTypeDef huart1;
  42. DMA_HandleTypeDef hdma_usart1_rx;
  43. DMA_HandleTypeDef hdma_usart1_tx;
  44. /* USART1 init function */
  45. void MX_USART1_UART_Init(void)
  46. {
  47. /* USER CODE BEGIN USART1_Init 0 */
  48. /* USER CODE END USART1_Init 0 */
  49. /* USER CODE BEGIN USART1_Init 1 */
  50. /* USER CODE END USART1_Init 1 */
  51. huart1.Instance = USART1;
  52. huart1.Init.BaudRate = 9600;
  53. huart1.Init.WordLength = UART_WORDLENGTH_9B;
  54. huart1.Init.StopBits = UART_STOPBITS_1;
  55. huart1.Init.Parity = UART_PARITY_ODD;
  56. huart1.Init.Mode = UART_MODE_TX_RX;
  57. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  58. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  59. if (HAL_UART_Init(&huart1) != HAL_OK)
  60. {
  61. Error_Handler();
  62. }
  63. /* USER CODE BEGIN USART1_Init 2 */
  64. __HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);
  65. HAL_UART_Receive_DMA(&huart1, RX1Data, RX_BUFFER_SIZE);
  66. /* USER CODE END USART1_Init 2 */
  67. }
  68. void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
  69. {
  70. GPIO_InitTypeDef GPIO_InitStruct = {0};
  71. if(uartHandle->Instance==USART1)
  72. {
  73. /* USER CODE BEGIN USART1_MspInit 0 */
  74. /* USER CODE END USART1_MspInit 0 */
  75. /* USART1 clock enable */
  76. __HAL_RCC_USART1_CLK_ENABLE();
  77. __HAL_RCC_GPIOA_CLK_ENABLE();
  78. /**USART1 GPIO Configuration
  79. PA9 ------> USART1_TX
  80. PA10 ------> USART1_RX
  81. */
  82. GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10;
  83. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  84. GPIO_InitStruct.Pull = GPIO_NOPULL;
  85. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  86. GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
  87. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  88. /* USART1 DMA Init */
  89. /* USART1_RX Init */
  90. hdma_usart1_rx.Instance = DMA2_Stream2;
  91. hdma_usart1_rx.Init.Channel = DMA_CHANNEL_4;
  92. hdma_usart1_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
  93. hdma_usart1_rx.Init.PeriphInc = DMA_PINC_DISABLE;
  94. hdma_usart1_rx.Init.MemInc = DMA_MINC_ENABLE;
  95. hdma_usart1_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  96. hdma_usart1_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  97. hdma_usart1_rx.Init.Mode = DMA_CIRCULAR;
  98. hdma_usart1_rx.Init.Priority = DMA_PRIORITY_MEDIUM;
  99. hdma_usart1_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  100. if (HAL_DMA_Init(&hdma_usart1_rx) != HAL_OK)
  101. {
  102. Error_Handler();
  103. }
  104. __HAL_LINKDMA(uartHandle,hdmarx,hdma_usart1_rx);
  105. /* USART1_TX Init */
  106. hdma_usart1_tx.Instance = DMA2_Stream7;
  107. hdma_usart1_tx.Init.Channel = DMA_CHANNEL_4;
  108. hdma_usart1_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
  109. hdma_usart1_tx.Init.PeriphInc = DMA_PINC_DISABLE;
  110. hdma_usart1_tx.Init.MemInc = DMA_MINC_ENABLE;
  111. hdma_usart1_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  112. hdma_usart1_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  113. hdma_usart1_tx.Init.Mode = DMA_NORMAL;
  114. hdma_usart1_tx.Init.Priority = DMA_PRIORITY_LOW;
  115. hdma_usart1_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  116. if (HAL_DMA_Init(&hdma_usart1_tx) != HAL_OK)
  117. {
  118. Error_Handler();
  119. }
  120. __HAL_LINKDMA(uartHandle,hdmatx,hdma_usart1_tx);
  121. /* USART1 interrupt Init */
  122. HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
  123. HAL_NVIC_EnableIRQ(USART1_IRQn);
  124. /* USER CODE BEGIN USART1_MspInit 1 */
  125. /* USER CODE END USART1_MspInit 1 */
  126. }
  127. }
  128. void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
  129. {
  130. if(uartHandle->Instance==USART1)
  131. {
  132. /* USER CODE BEGIN USART1_MspDeInit 0 */
  133. /* USER CODE END USART1_MspDeInit 0 */
  134. /* Peripheral clock disable */
  135. __HAL_RCC_USART1_CLK_DISABLE();
  136. /**USART1 GPIO Configuration
  137. PA9 ------> USART1_TX
  138. PA10 ------> USART1_RX
  139. */
  140. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);
  141. /* USART1 DMA DeInit */
  142. HAL_DMA_DeInit(uartHandle->hdmarx);
  143. HAL_DMA_DeInit(uartHandle->hdmatx);
  144. /* USART1 interrupt Deinit */
  145. HAL_NVIC_DisableIRQ(USART1_IRQn);
  146. /* USER CODE BEGIN USART1_MspDeInit 1 */
  147. /* USER CODE END USART1_MspDeInit 1 */
  148. }
  149. }
  150. /* USER CODE BEGIN 1 */
  151. /* USER CODE END 1 */