nRF51 SDK - S110 SoftDevice
|
Nordic UART Service(NUS) Application is an example that emulates a serial port over BLE using the Nordic nRF51422 evaluation board (PCA10028). 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 name of the example is experimental_ble_app_uart_s110_pca10028. If you are not using the Keil Pack Installer, you can find the source code and project file of the example in the following folder: <InstallFolder>\Nordic\nrf51\examples\ble_peripheral\experimental_ble_app_uart
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 nRF51422 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):
The 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 that 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 the 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 undergoes certain checks before getting relayed to the BLE peer using the Nordic UART Service. The following code is inside the UART Interrupt handler that 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:
Button assignments in system-off mode:
The UART Application can be tested using the application 'nRF UART', which is available for iOS and Android. It is listed as 'nRF UART' on Apple Store and on Google Play Store.
You can also test the application with the Master Control Panel by performing the following steps: