打印
[方案相关]

基于 华大 HC32F460 芯片的 串口封装

[复制链接]
133|4
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
斧王FUWANG|  楼主 | 2023-12-20 15:16 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

接触嵌入式短短1年,也不知道封装的合理性如何,实际性能如何,就是自己写的爽,我就这么干了~~~

直接上代码~

#define Print(type,fmt,...){m_PrintfDevice = usart[type].usart_ch;printf(fmt,##__VA_ARGS__);};

typedef enum usart_type
{
    uart1  = 0u, uart2, uart3, uart4
} usart_t;

typedef struct Usart_Struct{
        M4_USART_TypeDef *          usart_ch;                        // 串口通道
        en_port_t                                 Rx_Port;                        // Rx串口Port
        en_pin_t                                 Rx_Pin;                                // Tx串口Pin
        en_port_t                                 Tx_Port;                       
        en_pin_t                                 Tx_Pin;
        uint32_t                         BAUDRATE;
        en_port_func_t                    USART_RX_FUNC;
        en_port_func_t                    USART_TX_FUNC;  
        en_int_src_t                           INT_USART_RI;        
        en_int_src_t                           INT_USART_EI;
        en_int_src_t                           INT_USART_TI;
        en_int_src_t                           INT_USART_TCI;
        enum IRQn                        Rx_IRQn;
        enum IRQn                       Tx_IRQn;
        en_usart_parity_t       usart_type;
        en_usart_clk_div_t                 clk;
        uint16_t                                 u16UsedSize;  
    uint16_t                                  au8Buf[200];
}Usart;

void usart_common_call(void);
void usart_common_err_call(void);
static void RingBufWriteNew(Usart *usart, uint8_t u8Data,usart_t type);
void all_usart_init(void);
void new_usart_init(Usart *u);
void resetBuf(Usart *u);


使用特权

评论回复
沙发
斧王FUWANG|  楼主 | 2023-12-20 15:17 | 只看该作者
配置4路串口
Usart usart[4]   = {
        {
                M4_USART3, // group 2
                PortE,Pin13,PortE,Pin14,
                115200ul,
                Func_Usart3_Rx,Func_Usart3_Tx,
                INT_USART3_RI,INT_USART3_EI,INT_USART3_TI,INT_USART3_TCI,
                Int001_IRQn,Int003_IRQn,
                UsartParityNone,
                UsartClkDiv_1,0
        },
        
    //...
}

使用特权

评论回复
板凳
斧王FUWANG|  楼主 | 2023-12-20 15:17 | 只看该作者
初始化各路串口

void all_usart_init () {
        PORT_DebugPortSetting(TDI,Disable);
        int len = sizeof(usart) / sizeof(usart[0]);
        for ( int i = 0 ; i < len ; i++) {
                new_usart_init(&usart[i]);
        }
}

void new_usart_init(Usart *u){
       
    stc_irq_regi_conf_t stcIrqRegiCfg;
    const stc_usart_uart_init_t stcInitCfg = {
        UsartIntClkCkNoOutput,
        u->clk,
        UsartDataBits8,
        UsartDataLsbFirst,
        UsartOneStopBit,
        u->usart_type,
        UsartSampleBit8,
        UsartStartBitFallEdge,
        UsartRtsEnable,
    };
  
        PORT_SetFunc(u->Rx_Port, u->Rx_Pin, u->USART_RX_FUNC, Disable);
        PORT_SetFunc(u->Tx_Port, u->Tx_Pin, u->USART_TX_FUNC, Disable);
       
    USART_UART_Init(u->usart_ch, &stcInitCfg);

    USART_SetBaudrate(u->usart_ch, u->BAUDRATE);
  
    stcIrqRegiCfg.enIRQn = u->Rx_IRQn;
    stcIrqRegiCfg.pfnCallback = usart_common_call;
    stcIrqRegiCfg.enIntSrc = u->INT_USART_RI ;
    enIrqRegistration(&stcIrqRegiCfg);
    NVIC_SetPriority(stcIrqRegiCfg.enIRQn, DDL_IRQ_PRIORITY_DEFAULT);
    NVIC_ClearPendingIRQ(stcIrqRegiCfg.enIRQn);
    NVIC_EnableIRQ(stcIrqRegiCfg.enIRQn);

    stcIrqRegiCfg.enIRQn =u->Tx_IRQn;
    stcIrqRegiCfg.pfnCallback = usart_common_err_call;
    stcIrqRegiCfg.enIntSrc = u->INT_USART_EI;
    enIrqRegistration(&stcIrqRegiCfg);
    NVIC_SetPriority(stcIrqRegiCfg.enIRQn, DDL_IRQ_PRIORITY_DEFAULT);
    NVIC_ClearPendingIRQ(stcIrqRegiCfg.enIRQn);
    NVIC_EnableIRQ(stcIrqRegiCfg.enIRQn);

        USART_FuncCmd(u->usart_ch, UsartRx, Enable);
    USART_FuncCmd(u->usart_ch, UsartRxInt, Enable);
    USART_FuncCmd(u->usart_ch, UsartTx, Enable);
}

使用特权

评论回复
地板
斧王FUWANG|  楼主 | 2023-12-20 15:17 | 只看该作者
处理4路串口的接收中断,异常处理中断
void usart_common_call(void){
        for ( usart_t i = u_4g ; i <= u_dw ; i++) {
                if (Set == USART_GetStatus(usart[i].usart_ch,UsartRxNoEmpty)){
                        uint8_t u8Data = USART_RecData(usart[i].usart_ch);
                        (void)RingBufWriteNew(&usart[i], u8Data,i);
                }
        }       
};

void usart_common_err_call(void){
        en_usart_status_t errFlag[3] = {UsartFrameErr,UsartParityErr,UsartOverrunErr};
        for (int i = 0 ; i < 3 ; i++) {
                for ( usart_t j = u_4g ; j <= u_dw ; j++ ) {
                        if (Set == USART_GetStatus(usart[j].usart_ch, errFlag[i]))
                        {
                                USART_ClearStatus(usart[j].usart_ch, errFlag[i]);
                        }
                }
        }
};

static void RingBufWriteNew(Usart *usart, uint8_t u8Data,usart_t type)
{
       
        if (usart->u16UsedSize > 150) { // 这里的长度没有配置
                resetBuf(usart);
        }
        usart->au8Buf[usart->u16UsedSize++] = u8Data;
       
        if ( type == uart1 ) {
                //自行判断是否完整接收 校验 ...
        }
       
        if ( type == uart2 ) {
               
               
        }
       
        if ( type == uart3 ) {
               
        }
       
        if ( type == uart4 ) {
               
        }
}

使用特权

评论回复
5
斧王FUWANG|  楼主 | 2023-12-20 15:18 | 只看该作者
最后~各路串口的打印调试

Print(uart1,"%s\r\n" , "Hello World!");

使用特权

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

本版积分规则

18

主题

242

帖子

0

粉丝