Files
mcu-codegen-template/App/Core/jb_uart.h
T

102 lines
3.2 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/******************************************************************************
* @file jb_uart.h
* @brief USART3 协议串口驱动(PB10=TX / PB11=RX
* @author cyWu <1917507415@qq.com>
* @date 2026.08.10
* @version V1.0.0
* @history
* - V1.0.0, 2026.08.10, cyWu, 首次发布
******************************************************************************/
#ifndef __JB_UART_H__
#define __JB_UART_H__
#include <stdint.h>
#include "py32f0xx_hal.h"
/*============================================================================*/
/* 宏定义(PY32F040 手册 PortB AFPB10/PB11 = USART3 @ AF4 */
/*============================================================================*/
#ifndef JB_UART_BAUDRATE
#define JB_UART_BAUDRATE (9600U)
#endif
#define JB_USART USART3
#define JB_USART_IRQn USART3_4_IRQn
#define JB_USART_CLK_ENABLE() __HAL_RCC_USART3_CLK_ENABLE()
#define JB_USART_TX_PIN GPIO_PIN_10
#define JB_USART_TX_GPIO_PORT GPIOB
#define JB_USART_TX_AF GPIO_AF4_USART3
#define JB_USART_TX_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
#define JB_USART_RX_PIN GPIO_PIN_11
#define JB_USART_RX_GPIO_PORT GPIOB
#define JB_USART_RX_AF GPIO_AF4_USART3
#define JB_USART_RX_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
/*============================================================================*/
/* 错误码 */
/*============================================================================*/
typedef enum {
JB_UART_OK = 0,
JB_UART_ERR_PARAM,
JB_UART_ERR_BUSY,
JB_UART_ERR_TIMEOUT,
JB_UART_ERR_NOT_INIT,
JB_UART_ERR_HAL,
JB_UART_ERR_UNKNOWN
} JbUart_Ret_t;
/**
* @brief 单字节 RX 回调(上层注册,驱动不依赖协议)
* @param byte 收到的字节
*/
typedef void (*JbUartRxCallback_t)(uint8_t byte);
/*============================================================================*/
/* 句柄(供 MSP / IRQ */
/*============================================================================*/
extern UART_HandleTypeDef g_jb_uart_handle;
#define UartHandle g_jb_uart_handle
/*============================================================================*/
/* 外部接口 */
/*============================================================================*/
/**
* @brief 注册 RX 回调(建议在 jb_uart_init 前调用)
* @param cb 回调,NULL 表示不向上层投递
*/
void jb_uart_set_rx_callback(JbUartRxCallback_t cb);
/**
* @brief 初始化 USART39600 8N1 + RX 中断
* @return JbUart_Ret_t
*/
JbUart_Ret_t jb_uart_init(void);
/**
* @brief 主循环调用:刷出 RX 调试日志
*/
void jb_uart_run(void);
/**
* @brief 阻塞发送
* @param buf 数据
* @param len 长度
* @return 成功返回 len,失败返回 -1
*/
int32_t jb_uart_write(uint8_t *buf, uint32_t len);
/**
* @brief 获取初始化状态
* @return JbUart_Ret_t
*/
JbUart_Ret_t jb_uart_get_status(void);
#endif /* __JB_UART_H__ */