app_util_platform.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* Copyright (c) 2014 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. /**@file
  13. *
  14. * @defgroup app_util_platform Utility Functions and Definitions (Platform)
  15. * @{
  16. * @ingroup app_common
  17. *
  18. * @brief Various types and definitions available to all applications when using SoftDevice.
  19. */
  20. #ifndef APP_UTIL_PLATFORM_H__
  21. #define APP_UTIL_PLATFORM_H__
  22. #include <stdint.h>
  23. #include "compiler_abstraction.h"
  24. #include "nrf.h"
  25. #ifdef SOFTDEVICE_PRESENT
  26. #include "nrf_soc.h"
  27. #include "nrf_nvic.h"
  28. #endif
  29. #include "nrf_assert.h"
  30. #include "app_error.h"
  31. #if defined(NRF51)
  32. #define _PRIO_SD_HIGH 0
  33. #define _PRIO_APP_HIGH 1
  34. #define _PRIO_APP_MID 1
  35. #define _PRIO_SD_LOW 2
  36. #define _PRIO_APP_LOW 3
  37. #define _PRIO_APP_LOWEST 3
  38. #define _PRIO_THREAD 4
  39. #elif defined(NRF52)
  40. #define _PRIO_SD_HIGH 0
  41. #define _PRIO_SD_MID 1
  42. #define _PRIO_APP_HIGH 2
  43. #define _PRIO_APP_MID 3
  44. #define _PRIO_SD_LOW 4
  45. #define _PRIO_SD_LOWEST 5
  46. #define _PRIO_APP_LOW 6
  47. #define _PRIO_APP_LOWEST 7
  48. #define _PRIO_THREAD 15
  49. #else
  50. #error "No platform defined"
  51. #endif
  52. /**@brief The interrupt priorities available to the application while the SoftDevice is active. */
  53. typedef enum
  54. {
  55. #ifdef SOFTDEVICE_PRESENT
  56. APP_IRQ_PRIORITY_HIGHEST = _PRIO_SD_HIGH,
  57. #else
  58. APP_IRQ_PRIORITY_HIGHEST = _PRIO_APP_HIGH,
  59. #endif
  60. APP_IRQ_PRIORITY_HIGH = _PRIO_APP_HIGH,
  61. #ifndef SOFTDEVICE_PRESENT
  62. APP_IRQ_PRIORITY_MID = _PRIO_SD_LOW,
  63. #else
  64. APP_IRQ_PRIORITY_MID = _PRIO_APP_MID,
  65. #endif
  66. APP_IRQ_PRIORITY_LOW = _PRIO_APP_LOW,
  67. APP_IRQ_PRIORITY_LOWEST = _PRIO_APP_LOWEST,
  68. APP_IRQ_PRIORITY_THREAD = _PRIO_THREAD /**< "Interrupt level" when running in Thread Mode. */
  69. } app_irq_priority_t;
  70. /*@brief The privilege levels available to applications in Thread Mode */
  71. typedef enum
  72. {
  73. APP_LEVEL_UNPRIVILEGED,
  74. APP_LEVEL_PRIVILEGED
  75. } app_level_t;
  76. /**@cond NO_DOXYGEN */
  77. #define EXTERNAL_INT_VECTOR_OFFSET 16
  78. /**@endcond */
  79. #define PACKED(TYPE) __packed TYPE
  80. void app_util_critical_region_enter (uint8_t *p_nested);
  81. void app_util_critical_region_exit (uint8_t nested);
  82. /**@brief Macro for entering a critical region.
  83. *
  84. * @note Due to implementation details, there must exist one and only one call to
  85. * CRITICAL_REGION_EXIT() for each call to CRITICAL_REGION_ENTER(), and they must be located
  86. * in the same scope.
  87. */
  88. #ifdef SOFTDEVICE_PRESENT
  89. #define CRITICAL_REGION_ENTER() \
  90. { \
  91. uint8_t __CR_NESTED = 0; \
  92. app_util_critical_region_enter(&__CR_NESTED);
  93. #else
  94. #define CRITICAL_REGION_ENTER() app_util_critical_region_enter(NULL)
  95. #endif
  96. /**@brief Macro for leaving a critical region.
  97. *
  98. * @note Due to implementation details, there must exist one and only one call to
  99. * CRITICAL_REGION_EXIT() for each call to CRITICAL_REGION_ENTER(), and they must be located
  100. * in the same scope.
  101. */
  102. #ifdef SOFTDEVICE_PRESENT
  103. #define CRITICAL_REGION_EXIT() \
  104. app_util_critical_region_exit(__CR_NESTED); \
  105. }
  106. #else
  107. #define CRITICAL_REGION_EXIT() app_util_critical_region_exit(0)
  108. #endif
  109. /* Workaround for Keil 4 */
  110. #ifndef IPSR_ISR_Msk
  111. #define IPSR_ISR_Msk (0x1FFUL /*<< IPSR_ISR_Pos*/) /*!< IPSR: ISR Mask */
  112. #endif
  113. /**@brief Macro to enable anonymous unions from a certain point in the code.
  114. */
  115. #if defined(__CC_ARM)
  116. #define ANON_UNIONS_ENABLE _Pragma("push") \
  117. _Pragma("anon_unions")
  118. #elif defined(__ICCARM__)
  119. #define ANON_UNIONS_ENABLE _Pragma("language=extended")
  120. #else
  121. #define ANON_UNIONS_ENABLE
  122. // No action will be taken.
  123. // For GCC anonymous unions are enabled by default.
  124. #endif
  125. /**@brief Macro to disable anonymous unions from a certain point in the code.
  126. * @note Call only after first calling @ref ANON_UNIONS_ENABLE.
  127. */
  128. #if defined(__CC_ARM)
  129. #define ANON_UNIONS_DISABLE _Pragma("pop")
  130. #elif defined(__ICCARM__)
  131. #define ANON_UNIONS_DISABLE
  132. // for IAR leave anonymous unions enabled
  133. #else
  134. #define ANON_UNIONS_DISABLE
  135. // No action will be taken.
  136. // For GCC anonymous unions are enabled by default.
  137. #endif
  138. /* Workaround for Keil 4 */
  139. #ifndef CONTROL_nPRIV_Msk
  140. #define CONTROL_nPRIV_Msk (1UL /*<< CONTROL_nPRIV_Pos*/) /*!< CONTROL: nPRIV Mask */
  141. #endif
  142. /**@brief Function for finding the current interrupt level.
  143. *
  144. * @return Current interrupt level.
  145. * @retval APP_IRQ_PRIORITY_HIGH We are running in Application High interrupt level.
  146. * @retval APP_IRQ_PRIORITY_LOW We are running in Application Low interrupt level.
  147. * @retval APP_IRQ_PRIORITY_THREAD We are running in Thread Mode.
  148. */
  149. static __INLINE uint8_t current_int_priority_get(void)
  150. {
  151. uint32_t isr_vector_num = __get_IPSR() & IPSR_ISR_Msk ;
  152. if (isr_vector_num > 0)
  153. {
  154. int32_t irq_type = ((int32_t)isr_vector_num - EXTERNAL_INT_VECTOR_OFFSET);
  155. return (NVIC_GetPriority((IRQn_Type)irq_type) & 0xFF);
  156. }
  157. else
  158. {
  159. return APP_IRQ_PRIORITY_THREAD;
  160. }
  161. }
  162. /**@brief Function for finding out the current privilege level.
  163. *
  164. * @return Current privilege level.
  165. * @retval APP_LEVEL_UNPRIVILEGED We are running in unprivileged level.
  166. * @retval APP_LEVEL_PRIVILEGED We are running in privileged level.
  167. */
  168. static __INLINE uint8_t privilege_level_get(void)
  169. {
  170. #if defined(NRF51)
  171. /* the Cortex-M0 has no concept of privilege */
  172. return APP_LEVEL_PRIVILEGED;
  173. #elif defined(NRF52)
  174. uint32_t isr_vector_num = __get_IPSR() & IPSR_ISR_Msk ;
  175. if (0 == isr_vector_num)
  176. {
  177. /* Thread Mode, check nPRIV */
  178. int32_t control = __get_CONTROL();
  179. return control & CONTROL_nPRIV_Msk ? APP_LEVEL_UNPRIVILEGED : APP_LEVEL_PRIVILEGED;
  180. }
  181. else
  182. {
  183. /* Handler Mode, always privileged */
  184. return APP_LEVEL_PRIVILEGED;
  185. }
  186. #endif
  187. }
  188. #endif // APP_UTIL_PLATFORM_H__
  189. /** @} */