打印
[MM32硬件]

【MM32 eMiniBoard测评报告】串口shell测试

[复制链接]
492|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
本帖最后由 WoodData 于 2020-4-29 20:20 编辑

    搞了一下串口shell,移植的nr_micro_shell.( https://gitee.com/nrush/nr_micro_shell )。测试感觉还不错,shell移植也很方便。
测试:

#include "nr_micro_shell.h"
#include "string.h"
#include "ctype.h"
#include "led.h"

/**
* [url=home.php?mod=space&uid=247401]@brief[/url] ls command
*/
void shell_ls_cmd(char argc, char *argv)
{
        unsigned int i = 0;
        if (argc > 1)
        {
                if (!strcmp("cmd", &argv[argv[1]]))
                {

                        for (i = 0; nr_shell.static_cmd[i].fp != NULL; i++)
                        {
                                shell_printf(nr_shell.static_cmd[i].cmd);
                                shell_printf("\r\n");
                        }
                }
                else if (!strcmp("-v", &argv[argv[1]]))
                {
                        shell_printf("ls version 1.0.\r\n");
                }
                else if (!strcmp("-h", &argv[argv[1]]))
                {
                        shell_printf("useage: ls [options]\r\n");
                        shell_printf("options: \r\n");
                        shell_printf("\t -h \t: show help\r\n");
                        shell_printf("\t -v \t: show version\r\n");
                        shell_printf("\t cmd \t: show all commands\r\n");
                }
        }
        else
        {
                shell_printf("ls need more arguments!\r\n");
        }
}

/**
* [url=home.php?mod=space&uid=247401]@brief[/url] test command
*/
void shell_test_cmd(char argc, char *argv)
{
        unsigned int i;
        shell_printf("test command:\r\n");
        for (i = 0; i < argc; i++)
        {
                shell_printf("paras %d: %s\r\n", i, &(argv[argv[i]]));
        }
}

/**
* @brief test command
*/
void shell_led_cmd(char argc, char *argv)
{
        if (argc > 1)
        {        
                if (!strcmp("on", &argv[argv[1]]))
                {
                        if (!strcmp("1", &argv[argv[2]]))
                        {
                                LED1_ON();
                        }else if (!strcmp("2", &argv[argv[2]]))
                        {
                                LED2_ON();
                        }else if (!strcmp("3", &argv[argv[2]]))
                        {
                                LED3_ON();
                        }else if (!strcmp("4", &argv[argv[2]]))
                        {
                                LED4_ON();
                        }else
                        {
                                shell_printf("useage: led [on/off] [1/2/3/4]\r\n");        
                        }
                }else if (!strcmp("off", &argv[argv[1]]))
                {
                        if (!strcmp("1", &argv[argv[2]]))
                        {
                                LED1_OFF();
                        }else if (!strcmp("2", &argv[argv[2]]))
                        {
                                LED2_OFF();
                        }else if (!strcmp("3", &argv[argv[2]]))
                        {
                                LED3_OFF();
                        }else if (!strcmp("4", &argv[argv[2]]))
                        {
                                LED4_OFF();
                        }else
                        {
                                shell_printf("useage: led [on/off] [1/2/3/4]\r\n");        
                        }
                        
                }else{
                        shell_printf("useage: led [on/off] [1/2/3/4]\r\n");               
                }
        }
}


#ifdef NR_SHELL_USING_EXPORT_CMD
NR_SHELL_CMD_EXPORT(ls, shell_ls_cmd);
NR_SHELL_CMD_EXPORT(test, shell_test_cmd);
NR_SHELL_CMD_EXPORT(led, shell_led_cmd);


#else
const static_cmd_st static_cmd[] =
        {
                {"ls", shell_ls_cmd},
                {"test", shell_test_cmd},
                {"led", shell_led_cmd},
               
                {"\0", NULL}
        };
               
               
#endif
/*=============================================================================
// File Name:
// Author:
// Date:
// Version:

=============================================================================*/
/* Includes -----------------------------------------------------------------*/
#include "print.h"
#include "sys.h"


/********************************************************************************************************
**º¯ÊýÐÅÏ¢ UartInit_Loop(void)                     
**¹¦ÄÜÃèÊö £º³õʼ»¯´®¿Ú
**ÊäÈë²ÎÊý £ºÎÞ
**Êä³ö²ÎÊý £ºÎÞ
********************************************************************************************************/
void Uart1_Init(u32 boaud)
{
    //GPIO¶Ë¿ÚÉèÖà        
    RCC->AHBENR  |= 0x00000001<<17;   //ʹÄÜGPIOA¿ÚʱÖÓ  
    RCC->APB2ENR |= 0x00000001<<14;  //ʹÄÜ´®¿ÚʱÖÓ

    RCC->APB2RSTR |= 1<<14;   //????1
    RCC->APB2RSTR &= ~(1<<14);//????        
        
        //
    GPIOA->CRH &= 0XFFFFF00F;//IO״̬ÉèÖÃ
    GPIOA->CRH |= 0X00000890;//IO״̬ÉèÖÃ
        
        //IO¸´Óù¦ÄÜ
    GPIOA->AFRH &= 0xfffff00f;
    GPIOA->AFRH |= 0x00000110;   
   
    //UART ³õʼ»¯ÉèÖà       
    UART1->BRR  = (SystemCoreClock / 16) /(boaud);
    UART1->FRA  = (SystemCoreClock / 16) %(boaud);
    UART1->CCR |= 0x30; //110000
    UART1->GCR |= 0x19;  //11001;//ʹÄÜ´®¿Ú1                  

}

void Uart1_Send(u8 ch)
{
    while((UART1->CSR & ((uint16_t)0x0001))==0);//Ñ­»··¢ËÍ,Ö±µ½·¢ËÍÍê±Ï
    UART1->TDR = (ch & (uint16_t)0x00FF);
}

u8 Uart1_Recv(void)
{
        u8 ch;
    while((UART1->CSR & ((uint16_t)0x0002))==0);//Ñ­»··¢ËÍ,Ö±µ½·¢ËÍÍê±Ï
    ch = UART1->RDR & 0xFF;
        return ch;
}


void Uart2_Init(u32 boaud)
{
    //GPIO¶Ë¿ÚÉèÖà        PA2=TX2,PA3=RX2
    RCC->AHBENR  |= 0x00000001<<17;           //ʹÄÜGPIOA¿ÚʱÖÓ  
    RCC->APB1ENR |= 0x00000001<<17;          //ʹÄÜ´®¿Ú2ʱÖÓ

    RCC->APB1RSTR |= 1<<17;           //????1
    RCC->APB1RSTR &= ~(1<<17);        //????        
        
        //
    GPIOA->CRL &= 0XFFFF00FF;//IO״̬ÉèÖÃ
    GPIOA->CRL |= 0X00008900;//IO״̬ÉèÖÃ
        
        //IO¸´Óù¦ÄÜUART2
    GPIOA->AFRL &= 0xffff00ff;
    GPIOA->AFRL |= 0x00001100;   
   
    //UART ³õʼ»¯ÉèÖà       APB1ʱÖÓÊÇϵͳʱÖÓ2·ÖƵ¡£
    UART2->BRR  = ((SystemCoreClock/2) / 16) /(boaud);
    UART2->FRA  = ((SystemCoreClock/2) / 16) %(boaud);
    UART2->CCR |= 0x30; //110000
    UART2->GCR |= 0x19;  //11001;//ʹÄÜ´®¿Ú2                  

}

void Uart2_Send(u8 ch)
{
    while((UART2->CSR & ((uint16_t)0x0001))==0);//Ñ­»··¢ËÍ,Ö±µ½·¢ËÍÍê±Ï
    UART2->TDR = (ch & (uint16_t)0x00FF);
}

u8 Uart2_Recv(void)
{
        u8 ch;
    while((UART2->CSR & ((uint16_t)0x0002))==0);//Ñ­»··¢ËÍ,Ö±µ½·¢ËÍÍê±Ï
    ch = UART2->RDR & 0xFF;
        return ch;
}

#ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)

#endif


#ifdef USE_IAR
PUTCHAR_PROTOTYPE
{
    Uart1_Send(ch & (uint16_t)0x00FF);      
    return ch;
}

#else
#pragma import(__use_no_semihosting)            
//±ê×¼¿âÐèÒªµÄÖ§³Öº¯Êý                 
struct __FILE
{
    int handle;
   
};

FILE __stdout;      
//¶¨Òå_sys_exit()ÒÔ±ÜÃâʹÓðëÖ÷»úģʽ   
void _sys_exit(int x)
{
    x = x;
}

//Öض¨Òåfputcº¯Êý
PUTCHAR_PROTOTYPE
{     
    Uart1_Send(ch & (uint16_t)0x00FF);      
    return ch;        
}
#endif


int main(void)
{
        u8 ch;
        
    Delay_Init();
    LED_Init();                                   
        Uart1_Init(115200);
        Uart2_Init(115200);
        shell_init();            /* nr_micro_shell*/
               
    while(1)
    {
                ch = Uart1_Recv();
        shell(ch);
    }
}








工程:
游客,如果您要查看本帖隐藏内容请回复

使用特权

评论回复
沙发
jiekou001| | 2020-4-25 19:18 | 只看该作者
工程分享一下啊。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

119

主题

4485

帖子

26

粉丝