STM32F4 在中断时从UART接收数据的问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了STM32F4 在中断时从UART接收数据的问题相关的知识,希望对你有一定的参考价值。

我使用ST32F411VET,为了测试,我用CubeMX创建了一个特定的项目,将USB FTDI转换器从PC连接到STM板上,在轮询模式下,我可以发送和接收数据,但有些字节丢失,而且,阻塞模式对我来说也不好,然后我决定使用中断模式,但我可以从STM向PC发送数据,但不能从PC接收任何字节。main.c

#include "main.h"

__root uint16_t i; 
uint8_t rx_buffer[256];

UART_HandleTypeDef huart2;

void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);

int main(void)
{
  HAL_Init();
  SystemClock_Config();
  MX_GPIO_Init();
  MX_USART2_UART_Init();

  HAL_UART_Transmit(&huart2, (uint8_t*)"Ready...
", 10, 100);
  HAL_UART_Receive_IT(&huart2, (uint8_t *)rx_buffer, 4);

  while (1) {}
}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
  HAL_UART_Receive_IT(&huart2, (uint8_t *)rx_buffer, 4);
  i++;
}



void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

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

static void MX_USART2_UART_Init(void)
{
  huart2.Instance = USART2;
  huart2.Init.BaudRate = 115200;
  huart2.Init.WordLength = UART_WORDLENGTH_8B;
  huart2.Init.StopBits = UART_STOPBITS_1;
  huart2.Init.Parity = UART_PARITY_NONE;
  huart2.Init.Mode = UART_MODE_TX_RX;
  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart2) != HAL_OK)
  {
    Error_Handler();
  }
}

static void MX_GPIO_Init(void)
{
  __HAL_RCC_GPIOD_CLK_ENABLE();
}

void Error_Handler(void)
{
}

#ifdef  USE_FULL_ASSERT
void assert_failed(uint8_t *file, uint32_t line)
{ 
}
#endif /* USE_FULL_ASSERT */

和GPio设置为 stm32f4xx_hal_msp.c

void HAL_UART_MspInit(UART_HandleTypeDef* huart)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(huart->Instance==USART2)
  {
    __HAL_RCC_USART2_CLK_ENABLE();
    __HAL_RCC_GPIOD_CLK_ENABLE();
    PD5     ------> USART2_TX
    PD6     ------> USART2_RX 

    GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
    HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

    HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(USART2_IRQn);
  }
}

我一直在寻找解决方法,但没有找到,你能帮帮我吗,谢谢。

答案

为什么你有一个预约缓冲区,成本4?

HAL_UART_Receive_IT(&huart2, (uint8_t *)rx_buffer, 4);

试着一次只取一个字符,然后分析它们并将它们添加到一个数组中。

HAL_UART_Receive_IT(&huart2, (uint8_t *)rx_buffer, 1);

并尝试这个例子

  uint8_t rx_data = 0;
    uint8_t rx_buffer[256];
    int rx_index = 0;

    HAL_UART_Receive_IT(&huart2, &rx_data, 1);

    void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
     if (huart == &huart2) {
        if (rx_buffer != '
'){
                rx_buffer[rx_index++] = rx_data;
        }else{
                YouFun((char *) rx_buffer);
                rx_index = 0;
                memset(rx_buffer, 0, sizeof(rx_buffer));
        }
        HAL_UART_Receive_IT(&huart2, &rx_data, 1);     // activate receive
     }
    }

以上是关于STM32F4 在中断时从UART接收数据的问题的主要内容,如果未能解决你的问题,请参考以下文章

STM32F4 UART HAL 驱动程序

stm32f4 dma + uart idle + double 调试小记

STM32F0 在 SPI 接收中断时从 STOP 退出

STM32F4 SPI接收中断不起作用

STM32F103在RX中断时无法通过UART接收数据

STM32F4 UART半字寻址