日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

nRF51822蓝牙开发

發布時間:2024/3/24 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 nRF51822蓝牙开发 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

    • 一、nRF51822芯片介紹
    • 二、Keil環境搭建
        • 1、下載軟件包
        • 2、選擇設備
        • 3、配置JLINK
    • 三、nRFgo的下載
    • 四、藍牙協議棧
      • (一)、S110
      • (二)、S120
      • (三)、S130
    • 五、串口透傳例程
      • (一)、應用程序定時器(心跳)初始化
      • (二)、串口初始化
      • (三)、flash初始化
      • (四)、定時器初始化
      • (五)、藍牙協議棧初始化
      • (六)、GAP參數初始化
      • (七)、服務初始化
      • (八)、GAP廣播初始化
      • (九)、藍牙連接參數初始化
      • (十)、開啟廣播
      • (十一)、低功耗模式

一、nRF51822芯片介紹

低功耗藍牙和2.4 GHz SoC。nRF51822是一款通用的超低功耗SoC,非常適合Bluetooth?低功耗和2.4 GHz專有無線應用。它基于具有256/128KB閃存和32/16KB RAM32位ARM?Cortex?-M0CPU構建。靈活的2.4 GHz無線電支持藍牙低功耗和2.4 GHz專有協議,例如Gazell。它集成了豐富的模擬和數字外設,可以通過可編程外設互連(PPI)系統直接進行交互,而無需CPU干預。靈活的GPIO使您可以將SPI主/從,TWI主和UART等數字接口連接到設備上的31個GPIO中的任何一個

資源:

  • 16 MHz Cortex-M0
  • 256/128 KB閃存,32/ 16 KB RAM
  • 2.4 GHz收發器
  • 2Mbps,1 Mbps,250 kbps
  • 藍牙低功耗+4 dBm TX功率
  • 128位AES CCM
  • UART,SPI,TWI
  • 10位ADC

其他資料如下:

產品介紹

產品規格書

二、Keil環境搭建

1、下載軟件包

也可以下載下面的SDK進行軟件包的下載,但是我下載不了就用這個方法了。

nRF5 SDK

2、選擇設備

3、配置JLINK


三、nRFgo的下載

nRFgo用于設備的燒錄,當然也可以直接用jlink把協議棧、應用程序和bootloader燒錄進去,但官方軟件相對來說更加專業和好用。

nRFgo Studio下載鏈接

打開的燒錄頁面如下:


燒錄的地址順序為:

四、藍牙協議棧

關于藍牙協議的文章

協議棧的文檔和下載地址:
藍牙外圍協議棧S110
藍牙外圍協議棧S120
藍牙外圍協議棧S130

官方SoftDevice(協議棧)說明文檔

下面說明幾個協議棧的適用方向:

(一)、S110

S110是用于nRF51系列的Bluetooth 4.1認證的外圍協議棧。

  • 作為外圍設備和廣播器的并發鏈接
  • 可配置的ATT表
  • 隱私協議 1.1
  • 安全模式1 - 1、2和3級
  • 支持自定義UUID
  • 支持具有 2.4GHz專有協議的并發多重協議

簡單來說就是S110是針對外圍設備(終端節點)的協議棧,也就是說只能廣播,不能掃描,用于從設備。

不同版本的協議棧所占用的大小不用,如當前最新版本s110_nrf51_8.0.0為例(數據來自于官方文檔),Application項目部分ROM和RAM起始地址設置如下:

具體所需內存的值還要根據你所選擇的功能決定,上圖所示值僅僅是默認值,下面的協議棧同理。

(二)、S120


S120是用于nRF51系列的Bluetooth 4.1認證的中央或外圍協議棧。

  • 作為充當觀察者的中心設備或者充當廣播員的外設的8個并發鏈接
  • 可配置的ATT表
  • 隱私協議1.1
  • 安全模式1 - 1、2和3級
  • 支持自定義UUID
  • 支持具有 2.4GHz專有協議的并發多重協議

簡單來說就是S120是針對中心設備的協議棧,就是說可以掃描,用于主設備。

當前最新版本s120_nrf51_2.1.0所占大小為:

