nrf52官方串列埠例程
nrf52開發板拿到手快半年了,之前只跑過一次官方例程,然後就放一邊了,現在準備學習一下,首先從串列埠開始。
串列埠例程有兩個,這裡直接使用ble_app_uart這個例程,這是基於ble的例程,可以通過ble主機和開發版進行通訊,收發資料通過串列埠實現。
看了初始化函式,知道了預設波特率是38400,(可以通過修改巨集改變波特率),然後將例程編譯燒錄,例程跑起來了,但是串列埠助手卻沒有輸出任何資料,很奇怪,於是開關串列埠助手,偶爾會打印出來一個字元,不知道哪裡的問題,於是換了一個串列埠助手,還是一樣,拿手機可以連線ble,並且單步除錯也是可以執行的,一時不知道怎麼回事了,於是在網上找資料,看到一篇51串列埠講解的,串列埠配置圖都有,對比了一下,發下裡面還配置了 串列埠中斷超時100ms,以前從來沒有調過這個引數,然後找到自己助手裡有讀間隔超時1ms,改為100ms,奇蹟出現了,串列埠好了(自己已經改了波特率115200了),這個問題是字元傳輸間隔時間過程導致了超時。
串列埠通了,可以繼續學習nrf52了
將uart新增到其它工程非常方便,當然下面這樣操作是不推薦的,最快捷新增uart的方式是直接往main函式上面新增如下程式碼:
struct ble_nus_s;
/* Forward declaration of the ble_nus_t type. */
typedef struct ble_nus_s ble_nus_t;
/**@brief Nordic UART Service event handler type. */
typedef void (*ble_nus_data_handler_t) (ble_nus_t * p_nus, uint8_t * p_data, uint16_t length);
struct ble_nus_s
{
uint8_t uuid_type; /**< UUID type for Nordic UART Service Base UUID. */
uint16_t service_handle; /**< Handle of Nordic UART Service (as provided by the S110 SoftDevice). */
ble_gatts_char_handles_t tx_handles; /**< Handles related to the TX characteristic (as provided by the S110 SoftDevice). */
ble_gatts_char_handles_t rx_handles; /**< Handles related to the RX characteristic (as provided by the S110 SoftDevice). */
uint16_t conn_handle; /**< Handle of the current connection (as provided by the S110 SoftDevice). BLE_CONN_HANDLE_INVALID if not in a connection. */
bool is_notification_enabled; /**< Variable to indicate if the peer has enabled notification of the RX characteristic.*/
ble_nus_data_handler_t data_handler; /**< Event handler to be called for handling received data. */
};
/* Forward declaration of the ble_nus_t type. */
typedef struct ble_nus_s ble_nus_t;
#define BLE_UUID_NUS_SERVICE 0x0001 /**< The UUID of the Nordic UART Service. */
#define BLE_NUS_MAX_DATA_LEN (GATT_MTU_SIZE_DEFAULT - 3) /**< Maximum length of data (in bytes) that can be transmitted to the peer by the Nordic UART service module. */
static ble_nus_t m_nus; /**< Structure to identify the Nordic UART Service. */
/**@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' i.e '\n' (hex 0x0D) or if the string has reached a length of
* @ref NUS_MAX_DATA_LENGTH.
*/
/**@snippet [Handling the data received over UART] */
void uart_event_handle(app_uart_evt_t * p_event)
{
static uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
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[index]));
index++;
if ((data_array[index - 1] == '\n') || (index >= (BLE_NUS_MAX_DATA_LEN)))
{
// err_code = ble_nus_string_send(&m_nus, data_array, index);
if (err_code != NRF_ERROR_INVALID_STATE)
{
APP_ERROR_CHECK(err_code);
}
index = 0;
}
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;
}
}
/**@snippet [Handling the data received over UART] */
/**@brief Function for initializing the UART module.
*/
/**@snippet [UART Initialization] */
static void uart_init(void)
{
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_ENABLED,
false,
UART_BAUDRATE_BAUDRATE_Baud115200
};
APP_UART_FIFO_INIT( &comm_params,
UART_RX_BUF_SIZE,
UART_TX_BUF_SIZE,
uart_event_handle,
APP_IRQ_PRIORITY_LOW,
err_code);
APP_ERROR_CHECK(err_code);
}
/**@snippet [UART Initialization] */
/**@brief Function for application main entry.
*/
int main(void)
{
bool erase_bonds;
uint32_t err_code;
// Initialize.
app_trace_init();
timers_init();
buttons_leds_init(&erase_bonds);
ble_stack_init();
scheduler_init();
device_manager_init(erase_bonds);
gap_params_init();
advertising_init();
services_init();
sensor_simulator_init();
conn_params_init();
buffer_init();
//add by allen
uart_init();
// Start execution.
timers_start();
err_code = ble_advertising_start(BLE_ADV_MODE_FAST);
APP_ERROR_CHECK(err_code);
printf("hello nordic...\n");
// Enter main loop.
for (;;)
{
app_sched_execute();
power_manage();
}
}
/**
* @}
*/
這樣就能列印了,是不是很方便
閱讀原始碼發現demo裡面是自帶除錯資訊的,main函式中app_trace_init();就是初始化除錯資訊,也就是自帶串列埠驅動的,只需要新增巨集ENABLE_DEBUG_LOG_SUPPORT的定義即可開啟,看來nordic還是很人性化的,除錯起來方便多了。