一步一步和我一起走,进入缤纷的嵌入式世界(连载6)

这里我们说说关于UART口命令的设计,UART口的命令设计是个难点,主要是单线程的,一次轮询并不能从UART口得到一个完整的命令。需要多次轮询才能获得一个完整的命令,这里,难点在于如何保存多次轮询的结果,有两个办法,一个用全局变量的方式,一个采用局部静态变量的办法。

 

这里使用局部静态变量的办法,贴出代码:

void UARTCmdResponse(void)
{
    static U8 cmd[32];
    static int i = 0;
    U8 revByte;
    BOOL cmdReceiving = TRUE;
   
    while (UART_GetReceiveBytesNum())
    {
        UART_ReadReceiveBuffer(&revByte);
        if (revByte == '/r' || i == 31)
        {
            cmdReceiving = FALSE;
            break;
        }
        if (revByte != 0)
        {
            dbg_out_char(revByte);
            if (revByte >= 'a' && revByte <= 'z')
                revByte = revByte - 'a' + 'A';
            cmd[i++] = revByte;
        }
    }
    if (cmdReceiving)
        return ;
    cmd[i] = '/0';   
   
    if (strncmp((char const*)cmd, "READ PAGE", 9) == 0)
    {
        U8 *locate = cmd + 9;
        U32 address = 0;
        if (GetPlayState() != STOP)  
        {
            i = 0;
            dbg_out_text("/n/rPlease stop music!/n/r");
            return ;
        }
        while ((*locate == ' ') && (*locate != 0))
            locate++;
        address = Str2Hex(locate);
        dbg_out_text("/n/rRead SPI Flash from address 0x");
        dbg_out_hex(address);
        dbg_out_endl();
       
        U32 index;
        SPI_Selected();
        SPI_M25P32_ReadFrom(address);
#ifdef SPI_M25P32
        for (index = 0; index < 256; index++)
#endif
        {
            dbg_out_uint(SPI_ReadByte());
            dbg_out_char(' ');
        }
        dbg_out_endl();
        SPI_Deselected();
    }
    else if (strncmp((char const*)cmd, "PROG READ", 9) == 0)
    {
        U8 *locate = cmd + 9;
        U32 address = 0;
         if (GetPlayState() != STOP)  
        {
            i = 0;
            dbg_out_text("/n/rPlease stop music!/n/r");
            return ;
        }
        while ((*locate == ' ') && (*locate != 0))
            locate++;
        address = Str2Hex(locate);
               
        U32 index;
        SPI_Selected();
        SPI_M25P32_ReadFrom(address);
        for (index = 0; index < 256; index++)
        {
            dbg_out_char(SPI_ReadByte());
        }
        SPI_Deselected();
        dbg_out_text("/n/rRead done!/n/r");
    }
    else if (strncmp((char const*)cmd, "ERASE SECTOR", 12) == 0)
    {
        U8 *locate = cmd + 12;
        U32 nosector = 0;
        if (GetPlayState() != STOP)  
        {
            i = 0;
            dbg_out_text("/n/rPlease stop music!/n/r");
            return ;
        }
        while ((*locate == ' ') && (*locate != 0))
            locate++;
        nosector = Str2Hex(locate);
        dbg_out_text("/n/rErase the sector 0x");
        dbg_out_hex(nosector);
        dbg_out_text(" of SPI flash, Please wait.../n/r");
        SPI_WriteEnable();
        SPI_SectorErase(nosector);
        dbg_out_text("Erase sector done!/n/r");
    }
    else if (strncmp((char const*)cmd, "ERASE ALL", 9) == 0)
    {
        if (GetPlayState() != STOP)  
        {
            i = 0;
            dbg_out_text("/n/rPlease stop music!/n/r");
            return ;
        }       
        dbg_out_text("/n/rErase all sector of SPI flash, please wait.../n/r");
        SPI_WriteEnable();
        SPI_BulkErase();
        dbg_out_text("Erase all done!/n/r");
    }
    else if (strncmp((char const*)cmd, "WRITE", 5) == 0)
    {
        if (GetPlayState() != STOP)  
        {
            i = 0;
            dbg_out_text("/n/rPlease stop music!/n/r");
            return ;
        }
        U8 *locate = cmd + 5;
        U32 address = 0;
        while ((*locate == ' ') && (*locate != 0))
            locate++;
        address = Str2Hex(locate);
        int index = 0;
        SPI_InitWriteBuffer();
#ifdef SPI_M25P32
        for (index = 0; index < 256; index++)
#endif
        {
            while (!UART_GetReceiveBytesNum());
            UART_ReadReceiveBuffer(&revByte);
            dbg_out_char(revByte);
            SPI_WriteToBuffer(index, revByte);
        }
        SPI_ApplyPageProgram(address);
        dbg_out_text("/n/rWrite done!/n/r");
    }
    else if (strncmp((char const*)cmd, "IDENTIFY", 8) == 0)
    {
        dbg_out_text("/n/rIt's Audio board!/n/r");
        dbg_out_text("Identify done!/n/r");
    }
    else if (strncmp((char const*)cmd, "PLAY", 4) == 0)
    {
        SetPlayState(PLAY);
        dbg_out_text("/n/rPlay done!/n/r");
    }
    else if (strncmp((char const*)cmd, "PAUSE", 5) == 0)
    {
        SetPlayState(PAUSE);
        dbg_out_text("/n/rPause done!/n/r");       
    }
    else if (strncmp((char const*)cmd, "STOP", 4) == 0)
    {
        SetPlayState(STOP);
        dbg_out_text("/n/rStop done!/n/r");
    }
    else if (strncmp((char const*)cmd, "SPI FLASH", 9) == 0)
    {
        if (GetPlayState() != STOP)  
        {
            i = 0;
            dbg_out_text("/n/rPlease stop music!/n/r");
            return ;
        }
        SPI_M25P32_ReadInfo();
        dbg_out_text("SPI flash identify done!/n/r");
    }
    else
    {
        dbg_out_text("/n/rBad command!/n/r");
    }
    i = 0;
}

U32 Str2Hex(U8 const *inStr)
{
    U32 value = 0;
    while (*inStr != 0 && *inStr != ' ')
    {
        value <<= 4;
        if (*inStr >= '0' && *inStr <= '9')
            value |= (*inStr - '0');          
        else if (*inStr >= 'A' && *inStr <= 'F')
            value |= (*inStr - 'A') + 10;
        else if (*inStr >= 'a' && *inStr <= 'f')
            value |= (*inStr - 'a') + 10;
        else
        {
            value >>= 4;
            return (value);
            //null
        }
        inStr++;
    }
    return (value);
}

 

至此,核心的细节设计都和大家说了,下次我们说说PC端软件的设计。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值