(三)、S130

S130是nRF51系列的藍牙4.2認證的中央和外圍協議棧

  • 作為中心設備的觀察者和外圍設備的廣播員的8個并發鏈接
  • 可配置的ATT表
  • 隱私協議1.1
  • 安全模式1 - 1、2、3和4級
  • 支持自定義UUID
  • 支持具有 2.4GHz的專有協議的并發多重協議
  • LE安全連接

S130即可作為主設備,又可作為從設備。

當前最新版本s130_nrf51_2.0.1所占大小為:

五、串口透傳例程

該程序可以通過下載nRF_SDK下載、nRF_SDK說明文檔來獲取,下載不了的話可以采用科學上網工具。

下面我大概說一下配置的地方。

(一)、應用程序定時器(心跳)初始化

該函數原型為APP_TIMER_INIT(PRESCALER, OP_QUEUES_SIZE, SCHEDULER_FUNC) 。
它是一個宏定義,用于定義RTC1定時器的預分頻值PRESCALER、持有掛起執行的計時器操作的隊列的大小OP_QUEUES_SIZE和是否開啟指向調度程序事件處理程序的指針。

#define APP_TIMER_INIT(PRESCALER, OP_QUEUES_SIZE, SCHEDULER_FUNC) \do \{ \static uint32_t APP_TIMER_BUF[CEIL_DIV(APP_TIMER_BUF_SIZE((OP_QUEUES_SIZE) + 1), \sizeof(uint32_t))]; \uint32_t ERR_CODE = app_timer_init((PRESCALER), \(OP_QUEUES_SIZE) + 1, \APP_TIMER_BUF, \SCHEDULER_FUNC); \APP_ERROR_CHECK(ERR_CODE); \} while (0)

(二)、串口初始化

該函數用于設置UART的引腳和波特率等配置,同時初始化UART_FIFO的接收和發送緩沖區大小,另外還有事件句柄的定義,其中包括以下幾種:

/**@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;

我們主要用數據已接收事件APP_UART_DATA_READY、FIFO錯誤事件APP_UART_FIFO_ERROR和通信錯誤事件APP_UART_COMMUNICATION_ERROR。第一個事件采用app_uart_get函數來獲取FIFO中的數據,其他兩個采用APP_ERROR_HANDLER函數進行錯誤處理。

它的配置結構體如下:

/**@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;

另外串口數據的讀寫采用環形緩沖區,不懂的小伙伴可以自行百度一下,它的結構體定義如下,用起來還是挺方便的。^^

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初始化

該函數pstorage_init主要用于存儲和讀出藍牙名字。他的初始化結構體如下:

/**@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;

(四)、定時器初始化

該函數time2_init主要用于藍牙透傳和AT指令的處理,它的定時觸發時間為發送20(字節)*32us=640。

(五)、藍牙協議棧初始化

該函數ble_stack_init用于藍牙協議棧的初始化。如果之前藍牙協議棧地址配置錯誤的話,這里會進入硬件中斷。

(六)、GAP參數初始化

該函數gap_params_init用于初始化GAP(Generic Access Profile),配置藍牙名字和連接參數的配置。它的結構體如下:

/**@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;

(七)、服務初始化

該函數services_init調用函數ble_nus_init,主要用于配置服務的UUID及其類型、配置各種特性等等。它的結構體如下:

/**@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廣播初始化

該函數advertising_init用于配置廣播中UART服務的UUID和可接收的最大長度等信息。它的結構體如下:

/**@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;

(九)、藍牙連接參數初始化

該函數conn_params_init主要用于配置藍牙連接的連接更新延時、事件/錯誤句柄等連接相關的參數。它的結構體如下:

/**@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;

(十)、開啟廣播

該函數原型為uint32_t ble_advertising_start(ble_adv_mode_t advertising_mode),傳入參數為廣播模式,枚舉如下:

/**@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;

(十一)、低功耗模式

最后的這個函數power_manage用于使藍牙進入低功耗模式等待事件到來,調用的函數為sd_app_evt_wait。

總結

以上是生活随笔為你收集整理的nRF51822蓝牙开发的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。