main.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. /** @file
  13. *
  14. * @defgroup ble_sdk_app_dtm_main main.c
  15. * @{
  16. * @ingroup ble_sdk_app_dtm
  17. * @brief Serialized DTM Sample Application main file.
  18. *
  19. * This file contains the source code for an DTM activation example.
  20. */
  21. #include <stdbool.h>
  22. #include <stdint.h>
  23. #include "app_button.h"
  24. #include "app_timer.h"
  25. #include "ble_advdata.h"
  26. #include "ble_dtm_app.h"
  27. #include "nordic_common.h"
  28. #include "softdevice_handler.h"
  29. #include "bsp.h"
  30. #define IS_SRVC_CHANGED_CHARACT_PRESENT 0 /**< Include or not the service_changed characteristic. if not enabled, the server's database cannot be changed for the lifetime of the device*/
  31. #define DTM_INIT_BUTTON_ID 0 /**< Button to initializing DTM mode on connectivity chip. */
  32. #define DTM_TX_PIN_NO TX_PIN_NUMBER /**< Pin used by DTM to transmitting a data. */
  33. #define DTM_RX_PIN_NO RX_PIN_NUMBER /**< Pin used by DTM to receiving a data. */
  34. #define DEVICE_NAME "Nordic_DTM" /**< Name of device. Will be included in the advertising data. */
  35. #define APP_ADV_INTERVAL 64 /**< The advertising interval (in units of 0.625 ms. This value corresponds to 40 ms). */
  36. #define APP_ADV_TIMEOUT_IN_SECONDS 180 /**< The advertising timeout (in units of seconds). */
  37. #define APP_BUTTON_DETECTION_DELAY 100 /**< Delay of detecting button events. */
  38. #define APP_TIMER_PRESCALER 0 /**< Value of the RTC1 PRESCALER register. */
  39. #define APP_TIMER_OP_QUEUE_SIZE 4 /**< Size of timer operation queues. */
  40. #define DEAD_BEEF 0xDEADBEEF /**< Value used as error code on stack dump, can be used to identify stack location on stack unwind. */
  41. static ble_gap_adv_params_t m_adv_params; /**< Parameters to be passed to the stack when starting advertising. */
  42. /**@brief Callback function for asserts in the SoftDevice.
  43. *
  44. * @details This function will be called in case of an assert in the SoftDevice.
  45. *
  46. * @warning This handler is an example only and does not fit a final product. You need to analyze
  47. * how your product is supposed to react in case of Assert.
  48. * @warning On assert from the SoftDevice, the system can only recover on reset.
  49. *
  50. * @param[in] line_num Line number of the failing ASSERT call.
  51. * @param[in] file_name File name of the failing ASSERT call.
  52. */
  53. void assert_nrf_callback(uint16_t line_num, const uint8_t * p_file_name)
  54. {
  55. app_error_handler(DEAD_BEEF, line_num, p_file_name);
  56. }
  57. /**@brief Function for initializing the Advertising functionality.
  58. *
  59. * @details Encodes the required advertising data and passes it to the stack.
  60. * Also builds a structure to be passed to the stack when starting advertising.
  61. */
  62. static void advertising_init(void)
  63. {
  64. uint32_t err_code;
  65. ble_advdata_t advdata;
  66. ble_gap_conn_sec_mode_t sec_mode;
  67. uint8_t flags = BLE_GAP_ADV_FLAGS_LE_ONLY_LIMITED_DISC_MODE;
  68. // Set GAP parameters
  69. BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);
  70. err_code =
  71. sd_ble_gap_device_name_set(&sec_mode, (const uint8_t *)DEVICE_NAME, strlen(DEVICE_NAME));
  72. APP_ERROR_CHECK(err_code);
  73. err_code = sd_ble_gap_appearance_set(BLE_APPEARANCE_GENERIC_WATCH);
  74. APP_ERROR_CHECK(err_code);
  75. // Build and set advertising data
  76. memset(&advdata, 0, sizeof (advdata));
  77. advdata.name_type = BLE_ADVDATA_FULL_NAME;
  78. advdata.include_appearance = false;
  79. advdata.flags = flags;
  80. err_code = ble_advdata_set(&advdata, NULL);
  81. APP_ERROR_CHECK(err_code);
  82. // Initialize advertising parameters (used when starting advertising)
  83. memset(&m_adv_params, 0, sizeof (m_adv_params));
  84. m_adv_params.type = BLE_GAP_ADV_TYPE_ADV_IND;
  85. m_adv_params.p_peer_addr = NULL; // Undirected advertisement
  86. m_adv_params.fp = BLE_GAP_ADV_FP_ANY;
  87. m_adv_params.interval = APP_ADV_INTERVAL;
  88. m_adv_params.timeout = APP_ADV_TIMEOUT_IN_SECONDS;
  89. }
  90. /**@brief Function for starting advertising.
  91. */
  92. static void advertising_start(void)
  93. {
  94. uint32_t err_code;
  95. err_code = sd_ble_gap_adv_start(&m_adv_params);
  96. APP_ERROR_CHECK(err_code);
  97. err_code = bsp_indication_set(BSP_INDICATE_ADVERTISING);
  98. APP_ERROR_CHECK(err_code);
  99. // err_code = bsp_buttons_enable(1 << DTM_INIT_BUTTON_ID);
  100. // APP_ERROR_CHECK(err_code);
  101. }
  102. /**@brief Function for initializing the DTM mode.
  103. */
  104. static void dtm_init(void)
  105. {
  106. uint32_t err_code;
  107. app_uart_stream_comm_params_t uart_params;
  108. uart_params.baud_rate = UART_BAUD_RATE_19200;
  109. uart_params.rx_pin_no = DTM_RX_PIN_NO;
  110. uart_params.tx_pin_no = DTM_TX_PIN_NO;
  111. err_code = ble_dtm_init(&uart_params);
  112. if (err_code == NRF_SUCCESS)
  113. {
  114. err_code = bsp_indication_set(BSP_INDICATE_USER_STATE_OFF);
  115. APP_ERROR_CHECK(err_code);
  116. // Close serialization transport layer.
  117. err_code = sd_softdevice_disable();
  118. APP_ERROR_CHECK(err_code);
  119. // Enter to infinite loop.
  120. for (;;)
  121. {
  122. err_code = sd_app_evt_wait();
  123. APP_ERROR_CHECK(err_code);
  124. }
  125. }
  126. APP_ERROR_CHECK(err_code);
  127. }
  128. /**@brief Function for handling events from the BSP module.
  129. *
  130. * @param[in] event Event generated by button press.
  131. */
  132. static void bsp_event_handler(bsp_event_t event)
  133. {
  134. switch (event)
  135. {
  136. case BSP_EVENT_KEY_0:
  137. dtm_init();
  138. break;
  139. default:
  140. break;
  141. }
  142. }
  143. /**@brief Function for initializing the BLE stack.
  144. *
  145. * @details Initializes the SoftDevice and the BLE event interrupt.
  146. */
  147. static void ble_stack_init(void)
  148. {
  149. uint32_t err_code;
  150. nrf_clock_lf_cfg_t clock_lf_cfg = NRF_CLOCK_LFCLKSRC;
  151. // Initialize the SoftDevice handler module.
  152. SOFTDEVICE_HANDLER_INIT(&clock_lf_cfg, NULL);
  153. // Enable BLE stack
  154. ble_enable_params_t ble_enable_params;
  155. memset(&ble_enable_params, 0, sizeof (ble_enable_params));
  156. ble_enable_params.gap_enable_params.periph_conn_count = 1;
  157. ble_enable_params.gatts_enable_params.service_changed = IS_SRVC_CHANGED_CHARACT_PRESENT;
  158. err_code = sd_ble_enable(&ble_enable_params, NULL);
  159. APP_ERROR_CHECK(err_code);
  160. }
  161. /**@brief Function for application main entry.
  162. */
  163. int main(void)
  164. {
  165. uint32_t err_code = NRF_SUCCESS;
  166. // Initialize.
  167. ble_stack_init();
  168. APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, NULL);
  169. err_code = bsp_init(BSP_INIT_LED | BSP_INIT_BUTTONS,
  170. APP_TIMER_TICKS(APP_BUTTON_DETECTION_DELAY, APP_TIMER_PRESCALER),
  171. bsp_event_handler);
  172. APP_ERROR_CHECK(err_code);
  173. // Initialize advertising.
  174. advertising_init();
  175. // Start execution.
  176. advertising_start();
  177. // Enter main loop.
  178. for (;;)
  179. {
  180. err_code = sd_app_evt_wait();
  181. APP_ERROR_CHECK(err_code);
  182. }
  183. }
  184. /**
  185. * @}
  186. */