单导联心电记录仪
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

729 lines
24 KiB

  1. /**
  2. * Copyright (c) 2014 - 2021, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. /** @file
  41. *
  42. * @defgroup ble_sdk_uart_over_ble_main main.c
  43. * @{
  44. * @ingroup ble_sdk_app_nus_eval
  45. * @brief UART over BLE application main file.
  46. *
  47. * This file contains the source code for a sample application that uses the Nordic UART service.
  48. * This application uses the @ref srvlib_conn_params module.
  49. */
  50. #include <stdint.h>
  51. #include <string.h>
  52. #include "nordic_common.h"
  53. #include "nrf.h"
  54. #include "ble_hci.h"
  55. #include "ble_advdata.h"
  56. #include "ble_advertising.h"
  57. #include "ble_conn_params.h"
  58. #include "nrf_sdh.h"
  59. #include "nrf_sdh_soc.h"
  60. #include "nrf_sdh_ble.h"
  61. #include "nrf_ble_gatt.h"
  62. #include "nrf_ble_qwr.h"
  63. #include "app_timer.h"
  64. #include "ble_nus.h"
  65. #include "app_uart.h"
  66. #include "app_util_platform.h"
  67. #include "bsp_btn_ble.h"
  68. #include "nrf_pwr_mgmt.h"
  69. #if defined (UART_PRESENT)
  70. #include "nrf_uart.h"
  71. #endif
  72. #if defined (UARTE_PRESENT)
  73. #include "nrf_uarte.h"
  74. #endif
  75. #include "nrf_log.h"
  76. #include "nrf_log_ctrl.h"
  77. #include "nrf_log_default_backends.h"
  78. #define APP_BLE_CONN_CFG_TAG 1 /**< A tag identifying the SoftDevice BLE configuration. */
  79. #define DEVICE_NAME "Nordic_UART" /**< Name of device. Will be included in the advertising data. */
  80. #define NUS_SERVICE_UUID_TYPE BLE_UUID_TYPE_VENDOR_BEGIN /**< UUID type for the Nordic UART Service (vendor specific). */
  81. #define APP_BLE_OBSERVER_PRIO 3 /**< Application's BLE observer priority. You shouldn't need to modify this value. */
  82. #define APP_ADV_INTERVAL 64 /**< The advertising interval (in units of 0.625 ms. This value corresponds to 40 ms). */
  83. #define APP_ADV_DURATION 18000 /**< The advertising duration (180 seconds) in units of 10 milliseconds. */
  84. #define MIN_CONN_INTERVAL MSEC_TO_UNITS(20, UNIT_1_25_MS) /**< Minimum acceptable connection interval (20 ms), Connection interval uses 1.25 ms units. */
  85. #define MAX_CONN_INTERVAL MSEC_TO_UNITS(75, UNIT_1_25_MS) /**< Maximum acceptable connection interval (75 ms), Connection interval uses 1.25 ms units. */
  86. #define SLAVE_LATENCY 0 /**< Slave latency. */
  87. #define CONN_SUP_TIMEOUT MSEC_TO_UNITS(4000, UNIT_10_MS) /**< Connection supervisory timeout (4 seconds), Supervision Timeout uses 10 ms units. */
  88. #define FIRST_CONN_PARAMS_UPDATE_DELAY APP_TIMER_TICKS(5000) /**< Time from initiating event (connect or start of notification) to first time sd_ble_gap_conn_param_update is called (5 seconds). */
  89. #define NEXT_CONN_PARAMS_UPDATE_DELAY APP_TIMER_TICKS(30000) /**< Time between each call to sd_ble_gap_conn_param_update after the first call (30 seconds). */
  90. #define MAX_CONN_PARAMS_UPDATE_COUNT 3 /**< Number of attempts before giving up the connection parameter negotiation. */
  91. #define DEAD_BEEF 0xDEADBEEF /**< Value used as error code on stack dump, can be used to identify stack location on stack unwind. */
  92. #define UART_TX_BUF_SIZE 256 /**< UART TX buffer size. */
  93. #define UART_RX_BUF_SIZE 256 /**< UART RX buffer size. */
  94. BLE_NUS_DEF(m_nus, NRF_SDH_BLE_TOTAL_LINK_COUNT); /**< BLE NUS service instance. */
  95. NRF_BLE_GATT_DEF(m_gatt); /**< GATT module instance. */
  96. NRF_BLE_QWR_DEF(m_qwr); /**< Context for the Queued Write module.*/
  97. BLE_ADVERTISING_DEF(m_advertising); /**< Advertising module instance. */
  98. static uint16_t m_conn_handle = BLE_CONN_HANDLE_INVALID; /**< Handle of the current connection. */
  99. static uint16_t m_ble_nus_max_data_len = BLE_GATT_ATT_MTU_DEFAULT - 3; /**< Maximum length of data (in bytes) that can be transmitted to the peer by the Nordic UART service module. */
  100. static ble_uuid_t m_adv_uuids[] = /**< Universally unique service identifier. */
  101. {
  102. {BLE_UUID_NUS_SERVICE, NUS_SERVICE_UUID_TYPE}
  103. };
  104. /**@brief Function for assert macro callback.
  105. *
  106. * @details This function will be called in case of an assert in the SoftDevice.
  107. *
  108. * @warning This handler is an example only and does not fit a final product. You need to analyse
  109. * how your product is supposed to react in case of Assert.
  110. * @warning On assert from the SoftDevice, the system can only recover on reset.
  111. *
  112. * @param[in] line_num Line number of the failing ASSERT call.
  113. * @param[in] p_file_name File name of the failing ASSERT call.
  114. */
  115. void assert_nrf_callback(uint16_t line_num, const uint8_t * p_file_name)
  116. {
  117. app_error_handler(DEAD_BEEF, line_num, p_file_name);
  118. }
  119. /**@brief Function for initializing the timer module.
  120. */
  121. static void timers_init(void)
  122. {
  123. ret_code_t err_code = app_timer_init();
  124. APP_ERROR_CHECK(err_code);
  125. }
  126. /**@brief Function for the GAP initialization.
  127. *
  128. * @details This function will set up all the necessary GAP (Generic Access Profile) parameters of
  129. * the device. It also sets the permissions and appearance.
  130. */
  131. static void gap_params_init(void)
  132. {
  133. uint32_t err_code;
  134. ble_gap_conn_params_t gap_conn_params;
  135. ble_gap_conn_sec_mode_t sec_mode;
  136. BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);
  137. err_code = sd_ble_gap_device_name_set(&sec_mode,
  138. (const uint8_t *) DEVICE_NAME,
  139. strlen(DEVICE_NAME));
  140. APP_ERROR_CHECK(err_code);
  141. memset(&gap_conn_params, 0, sizeof(gap_conn_params));
  142. gap_conn_params.min_conn_interval = MIN_CONN_INTERVAL;
  143. gap_conn_params.max_conn_interval = MAX_CONN_INTERVAL;
  144. gap_conn_params.slave_latency = SLAVE_LATENCY;
  145. gap_conn_params.conn_sup_timeout = CONN_SUP_TIMEOUT;
  146. err_code = sd_ble_gap_ppcp_set(&gap_conn_params);
  147. APP_ERROR_CHECK(err_code);
  148. }
  149. /**@brief Function for handling Queued Write Module errors.
  150. *
  151. * @details A pointer to this function will be passed to each service which may need to inform the
  152. * application about an error.
  153. *
  154. * @param[in] nrf_error Error code containing information about what went wrong.
  155. */
  156. static void nrf_qwr_error_handler(uint32_t nrf_error)
  157. {
  158. APP_ERROR_HANDLER(nrf_error);
  159. }
  160. /**@brief Function for handling the data from the Nordic UART Service.
  161. *
  162. * @details This function will process the data received from the Nordic UART BLE Service and send
  163. * it to the UART module.
  164. *
  165. * @param[in] p_evt Nordic UART Service event.
  166. */
  167. /**@snippet [Handling the data received over BLE] */
  168. static void nus_data_handler(ble_nus_evt_t * p_evt)
  169. {
  170. if (p_evt->type == BLE_NUS_EVT_RX_DATA)
  171. {
  172. uint32_t err_code;
  173. NRF_LOG_DEBUG("Received data from BLE NUS. Writing data on UART.");
  174. NRF_LOG_HEXDUMP_DEBUG(p_evt->params.rx_data.p_data, p_evt->params.rx_data.length);
  175. for (uint32_t i = 0; i < p_evt->params.rx_data.length; i++)
  176. {
  177. do
  178. {
  179. err_code = app_uart_put(p_evt->params.rx_data.p_data[i]);
  180. if ((err_code != NRF_SUCCESS) && (err_code != NRF_ERROR_BUSY))
  181. {
  182. NRF_LOG_ERROR("Failed receiving NUS message. Error 0x%x. ", err_code);
  183. APP_ERROR_CHECK(err_code);
  184. }
  185. } while (err_code == NRF_ERROR_BUSY);
  186. }
  187. if (p_evt->params.rx_data.p_data[p_evt->params.rx_data.length - 1] == '\r')
  188. {
  189. while (app_uart_put('\n') == NRF_ERROR_BUSY);
  190. }
  191. }
  192. }
  193. /**@snippet [Handling the data received over BLE] */
  194. /**@brief Function for initializing services that will be used by the application.
  195. */
  196. static void services_init(void)
  197. {
  198. uint32_t err_code;
  199. ble_nus_init_t nus_init;
  200. nrf_ble_qwr_init_t qwr_init = {0};
  201. // Initialize Queued Write Module.
  202. qwr_init.error_handler = nrf_qwr_error_handler;
  203. err_code = nrf_ble_qwr_init(&m_qwr, &qwr_init);
  204. APP_ERROR_CHECK(err_code);
  205. // Initialize NUS.
  206. memset(&nus_init, 0, sizeof(nus_init));
  207. nus_init.data_handler = nus_data_handler;
  208. err_code = ble_nus_init(&m_nus, &nus_init);
  209. APP_ERROR_CHECK(err_code);
  210. }
  211. /**@brief Function for handling an event from the Connection Parameters Module.
  212. *
  213. * @details This function will be called for all events in the Connection Parameters Module
  214. * which are passed to the application.
  215. *
  216. * @note All this function does is to disconnect. This could have been done by simply setting
  217. * the disconnect_on_fail config parameter, but instead we use the event handler
  218. * mechanism to demonstrate its use.
  219. *
  220. * @param[in] p_evt Event received from the Connection Parameters Module.
  221. */
  222. static void on_conn_params_evt(ble_conn_params_evt_t * p_evt)
  223. {
  224. uint32_t err_code;
  225. if (p_evt->evt_type == BLE_CONN_PARAMS_EVT_FAILED)
  226. {
  227. err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_CONN_INTERVAL_UNACCEPTABLE);
  228. APP_ERROR_CHECK(err_code);
  229. }
  230. }
  231. /**@brief Function for handling errors from the Connection Parameters module.
  232. *
  233. * @param[in] nrf_error Error code containing information about what went wrong.
  234. */
  235. static void conn_params_error_handler(uint32_t nrf_error)
  236. {
  237. APP_ERROR_HANDLER(nrf_error);
  238. }
  239. /**@brief Function for initializing the Connection Parameters module.
  240. */
  241. static void conn_params_init(void)
  242. {
  243. uint32_t err_code;
  244. ble_conn_params_init_t cp_init;
  245. memset(&cp_init, 0, sizeof(cp_init));
  246. cp_init.p_conn_params = NULL;
  247. cp_init.first_conn_params_update_delay = FIRST_CONN_PARAMS_UPDATE_DELAY;
  248. cp_init.next_conn_params_update_delay = NEXT_CONN_PARAMS_UPDATE_DELAY;
  249. cp_init.max_conn_params_update_count = MAX_CONN_PARAMS_UPDATE_COUNT;
  250. cp_init.start_on_notify_cccd_handle = BLE_GATT_HANDLE_INVALID;
  251. cp_init.disconnect_on_fail = false;
  252. cp_init.evt_handler = on_conn_params_evt;
  253. cp_init.error_handler = conn_params_error_handler;
  254. err_code = ble_conn_params_init(&cp_init);
  255. APP_ERROR_CHECK(err_code);
  256. }
  257. /**@brief Function for putting the chip into sleep mode.
  258. *
  259. * @note This function will not return.
  260. */
  261. static void sleep_mode_enter(void)
  262. {
  263. uint32_t err_code = bsp_indication_set(BSP_INDICATE_IDLE);
  264. APP_ERROR_CHECK(err_code);
  265. // Prepare wakeup buttons.
  266. err_code = bsp_btn_ble_sleep_mode_prepare();
  267. APP_ERROR_CHECK(err_code);
  268. // Go to system-off mode (this function will not return; wakeup will cause a reset).
  269. err_code = sd_power_system_off();
  270. APP_ERROR_CHECK(err_code);
  271. }
  272. /**@brief Function for handling advertising events.
  273. *
  274. * @details This function will be called for advertising events which are passed to the application.
  275. *
  276. * @param[in] ble_adv_evt Advertising event.
  277. */
  278. static void on_adv_evt(ble_adv_evt_t ble_adv_evt)
  279. {
  280. uint32_t err_code;
  281. switch (ble_adv_evt)
  282. {
  283. case BLE_ADV_EVT_FAST:
  284. err_code = bsp_indication_set(BSP_INDICATE_ADVERTISING);
  285. APP_ERROR_CHECK(err_code);
  286. break;
  287. case BLE_ADV_EVT_IDLE:
  288. sleep_mode_enter();
  289. break;
  290. default:
  291. break;
  292. }
  293. }
  294. /**@brief Function for handling BLE events.
  295. *
  296. * @param[in] p_ble_evt Bluetooth stack event.
  297. * @param[in] p_context Unused.
  298. */
  299. static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
  300. {
  301. uint32_t err_code;
  302. switch (p_ble_evt->header.evt_id)
  303. {
  304. case BLE_GAP_EVT_CONNECTED:
  305. NRF_LOG_INFO("Connected");
  306. err_code = bsp_indication_set(BSP_INDICATE_CONNECTED);
  307. APP_ERROR_CHECK(err_code);
  308. m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
  309. err_code = nrf_ble_qwr_conn_handle_assign(&m_qwr, m_conn_handle);
  310. APP_ERROR_CHECK(err_code);
  311. break;
  312. case BLE_GAP_EVT_DISCONNECTED:
  313. NRF_LOG_INFO("Disconnected");
  314. // LED indication will be changed when advertising starts.
  315. m_conn_handle = BLE_CONN_HANDLE_INVALID;
  316. break;
  317. case BLE_GAP_EVT_PHY_UPDATE_REQUEST:
  318. {
  319. NRF_LOG_DEBUG("PHY update request.");
  320. ble_gap_phys_t const phys =
  321. {
  322. .rx_phys = BLE_GAP_PHY_AUTO,
  323. .tx_phys = BLE_GAP_PHY_AUTO,
  324. };
  325. err_code = sd_ble_gap_phy_update(p_ble_evt->evt.gap_evt.conn_handle, &phys);
  326. APP_ERROR_CHECK(err_code);
  327. } break;
  328. case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
  329. // Pairing not supported
  330. err_code = sd_ble_gap_sec_params_reply(m_conn_handle, BLE_GAP_SEC_STATUS_PAIRING_NOT_SUPP, NULL, NULL);
  331. APP_ERROR_CHECK(err_code);
  332. break;
  333. case BLE_GATTS_EVT_SYS_ATTR_MISSING:
  334. // No system attributes have been stored.
  335. err_code = sd_ble_gatts_sys_attr_set(m_conn_handle, NULL, 0, 0);
  336. APP_ERROR_CHECK(err_code);
  337. break;
  338. case BLE_GATTC_EVT_TIMEOUT:
  339. // Disconnect on GATT Client timeout event.
  340. err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gattc_evt.conn_handle,
  341. BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
  342. APP_ERROR_CHECK(err_code);
  343. break;
  344. case BLE_GATTS_EVT_TIMEOUT:
  345. // Disconnect on GATT Server timeout event.
  346. err_code = sd_ble_gap_disconnect(p_ble_evt->evt.gatts_evt.conn_handle,
  347. BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
  348. APP_ERROR_CHECK(err_code);
  349. break;
  350. default:
  351. // No implementation needed.
  352. break;
  353. }
  354. }
  355. /**@brief Function for the SoftDevice initialization.
  356. *
  357. * @details This function initializes the SoftDevice and the BLE event interrupt.
  358. */
  359. static void ble_stack_init(void)
  360. {
  361. ret_code_t err_code;
  362. err_code = nrf_sdh_enable_request();
  363. APP_ERROR_CHECK(err_code);
  364. // Configure the BLE stack using the default settings.
  365. // Fetch the start address of the application RAM.
  366. uint32_t ram_start = 0;
  367. err_code = nrf_sdh_ble_default_cfg_set(APP_BLE_CONN_CFG_TAG, &ram_start);
  368. APP_ERROR_CHECK(err_code);
  369. // Enable BLE stack.
  370. err_code = nrf_sdh_ble_enable(&ram_start);
  371. APP_ERROR_CHECK(err_code);
  372. // Register a handler for BLE events.
  373. NRF_SDH_BLE_OBSERVER(m_ble_observer, APP_BLE_OBSERVER_PRIO, ble_evt_handler, NULL);
  374. }
  375. /**@brief Function for handling events from the GATT library. */
  376. void gatt_evt_handler(nrf_ble_gatt_t * p_gatt, nrf_ble_gatt_evt_t const * p_evt)
  377. {
  378. if ((m_conn_handle == p_evt->conn_handle) && (p_evt->evt_id == NRF_BLE_GATT_EVT_ATT_MTU_UPDATED))
  379. {
  380. m_ble_nus_max_data_len = p_evt->params.att_mtu_effective - OPCODE_LENGTH - HANDLE_LENGTH;
  381. NRF_LOG_INFO("Data len is set to 0x%X(%d)", m_ble_nus_max_data_len, m_ble_nus_max_data_len);
  382. }
  383. NRF_LOG_DEBUG("ATT MTU exchange completed. central 0x%x peripheral 0x%x",
  384. p_gatt->att_mtu_desired_central,
  385. p_gatt->att_mtu_desired_periph);
  386. }
  387. /**@brief Function for initializing the GATT library. */
  388. void gatt_init(void)
  389. {
  390. ret_code_t err_code;
  391. err_code = nrf_ble_gatt_init(&m_gatt, gatt_evt_handler);
  392. APP_ERROR_CHECK(err_code);
  393. err_code = nrf_ble_gatt_att_mtu_periph_set(&m_gatt, NRF_SDH_BLE_GATT_MAX_MTU_SIZE);
  394. APP_ERROR_CHECK(err_code);
  395. }
  396. /**@brief Function for handling events from the BSP module.
  397. *
  398. * @param[in] event Event generated by button press.
  399. */
  400. void bsp_event_handler(bsp_event_t event)
  401. {
  402. uint32_t err_code;
  403. switch (event)
  404. {
  405. case BSP_EVENT_SLEEP:
  406. sleep_mode_enter();
  407. break;
  408. case BSP_EVENT_DISCONNECT:
  409. err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
  410. if (err_code != NRF_ERROR_INVALID_STATE)
  411. {
  412. APP_ERROR_CHECK(err_code);
  413. }
  414. break;
  415. case BSP_EVENT_WHITELIST_OFF:
  416. if (m_conn_handle == BLE_CONN_HANDLE_INVALID)
  417. {
  418. err_code = ble_advertising_restart_without_whitelist(&m_advertising);
  419. if (err_code != NRF_ERROR_INVALID_STATE)
  420. {
  421. APP_ERROR_CHECK(err_code);
  422. }
  423. }
  424. break;
  425. default:
  426. break;
  427. }
  428. }
  429. /**@brief Function for handling app_uart events.
  430. *
  431. * @details This function will receive a single character from the app_uart module and append it to
  432. * a string. The string will be be sent over BLE when the last character received was a
  433. * 'new line' '\n' (hex 0x0A) or if the string has reached the maximum data length.
  434. */
  435. /**@snippet [Handling the data received over UART] */
  436. void uart_event_handle(app_uart_evt_t * p_event)
  437. {
  438. static uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
  439. static uint8_t index = 0;
  440. uint32_t err_code;
  441. switch (p_event->evt_type)
  442. {
  443. case APP_UART_DATA_READY:
  444. UNUSED_VARIABLE(app_uart_get(&data_array[index]));
  445. index++;
  446. if ((data_array[index - 1] == '\n') ||
  447. (data_array[index - 1] == '\r') ||
  448. (index >= m_ble_nus_max_data_len))
  449. {
  450. if (index > 1)
  451. {
  452. NRF_LOG_DEBUG("Ready to send data over BLE NUS");
  453. NRF_LOG_HEXDUMP_DEBUG(data_array, index);
  454. do
  455. {
  456. uint16_t length = (uint16_t)index;
  457. err_code = ble_nus_data_send(&m_nus, data_array, &length, m_conn_handle);
  458. if ((err_code != NRF_ERROR_INVALID_STATE) &&
  459. (err_code != NRF_ERROR_RESOURCES) &&
  460. (err_code != NRF_ERROR_NOT_FOUND))
  461. {
  462. APP_ERROR_CHECK(err_code);
  463. }
  464. } while (err_code == NRF_ERROR_RESOURCES);
  465. }
  466. index = 0;
  467. }
  468. break;
  469. case APP_UART_COMMUNICATION_ERROR:
  470. APP_ERROR_HANDLER(p_event->data.error_communication);
  471. break;
  472. case APP_UART_FIFO_ERROR:
  473. APP_ERROR_HANDLER(p_event->data.error_code);
  474. break;
  475. default:
  476. break;
  477. }
  478. }
  479. /**@snippet [Handling the data received over UART] */
  480. /**@brief Function for initializing the UART module.
  481. */
  482. /**@snippet [UART Initialization] */
  483. static void uart_init(void)
  484. {
  485. uint32_t err_code;
  486. app_uart_comm_params_t const comm_params =
  487. {
  488. .rx_pin_no = RX_PIN_NUMBER,
  489. .tx_pin_no = TX_PIN_NUMBER,
  490. .rts_pin_no = RTS_PIN_NUMBER,
  491. .cts_pin_no = CTS_PIN_NUMBER,
  492. .flow_control = APP_UART_FLOW_CONTROL_DISABLED,
  493. .use_parity = false,
  494. #if defined (UART_PRESENT)
  495. .baud_rate = NRF_UART_BAUDRATE_115200
  496. #else
  497. .baud_rate = NRF_UARTE_BAUDRATE_115200
  498. #endif
  499. };
  500. APP_UART_FIFO_INIT(&comm_params,
  501. UART_RX_BUF_SIZE,
  502. UART_TX_BUF_SIZE,
  503. uart_event_handle,
  504. APP_IRQ_PRIORITY_LOWEST,
  505. err_code);
  506. APP_ERROR_CHECK(err_code);
  507. }
  508. /**@snippet [UART Initialization] */
  509. /**@brief Function for initializing the Advertising functionality.
  510. */
  511. static void advertising_init(void)
  512. {
  513. uint32_t err_code;
  514. ble_advertising_init_t init;
  515. memset(&init, 0, sizeof(init));
  516. init.advdata.name_type = BLE_ADVDATA_FULL_NAME;
  517. init.advdata.include_appearance = false;
  518. init.advdata.flags = BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE;
  519. init.srdata.uuids_complete.uuid_cnt = sizeof(m_adv_uuids) / sizeof(m_adv_uuids[0]);
  520. init.srdata.uuids_complete.p_uuids = m_adv_uuids;
  521. init.config.ble_adv_fast_enabled = true;
  522. init.config.ble_adv_fast_interval = APP_ADV_INTERVAL;
  523. init.config.ble_adv_fast_timeout = APP_ADV_DURATION;
  524. init.evt_handler = on_adv_evt;
  525. err_code = ble_advertising_init(&m_advertising, &init);
  526. APP_ERROR_CHECK(err_code);
  527. ble_advertising_conn_cfg_tag_set(&m_advertising, APP_BLE_CONN_CFG_TAG);
  528. }
  529. /**@brief Function for initializing buttons and leds.
  530. *
  531. * @param[out] p_erase_bonds Will be true if the clear bonding button was pressed to wake the application up.
  532. */
  533. static void buttons_leds_init(bool * p_erase_bonds)
  534. {
  535. bsp_event_t startup_event;
  536. uint32_t err_code = bsp_init(BSP_INIT_LEDS | BSP_INIT_BUTTONS, bsp_event_handler);
  537. APP_ERROR_CHECK(err_code);
  538. err_code = bsp_btn_ble_init(NULL, &startup_event);
  539. APP_ERROR_CHECK(err_code);
  540. *p_erase_bonds = (startup_event == BSP_EVENT_CLEAR_BONDING_DATA);
  541. }
  542. /**@brief Function for initializing the nrf log module.
  543. */
  544. static void log_init(void)
  545. {
  546. ret_code_t err_code = NRF_LOG_INIT(NULL);
  547. APP_ERROR_CHECK(err_code);
  548. NRF_LOG_DEFAULT_BACKENDS_INIT();
  549. }
  550. /**@brief Function for initializing power management.
  551. */
  552. static void power_management_init(void)
  553. {
  554. ret_code_t err_code;
  555. err_code = nrf_pwr_mgmt_init();
  556. APP_ERROR_CHECK(err_code);
  557. }
  558. /**@brief Function for handling the idle state (main loop).
  559. *
  560. * @details If there is no pending log operation, then sleep until next the next event occurs.
  561. */
  562. static void idle_state_handle(void)
  563. {
  564. if (NRF_LOG_PROCESS() == false)
  565. {
  566. nrf_pwr_mgmt_run();
  567. }
  568. }
  569. /**@brief Function for starting advertising.
  570. */
  571. static void advertising_start(void)
  572. {
  573. uint32_t err_code = ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST);
  574. APP_ERROR_CHECK(err_code);
  575. }
  576. /**@brief Application main function.
  577. */
  578. int main(void)
  579. {
  580. bool erase_bonds;
  581. // Initialize.
  582. uart_init();
  583. log_init();
  584. timers_init();
  585. buttons_leds_init(&erase_bonds);
  586. power_management_init();
  587. ble_stack_init();
  588. gap_params_init();
  589. gatt_init();
  590. services_init();
  591. advertising_init();
  592. conn_params_init();
  593. // Start execution.
  594. printf("\r\nUART started.\r\n");
  595. NRF_LOG_INFO("Debug logging for UART over RTT started.");
  596. advertising_start();
  597. // Enter main loop.
  598. for (;;)
  599. {
  600. idle_state_handle();
  601. }
  602. }
  603. /**
  604. * @}
  605. */