boards.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /**
  2. * Copyright (c) 2016 - 2017, 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. #include <stdint.h>
  42. #include <stdbool.h>
  43. #if LEDS_NUMBER > 0
  44. static const uint8_t m_board_led_list[LEDS_NUMBER] = LEDS_LIST;
  45. #endif
  46. #if BUTTONS_NUMBER > 0
  47. static const uint8_t m_board_btn_list[BUTTONS_NUMBER] = BUTTONS_LIST;
  48. #endif
  49. #if LEDS_NUMBER > 0
  50. bool bsp_board_led_state_get(uint32_t led_idx)
  51. {
  52. ASSERT(led_idx < LEDS_NUMBER);
  53. bool pin_set = nrf_gpio_pin_out_read(m_board_led_list[led_idx]) ? true : false;
  54. return (pin_set == (LEDS_ACTIVE_STATE ? true : false));
  55. }
  56. void bsp_board_led_on(uint32_t led_idx)
  57. {
  58. ASSERT(led_idx < LEDS_NUMBER);
  59. nrf_gpio_pin_write(m_board_led_list[led_idx], LEDS_ACTIVE_STATE ? 1 : 0);
  60. }
  61. void bsp_board_led_off(uint32_t led_idx)
  62. {
  63. ASSERT(led_idx < LEDS_NUMBER);
  64. nrf_gpio_pin_write(m_board_led_list[led_idx], LEDS_ACTIVE_STATE ? 0 : 1);
  65. }
  66. void bsp_board_leds_off(void)
  67. {
  68. uint32_t i;
  69. for(i = 0; i < LEDS_NUMBER; ++i)
  70. {
  71. bsp_board_led_off(i);
  72. }
  73. }
  74. void bsp_board_leds_on(void)
  75. {
  76. uint32_t i;
  77. for(i = 0; i < LEDS_NUMBER; ++i)
  78. {
  79. bsp_board_led_on(i);
  80. }
  81. }
  82. void bsp_board_led_invert(uint32_t led_idx)
  83. {
  84. ASSERT(led_idx < LEDS_NUMBER);
  85. nrf_gpio_pin_toggle(m_board_led_list[led_idx]);
  86. }
  87. void bsp_board_leds_init(void)
  88. {
  89. uint32_t i;
  90. for(i = 0; i < LEDS_NUMBER; ++i)
  91. {
  92. nrf_gpio_cfg_output(m_board_led_list[i]);
  93. }
  94. bsp_board_leds_off();
  95. }
  96. uint32_t bsp_board_led_idx_to_pin(uint32_t led_idx)
  97. {
  98. ASSERT(led_idx < LEDS_NUMBER);
  99. return m_board_led_list[led_idx];
  100. }
  101. uint32_t bsp_board_pin_to_led_idx(uint32_t pin_number)
  102. {
  103. uint32_t ret = 0xFFFFFFFF;
  104. uint32_t i;
  105. for(i = 0; i < LEDS_NUMBER; ++i)
  106. {
  107. if (m_board_led_list[i] == pin_number)
  108. {
  109. ret = i;
  110. break;
  111. }
  112. }
  113. return ret;
  114. }
  115. #endif //LEDS_NUMBER > 0
  116. #if BUTTONS_NUMBER > 0
  117. bool bsp_board_button_state_get(uint32_t button_idx)
  118. {
  119. ASSERT(button_idx < BUTTONS_NUMBER);
  120. bool pin_set = nrf_gpio_pin_read(m_board_btn_list[button_idx]) ? true : false;
  121. return (pin_set == (BUTTONS_ACTIVE_STATE ? true : false));
  122. }
  123. void bsp_board_buttons_init(void)
  124. {
  125. uint32_t i;
  126. for(i = 0; i < BUTTONS_NUMBER; ++i)
  127. {
  128. nrf_gpio_cfg_input(m_board_btn_list[i], BUTTON_PULL);
  129. }
  130. }
  131. uint32_t bsp_board_pin_to_button_idx(uint32_t pin_number)
  132. {
  133. uint32_t i;
  134. uint32_t ret = 0xFFFFFFFF;
  135. for(i = 0; i < BUTTONS_NUMBER; ++i)
  136. {
  137. if (m_board_btn_list[i] == pin_number)
  138. {
  139. ret = i;
  140. break;
  141. }
  142. }
  143. return ret;
  144. }
  145. uint32_t bsp_board_button_idx_to_pin(uint32_t button_idx)
  146. {
  147. ASSERT(button_idx < BUTTONS_NUMBER);
  148. return m_board_btn_list[button_idx];
  149. }
  150. #endif //BUTTONS_NUMBER > 0