STM32xx系列单片机串口数据收发

1. 串口初始化

复制代码
void ShockWaveHandle_USART2Init(void)
{
    GPIO_InitTypeDef GPIO_InitStruct;
    NVIC_InitTypeDef NVIC_InitStruct;

    // RCC_APB1PeriphClockCmd: usart2 is hanging under APB1, only usart1 is hanging under APB2
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 | RCC_APB2Periph_GPIOA, ENABLE);

    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP; // TX3 - PB10 - ch1
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_Init(GPIOA, &GPIO_InitStruct);

    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING; // RX3 - PB11 - ch1
    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3;

    GPIO_Init(GPIOA, &GPIO_InitStruct);

    USART2_Configuration();

    NVIC_InitStruct.NVIC_IRQChannel = USART2_IRQn;
    NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
    NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = PreemptionPriority_0;
    NVIC_InitStruct.NVIC_IRQChannelSubPriority = SubPriority_1;

    NVIC_Init(&NVIC_InitStruct);

    USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);

    USART_Cmd(USART2, ENABLE);
}

2. 串口中断服务函数

复制代码
void USART2_IRQHandler(void)
{
    if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
    {
        uint8_t data;

        data = USART_ReceiveData(USART2);

        ch1_msg_rx(USART2, data);
    }
}

3. 串口数据接收

复制代码
void ch1_msg_rx(USART_TypeDef *USARTx, uint8_t receiveData)
{
    UartMsgRecvHandler(USARTx, &ch1_msg[ch1_re], &ch1_re, receiveData);
}

void UartMsgRecvHandler(USART_TypeDef *USARTx, TUartMsg *ptUartMsg, uint16_t *rev, BYTE ucRecvDate)
{
    if (FALSE == (ptUartMsg->ucRecvSta & RECV_FLG_HEAD1)) // Not receive first data (0x55)
    {
        ptUartMsg->ucRecvSta = 0; // set flag

        if (DATA_HEAD_1 == ucRecvDate) // receive first data (0x55)
        {
            ptUartMsg->aucBuf[0] = DATA_HEAD_1;
            ptUartMsg->ucRecvSta |=  RECV_FLG_HEAD1;
        }
    }
    else // 1. receive first data (0x55)
    {
        if (FALSE == (ptUartMsg->ucRecvSta & RECV_FLG_HEAD2)) // Not receive second data (0xAA)
        {
            if (DATA_HEAD_2 == ucRecvDate) // receive second data (0xAA)
            {
                ptUartMsg->aucBuf[1] = DATA_HEAD_2;
                ptUartMsg->ucRecvSta |=  RECV_FLG_HEAD2;
            }
            else
            {
                ptUartMsg->ucRecvSta = 0;
            }
        }
        else // 2. receive second data (0xAA)
        {
            if (FALSE == (ptUartMsg->ucRecvSta & RECV_FLG_LEN)) // not receive third data (cmdLen)
            {
                if ((UART_LEN >= ucRecvDate) && (2 <= ucRecvDate)) // receive third data (cmdLen)
                {
                    ptUartMsg->aucBuf[2] = ucRecvDate;
                    ptUartMsg->ucRecvSta |= RECV_FLG_LEN;

                    ptUartMsg->ucRecvCount = ucRecvDate + 5;
                    ptUartMsg->ucRecvIndex = 3;
                }
                else
                {
                    ptUartMsg->ucRecvSta = 0;
                }
            }
            else // 3. receive third data (cmdLen)
            {
                ptUartMsg->aucBuf[ptUartMsg->ucRecvIndex] = ucRecvDate; // 4. receive cmd data (payload)
                ptUartMsg->ucRecvIndex++;

                if ((ptUartMsg->ucRecvIndex >= ptUartMsg->ucRecvCount)) // 5. receive complete
                {
                    ptUartMsg->bFlagRecv = TRUE;
                    ptUartMsg->ucRecvSta = 0;
                    ptUartMsg->USARTx = USARTx;
                    *rev += 1;
                    if (*rev >= UART_MSG_LEN)
                    {
                        *rev = 0;
                    }
                }
                else
                {
                    ptUartMsg->bFlagRecv = FALSE;
                }
                
            }
        }
    }
}

