我正在尝试使用 DMA 进行 USART
通信。使用 HAL 库编写的代码可以在
STM32F0 上运行,但不能在 L0 上运行。我在终端获得不相关的数据。
此外,HAL_UARTEx_RxEventCallback 函数可以在 F0 上调用,但不能在 L0 上调用。
这是我的一些代码。
我哪里错了?
- /* USER CODE BEGIN PV */
- #define RxBuf_SIZE 10
- #define MainBuf_SIZE 10
- char TxBuffer[4]={0x12,0x34,0x56,0x78};
- uint8_t RxBuffer[RxBuf_SIZE],MainBuf[MainBuf_SIZE];
- char str[24];
- int sayi=0;
- uint16_t oldPos = 0;
- uint16_t newPos = 0;
- int isOK = 0;
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- /* USER CODE BEGIN PFP */
- /* USER CODE END PFP */
- /* Private user code ---------------------------------------------------------*/
- /* USER CODE BEGIN 0 */
- void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef *huart)
- {
- __NOP();
- }
- void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
- {
- if (huart->Instance == USART1)
- {
- oldPos = newPos; // Update the last position before copying new data
- /* If the data in large and it is about to exceed the buffer size, we have to route it to the start of the buffer
- * This is to maintain the circular buffer
- * The old data in the main buffer will be overlapped
- */
- if (oldPos+Size > MainBuf_SIZE) // If the current position + new data size is greater than the main buffer
- {
- uint16_t datatocopy = MainBuf_SIZE-oldPos; // find out how much space is left in the main buffer
- memcpy ((uint8_t *)MainBuf+oldPos, RxBuffer, datatocopy); // copy data in that remaining space
- oldPos = 0; // point to the start of the buffer
- memcpy ((uint8_t *)MainBuf, (uint8_t *)RxBuffer+datatocopy, (Size-datatocopy)); // copy the remaining data
- newPos = (Size-datatocopy); // update the position
- }
- /* if the current position + new data size is less than the main buffer
- * we will simply copy the data into the buffer and update the position
- */
- else
- {
- memcpy ((uint8_t *)MainBuf+oldPos, RxBuffer, Size);
- newPos = Size+oldPos;
- }
- for(int i=Size;i RxBuffer=0;
- /* start the DMA again */
- HAL_UARTEx_ReceiveToIdle_DMA(&huart1, (uint8_t *) RxBuffer, RxBuf_SIZE);
- __HAL_DMA_DISABLE_IT(&hdma_usart1_rx, DMA_IT_HT);
- }
- /* USER CODE BEGIN 2 */
- HAL_UART_Transmit_DMA(&huart1,(uint8_t*)TxBuffer,sizeof(TxBuffer));
- HAL_UARTEx_ReceiveToIdle_DMA(&huart1, RxBuffer, RxBuf_SIZE);
- __HAL_DMA_DISABLE_IT(&hdma_usart1_rx, DMA_IT_HT);
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- HAL_UART_Transmit_DMA(&huart1,(uint8_t*)TxBuffer,sizeof(TxBuffer));
- HAL_Delay(20);
- }
- /* USER CODE END 3 */