TA的每日心情  | 慵懒 2025-9-22 14:27 | 
|---|
 
  签到天数: 777 天 连续签到: 1 天 [LV.10]以坛为家III 
翰林 
 
 
	- 积分
 - 12027
 
 
 
 
 | 
 
 
   上次玩了一下N32G430开发板点亮了led灯,这次就试试串口。实现串口的发送接收。然后尝试移植了nr_micro_shell。 
移植shell首先要把串口发送接收搞定。 
下面就是我使用USART1测试,初始化USART1如下:使用中断接收串口数据,这样就不会丢失数据了。 
- void uart_init(void)
 
 - {
 
 -     GPIO_InitType GPIO_InitStructure;
 
 -     USART_InitType USART_InitStructure;
 
 -     NVIC_InitType NVIC_InitStructure;
 
  
-     RCC_AHB_Peripheral_Clock_Enable(RCC_AHB_PERIPH_GPIOA);
 
 -     RCC_APB2_Peripheral_Clock_Enable(RCC_APB2_PERIPH_AFIO | RCC_APB2_PERIPH_USART1);
 
  
-     GPIO_Structure_Initialize(&GPIO_InitStructure);
 
 -     GPIO_InitStructure.Pin            = GPIO_PIN_9;
 
 -     GPIO_InitStructure.GPIO_Mode      = GPIO_MODE_AF_PP;
 
 -     GPIO_InitStructure.GPIO_Alternate = GPIO_AF5_USART1;
 
 -     GPIO_Peripheral_Initialize(GPIOA, &GPIO_InitStructure);
 
  
-     GPIO_InitStructure.Pin             = GPIO_PIN_10;
 
 -     GPIO_InitStructure.GPIO_Alternate  = GPIO_AF5_USART1;
 
 -     GPIO_Peripheral_Initialize(GPIOA, &GPIO_InitStructure);
 
  
-     USART_InitStructure.BaudRate            = 115200;
 
 -     USART_InitStructure.WordLength          = USART_WL_8B;
 
 -     USART_InitStructure.StopBits            = USART_STPB_1;
 
 -     USART_InitStructure.Parity              = USART_PE_NO;
 
 -     USART_InitStructure.HardwareFlowControl = USART_HFCTRL_NONE;
 
 -     USART_InitStructure.Mode                = USART_MODE_TX | USART_MODE_RX;
 
 -     /* init uart */
 
 -     USART_Initializes(USART1, &USART_InitStructure);
 
 -     /* enable uart */
 
 -     USART_Enable(USART1);
 
 -     
 
 -     USART_Interrput_Enable(USART1, USART_INT_RXDNE);
 
 - //    USART_Interrput_Enable(USART1, USART_INT_TXDE);
 
  
-     NVIC_Priority_Group_Set(NVIC_PER2_SUB2_PRIORITYGROUP);
 
 -     /* Enable the USARTy Interrupt */
 
 -     NVIC_InitStructure.NVIC_IRQChannel                   = USART1_IRQn;
 
 -     NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 0;
 
 -     NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
 
 -     NVIC_Initializes(&NVIC_InitStructure);
 
 -     
 
 - }
 
  复制代码 
 
为了方便接收数据,下面封装了简单的FIFO缓存,用于串口接收。 
- typedef struct fifo_buffer
 
 - {
 
 -     volatile uint32_t    read_i;
 
 -     volatile uint32_t    write_i;
 
 -     uint8_t     buff[128];
 
 - }fifo_buffer;
 
  
- fifo_buffer  uart_rx = {
 
 -     .read_i = 0,
 
 -     .write_i = 0,
 
 - };
 
  
- fifo_buffer  uart_tx = {
 
 -     .read_i = 0,
 
 -     .write_i = 0,
 
 - };
 
  
- void fifo_init(fifo_buffer *fifo)
 
 - {
 
 -     fifo->read_i = 0;
 
 -     fifo->write_i = 0;
 
 - }
 
  
