main.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /**
  2. * Copyright (c) 2013 - 2020, 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_app_connectivity_main main.c
  43. * @{
  44. * @ingroup ble_sdk_app_connectivity
  45. *
  46. * @brief BLE Connectivity application.
  47. */
  48. #include <stdbool.h>
  49. #include "nrf_sdm.h"
  50. #include "nrf_soc.h"
  51. #include "app_error.h"
  52. #include "app_scheduler.h"
  53. #include "nrf_sdh.h"
  54. #include "ser_hal_transport.h"
  55. #include "ser_conn_handlers.h"
  56. #include "boards.h"
  57. #include "nrf_drv_clock.h"
  58. #include "nrf_log.h"
  59. #include "nrf_log_ctrl.h"
  60. #include "nrf_log_default_backends.h"
  61. #include "app_timer.h"
  62. #include "ser_phy_debug_comm.h"
  63. #if defined(APP_USBD_ENABLED) && APP_USBD_ENABLED
  64. #include "app_usbd_serial_num.h"
  65. #ifdef BOARD_PCA10059
  66. #include "nrf_dfu_trigger_usb.h"
  67. #endif
  68. #include "app_usbd.h"
  69. static volatile bool m_usb_started;
  70. static void usbd_user_evt_handler(app_usbd_event_type_t event)
  71. {
  72. switch (event)
  73. {
  74. case APP_USBD_EVT_DRV_SUSPEND:
  75. break;
  76. case APP_USBD_EVT_DRV_RESUME:
  77. break;
  78. case APP_USBD_EVT_STARTED:
  79. m_usb_started = true;
  80. break;
  81. case APP_USBD_EVT_STOPPED:
  82. app_usbd_disable();
  83. break;
  84. case APP_USBD_EVT_POWER_DETECTED:
  85. NRF_LOG_INFO("USB power detected");
  86. if (!nrf_drv_usbd_is_enabled())
  87. {
  88. app_usbd_enable();
  89. }
  90. break;
  91. case APP_USBD_EVT_POWER_REMOVED:
  92. NRF_LOG_INFO("USB power removed");
  93. app_usbd_stop();
  94. break;
  95. case APP_USBD_EVT_POWER_READY:
  96. NRF_LOG_INFO("USB ready");
  97. app_usbd_start();
  98. break;
  99. default:
  100. break;
  101. }
  102. }
  103. static void usbd_init(void)
  104. {
  105. app_usbd_serial_num_generate();
  106. static const app_usbd_config_t usbd_config = {
  107. .ev_state_proc = usbd_user_evt_handler
  108. };
  109. APP_ERROR_CHECK(app_usbd_init(&usbd_config));
  110. }
  111. static void usbd_enable(void)
  112. {
  113. #ifdef BOARD_PCA10059
  114. APP_ERROR_CHECK(nrf_dfu_trigger_usb_init());
  115. #endif
  116. APP_ERROR_CHECK(app_usbd_power_events_enable());
  117. /* Process USB events until USB is started. This is related to the fact that
  118. * current version of softdevice does not handle USB POWER events. */
  119. while (m_usb_started == false)
  120. {
  121. while (app_usbd_event_queue_process())
  122. {
  123. /* Nothing to do */
  124. }
  125. }
  126. }
  127. #endif //APP_USBD_ENABLED
  128. static void on_idle(void)
  129. {
  130. if (!NRF_LOG_PROCESS())
  131. {
  132. // Wait for an event.
  133. if (nrf_sdh_is_enabled())
  134. {
  135. ret_code_t ret_code = sd_app_evt_wait();
  136. ASSERT((ret_code == NRF_SUCCESS) || (ret_code == NRF_ERROR_SOFTDEVICE_NOT_ENABLED));
  137. UNUSED_VARIABLE(ret_code);
  138. }
  139. else
  140. {
  141. // Wait for an event.
  142. __WFE();
  143. // Clear the internal event register.
  144. __SEV();
  145. __WFE();
  146. }
  147. }
  148. #if defined(APP_USBD_ENABLED) && APP_USBD_ENABLED
  149. while (app_usbd_event_queue_process())
  150. {
  151. /* Nothing to do */
  152. }
  153. #endif
  154. }
  155. /**@brief Main function of the connectivity application. */
  156. int main(void)
  157. {
  158. uint32_t err_code = NRF_SUCCESS;
  159. APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
  160. NRF_LOG_DEFAULT_BACKENDS_INIT();
  161. NRF_LOG_INFO("BLE connectivity started");
  162. bsp_board_init(BSP_INIT_LEDS);
  163. #if (defined(SER_PHY_HCI_DEBUG_ENABLE) || defined(SER_PHY_DEBUG_APP_ENABLE))
  164. debug_init(NULL);
  165. #endif
  166. /* Force constant latency mode to control SPI slave timing */
  167. NRF_POWER->TASKS_CONSTLAT = 1;
  168. /* Initialize scheduler queue. */
  169. //lint -save -e666 -e587
  170. APP_SCHED_INIT(SER_CONN_SCHED_MAX_EVENT_DATA_SIZE, SER_CONN_SCHED_QUEUE_SIZE);
  171. //lint -restore
  172. /* Initialize SoftDevice.
  173. * SoftDevice Event IRQ is not scheduled but immediately copies BLE events to the application
  174. * scheduler queue */
  175. err_code = nrf_drv_clock_init();
  176. if ((err_code != NRF_SUCCESS) &&
  177. (err_code != NRF_ERROR_MODULE_ALREADY_INITIALIZED))
  178. {
  179. APP_ERROR_CHECK(err_code);
  180. }
  181. nrf_drv_clock_hfclk_request(NULL);
  182. while (!nrf_drv_clock_hfclk_is_running())
  183. {}
  184. #if defined(APP_USBD_ENABLED) && APP_USBD_ENABLED
  185. usbd_init();
  186. #endif
  187. /* Open serialization HAL Transport layer and subscribe for HAL Transport events. */
  188. err_code = ser_hal_transport_open(ser_conn_hal_transport_event_handle);
  189. APP_ERROR_CHECK(err_code);
  190. #if defined(APP_USBD_ENABLED) && APP_USBD_ENABLED
  191. usbd_enable();
  192. #endif
  193. err_code = nrf_sdh_enable_request();
  194. APP_ERROR_CHECK(err_code);
  195. err_code = app_timer_init();
  196. APP_ERROR_CHECK(err_code);
  197. ser_conn_on_no_mem_handler_set(on_idle);
  198. /* Enter main loop. */
  199. for (;;)
  200. {
  201. /* Process SoftDevice events. */
  202. app_sched_execute();
  203. CRITICAL_REGION_ENTER();
  204. if (nrf_sdh_is_suspended())
  205. {
  206. // Resume pulling new events if queue utilization drops below 50%.
  207. if (app_sched_queue_space_get() > (SER_CONN_SCHED_QUEUE_SIZE >> 1))
  208. {
  209. nrf_sdh_resume();
  210. }
  211. }
  212. CRITICAL_REGION_EXIT();
  213. /* Process received packets.
  214. * We can NOT add received packets as events to the application scheduler queue because
  215. * received packets have to be processed before SoftDevice events but the scheduler queue
  216. * does not have priorities. */
  217. err_code = ser_conn_rx_process();
  218. APP_ERROR_CHECK(err_code);
  219. on_idle();
  220. }
  221. }
  222. /** @} */