NRF52832学习笔记(12)——UART接口使用

一、简介

UARTE 是带有 EasyDMA 的通用异步接收器/发送器 UART。提供快速、全双工、异步的串口通信,内置流量控制(CTS,RTS)支持硬件,速率高达 1 Mbps。

以下是 UARTE 的主要功能:

  • 全双工操作
  • 自动硬件流控制
  • 生成9位数据带奇偶校验
  • EasyDMA
  • 波特率高达 1 Mbps
  • 在支持的事务之间返回 IDLE(使用HW流控制时)
  • 一个停止位
  • 最低有效位(LSB)优先

用于每个 UART 接口的 GPIO 可以从设备上的任何 GPIO 来选择并且独立地为可配置的。这使得能够在器件的引脚和有效地利用电路板空间和信号路有很大的灵活性。

二、硬件连接

功能 引脚 描述
TXD 6 串口发送端
RXD 8 串口接收端
RTS 5 流量控制发送请求、低有效
CTS 7 流量控制发送清除、低有效

三、移植文件

注意:以下出现缺失common.h文件错误,去除即可。uint8改为uint8_t或unsigned char或自己宏定义
链接:https://pan.baidu.com/s/1GSyoRfMyLhImTV–5VZPMw 提取码:71at
board_uart.cboard_uart.h 两个文件加入工程的Application文件夹下

3.1 board_uart.c

/*********************************************************************
 * INCLUDES
 */
#include "pca10040.h"
#include "nrf_uart.h"
#include "app_uart.h"

#include "board_uart.h"
#include "common.h"

static void uart_handleIrqEvent(app_uart_evt_t *pEvent);

/*********************************************************************
 * PUBLIC FUNCTIONS
 */
/**
 @brief 串口驱动初始化
 @param 无
 @return 无
*/
void UART_Init(void)
{
	uint32 errCode;
	app_uart_comm_params_t const commParams =
	{
		.rx_pin_no    = RX_PIN_NUMBER,
		.tx_pin_no    = TX_PIN_NUMBER,
		.rts_pin_no   = RTS_PIN_NUMBER,
		.cts_pin_no   = CTS_PIN_NUMBER,						
		.flow_control = APP_UART_FLOW_CONTROL_DISABLED,		// 关掉流控
		.use_parity   = false,
#if defined (UART_PRESENT)
		.baud_rate    = NRF_UART_BAUDRATE_115200			// 波特率
#else
		.baud_rate    = NRF_UARTE_BAUDRATE_115200
#endif
	};
    
	APP_UART_FIFO_INIT(&commParams, UART_RX_BUF_SIZE, UART_TX_BUF_SIZE,
						uart_handleIrqEvent, APP_IRQ_PRIORITY_LOWEST, errCode);
	APP_ERROR_CHECK(errCode);
}

/**
 @brief 串口写数据函数
 @param pData -[in] 写入数据
 @param dataLen -[in] 写入数据长度
 @return 无
*/
void UART_WriteData(uint8 *pData, uint8 dataLen)
{
	uint8 i;
	for(i = 0; i < dataLen; i++)
	{
		app_uart_put(pData[i]);
	}
}

/**
 @brief 串口读数据函数
 @param pData -[out] 读取数据
 @return 无
*/
void UART_ReadData(uint8 *pData)
{
	uint32 errCode;
	errCode = app_uart_get(pData);
	APP_ERROR_CHECK(errCode);
}


/*********************************************************************
 * LOCAL FUNCTIONS
 */
/**
 @brief 串口读取数据处理函数
 @param pEvent -[in] 串口事件
 @return 无
*/
static void uart_handleIrqEvent(app_uart_evt_t *pEvent)
{
    switch(pEvent->evt_type)
    {
	case APP_UART_DATA_READY:		// 已接收到UART数据
		break;

	case APP_UART_COMMUNICATION_ERROR:	// 接收过程中发生通信错误
		APP_ERROR_HANDLER(pEvent->data.error_communication);
		break;

	case APP_UART_FIFO_ERROR:		// app_uart模块使用的FIFO模块中出现错误
		APP_ERROR_HANDLER(pEvent->data.error_code);
		break;

	default:
		break;
	}
}

