boards.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /**
  2. * Copyright (c) 2016 - 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. #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. bsp_board_leds_off();
  89. nrf_gpio_pin_toggle(m_board_led_list[led_idx]);
  90. }
  91. #if defined(BOARD_PCA10059)
  92. /**
  93. * Function for configuring UICR_REGOUT0 register
  94. * to set GPIO output voltage to 3.0V.
  95. */
  96. static void gpio_output_voltage_setup(void)
  97. {
  98. // Configure UICR_REGOUT0 register only if it is set to default value.
  99. if ((NRF_UICR->REGOUT0 & UICR_REGOUT0_VOUT_Msk) ==
  100. (UICR_REGOUT0_VOUT_DEFAULT << UICR_REGOUT0_VOUT_Pos))
  101. {
  102. NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen;
  103. while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
  104. NRF_UICR->REGOUT0 = (NRF_UICR->REGOUT0 & ~((uint32_t)UICR_REGOUT0_VOUT_Msk)) |
  105. (UICR_REGOUT0_VOUT_3V0 << UICR_REGOUT0_VOUT_Pos);
  106. NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren;
  107. while (NRF_NVMC->READY == NVMC_READY_READY_Busy){}
  108. // System reset is needed to update UICR registers.
  109. NVIC_SystemReset();
  110. }
  111. }
  112. #endif
  113. static void bsp_board_leds_init(void)
  114. {
  115. #if defined(BOARD_PCA10059)
  116. // If nRF52 USB Dongle is powered from USB (high voltage mode),
  117. // GPIO output voltage is set to 1.8 V by default, which is not
  118. // enough to turn on green and blue LEDs. Therefore, GPIO voltage
  119. // needs to be increased to 3.0 V by configuring the UICR register.
  120. if (NRF_POWER->MAINREGSTATUS &
  121. (POWER_MAINREGSTATUS_MAINREGSTATUS_High << POWER_MAINREGSTATUS_MAINREGSTATUS_Pos))
  122. {
  123. gpio_output_voltage_setup();
  124. }
  125. #endif
  126. uint32_t i;
  127. for (i = 0; i < LEDS_NUMBER; ++i)
  128. {
  129. nrf_gpio_cfg_output(m_board_led_list[i]);
  130. }
  131. bsp_board_leds_off();
  132. }
  133. uint32_t bsp_board_led_idx_to_pin(uint32_t led_idx)
  134. {
  135. ASSERT(led_idx < LEDS_NUMBER);
  136. return m_board_led_list[led_idx];
  137. }
  138. uint32_t bsp_board_pin_to_led_idx(uint32_t pin_number)
  139. {
  140. uint32_t ret = 0xFFFFFFFF;
  141. uint32_t i;
  142. for (i = 0; i < LEDS_NUMBER; ++i)
  143. {
  144. if (m_board_led_list[i] == pin_number)
  145. {
  146. ret = i;
  147. break;
  148. }
  149. }
  150. return ret;
  151. }
  152. #endif //LEDS_NUMBER > 0
  153. #if BUTTONS_NUMBER > 0
  154. bool bsp_board_button_state_get(uint32_t button_idx)
  155. {
  156. ASSERT(button_idx < BUTTONS_NUMBER);
  157. bool pin_set = nrf_gpio_pin_read(m_board_btn_list[button_idx]) ? true : false;
  158. return (pin_set == (BUTTONS_ACTIVE_STATE ? true : false));
  159. }
  160. static void bsp_board_buttons_init(void)
  161. {
  162. uint32_t i;
  163. for (i = 0; i < BUTTONS_NUMBER; ++i)
  164. {
  165. nrf_gpio_cfg_input(m_board_btn_list[i], BUTTON_PULL);
  166. }
  167. }
  168. uint32_t bsp_board_pin_to_button_idx(uint32_t pin_number)
  169. {
  170. uint32_t i;
  171. uint32_t ret = 0xFFFFFFFF;
  172. for (i = 0; i < BUTTONS_NUMBER; ++i)
  173. {
  174. if (m_board_btn_list[i] == pin_number)
  175. {
  176. ret = i;
  177. break;
  178. }
  179. }
  180. return ret;
  181. }
  182. uint32_t bsp_board_button_idx_to_pin(uint32_t button_idx)
  183. {
  184. ASSERT(button_idx < BUTTONS_NUMBER);
  185. return m_board_btn_list[button_idx];
  186. }
  187. #endif //BUTTONS_NUMBER > 0
  188. void bsp_board_init(uint32_t init_flags)
  189. {
  190. #if defined(BOARDS_WITH_USB_DFU_TRIGGER) && defined(BOARD_PCA10059)
  191. (void) nrf_dfu_trigger_usb_init();
  192. #endif
  193. #if LEDS_NUMBER > 0
  194. if (init_flags & BSP_INIT_LEDS)
  195. {
  196. bsp_board_leds_init();
  197. }
  198. #endif //LEDS_NUMBER > 0
  199. #if BUTTONS_NUMBER > 0
  200. if (init_flags & BSP_INIT_BUTTONS)
  201. {
  202. bsp_board_buttons_init();
  203. }
  204. #endif //BUTTONS_NUMBER > 0
  205. }