/*! \file usart.c \brief 串口(USART/UART)统一驱动入口(集中式) 目标:把 `BSP\com\` 下分散的串口实现整理为“单入口/统一API”,形态上尽量贴近 `CCU601E_RUN\BSP\ST_HAL\usart.c`(集中管理各串口初始化、发送、接收缓存、中断)。 */ #include "usart.h" #include #include #include #include "gd32h7xx.h" #include "publicdata/public_define.h" #include "ringfifo/ringfifo.h" #ifndef DEBUG_SHELL_USART_NULL #define DEBUG_SHELL_USART_NULL (0xFFU) #endif #ifndef DEBUG_SHELL_USART_ID #define DEBUG_SHELL_USART_ID DEBUG_SHELL_USART_NULL #endif /* ===== 通用:ringfifo 接收缓存(每路一套)===== */ #define UART0_RX_BUFFER_SIZE 256U #define UART2_RX_BUFFER_SIZE 256U #define UART3_RX_BUFFER_SIZE 256U #define UART4_RX_BUFFER_SIZE 256U #define UART5_RX_BUFFER_SIZE 256U #define UART6_RX_BUFFER_SIZE 4096U #define UART7_RX_BUFFER_SIZE 256U #define UART0_BAUDRATE 115200U #define UART2_BAUDRATE 115200U #define UART3_BAUDRATE 2400U #define UART4_BAUDRATE 2400U #define UART5_BAUDRATE 115200U #define UART6_BAUDRATE 115200U #define UART7_BAUDRATE 115200U static _fifo_t s_uart0_rx; static _fifo_t s_uart2_rx; static _fifo_t s_uart3_rx; static _fifo_t s_uart4_rx; static _fifo_t s_uart5_rx; static _fifo_t s_uart6_rx; static _fifo_t s_uart7_rx; static uint8_t s_uart0_rx_mem[UART0_RX_BUFFER_SIZE]; static uint8_t s_uart2_rx_mem[UART2_RX_BUFFER_SIZE]; static uint8_t s_uart3_rx_mem[UART3_RX_BUFFER_SIZE]; static uint8_t s_uart4_rx_mem[UART4_RX_BUFFER_SIZE]; static uint8_t s_uart5_rx_mem[UART5_RX_BUFFER_SIZE]; static uint8_t s_uart6_rx_mem[UART6_RX_BUFFER_SIZE]; static uint8_t s_uart7_rx_mem[UART7_RX_BUFFER_SIZE]; /* 前置声明:避免 usart6_init 中调用时出现隐式声明 */ void uart6_init(void); /* 与 CCU601E_D 一致:按 slot 0~6 对应 7 路物理串口(实际编号 0/2/3/4/5/6/7),Shell 用 usart_printf 配置。 */ U8_T u8_uasrt_print[USART_NUM_PRINT_CNT] = {0}; static uint8_t usart_hw_num_to_slot(uint8_t usart_num) { switch (usart_num) { case 0: return 0U; case 2: return 1U; case 3: return 2U; case 4: return 3U; case 5: return 4U; case 6: return 5U; case 7: return 6U; default: return 0xFFU; } } static void usart_dump_line(const char *tag, uint8_t usart_num, const uint8_t *data, uint16_t len) { uint16_t i; if (data == NULL || len == 0U) { return; } (void)printf("\r\n--------------usart(%u) %s :\r\n", (unsigned)usart_num, tag); for (i = 0U; i < len; i++) { (void)printf("%02X ", (unsigned int)data[i]); } (void)printf("\r\n---------------%s end\r\n", tag); } /** * @brief 根据串口编号返回对应 ringfifo 句柄 * @param usart_num 串口编号 * @retval 对应的 _fifo_t 指针;不支持时返回 NULL */ static _fifo_t *get_fifo_by_id(uint8_t usart_num) { switch (usart_num) { case 0: return &s_uart0_rx; case 2: return &s_uart2_rx; case 3: return &s_uart3_rx; case 4: return &s_uart4_rx; case 5: return &s_uart5_rx; case 6: return &s_uart6_rx; case 7: return &s_uart7_rx; default: return NULL; } } /** * @brief 注册全部串口接收 FIFO(仅执行一次) * @retval none */ static void usart_fifo_register_all(void) { static uint8_t registered = 0U; if (registered) { return; } fifo_register(&s_uart0_rx, s_uart0_rx_mem, UART0_RX_BUFFER_SIZE, NULL, NULL); fifo_register(&s_uart2_rx, s_uart2_rx_mem, UART2_RX_BUFFER_SIZE, NULL, NULL); fifo_register(&s_uart3_rx, s_uart3_rx_mem, UART3_RX_BUFFER_SIZE, NULL, NULL); fifo_register(&s_uart4_rx, s_uart4_rx_mem, UART4_RX_BUFFER_SIZE, NULL, NULL); fifo_register(&s_uart5_rx, s_uart5_rx_mem, UART5_RX_BUFFER_SIZE, NULL, NULL); fifo_register(&s_uart6_rx, s_uart6_rx_mem, UART6_RX_BUFFER_SIZE, NULL, NULL); fifo_register(&s_uart7_rx, s_uart7_rx_mem, UART7_RX_BUFFER_SIZE, NULL, NULL); registered = 1U; } /* ===== 通用:阻塞式发送 ===== */ /** * @brief 阻塞发送指定长度数据 * @param periph 串口外设基地址 * @param data 待发送数据指针 * @param len 待发送长度 * @retval none */ static void usart_send_blocking(uint32_t periph, const uint8_t *data, uint16_t len) { for (uint16_t i = 0; i < len; i++) { while (RESET == usart_flag_get(periph, USART_FLAG_TBE)) {} usart_data_transmit(periph, data[i]); } while (RESET == usart_flag_get(periph, USART_FLAG_TC)) {} } static uint32_t usart_periph_from_id(uint8_t usart_id) { switch (usart_id) { case E_USART_0: return USART0; case E_USART_2: return USART2; case E_USART_3: return UART3; case E_USART_4: return UART4; case E_USART_5: return USART5; case E_USART_6: return UART6; case E_USART_7: return UART7; default: return 0U; } } /* ===== 各串口硬件初始化(集中在同一个文件里)===== */ /** * @brief USART0 初始化:PF4/PF5,115200,8N1 * @retval none */ void usart0_init(void) { rcu_periph_clock_enable(RCU_GPIOF); rcu_periph_clock_enable(RCU_USART0); gpio_af_set(GPIOF, GPIO_AF_4, GPIO_PIN_4); gpio_mode_set(GPIOF, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_4); gpio_output_options_set(GPIOF, GPIO_OTYPE_PP, GPIO_OSPEED_60MHZ, GPIO_PIN_4); gpio_af_set(GPIOF, GPIO_AF_4, GPIO_PIN_5); gpio_mode_set(GPIOF, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_5); usart_deinit(USART0); usart_word_length_set(USART0, USART_WL_8BIT); usart_stop_bit_set(USART0, USART_STB_1BIT); usart_parity_config(USART0, USART_PM_NONE); usart_baudrate_set(USART0, UART0_BAUDRATE); usart_hardware_flow_rts_config(USART0, USART_RTS_DISABLE); usart_hardware_flow_cts_config(USART0, USART_CTS_DISABLE); usart_receive_config(USART0, USART_RECEIVE_ENABLE); usart_transmit_config(USART0, USART_TRANSMIT_ENABLE); // nvic_irq_enable(USART0_IRQn, 3U, 1U); /* 已迁移到 sys_drv_init.c:nvic_configuration() */ usart_interrupt_enable(USART0, USART_INT_RBNE); usart_enable(USART0); } /** * @brief USART2 初始化:PD8/PD9,115200,8N1 * @retval none */ void usart2_init(void) { rcu_periph_clock_enable(RCU_GPIOD); rcu_periph_clock_enable(RCU_USART2); gpio_af_set(GPIOD, GPIO_AF_7, GPIO_PIN_8); gpio_mode_set(GPIOD, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_8); gpio_output_options_set(GPIOD, GPIO_OTYPE_PP, GPIO_OSPEED_60MHZ, GPIO_PIN_8); gpio_af_set(GPIOD, GPIO_AF_7, GPIO_PIN_9); gpio_mode_set(GPIOD, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_9); gpio_output_options_set(GPIOD, GPIO_OTYPE_PP, GPIO_OSPEED_60MHZ, GPIO_PIN_9); usart_deinit(USART2); usart_word_length_set(USART2, USART_WL_8BIT); usart_stop_bit_set(USART2, USART_STB_1BIT); usart_parity_config(USART2, USART_PM_NONE); usart_baudrate_set(USART2, UART2_BAUDRATE); usart_receive_config(USART2, USART_RECEIVE_ENABLE); usart_transmit_config(USART2, USART_TRANSMIT_ENABLE); // nvic_irq_enable(USART2_IRQn, 3U, 2U); /* 已迁移到 sys_drv_init.c:nvic_configuration() */ usart_interrupt_enable(USART2, USART_INT_RBNE); usart_enable(USART2); } /** * @brief UART3 初始化:PC10/PC11,2400,8E1 * @retval none */ void uart3_init(void) { rcu_periph_clock_enable(RCU_GPIOC); rcu_periph_clock_enable(RCU_UART3); gpio_af_set(GPIOC, GPIO_AF_8, GPIO_PIN_10); gpio_mode_set(GPIOC, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_10); gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_60MHZ, GPIO_PIN_10); gpio_af_set(GPIOC, GPIO_AF_8, GPIO_PIN_11); gpio_mode_set(GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_11); usart_deinit(UART3); /* 8E1: data(8) + parity(1) -> word length must be 9-bit on this peripheral */ usart_word_length_set(UART3, USART_WL_9BIT); usart_stop_bit_set(UART3, USART_STB_1BIT); usart_parity_config(UART3, USART_PM_EVEN); usart_baudrate_set(UART3, UART3_BAUDRATE); usart_hardware_flow_rts_config(UART3, USART_RTS_DISABLE); usart_hardware_flow_cts_config(UART3, USART_CTS_DISABLE); usart_receive_config(UART3, USART_RECEIVE_ENABLE); usart_transmit_config(UART3, USART_TRANSMIT_ENABLE); // nvic_irq_enable(UART3_IRQn, 3U, 0U); /* 已迁移到 sys_drv_init.c:nvic_configuration() */ usart_interrupt_enable(UART3, USART_INT_RBNE); usart_enable(UART3); } /** * @brief UART4 初始化:PC12/PD2,2400,8E1 * @retval none */ void uart4_init(void) { rcu_periph_clock_enable(RCU_GPIOC); rcu_periph_clock_enable(RCU_GPIOD); rcu_periph_clock_enable(RCU_UART4); gpio_af_set(GPIOC, GPIO_AF_8, GPIO_PIN_12); gpio_mode_set(GPIOC, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_12); gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_60MHZ, GPIO_PIN_12); gpio_af_set(GPIOD, GPIO_AF_8, GPIO_PIN_2); gpio_mode_set(GPIOD, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_2); usart_deinit(UART4); /* 8E1: data(8) + parity(1) -> word length must be 9-bit on this peripheral */ usart_word_length_set(UART4, USART_WL_9BIT); usart_stop_bit_set(UART4, USART_STB_1BIT); usart_parity_config(UART4, USART_PM_EVEN); usart_baudrate_set(UART4, UART4_BAUDRATE); usart_hardware_flow_rts_config(UART4, USART_RTS_DISABLE); usart_hardware_flow_cts_config(UART4, USART_CTS_DISABLE); usart_receive_config(UART4, USART_RECEIVE_ENABLE); usart_transmit_config(UART4, USART_TRANSMIT_ENABLE); // nvic_irq_enable(UART4_IRQn, 2U, 0U); /* 已迁移到 sys_drv_init.c:nvic_configuration() */ usart_interrupt_enable(UART4, USART_INT_RBNE); usart_enable(UART4); } /** * @brief USART5 初始化:PC6/PC7,115200,8N1 * @retval none */ void usart5_init(void) { rcu_periph_clock_enable(RCU_GPIOC); rcu_periph_clock_enable(RCU_USART5); gpio_af_set(GPIOC, GPIO_AF_7, GPIO_PIN_6); gpio_mode_set(GPIOC, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_6); gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_60MHZ, GPIO_PIN_6); gpio_af_set(GPIOC, GPIO_AF_7, GPIO_PIN_7); gpio_mode_set(GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_7); usart_deinit(USART5); usart_oversample_config(USART5, USART_OVSMOD_16); usart_word_length_set(USART5, USART_WL_8BIT); usart_stop_bit_set(USART5, USART_STB_1BIT); usart_parity_config(USART5, USART_PM_NONE); usart_baudrate_set(USART5, UART5_BAUDRATE); usart_hardware_flow_rts_config(USART5, USART_RTS_DISABLE); usart_hardware_flow_cts_config(USART5, USART_CTS_DISABLE); usart_receive_config(USART5, USART_RECEIVE_ENABLE); usart_transmit_config(USART5, USART_TRANSMIT_ENABLE); // nvic_irq_enable(USART5_IRQn, 1U, 0U); /* 已迁移到 sys_drv_init.c:nvic_configuration() */ usart_interrupt_enable(USART5, USART_INT_RBNE); usart_enable(USART5); } /** * @brief 兼容接口:旧代码调用 usart6_init,内部转到 uart6_init * @retval none */ void usart6_init(void) { /* 兼容旧头文件声明:usart6_init -> uart6_init */ uart6_init(); } /** * @brief UART6 初始化:PB4/PB3,115200,8N1 * @retval none */ void uart6_init(void) { rcu_periph_clock_enable(RCU_GPIOB); rcu_periph_clock_enable(RCU_UART6); gpio_af_set(GPIOB, GPIO_AF_11, GPIO_PIN_4); gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_4); gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_60MHZ, GPIO_PIN_4); gpio_af_set(GPIOB, GPIO_AF_11, GPIO_PIN_3); gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_3); usart_deinit(UART6); usart_word_length_set(UART6, USART_WL_8BIT); usart_stop_bit_set(UART6, USART_STB_1BIT); usart_parity_config(UART6, USART_PM_NONE); usart_baudrate_set(UART6, UART6_BAUDRATE); usart_hardware_flow_rts_config(UART6, USART_RTS_DISABLE); usart_hardware_flow_cts_config(UART6, USART_CTS_DISABLE); usart_receive_config(UART6, USART_RECEIVE_ENABLE); usart_transmit_config(UART6, USART_TRANSMIT_ENABLE); // nvic_irq_enable(UART6_IRQn, 0U, 0U); /* 已迁移到 sys_drv_init.c:nvic_configuration() */ usart_interrupt_enable(UART6, USART_INT_RBNE); usart_enable(UART6); } /** * @brief UART7 初始化:PE1/PE0,115200,8N1 * @retval none */ void uart7_init(void) { rcu_periph_clock_enable(RCU_GPIOE); rcu_periph_clock_enable(RCU_UART7); gpio_af_set(GPIOE, GPIO_AF_8, GPIO_PIN_1); gpio_mode_set(GPIOE, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_1); gpio_output_options_set(GPIOE, GPIO_OTYPE_PP, GPIO_OSPEED_60MHZ, GPIO_PIN_1); gpio_af_set(GPIOE, GPIO_AF_8, GPIO_PIN_0); gpio_mode_set(GPIOE, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_0); usart_deinit(UART7); usart_word_length_set(UART7, USART_WL_8BIT); usart_stop_bit_set(UART7, USART_STB_1BIT); usart_parity_config(UART7, USART_PM_NONE); usart_baudrate_set(UART7, UART7_BAUDRATE); usart_hardware_flow_rts_config(UART7, USART_RTS_DISABLE); usart_hardware_flow_cts_config(UART7, USART_CTS_DISABLE); usart_receive_config(UART7, USART_RECEIVE_ENABLE); usart_transmit_config(UART7, USART_TRANSMIT_ENABLE); // nvic_irq_enable(UART7_IRQn, 3U, 7U); /* 已迁移到 sys_drv_init.c:nvic_configuration() */ usart_interrupt_enable(UART7, USART_INT_RBNE); usart_enable(UART7); } /* ===== 中断服务程序(集中管理)===== */ /** * @brief USART0 中断处理函数,接收数据压入 FIFO * @retval none */ void USART0_IRQHandler(void) { if (RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE)) { uint8_t b = usart_data_receive(USART0); fifo_write(&s_uart0_rx, &b, 1U); usart_interrupt_flag_clear(USART0, USART_INT_FLAG_RBNE); } } /** * @brief USART2 中断处理函数,接收数据压入 FIFO * @retval none */ void USART2_IRQHandler(void) { if (RESET != usart_interrupt_flag_get(USART2, USART_INT_FLAG_RBNE)) { uint8_t b = usart_data_receive(USART2); fifo_write(&s_uart2_rx, &b, 1U); usart_interrupt_flag_clear(USART2, USART_INT_FLAG_RBNE); } } /** * @brief UART3 中断处理函数,接收数据压入 FIFO * @retval none */ void UART3_IRQHandler(void) { if (RESET != usart_flag_get(UART3, USART_FLAG_RBNE)) { uint8_t b = usart_data_receive(UART3); fifo_write(&s_uart3_rx, &b, 1U); } } /** * @brief UART4 中断处理函数,接收数据压入 FIFO * @retval none */ void UART4_IRQHandler(void) { if (RESET != usart_flag_get(UART4, USART_FLAG_RBNE)) { uint8_t b = usart_data_receive(UART4); fifo_write(&s_uart4_rx, &b, 1U); } } /** * @brief USART5 中断处理函数,接收数据压入 FIFO * @retval none */ void USART5_IRQHandler(void) { if (RESET != usart_interrupt_flag_get(USART5, USART_INT_FLAG_RBNE)) { uint8_t b = usart_data_receive(USART5); fifo_write(&s_uart5_rx, &b, 1U); } } /** * @brief UART6 中断处理函数,接收数据压入 FIFO * @retval none */ void UART6_IRQHandler(void) { if (RESET != usart_interrupt_flag_get(UART6, USART_INT_FLAG_RBNE)) { uint8_t b = usart_data_receive(UART6); fifo_write(&s_uart6_rx, &b, 1U); } } /** * @brief UART7 中断处理函数,接收数据压入 FIFO * @retval none */ void UART7_IRQHandler(void) { if (RESET != usart_interrupt_flag_get(UART7, USART_INT_FLAG_RBNE)) { uint8_t b = usart_data_receive(UART7); fifo_write(&s_uart7_rx, &b, 1U); } } /* ===== 对外:兼容现有统一API ===== */ /** * @brief 初始化指定串口 * @param usart_num 串口编号(0/2/3/4/5/6/7) * @retval 0 初始化成功;-1 参数不支持 */ int v_usart_init(uint8_t usart_num) { usart_fifo_register_all(); switch (usart_num) { case 0: usart0_init(); return 0; case 2: usart2_init(); return 0; case 3: uart3_init(); return 0; case 4: uart4_init(); return 0; case 5: usart5_init(); return 0; case 6: uart6_init(); return 0; case 7: uart7_init(); return 0; default: return -1; } } /** * @brief 批量初始化全部串口 * @retval 0 */ int v_usart_init_all(void) { usart_fifo_register_all(); usart0_init(); usart2_init(); uart3_init(); uart4_init(); usart5_init(); uart6_init(); uart7_init(); return 0; } /** * @brief 统一发送二进制数据接口 * @param usart_num 串口编号 * @param data 发送缓冲区 * @param len 发送长度 * @retval 0 发送成功;-1 参数或编号不支持 */ int v_usart_send_data(uint8_t usart_num, uint8_t *data, uint16_t len) { if (data == NULL || len == 0U) return -1; switch (usart_num) { case 0: usart_send_blocking(USART0, data, len); return 0; case 2: usart_send_blocking(USART2, data, len); return 0; case 3: usart_send_blocking(UART3, data, len); return 0; case 4: usart_send_blocking(UART4, data, len); return 0; case 5: usart_send_blocking(USART5, data, len); return 0; case 6: usart_send_blocking(UART6, data, len); return 0; case 7: usart_send_blocking(UART7, data, len); return 0; default: return -1; } } /** * @brief 查询指定串口接收 FIFO 的有效数据长度 * @param usart_num 串口编号 * @retval >=0 有效数据长度;-1 编号不支持 */ int v_usart_get_rx_data_count(uint8_t usart_num) { _fifo_t *f = get_fifo_by_id(usart_num); if (f == NULL) return -1; return (int)fifo_get_occupy_size(f); } /** * @brief 从指定串口 FIFO 读取数据 * @param usart_num 串口编号 * @param data 读取目标缓冲区 * @param len 期望读取长度 * @retval >=0 实际读取长度;-1 参数或编号不支持 */ int v_usart_read_rx_buffer(uint8_t usart_num, uint8_t *data, uint16_t len) { if (data == NULL || len == 0U) return -1; _fifo_t *f = get_fifo_by_id(usart_num); if (f == NULL) return -1; return (int)fifo_read(f, data, len); } /** * @brief 快速读取接口(当前复用普通读取实现) * @param usart_num 串口编号 * @param data 读取目标缓冲区 * @param len 期望读取长度 * @retval >=0 实际读取长度;-1 参数或编号不支持 */ int v_usart_read_rx_buffer_fast(uint8_t usart_num, uint8_t *data, uint16_t len) { return v_usart_read_rx_buffer(usart_num, data, len); } /** * @brief 清空指定串口接收 FIFO * @param usart_num 串口编号 * @retval 0 清空成功;-1 编号不支持 */ int v_usart_clear_rx_buffer(uint8_t usart_num) { _fifo_t *f = get_fifo_by_id(usart_num); if (f == NULL) return -1; fifo_release(f); switch (usart_num) { case 0: fifo_register(f, s_uart0_rx_mem, UART0_RX_BUFFER_SIZE, NULL, NULL); break; case 2: fifo_register(f, s_uart2_rx_mem, UART2_RX_BUFFER_SIZE, NULL, NULL); break; case 3: fifo_register(f, s_uart3_rx_mem, UART3_RX_BUFFER_SIZE, NULL, NULL); break; case 4: fifo_register(f, s_uart4_rx_mem, UART4_RX_BUFFER_SIZE, NULL, NULL); break; case 5: fifo_register(f, s_uart5_rx_mem, UART5_RX_BUFFER_SIZE, NULL, NULL); break; case 6: fifo_register(f, s_uart6_rx_mem, UART6_RX_BUFFER_SIZE, NULL, NULL); break; case 7: fifo_register(f, s_uart7_rx_mem, UART7_RX_BUFFER_SIZE, NULL, NULL); break; default: return -1; } return 0; } /** * @brief 对外统一二进制接收接口 * @param usart_id 串口ID * @param u8_recvBuf 接收缓存 * @param u16_len 期望读取长度 * @retval 实际读取长度,失败返回0 */ uint16_t u16_usart_recv(E_USART_ID usart_id ,uint8_t* u8_recvBuf ,uint16_t u16_len) { if(usart_id == E_USART_3) { //printf("\r\n"); } int ret = v_usart_read_rx_buffer((uint8_t)usart_id, u8_recvBuf, u16_len); if (ret <= 0) { return 0U; } #if 1 { uint8_t slot = usart_hw_num_to_slot((uint8_t)usart_id); if ((ret > 0) && (slot < USART_NUM_PRINT_CNT) && (u8_uasrt_print[slot] != 0U)) { usart_dump_line("recv", (uint8_t)usart_id, u8_recvBuf, (uint16_t)ret); } } #endif return (uint16_t)ret; } /** * @brief 对外统一二进制发送接口 * @param usart_id 串口ID * @param u8_sendBuf 发送缓存 * @param u16_len 待发送长度 * @retval 实际发送长度,失败返回0 */ uint16_t u16_usart_send(E_USART_ID usart_id ,uint8_t* u8_sendBuf ,uint16_t u16_len) { int ret; #if 1 { uint8_t slot = usart_hw_num_to_slot((uint8_t)usart_id); if ((u16_len > 0U) && (slot < USART_NUM_PRINT_CNT) && (u8_uasrt_print[slot] != 0U)) { usart_dump_line("send", (uint8_t)usart_id, u8_sendBuf, u16_len); } } #endif ret = v_usart_send_data((uint8_t)usart_id, u8_sendBuf, u16_len); if (ret < 0) { return 0U; } return u16_len; } /** * @brief 按串口编号打印格式化字符串 * @param usart_num 串口编号 * @param format 格式化字符串 * @param ... 可变参数 * @retval >0 输出长度;<=0 表示失败或无输出 */ int v_usart_printf(uint8_t usart_num, const char *format, ...) { char buf[256]; va_list args; va_start(args, format); int n = vsnprintf(buf, sizeof(buf), format, args); va_end(args); if (n <= 0) return n; if ((size_t)n >= sizeof(buf)) n = (int)(sizeof(buf) - 1U); v_usart_send_data(usart_num, (uint8_t *)buf, (uint16_t)n); return n; } void printf_hex(const char *mag , U8_T *data , U16_T len) { U16_T i; const char *tag = (mag != NULL) ? mag : ""; if (data == NULL) { printf("%s (00): \r\n", tag); return; } printf("%s (%02u): ", tag, (unsigned int)len); for (i = 0U; i < len; i++) { printf("%02X ", (unsigned int)data[i]); } printf("\r\n"); } /** * @brief printf 重定向接口,默认输出到 USART5 * @param ch 字符 * @param f 文件指针(未使用) * @retval 原字符 ch */ int fputc(int ch, FILE *f) { (void)f; uint8_t b = (uint8_t)ch; uint32_t debug_periph; if ((uint8_t)DEBUG_SHELL_USART_ID == (uint8_t)DEBUG_SHELL_USART_NULL) { return ch; } debug_periph = usart_periph_from_id((uint8_t)DEBUG_SHELL_USART_ID); if (debug_periph != 0U) { usart_send_blocking(debug_periph, &b, 1U); } return ch; }