nrfx_prs.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**
  2. * Copyright (c) 2017 - 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 <nrfx.h>
  41. #if NRFX_CHECK(NRFX_PRS_ENABLED)
  42. #include "nrfx_prs.h"
  43. #define NRFX_LOG_MODULE PRS
  44. #include <nrfx_log.h>
  45. #define LOG_FUNCTION_EXIT(level, ret_code) \
  46. NRFX_LOG_##level("Function: %s, error code: %s.", \
  47. __func__, \
  48. NRFX_LOG_ERROR_STRING_GET(ret_code))
  49. typedef struct {
  50. nrfx_irq_handler_t handler;
  51. bool acquired;
  52. } prs_box_t;
  53. #ifndef USE_REGISTER_UART
  54. #define PRS_BOX_DEFINE(n) \
  55. static prs_box_t m_prs_box_##n = { .handler = NULL, .acquired = false }; \
  56. void nrfx_prs_box_##n##_irq_handler(void) \
  57. { \
  58. NRFX_ASSERT(m_prs_box_##n.handler); \
  59. m_prs_box_##n.handler(); \
  60. }
  61. #else
  62. #define PRS_BOX_DEFINE(n) \
  63. static prs_box_t m_prs_box_##n = { .handler = NULL, .acquired = false };
  64. #endif
  65. //#define PRS_BOX_DEFINE(n) \
  66. // static prs_box_t m_prs_box_##n = { .handler = NULL, .acquired = false }; \
  67. // void nrfx_prs_box_##n##_irq_handler(void) \
  68. // { \
  69. // NRFX_ASSERT(m_prs_box_##n.handler); \
  70. // m_prs_box_##n.handler(); \
  71. // }
  72. #if defined(NRFX_PRS_BOX_0_ADDR) && NRFX_CHECK(NRFX_PRS_BOX_0_ENABLED)
  73. PRS_BOX_DEFINE(0)
  74. #endif
  75. #if defined(NRFX_PRS_BOX_1_ADDR) && NRFX_CHECK(NRFX_PRS_BOX_1_ENABLED)
  76. PRS_BOX_DEFINE(1)
  77. #endif
  78. #if defined(NRFX_PRS_BOX_2_ADDR) && NRFX_CHECK(NRFX_PRS_BOX_2_ENABLED)
  79. PRS_BOX_DEFINE(2)
  80. #endif
  81. #if defined(NRFX_PRS_BOX_3_ADDR) && NRFX_CHECK(NRFX_PRS_BOX_3_ENABLED)
  82. PRS_BOX_DEFINE(3)
  83. #endif
  84. #if defined(NRFX_PRS_BOX_4_ADDR) && NRFX_CHECK(NRFX_PRS_BOX_4_ENABLED)
  85. PRS_BOX_DEFINE(4)
  86. #endif
  87. static prs_box_t * prs_box_get(void const * p_base_addr)
  88. {
  89. #if !defined(IS_PRS_BOX)
  90. #define IS_PRS_BOX(n, p_base_addr) ((p_base_addr) == NRFX_PRS_BOX_##n##_ADDR)
  91. #endif
  92. #if defined(NRFX_PRS_BOX_0_ADDR) && NRFX_CHECK(NRFX_PRS_BOX_0_ENABLED)
  93. if (IS_PRS_BOX(0, p_base_addr)) { return &m_prs_box_0; }
  94. else
  95. #endif
  96. #if defined(NRFX_PRS_BOX_1_ADDR) && NRFX_CHECK(NRFX_PRS_BOX_1_ENABLED)
  97. if (IS_PRS_BOX(1, p_base_addr)) { return &m_prs_box_1; }
  98. else
  99. #endif
  100. #if defined(NRFX_PRS_BOX_2_ADDR) && NRFX_CHECK(NRFX_PRS_BOX_2_ENABLED)
  101. if (IS_PRS_BOX(2, p_base_addr)) { return &m_prs_box_2; }
  102. else
  103. #endif
  104. #if defined(NRFX_PRS_BOX_3_ADDR) && NRFX_CHECK(NRFX_PRS_BOX_3_ENABLED)
  105. if (IS_PRS_BOX(3, p_base_addr)) { return &m_prs_box_3; }
  106. else
  107. #endif
  108. #if defined(NRFX_PRS_BOX_4_ADDR) && NRFX_CHECK(NRFX_PRS_BOX_4_ENABLED)
  109. if (IS_PRS_BOX(4, p_base_addr)) { return &m_prs_box_4; }
  110. else
  111. #endif
  112. {
  113. return NULL;
  114. }
  115. }
  116. nrfx_err_t nrfx_prs_acquire(void const * p_base_addr,
  117. nrfx_irq_handler_t irq_handler)
  118. {
  119. NRFX_ASSERT(p_base_addr);
  120. nrfx_err_t ret_code;
  121. prs_box_t * p_box = prs_box_get(p_base_addr);
  122. if (p_box != NULL)
  123. {
  124. bool busy = false;
  125. NRFX_CRITICAL_SECTION_ENTER();
  126. if (p_box->acquired)
  127. {
  128. busy = true;
  129. }
  130. else
  131. {
  132. p_box->handler = irq_handler;
  133. p_box->acquired = true;
  134. }
  135. NRFX_CRITICAL_SECTION_EXIT();
  136. if (busy)
  137. {
  138. ret_code = NRFX_ERROR_BUSY;
  139. LOG_FUNCTION_EXIT(WARNING, ret_code);
  140. return ret_code;
  141. }
  142. }
  143. ret_code = NRFX_SUCCESS;
  144. LOG_FUNCTION_EXIT(INFO, ret_code);
  145. return ret_code;
  146. }
  147. void nrfx_prs_release(void const * p_base_addr)
  148. {
  149. NRFX_ASSERT(p_base_addr);
  150. prs_box_t * p_box = prs_box_get(p_base_addr);
  151. if (p_box != NULL)
  152. {
  153. p_box->handler = NULL;
  154. p_box->acquired = false;
  155. }
  156. }
  157. #endif // NRFX_CHECK(NRFX_PRS_ENABLED)