CH57x/CH58xUART3收数据并校验修改MAC地址

前言:

UART1中断例程中达到触发点或超时接收串口数据会立马发送出去。本博客实现的是接收数据保存在数组中,同时校验数据内容是否为修改MAC帧。

接收数据不需要在UART_II_RECV_RDY中接收数据,可以都在UART_II_RECV_TOUT中收数据,直接读取RBR寄存器即可。

程序参考:

 基于BLE_UART例程修改。蓝牙连接后串口接收数据保存在环形缓冲区中;蓝牙未连接时,串口接收数据并保存在数组里面。

enum uart_return_result
{
    ADD_REC_SUCCESS = 0,
    ADD_REC_ERROR,
};

typedef enum       //定义与声明
{
    changemac_enable = 0,
    changemac_disable = !changemac_enable,
}changemac;

struct uart_t
{
    uint8 staticRfcLen;
    uint8 connectflag;
    uint16 x;
    uint8 judgeBuff[100];
    uint8 RxBuff[1000]; //接收最大长度
    char judgeMac[4];
};

__HIGH_CODE
uint8 REC_UART3(uint8 len, changemac enable){
    static uint32_t rxlen;
    uart_add = &uart_add_t;
    if(len >= 4)
    {
        for(uart_add->x = 0; uart_add->x < (4 - 1); uart_add->x++)
        {
            uart_add->RxBuff[uart_add->x] = R8_UART3_RBR;
            uart_add->judgeBuff[rxlen++] = uart_add->RxBuff[uart_add->x];
        }
    }
    else
    {
        uart_add->staticRfcLen = len;
        for( uart_add->x = 0; uart_add->x < uart_add->staticRfcLen; uart_add->x++)
        {
            uart_add->RxBuff[uart_add->x] = R8_UART3_RBR;
            uart_add->judgeBuff[rxlen++] = uart_add->RxBuff[uart_add->x];
        }
        if(!(R8_UART3_LSR & RB_LSR_DATA_RDY))                   //数据接收完成
        {
            printf("rxlen = %d, ", rxlen);                      //接收的总长度
            printf("judgeBuff = %s\n ", uart_add->judgeBuff);   //接收的数据
            printf("REC OVER\n");
            if(enable == 0)
            {
                strcpy(uart_add->judgeMac,"MAC");
                if(strstr(uart_add->judgeBuff, uart_add->judgeMac))         //判断是否为"MAC="
                {
                    printf("Rec MAC ADDR\r\n");
                    tmos_memcpy(NewMacAddr, &(uart_add->judgeBuff[4]), 6);  //前四个字节固定为"MAC=",否则无法识别,因此此处从第五个字节开始计数
                    tmos_start_task(Peripheral_TaskID, rst_evt, 8000);      //MAC地址设置完成后,5s复位,进行重新广播,用手机再搜索,发现MAC地址会改变。
                    EEPROM_ERASE(0, EEPROM_PAGE_SIZE);                      //将MAC保存到DataFlash,下次上电可以获取
                    EEPROM_WRITE(0, NewMacAddr, 6);
                }
                else
                {
                    printf("ENABLE changemac but not rec MACADDR\r\n");
                }
            }
            memset(uart_add->judgeBuff, 0, rxlen);              //开启串口接收并修改MAC地址,将清0放在更改地址后面
            rxlen = 0;                                          //长度清0,防止溢出
            return ADD_REC_SUCCESS;
        }
        else
        {
            printf("REC FAIL\n");
            return ADD_REC_ERROR;
        }rxlen = 0;                                             //长度清0,防止溢出
    }
    return 0;
}

switch(UART3_GetITFlag())
{
    case UART_II_LINE_STAT:
        UART3_GetLinSTA();
        break;

    case UART_II_RECV_RDY:
    case UART_II_RECV_TOUT:
        if(uart_add->connectflag)   //蓝牙连接,执行环形缓冲区的处理(即执行原本例程的操作)
        {
            error = app_drv_fifo_write_from_same_addr(&app_uart_rx_fifo, (uint8_t *)&R8_UART3_RBR, R8_UART3_RFC);
            if(error != APP_DRV_FIFO_RESULT_SUCCESS)
            {
                for(uint8_t i = 0; i < R8_UART3_RFC; i++)
                {
                    //fifo full,put to fifo black hole
                    for_uart_rx_black_hole = R8_UART3_RBR;
                }
            }
            uart_rx_flag = true;
        }
        else if(!uart_add->connectflag) //蓝牙未连接,通过串口3透传数据(可选择是否修改MAC使能)
        {
            REC_UART3(R8_UART3_RFC, changemac_enable );
        }
            break;

    case UART_II_THR_EMPTY:
        break;
    case UART_II_MODEM_CHG:
        break;
    default:
        break;
}

 

posted @ 2023-08-04 10:19  SweetTea_lllpc  阅读(218)  评论(0编辑  收藏  举报