c - STM32F3 UART接收中断数据在从Arduino接收到第一组数据后挂起

标签 c uart interrupt-handling stm32 arduino-uno

作为 ARM 微 Controller 的初学者,我使用 STM32F3 Discovery Kit 与 Arduino 进行通信。我使用 EWARM 进行编码,使用 Putty 进行串行终端。

我在stm32f3 UART2上做了回声测试。数据正确显示在串口终端上,因此我确认了uart通信正常工作。

然后,我尝试将数据从arduino传输到stm32f3,但是stm32f3上的uart在第一组数据之后挂起(第一组数据正确显示,其中包含一些垃圾字符)。我已经被困了很长一段时间了。

下面是我用于 UART 的编码。波特率是9600。有人可以帮忙吗?

#define LINEMAX 15 // Maximal allowed/expected line length

volatile char line_buffer[LINEMAX + 1]; // Holding buffer with space for terminating NUL
volatile int line_valid = 0;

/**
  * @brief  Initialize USART2 for PD6 (USART2_RX) and PD5 (USART2_TX)
  *         used for receiving mouse data from arduino
  * @param  baudrate; by default is 9600
  * @retval None
  * link: http://eliaselectronics.com/stm32f4-usart-example-with-interrupt/
  */
void init_USART2(int baudrate)
{
      USART_InitTypeDef USART_InitStructure;    // this is for the GPIO pins used as TX and R
      GPIO_InitTypeDef GPIO_InitStructure;      // this is for the USART1 initilization
      NVIC_InitTypeDef NVIC_InitStructure;      // this is used to configure the NVIC (nested vector interrupt controller)

      /* Configure USART1 pins:  Rx and Tx ----------------------------*/
      RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOD, ENABLE);
      GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_5 | GPIO_Pin_6;
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
      GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
      GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
      GPIO_Init(GPIOD, &GPIO_InitStructure);   

      GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_7);
      GPIO_PinAFConfig(GPIOD, GPIO_PinSource6, GPIO_AF_7);

      /* Configure USART1 pins:  --------------------------------------*/
      RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

      USART_DeInit(USART2);
      USART_InitStructure.USART_BaudRate = baudrate;
      USART_InitStructure.USART_WordLength = USART_WordLength_8b;
      USART_InitStructure.USART_StopBits = USART_StopBits_1;
      USART_InitStructure.USART_Parity = USART_Parity_No;
      USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
      USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
      USART_Init(USART2,&USART_InitStructure);

      USART_Cmd(USART2, ENABLE);

      /* Here the USART2 receive interrupt is enabled
       * and the interrupt controller is configured
       * to jump to the USART2_IRQHandler() function
       * if the USART2 receive interrupt occurs
       */
      USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); // enable the USART2 receive interrupt

      NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;             // we want to configure the USART1 interrupts
      NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;         // this sets the priority group of the USART1 interrupts
      NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;        // this sets the subpriority inside the group
      NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;           // the USART2 interrupts are globally enabled
      NVIC_Init(&NVIC_InitStructure);                       // the properties are passed to the NVIC_Init function which takes care of the low level stuff

      // finally this enables the complete USART2 peripheral
      USART_Cmd(USART2, ENABLE);
}

void serial_prints (USART_TypeDef* USARTx, volatile char *buffer)
{
    /* transmit till NULL character is encountered */
    while(*buffer)
    {
      USART_SendData(USARTx, *buffer++);
      while (USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET);
      delay_us(5);
    }
}

void USART2_IRQHandler(void)
{
  static char rx_buffer[LINEMAX];   // Local holding buffer to build line
  static int rx_index = 0;

  if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) // Received character?
  {
    char rx =  USART_ReceiveData(USART2);

    if ((rx == '\r') || (rx == '\n')) // Is this an end-of-line condition, either will suffice?
    {
        if (rx_index != 0) // Line has some content?
        { 
            memcpy((void *)line_buffer, rx_buffer, rx_index); // Copy to static line buffer from dynamic receive buffer
            line_buffer[rx_index] = 0; // Add terminating NUL
            line_valid = 1; // flag new line valid for processing
            serial_prints(USART2, rx_buffer);    
            rx_index = 0; // Reset content pointer
        }
    }
    else
    {
        if (rx_index == LINEMAX) // If overflows pull back to start
            rx_index = 0;

          rx_buffer[rx_index++] = rx; // Copy to buffer and increment
    }
  }
}

对于arduino,我使用以下代码,数据在串行终端中正确显示:

printf("%d, %d \n", X, Y);

最佳答案

将终止空字符添加到 line_buffer 中,然后将 rx_buffer 传递给函数 serial_prints:

line_buffer[rx_index] = 0; // Add terminating NUL
line_valid = 1; // flag new line valid for processing
serial_prints(USART2, rx_buffer);    
rx_index = 0; // Reset content pointer

这会导致您将在函数 serial_prints 中迭代超过 buffer 的长度,因为 buffer 不会以 null 终止:

while(*buffer)
{
  USART_SendData(USARTx, *buffer++);
  while (USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET);
  delay_us(5);
}

在侧节点上,您还应该使用中断来发送字符(TX 空中断)。在中断服务例程中主动等待 UART 字符发送将会大大减慢您的程序速度。

关于c - STM32F3 UART接收中断数据在从Arduino接收到第一组数据后挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23757857/

相关文章:

embedded - 如何调试 STM32 设备中的意外复位?

c - C中常量的内存地址

c - valgrind 需要很长时间才能发现内存泄漏,但程序在没有 valgrind 的情况下运行需要几秒钟

c - 内核函数中断失败但 ioctl 失败

c - UART pic18 到 pic18

c++ - 为什么我的STM32F7每43秒才中断一次?

c - 我怎样才能延迟 Linux 中断处理程序(我知道 sleep 通常是不可能的)

c - 枚举范围

c - PIC 18F46K22,微型 : 'Multitasking' simple and time consuming task

c - 如何用HAL_UART接收数据?