回答

收藏

[评测分享] 【灵动Motor-DK电机控制板】2、串口printf输出及中断接收

#板卡评测 #板卡评测 1885 人阅读 | 0 人回复 | 2023-07-22

本帖最后由 流水源 于 2023-7-24 13:50 编辑

   本次实验测试一下串口的发送接收。方便后面输出调试信息。
首先找到串口端口,PA11和PA12。



找到PA11和PA12的复用功能表,可以看到是AF5组复用功能对应USART1.


下面初始化串口端口:
  1. void PLATFORM_InitConsole(uint32_t Baudrate)
  2. {
  3.     GPIO_InitTypeDef  GPIO_InitStruct;
  4.     USART_InitTypeDef USART_InitStruct;

  5.     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

  6.     GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_5);
  7.     GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_5);

  8.     GPIO_StructInit(&GPIO_InitStruct);
  9.     GPIO_InitStruct.GPIO_Pin   = GPIO_Pin_11 | GPIO_Pin_12;
  10.     GPIO_InitStruct.GPIO_Speed = GPIO_Speed_High;
  11.     GPIO_InitStruct.GPIO_Mode  = GPIO_Mode_AF_PP;
  12.     GPIO_Init(GPIOA, &GPIO_InitStruct);
  13.    
  14.    
  15.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART1, ENABLE);

  16.     USART_StructInit(&USART_InitStruct);
  17.     USART_InitStruct.USART_BaudRate   = Baudrate;
  18.     USART_InitStruct.USART_WordLength = USART_WordLength_8b;
  19.     USART_InitStruct.USART_StopBits   = USART_StopBits_1;
  20.     USART_InitStruct.USART_Parity     = USART_Parity_No;
  21.     USART_InitStruct.USART_Mode       = USART_Mode_Tx|USART_Mode_Rx;
  22.     USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  23.     USART_Init(USART1, &USART_InitStruct);

  24.     USART_Cmd(USART1, ENABLE);
  25.    
  26.     //使能UARTx RC中断
  27.     USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  28.     //优先级,无优先级分组
  29.     NVIC_SetPriority(USART1_IRQn, 0);
  30.     //UARTx中断使能
  31.     NVIC_EnableIRQ(USART1_IRQn);
  32. }
复制代码
映射到printf输出:
  1. int stdout_putchar (int ch)
  2. {
  3.     USART_SendData(USART1, (uint8_t)ch);
  4.     while (RESET == USART_GetFlagStatus(USART1, USART_FLAG_TC)){;}
  5.     return (ch);
  6. }

  7. int fputc(int ch,FILE *f)
  8. {
  9.     return stdout_putchar(ch);
  10. }
复制代码
中断接收数据,通过FIFO缓存:
  1. typedef struct fifo_buffer
  2. {
  3.     volatile uint32_t    read_i;
  4.     volatile uint32_t    write_i;
  5.     uint8_t     buff[128];
  6. }fifo_buffer;
  7. extern fifo_buffer  shell_uart_rx;

  8. fifo_buffer  shell_uart_rx=
  9. {
  10.     .read_i = 0,
  11.     .write_i = 0,
  12. };

  13. void USART1_IRQHandler(void)
  14. {
  15.     uint32_t statusFlag;
  16.     uint32_t ch;
  17.    
  18.     statusFlag = USART1->SR;
  19.     if((statusFlag & USART_IT_RXNE) != 0)
  20.     {
  21.         ch = USART_ReceiveData(USART1);
  22.         if(((shell_uart_rx.write_i+1)&0x7f) != shell_uart_rx.read_i)
  23.         {
  24.             shell_uart_rx.buff[shell_uart_rx.write_i++] = ch & 0xff;
  25.             shell_uart_rx.write_i &= 0x7f;
  26.         }
  27.     }
  28.     USART1->SR &= ~statusFlag;
  29. }
复制代码
测试接收数据,将接收的数据重复发送出来。
  1. void shell_usart_loop(void)
  2. {
  3.     if(shell_uart_rx.read_i != shell_uart_rx.write_i)
  4.     {
  5.         stdout_putchar(shell_uart_rx.buff[shell_uart_rx.read_i++]);
  6.         shell_uart_rx.read_i &= 0x7f;
  7.     }
  8. }
复制代码

  1. void GPIO_LED_Toggle_Sample(void)
  2. {
  3.     printf("\r\nTest %s", __FUNCTION__);

  4.     GPIO_Configure();

  5.     while (1)
  6.     {
  7.         GPIO_IO_Toggle(GPIOB, GPIO_Pin_9);
  8.         shell_usart_loop(); //串口接收并发送
  9.         PLATFORM_DelayMS(100);
  10.     }
  11. }
复制代码


分享到:
回复

使用道具 举报

您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /3 下一条