main.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * Copyright (c) 2017 - 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 ant_sdk_app_connectivity_main main.c
  43. * @{
  44. * @ingroup ant_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 "nrf_sdh_ant.h"
  55. #include "ser_hal_transport.h"
  56. #include "ser_conn_handlers.h"
  57. #include "boards.h"
  58. #include "nrf_gpio.h"
  59. #include "nrf_log_ctrl.h"
  60. #include "nrf_log_default_backends.h"
  61. #define NRF_LOG_MODULE_NAME CONN
  62. #include "nrf_log.h"
  63. NRF_LOG_MODULE_REGISTER();
  64. #include "ser_phy_debug_comm.h"
  65. /**@brief Main function of the connectivity application. */
  66. int main(void)
  67. {
  68. uint32_t err_code = NRF_SUCCESS;
  69. APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
  70. NRF_LOG_DEFAULT_BACKENDS_INIT();
  71. NRF_LOG_INFO("ANT connectivity started");
  72. #if ( defined(SER_PHY_HCI_DEBUG_ENABLE) || defined(SER_PHY_DEBUG_APP_ENABLE))
  73. debug_init(NULL);
  74. #endif
  75. /* Force constant latency mode to control SPI slave timing */
  76. NRF_POWER->TASKS_CONSTLAT = 1;
  77. /* Initialize scheduler queue. */
  78. APP_SCHED_INIT(SER_CONN_SCHED_MAX_EVENT_DATA_SIZE, SER_CONN_SCHED_QUEUE_SIZE);
  79. /* Initialize SoftDevice.
  80. * SoftDevice Event IRQ is not scheduled but immediately copies ANT events to the application
  81. * scheduler queue */
  82. err_code = nrf_sdh_enable_request();
  83. APP_ERROR_CHECK(err_code);
  84. /* Open serialization HAL Transport layer and subscribe for HAL Transport events. */
  85. err_code = ser_hal_transport_open(ser_conn_hal_transport_event_handle);
  86. APP_ERROR_CHECK(err_code);
  87. /* Enter main loop. */
  88. for (;;)
  89. {
  90. /* Process SoftDevice events. */
  91. app_sched_execute();
  92. if (nrf_sdh_is_suspended())
  93. {
  94. // Resume pulling new events if queue utilization drops below 50%.
  95. if (app_sched_queue_space_get() > (SER_CONN_SCHED_QUEUE_SIZE >> 1))
  96. {
  97. nrf_sdh_resume();
  98. }
  99. }
  100. /* Process received packets.
  101. * We can NOT add received packets as events to the application scheduler queue because
  102. * received packets have to be processed before SoftDevice events but the scheduler queue
  103. * does not have priorities. */
  104. err_code = ser_conn_rx_process();
  105. APP_ERROR_CHECK(err_code);
  106. nrf_gpio_cfg_output(18);
  107. nrf_gpio_pin_set(18);
  108. (void)NRF_LOG_PROCESS();
  109. /* Sleep waiting for an application event. */
  110. err_code = sd_app_evt_wait();
  111. APP_ERROR_CHECK(err_code);
  112. }
  113. }
  114. /** @} */