bsp.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 bsp Board Support Package
  15. * @{
  16. * @ingroup app_common
  17. *
  18. * @brief BSP module.
  19. * @details This module provides a layer of abstraction from the board.
  20. * It allows the user to indicate certain states on LEDs in a simple way.
  21. * Module functionality can be modified by additional defines:
  22. * - BSP_SIMPLE reduces functionality of this module to enable
  23. * and read state of the buttons
  24. * - BSP_UART_SUPPORT enables support for UART
  25. */
  26. #ifndef BSP_H__
  27. #define BSP_H__
  28. #include <stdint.h>
  29. #include <stdbool.h>
  30. #include "boards.h"
  31. #if !defined(BSP_DEFINES_ONLY) && !defined(BSP_SIMPLE)
  32. #include "app_button.h"
  33. #define BSP_BUTTON_ACTION_PUSH (APP_BUTTON_PUSH) /**< Represents pushing a button. See @ref bsp_button_action_t. */
  34. #define BSP_BUTTON_ACTION_RELEASE (APP_BUTTON_RELEASE) /**< Represents releasing a button. See @ref bsp_button_action_t. */
  35. #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. */
  36. #endif
  37. /* BSP_UART_SUPPORT
  38. * This define enables UART support module.
  39. */
  40. #ifdef BSP_UART_SUPPORT
  41. #include "app_uart.h"
  42. #endif // BSP_UART_SUPPORT
  43. #define BUTTON_ERASE_BONDING BSP_BUTTON_0_MASK
  44. #define BUTTON_ERASE_ALL BSP_BUTTON_1_MASK
  45. #define BUTTON_ADVERTISE BSP_BUTTON_0_MASK
  46. #define BUTTON_CLEAR_EVT BSP_BUTTON_1_MASK
  47. #define BUTTON_CAPSLOCK BSP_BUTTON_2_MASK
  48. #define BSP_BUTTONS_ALL 0xFFFFFFFF
  49. #define BSP_BUTTONS_NONE 0
  50. /**@brief Types of BSP initialization.
  51. */
  52. #define BSP_INIT_NONE 0 /**< This define specifies the type of initialization without support for LEDs and buttons (@ref bsp_init).*/
  53. #define BSP_INIT_LED (1 << 0) /**< This bit enables LEDs during initialization (@ref bsp_init).*/
  54. #define BSP_INIT_BUTTONS (1 << 1) /**< This bit enables buttons during initialization (@ref bsp_init).*/
  55. #define BSP_LONG_PUSH_TIMEOUT_MS (1000) /**< The time to hold for a long push (in milliseconds). */
  56. typedef uint8_t bsp_button_action_t; /**< The different actions that can be performed on a button. */
  57. #define BSP_INDICATIONS_LIST { \
  58. "BSP_INDICATE_IDLE\n\r", \
  59. "BSP_INDICATE_SCANNING\n\r", \
  60. "BSP_INDICATE_ADVERTISING\n\r", \
  61. "BSP_INDICATE_ADVERTISING_WHITELIST\n\r", \
  62. "BSP_INDICATE_ADVERTISING_SLOW\n\r", \
  63. "BSP_INDICATE_ADVERTISING_DIRECTED\n\r", \
  64. "BSP_INDICATE_BONDING\n\r", \
  65. "BSP_INDICATE_CONNECTED\n\r", \
  66. "BSP_INDICATE_SENT_OK\n\r", \
  67. "BSP_INDICATE_SEND_ERROR\n\r", \
  68. "BSP_INDICATE_RCV_OK\n\r", \
  69. "BSP_INDICATE_RCV_ERROR\n\r", \
  70. "BSP_INDICATE_FATAL_ERROR\n\r", \
  71. "BSP_INDICATE_ALERT_0\n\r", \
  72. "BSP_INDICATE_ALERT_1\n\r", \
  73. "BSP_INDICATE_ALERT_2\n\r", \
  74. "BSP_INDICATE_ALERT_3\n\r", \
  75. "BSP_INDICATE_ALERT_OFF\n\r", \
  76. "BSP_INDICATE_USER_STATE_OFF\n\r", \
  77. "BSP_INDICATE_USER_STATE_0\n\r", \
  78. "BSP_INDICATE_USER_STATE_1\n\r", \
  79. "BSP_INDICATE_USER_STATE_2\n\r", \
  80. "BSP_INDICATE_USER_STATE_3\n\r", \
  81. "BSP_INDICATE_USER_STATE_ON\n\r" \
  82. } /**< See @ref examples_bsp_states for a list of how these states are indicated for the PCA10028/PCA10040 board and the PCA10031 dongle.*/
  83. /**@brief BSP indication states.
  84. *
  85. * @details See @ref examples_bsp_states for a list of how these states are indicated for the PCA10028/PCA10040 board and the PCA10031 dongle.
  86. */
  87. typedef enum
  88. {
  89. BSP_INDICATE_FIRST = 0,
  90. BSP_INDICATE_IDLE = BSP_INDICATE_FIRST, /**< See \ref BSP_INDICATE_IDLE.*/
  91. BSP_INDICATE_SCANNING, /**< See \ref BSP_INDICATE_SCANNING.*/
  92. BSP_INDICATE_ADVERTISING, /**< See \ref BSP_INDICATE_ADVERTISING.*/
  93. BSP_INDICATE_ADVERTISING_WHITELIST, /**< See \ref BSP_INDICATE_ADVERTISING_WHITELIST.*/
  94. BSP_INDICATE_ADVERTISING_SLOW, /**< See \ref BSP_INDICATE_ADVERTISING_SLOW.*/
  95. BSP_INDICATE_ADVERTISING_DIRECTED, /**< See \ref BSP_INDICATE_ADVERTISING_DIRECTED.*/
  96. BSP_INDICATE_BONDING, /**< See \ref BSP_INDICATE_BONDING.*/
  97. BSP_INDICATE_CONNECTED, /**< See \ref BSP_INDICATE_CONNECTED.*/
  98. BSP_INDICATE_SENT_OK, /**< See \ref BSP_INDICATE_SENT_OK.*/
  99. BSP_INDICATE_SEND_ERROR, /**< See \ref BSP_INDICATE_SEND_ERROR.*/
  100. BSP_INDICATE_RCV_OK, /**< See \ref BSP_INDICATE_RCV_OK.*/
  101. BSP_INDICATE_RCV_ERROR, /**< See \ref BSP_INDICATE_RCV_ERROR.*/
  102. BSP_INDICATE_FATAL_ERROR, /**< See \ref BSP_INDICATE_FATAL_ERROR.*/
  103. BSP_INDICATE_ALERT_0, /**< See \ref BSP_INDICATE_ALERT_0.*/
  104. BSP_INDICATE_ALERT_1, /**< See \ref BSP_INDICATE_ALERT_1.*/
  105. BSP_INDICATE_ALERT_2, /**< See \ref BSP_INDICATE_ALERT_2.*/
  106. BSP_INDICATE_ALERT_3, /**< See \ref BSP_INDICATE_ALERT_3.*/
  107. BSP_INDICATE_ALERT_OFF, /**< See \ref BSP_INDICATE_ALERT_OFF.*/
  108. BSP_INDICATE_USER_STATE_OFF, /**< See \ref BSP_INDICATE_USER_STATE_OFF.*/
  109. BSP_INDICATE_USER_STATE_0, /**< See \ref BSP_INDICATE_USER_STATE_0.*/
  110. BSP_INDICATE_USER_STATE_1, /**< See \ref BSP_INDICATE_USER_STATE_1.*/
  111. BSP_INDICATE_USER_STATE_2, /**< See \ref BSP_INDICATE_USER_STATE_2.*/
  112. BSP_INDICATE_USER_STATE_3, /**< See \ref BSP_INDICATE_USER_STATE_3.*/
  113. BSP_INDICATE_USER_STATE_ON, /**< See \ref BSP_INDICATE_USER_STATE_ON.*/
  114. BSP_INDICATE_LAST = BSP_INDICATE_USER_STATE_ON
  115. } bsp_indication_t;
  116. /**@brief BSP events.
  117. *
  118. * @note Events from BSP_EVENT_KEY_0 to BSP_EVENT_KEY_LAST are by default assigned to buttons.
  119. */
  120. typedef enum
  121. {
  122. BSP_EVENT_NOTHING = 0, /**< Assign this event to an action to prevent the action from generating an event (disable the action). */
  123. BSP_EVENT_DEFAULT, /**< Assign this event to an action to assign the default event to the action. */
  124. BSP_EVENT_CLEAR_BONDING_DATA, /**< Persistent bonding data should be erased. */
  125. BSP_EVENT_CLEAR_ALERT, /**< An alert should be cleared. */
  126. BSP_EVENT_DISCONNECT, /**< A link should be disconnected. */
  127. BSP_EVENT_ADVERTISING_START, /**< The device should start advertising. */
  128. BSP_EVENT_ADVERTISING_STOP, /**< The device should stop advertising. */
  129. BSP_EVENT_WHITELIST_OFF, /**< The device should remove its advertising whitelist. */
  130. BSP_EVENT_BOND, /**< The device should bond to the currently connected peer. */
  131. BSP_EVENT_RESET, /**< The device should reset. */
  132. BSP_EVENT_SLEEP, /**< The device should enter sleep mode. */
  133. BSP_EVENT_WAKEUP, /**< The device should wake up from sleep mode. */
  134. BSP_EVENT_DFU, /**< The device should enter DFU mode. */
  135. BSP_EVENT_KEY_0, /**< Default event of the push action of BSP_BUTTON_0 (only if this button is present). */
  136. BSP_EVENT_KEY_1, /**< Default event of the push action of BSP_BUTTON_1 (only if this button is present). */
  137. BSP_EVENT_KEY_2, /**< Default event of the push action of BSP_BUTTON_2 (only if this button is present). */
  138. BSP_EVENT_KEY_3, /**< Default event of the push action of BSP_BUTTON_3 (only if this button is present). */
  139. BSP_EVENT_KEY_4, /**< Default event of the push action of BSP_BUTTON_4 (only if this button is present). */
  140. BSP_EVENT_KEY_5, /**< Default event of the push action of BSP_BUTTON_5 (only if this button is present). */
  141. BSP_EVENT_KEY_6, /**< Default event of the push action of BSP_BUTTON_6 (only if this button is present). */
  142. BSP_EVENT_KEY_7, /**< Default event of the push action of BSP_BUTTON_7 (only if this button is present). */
  143. BSP_EVENT_KEY_LAST = BSP_EVENT_KEY_7,
  144. } bsp_event_t;
  145. typedef struct
  146. {
  147. bsp_event_t push_event; /**< The event to fire on regular button press. */
  148. bsp_event_t long_push_event; /**< The event to fire on long button press. */
  149. bsp_event_t release_event; /**< The event to fire on button release. */
  150. } bsp_button_event_cfg_t;
  151. /**@brief BSP module event callback function type.
  152. *
  153. * @details Upon an event in the BSP module, this callback function will be called to notify
  154. * the application about the event.
  155. *
  156. * @param[in] bsp_event_t BSP event type.
  157. */
  158. typedef void (* bsp_event_callback_t)(bsp_event_t);
  159. /**@brief Function for initializing BSP.
  160. *
  161. * @details The function initializes the board support package to allow state indication and
  162. * button reaction. Default events are assigned to buttons.
  163. * @note Before calling this function, you must initiate the following required modules:
  164. * - @ref app_timer for LED support
  165. * - @ref app_gpiote for button support
  166. * - @ref app_uart for UART support
  167. *
  168. * @param[in] type Type of peripherals used.
  169. * @param[in] ticks_per_100ms Number of RTC ticks for 100 ms.
  170. * @param[in] callback Function to be called when button press/event is detected.
  171. *
  172. * @retval NRF_SUCCESS If the BSP module was successfully initialized.
  173. * @retval NRF_ERROR_INVALID_STATE If the application timer module has not been initialized.
  174. * @retval NRF_ERROR_NO_MEM If the maximum number of timers has already been reached.
  175. * @retval NRF_ERROR_INVALID_PARAM If GPIOTE has too many users.
  176. * @retval NRF_ERROR_INVALID_STATE If button or GPIOTE has not been initialized.
  177. */
  178. uint32_t bsp_init(uint32_t type, uint32_t ticks_per_100ms, bsp_event_callback_t callback);
  179. /**@brief Function for getting buttons states.
  180. *
  181. * @details This function allows to get the state of all buttons.
  182. *
  183. * @param[in] p_buttons_state This variable will store buttons state. Button 0 state is
  184. * represented by bit 0 (1=pressed), Button 1 state by bit 1,
  185. * and so on.
  186. *
  187. * @retval NRF_SUCCESS If buttons state was successfully read.
  188. */
  189. uint32_t bsp_buttons_state_get(uint32_t * p_buttons_state);
  190. /**@brief Function for checking buttons states.
  191. *
  192. * @details This function checks if the button is pressed. If the button ID iss out of range,
  193. * the function returns false.
  194. *
  195. * @param[in] button Button ID to check.
  196. * @param[in] p_state This variable will store the information whether the
  197. * specified button is pressed (true) or not.
  198. *
  199. * @retval NRF_SUCCESS If the button state was successfully read.
  200. */
  201. uint32_t bsp_button_is_pressed(uint32_t button, bool * p_state);
  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 configuring indicators to required state.
  230. *
  231. * @details This function indicates the required state by means of LEDs (if enabled)
  232. * and UART (if enabled).
  233. *
  234. * @note Alerts are indicated independently.
  235. *
  236. * @param[in] indicate State to be indicated.
  237. * @param[in] p_text Text to be output on UART.
  238. *
  239. * @retval NRF_SUCCESS If the state was successfully indicated.
  240. * @retval NRF_ERROR_NO_MEM If the internal timer operations queue was full.
  241. * @retval NRF_ERROR_INVALID_STATE If the application timer module has not been initialized,
  242. * or timer has not been created.
  243. */
  244. uint32_t bsp_indication_text_set(bsp_indication_t indicate, const char * p_text);
  245. /**@brief Function for enabling all buttons.
  246. *
  247. * @details After calling this function, all buttons will generate events when pressed, and
  248. * all buttons will be able to wake the system up from sleep mode.
  249. *
  250. * @retval NRF_SUCCESS If the buttons were successfully enabled.
  251. * @retval NRF_ERROR_NOT_SUPPORTED If the board has no buttons or BSP_SIMPLE is defined.
  252. * @return A propagated error.
  253. */
  254. uint32_t bsp_buttons_enable(void);
  255. /**@brief Function for disabling all buttons.
  256. *
  257. * @details After calling this function, no buttons will generate events when pressed, and
  258. * no buttons will be able to wake the system up from sleep mode.
  259. *
  260. * @retval NRF_SUCCESS If the buttons were successfully disabled.
  261. * @retval NRF_ERROR_NOT_SUPPORTED If the board has no buttons or BSP_SIMPLE is defined.
  262. * @return A propagated error.
  263. */
  264. uint32_t bsp_buttons_disable(void);
  265. /**@brief Function for configuring wakeup buttons before going into sleep mode.
  266. *
  267. * @details After calling this function, only the buttons that are set to 1 in wakeup_buttons
  268. * can be used to wake up the chip. If this function is not called before going to,
  269. * sleep either all or no buttons can wake up the chip.
  270. *
  271. * This function should only be called immediately before going into sleep.
  272. *
  273. * @param[in] wakeup_buttons Mask describing which buttons should be able to wake up the chip.
  274. *
  275. * @retval NRF_SUCCESS If the buttons were successfully enabled.
  276. * @retval NRF_ERROR_NOT_SUPPORTED If the board has no buttons or BSP_SIMPLE is defined.
  277. */
  278. uint32_t bsp_wakeup_buttons_set(uint32_t wakeup_buttons);
  279. #endif // BSP_H__
  280. /** @} */