查看: 517|回复: 1

【LPC845月饼板】+串口移植nr_micro_shell,方便命令测试

[复制链接]
  • TA的每日心情
    奋斗
    昨天 08:56
  • 签到天数: 1939 天

    [LV.Master]伴坛终老

    61

    主题

    1万

    帖子

    3

    版主

    Rank: 7Rank: 7Rank: 7

    积分
    17288
    最后登录
    2024-4-25
    发表于 2023-5-24 09:46:27 | 显示全部楼层 |阅读模式
    本帖最后由 流水源 于 2023-5-24 10:15 编辑

        nr_micro_shell是一个开源的shell工具。使用简单,移植也容易。和它类似的有letter-shell和RTTHREAD的finsh shell。
        下面简单介绍下如何移植nr_micro_shell吧。
         移植之前首先要搞定串口通信。
    串口初始化如下,本次使用中断加fifo缓存接收方式。
    1. void drv_usart_gpio_init(void)
    2. {
    3.     usart_config_t   USART_InitStructure;
    4.    
    5.         /* Enables clock for switch matrix.: enable */
    6.     CLOCK_EnableClock(kCLOCK_Swm);
    7.     const uint32_t DEBUG_UART_RX = (/* Selects pull-up function */
    8.                                     IOCON_PIO_MODE_PULLUP |
    9.                                     /* Enable hysteresis */
    10.                                     IOCON_PIO_HYS_EN |
    11.                                     /* Input not invert */
    12.                                     IOCON_PIO_INV_DI |
    13.                                     /* Disables Open-drain function */
    14.                                     IOCON_PIO_OD_DI |
    15.                                     /* Bypass input filter */
    16.                                     IOCON_PIO_SMODE_BYPASS |
    17.                                     /* IOCONCLKDIV0 */
    18.                                     IOCON_PIO_CLKDIV0);
    19.     /* PIO1 PIN16 (coords: 36) is configured as USART0, RXD. */
    20.     IOCON_PinMuxSet(IOCON, IOCON_INDEX_PIO0_24, DEBUG_UART_RX);
    21.    
    22.     const uint32_t DEBUG_UART_TX = (/* Selects pull-up function */
    23.                                     IOCON_PIO_MODE_PULLUP |
    24.                                     /* Enable hysteresis */
    25.                                     IOCON_PIO_HYS_EN |
    26.                                     /* Input not invert */
    27.                                     IOCON_PIO_INV_DI |
    28.                                     /* Disables Open-drain function */
    29.                                     IOCON_PIO_OD_DI |
    30.                                     /* Bypass input filter */
    31.                                     IOCON_PIO_SMODE_BYPASS |
    32.                                     /* IOCONCLKDIV0 */
    33.                                     IOCON_PIO_CLKDIV0);
    34.     /* PIO1 PIN17 (coords: 37) is configured as USART0, TXD. */
    35.     IOCON_PinMuxSet(IOCON, IOCON_INDEX_PIO0_25, DEBUG_UART_TX);
    36.    
    37.     /* USART0_TXD connect to P0_25 */
    38.     SWM_SetMovablePinSelect(SWM0, kSWM_USART0_TXD, kSWM_PortPin_P0_25);
    39.     /* USART0_RXD connect to P0_24 */
    40.     SWM_SetMovablePinSelect(SWM0, kSWM_USART0_RXD, kSWM_PortPin_P0_24);
    41.     /* Disable clock for switch matrix. */
    42.     CLOCK_DisableClock(kCLOCK_Swm);CLOCK_Select(kUART0_Clk_From_MainClk);
    43.     RESET_PeripheralReset(kUART0_RST_N_SHIFT_RSTn);
    44.    
    45.     USART_GetDefaultConfig(&USART_InitStructure);
    46.     USART_InitStructure.baudRate_Bps = 115200;
    47.     USART_InitStructure.enableRx     = 1U;
    48.     USART_InitStructure.enableTx     = 1U;
    49.     USART_Init(USART0, &USART_InitStructure, CLOCK_GetMainClkFreq());
    50.    
    51.     //使能UARTx RC中断
    52.     USART_EnableInterrupts(USART0, kUSART_RxReadyInterruptEnable);
    53.     //优先级,无优先级分组
    54.     NVIC_SetPriority(USART0_IRQn, 0);
    55.     //UARTx中断使能
    56.     NVIC_EnableIRQ(USART0_IRQn);
    57. }
    复制代码

    串口发送:
    1. int stdout_putchar (int ch)
    2. {
    3.     while (0 == (USART_GetStatusFlags(USART0) & USART_STAT_TXRDY_MASK));
    4.     USART_WriteByte(USART0, (uint8_t)ch);
    5.    
    6.     return ch;
    7. }

    8. int fputc(int ch,FILE *f)
    9. {
    10.     return stdout_putchar(ch);
    11. }
    复制代码
    中断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  shell_uart_rx=
    8. {
    9.     .read_i = 0,
    10.     .write_i = 0,
    11. };

    12. void USART0_IRQHandler(void)
    13. {
    14.     uint32_t statusFlag;
    15.     uint32_t ch;
    16.    
    17.     statusFlag = USART0->STAT;
    18. //    if((statusFlag & USART_STAT_OVERRUNINT_MASK) != 0)
    19. //    {
    20. //        ch =ch;
    21. //    }
    22.     if((statusFlag & USART_STAT_RXRDY_MASK) != 0)
    23.     {
    24.         ch = USART_ReadByte(USART0);
    25.         if(((shell_uart_rx.write_i+1)&0x7f) != shell_uart_rx.read_i)
    26.         {
    27.             shell_uart_rx.buff[shell_uart_rx.write_i++] = ch & 0xff;
    28.             shell_uart_rx.write_i &= 0x7f;
    29.         }
    30.     }
    31.     USART0->STAT |= statusFlag;
    32. }

    复制代码

    下面就是移植nr_micro_shell代码了。首先去nr_micro_shell仓库下载代码:https://gitee.com/nrush/nr_micro_shell     。
    将代码文件加入到工程中。

    a1.jpg

    移植主要是在nr_micro_shell_config.h配置。配置串口字节输出和printf格式化输出。
    1. /* ANSI command line buffer size. */
    2. #define NR_ANSI_LINE_SIZE                                                 256

    3. /* Maximum user name length. */
    4. #define NR_SHELL_USER_NAME_MAX_LENGTH                         16

    5. /* Maximum command name length. */
    6. #define NR_SHELL_CMD_NAME_MAX_LENGTH                         16

    7. /* Command line buffer size. */
    8. #define NR_SHELL_CMD_LINE_MAX_LENGTH                         NR_ANSI_LINE_SIZE

    9. /* The maximum number of parameters in the command. */
    10. #define NR_SHELL_CMD_PARAS_MAX_NUM                                 10

    11. /* Command stores the most history commands (the maximum number here refers to the maximum number of commands that can be stored. When the history command line cache is full, it will automatically release the earliest command record) */
    12. #define NR_SHELL_MAX_CMD_HISTORY_NUM                         8

    13. /* History command cache length */
    14. #define NR_SHELL_CMD_HISTORY_BUF_LENGTH                 253

    15. /* The user's name. */
    16. #define NR_SHELL_USER_NAME                                                 "root:"

    17. /*
    18. 0: \n
    19. 1: \r
    20. 2: \r\n
    21. */
    22. #define NR_SHELL_END_OF_LINE                                 1

    23. /* Weather the terminal support all ANSI codes. */
    24. #define NR_SHLL_FULL_ANSI                                         1

    25. /* Show logo or not. */
    26. #define NR_SHELL_SHOW_LOG

    27. // /* Use NR_SHELL_CMD_EXPORT() or not */
    28. #define NR_SHELL_USING_EXPORT_CMD

    29. /* If you use RTOS, you may need to do some special processing for printf(). */
    30. extern int stdout_putchar (int ch) ;
    31. #define shell_printf(fmt, args...)      printf(fmt, ##args);
    32. #define ansi_show_char(x)               stdout_putchar(x)
    复制代码
    然后在main中调用初始化shell以及从串口中读取输入数据进行解析。

    1. void shell_usart_loop(void)
    2. {
    3.     if(shell_uart_rx.read_i != shell_uart_rx.write_i)
    4.     {
    5.         shell(shell_uart_rx.buff[shell_uart_rx.read_i++]);
    6.         shell_uart_rx.read_i &= 0x7f;
    7.     }
    8. }
    复制代码
    a2.jpg

    编译下载后就可以通过串口来控制了。nr_micro_shell默认有ls和test命令例子。
    a3.jpg


    该会员没有填写今日想说内容.
    回复

    使用道具 举报

  • TA的每日心情
    慵懒
    2024-2-21 10:06
  • 签到天数: 310 天

    [LV.8]以坛为家I

    2

    主题

    5670

    帖子

    0

    金牌会员

    Rank: 6Rank: 6

    积分
    11646
    最后登录
    2024-2-21
    发表于 2023-8-28 05:35:23 | 显示全部楼层
    活动第13天打卡
    活动第13天打卡.png
    该会员没有填写今日想说内容.
    回复 支持 反对

    使用道具 举报

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

    本版积分规则

    关闭

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

    Archiver|手机版|小黑屋|恩智浦技术社区

    GMT+8, 2024-4-26 04:10 , Processed in 0.108900 second(s), 20 queries , MemCache On.

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.

    快速回复 返回顶部 返回列表