/****************************************************END OF FILE****************************************************/

3.2 board_uart.h

#ifndef _BOARD_UART_H_
#define _BOARD_UART_H_

/*********************************************************************
 * INCLUDES
 */
#include "common.h"

/*********************************************************************
 * DEFINITIONS
 */
#define UART_TX_BUF_SIZE                256		// UART TX buffer size
#define UART_RX_BUF_SIZE                256		// UART RX buffer size

/*********************************************************************
 * API FUNCTIONS
 */
void UART_Init(void);
void UART_WriteData(uint8 *pData, uint8 dataLen);
void UART_ReadData(uint8 *pData);

#endif /* _BOARD_UART_H_ */

四、API调用

需包含头文件 board_uart.h

UART_Init

功能 初始化UART驱动
函数定义 void UART_Init(void)
参数
返回

UART_WriteData

功能 串口写数据函数
函数定义 void UART_WriteData(uint8 *pData, uint8 dataLen)
参数 pData:写入数据
pdataLen:写入数据长度
返回

UART_ReadData

功能 串口读数据函数
函数定义 void UART_ReadData(uint8 *pData)
参数 pData:读取数据
返回

五、SDK配置

点击 sdk_config.h 文件

选择 Configuration Wizard

nRF_Drivers 中勾选UART、UARTE、FIFO、STRERROR和RETARGET相关选项

有的工程 nRF_Libraries 没有 APP_FIFO_ENABLEDAPP_UART_ENABLEDRETARGET_ENABLED,则在 sdk_config.h 6044行后加上

//==========================================================
// <q> APP_FIFO_ENABLED  - app_fifo - Software FIFO implementation
#ifndef APP_FIFO_ENABLED
#define APP_FIFO_ENABLED 1
#endif

// <e> APP_UART_ENABLED - app_uart - UART driver
//==========================================================
#ifndef APP_UART_ENABLED
#define APP_UART_ENABLED 1
#endif
// <o> APP_UART_DRIVER_INSTANCE  - UART instance used
 
// <0=> 0 

#ifndef APP_UART_DRIVER_INSTANCE
#define APP_UART_DRIVER_INSTANCE 0
#endif

// </e>

// <q> RETARGET_ENABLED  - retarget - Retargeting stdio functions
#ifndef RETARGET_ENABLED
#define RETARGET_ENABLED 1
#endif

六、使用例子

1)添加头文件

#include "board_uart.h"

2)添加初始化代码(SDK15.3 中 ble_peripheral 的 ble_app_template 工程 main() 函数中)
加入 UART_Init()

int main(void)
{
	bool erase_bonds;

    /*-------------------------- 外设驱动初始化 ---------------------------*/
	// Initialize.
    log_init();																	// 日志驱动初始化																	
    timers_init();																// 定时器驱动初始化(在此加入自定义定时器)
	UART_Init();																// SI522驱动初始化(含SPI)	
	
	/*-------------------------- 蓝牙协议栈初始化 ---------------------------*/
    power_management_init();
    ble_stack_init();															// 协议栈初始化
    gap_params_init();
    gatt_init();
    advertising_init();															// 广播初始化
    services_init();															// 服务初始化
    conn_params_init();															// 连接参数初始化
    peer_manager_init();
	
	/*-------------------------- 开启应用 ---------------------------*/
	// Start execution.
    NRF_LOG_INFO("Template example started."); 
    advertising_start(erase_bonds);												// 开启广播	
	application_timers_start();													// 定时器应用开启(在此开启自定义定时器)	
	
    // Enter main loop.
    for(;;)
    {
        idle_state_handle();
    }
}

3)写入串口数据

uint8_t temp[1] = {0x01};
UART_WriteData((uint8 *)&temp, 1);

• 由 Leung 写于 2020 年 2 月 24 日

• 参考:青风电子社区

发布了90 篇原创文章 · 获赞 136 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/qq_36347513/article/details/104478737