portmacro_cmsis.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * FreeRTOS Kernel V10.0.0
  3. * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. * this software and associated documentation files (the "Software"), to deal in
  7. * the Software without restriction, including without limitation the rights to
  8. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. * the Software, and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software. If you wish to use our Amazon
  14. * FreeRTOS name, please do so in a fair use way that does not cause confusion.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. * http://www.FreeRTOS.org
  24. * http://aws.amazon.com/freertos
  25. *
  26. * 1 tab == 4 spaces!
  27. */
  28. #ifndef PORTMACRO_CMSIS_H
  29. #define PORTMACRO_CMSIS_H
  30. #include "app_util.h"
  31. #include "nordic_common.h"
  32. #ifdef SOFTDEVICE_PRESENT
  33. #include "nrf_soc.h"
  34. #include "softdevice_handler.h"
  35. #include "app_error.h"
  36. #include "app_util_platform.h"
  37. #endif
  38. #ifdef __cplusplus
  39. extern "C" {
  40. #endif
  41. /*-----------------------------------------------------------
  42. * Port specific definitions.
  43. *
  44. * The settings in this file configure FreeRTOS correctly for the
  45. * given hardware and compiler.
  46. *
  47. * These settings should not be altered.
  48. *-----------------------------------------------------------
  49. */
  50. /* Type definitions. */
  51. #define portCHAR char
  52. #define portFLOAT float
  53. #define portDOUBLE double
  54. #define portLONG long
  55. #define portSHORT short
  56. #define portSTACK_TYPE uint32_t
  57. #define portBASE_TYPE long
  58. typedef portSTACK_TYPE StackType_t;
  59. typedef long BaseType_t;
  60. typedef uint32_t UBaseType_t; /* to provide compatibility with CMSIS */
  61. #if ( configUSE_16_BIT_TICKS == 1 )
  62. typedef uint16_t TickType_t;
  63. #define portMAX_DELAY ( TickType_t ) 0xffff
  64. #else
  65. typedef uint32_t TickType_t;
  66. #define portMAX_DELAY ( TickType_t ) 0xffffffffUL
  67. /* 32-bit tick type on a 32-bit architecture, so reads of the tick count do
  68. not need to be guarded with a critical section. */
  69. #define portTICK_TYPE_IS_ATOMIC 1
  70. #endif
  71. /*-----------------------------------------------------------*/
  72. /* Architecture specifics. */
  73. #define portSTACK_GROWTH ( -1 )
  74. #define portTICK_PERIOD_MS ( ( TickType_t ) 1000 / configTICK_RATE_HZ )
  75. #define portBYTE_ALIGNMENT 8
  76. /* RTC register */
  77. #define portNRF_RTC_REG NRF_RTC1
  78. /* IRQn used by the selected RTC */
  79. #define portNRF_RTC_IRQn RTC1_IRQn
  80. /* Constants required to manipulate the NVIC. */
  81. #define portNRF_RTC_PRESCALER ( (uint32_t) (ROUNDED_DIV(configSYSTICK_CLOCK_HZ, configTICK_RATE_HZ) - 1) )
  82. /* Maximum RTC ticks */
  83. #define portNRF_RTC_MAXTICKS ((1U<<24)-1U)
  84. /*-----------------------------------------------------------*/
  85. /* Internal auxiliary macro */
  86. #define portPendSVSchedule() do \
  87. { \
  88. /* Set a PendSV to request a context switch. */ \
  89. SCB->ICSR = SCB_ICSR_PENDSVSET_Msk; \
  90. __SEV(); \
  91. /* Barriers are normally not required but do ensure the code is completely \
  92. within the specified behaviour for the architecture. */ \
  93. __DSB(); \
  94. __ISB(); \
  95. }while (0)
  96. /* Scheduler utilities. */
  97. #define portYIELD() vPortTaskYield()
  98. #define portEND_SWITCHING_ISR( xSwitchRequired ) if ( (xSwitchRequired) != pdFALSE ) { portPendSVSchedule(); }
  99. #define portYIELD_FROM_ISR( x ) portEND_SWITCHING_ISR( x )
  100. /*-----------------------------------------------------------*/
  101. /* Critical section management. */
  102. extern void vPortEnterCritical( void );
  103. extern void vPortExitCritical( void );
  104. extern void vPortTaskYield( void );
  105. #define portSET_INTERRUPT_MASK_FROM_ISR() ulPortDisableISR()
  106. #define portCLEAR_INTERRUPT_MASK_FROM_ISR(x) vPortRestoreISR(x)
  107. #define portDISABLE_INTERRUPTS() vPortDisableISR()
  108. #define portENABLE_INTERRUPTS() vPortEnableISR()
  109. #define portENTER_CRITICAL() vPortEnterCritical()
  110. #define portEXIT_CRITICAL() vPortExitCritical()
  111. /*-----------------------------------------------------------*/
  112. /* Task function macros as described on the FreeRTOS.org WEB site. These are
  113. not necessary for to use this port. They are defined so the common demo files
  114. (which build with all the ports) will build. */
  115. #define portTASK_FUNCTION_PROTO( vFunction, pvParameters ) void vFunction( void *pvParameters )
  116. #define portTASK_FUNCTION( vFunction, pvParameters ) void vFunction( void *pvParameters )
  117. /*-----------------------------------------------------------*/
  118. /* Tickless idle/low power functionality. */
  119. #ifndef portSUPPRESS_TICKS_AND_SLEEP
  120. extern void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime );
  121. #define portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime ) vPortSuppressTicksAndSleep( xExpectedIdleTime )
  122. #endif
  123. /*-----------------------------------------------------------*/
  124. __STATIC_INLINE void vPortDisableISR(void)
  125. {
  126. #ifdef SOFTDEVICE_PRESENT
  127. uint8_t dummy = 0;
  128. uint32_t err_code = sd_nvic_critical_region_enter(&dummy);
  129. APP_ERROR_CHECK(err_code);
  130. #else
  131. __disable_irq();
  132. #endif
  133. }
  134. __STATIC_INLINE void vPortEnableISR(void)
  135. {
  136. #ifdef SOFTDEVICE_PRESENT
  137. uint32_t err_code = sd_nvic_critical_region_exit(0);
  138. APP_ERROR_CHECK(err_code);
  139. #else
  140. __enable_irq();
  141. #endif
  142. }
  143. /* Union used to remember current ISR state.
  144. * It concentrates information about ISR state and internal SD state into 32 bit variable. */
  145. typedef union
  146. {
  147. uint32_t u32; //< 32 bit access to the variable
  148. uint8_t sd_nested; //< Soft device nested element
  149. bool irq_nested; //< Global was disabled
  150. }portISRState_t;
  151. __STATIC_INLINE uint32_t ulPortDisableISR(void)
  152. {
  153. portISRState_t isrs = {0};
  154. #ifdef SOFTDEVICE_PRESENT
  155. uint32_t err_code = sd_nvic_critical_region_enter(&isrs.sd_nested);
  156. APP_ERROR_CHECK(err_code);
  157. #else
  158. uint32_t primask = __get_PRIMASK();
  159. if(primask == 0)
  160. {
  161. __disable_irq();
  162. }
  163. else
  164. {
  165. isrs.irq_nested = true;
  166. }
  167. #endif
  168. return isrs.u32;
  169. }
  170. __STATIC_INLINE void vPortRestoreISR(uint32_t state)
  171. {
  172. portISRState_t isrs = {state};
  173. #ifdef SOFTDEVICE_PRESENT
  174. uint32_t err_code = sd_nvic_critical_region_exit(isrs.sd_nested);
  175. APP_ERROR_CHECK(err_code);
  176. #else
  177. if(!isrs.irq_nested)
  178. {
  179. __enable_irq();
  180. }
  181. #endif
  182. }
  183. /*-----------------------------------------------------------*/
  184. #ifdef __cplusplus
  185. }
  186. #endif
  187. #endif /* PORTMACRO_CMSIS_H */