4. 串口数据接收处理

复制代码
static void ch1_PackageAnalyze(void)
{
    cmd_TypeDef msg;
    BYTE value;

    taskENTER_CRITICAL();
    msg.cmd_main = ch1_msg[ch1_prc].aucBuf[CMD1_ADDH];
    msg.cmd_main <<= 8;
    msg.cmd_main |= ch1_msg[ch1_prc].aucBuf[CMD1_ADDL]; // get main cmd

    msg.cmd_type = ch1_msg[ch1_prc].aucBuf[CMDTYPE_ADD]; // cmd type high
    msg.cmd_target = ch1_msg[ch1_prc].aucBuf[CMD2_ADD];

    value = ch1_msg[ch1_prc].aucBuf[CMD3_VALUE_INDEX];
    taskEXIT_CRITICAL();

    // receive data format: [head1, head2, msg_len1, msg_len2, cmd_main1, cmd_main2, cmd_type, cmd_target, value, crc1, crc2]
    switch (msg.cmd_main)
    {
        case CMD_31: // 0x06, 0x31
        {
            switch (msg.cmd_type)
            {
                case CMD_TYPE_SET: // 0x02
                {
                    ShockWaveHandle_PowerCtrlAndSetParameterAndWorkingCtrl(CH1_UART, msg.cmd_target, value);
                }
                break;
                case CMD_TYPE_ACK:
                {
                    // TODO
                }
                break;
                default:
                {
                    // TODO
                }
                break;
            }
        }
        break;
        case CMD_32: // 0x06, 0x32, recevi handle shake packet or heart beat first packet
        {
            ShockWaveHandle_HandShakeAndHeartBeatPacketHandle(CH1_UART, msg.cmd_type, msg.cmd_target);
        }
        break;
        case CMD_33: // 0x06, 0x33, receive heart beat seconde packet
        {
            // TODO
        }
        break;
        default:
        {
            // TODO
        }
        break;
    }
    UartMsgRecvFlagClr(&ch1_msg[uart_msg_prc]);
}

void UartMsgRecvFlagClr(TUartMsg *ptUartMsg)
{
    if(NULL == ptUartMsg)
    {
        return;
    }
    ptUartMsg->bFlagRecv = FALSE;
    ptUartMsg->ucRecvSta = 0;
    ptUartMsg->ucRecvCount = 0;
}
相关推荐
三佛科技-1341638421228 分钟前
FT32F103系列与APM32F103,STM32F103之间的对比,能否替换?
单片机·嵌入式硬件·物联网·智能家居·pcb工艺
李永奉1 小时前
杰理可视化SDK开发-蓝牙的可发现可连接和回连
单片机·嵌入式硬件·物联网·语音识别
【ql君】qlexcel2 小时前
Visual Studio Code开发STM32设置头文件宏定义uint32_t报错
vscode·stm32·vs code·头文件宏定义·uint32_t报错·uint8_t报错·uint16_t报错
振浩微433射频芯片3 小时前
标签界的“千里眼”:VRT5312,150cm超远读写距离,重新定义RFID新可能!
科技·单片机·嵌入式硬件·物联网
andylauren3 小时前
论单点接地的重要性——从MP3模块噪声问题看接地设计的关键
嵌入式硬件
修勾勾L4 小时前
使用VSCode开发嵌入式开发详细教程——步骤二项目实战
嵌入式硬件
染予4 小时前
定时器时钟源介绍
单片机·嵌入式硬件
时空自由民.4 小时前
ESP32编译固件内存信息解读
单片机·性能优化
LCMICRO-133108477465 小时前
长芯微LPS6288完全P2P替代TPS61288,是一款具有 15A 开关电流的全集成同步升压转换器
stm32·单片机·嵌入式硬件·fpga开发·硬件工程·同步升压转换器
FreakStudio5 小时前
MicroPython对接大模型:uopenai + 火山方舟实现文字聊天和图片理解
python·单片机·ai·嵌入式·面向对象·电子diy