查看: 971|回复: 0

[项目分享] 【N32G430开发板】+ usart shell移植

[复制链接]
  • TA的每日心情
    奋斗
    2024-4-10 11:17
  • 签到天数: 768 天

    连续签到: 1 天

    [LV.10]以坛为家III

    发表于 2023-9-14 15:28:02 | 显示全部楼层 |阅读模式
    分享到:
       上次玩了一下N32G430开发板点亮了led灯,这次就试试串口。实现串口的发送接收。然后尝试移植了nr_micro_shell。
    移植shell首先要把串口发送接收搞定。
    下面就是我使用USART1测试,初始化USART1如下:使用中断接收串口数据,这样就不会丢失数据了。
    1. void uart_init(void)
    2. {
    3.     GPIO_InitType GPIO_InitStructure;
    4.     USART_InitType USART_InitStructure;
    5.     NVIC_InitType NVIC_InitStructure;

    6.     RCC_AHB_Peripheral_Clock_Enable(RCC_AHB_PERIPH_GPIOA);
    7.     RCC_APB2_Peripheral_Clock_Enable(RCC_APB2_PERIPH_AFIO | RCC_APB2_PERIPH_USART1);

    8.     GPIO_Structure_Initialize(&GPIO_InitStructure);
    9.     GPIO_InitStructure.Pin            = GPIO_PIN_9;
    10.     GPIO_InitStructure.GPIO_Mode      = GPIO_MODE_AF_PP;
    11.     GPIO_InitStructure.GPIO_Alternate = GPIO_AF5_USART1;
    12.     GPIO_Peripheral_Initialize(GPIOA, &GPIO_InitStructure);

    13.     GPIO_InitStructure.Pin             = GPIO_PIN_10;
    14.     GPIO_InitStructure.GPIO_Alternate  = GPIO_AF5_USART1;
    15.     GPIO_Peripheral_Initialize(GPIOA, &GPIO_InitStructure);

    16.     USART_InitStructure.BaudRate            = 115200;
    17.     USART_InitStructure.WordLength          = USART_WL_8B;
    18.     USART_InitStructure.StopBits            = USART_STPB_1;
    19.     USART_InitStructure.Parity              = USART_PE_NO;
    20.     USART_InitStructure.HardwareFlowControl = USART_HFCTRL_NONE;
    21.     USART_InitStructure.Mode                = USART_MODE_TX | USART_MODE_RX;
    22.     /* init uart */
    23.     USART_Initializes(USART1, &USART_InitStructure);
    24.     /* enable uart */
    25.     USART_Enable(USART1);
    26.    
    27.     USART_Interrput_Enable(USART1, USART_INT_RXDNE);
    28. //    USART_Interrput_Enable(USART1, USART_INT_TXDE);

    29.     NVIC_Priority_Group_Set(NVIC_PER2_SUB2_PRIORITYGROUP);
    30.     /* Enable the USARTy Interrupt */
    31.     NVIC_InitStructure.NVIC_IRQChannel                   = USART1_IRQn;
    32.     NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 0;
    33.     NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
    34.     NVIC_Initializes(&NVIC_InitStructure);
    35.    
    36. }
    复制代码


    为了方便接收数据,下面封装了简单的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. fifo_buffer  uart_rx = {
    8.     .read_i = 0,
    9.     .write_i = 0,
    10. };

    11. fifo_buffer  uart_tx = {
    12.     .read_i = 0,
    13.     .write_i = 0,
    14. };

    15. void fifo_init(fifo_buffer *fifo)
    16. {
    17.     fifo->read_i = 0;
    18.     fifo->write_i = 0;
    19. }

    20. int fifo_push(fifo_buffer *fifo, uint8_t ch)
    21. {
    22.     if(((fifo->write_i+1)&0x7f) != fifo->read_i)
    23.     {
    24.         fifo->buff[fifo->write_i++] = ch;
    25.         fifo->write_i &= 0x7f;
    26.         return 0;
    27.     }else return 1; //full
    28. }

    29. int fifo_pop(fifo_buffer *fifo, uint8_t *ch)
    30. {
    31.     if(fifo->read_i != fifo->write_i)
    32.     {
    33.         *ch = fifo->buff[fifo->read_i++];
    34.         fifo->read_i &= 0x7f;
    35.         return 0;
    36.     }else return 1; //empty
    37. }
    复制代码


    串口中断如下:将接收的数据存入FIFO缓存,然后在主循环中取出数据。
    1. void USART1_IRQHandler(void)
    2. {
    3.     uint8_t ch;
    4.     if (USART_Interrupt_Status_Get(USART1, USART_INT_RXDNE) != RESET)
    5.     {
    6.         /* Read one byte from the receive data register */
    7.         ch = USART_Data_Receive(USART1);
    8.         fifo_push(&uart_rx,ch);
    9.     }

    10. //    if (USART_Interrupt_Status_Get(USART1, USART_INT_TXDE) != RESET)
    11. //    {
    12. //        /* Write one byte to the transmit data register */
    13. //        if(fifo_pop(&uart_tx,&ch) == 0)
    14. //        {
    15. //            USART_Data_Send(USART1, ch);
    16. //        }
    17. //    }
    18. }
    复制代码


    串口发送,重定向到printf输出。
    1. int stdout_putchar (int ch)
    2. {
    3.     while (0 == USART_Flag_Status_Get(USART1,USART_FLAG_TXDE));
    4.     USART_Data_Send(USART1, ch);
    5.     return ch;
    6. }

    7. int fputc(int ch,FILE *f)
    8. {
    9.     return stdout_putchar(ch);
    10. }
    复制代码


    可以通过如下程序测试一下发送接收数据:将接收的数据重新发送出去。测试完成OK后,就可以进行nr_micro_shell移植了。
    1. int main(void)
    2. {
    3.     uint8_t ch;
    4.     SysTick_Config(SystemClockFrequency/1000);
    5.     /* Initialize Led1~Led3 as output push-pull mode */
    6.     LED_Initialize(LED1_GPIO_PORT, LED1_GPIO_PIN | LED2_GPIO_PIN );
    7.     /* Turn off Led1~Led3 */
    8.     LED_Off(LED1_GPIO_PORT, LED1_GPIO_PIN | LED2_GPIO_PIN );
    9.    
    10.     uart_init();
    11.     printf("N32G430 USART1 TEST.\r\n");
    12.    
    13.     while(1)
    14.     {
    15.         if(fifo_pop(&uart_rx,&ch) == 0)
    16.         {
    17.             LED_Toggle(LED2_GPIO_PORT, LED2_GPIO_PIN);
    18.             stdout_putchar(ch);
    19.         }
    20.     }
    21. }
    复制代码


    下面开始移植nr_micro_shell。首先下载shell源码,将源码添加到工程中:
    a.jpg

    添加源码头文件路径
    aa.jpg

    然后配置shell功能,配置在nr_micro_shell_config.h文件中。主要配置串口shell输出打印函数
    b.jpg

    下一步就是初始化shell以及读取串口接收数据并传给shell解析。
    c.jpg

    然后编译下载即可。烧写成功后,打开串口测试一下。
    d.jpg



    程序:
    n32_g430.zip (1.4 MB, 下载次数: 0)
    回复

    使用道具 举报

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

    本版积分规则

    关闭

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

    手机版|小黑屋|与非网

    GMT+8, 2024-4-29 09:03 , Processed in 0.119796 second(s), 17 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.