boards.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. nrf_gpio_pin_write(7, LEDS_ACTIVE_STATE ? 0 : 1);
  77. }
  78. void bsp_board_leds_on(void)
  79. {
  80. uint32_t i;
  81. for (i = 0; i < LEDS_NUMBER; ++i)
  82. {
  83. bsp_board_led_on(i);
  84. }
  85. }
  86. void bsp_board_led_invert(uint32_t led_idx)
  87. {
  88. ASSERT(led_idx < LEDS_NUMBER);
  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. nrf_gpio_cfg_output(7);
  133. nrf_gpio_pin_write(7, LEDS_ACTIVE_STATE ? 0 : 1);
  134. }
  135. uint32_t bsp_board_led_idx_to_pin(uint32_t led_idx)
  136. {
  137. ASSERT(led_idx < LEDS_NUMBER);
  138. return m_board_led_list[led_idx];
  139. }
  140. uint32_t bsp_board_pin_to_led_idx(uint32_t pin_number)
  141. {
  142. uint32_t ret = 0xFFFFFFFF;
  143. uint32_t i;
  144. for (i = 0; i < LEDS_NUMBER; ++i)
  145. {
  146. if (m_board_led_list[i] == pin_number)
  147. {
  148. ret = i;
  149. break;
  150. }
  151. }
  152. return ret;
  153. }
  154. #endif //LEDS_NUMBER > 0
  155. #if BUTTONS_NUMBER > 0
  156. bool bsp_board_button_state_get(uint32_t button_idx)
  157. {
  158. ASSERT(button_idx < BUTTONS_NUMBER);
  159. bool pin_set = nrf_gpio_pin_read(m_board_btn_list[button_idx]) ? true : false;
  160. return (pin_set == (BUTTONS_ACTIVE_STATE ? true : false));
  161. }
  162. static void bsp_board_buttons_init(void)
  163. {
  164. uint32_t i;
  165. for (i = 0; i < BUTTONS_NUMBER; ++i)
  166. {
  167. nrf_gpio_cfg_input(m_board_btn_list[i], BUTTON_PULL);
  168. }
  169. }
  170. uint32_t bsp_board_pin_to_button_idx(uint32_t pin_number)
  171. {
  172. uint32_t i;
  173. uint32_t ret = 0xFFFFFFFF;
  174. for (i = 0; i < BUTTONS_NUMBER; ++i)
  175. {
  176. if (m_board_btn_list[i] == pin_number)
  177. {
  178. ret = i;
  179. break;
  180. }
  181. }
  182. return ret;
  183. }
  184. uint32_t bsp_board_button_idx_to_pin(uint32_t button_idx)
  185. {
  186. ASSERT(button_idx < BUTTONS_NUMBER);
  187. return m_board_btn_list[button_idx];
  188. }
  189. #endif //BUTTONS_NUMBER > 0
  190. void bsp_board_init(uint32_t init_flags)
  191. {
  192. #if defined(BOARDS_WITH_USB_DFU_TRIGGER) && defined(BOARD_PCA10059)
  193. (void) nrf_dfu_trigger_usb_init();
  194. #endif
  195. #if LEDS_NUMBER > 0
  196. if (init_flags & BSP_INIT_LEDS)
  197. {
  198. bsp_board_leds_init();
  199. }
  200. #endif //LEDS_NUMBER > 0
  201. #if BUTTONS_NUMBER > 0
  202. if (init_flags & BSP_INIT_BUTTONS)
  203. {
  204. bsp_board_buttons_init();
  205. }
  206. #endif //BUTTONS_NUMBER > 0
  207. }