app_util_platform.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /**
  2. * Copyright (c) 2014 - 2018, 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. /**@file
  41. *
  42. * @defgroup app_util_platform Utility Functions and Definitions (Platform)
  43. * @{
  44. * @ingroup app_common
  45. *
  46. * @brief Various types and definitions available to all applications when using SoftDevice.
  47. */
  48. #ifndef APP_UTIL_PLATFORM_H__
  49. #define APP_UTIL_PLATFORM_H__
  50. #include <stdint.h>
  51. #include "compiler_abstraction.h"
  52. #include "nrf.h"
  53. #ifdef SOFTDEVICE_PRESENT
  54. #include "nrf_soc.h"
  55. #include "nrf_nvic.h"
  56. #endif
  57. #include "nrf_assert.h"
  58. #include "app_error.h"
  59. #ifdef __cplusplus
  60. extern "C" {
  61. #endif
  62. #if __CORTEX_M == (0x00U)
  63. #define _PRIO_SD_HIGH 0
  64. #define _PRIO_APP_HIGH 1
  65. #define _PRIO_APP_MID 1
  66. #define _PRIO_SD_LOW 2
  67. #define _PRIO_APP_LOW 3
  68. #define _PRIO_APP_LOWEST 3
  69. #define _PRIO_THREAD 4
  70. #elif __CORTEX_M == (0x04U)
  71. #define _PRIO_SD_HIGH 0
  72. #define _PRIO_SD_MID 1
  73. #define _PRIO_APP_HIGH 2
  74. #define _PRIO_APP_MID 3
  75. #define _PRIO_SD_LOW 4
  76. #define _PRIO_SD_LOWEST 5
  77. #define _PRIO_APP_LOW 6
  78. #define _PRIO_APP_LOWEST 7
  79. #define _PRIO_THREAD 15
  80. #else
  81. #error "No platform defined"
  82. #endif
  83. //lint -save -e113 -e452
  84. /**@brief The interrupt priorities available to the application while the SoftDevice is active. */
  85. typedef enum
  86. {
  87. #ifndef SOFTDEVICE_PRESENT
  88. APP_IRQ_PRIORITY_HIGHEST = _PRIO_SD_HIGH,
  89. #else
  90. APP_IRQ_PRIORITY_HIGHEST = _PRIO_APP_HIGH,
  91. #endif
  92. APP_IRQ_PRIORITY_HIGH = _PRIO_APP_HIGH,
  93. #ifndef SOFTDEVICE_PRESENT
  94. APP_IRQ_PRIORITY_MID = _PRIO_SD_LOW,
  95. #else
  96. APP_IRQ_PRIORITY_MID = _PRIO_APP_MID,
  97. #endif
  98. APP_IRQ_PRIORITY_LOW = _PRIO_APP_LOW,
  99. APP_IRQ_PRIORITY_LOWEST = _PRIO_APP_LOWEST,
  100. APP_IRQ_PRIORITY_THREAD = _PRIO_THREAD /**< "Interrupt level" when running in Thread Mode. */
  101. } app_irq_priority_t;
  102. //lint -restore
  103. /*@brief The privilege levels available to applications in Thread Mode */
  104. typedef enum
  105. {
  106. APP_LEVEL_UNPRIVILEGED,
  107. APP_LEVEL_PRIVILEGED
  108. } app_level_t;
  109. /**@cond NO_DOXYGEN */
  110. #define EXTERNAL_INT_VECTOR_OFFSET 16
  111. /**@endcond */
  112. /**@brief Macro for setting a breakpoint.
  113. */
  114. #if defined(__GNUC__)
  115. #define NRF_BREAKPOINT __asm__("BKPT 0");
  116. #else
  117. #define NRF_BREAKPOINT __BKPT(0)
  118. #endif
  119. /** @brief Macro for setting a breakpoint.
  120. *
  121. * If it is possible to detect debugger presence then it is set only in that case.
  122. *
  123. */
  124. #if __CORTEX_M == 0x04
  125. #define NRF_BREAKPOINT_COND do { \
  126. /* C_DEBUGEN == 1 -> Debugger Connected */ \
  127. if (CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk) \
  128. { \
  129. /* Generate breakpoint if debugger is connected */ \
  130. NRF_BREAKPOINT; \
  131. } \
  132. }while (0)
  133. #else
  134. #define NRF_BREAKPOINT_COND NRF_BREAKPOINT
  135. #endif // __CORTEX_M == 0x04
  136. #if defined ( __CC_ARM )
  137. #define PACKED(TYPE) __packed TYPE
  138. #define PACKED_STRUCT PACKED(struct)
  139. #elif defined ( __GNUC__ )
  140. #define PACKED __attribute__((packed))
  141. #define PACKED_STRUCT struct PACKED
  142. #elif defined (__ICCARM__)
  143. #define PACKED_STRUCT __packed struct
  144. #endif
  145. #if defined ( __CC_ARM )
  146. #define PRAGMA_OPTIMIZATION_FORCE_START _Pragma ("push") \
  147. _Pragma ("O3")
  148. #define PRAGMA_OPTIMIZATION_FORCE_END _Pragma ("pop")
  149. #elif defined ( __GNUC__ )
  150. #define PRAGMA_OPTIMIZATION_FORCE_START _Pragma("GCC push_options") \
  151. _Pragma ("GCC optimize (\"Os\")")
  152. #define PRAGMA_OPTIMIZATION_FORCE_END _Pragma ("GCC pop_options")
  153. #elif defined (__ICCARM__)
  154. #define PRAGMA_OPTIMIZATION_FORCE_START _Pragma ("optimize=high z")
  155. #define PRAGMA_OPTIMIZATION_FORCE_END
  156. #endif
  157. void app_util_critical_region_enter (uint8_t *p_nested);
  158. void app_util_critical_region_exit (uint8_t nested);
  159. /**@brief Macro for entering a critical region.
  160. *
  161. * @note Due to implementation details, there must exist one and only one call to
  162. * CRITICAL_REGION_EXIT() for each call to CRITICAL_REGION_ENTER(), and they must be located
  163. * in the same scope.
  164. */
  165. #ifdef SOFTDEVICE_PRESENT
  166. #define CRITICAL_REGION_ENTER() \
  167. { \
  168. uint8_t __CR_NESTED = 0; \
  169. app_util_critical_region_enter(&__CR_NESTED);
  170. #else
  171. #define CRITICAL_REGION_ENTER() app_util_critical_region_enter(NULL)
  172. #endif
  173. /**@brief Macro for leaving a critical region.
  174. *
  175. * @note Due to implementation details, there must exist one and only one call to
  176. * CRITICAL_REGION_EXIT() for each call to CRITICAL_REGION_ENTER(), and they must be located
  177. * in the same scope.
  178. */
  179. #ifdef SOFTDEVICE_PRESENT
  180. #define CRITICAL_REGION_EXIT() \
  181. app_util_critical_region_exit(__CR_NESTED); \
  182. }
  183. #else
  184. #define CRITICAL_REGION_EXIT() app_util_critical_region_exit(0)
  185. #endif
  186. /* Workaround for Keil 4 */
  187. #ifndef IPSR_ISR_Msk
  188. #define IPSR_ISR_Msk (0x1FFUL /*<< IPSR_ISR_Pos*/) /*!< IPSR: ISR Mask */
  189. #endif
  190. /**@brief Macro to enable anonymous unions from a certain point in the code.
  191. */
  192. #if defined(__CC_ARM)
  193. #define ANON_UNIONS_ENABLE _Pragma("push") \
  194. _Pragma("anon_unions") \
  195. struct semicolon_swallower
  196. #elif defined(__ICCARM__)
  197. #define ANON_UNIONS_ENABLE _Pragma("language=extended") \
  198. struct semicolon_swallower
  199. #else
  200. #define ANON_UNIONS_ENABLE struct semicolon_swallower
  201. // No action will be taken.
  202. // For GCC anonymous unions are enabled by default.
  203. #endif
  204. /**@brief Macro to disable anonymous unions from a certain point in the code.
  205. * @note Call only after first calling @ref ANON_UNIONS_ENABLE.
  206. */
  207. #if defined(__CC_ARM)
  208. #define ANON_UNIONS_DISABLE _Pragma("pop") \
  209. struct semicolon_swallower
  210. #elif defined(__ICCARM__)
  211. #define ANON_UNIONS_DISABLE struct semicolon_swallower
  212. // for IAR leave anonymous unions enabled
  213. #else
  214. #define ANON_UNIONS_DISABLE struct semicolon_swallower
  215. // No action will be taken.
  216. // For GCC anonymous unions are enabled by default.
  217. #endif
  218. /**@brief Macro for adding pragma directive only for GCC.
  219. */
  220. #ifdef __GNUC__
  221. #define GCC_PRAGMA(v) _Pragma(v)
  222. #else
  223. #define GCC_PRAGMA(v)
  224. #endif
  225. /* Workaround for Keil 4 */
  226. #ifndef CONTROL_nPRIV_Msk
  227. #define CONTROL_nPRIV_Msk (1UL /*<< CONTROL_nPRIV_Pos*/) /*!< CONTROL: nPRIV Mask */
  228. #endif
  229. /**@brief Function for finding the current interrupt level.
  230. *
  231. * @return Current interrupt level.
  232. * @retval APP_IRQ_PRIORITY_HIGH We are running in Application High interrupt level.
  233. * @retval APP_IRQ_PRIORITY_LOW We are running in Application Low interrupt level.
  234. * @retval APP_IRQ_PRIORITY_THREAD We are running in Thread Mode.
  235. */
  236. uint8_t current_int_priority_get(void);
  237. /**@brief Function for finding out the current privilege level.
  238. *
  239. * @return Current privilege level.
  240. * @retval APP_LEVEL_UNPRIVILEGED We are running in unprivileged level.
  241. * @retval APP_LEVEL_PRIVILEGED We are running in privileged level.
  242. */
  243. uint8_t privilege_level_get(void);
  244. #ifdef __cplusplus
  245. }
  246. #endif
  247. #endif // APP_UTIL_PLATFORM_H__
  248. /** @} */