1_ble nrf51822 uart and ble forwarding

Source: Internet
Author: User

Last year, noridc developed a chip that integrates Bluetooth 4.0 and can communicate with 24l01. The film encapsulation can be very small, and the power consumption is very low. It is very suitable for wearable devices, so I recently had a set of second-hand ak ii and no becon board. I first played some basic peripherals, And now I started to develop ble.

Under the official SDK directory, we can find the project file Nordic \ nrf51822 \ board \ pca10001 \ ble \ experimental \ ble_app_uart, in addition, the SDK documentation also briefly introduces the ble UART data forwarding application.

This application includes a service called Nordic UART Service (UUID: 0x0001 ).The UUID of the Nordic UART service is 6e400001-b5a3-f393-e0a9-e50e24dcca9e.

This service includes two features: one for receiving and the other for sending:

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 communications. The application will transmit all data transmitted ed over UART as communications.

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 has Ed is sent on the UART interface.


A Brief Introduction to the important code:

Proprietary service and Characteristic)

The initialization operation room for private services and features is inBle_nus.c,The process of adding the Nordic UART service to s110 softdevice is as follows:

// Add custom base UUID. err_code = sd_ble_uuid_vs_add (& nus_base_uuid, & p_nus-> uuid_type); If (err_code! = Nrf_success) {return err_code;} ble_uuid.type = p_nus-> uuid_type; ble_uuid.uuid = ble_uuid_nus_service; // Add service. err_code = sd_ble_gatts_service_add (ble_gatts_srvc_type_primary, & ble_uuid, & p_nus-> service_handle); the process of adding RX features to softdevice is as follows, the read and write permissions of the feature and Its CCCD are set to open, which means there are no security restrictions on the feature. Also, pay attention to the uuid type (ble_uuid.type) the value is obtained by calling sd_ble_uuid_vs_add (). The TX feature is also added in the same way:
Ble_gatts_char_md_t char_md; ble_gatts_attr_md_t cccd_md; ble_gatts_attr_t attr_char_value; ble_uuid_t ble_uuid; ble_gatts_attr_md_t attr_md; memset (& cccd_md, 0, sizeof (cccd_md); done (& found ); cccd_md.vloc = ble_gatts_vloc_stack; memset (& char_md, 0, sizeof (char_md); shard = 1; shard = NULL; char_md.p_char_pf = NULL; shard = & cccd_md; priority = NULL; ble_uuid.type = p_nus-> uuid_type; ble_uuid.uuid = priority; memset (& attr_md, 0, sizeof (attr_md); priority (& signature ); attr_md.vloc = success; success = 0; success = 0; attr_md.vlen = 1; memset (& attr_char_value, 0, sizeof (attr_char_value); success = & ble_uuid; success = & attr_md; outputs = sizeof (uint8_t); outputs = 0; attr_char_value.max_len = loads; loads (p_nus-> service_handle, & char_md, & attr_char_value, & p_nus-> rx_handles );

UART Initialization is related to initialization. receiving and sending data through ble and UART are implemented in Main. C. The UART initialization operation is as follows. The SDK provides Code related to the UART driver to perform UART configuration. Note that the last parameter hwfc of the simple_uart_config function indicates the hardware throttling enabling, that is, the RTS and CTS are used, and then the UART is interrupted.
Simple_uart_config (rts_pin_number, tx_pin_number, cts_pin_number, rx_pin_number, hwfc); nrf_uart0-> intenset = Priority <strong; nvic_setpriority (priority, priority); priority (priority );
Processing the data received through ble initializes the service in the services_init () function, and the application processes the data received through ble through the nus_data_handler function, at this time, the Nordic UART service indicates that data from the paired device already exists. The data will be forwarded to the UART. The related code is as follows:
Void nus_data_handler (ble_nus_t * p_nus, uint8_t * p_data, uint16_t length) {for (INT I = 0; I <length; I ++) {simple_uart_put (p_data [I]);} simple_uart_put ('\ n ');}
Process data received through UART

Some checks are performed before data from UART is forwarded to the ble pairing device. The following code calls the interrupt function after the data received by UART, these received characters are first cached in an array. If the received character is 'New line' or exceeds the length of nus_max_data_length, ble_nus_send_string is called and sent through ble.

Note: by default, nus_max_data_length is set to the maximum value, which is also the maximum length of the notification package. For example, ble_att_mtu-3 cannot be changed to a longer value.

Data_array [Index] = simple_uart_get (); index ++; If (data_array [index-1] = '\ n ') | (index >=( ble_nus_max_data_len-1) {err_code = ble_nus_send_string (& m_nus, data_array, index + 1); If (err_code! = Nrf_error_invalid_state) {app_error_check (err_code);} Index = 0 ;}

Distribution of buttons and LED lights:

Led0: advertising. The device name can be displayed by the paired device. The name is 'Nordic _ UART 'and will flash until the device is not connected.

Led1: connected. After connection, led0 is disabled and led1 is on

Button 0: when the system is off, pressing this button will wake up the system and start advertising


Tested hardware environment:

Redmi 1S Telecom version, ak ii, jlink.


Images of the previous tests:

Serial Port sending and receiving:



Android 4.3, nrf uart software connection, send and receive data:








Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.