boards.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /**
  2. * Copyright (c) 2014 - 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. #ifndef BOARDS_H
  41. #define BOARDS_H
  42. #include "nrf_gpio.h"
  43. #include "nordic_common.h"
  44. #if defined(BOARD_NRF6310)
  45. #include "nrf6310.h"
  46. #elif defined(BOARD_PCA10000)
  47. #include "pca10000.h"
  48. #elif defined(BOARD_PCA10001)
  49. #include "pca10001.h"
  50. #elif defined(BOARD_PCA10002)
  51. #include "pca10000.h"
  52. #elif defined(BOARD_PCA10003)
  53. #include "pca10003.h"
  54. #elif defined(BOARD_PCA20006)
  55. #include "pca20006.h"
  56. #elif defined(BOARD_PCA10028)
  57. #include "pca10028.h"
  58. #elif defined(BOARD_PCA10031)
  59. #include "pca10031.h"
  60. #elif defined(BOARD_PCA10036)
  61. #include "pca10036.h"
  62. #elif defined(BOARD_PCA10040)
  63. #include "pca10040.h"
  64. #elif defined(BOARD_PCA10056)
  65. #include "pca10056.h"
  66. #elif defined(BOARD_PCA10100)
  67. #include "pca10100.h"
  68. #elif defined(BOARD_PCA10112)
  69. #include "pca10112.h"
  70. #elif defined(BOARD_PCA20020)
  71. #include "pca20020.h"
  72. #elif defined(BOARD_PCA10059)
  73. #include "pca10059.h"
  74. #elif defined(BOARD_WT51822)
  75. #include "wt51822.h"
  76. #elif defined(BOARD_N5DK1)
  77. #include "n5_starterkit.h"
  78. #elif defined (BOARD_D52DK1)
  79. #include "d52_starterkit.h"
  80. #elif defined (BOARD_ARDUINO_PRIMO)
  81. #include "arduino_primo.h"
  82. #elif defined (CUSTOM_BOARD_INC)
  83. #include STRINGIFY(CUSTOM_BOARD_INC.h)
  84. #elif defined(BOARD_CUSTOM)
  85. #include "custom_board.h"
  86. #else
  87. #error "Board is not defined"
  88. #endif
  89. #if defined (SHIELD_BSP_INC)
  90. #include STRINGIFY(SHIELD_BSP_INC.h)
  91. #endif
  92. #ifdef __cplusplus
  93. extern "C" {
  94. #endif
  95. /**@defgroup BSP_BOARD_INIT_FLAGS Board initialization flags.
  96. * @{ */
  97. #define BSP_INIT_NONE 0 /**< No initialization of LEDs or buttons (@ref bsp_board_init).*/
  98. #define BSP_INIT_LEDS (1 << 0) /**< Enable LEDs during initialization (@ref bsp_board_init).*/
  99. #define BSP_INIT_BUTTONS (1 << 1) /**< Enable buttons during initialization (@ref bsp_board_init).*/
  100. /**@} */
  101. /**
  102. * Function for returning the state of an LED.
  103. *
  104. * @param led_idx LED index (starting from 0), as defined in the board-specific header.
  105. *
  106. * @return True if the LED is turned on.
  107. */
  108. bool bsp_board_led_state_get(uint32_t led_idx);
  109. /**
  110. * Function for turning on an LED.
  111. *
  112. * @param led_idx LED index (starting from 0), as defined in the board-specific header.
  113. */
  114. void bsp_board_led_on(uint32_t led_idx);
  115. /**
  116. * Function for turning off an LED.
  117. *
  118. * @param led_idx LED index (starting from 0), as defined in the board-specific header.
  119. */
  120. void bsp_board_led_off(uint32_t led_idx);
  121. /**
  122. * Function for inverting the state of an LED.
  123. *
  124. * @param led_idx LED index (starting from 0), as defined in the board-specific header.
  125. */
  126. void bsp_board_led_invert(uint32_t led_idx);
  127. /**
  128. * Function for turning off all LEDs.
  129. */
  130. void bsp_board_leds_off(void);
  131. /**
  132. * Function for turning on all LEDs.
  133. */
  134. void bsp_board_leds_on(void);
  135. /**
  136. * Function for initializing the BSP handling for the board.
  137. *
  138. * @note This also initializes the USB DFU trigger library if @ref BOARDS_WITH_USB_DFU_TRIGGER is 1.
  139. *
  140. * @param[in] init_flags Flags specifying what to initialize (LEDs/buttons).
  141. * See @ref BSP_BOARD_INIT_FLAGS.
  142. */
  143. void bsp_board_init(uint32_t init_flags);
  144. /**
  145. * Function for converting pin number to LED index.
  146. *
  147. * @param pin_number Pin number.
  148. *
  149. * @return LED index of the given pin or 0xFFFFFFFF if invalid pin provided.
  150. */
  151. uint32_t bsp_board_pin_to_led_idx(uint32_t pin_number);
  152. /**
  153. * Function for converting LED index to pin number.
  154. *
  155. * @param led_idx LED index.
  156. *
  157. * @return Pin number.
  158. */
  159. uint32_t bsp_board_led_idx_to_pin(uint32_t led_idx);
  160. /**
  161. * Function for returning the state of a button.
  162. *
  163. * @param button_idx Button index (starting from 0), as defined in the board-specific header.
  164. *
  165. * @return True if the button is pressed.
  166. */
  167. bool bsp_board_button_state_get(uint32_t button_idx);
  168. /**
  169. * Function for converting pin number to button index.
  170. *
  171. * @param pin_number Pin number.
  172. *
  173. * @return Button index of the given pin or 0xFFFFFFFF if invalid pin provided.
  174. */
  175. uint32_t bsp_board_pin_to_button_idx(uint32_t pin_number);
  176. /**
  177. * Function for converting button index to pin number.
  178. *
  179. * @param button_idx Button index.
  180. *
  181. * @return Pin number.
  182. */
  183. uint32_t bsp_board_button_idx_to_pin(uint32_t button_idx);
  184. #define BSP_BOARD_LED_0 17
  185. #define BSP_BOARD_LED_1 18
  186. #define BSP_BOARD_LED_2 19
  187. #define BSP_BOARD_LED_3 20
  188. #define BSP_BOARD_LED_4 4
  189. #define BSP_BOARD_LED_5 5
  190. #define BSP_BOARD_LED_6 6
  191. #define BSP_BOARD_LED_7 7
  192. #define PIN_MASK(_pin) /*lint -save -e504 */ \
  193. (1u << (uint32_t)((_pin) & (~P0_PIN_NUM))) \
  194. /*lint -restore */
  195. #define PIN_PORT(_pin) (((_pin) >= P0_PIN_NUM) ? NRF_P1 : NRF_GPIO)
  196. #ifdef BSP_LED_0
  197. #define BSP_LED_0_MASK PIN_MASK(BSP_LED_0)
  198. #define BSP_LED_0_PORT PIN_PORT(BSP_LED_0)
  199. #else
  200. #define BSP_LED_0_MASK 0
  201. #define BSP_LED_0_PORT 0
  202. #endif
  203. #ifdef BSP_LED_1
  204. #define BSP_LED_1_MASK PIN_MASK(BSP_LED_1)
  205. #define BSP_LED_1_PORT PIN_PORT(BSP_LED_1)
  206. #else
  207. #define BSP_LED_1_MASK 0
  208. #define BSP_LED_1_PORT 0
  209. #endif
  210. #ifdef BSP_LED_2
  211. #define BSP_LED_2_MASK PIN_MASK(BSP_LED_2)
  212. #define BSP_LED_2_PORT PIN_PORT(BSP_LED_2)
  213. #else
  214. #define BSP_LED_2_MASK 0
  215. #define BSP_LED_2_PORT 0
  216. #endif
  217. #ifdef BSP_LED_3
  218. #define BSP_LED_3_MASK PIN_MASK(BSP_LED_3)
  219. #define BSP_LED_3_PORT PIN_PORT(BSP_LED_3)
  220. #else
  221. #define BSP_LED_3_MASK 0
  222. #define BSP_LED_3_PORT 0
  223. #endif
  224. #ifdef BSP_LED_4
  225. #define BSP_LED_4_MASK PIN_MASK(BSP_LED_4)
  226. #define BSP_LED_4_PORT PIN_PORT(BSP_LED_4)
  227. #else
  228. #define BSP_LED_4_MASK 0
  229. #define BSP_LED_4_PORT 0
  230. #endif
  231. #ifdef BSP_LED_5
  232. #define BSP_LED_5_MASK PIN_MASK(BSP_LED_5)
  233. #define BSP_LED_5_PORT PIN_PORT(BSP_LED_5)
  234. #else
  235. #define BSP_LED_5_MASK 0
  236. #define BSP_LED_5_PORT 0
  237. #endif
  238. #ifdef BSP_LED_6
  239. #define BSP_LED_6_MASK PIN_MASK(BSP_LED_6)
  240. #define BSP_LED_6_PORT PIN_PORT(BSP_LED_6)
  241. #else
  242. #define BSP_LED_6_MASK 0
  243. #define BSP_LED_6_PORT 0
  244. #endif
  245. #ifdef BSP_LED_7
  246. #define BSP_LED_7_MASK PIN_MASK(BSP_LED_7)
  247. #define BSP_LED_7_PORT PIN_PORT(BSP_LED_7)
  248. #else
  249. #define BSP_LED_7_MASK 0
  250. #define BSP_LED_7_PORT 0
  251. #endif
  252. #define LEDS_MASK (BSP_LED_0_MASK | BSP_LED_1_MASK | \
  253. BSP_LED_2_MASK | BSP_LED_3_MASK | \
  254. BSP_LED_4_MASK | BSP_LED_5_MASK | \
  255. BSP_LED_6_MASK | BSP_LED_7_MASK)
  256. #define BSP_BOARD_BUTTON_0 0
  257. #define BSP_BOARD_BUTTON_1 1
  258. #define BSP_BOARD_BUTTON_2 2
  259. #define BSP_BOARD_BUTTON_3 3
  260. #define BSP_BOARD_BUTTON_4 4
  261. #define BSP_BOARD_BUTTON_5 5
  262. #define BSP_BOARD_BUTTON_6 6
  263. #define BSP_BOARD_BUTTON_7 7
  264. #ifdef BSP_BUTTON_0
  265. #define BSP_BUTTON_0_MASK (1<<BSP_BUTTON_0)
  266. #else
  267. #define BSP_BUTTON_0_MASK 0
  268. #endif
  269. #ifdef BSP_BUTTON_1
  270. #define BSP_BUTTON_1_MASK (1<<BSP_BUTTON_1)
  271. #else
  272. #define BSP_BUTTON_1_MASK 0
  273. #endif
  274. #ifdef BSP_BUTTON_2
  275. #define BSP_BUTTON_2_MASK (1<<BSP_BUTTON_2)
  276. #else
  277. #define BSP_BUTTON_2_MASK 0
  278. #endif
  279. #ifdef BSP_BUTTON_3
  280. #define BSP_BUTTON_3_MASK (1<<BSP_BUTTON_3)
  281. #else
  282. #define BSP_BUTTON_3_MASK 0
  283. #endif
  284. #ifdef BSP_BUTTON_4
  285. #define BSP_BUTTON_4_MASK (1<<BSP_BUTTON_4)
  286. #else
  287. #define BSP_BUTTON_4_MASK 0
  288. #endif
  289. #ifdef BSP_BUTTON_5
  290. #define BSP_BUTTON_5_MASK (1<<BSP_BUTTON_5)
  291. #else
  292. #define BSP_BUTTON_5_MASK 0
  293. #endif
  294. #ifdef BSP_BUTTON_6
  295. #define BSP_BUTTON_6_MASK (1<<BSP_BUTTON_6)
  296. #else
  297. #define BSP_BUTTON_6_MASK 0
  298. #endif
  299. #ifdef BSP_BUTTON_7
  300. #define BSP_BUTTON_7_MASK (1<<BSP_BUTTON_7)
  301. #else
  302. #define BSP_BUTTON_7_MASK 0
  303. #endif
  304. #define BUTTONS_MASK (BSP_BUTTON_0_MASK | BSP_BUTTON_1_MASK | \
  305. BSP_BUTTON_2_MASK | BSP_BUTTON_3_MASK | \
  306. BSP_BUTTON_4_MASK | BSP_BUTTON_5_MASK | \
  307. BSP_BUTTON_6_MASK | BSP_BUTTON_7_MASK)
  308. /* This macro is supporting only P0 and should not be used if LEDs are on other ports. */
  309. #define LEDS_OFF(leds_mask) do { ASSERT(sizeof(leds_mask) == 4); \
  310. NRF_GPIO->OUTSET = (leds_mask) & (LEDS_MASK & LEDS_INV_MASK); \
  311. NRF_GPIO->OUTCLR = (leds_mask) & (LEDS_MASK & ~LEDS_INV_MASK); } while (0)
  312. /* This macro is supporting only P0 and should not be used if LEDs are on other ports. */
  313. #define LEDS_ON(leds_mask) do { ASSERT(sizeof(leds_mask) == 4); \
  314. NRF_GPIO->OUTCLR = (leds_mask) & (LEDS_MASK & LEDS_INV_MASK); \
  315. NRF_GPIO->OUTSET = (leds_mask) & (LEDS_MASK & ~LEDS_INV_MASK); } while (0)
  316. /* This macro is supporting only P0 and should not be used if LEDs are on other ports. */
  317. #define LED_IS_ON(leds_mask) ((leds_mask) & (NRF_GPIO->OUT ^ LEDS_INV_MASK) )
  318. /* This macro is supporting only P0 and should not be used if LEDs are on other ports. */
  319. #define LEDS_INVERT(leds_mask) do { uint32_t gpio_state = NRF_GPIO->OUT; \
  320. ASSERT(sizeof(leds_mask) == 4); \
  321. NRF_GPIO->OUTSET = ((leds_mask) & ~gpio_state); \
  322. NRF_GPIO->OUTCLR = ((leds_mask) & gpio_state); } while (0)
  323. /* This macro is supporting only P0 and should not be used if LEDs are on other ports. */
  324. #define LEDS_CONFIGURE(leds_mask) do { uint32_t pin; \
  325. ASSERT(sizeof(leds_mask) == 4); \
  326. for (pin = 0; pin < 32; pin++) \
  327. if ( (leds_mask) & (1 << pin) ) \
  328. nrf_gpio_cfg_output(pin); } while (0)
  329. #ifdef __cplusplus
  330. }
  331. #endif
  332. #endif