nRF51 SDK
|
Nordic UART Service(NUS) Application is a firmware example that emulates a serial port over BLE using the Nordic nRF51822 evaluation board (PCA10001). This is intended to serve as a peer to the phone application 'nRF UART' (available for iOS on Apple Store and for Android on Google Play Store) and also to demonstrate the usage of proprietary (vendor specific) service and characteristics with the S110 SoftDevice.
The source code and project file can be found in the <InstallFolder>\Nordic\nrf51822\Board\pca10001\s110\experimental\ble_app_uart
folder.
The application includes one service namely the Nordic UART Service (UUID: 0x0001). The UUID of the Nordic UART Service is 6E400001-B5A3-F393-E0A9-E50E24DCCA9E.
This service exposes two characteristics - one for receiving and and another for transmitting, as seen from the nRF51822 application..
TX Characteristic (UUID: 6E400002-B5A3-F393-E0A9-E50E24DCCA9E):
When the peer has enabled notification for the Tx Characteristic, the application can send data to the peer as notifications. The application will transmit all data received over UART as notifications.
RX Characteristic (UUID: 6E400003-B5A3-F393-E0A9-E50E24DCCA9E):
Peer can start sending data to the device by writing to the Rx Characteristic of the service. ATT Write Request or ATT Write Command can be used. The data received is sent on the UART interface.
The initialization of the proprietary service and its characteristics are done in ble_nus.c.
The Nordic UART Service is added to the S110 SoftDevice as follows.
The RX characteristic is added to the SoftDevice as follows. Notice that the read and write permissions of the characteristic and its CCCD are set as 'open' - which means there are no security restrictions on this characteristic. Also note that the type of the UUID (ble_uuid.type) is the value that was returned in the call to sd_ble_uuid_vs_add(). The TX characteristic is also added in a similar way.
All application initialization and handling of data sent and received through BLE and UART are done in main.c.
The UART initialization is done as shown in the code below. This code segment uses the UART driver provided in the SDK to perform the UART configuration. Note that the last parameter to the simple_uart_config function indicates that the application intends to use Hardware Flow Control. This means the RTS_PIN_NUMBER and CTS_PIN_NUMBER will be used as Ready-to-Send and Clear-to-Send pins respectively. The code below then enables the UART interrupt.
When initializing the service in services_init() function, the application passes nus_data_handler to be used for handling the received data. When the Nordic UART Service indicates that there has been some data received over BLE from the peer, the same data is relayed to the UART. This function consists of the following piece of code.
The data received from the UART though undergoes certain checks before getting relayed to the BLE peer using the Nordic UART Service. The following code is inside the UART Interrupt handler which will be called each time a character is received over the UART. Each of these characters is buffered up into a string until a 'new line' character is received OR the size of the string exceeds the limit indicated by NUS_MAX_DATA_LENGTH. Once one of these two conditions is met, the string is sent over BLE using ble_nus_send_string function.
LED assignments:
Buttons assignments:
The UART Application can be tested using the application 'nRF UART' available for iOS and Android. It is listed as 'nRF UART' on Apple Store and on Google Play Store.
It can also be tested using the Master Control Panel as follows: