Universal Asynchronous Receiver/Transmitter (UART)
The Universal Asynchronous Receiver/Transmitter offers fast, full-duplex, asynchronous serial communication with built-in flow control (CTS, RTS) support in hardware up to 1 Mbps baud. Parity checking is supported.
The GPIOs used for each UART interface line can be chosen from any GPIO on the device and are independently configurable. This enables great flexibility in device pinout and efficient use of board space and signal routing.
通用异步接收/发送器提供快速,全双工,内置流控制(CTS, RTS)的异步串行通信硬件支持高达1mbps波特率。 支持奇偶校验。
用于每个UART接口线的GPIO可以从设备上的任何GPIO中选择,并且是独立配置的。 这使得设备引脚具有很大的灵活性,并能有效地利用板空间和信号路由。
/********************************************************************************
* @file bsp_uart.c
* @author jianqiang.xue
* @version V1.0.0
* @date 2021-04-13
* @brief uart驱动
********************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include #include #include #include "RTE_Components.h" #include CMSIS_device_header #include "nrf_uart.h" #include "app_uart.h" #include "bsp_gpio.h" #include "bsp_uart.h" #include "boards.h" #include "business_gpio.h" #include "business_function.h" /* Private Define ------------------------------------------------------------*/ /* External Variables --------------------------------------------------------*/ /* Private Typedef -----------------------------------------------------------*/ typedef void(*bsp_uart_callback)(void); /* Private Variables ---------------------------------------------------------*/ #if BS_UART0_EN #define BSP_UART0_IRQ_HANDLER uart0_event_handle // 定义串口缓存区 uint8_t bsp_uart0_tx_buff[BS_UART0_CACHE_SIZE] = {0}; uint8_t bsp_uart0_rx_buff[BS_UART0_CACHE_SIZE] = {0}; // 定义串口初始化标记位 0--未初始化 1--初始化完成 bool g_uart0_init = false; // 定义串口发送标记位 0--free闲 1--bus忙 bool g_uart0_send_lock = false; uint16_t bsp_uart0_rx_buff_position = 0; static bsp_uart_callback uart0_irq_rx_callback; // 定义串口信息初始化结构体 static app_uart_comm_params_t uart0_comm_params = { .rx_pin_no = BS_UART0_RX_PIN, .tx_pin_no = BS_UART0_TX_PIN, .rts_pin_no = RTS_PIN_NUMBER, .cts_pin_no = CTS_PIN_NUMBER, .flow_control = APP_UART_FLOW_CONTROL_DISABLED, .use_parity = false, .baud_rate = NRF_UART_BAUDRATE_115200 }; #endif /* Private Function Prototypes -----------------------------------------------*/ /** * @brief Rx Transfer completed callbacks. * @note NULL * @param p_event: Struct containing events from the UART module. * @retval None */ static void BSP_UART0_IRQ_HANDLER(app_uart_evt_t * p_event) { if (p_event->evt_type == APP_UART_DATA_READY) { app_uart_get(&bsp_uart0_rx_buff[bsp_uart0_rx_buff_position]); if (bsp_uart0_rx_buff_position < (BS_UART0_CACHE_SIZE - 1)) { bsp_uart0_rx_buff_position++; } if (uart0_irq_rx_callback) { uart0_irq_rx_callback(); } } } /* Public Function Prototypes ------------------------------------------------*/ /** * @brief 设置串口波特率 * @note NULL * @param uart: 串口组号 * @param baud: 波特率 * @retval None */ void biz_uart_set_baud_rate(bsp_uart_t uart, uint32_t baud) { if (uart == BSP_UART_0) { if (baud == 115200) { uart0_comm_params.baud_rate = NRF_UART_BAUDRATE_115200; } else if (baud == 19200) { uart0_comm_params.baud_rate = NRF_UART_BAUDRATE_19200; } } } /** * @brief 串口初始化 * @note None * @param uart: 串口组号 * @retval None */ void bsp_uart_init(bsp_uart_t uart) { if (uart == BSP_UART_0) { #if BS_UART0_EN if (g_uart0_init) { return; } uint32_t err_code; APP_UART_FIFO_INIT(&uart0_comm_params, BS_UART0_CACHE_SIZE, BS_UART0_CACHE_SIZE, uart0_event_handle, APP_IRQ_PRIORITY_LOWEST, err_code); APP_ERROR_CHECK(err_code); bsp_uart0_rx_buff_position = 0; g_uart0_init = true; #endif } } /** * @brief 串口反注册 关闭串口时钟并复位引脚 * @note NULL * @param uart: 串口组号 * @retval None */ void bsp_uart_deinit(bsp_uart_t uart) { if (uart == BSP_UART_0) { #if BS_UART0_EN if (!g_uart0_init) { return; } app_uart_close(); g_uart0_init = false; #endif } } /** * @brief 注册串口接收回调函数 * @note NULL * @param uart: 串口组号 * @param event: 事件回调函数 * @retval 0--失败 1--成功 */ bool bsp_uart_rx_irq_callback(bsp_uart_t uart, void *event) { if (uart == BSP_UART_0) { #if BS_UART0_EN if (uart0_irq_rx_callback != NULL) { return true; } else { uart0_irq_rx_callback = (bsp_uart_callback)event; } #endif } return false; } /************************************[uart] 使用函数************************************/ /** * @brief 发送一个字节 * @note NULL * @param uart: 串口组号 * @param data: 字节值 * @retval None */ void bsp_uart_send_byte(bsp_uart_t uart, uint8_t data) { if (uart == BSP_UART_0) { #if BS_UART0_EN if (!g_uart0_init) { return; } app_uart_put(data); #endif } return; } /** * @brief 发送多个字节(堵塞) * @note NULL * @param uart: 串口组号 * @param *data: 数据头指针 * @param len: 数据长度 * @retval None */ void bsp_uart_send_nbyte(bsp_uart_t uart, uint8_t *data, uint16_t len) { if (uart == BSP_UART_0) { #if BS_UART0_EN if (!g_uart0_init) { return; } if (g_uart0_send_lock) { return; } g_uart0_send_lock = true; if (data != NULL) { for (uint16_t i = 0; i < len; i++) { app_uart_put(data[i]); } } else { for (uint16_t i = 0; i < len; i++) { app_uart_put(bsp_uart0_tx_buff[i]); } } g_uart0_send_lock = false; #endif } return; } /** * @brief 发送多个字节(非堵塞) 一般DMA方式 * @note NULL * @param uart: 串口组号 * @param *data: 数据头指针 * @param len: 数据长度 * @retval None */ void bsp_uart_send_nbyte_nowait(bsp_uart_t uart, uint8_t *data, uint16_t len) { if (uart == BSP_UART_0) { #if BS_UART0_EN bsp_uart_send_nbyte(uart, data, len); #endif } return; } /** * @brief 得到txbuff头指针 * @note NULL * @param uart: 串口组号 * @retval None */ uint8_t *bsp_uart_get_txbuff(bsp_uart_t uart) { if (uart == BSP_UART_0) { #if BS_UART0_EN if (g_uart0_send_lock == false) { return bsp_uart0_tx_buff; } return NULL; #endif } return NULL; } /** * @brief 得到rxbuff头指针 * @note NULL * @param uart: 串口组号 * @retval None */
上一篇:[单片机框架][bsp层][nrf51822][nrf51422][nrf51802][bsp_rng] rng随机数生成器配置和使用
下一篇:[单片机框架][bsp层][nrf51822][nrf51422][nrf51802][bsp_led] LED配置和使用
推荐阅读最新更新时间:2024-10-20 14:08
设计资源 培训 开发板 精华推荐
- MC34072DR2G 低压快速数模转换器的典型应用
- MC34074ADR2G 有源高 Q 陷波滤波器运算放大器的典型应用
- esp32充电开发板
- AKD4951EN-B,AK4951EN 24 位音频编解码器评估板
- LTC3588EDD-1 热电能量收集器的典型应用电路
- 50PINRGB转反客科技40PINRGB
- STEVAL-ILL035V1,使用带有集成升压控制器的 LED7708 多通道 LED 背光驱动器的演示板
- 频率选择、RMS 响应射频检波器,动态范围为 90 dB,从 35 MHz 到 4.4 GHz
- EVAL-ADG2128EBZ,用于 PBX 数字基站线路模块的模拟开关评估板
- CH341A编程器