Guest User

ble_app_uart_flow_control

a guest
Mar 31st, 2016
813
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 20.67 KB | None | 0 0
  1. /* Copyright (c) 2014 Nordic Semiconductor. All Rights Reserved.
  2.  *
  3.  * The information contained herein is property of Nordic Semiconductor ASA.
  4.  * Terms and conditions of usage are described in detail in NORDIC
  5.  * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6.  *
  7.  * Licensees are granted free, non-transferable use of the information. NO
  8.  * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9.  * the file.
  10.  *
  11.  */
  12.  
  13. /** @file
  14.  *
  15.  * @defgroup ble_sdk_uart_over_ble_main main.c
  16.  * @{
  17.  * @ingroup  ble_sdk_app_nus_eval
  18.  * @brief    UART over BLE application main file.
  19.  *
  20.  * This file contains the source code for a sample application that uses the Nordic UART service.
  21.  * This application uses the @ref srvlib_conn_params module.
  22.  */
  23.  
  24. #include <stdint.h>
  25. #include <string.h>
  26. #include "nordic_common.h"
  27. #include "nrf.h"
  28. #include "ble_hci.h"
  29. #include "ble_advdata.h"
  30. #include "ble_advertising.h"
  31. #include "ble_conn_params.h"
  32. #include "softdevice_handler.h"
  33. #include "app_timer.h"
  34. #include "app_button.h"
  35. #include "ble_nus.h"
  36. #include "app_uart.h"
  37. #include "app_util_platform.h"
  38. #include "bsp.h"
  39. #include "bsp_btn_ble.h"
  40. #include "nrf_delay.h"
  41. #include "app_fifo.h"
  42.  
  43. #define IS_SRVC_CHANGED_CHARACT_PRESENT 0                                           /**< Include the service_changed characteristic. If not enabled, the server's database cannot be changed for the lifetime of the device. */
  44.  
  45. #define CENTRAL_LINK_COUNT              0                                           /**< Number of central links used by the application. When changing this number remember to adjust the RAM settings*/
  46. #define PERIPHERAL_LINK_COUNT           1                                           /**< Number of peripheral links used by the application. When changing this number remember to adjust the RAM settings*/
  47.  
  48. #define DEVICE_NAME                     "Nordic_UART"                               /**< Name of device. Will be included in the advertising data. */
  49. #define NUS_SERVICE_UUID_TYPE           BLE_UUID_TYPE_VENDOR_BEGIN                  /**< UUID type for the Nordic UART Service (vendor specific). */
  50.  
  51. #define APP_ADV_INTERVAL                64                                          /**< The advertising interval (in units of 0.625 ms. This value corresponds to 40 ms). */
  52. #define APP_ADV_TIMEOUT_IN_SECONDS      180                                         /**< The advertising timeout (in units of seconds). */
  53.  
  54. #define APP_TIMER_PRESCALER             0                                           /**< Value of the RTC1 PRESCALER register. */
  55. #define APP_TIMER_OP_QUEUE_SIZE         4                                           /**< Size of timer operation queues. */
  56.  
  57. #define MIN_CONN_INTERVAL               MSEC_TO_UNITS(500, UNIT_1_25_MS)             /**< Minimum acceptable connection interval (20 ms), Connection interval uses 1.25 ms units. */
  58. #define MAX_CONN_INTERVAL               MSEC_TO_UNITS(1000, UNIT_1_25_MS)             /**< Maximum acceptable connection interval (75 ms), Connection interval uses 1.25 ms units. */
  59. #define SLAVE_LATENCY                   0                                           /**< Slave latency. */
  60. #define CONN_SUP_TIMEOUT                MSEC_TO_UNITS(4000, UNIT_10_MS)             /**< Connection supervisory timeout (4 seconds), Supervision Timeout uses 10 ms units. */
  61. #define FIRST_CONN_PARAMS_UPDATE_DELAY  APP_TIMER_TICKS(1000, APP_TIMER_PRESCALER)  /**< Time from initiating event (connect or start of notification) to first time sd_ble_gap_conn_param_update is called (5 seconds). */
  62. #define NEXT_CONN_PARAMS_UPDATE_DELAY   APP_TIMER_TICKS(5000, APP_TIMER_PRESCALER) /**< Time between each call to sd_ble_gap_conn_param_update after the first call (30 seconds). */
  63. #define MAX_CONN_PARAMS_UPDATE_COUNT    3                                           /**< Number of attempts before giving up the connection parameter negotiation. */
  64.  
  65. #define DEAD_BEEF                       0xDEADBEEF                                  /**< Value used as error code on stack dump, can be used to identify stack location on stack unwind. */
  66.  
  67. #define UART_TX_BUF_SIZE                256                                         /**< UART TX buffer size. */
  68. #define UART_RX_BUF_SIZE                256                                         /**< UART RX buffer size. */
  69.  
  70. static ble_nus_t                        m_nus;                                      /**< Structure to identify the Nordic UART Service. */
  71. static uint16_t                         m_conn_handle = BLE_CONN_HANDLE_INVALID;    /**< Handle of the current connection. */
  72.  
  73. static ble_uuid_t                       m_adv_uuids[] = {{BLE_UUID_NUS_SERVICE, NUS_SERVICE_UUID_TYPE}};  /**< Universally unique service identifier. */
  74.  
  75.  
  76. /**@brief Function for assert macro callback.
  77.  *
  78.  * @details This function will be called in case of an assert in the SoftDevice.
  79.  *
  80.  * @warning This handler is an example only and does not fit a final product. You need to analyse
  81.  *          how your product is supposed to react in case of Assert.
  82.  * @warning On assert from the SoftDevice, the system can only recover on reset.
  83.  *
  84.  * @param[in] line_num    Line number of the failing ASSERT call.
  85.  * @param[in] p_file_name File name of the failing ASSERT call.
  86.  */
  87. void assert_nrf_callback(uint16_t line_num, const uint8_t * p_file_name)
  88. {
  89.     app_error_handler(DEAD_BEEF, line_num, p_file_name);
  90. }
  91.  
  92.  
  93. /**@brief Function for the GAP initialization.
  94.  *
  95.  * @details This function will set up all the necessary GAP (Generic Access Profile) parameters of
  96.  *          the device. It also sets the permissions and appearance.
  97.  */
  98. static void gap_params_init(void)
  99. {
  100.     uint32_t                err_code;
  101.     ble_gap_conn_params_t   gap_conn_params;
  102.     ble_gap_conn_sec_mode_t sec_mode;
  103.  
  104.     BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);
  105.    
  106.     err_code = sd_ble_gap_device_name_set(&sec_mode,
  107.                                           (const uint8_t *) DEVICE_NAME,
  108.                                           strlen(DEVICE_NAME));
  109.     APP_ERROR_CHECK(err_code);
  110.  
  111.     memset(&gap_conn_params, 0, sizeof(gap_conn_params));
  112.  
  113.     gap_conn_params.min_conn_interval = MIN_CONN_INTERVAL;
  114.     gap_conn_params.max_conn_interval = MAX_CONN_INTERVAL;
  115.     gap_conn_params.slave_latency     = SLAVE_LATENCY;
  116.     gap_conn_params.conn_sup_timeout  = CONN_SUP_TIMEOUT;
  117.  
  118.     err_code = sd_ble_gap_ppcp_set(&gap_conn_params);
  119.     APP_ERROR_CHECK(err_code);
  120. }
  121.  
  122.  
  123. /**@brief Function for handling the data from the Nordic UART Service.
  124.  *
  125.  * @details This function will process the data received from the Nordic UART BLE Service and send
  126.  *          it to the UART module.
  127.  *
  128.  * @param[in] p_nus    Nordic UART Service structure.
  129.  * @param[in] p_data   Data to be send to UART module.
  130.  * @param[in] length   Length of the data.
  131.  */
  132. /**@snippet [Handling the data received over BLE] */
  133. static void nus_data_handler(ble_nus_t * p_nus, uint8_t * p_data, uint16_t length)
  134. {
  135.     for (uint32_t i = 0; i < length; i++)
  136.     {
  137.         while(app_uart_put(p_data[i]) != NRF_SUCCESS);
  138.     }
  139.     while(app_uart_put('\n') != NRF_SUCCESS);
  140. }
  141. /**@snippet [Handling the data received over BLE] */
  142.  
  143.  
  144. /**@brief Function for initializing services that will be used by the application.
  145.  */
  146. static void services_init(void)
  147. {
  148.     uint32_t       err_code;
  149.     ble_nus_init_t nus_init;
  150.    
  151.     memset(&nus_init, 0, sizeof(nus_init));
  152.  
  153.     nus_init.data_handler = nus_data_handler;
  154.    
  155.     err_code = ble_nus_init(&m_nus, &nus_init);
  156.     APP_ERROR_CHECK(err_code);
  157. }
  158.  
  159.  
  160. /**@brief Function for handling an event from the Connection Parameters Module.
  161.  *
  162.  * @details This function will be called for all events in the Connection Parameters Module
  163.  *          which are passed to the application.
  164.  *
  165.  * @note All this function does is to disconnect. This could have been done by simply setting
  166.  *       the disconnect_on_fail config parameter, but instead we use the event handler
  167.  *       mechanism to demonstrate its use.
  168.  *
  169.  * @param[in] p_evt  Event received from the Connection Parameters Module.
  170.  */
  171. static void on_conn_params_evt(ble_conn_params_evt_t * p_evt)
  172. {
  173.     uint32_t err_code;
  174.    
  175.     if(p_evt->evt_type == BLE_CONN_PARAMS_EVT_FAILED)
  176.     {
  177.         err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_CONN_INTERVAL_UNACCEPTABLE);
  178.         APP_ERROR_CHECK(err_code);
  179.     }
  180. }
  181.  
  182.  
  183. /**@brief Function for handling errors from the Connection Parameters module.
  184.  *
  185.  * @param[in] nrf_error  Error code containing information about what went wrong.
  186.  */
  187. static void conn_params_error_handler(uint32_t nrf_error)
  188. {
  189.     APP_ERROR_HANDLER(nrf_error);
  190. }
  191.  
  192.  
  193. /**@brief Function for initializing the Connection Parameters module.
  194.  */
  195. static void conn_params_init(void)
  196. {
  197.     uint32_t               err_code;
  198.     ble_conn_params_init_t cp_init;
  199.    
  200.     memset(&cp_init, 0, sizeof(cp_init));
  201.  
  202.     cp_init.p_conn_params                  = NULL;
  203.     cp_init.first_conn_params_update_delay = FIRST_CONN_PARAMS_UPDATE_DELAY;
  204.     cp_init.next_conn_params_update_delay  = NEXT_CONN_PARAMS_UPDATE_DELAY;
  205.     cp_init.max_conn_params_update_count   = MAX_CONN_PARAMS_UPDATE_COUNT;
  206.     cp_init.start_on_notify_cccd_handle    = BLE_GATT_HANDLE_INVALID;
  207.     cp_init.disconnect_on_fail             = false;
  208.     cp_init.evt_handler                    = on_conn_params_evt;
  209.     cp_init.error_handler                  = conn_params_error_handler;
  210.    
  211.     err_code = ble_conn_params_init(&cp_init);
  212.     APP_ERROR_CHECK(err_code);
  213. }
  214.  
  215.  
  216. /**@brief Function for putting the chip into sleep mode.
  217.  *
  218.  * @note This function will not return.
  219.  */
  220. static void sleep_mode_enter(void)
  221. {
  222.     uint32_t err_code = bsp_indication_set(BSP_INDICATE_IDLE);
  223.     APP_ERROR_CHECK(err_code);
  224.  
  225.     // Prepare wakeup buttons.
  226.     err_code = bsp_btn_ble_sleep_mode_prepare();
  227.     APP_ERROR_CHECK(err_code);
  228.  
  229.     // Go to system-off mode (this function will not return; wakeup will cause a reset).
  230.     err_code = sd_power_system_off();
  231.     APP_ERROR_CHECK(err_code);
  232. }
  233.  
  234.  
  235. /**@brief Function for handling advertising events.
  236.  *
  237.  * @details This function will be called for advertising events which are passed to the application.
  238.  *
  239.  * @param[in] ble_adv_evt  Advertising event.
  240.  */
  241. static void on_adv_evt(ble_adv_evt_t ble_adv_evt)
  242. {
  243.     uint32_t err_code;
  244.  
  245.     switch (ble_adv_evt)
  246.     {
  247.         case BLE_ADV_EVT_FAST:
  248.             err_code = bsp_indication_set(BSP_INDICATE_ADVERTISING);
  249.             APP_ERROR_CHECK(err_code);
  250.             break;
  251.         case BLE_ADV_EVT_IDLE:
  252.             sleep_mode_enter();
  253.             break;
  254.         default:
  255.             break;
  256.     }
  257. }
  258.  
  259. #define UART_FIFO_BUF_SIZE  1024
  260.  
  261. static uint32_t fifo_length(app_fifo_t * const fifo)
  262. {
  263.   uint32_t tmp = fifo->read_pos;
  264.   return fifo->write_pos - tmp;
  265. }
  266.  
  267. static app_fifo_t   uart_rx_fifo;
  268. static uint8_t      uart_rx_buf[UART_FIFO_BUF_SIZE];
  269.  
  270. /**@brief   Function for handling app_uart events.
  271.  *
  272.  * @details This function will receive a single character from the app_uart module and append it to
  273.  *          a string. The string will be be sent over BLE when the last character received was a
  274.  *          'new line' i.e '\n' (hex 0x0D) or if the string has reached a length of
  275.  *          @ref NUS_MAX_DATA_LENGTH.
  276.  */
  277. /**@snippet [Handling the data received over UART] */
  278. void uart_event_handle(app_uart_evt_t * p_event)
  279. {
  280.     static uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
  281.     static uint8_t index = 0;
  282.     uint32_t       err_code;
  283.  
  284.     switch (p_event->evt_type)
  285.     {
  286.         case APP_UART_DATA_READY:
  287.             UNUSED_VARIABLE(app_uart_get(&data_array[index]));
  288.             index++;
  289.  
  290.             if ((data_array[index - 1] == '\n') || (index >= (BLE_NUS_MAX_DATA_LEN)))
  291.             {
  292.                 err_code = ble_nus_string_send(&m_nus, data_array, index);
  293.                 if (err_code == BLE_ERROR_NO_TX_PACKETS)
  294.                 {
  295.                     for(int i = 0; i < index; i++)
  296.                     {
  297.                         app_fifo_put(&uart_rx_fifo, data_array[i]);
  298.                     }
  299.                 }
  300.                 else if (err_code != NRF_ERROR_INVALID_STATE)
  301.                 {
  302.                     APP_ERROR_CHECK(err_code);
  303.                 }
  304.                
  305.                 index = 0;
  306.             }
  307.             break;
  308.  
  309.         case APP_UART_COMMUNICATION_ERROR:
  310.             APP_ERROR_HANDLER(p_event->data.error_communication);
  311.             break;
  312.  
  313.         case APP_UART_FIFO_ERROR:
  314.             APP_ERROR_HANDLER(p_event->data.error_code);
  315.             break;
  316.  
  317.         default:
  318.             break;
  319.     }
  320. }
  321. /**@snippet [Handling the data received over UART] */
  322.  
  323.  
  324. /**@brief  Function for initializing the UART module.
  325.  */
  326. /**@snippet [UART Initialization] */
  327. static void uart_init(void)
  328. {
  329.     uint32_t                     err_code;
  330.     const app_uart_comm_params_t comm_params =
  331.     {
  332.         RX_PIN_NUMBER,
  333.         TX_PIN_NUMBER,
  334.         RTS_PIN_NUMBER,
  335.         CTS_PIN_NUMBER,
  336.         APP_UART_FLOW_CONTROL_ENABLED,
  337.         false,
  338.         UART_BAUDRATE_BAUDRATE_Baud230400
  339.     };
  340.  
  341.     APP_UART_FIFO_INIT( &comm_params,
  342.                        UART_RX_BUF_SIZE,
  343.                        UART_TX_BUF_SIZE,
  344.                        uart_event_handle,
  345.                        APP_IRQ_PRIORITY_LOW,
  346.                        err_code);
  347.     APP_ERROR_CHECK(err_code);
  348.    
  349.     err_code = app_fifo_init(&uart_rx_fifo, uart_rx_buf, UART_FIFO_BUF_SIZE);
  350.     APP_ERROR_CHECK(err_code);
  351.    
  352. }
  353. /**@snippet [UART Initialization] */
  354.  
  355. void on_tx_complete()
  356. {
  357.     uint32_t err_code;
  358.     uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
  359.     uint8_t index = 0;
  360.     uint8_t data;
  361.    
  362.     while( fifo_length(&uart_rx_fifo) > 0)
  363.     {
  364.         app_fifo_get(&uart_rx_fifo, &data);
  365.         data_array[index++] = data;
  366.         if( (index == BLE_NUS_MAX_DATA_LEN) || (fifo_length(&uart_rx_fifo) == 0) )
  367.         {
  368.             err_code = ble_nus_string_send(&m_nus, data_array, index);
  369.             if (err_code == BLE_ERROR_NO_TX_PACKETS)
  370.             {
  371.                 for(int i = 0; i < index; i++)
  372.                 {
  373.                     app_fifo_put(&uart_rx_fifo, data_array[i]);
  374.                 }
  375.                 break;
  376.             }
  377.             else if (err_code != NRF_ERROR_INVALID_STATE)
  378.             {
  379.                 APP_ERROR_CHECK(err_code);
  380.             }
  381.             index = 0;
  382.         }
  383.     }
  384. }
  385.  
  386. /**@brief Function for the application's SoftDevice event handler.
  387.  *
  388.  * @param[in] p_ble_evt SoftDevice event.
  389.  */
  390. static void on_ble_evt(ble_evt_t * p_ble_evt)
  391. {
  392.     uint32_t                         err_code;
  393.    
  394.     switch (p_ble_evt->header.evt_id)
  395.     {
  396.         case BLE_GAP_EVT_CONNECTED:
  397.             err_code = bsp_indication_set(BSP_INDICATE_CONNECTED);
  398.             APP_ERROR_CHECK(err_code);
  399.             m_conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
  400.             break;
  401.            
  402.         case BLE_GAP_EVT_DISCONNECTED:
  403.             err_code = bsp_indication_set(BSP_INDICATE_IDLE);
  404.             APP_ERROR_CHECK(err_code);
  405.             m_conn_handle = BLE_CONN_HANDLE_INVALID;
  406.             break;
  407.  
  408.         case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
  409.             // Pairing not supported
  410.             err_code = sd_ble_gap_sec_params_reply(m_conn_handle, BLE_GAP_SEC_STATUS_PAIRING_NOT_SUPP, NULL, NULL);
  411.             APP_ERROR_CHECK(err_code);
  412.             break;
  413.  
  414.         case BLE_GATTS_EVT_SYS_ATTR_MISSING:
  415.             // No system attributes have been stored.
  416.             err_code = sd_ble_gatts_sys_attr_set(m_conn_handle, NULL, 0, 0);
  417.             APP_ERROR_CHECK(err_code);
  418.             break;
  419.        
  420.         case BLE_EVT_TX_COMPLETE:
  421.             on_tx_complete();
  422.             break;
  423.  
  424.         default:
  425.             // No implementation needed.
  426.             break;
  427.     }
  428. }
  429.  
  430.  
  431. /**@brief Function for dispatching a SoftDevice event to all modules with a SoftDevice
  432.  *        event handler.
  433.  *
  434.  * @details This function is called from the SoftDevice event interrupt handler after a
  435.  *          SoftDevice event has been received.
  436.  *
  437.  * @param[in] p_ble_evt  SoftDevice event.
  438.  */
  439. static void ble_evt_dispatch(ble_evt_t * p_ble_evt)
  440. {
  441.     ble_conn_params_on_ble_evt(p_ble_evt);
  442.     ble_nus_on_ble_evt(&m_nus, p_ble_evt);
  443.     on_ble_evt(p_ble_evt);
  444.     ble_advertising_on_ble_evt(p_ble_evt);
  445.     bsp_btn_ble_on_ble_evt(p_ble_evt);
  446.    
  447. }
  448.  
  449.  
  450. /**@brief Function for the SoftDevice initialization.
  451.  *
  452.  * @details This function initializes the SoftDevice and the BLE event interrupt.
  453.  */
  454. static void ble_stack_init(void)
  455. {
  456.     uint32_t err_code;
  457.    
  458.     nrf_clock_lf_cfg_t clock_lf_cfg = NRF_CLOCK_LFCLKSRC;
  459.    
  460.     // Initialize SoftDevice.
  461.     SOFTDEVICE_HANDLER_INIT(&clock_lf_cfg, NULL);
  462.    
  463.     ble_enable_params_t ble_enable_params;
  464.     err_code = softdevice_enable_get_default_config(CENTRAL_LINK_COUNT,
  465.                                                     PERIPHERAL_LINK_COUNT,
  466.                                                     &ble_enable_params);
  467.     APP_ERROR_CHECK(err_code);
  468.        
  469.     //Check the ram settings against the used number of links
  470.     CHECK_RAM_START_ADDR(CENTRAL_LINK_COUNT,PERIPHERAL_LINK_COUNT);
  471.     // Enable BLE stack.
  472.     err_code = softdevice_enable(&ble_enable_params);
  473.     APP_ERROR_CHECK(err_code);
  474.    
  475.     // Subscribe for BLE events.
  476.     err_code = softdevice_ble_evt_handler_set(ble_evt_dispatch);
  477.     APP_ERROR_CHECK(err_code);
  478. }
  479.  
  480.  
  481. /**@brief Function for handling events from the BSP module.
  482.  *
  483.  * @param[in]   event   Event generated by button press.
  484.  */
  485. void bsp_event_handler(bsp_event_t event)
  486. {
  487.     uint32_t err_code;
  488.     switch (event)
  489.     {
  490.         case BSP_EVENT_SLEEP:
  491.             sleep_mode_enter();
  492.             break;
  493.  
  494.         case BSP_EVENT_DISCONNECT:
  495.             err_code = sd_ble_gap_disconnect(m_conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
  496.             if (err_code != NRF_ERROR_INVALID_STATE)
  497.             {
  498.                 APP_ERROR_CHECK(err_code);
  499.             }
  500.             break;
  501.  
  502.         case BSP_EVENT_WHITELIST_OFF:
  503.             err_code = ble_advertising_restart_without_whitelist();
  504.             if (err_code != NRF_ERROR_INVALID_STATE)
  505.             {
  506.                 APP_ERROR_CHECK(err_code);
  507.             }
  508.             break;
  509.  
  510.         default:
  511.             break;
  512.     }
  513. }
  514.  
  515.  
  516. /**@brief Function for initializing the Advertising functionality.
  517.  */
  518. static void advertising_init(void)
  519. {
  520.     uint32_t      err_code;
  521.     ble_advdata_t advdata;
  522.     ble_advdata_t scanrsp;
  523.  
  524.     // Build advertising data struct to pass into @ref ble_advertising_init.
  525.     memset(&advdata, 0, sizeof(advdata));
  526.     advdata.name_type          = BLE_ADVDATA_FULL_NAME;
  527.     advdata.include_appearance = false;
  528.     advdata.flags              = BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE;
  529.  
  530.     memset(&scanrsp, 0, sizeof(scanrsp));
  531.     scanrsp.uuids_complete.uuid_cnt = sizeof(m_adv_uuids) / sizeof(m_adv_uuids[0]);
  532.     scanrsp.uuids_complete.p_uuids  = m_adv_uuids;
  533.  
  534.     ble_adv_modes_config_t options = {0};
  535.     options.ble_adv_fast_enabled  = BLE_ADV_FAST_ENABLED;
  536.     options.ble_adv_fast_interval = APP_ADV_INTERVAL;
  537.     options.ble_adv_fast_timeout  = APP_ADV_TIMEOUT_IN_SECONDS;
  538.  
  539.     err_code = ble_advertising_init(&advdata, &scanrsp, &options, on_adv_evt, NULL);
  540.     APP_ERROR_CHECK(err_code);
  541. }
  542.  
  543.  
  544. /**@brief Function for initializing buttons and leds.
  545.  *
  546.  * @param[out] p_erase_bonds  Will be true if the clear bonding button was pressed to wake the application up.
  547.  */
  548. static void buttons_leds_init(bool * p_erase_bonds)
  549. {
  550.     bsp_event_t startup_event;
  551.  
  552.     uint32_t err_code = bsp_init(BSP_INIT_LED | BSP_INIT_BUTTONS,
  553.                                  APP_TIMER_TICKS(100, APP_TIMER_PRESCALER),
  554.                                  bsp_event_handler);
  555.     APP_ERROR_CHECK(err_code);
  556.  
  557.     err_code = bsp_btn_ble_init(NULL, &startup_event);
  558.     APP_ERROR_CHECK(err_code);
  559.  
  560.     *p_erase_bonds = (startup_event == BSP_EVENT_CLEAR_BONDING_DATA);
  561. }
  562.  
  563.  
  564. /**@brief Function for placing the application in low power state while waiting for events.
  565.  */
  566. static void power_manage(void)
  567. {
  568.     uint32_t err_code = sd_app_evt_wait();
  569.     APP_ERROR_CHECK(err_code);
  570. }
  571.  
  572.  
  573. /**@brief Application main function.
  574.  */
  575. int main(void)
  576. {
  577.     uint32_t err_code;
  578.     bool erase_bonds;
  579.  
  580.     // Initialize.
  581.     APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);
  582.     uart_init();
  583.    
  584.     buttons_leds_init(&erase_bonds);
  585.     ble_stack_init();
  586.     gap_params_init();
  587.     services_init();
  588.     advertising_init();
  589.     conn_params_init();
  590.  
  591.     printf("\r\nUART Start!\r\n");
  592.     err_code = ble_advertising_start(BLE_ADV_MODE_FAST);
  593.     APP_ERROR_CHECK(err_code);
  594.    
  595.     // Enter main loop.
  596.     for (;;)
  597.     {
  598.         power_manage();
  599.     }
  600. }
  601.  
  602.  
  603. /**
  604.  * @}
  605.  */
Advertisement
Add Comment
Please, Sign In to add comment