1,增加代码
#if defined (UART_PRESENT)
#include "nrf_uart.h"
#endif
#include "app_uart.h"
#define UART_TX_BUF_SIZE 256 /**< UART TX buffer size. */
#define UART_RX_BUF_SIZE 256 /**< UART RX buffer size. */
void uart_write(uint8_t *pdata,uint8_t length)
{
int i;
for (i = 0; i < length; i++)
{
UNUSED_VARIABLE(app_uart_put(*pdata++));
}
}
/**@brief Function for handling app_uart events.
*
* @details This function will receive a single character from the app_uart module and append it to
* a string. The string will be be sent over BLE when the last character received was a
* 'new line' '\n' (hex 0x0A) or if the string has reached the maximum data length.
*/
/**@snippet [Handling the data received over UART] */
void uart_event_handle(app_uart_evt_t * p_event)
{
static uint8_t data_array[256];
// static uint8_t index = 0;
// uint32_t err_code;
switch (p_event->evt_type)
{
case APP_UART_DATA_READY:
UNUSED_VARIABLE(app_uart_get(&data_array[0]));
uart_write(data_array,1); //可以根据需要修改对收到的数据的处理。
break;
case APP_UART_COMMUNICATION_ERROR:
APP_ERROR_HANDLER(p_event->data.error_communication);
break;
case APP_UART_FIFO_ERROR:
APP_ERROR_HANDLER(p_event->data.error_code);
break;
default:
break;
}
}
/**@brief Function for initializing the UART module.
*/
/**@snippet [UART Initialization] */
static void uart_init(void)
{
uint32_t err_code;
app_uart_comm_params_t const comm_params =
{
.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(&comm_params,
UART_RX_BUF_SIZE,
UART_TX_BUF_SIZE,
uart_event_handle,
APP_IRQ_PRIORITY_LOWEST,
err_code);
APP_ERROR_CHECK(err_code);
}
/**@snippet [UART Initialization] */
2,main函数里面加入初始化的调用。
3,增加或者修改配置文件里的宏定义
//
//==========================================================
#ifndef APP_FIFO_ENABLED
#define APP_FIFO_ENABLED 1
#endif
#ifndef APP_UART_ENABLED
#define APP_UART_ENABLED 1
#endif
//
// <0=> 0
#ifndef APP_UART_DRIVER_INSTANCE
#define APP_UART_DRIVER_INSTANCE 0
#endif
#ifndef NRF_LOG_BACKEND_RTT_ENABLED
#define NRF_LOG_BACKEND_RTT_ENABLED 1
#endif
#ifndef NRF_LOG_BACKEND_UART_ENABLED
#define NRF_LOG_BACKEND_UART_ENABLED 0
#endif
4,增加相关的驱动文件
app_fifo.c ,app_uart_fifo.c 这两个文件在libraries文件夹下,找找。
5,修改驱动文件的头文件路径
$PROJ_DIR$\..\..\..\..\..\..\components\libraries\fifo
$PROJ_DIR$\..\..\..\..\..\..\components\libraries\uart
编译下载固件即可。管脚可以自行修改。
效果: