nrfx_glue.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2018 Glenn Ruben Bakke
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #ifndef NRFX_GLUE_H
  27. #define NRFX_GLUE_H
  28. #include <soc/nrfx_irqs.h>
  29. #define NRFX_ASSERT(expression) do { bool res = expression; (void)res; } while (0)
  30. #define NRFX_DELAY_US mp_hal_delay_us
  31. #if BLUETOOTH_SD
  32. #if NRF51
  33. #include "nrf_soc.h"
  34. #else
  35. #include "nrf_nvic.h"
  36. #endif
  37. #include "ble_drv.h"
  38. #if (BLUETOOTH_SD == 110)
  39. #define NRFX_IRQ_ENABLE(irq_number) \
  40. do { \
  41. if (ble_drv_stack_enabled() == 1) \
  42. { \
  43. sd_nvic_EnableIRQ(irq_number); \
  44. } else { \
  45. NVIC_EnableIRQ(irq_number); \
  46. } \
  47. } while(0)
  48. #else
  49. #define NRFX_IRQ_ENABLE(irq_number) sd_nvic_EnableIRQ(irq_number)
  50. #endif
  51. #if (BLUETOOTH_SD == 110)
  52. #define NRFX_IRQ_DISABLE(irq_number) \
  53. do { \
  54. if (ble_drv_stack_enabled() == 1) \
  55. { \
  56. sd_nvic_DisableIRQ(irq_number); \
  57. } else { \
  58. NVIC_DisableIRQ(irq_number); \
  59. } \
  60. } while(0)
  61. #else
  62. #define NRFX_IRQ_DISABLE(irq_number) sd_nvic_DisableIRQ(irq_number)
  63. #endif
  64. #if (BLUETOOTH_SD == 110)
  65. #define NRFX_IRQ_PRIORITY_SET(irq_number, priority) \
  66. do { \
  67. if (ble_drv_stack_enabled() == 1) \
  68. { \
  69. sd_nvic_SetPriority(irq_number, priority); \
  70. } else { \
  71. NVIC_SetPriority(irq_number, priority); \
  72. } \
  73. } while(0)
  74. #else
  75. #define NRFX_IRQ_PRIORITY_SET(irq_number, priority) sd_nvic_SetPriority(irq_number, priority)
  76. #endif
  77. #if (BLUETOOTH_SD == 110)
  78. #define NRFX_IRQ_PENDING_SET(irq_number) \
  79. do { \
  80. if (ble_drv_stack_enabled() == 1) \
  81. { \
  82. sd_nvic_SetPendingIRQ(irq_number); \
  83. } else { \
  84. NVIC_SetPendingIRQ(irq_number); \
  85. } \
  86. } while(0)
  87. #else
  88. #define NRFX_IRQ_PENDING_SET(irq_number) sd_nvic_SetPendingIRQ(irq_number)
  89. #endif
  90. #if (BLUETOOTH_SD == 110)
  91. #define NRFX_IRQ_PENDING_CLEAR(irq_number) \
  92. do { \
  93. if (ble_drv_stack_enabled() == 1) \
  94. { \
  95. sd_nvic_ClearPendingIRQ(irq_number); \
  96. } else { \
  97. NVIC_ClearPendingIRQ(irq_number)(irq_number); \
  98. } \
  99. } while(0)
  100. #else
  101. #define NRFX_IRQ_PENDING_CLEAR(irq_number) sd_nvic_ClearPendingIRQ(irq_number)
  102. #endif
  103. #define NRFX_CRITICAL_SECTION_ENTER() \
  104. { \
  105. uint8_t _is_nested_critical_region; \
  106. sd_nvic_critical_region_enter(&_is_nested_critical_region);
  107. #define NRFX_CRITICAL_SECTION_EXIT() \
  108. sd_nvic_critical_region_exit(_is_nested_critical_region); \
  109. }
  110. #else // BLUETOOTH_SD
  111. #define NRFX_IRQ_ENABLE(irq_number) NVIC_EnableIRQ(irq_number)
  112. #define NRFX_IRQ_DISABLE(irq_number) NVIC_DisableIRQ(irq_number)
  113. #define NRFX_IRQ_PRIORITY_SET(irq_number, priority) NVIC_SetPriority(irq_number, priority)
  114. #define NRFX_IRQ_PENDING_SET(irq_number) NVIC_SetPendingIRQ(irq_number)
  115. #define NRFX_IRQ_PENDING_CLEAR(irq_number) NVIC_ClearPendingIRQ(irq_number)
  116. // Source:
  117. // https://devzone.nordicsemi.com/f/nordic-q-a/8572/disable-interrupts-and-enable-interrupts-if-they-where-enabled/31347#31347
  118. #define NRFX_CRITICAL_SECTION_ENTER() { int _old_primask = __get_PRIMASK(); __disable_irq();
  119. #define NRFX_CRITICAL_SECTION_EXIT() __set_PRIMASK(_old_primask); }
  120. #endif // !BLUETOOTH_SD
  121. #endif // NRFX_GLUE_H