boards.h 11 KB

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