bsp.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /**
  2. * Copyright (c) 2014 - 2019, 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 bsp Board Support Package
  43. * @{
  44. * @ingroup app_common
  45. *
  46. * @brief BSP module.
  47. * @details This module provides a layer of abstraction from the board.
  48. * It allows the user to indicate certain states on LEDs in a simple way.
  49. * Module functionality can be modified by defining BSP_SIMPLE to reduce
  50. * functionality of this module to enable and read state of the buttons.
  51. */
  52. #ifndef BSP_H__
  53. #define BSP_H__
  54. #include <stdint.h>
  55. #include <stdbool.h>
  56. #include "boards.h"
  57. #if !defined(BSP_DEFINES_ONLY) && !defined(BSP_SIMPLE)
  58. #include "app_button.h"
  59. #define BSP_BUTTON_ACTION_PUSH (APP_BUTTON_PUSH) /**< Represents pushing a button. See @ref bsp_button_action_t. */
  60. #define BSP_BUTTON_ACTION_RELEASE (APP_BUTTON_RELEASE) /**< Represents releasing a button. See @ref bsp_button_action_t. */
  61. #define BSP_BUTTON_ACTION_LONG_PUSH (2) /**< Represents pushing and holding a button for @ref BSP_LONG_PUSH_TIMEOUT_MS milliseconds. See also @ref bsp_button_action_t. */
  62. #endif
  63. /* BSP_UART_SUPPORT
  64. * This define enables UART support module.
  65. */
  66. #ifdef __cplusplus
  67. extern "C" {
  68. #endif
  69. typedef uint8_t bsp_button_action_t; /**< The different actions that can be performed on a button. */
  70. #define BSP_INDICATIONS_LIST { \
  71. "BSP_INDICATE_IDLE", \
  72. "BSP_INDICATE_SCANNING", \
  73. "BSP_INDICATE_ADVERTISING", \
  74. "BSP_INDICATE_ADVERTISING_WHITELIST", \
  75. "BSP_INDICATE_ADVERTISING_SLOW", \
  76. "BSP_INDICATE_ADVERTISING_DIRECTED", \
  77. "BSP_INDICATE_BONDING", \
  78. "BSP_INDICATE_CONNECTED", \
  79. "BSP_INDICATE_SENT_OK", \
  80. "BSP_INDICATE_SEND_ERROR", \
  81. "BSP_INDICATE_RCV_OK", \
  82. "BSP_INDICATE_RCV_ERROR", \
  83. "BSP_INDICATE_FATAL_ERROR", \
  84. "BSP_INDICATE_ALERT_0", \
  85. "BSP_INDICATE_ALERT_1", \
  86. "BSP_INDICATE_ALERT_2", \
  87. "BSP_INDICATE_ALERT_3", \
  88. "BSP_INDICATE_ALERT_OFF", \
  89. "BSP_INDICATE_USER_STATE_OFF", \
  90. "BSP_INDICATE_USER_STATE_0", \
  91. "BSP_INDICATE_USER_STATE_1", \
  92. "BSP_INDICATE_USER_STATE_2", \
  93. "BSP_INDICATE_USER_STATE_3", \
  94. "BSP_INDICATE_USER_STATE_ON" \
  95. } /**< See @ref examples_bsp_states for a list of how these states are indicated for the PCA10028/PCA10040 board and the PCA10031 dongle.*/
  96. /**@brief BSP indication states.
  97. *
  98. * @details See @ref examples_bsp_states for a list of how these states are indicated for the PCA10028/PCA10040 board and the PCA10031 dongle.
  99. */
  100. typedef enum
  101. {
  102. BSP_INDICATE_FIRST = 0,
  103. BSP_INDICATE_IDLE = BSP_INDICATE_FIRST, /**< See \ref BSP_INDICATE_IDLE.*/
  104. BSP_INDICATE_SCANNING, /**< See \ref BSP_INDICATE_SCANNING.*/
  105. BSP_INDICATE_ADVERTISING, /**< See \ref BSP_INDICATE_ADVERTISING.*/
  106. BSP_INDICATE_ADVERTISING_WHITELIST, /**< See \ref BSP_INDICATE_ADVERTISING_WHITELIST.*/
  107. BSP_INDICATE_ADVERTISING_SLOW, /**< See \ref BSP_INDICATE_ADVERTISING_SLOW.*/
  108. BSP_INDICATE_ADVERTISING_DIRECTED, /**< See \ref BSP_INDICATE_ADVERTISING_DIRECTED.*/
  109. BSP_INDICATE_BONDING, /**< See \ref BSP_INDICATE_BONDING.*/
  110. BSP_INDICATE_CONNECTED, /**< See \ref BSP_INDICATE_CONNECTED.*/
  111. BSP_INDICATE_SENT_OK, /**< See \ref BSP_INDICATE_SENT_OK.*/
  112. BSP_INDICATE_SEND_ERROR, /**< See \ref BSP_INDICATE_SEND_ERROR.*/
  113. BSP_INDICATE_RCV_OK, /**< See \ref BSP_INDICATE_RCV_OK.*/
  114. BSP_INDICATE_RCV_ERROR, /**< See \ref BSP_INDICATE_RCV_ERROR.*/
  115. BSP_INDICATE_FATAL_ERROR, /**< See \ref BSP_INDICATE_FATAL_ERROR.*/
  116. BSP_INDICATE_ALERT_0, /**< See \ref BSP_INDICATE_ALERT_0.*/
  117. BSP_INDICATE_ALERT_1, /**< See \ref BSP_INDICATE_ALERT_1.*/
  118. BSP_INDICATE_ALERT_2, /**< See \ref BSP_INDICATE_ALERT_2.*/
  119. BSP_INDICATE_ALERT_3, /**< See \ref BSP_INDICATE_ALERT_3.*/
  120. BSP_INDICATE_ALERT_OFF, /**< See \ref BSP_INDICATE_ALERT_OFF.*/
  121. BSP_INDICATE_USER_STATE_OFF, /**< See \ref BSP_INDICATE_USER_STATE_OFF.*/
  122. BSP_INDICATE_USER_STATE_0, /**< See \ref BSP_INDICATE_USER_STATE_0.*/
  123. BSP_INDICATE_USER_STATE_1, /**< See \ref BSP_INDICATE_USER_STATE_1.*/
  124. BSP_INDICATE_USER_STATE_2, /**< See \ref BSP_INDICATE_USER_STATE_2.*/
  125. BSP_INDICATE_USER_STATE_3, /**< See \ref BSP_INDICATE_USER_STATE_3.*/
  126. BSP_INDICATE_USER_STATE_ON, /**< See \ref BSP_INDICATE_USER_STATE_ON.*/
  127. BSP_INDICATE_LAST = BSP_INDICATE_USER_STATE_ON
  128. } bsp_indication_t;
  129. /**@brief BSP events.
  130. *
  131. * @note Events from BSP_EVENT_KEY_0 to BSP_EVENT_KEY_LAST are by default assigned to buttons.
  132. */
  133. typedef enum
  134. {
  135. BSP_EVENT_NOTHING = 0, /**< Assign this event to an action to prevent the action from generating an event (disable the action). */
  136. BSP_EVENT_DEFAULT, /**< Assign this event to an action to assign the default event to the action. */
  137. BSP_EVENT_CLEAR_BONDING_DATA, /**< Persistent bonding data should be erased. */
  138. BSP_EVENT_CLEAR_ALERT, /**< An alert should be cleared. */
  139. BSP_EVENT_DISCONNECT, /**< A link should be disconnected. */
  140. BSP_EVENT_ADVERTISING_START, /**< The device should start advertising. */
  141. BSP_EVENT_ADVERTISING_STOP, /**< The device should stop advertising. */
  142. BSP_EVENT_WHITELIST_OFF, /**< The device should remove its advertising whitelist. */
  143. BSP_EVENT_BOND, /**< The device should bond to the currently connected peer. */
  144. BSP_EVENT_RESET, /**< The device should reset. */
  145. BSP_EVENT_SLEEP, /**< The device should enter sleep mode. */
  146. BSP_EVENT_WAKEUP, /**< The device should wake up from sleep mode. */
  147. BSP_EVENT_SYSOFF, /**< The device should enter system off mode (without wakeup). */
  148. BSP_EVENT_DFU, /**< The device should enter DFU mode. */
  149. BSP_EVENT_KEY_0, /**< Default event of the push action of BSP_BUTTON_0 (only if this button is present). */
  150. BSP_EVENT_KEY_1, /**< Default event of the push action of BSP_BUTTON_1 (only if this button is present). */
  151. BSP_EVENT_KEY_2, /**< Default event of the push action of BSP_BUTTON_2 (only if this button is present). */
  152. BSP_EVENT_KEY_3, /**< Default event of the push action of BSP_BUTTON_3 (only if this button is present). */
  153. BSP_EVENT_KEY_4, /**< Default event of the push action of BSP_BUTTON_4 (only if this button is present). */
  154. BSP_EVENT_KEY_5, /**< Default event of the push action of BSP_BUTTON_5 (only if this button is present). */
  155. BSP_EVENT_KEY_6, /**< Default event of the push action of BSP_BUTTON_6 (only if this button is present). */
  156. BSP_EVENT_KEY_7, /**< Default event of the push action of BSP_BUTTON_7 (only if this button is present). */
  157. BSP_EVENT_KEY_LAST = BSP_EVENT_KEY_7,
  158. } bsp_event_t;
  159. typedef struct
  160. {
  161. bsp_event_t push_event; /**< The event to fire on regular button press. */
  162. bsp_event_t long_push_event; /**< The event to fire on long button press. */
  163. bsp_event_t release_event; /**< The event to fire on button release. */
  164. } bsp_button_event_cfg_t;
  165. /**@brief BSP module event callback function type.
  166. *
  167. * @details Upon an event in the BSP module, this callback function will be called to notify
  168. * the application about the event.
  169. *
  170. * @param[in] bsp_event_t BSP event type.
  171. */
  172. typedef void (* bsp_event_callback_t)(bsp_event_t);
  173. /**@brief Function for initializing BSP.
  174. *
  175. * @details The function initializes the board support package to allow state indication and
  176. * button reaction. Default events are assigned to buttons.
  177. * @note Before calling this function, you must initiate the following required modules:
  178. * - @ref app_timer for LED support
  179. * - @ref app_gpiote for button support
  180. *
  181. * @param[in] type Type of peripherals used.
  182. * @param[in] callback Function to be called when button press/event is detected.
  183. *
  184. * @retval NRF_SUCCESS If the BSP module was successfully initialized.
  185. * @retval NRF_ERROR_INVALID_STATE If the application timer module has not been initialized.
  186. * @retval NRF_ERROR_NO_MEM If the maximum number of timers has already been reached.
  187. * @retval NRF_ERROR_INVALID_PARAM If GPIOTE has too many users.
  188. * @retval NRF_ERROR_INVALID_STATE If button or GPIOTE has not been initialized.
  189. */
  190. uint32_t bsp_init(uint32_t type, bsp_event_callback_t callback);
  191. /**@brief Function for checking button states.
  192. *
  193. * @details This function checks if the button is pressed. If the button ID is out of range,
  194. * the function returns false.
  195. *
  196. * @param[in] button Button ID to check.
  197. *
  198. * @retval true If the button is pressed.
  199. * @retval false If the button is not pressed.
  200. */
  201. bool bsp_button_is_pressed(uint32_t button);
  202. /**@brief Function for assigning a specific event to a button.
  203. *
  204. * @details This function allows redefinition of standard events assigned to buttons.
  205. * To unassign events, provide the event @ref BSP_EVENT_NOTHING.
  206. *
  207. * @param[in] button Button ID to be redefined.
  208. * @param[in] action Button action to assign event to.
  209. * @param[in] event Event to be assigned to button.
  210. *
  211. * @retval NRF_SUCCESS If the event was successfully assigned to button.
  212. * @retval NRF_ERROR_INVALID_PARAM If the button ID or button action was invalid.
  213. */
  214. uint32_t bsp_event_to_button_action_assign(uint32_t button, bsp_button_action_t action, bsp_event_t event);
  215. /**@brief Function for configuring indicators to required state.
  216. *
  217. * @details This function indicates the required state by means of LEDs (if enabled).
  218. *
  219. * @note Alerts are indicated independently.
  220. *
  221. * @param[in] indicate State to be indicated.
  222. *
  223. * @retval NRF_SUCCESS If the state was successfully indicated.
  224. * @retval NRF_ERROR_NO_MEM If the internal timer operations queue was full.
  225. * @retval NRF_ERROR_INVALID_STATE If the application timer module has not been initialized,
  226. * or internal timer has not been created.
  227. */
  228. uint32_t bsp_indication_set(bsp_indication_t indicate);
  229. /**@brief Function for enabling all buttons.
  230. *
  231. * @details After calling this function, all buttons will generate events when pressed, and
  232. * all buttons will be able to wake the system up from sleep mode.
  233. *
  234. * @retval NRF_SUCCESS If the buttons were successfully enabled.
  235. * @retval NRF_ERROR_NOT_SUPPORTED If the board has no buttons or BSP_SIMPLE is defined.
  236. * @return A propagated error.
  237. */
  238. uint32_t bsp_buttons_enable(void);
  239. /**@brief Function for disabling all buttons.
  240. *
  241. * @details After calling this function, no buttons will generate events when pressed, and
  242. * no buttons will be able to wake the system up from sleep mode.
  243. *
  244. * @retval NRF_SUCCESS If the buttons were successfully disabled.
  245. * @retval NRF_ERROR_NOT_SUPPORTED If the board has no buttons or BSP_SIMPLE is defined.
  246. * @return A propagated error.
  247. */
  248. uint32_t bsp_buttons_disable(void);
  249. /**@brief Function for enabling wakeup from SYSTEM OFF for given button.
  250. *
  251. * @details After calling this function, button can be used to wake up the chip.
  252. * This function should only be called immediately before going into sleep.
  253. *
  254. * @param[in] button_idx Index of the button.
  255. *
  256. * @retval NRF_SUCCESS If the button was successfully enabled.
  257. * @retval NRF_ERROR_NOT_SUPPORTED If the board has no buttons or BSP_SIMPLE is defined.
  258. */
  259. uint32_t bsp_wakeup_button_enable(uint32_t button_idx);
  260. /**@brief Function for disabling wakeup for the given button.
  261. *
  262. * @param[in] button_idx index of the button.
  263. *
  264. * @retval NRF_SUCCESS If the button was successfully disabled.
  265. * @retval NRF_ERROR_NOT_SUPPORTED If the board has no buttons or BSP_SIMPLE is defined.
  266. */
  267. uint32_t bsp_wakeup_button_disable(uint32_t button_idx);
  268. #ifdef __cplusplus
  269. }
  270. #endif
  271. #endif // BSP_H__
  272. /** @} */