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

六、添加組件庫

nRF_Drivers 文件夾和 nRF_Libraries 文件夾確認以下組件庫是否存在,不存在則添加。

在主從一體的工程中後三個文件沒有,需要添加:

  1. 添加 app_uart_fifo.cretarget.c


  2. 添加 app_fifo.c

  3. 添加上述編譯文件路徑

七、使用例子

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 日

• 參考:青風電子社區

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章