一伺帘、簡(jiǎn)介
UARTE 是帶有 EasyDMA 的通用異步接收器/發(fā)送器 UART。提供快速则果、全雙工幔翰、異步的串口通信,內(nèi)置流量控制(CTS西壮,RTS)支持硬件遗增,速率高達(dá) 1 Mbps。
以下是 UARTE 的主要功能:
- 全雙工操作
- 自動(dòng)硬件流控制
- 生成9位數(shù)據(jù)帶奇偶校驗(yàn)
- EasyDMA
- 波特率高達(dá) 1 Mbps
- 在支持的事務(wù)之間返回 IDLE(使用HW流控制時(shí))
- 一個(gè)停止位
- 最低有效位(LSB)優(yōu)先
用于每個(gè) UART 接口的 GPIO 可以從設(shè)備上的任何 GPIO 來(lái)選擇并且獨(dú)立地為可配置的款青。這使得能夠在器件的引腳和有效地利用電路板空間和信號(hào)路有很大的靈活性做修。
二、硬件連接
功能 | 引腳 | 描述 |
---|---|---|
TXD | 6 | 串口發(fā)送端 |
RXD | 8 | 串口接收端 |
RTS | 5 | 流量控制發(fā)送請(qǐng)求抡草、低有效 |
CTS | 7 | 流量控制發(fā)送清除饰及、低有效 |
三、移植文件
注意:以下出現(xiàn)缺失common.h文件錯(cuò)誤康震,去除即可燎含。uint8改為uint8_t或unsigned char或自己宏定義
鏈接:https://pan.baidu.com/s/1GSyoRfMyLhImTV--5VZPMw 提取碼:71at
將 board_uart.c 和 board_uart.h 兩個(gè)文件加入工程的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 串口驅(qū)動(dòng)初始化
@param 無(wú)
@return 無(wú)
*/
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, // 關(guān)掉流控
.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 串口寫(xiě)數(shù)據(jù)函數(shù)
@param pData -[in] 寫(xiě)入數(shù)據(jù)
@param dataLen -[in] 寫(xiě)入數(shù)據(jù)長(zhǎng)度
@return 無(wú)
*/
void UART_WriteData(uint8 *pData, uint8 dataLen)
{
uint8 i;
for(i = 0; i < dataLen; i++)
{
app_uart_put(pData[i]);
}
}
/**
@brief 串口讀數(shù)據(jù)函數(shù)
@param pData -[out] 讀取數(shù)據(jù)
@return 無(wú)
*/
void UART_ReadData(uint8 *pData)
{
uint32 errCode;
errCode = app_uart_get(pData);
APP_ERROR_CHECK(errCode);
}
/*********************************************************************
* LOCAL FUNCTIONS
*/
/**
@brief 串口讀取數(shù)據(jù)處理函數(shù)
@param pEvent -[in] 串口事件
@return 無(wú)
*/
static void uart_handleIrqEvent(app_uart_evt_t *pEvent)
{
switch(pEvent->evt_type)
{
case APP_UART_DATA_READY: // 已接收到UART數(shù)據(jù)
break;
case APP_UART_COMMUNICATION_ERROR: // 接收過(guò)程中發(fā)生通信錯(cuò)誤
APP_ERROR_HANDLER(pEvent->data.error_communication);
break;
case APP_UART_FIFO_ERROR: // app_uart模塊使用的FIFO模塊中出現(xiàn)錯(cuò)誤
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調(diào)用
需包含頭文件 board_uart.h
UART_Init
功能 | 初始化UART驅(qū)動(dòng) |
---|---|
函數(shù)定義 | void UART_Init(void) |
參數(shù) | 無(wú) |
返回 | 無(wú) |
UART_WriteData
功能 | 串口寫(xiě)數(shù)據(jù)函數(shù) |
---|---|
函數(shù)定義 | void UART_WriteData(uint8 *pData, uint8 dataLen) |
參數(shù) | pData:寫(xiě)入數(shù)據(jù) pdataLen:寫(xiě)入數(shù)據(jù)長(zhǎng)度 |
返回 | 無(wú) |
UART_ReadData
功能 | 串口讀數(shù)據(jù)函數(shù) |
---|---|
函數(shù)定義 | void UART_ReadData(uint8 *pData) |
參數(shù) | pData:讀取數(shù)據(jù) |
返回 | 無(wú) |
五腿短、SDK配置
點(diǎn)擊 sdk_config.h 文件
選擇 Configuration Wizard
nRF_Drivers 中勾選UART屏箍、UARTE绘梦、FIFO、STRERROR和RETARGET相關(guān)選項(xiàng)
有的工程 nRF_Libraries 沒(méi)有 APP_FIFO_ENABLED
铣除、APP_UART_ENABLED
谚咬、RETARGET_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
六尚粘、添加組件庫(kù)
在 nRF_Drivers 文件夾和 nRF_Libraries 文件夾確認(rèn)以下組件庫(kù)是否存在择卦,不存在則添加。
在主從一體的工程中后三個(gè)文件沒(méi)有郎嫁,需要添加:
- 添加 app_uart_fifo.c 和 retarget.c
- 添加 app_fifo.c
-
添加上述編譯文件路徑
七秉继、使用例子
1)添加頭文件
#include "board_uart.h"
2)添加初始化代碼(SDK15.3 中 ble_peripheral 的 ble_app_template 工程 main() 函數(shù)中)
加入 UART_Init()
int main(void)
{
bool erase_bonds;
/*-------------------------- 外設(shè)驅(qū)動(dòng)初始化 ---------------------------*/
// Initialize.
log_init(); // 日志驅(qū)動(dòng)初始化
timers_init(); // 定時(shí)器驅(qū)動(dòng)初始化(在此加入自定義定時(shí)器)
UART_Init(); // SI522驅(qū)動(dòng)初始化(含SPI)
/*-------------------------- 藍(lán)牙協(xié)議棧初始化 ---------------------------*/
power_management_init();
ble_stack_init(); // 協(xié)議棧初始化
gap_params_init();
gatt_init();
advertising_init(); // 廣播初始化
services_init(); // 服務(wù)初始化
conn_params_init(); // 連接參數(shù)初始化
peer_manager_init();
/*-------------------------- 開(kāi)啟應(yīng)用 ---------------------------*/
// Start execution.
NRF_LOG_INFO("Template example started.");
advertising_start(erase_bonds); // 開(kāi)啟廣播
application_timers_start(); // 定時(shí)器應(yīng)用開(kāi)啟(在此開(kāi)啟自定義定時(shí)器)
// Enter main loop.
for(;;)
{
idle_state_handle();
}
}
3)寫(xiě)入串口數(shù)據(jù)
uint8_t temp[1] = {0x01};
UART_WriteData((uint8 *)&temp, 1);
? 由 Leung 寫(xiě)于 2020 年 2 月 24 日
? 參考:青風(fēng)電子社區(qū)