I am using a ultra-miniature (BMD-350) series module from Rigdo which has the nRF52832 BLE SoC. The pins from the nrf52 SoC are all brought out on a female headers on a custom PCB.
Firmware/SDK configuration:
- nRF5_SDK_15.3.0_59ac345 SDK
- pca10040 and softdevice s132
- nrf52832_xxaa
Debugger
- J-Link EDU Mini debug module and using Segger Ozone software for debugging.
nrf Android Apps:
- nrFConnect, nrF Logger and nrfUART app
I have made following changes to the code of ble_app_uart application example before I flash it on my nrf52 SoC:
- modules\nrfx\drivers\src\nrfx_uarte.c
- apply_config() function: configured pins mapped to uarte cts and rxd as pullup
- examples\ble_peripheral\ble_app_uart\main.c
- uart_init() function: set flow control flag to APP_UART_FLOW_CONTROL_ENABLED in the communication parameters configuration.
- Other functions: Added NRF_LOG_INFO statements to mainly log function calls and events
- components\ble\ble_services\ble_nus\ble_nus.c
- Other functions: Added NRF_LOG_INFO statements to mainly log function calls and events
I wanted to test ble_app_uart application in two modes, the first mode of serial communication with an external device having a UART (works) and loopback mode (doesn't work)
- Serial communication with an external device (WORKS)
- The external device is a UART protocol analyzer on a Mixed Signal Scope (Analog discovery).I configured it to 8-N-1 @115200 bps baud rate settings, identical to nrf52 UART communication settings in uart_init() function.
- I connect TXD, RXD pins of my external device UART protocol analyzer to RXD (pin 8) and TXD (6) pins of UART on nrf52. The CTS (pin 7) and RTS (pin 5) pins of nrf52 UART are connected to each other (since I had enabled flow control in code)
- Using nrfUART app,
- I first write (TX ATT request) a message to RX characteristic of UART service. I receive this message successfully on the RX console of my external UART protocol analyzer. In my JLINK debugger logs, I see following logged: : event BLE_GATTS_EVT_WRITE, event BLE_NUS_EVT_RX_DATA, and nrfx_uarte_tx function call with tx len.
- Next I send message from Tx console of external UART protocol analyzer and I receive this message successfully on the my UART app. In my debugger logs, I see following: event APP_UART_DATA_READY, nrfx_uarte_rx function call, event BLE_GATTS_EVT_HVN_TX_COMPLETE, notification enabled and event BLE_NUS_EVT_TX_RDY
- Below is the log generated by debugger:
<info> app: [2]ble_stack_init <info> app: [3]gatt_init <info> app: [4]services_init <info> app: [5]advertising_init <info> app: [6]conn_params_init <info> UARTE: Transfer tx_len: 1. <debug> UARTE: Tx data: <debug> UARTE: 0D |. <info> UARTE: Function: nrfx_uarte_tx, error code: NRF_SUCCESS. <info> UARTE: Transfer tx_len: 1. <debug> UARTE: Tx data: <debug> UARTE: 0A |. <info> app: Debug logging for UART over RTT started. <info> ble_nus: [79]ble_nus_on_ble_evt, Evt=BLE_GATTS_EVT_WRITE <info> app: [80]nus_data_handler <info> app: [81]nus_data_handler: evt=BLE_NUS_EVT_RX_DATA <debug> app: Received data from BLE NUS. Writing data on UART. <debug> app: 77 68 61 74 27 73 20 75|what's u <debug> app: 70 20 3F |p ? <info> UARTE: Transfer tx_len: 1. <debug> UARTE: Tx data: <debug> UARTE: 77 |w <info> UARTE: Function: nrfx_uarte_tx, error code: NRF_SUCCESS. <info> ble_nus: [82]on_write: p_nus->rx_handles.value_handle: 13 <debug> UARTE: Tx data: <debug> UARTE: 74 |t <info> UARTE: Function: nrfx_uarte_tx, error code: NRF_SUCCESS. <info> UARTE: Transfer tx_len: 1. <debug> UARTE: Tx data: <debug> UARTE: 27 |' <info> UARTE: Function: nrfx_uarte_tx, error code: NRF_SUCCESS. <info> UARTE: Transfer tx_len: 1. <debug> UARTE: Tx data: <debug> UARTE: 73 |s <info> UARTE: Function: nrfx_uarte_tx, error code: NRF_SUCCESS. <info> UARTE: Transfer tx_len: 1. <info> app: [84]uart_event_handle: evt_type=APP_UART_DATA_READY <info> UARTE: Transfer rx_len: 1. <info> UARTE: Function: nrfx_uarte_rx, error code: NRF_SUCCESS. <info> app: [85]uart_event_handle: evt_type=APP_UART_DATA_READY <info> UARTE: Transfer rx_len: 1. <info> UARTE: Function: nrfx_uarte_rx, error code: NRF_SUCCESS. <info> app: [86]uart_event_handle: evt_type=APP_UART_DATA_READY <info> UARTE: Transfer rx_len: 1. <info> UARTE: Function: nrfx_uarte_rx, error code: NRF_SUCCESS. <info> app: [88]uart_event_handle: evt_type=APP_UART_DATA_READY <info> UARTE: Transfer rx_len: 1. <info> UARTE: Function: nrfx_uarte_rx, error code: NRF_SUCCESS. <info> app: [89]uart_event_handle: evt_type=APP_UART_DATA_READY <info> UARTE: Transfer rx_len: 1. <info> UARTE: Function: nrfx_uarte_rx, error code: NRF_SUCCESS. <info> app: [90]uart_event_handle: evt_type=APP_UART_DATA_READY <info> UARTE: Transfer rx_len: 1. <info> UARTE: Function: nrfx_uarte_rx, error code: NRF_SUCCESS. <info> app: [92]uart_event_handle: evt_type=APP_UART_DATA_READY <info> UARTE: Transfer rx_len: 1. <info> UARTE: Function: nrfx_uarte_rx, error code: NRF_SUCCESS. <info> app: [93]uart_event_handle: evt_type=APP_UART_DATA_READY <debug> app: Ready to send data over BLE NUS <debug> app: 64 6F 69 6E 67 20 6F 6B|doing ok <info> ble_nus: [94]ble_nus_on_ble_evt, Evt=BLE_GATTS_EVT_HVN_TX_COMPLETE <info> ble_nus: [95]on_hvx_tx_complete: is notification enabled: true <info> app: [96]nus_data_handler <info> ble_nus: [97]on_hvx_tx_complete: event=BLE_NUS_EVT_TX_RDY
- Loopback mode: I tried two ways to do it, one hardware and other software but I am unable to get message loopback with both methods
- Hardware method (DOESN'T WORK)
- Connected RXD (pin 8) with TXD (6) pin and CTS (pin 7) with RTS (pin 5). Flow control is enabled in code as usual.
- Using nrfConnect app, I enabled notifications on the TX characteristic of UART service
- From nrfUART app, I write (TX ATT request) a message to RX characteristic of UART service. However, I fail to get the same message as loopback RX message on my nrfUART app. In the debug log, I can see following generated: event BLE_NUS_EVT_RX_DATA, function call nrfx_uarte_tx, event APP_UART_DATA_READY and event APP_UART_TX_EMPTY. However, I donot see BLE_GATTS_EVT_HVN_TX_COMPLETE being logged, not sure why ? Below is the debug log generated:
<info> ble_nus: [53]ble_nus_on_ble_evt, Evt=BLE_GATTS_EVT_WRITE <info> app: [54]nus_data_handler <info> app: [55]nus_data_handler: evt=BLE_NUS_EVT_RX_DATA <debug> app: Received data from BLE NUS. Writing data on UART. <debug> app: 55 50 |UP <info> UARTE: Transfer tx_len: 1. <debug> UARTE: Tx data: <debug> UARTE: 55 |U <info> UARTE: Function: nrfx_uarte_tx, error code: NRF_SUCCESS. <info> UARTE: Transfer tx_len: 1. <debug> UARTE: Tx data: <debug> UARTE: 50 |P <info> UARTE: Function: nrfx_uarte_tx, error code: NRF_SUCCESS. <info> app: [57]uart_event_handle: evt_type=APP_UART_DATA_READY <info> UARTE: Transfer rx_len: 1. <info> UARTE: Function: nrfx_uarte_rx, error code: NRF_SUCCESS. <info> app: [58]uart_event_handle: evt_type=APP_UART_TX_EMPTY <info> app: [59]uart_event_handle: evt_type=APP_UART_DATA_READY
- Software Method (DOESN'T WORK)
- I disabled the flow control in code
- I make following changes to my code but I cannot get a loopback message:
- nus_data_handler()
- Hardware method (DOESN'T WORK)
static void nus_data_handler(ble_nus_evt_t * p_evt) { uint8_t rx_byte; NRF_LOG_INFO("[%d]nus_data_handler", order++); if (p_evt->type == BLE_NUS_EVT_RX_DATA) { NRF_LOG_INFO("[%d]nus_data_handler: evt=BLE_NUS_EVT_RX_DATA", order++); uint32_t err_code; NRF_LOG_DEBUG("Received data from BLE NUS. Writing data on UART."); NRF_LOG_HEXDUMP_DEBUG(p_evt->params.rx_data.p_data, p_evt->params.rx_data.length); for (uint32_t i = 0; i < p_evt->params.rx_data.length; i++) { do { err_code = app_uart_put(p_evt->params.rx_data.p_data[i]); if ((err_code != NRF_SUCCESS) && (err_code != NRF_ERROR_BUSY)) { NRF_LOG_ERROR("Failed receiving NUS message. Error 0x%x. ", err_code); APP_ERROR_CHECK(err_code); } } while (err_code == NRF_ERROR_BUSY); nrf_delay_ms(10); err_code = app_uart_get(&rx_byte); if( ( rx_byte != p_evt->params.rx_data.p_data[i] ) && err_code != NRF_SUCCESS ) { NRF_LOG_ERROR("Failed to Loopback character 0x%x. Error: 0x%x", p_evt->params.rx_data.p_data[i], err_code ); } } if (p_evt->params.rx_data.p_data[p_evt->params.rx_data.length - 1] == '\r') { while (app_uart_put('\n') == NRF_ERROR_BUSY); nrf_delay_ms(10); err_code = app_uart_get(&rx_byte); if( ( rx_byte != p_evt->params.rx_data.p_data[p_evt->params.rx_data.length - 1] ) && err_code != NRF_SUCCESS ) { NRF_LOG_ERROR("Failed to Loopback character 0x%x. Error: 0x%x", p_evt->params.rx_data.p_data[p_evt->params.rx_data.length - 1], err_code ); } } } }
So what I am doing wrong that Loopback of UART over BLE is not working ?