boards.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /**
  2. * Copyright (c) 2016 - 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. #include "boards.h"
  41. #if defined(BOARDS_WITH_USB_DFU_TRIGGER) && defined(BOARD_PCA10059)
  42. #include "nrf_dfu_trigger_usb.h"
  43. #endif
  44. #include <stdint.h>
  45. #include <stdbool.h>
  46. #if LEDS_NUMBER > 0
  47. static const uint8_t m_board_led_list[LEDS_NUMBER] = LEDS_LIST;
  48. #endif
  49. #if BUTTONS_NUMBER > 0
  50. static const uint8_t m_board_btn_list[BUTTONS_NUMBER] = BUTTONS_LIST;
  51. #endif
  52. #if LEDS_NUMBER > 0
  53. bool bsp_board_led_state_get(uint32_t led_idx)
  54. {
  55. ASSERT(led_idx < LEDS_NUMBER);
  56. bool pin_set = nrf_gpio_pin_out_read(m_board_led_list[led_idx]) ? true : false;
  57. return (pin_set == (LEDS_ACTIVE_STATE ? true : false));
  58. }
  59. void bsp_board_led_on(uint32_t led_idx)
  60. {
  61. ASSERT(led_idx < LEDS_NUMBER);
  62. nrf_gpio_pin_write(m_board_led_list[led_idx], LEDS_ACTIVE_STATE ? 1 : 0);
  63. }
  64. void bsp_board_led_off(uint32_t led_idx)
  65. {
  66. ASSERT(led_idx < LEDS_NUMBER);
  67. nrf_gpio_pin_write(m_board_led_list[led_idx], LEDS_ACTIVE_STATE ? 0 : 1);
  68. }
  69. void bsp_board_leds_off(void)
  70. {
  71. uint32_t i;
  72. for (i = 0; i < LEDS_NUMBER; ++i)
  73. {
  74. bsp_board_led_off(i);
  75. }
  76. }
  77. void bsp_board_leds_on(void)
  78. {
  79. uint32_t i;
  80. for (i = 0; i < LEDS_NUMBER; ++i)
  81. {
  82. bsp_board_led_on(i);
  83. }
  84. }
  85. void bsp_board_led_invert(uint32_t led_idx)
  86. {
  87. ASSERT(led_idx < LEDS_NUMBER);
  88. nrf_gpio_pin_toggle(m_board_led_list[led_idx]);
  89. }
  90. #if defined(BOARD_PCA10059)
  91. /**
  92. * Function for configuring UICR_REGOUT0 register
  93. * to set GPIO output voltage to 3.0V.
  94. */
  95. static void gpio_output_voltage_setup(void)
  96. {
  97. // Configure UICR_REGOUT0 register only if it is set to default value.
  98. if ((NRF_UICR->REGOUT0 & UICR_REGOUT0_VOUT_Msk) ==
  99. (UICR_REGOUT0_VOUT_DEFAULT << UICR_REGOUT0_VOUT_Pos))
  100. {
  101. NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen;
  102. while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
  103. NRF_UICR->REGOUT0 = (NRF_UICR->REGOUT0 & ~((uint32_t)UICR_REGOUT0_VOUT_Msk)) |
  104. (UICR_REGOUT0_VOUT_3V0 << UICR_REGOUT0_VOUT_Pos);
  105. NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren;
  106. while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
  107. // System reset is needed to update UICR registers.
  108. NVIC_SystemReset();
  109. }
  110. }
  111. #endif
  112. static void bsp_board_leds_init(void)
  113. {
  114. #if defined(BOARD_PCA10059)
  115. // If nRF52 USB Dongle is powered from USB (high voltage mode),
  116. // GPIO output voltage is set to 1.8 V by default, which is not
  117. // enough to turn on green and blue LEDs. Therefore, GPIO voltage
  118. // needs to be increased to 3.0 V by configuring the UICR register.
  119. if (NRF_POWER->MAINREGSTATUS &
  120. (POWER_MAINREGSTATUS_MAINREGSTATUS_High << POWER_MAINREGSTATUS_MAINREGSTATUS_Pos))
  121. {
  122. gpio_output_voltage_setup();
  123. }
  124. #endif
  125. uint32_t i;
  126. for (i = 0; i < LEDS_NUMBER; ++i)
  127. {
  128. nrf_gpio_cfg_output(m_board_led_list[i]);
  129. }
  130. bsp_board_leds_off();
  131. }
  132. uint32_t bsp_board_led_idx_to_pin(uint32_t led_idx)
  133. {
  134. ASSERT(led_idx < LEDS_NUMBER);
  135. return m_board_led_list[led_idx];
  136. }
  137. uint32_t bsp_board_pin_to_led_idx(uint32_t pin_number)
  138. {
  139. uint32_t ret = 0xFFFFFFFF;
  140. uint32_t i;
  141. for (i = 0; i < LEDS_NUMBER; ++i)
  142. {
  143. if (m_board_led_list[i] == pin_number)
  144. {
  145. ret = i;
  146. break;
  147. }
  148. }
  149. return ret;
  150. }
  151. #endif //LEDS_NUMBER > 0
  152. #if BUTTONS_NUMBER > 0
  153. bool bsp_board_button_state_get(uint32_t button_idx)
  154. {
  155. ASSERT(button_idx < BUTTONS_NUMBER);
  156. bool pin_set = nrf_gpio_pin_read(m_board_btn_list[button_idx]) ? true : false;
  157. return (pin_set == (BUTTONS_ACTIVE_STATE ? true : false));
  158. }
  159. static void bsp_board_buttons_init(void)
  160. {
  161. uint32_t i;
  162. for (i = 0; i < BUTTONS_NUMBER; ++i)
  163. {
  164. nrf_gpio_cfg_input(m_board_btn_list[i], BUTTON_PULL);
  165. }
  166. }
  167. uint32_t bsp_board_pin_to_button_idx(uint32_t pin_number)
  168. {
  169. uint32_t i;
  170. uint32_t ret = 0xFFFFFFFF;
  171. for (i = 0; i < BUTTONS_NUMBER; ++i)
  172. {
  173. if (m_board_btn_list[i] == pin_number)
  174. {
  175. ret = i;
  176. break;
  177. }
  178. }
  179. return ret;
  180. }
  181. uint32_t bsp_board_button_idx_to_pin(uint32_t button_idx)
  182. {
  183. ASSERT(button_idx < BUTTONS_NUMBER);
  184. return m_board_btn_list[button_idx];
  185. }
  186. #endif //BUTTONS_NUMBER > 0
  187. void bsp_board_init(uint32_t init_flags)
  188. {
  189. #if defined(BOARDS_WITH_USB_DFU_TRIGGER) && defined(BOARD_PCA10059)
  190. (void) nrf_dfu_trigger_usb_init();
  191. #endif
  192. #if LEDS_NUMBER > 0
  193. if (init_flags & BSP_INIT_LEDS)
  194. {
  195. bsp_board_leds_init();
  196. }
  197. #endif //LEDS_NUMBER > 0
  198. #if BUTTONS_NUMBER > 0
  199. if (init_flags & BSP_INIT_BUTTONS)
  200. {
  201. bsp_board_buttons_init();
  202. }
  203. #endif //BUTTONS_NUMBER > 0
  204. }