nRF51822蓝牙开发
文章目錄
- 一、nRF51822芯片介紹
- 二、Keil環(huán)境搭建
- 1、下載軟件包
- 2、選擇設(shè)備
- 3、配置JLINK
- 三、nRFgo的下載
- 四、藍(lán)牙協(xié)議棧
- (一)、S110
- (二)、S120
- (三)、S130
- 五、串口透傳例程
- (一)、應(yīng)用程序定時(shí)器(心跳)初始化
- (二)、串口初始化
- (三)、flash初始化
- (四)、定時(shí)器初始化
- (五)、藍(lán)牙協(xié)議棧初始化
- (六)、GAP參數(shù)初始化
- (七)、服務(wù)初始化
- (八)、GAP廣播初始化
- (九)、藍(lán)牙連接參數(shù)初始化
- (十)、開啟廣播
- (十一)、低功耗模式
一、nRF51822芯片介紹
低功耗藍(lán)牙和2.4 GHz SoC。nRF51822是一款通用的超低功耗SoC,非常適合Bluetooth?低功耗和2.4 GHz專有無線應(yīng)用。它基于具有256/128KB閃存和32/16KB RAM的32位ARM?Cortex?-M0CPU構(gòu)建。靈活的2.4 GHz無線電支持藍(lán)牙低功耗和2.4 GHz專有協(xié)議,例如Gazell。它集成了豐富的模擬和數(shù)字外設(shè),可以通過可編程外設(shè)互連(PPI)系統(tǒng)直接進(jìn)行交互,而無需CPU干預(yù)。靈活的GPIO使您可以將SPI主/從,TWI主和UART等數(shù)字接口連接到設(shè)備上的31個GPIO中的任何一個。
資源:
- 16 MHz Cortex-M0
- 256/128 KB閃存,32/ 16 KB RAM
- 2.4 GHz收發(fā)器
- 2Mbps,1 Mbps,250 kbps
- 藍(lán)牙低功耗+4 dBm TX功率
- 128位AES CCM
- UART,SPI,TWI
- 10位ADC
其他資料如下:
產(chǎn)品介紹
產(chǎn)品規(guī)格書
二、Keil環(huán)境搭建
1、下載軟件包
也可以下載下面的SDK進(jìn)行軟件包的下載,但是我下載不了就用這個方法了。
nRF5 SDK
2、選擇設(shè)備
3、配置JLINK
三、nRFgo的下載
nRFgo用于設(shè)備的燒錄,當(dāng)然也可以直接用jlink把協(xié)議棧、應(yīng)用程序和bootloader燒錄進(jìn)去,但官方軟件相對來說更加專業(yè)和好用。
nRFgo Studio下載鏈接
打開的燒錄頁面如下:
燒錄的地址順序?yàn)?#xff1a;
四、藍(lán)牙協(xié)議棧
關(guān)于藍(lán)牙協(xié)議的文章
協(xié)議棧的文檔和下載地址:
藍(lán)牙外圍協(xié)議棧S110
藍(lán)牙外圍協(xié)議棧S120
藍(lán)牙外圍協(xié)議棧S130
官方SoftDevice(協(xié)議棧)說明文檔
下面說明幾個協(xié)議棧的適用方向:
(一)、S110
S110是用于nRF51系列的Bluetooth 4.1認(rèn)證的外圍協(xié)議棧。
- 作為外圍設(shè)備和廣播器的并發(fā)鏈接
- 可配置的ATT表
- 隱私協(xié)議 1.1
- 安全模式1 - 1、2和3級
- 支持自定義UUID
- 支持具有 2.4GHz專有協(xié)議的并發(fā)多重協(xié)議
簡單來說就是S110是針對外圍設(shè)備(終端節(jié)點(diǎn))的協(xié)議棧,也就是說只能廣播,不能掃描,用于從設(shè)備。
不同版本的協(xié)議棧所占用的大小不用,如當(dāng)前最新版本s110_nrf51_8.0.0為例(數(shù)據(jù)來自于官方文檔),Application項(xiàng)目部分的ROM和RAM起始地址設(shè)置如下:
具體所需內(nèi)存的值還要根據(jù)你所選擇的功能決定,上圖所示值僅僅是默認(rèn)值,下面的協(xié)議棧同理。
(二)、S120
S120是用于nRF51系列的Bluetooth 4.1認(rèn)證的中央或外圍協(xié)議棧。
- 作為充當(dāng)觀察者的中心設(shè)備或者充當(dāng)廣播員的外設(shè)的8個并發(fā)鏈接
- 可配置的ATT表
- 隱私協(xié)議1.1
- 安全模式1 - 1、2和3級
- 支持自定義UUID
- 支持具有 2.4GHz專有協(xié)議的并發(fā)多重協(xié)議
簡單來說就是S120是針對中心設(shè)備的協(xié)議棧,就是說可以掃描,用于主設(shè)備。
當(dāng)前最新版本s120_nrf51_2.1.0所占大小為:
(三)、S130
S130是nRF51系列的藍(lán)牙4.2認(rèn)證的中央和外圍協(xié)議棧
- 作為中心設(shè)備的觀察者和外圍設(shè)備的廣播員的8個并發(fā)鏈接
- 可配置的ATT表
- 隱私協(xié)議1.1
- 安全模式1 - 1、2、3和4級
- 支持自定義UUID
- 支持具有 2.4GHz的專有協(xié)議的并發(fā)多重協(xié)議
- LE安全連接
S130即可作為主設(shè)備,又可作為從設(shè)備。
當(dāng)前最新版本s130_nrf51_2.0.1所占大小為:
五、串口透傳例程
該程序可以通過下載nRF_SDK下載、nRF_SDK說明文檔來獲取,下載不了的話可以采用科學(xué)上網(wǎng)工具。
下面我大概說一下配置的地方。
(一)、應(yīng)用程序定時(shí)器(心跳)初始化
該函數(shù)原型為APP_TIMER_INIT(PRESCALER, OP_QUEUES_SIZE, SCHEDULER_FUNC) 。
它是一個宏定義,用于定義RTC1定時(shí)器的預(yù)分頻值PRESCALER、持有掛起執(zhí)行的計(jì)時(shí)器操作的隊(duì)列的大小OP_QUEUES_SIZE和是否開啟指向調(diào)度程序事件處理程序的指針。
(二)、串口初始化
該函數(shù)用于設(shè)置UART的引腳和波特率等配置,同時(shí)初始化UART_FIFO的接收和發(fā)送緩沖區(qū)大小,另外還有事件句柄的定義,其中包括以下幾種:
/**@brief Enumeration which defines events used by the UART module upon data reception or error.** @details The event type is used to indicate the type of additional information in the event* @ref app_uart_evt_t.*/ typedef enum {APP_UART_DATA_READY, /**< An event indicating that UART data has been received. The data is available in the FIFO and can be fetched using @ref app_uart_get. */APP_UART_FIFO_ERROR, /**< An error in the FIFO module used by the app_uart module has occured. The FIFO error code is stored in app_uart_evt_t.data.error_code field. */APP_UART_COMMUNICATION_ERROR, /**< An communication error has occured during reception. The error is stored in app_uart_evt_t.data.error_communication field. */APP_UART_TX_EMPTY, /**< An event indicating that UART has completed transmission of all available data in the TX FIFO. */APP_UART_DATA, /**< An event indicating that UART data has been received, and data is present in data field. This event is only used when no FIFO is configured. */ } app_uart_evt_type_t;我們主要用數(shù)據(jù)已接收事件APP_UART_DATA_READY、FIFO錯誤事件APP_UART_FIFO_ERROR和通信錯誤事件APP_UART_COMMUNICATION_ERROR。第一個事件采用app_uart_get函數(shù)來獲取FIFO中的數(shù)據(jù),其他兩個采用APP_ERROR_HANDLER函數(shù)進(jìn)行錯誤處理。
它的配置結(jié)構(gòu)體如下:
/**@brief UART communication structure holding configuration settings for the peripheral.*/ typedef struct {uint8_t rx_pin_no; /**< RX pin number. */uint8_t tx_pin_no; /**< TX pin number. */uint8_t rts_pin_no; /**< RTS pin number, only used if flow control is enabled. */uint8_t cts_pin_no; /**< CTS pin number, only used if flow control is enabled. */app_uart_flow_control_t flow_control; /**< Flow control setting, if flow control is used, the system will use low power UART mode, based on CTS signal. */bool use_parity; /**< Even parity if TRUE, no parity if FALSE. */uint32_t baud_rate; /**< Baud rate configuration. */ } app_uart_comm_params_t;另外串口數(shù)據(jù)的讀寫采用環(huán)形緩沖區(qū),不懂的小伙伴可以自行百度一下,它的結(jié)構(gòu)體定義如下,用起來還是挺方便的。^^
typedef struct {int size; /* maximum number of elements */int start; /* index of oldest element */int end; /* index at which to write new element */int count;unsigned char *elems; /* vector of elements */ } CircularBuffer;(三)、flash初始化
該函數(shù)pstorage_init主要用于存儲和讀出藍(lán)牙名字。他的初始化結(jié)構(gòu)體如下:
/**@brief Persistent storage operation completion callback function type.** @details The persistent storage operation completion callback is used by the interface to report* success or failure of a flash operation. Since data is not copied for a store operation, * a callback is an indication that the resident memory can now be reused or freed.* * @param[in] handle Identifies the module and block for the callback that is received.* @param[in] op_code Identifies the operation for the event that is notified.* @param[in] result Identifies the result of a flash access operation. NRF_SUCCESS implies * operation succeeded.** @note Unmanaged (abnormal behaviour) error codes from the SoftDevice flash * access API are forwarded as is and are expected to be handled by the * application. For details refer to the implementation file and corresponding * SoftDevice flash API documentation.* * @param[in] p_data Identifies the application data pointer. For a store operation, this points * to the resident source of application memory that the application can now * free or reuse. When there is a clear operation, this is NULL since no * application pointer is needed for this operation.* @param[in] data_len Length data the application provided for the operation. */ typedef void (*pstorage_ntf_cb_t)(pstorage_handle_t * p_handle,uint8_t op_code,uint32_t result,uint8_t * p_data,uint32_t data_len);/**@brief Struct containing module registration context. */ typedef struct {pstorage_ntf_cb_t cb; /**< Persistent storage operation completion callback function @ref pstorage_ntf_cb_t. */pstorage_size_t block_size; /**< Desired block size for persistent memory storage. For example, if a module has a table with 10 entries, and each entry is 64 bytes in size,* it can request 10 blocks with a block size of 64 bytes. The module can also request one block that is 640 bytes depending * on how it would like to access or alter the memory in persistent memory.* The first option is preferred when it is a single entry that needs to be updated often and doesn't impact the other entries.* The second option is preferred when table entries are not changed individually but have a common point of loading and storing* data. */pstorage_size_t block_count; /** Number of blocks requested by the module; minimum values is 1. */ } pstorage_module_param_t;(四)、定時(shí)器初始化
該函數(shù)time2_init主要用于藍(lán)牙透傳和AT指令的處理,它的定時(shí)觸發(fā)時(shí)間為發(fā)送20(字節(jié))*32us=640。
(五)、藍(lán)牙協(xié)議棧初始化
該函數(shù)ble_stack_init用于藍(lán)牙協(xié)議棧的初始化。如果之前藍(lán)牙協(xié)議棧地址配置錯誤的話,這里會進(jìn)入硬件中斷。
(六)、GAP參數(shù)初始化
該函數(shù)gap_params_init用于初始化GAP(Generic Access Profile),配置藍(lán)牙名字和連接參數(shù)的配置。它的結(jié)構(gòu)體如下:
/**@brief GAP connection parameters.** @note When ble_conn_params_t is received in an event, both min_conn_interval and* max_conn_interval will be equal to the connection interval set by the central.** @note If both conn_sup_timeout and max_conn_interval are specified, then the following constraint applies:* conn_sup_timeout * 4 > (1 + slave_latency) * max_conn_interval* that corresponds to the following Bluetooth Spec requirement:* The Supervision_Timeout in milliseconds shall be larger than* (1 + Conn_Latency) * Conn_Interval_Max * 2, where Conn_Interval_Max is given in milliseconds.*/ typedef struct {uint16_t min_conn_interval; /**< Minimum Connection Interval in 1.25 ms units, see @ref BLE_GAP_CP_LIMITS.*/uint16_t max_conn_interval; /**< Maximum Connection Interval in 1.25 ms units, see @ref BLE_GAP_CP_LIMITS.*/uint16_t slave_latency; /**< Slave Latency in number of connection events, see @ref BLE_GAP_CP_LIMITS.*/uint16_t conn_sup_timeout; /**< Connection Supervision Timeout in 10 ms units, see @ref BLE_GAP_CP_LIMITS.*/ } ble_gap_conn_params_t; /**@brief GAP connection security modes.** Security Mode 0 Level 0: No access permissions at all (this level is not defined by the Bluetooth Core specification).\n* Security Mode 1 Level 1: No security is needed (aka open link).\n* Security Mode 1 Level 2: Encrypted link required, MITM protection not necessary.\n* Security Mode 1 Level 3: MITM protected encrypted link required.\n* Security Mode 2 Level 1: Signing or encryption required, MITM protection not necessary.\n* Security Mode 2 Level 2: MITM protected signing required, unless link is MITM protected encrypted.\n*/ typedef struct {uint8_t sm : 4; /**< Security Mode (1 or 2), 0 for no permissions at all. */uint8_t lv : 4; /**< Level (1, 2 or 3), 0 for no permissions at all. */} ble_gap_conn_sec_mode_t;(七)、服務(wù)初始化
該函數(shù)services_init調(diào)用函數(shù)ble_nus_init,主要用于配置服務(wù)的UUID及其類型、配置各種特性等等。它的結(jié)構(gòu)體如下:
/**@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);/**@brief Nordic UART Service initialization structure.** @details This structure contains the initialization information for the service. The application* must fill this structure and pass it to the service using the @ref ble_nus_init* function.*/ typedef struct {ble_nus_data_handler_t data_handler; /**< Event handler to be called for handling received data. */ } ble_nus_init_t;(八)、GAP廣播初始化
該函數(shù)advertising_init用于配置廣播中UART服務(wù)的UUID和可接收的最大長度等信息。它的結(jié)構(gòu)體如下:
/**@brief Advertising data structure. This structure contains all options and data needed for encoding and* setting the advertising data. */ typedef struct {ble_advdata_name_type_t name_type; /**< Type of device name. */uint8_t short_name_len; /**< Length of short device name (if short type is specified). */bool include_appearance; /**< Determines if Appearance shall be included. */uint8_t flags; /**< Advertising data Flags field. */int8_t * p_tx_power_level; /**< TX Power Level field. */ble_advdata_uuid_list_t uuids_more_available; /**< List of UUIDs in the 'More Available' list. */ble_advdata_uuid_list_t uuids_complete; /**< List of UUIDs in the 'Complete' list. */ble_advdata_uuid_list_t uuids_solicited; /**< List of solicited UUIDs. */ble_advdata_conn_int_t * p_slave_conn_int; /**< Slave Connection Interval Range. */ble_advdata_manuf_data_t * p_manuf_specific_data; /**< Manufacturer specific data. */ble_advdata_service_data_t * p_service_data_array; /**< Array of Service data structures. */uint8_t service_data_count; /**< Number of Service data structures. */bool include_ble_device_addr; /**< Determines if LE Bluetooth Device Address shall be included. */ble_advdata_le_role_t le_role; /**< LE Role field. Included when different from @ref BLE_ADVDATA_ROLE_NOT_PRESENT.*/ble_advdata_tk_value_t * p_tk_value; /**< Security Manager TK value field. Included when different from NULL.*/uint8_t * p_sec_mgr_oob_flags; /**< Security Manager Out Of Band Flags field. Included when different from NULL.*/ } ble_advdata_t;(九)、藍(lán)牙連接參數(shù)初始化
該函數(shù)conn_params_init主要用于配置藍(lán)牙連接的連接更新延時(shí)、事件/錯誤句柄等連接相關(guān)的參數(shù)。它的結(jié)構(gòu)體如下:
/**@brief Connection Parameters Module init structure. This contains all options and data needed for* initialization of the connection parameters negotiation module. */ typedef struct {ble_gap_conn_params_t * p_conn_params; /**< Pointer to the connection parameters desired by the application. When calling ble_conn_params_init, if this parameter is set to NULL, the connection parameters will be fetched from host. */uint32_t first_conn_params_update_delay; /**< Time from initiating event (connect or start of notification) to first time sd_ble_gap_conn_param_update is called (in number of timer ticks). */uint32_t next_conn_params_update_delay; /**< Time between each call to sd_ble_gap_conn_param_update after the first (in number of timer ticks). Recommended value 30 seconds as per BLUETOOTH SPECIFICATION Version 4.0. */uint8_t max_conn_params_update_count; /**< Number of attempts before giving up the negotiation. */uint16_t start_on_notify_cccd_handle; /**< If procedure is to be started when notification is started, set this to the handle of the corresponding CCCD. Set to BLE_GATT_HANDLE_INVALID if procedure is to be started on connect event. */bool disconnect_on_fail; /**< Set to TRUE if a failed connection parameters update shall cause an automatic disconnection, set to FALSE otherwise. */ble_conn_params_evt_handler_t evt_handler; /**< Event handler to be called for handling events in the Connection Parameters. */ble_srv_error_handler_t error_handler; /**< Function to be called in case of an error. */ } ble_conn_params_init_t; /**@brief GAP connection parameters.** @note When ble_conn_params_t is received in an event, both min_conn_interval and* max_conn_interval will be equal to the connection interval set by the central.** @note If both conn_sup_timeout and max_conn_interval are specified, then the following constraint applies:* conn_sup_timeout * 4 > (1 + slave_latency) * max_conn_interval* that corresponds to the following Bluetooth Spec requirement:* The Supervision_Timeout in milliseconds shall be larger than* (1 + Conn_Latency) * Conn_Interval_Max * 2, where Conn_Interval_Max is given in milliseconds.*/ typedef struct {uint16_t min_conn_interval; /**< Minimum Connection Interval in 1.25 ms units, see @ref BLE_GAP_CP_LIMITS.*/uint16_t max_conn_interval; /**< Maximum Connection Interval in 1.25 ms units, see @ref BLE_GAP_CP_LIMITS.*/uint16_t slave_latency; /**< Slave Latency in number of connection events, see @ref BLE_GAP_CP_LIMITS.*/uint16_t conn_sup_timeout; /**< Connection Supervision Timeout in 10 ms units, see @ref BLE_GAP_CP_LIMITS.*/ } ble_gap_conn_params_t;(十)、開啟廣播
該函數(shù)原型為uint32_t ble_advertising_start(ble_adv_mode_t advertising_mode),傳入?yún)?shù)為廣播模式,枚舉如下:
/**@brief Advertising modes. */ typedef enum {BLE_ADV_MODE_IDLE, /**< Idle; no connectable advertising is ongoing.*/BLE_ADV_MODE_DIRECTED, /**< Directed advertising attempts to connect to the most recently disconnected peer. */BLE_ADV_MODE_DIRECTED_SLOW, /**< Directed advertising (low duty cycle) attempts to connect to the most recently disconnected peer. */BLE_ADV_MODE_FAST, /**< Fast advertising will connect to any peer device, or filter with a whitelist if one exists. */BLE_ADV_MODE_SLOW, /**< Slow advertising is similar to fast advertising. By default, it uses a longer advertising interval and time-out than fast advertising. However, these options are defined by the user. */ } ble_adv_mode_t;(十一)、低功耗模式
最后的這個函數(shù)power_manage用于使藍(lán)牙進(jìn)入低功耗模式等待事件到來,調(diào)用的函數(shù)為sd_app_evt_wait。
總結(jié)
以上是生活随笔為你收集整理的nRF51822蓝牙开发的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 身份证转生日 15位 ,18位
- 下一篇: 机器人书籍大全