nRF52832的串口分为: UART 和 UARTE
UART
UART就是通常所指的串口,通用异步收发器,全双工通讯,其内部结构如下:
UARTE
UARTE是带有 EasyDMA 的UART,其内部结构如下:我们在上一节讲到过,nRF52832的 IO 口没有固定死哪个IO必须对应哪个外设,所以一般的 IO口都可以作为串口使用
UART的使用Demo
//设置配置参数结构体
const app_uart_comm_params_t comm_params =
{
RX_PIN_NUMBER, //RX引脚设置,根据自己的硬件来,任意IO口都可以,注意避开特殊用途的就行
TX_PIN_NUMBER, //同 RX
RTS_PIN_NUMBER, //RTS 引脚和CTS 引脚,一般不使用流控,所以不会用,但是不确定是否能随意设置
CTS_PIN_NUMBER, //
APP_UART_FLOW_CONTROL_DISABLED,/*库函数中的注释*< UART Hw Flow Control is disabled.禁止流控 */
false,///**< Even parity if TRUE, no parity if FALSE. 奇偶校验,True有,..*/
#if defined (UART_PRESENT)
NRF_UART_BAUDRATE_57600 //波特率
#else
NRF_UARTE_BAUDRATE_115200
#endif
};
//串口初始化(FIFO)
/**@brief Macro for safe initialization of the UART module in a single user instance when using
* a FIFO together with UART.
*
* @param[in] P_COMM_PARAMS Pointer to a UART communication structure: app_uart_comm_params_t
* @param[in] RX_BUF_SIZE Size of desired RX buffer, must be a power of 2 or ZERO (No FIFO).
* @param[in] TX_BUF_SIZE Size of desired TX buffer, must be a power of 2 or ZERO (No FIFO).
* @param[in] EVT_HANDLER Event handler function to be called when an event occurs in the
* UART module.
* @param[in] IRQ_PRIO IRQ priority, app_irq_priority_t, for the UART module irq handler.
* @param[out] ERR_CODE The return value of the UART initialization function will be
* written to this parameter.
*
* @note Since this macro allocates a buffer and registers the module as a GPIOTE user when flow
* control is enabled, it must only be called once.
*/
//串口初始化(FIFO)
APP_UART_FIFO_INIT(&comm_params,
UART_RX_BUF_SIZE,//RX缓冲区大小
UART_TX_BUF_SIZE,
uart_error_handle,//中断回调,错误处理
APP_IRQ_PRIORITY_LOWEST,//中断优先级
err_code);
APP_ERROR_CHECK(err_code);
一般来说,使用 UARTE中断接收,初始化和 UART一样,在回调函数中处理接收发送操作,参考下面的例子,例子是参考清风教程中的代码修改的:
#define UART_TX_BUF_SIZE 256
#define UART_RX_BUF_SIZE 256
uint8_t commad[] = {0x55,0x44,0xA7,0x88};
void UART_WriteData(uint8_t *pData, uint8_t dataLen)
{
uint8_t i;
for(i = 0; i < dataLen; i++)
{
app_uart_put(pData[i]);
}
}
void uart_error_handle(app_uart_evt_t * p_event)
{
uint8_t RX;
if (p_event->evt_type == APP_UART_COMMUNICATION_ERROR)
{
APP_ERROR_HANDLER(p_event->data.error_communication);
}
else if (p_event->evt_type == APP_UART_FIFO_ERROR)
{
APP_ERROR_HANDLER(p_event->data.error_code);
}
else if (p_event->evt_type == APP_UART_DATA_READY)
{
//从FIFO中读取数据
app_uart_get(&RX);
//串口输出数据
printf("%c",RX);
UART_WriteData(commad,sizeof(commad));
}
//串口发送完成事件,主函数找中的循环发送测试也会触发这个事件
else if (p_event->evt_type == APP_UART_TX_EMPTY)
{
nrf_gpio_pin_toggle(LED_1);
}
}
int main(void)
{
LEDS_CONFIGURE(LEDS_MASK);
LEDS_OFF(LEDS_MASK);
uint32_t err_code;
const app_uart_comm_params_t comm_params =
{
RX_PIN_NUMBER,
TX_PIN_NUMBER,
RTS_PIN_NUMBER,
CTS_PIN_NUMBER,
APP_UART_FLOW_CONTROL_DISABLED,
false,
UART_BAUDRATE_BAUDRATE_Baud115200
};
APP_UART_FIFO_INIT(&comm_params,
UART_RX_BUF_SIZE,
UART_TX_BUF_SIZE,
uart_error_handle,
APP_IRQ_PRIORITY_LOW,
err_code);
APP_ERROR_CHECK(err_code);
while (1)
{
UART_WriteData(commad,sizeof(commad));//发送完成也会进入中断回调函数
nrf_delay_ms(1000);
nrf_delay_ms(1000);
}
}