- int fifo_push(fifo_buffer *fifo, uint8_t ch)
 
 - {
 
 -     if(((fifo->write_i+1)&0x7f) != fifo->read_i)
 
 -     {
 
 -         fifo->buff[fifo->write_i++] = ch;
 
 -         fifo->write_i &= 0x7f;
 
 -         return 0;
 
 -     }else return 1; //full
 
 - }
 
  
- int fifo_pop(fifo_buffer *fifo, uint8_t *ch)
 
 - {
 
 -     if(fifo->read_i != fifo->write_i)
 
 -     {
 
 -         *ch = fifo->buff[fifo->read_i++];
 
 -         fifo->read_i &= 0x7f;
 
 -         return 0;
 
 -     }else return 1; //empty
 
 - }
 
  复制代码 
 
串口中断如下:将接收的数据存入FIFO缓存,然后在主循环中取出数据。 
- void USART1_IRQHandler(void)
 
 - {
 
 -     uint8_t ch;
 
 -     if (USART_Interrupt_Status_Get(USART1, USART_INT_RXDNE) != RESET)
 
 -     {
 
 -         /* Read one byte from the receive data register */
 
 -         ch = USART_Data_Receive(USART1);
 
 -         fifo_push(&uart_rx,ch);
 
 -     }
 
  
- //    if (USART_Interrupt_Status_Get(USART1, USART_INT_TXDE) != RESET)
 
 - //    {
 
 - //        /* Write one byte to the transmit data register */
 
 - //        if(fifo_pop(&uart_tx,&ch) == 0)
 
 - //        {
 
 - //            USART_Data_Send(USART1, ch);
 
 - //        }
 
 - //    }
 
 - }
 
  复制代码 
 
串口发送,重定向到printf输出。 
- int stdout_putchar (int ch)
 
 - {
 
 -     while (0 == USART_Flag_Status_Get(USART1,USART_FLAG_TXDE));
 
 -     USART_Data_Send(USART1, ch);
 
 -     return ch;
 
 - }
 
  
- int fputc(int ch,FILE *f)
 
 - {
 
 -     return stdout_putchar(ch);
 
 - }
 
 
  复制代码 
 
可以通过如下程序测试一下发送接收数据:将接收的数据重新发送出去。测试完成OK后,就可以进行nr_micro_shell移植了。 
- int main(void)
 
 - {
 
 -     uint8_t ch;
 
 -     SysTick_Config(SystemClockFrequency/1000);
 
 -     /* Initialize Led1~Led3 as output push-pull mode */
 
 -     LED_Initialize(LED1_GPIO_PORT, LED1_GPIO_PIN | LED2_GPIO_PIN );
 
 -     /* Turn off Led1~Led3 */
 
 -     LED_Off(LED1_GPIO_PORT, LED1_GPIO_PIN | LED2_GPIO_PIN );
 
 -     
 
 -     uart_init();
 
 -     printf("N32G430 USART1 TEST.\r\n");
 
 -     
 
 -     while(1)
 
 -     {
 
 -         if(fifo_pop(&uart_rx,&ch) == 0)
 
 -         {
 
 -             LED_Toggle(LED2_GPIO_PORT, LED2_GPIO_PIN);
 
 -             stdout_putchar(ch);
 
 -         }
 
 -     }
 
 - }
 
  复制代码 
 
下面开始移植nr_micro_shell。首先下载shell源码,将源码添加到工程中: 
 
 
添加源码头文件路径 
 
 
然后配置shell功能,配置在nr_micro_shell_config.h文件中。主要配置串口shell输出打印函数 
 
 
下一步就是初始化shell以及读取串口接收数据并传给shell解析。 
 
 
然后编译下载即可。烧写成功后,打开串口测试一下。 
 
 
 
 
程序: 
 
 
 
 
 
 
 |   
 
  
  
  
 
 
 |