RTX_Conf_CM.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*----------------------------------------------------------------------------
  2. * RL-ARM - RTX
  3. *----------------------------------------------------------------------------
  4. * Name: RTX_Conf_CM.C
  5. * Purpose: Configuration of CMSIS RTX Kernel for Cortex-M
  6. * Rev.: V4.74
  7. *----------------------------------------------------------------------------
  8. *
  9. * Copyright (c) 1999-2009 KEIL, 2009-2013 ARM Germany GmbH
  10. * All rights reserved.
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. * - Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * - Neither the name of ARM nor the names of its contributors may be used
  19. * to endorse or promote products derived from this software without
  20. * specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *---------------------------------------------------------------------------*/
  34. #include "cmsis_os.h"
  35. #include "nrf.h"
  36. /*----------------------------------------------------------------------------
  37. * RTX User configuration part BEGIN
  38. *---------------------------------------------------------------------------*/
  39. //-------- <<< Use Configuration Wizard in Context Menu >>> -----------------
  40. //
  41. // <h>Thread Configuration
  42. // =======================
  43. //
  44. // <o>Number of concurrent running user threads <1-250>
  45. // <i> Defines max. number of user threads that will run at the same time.
  46. // <i> Default: 6
  47. #ifndef OS_TASKCNT
  48. #define OS_TASKCNT 3
  49. #endif
  50. // <o>Default Thread stack size [bytes] <64-4096:8><#/4>
  51. // <i> Defines default stack size for threads with osThreadDef stacksz = 0
  52. // <i> Default: 200
  53. #ifndef OS_STKSIZE
  54. #define OS_STKSIZE 50
  55. #endif
  56. // <o>Main Thread stack size [bytes] <64-32768:8><#/4>
  57. // <i> Defines stack size for main thread.
  58. // <i> Default: 200
  59. #ifndef OS_MAINSTKSIZE
  60. #define OS_MAINSTKSIZE 250
  61. #endif
  62. // <o>Number of threads with user-provided stack size <0-250>
  63. // <i> Defines the number of threads with user-provided stack size.
  64. // <i> Default: 0
  65. #ifndef OS_PRIVCNT
  66. #define OS_PRIVCNT 0
  67. #endif
  68. // <o>Total stack size [bytes] for threads with user-provided stack size <0-1048576:8><#/4>
  69. // <i> Defines the combined stack size for threads with user-provided stack size.
  70. // <i> Default: 0
  71. #ifndef OS_PRIVSTKSIZE
  72. #define OS_PRIVSTKSIZE 0
  73. #endif
  74. // <q>Check for stack overflow
  75. // <i> Includes the stack checking code for stack overflow.
  76. // <i> Note that additional code reduces the Kernel performance.
  77. #ifndef OS_STKCHECK
  78. #define OS_STKCHECK 1
  79. #endif
  80. // <o>Processor mode for thread execution
  81. // <0=> Unprivileged mode
  82. // <1=> Privileged mode
  83. // <i> Default: Privileged mode
  84. #ifndef OS_RUNPRIV
  85. #define OS_RUNPRIV 1
  86. #endif
  87. // </h>
  88. // <h>RTX Kernel Timer Tick Configuration
  89. // ======================================
  90. // <q> Use Cortex-M SysTick timer as RTX Kernel Timer
  91. // <i> Cortex-M processors provide in most cases a SysTick timer that can be used as
  92. // <i> as time-base for RTX.
  93. #ifndef OS_SYSTICK
  94. #define OS_SYSTICK 0
  95. #endif
  96. //
  97. // <o>RTOS Kernel Timer input clock frequency [Hz] <1-1000000000>
  98. // <i> Defines the input frequency of the RTOS Kernel Timer.
  99. // <i> When the Cortex-M SysTick timer is used, the input clock
  100. // <i> is on most systems identical with the core clock.
  101. #ifndef OS_CLOCK
  102. #define OS_CLOCK 32768
  103. #endif
  104. // <o>RTX Timer tick interval value [us] <1-1000000>
  105. // <i> The RTX Timer tick interval value is used to calculate timeout values.
  106. // <i> When the Cortex-M SysTick timer is enabled, the value also configures the SysTick timer.
  107. // <i> Default: 1000 (1ms)
  108. #ifndef OS_TICK
  109. #define OS_TICK 1000
  110. #endif
  111. // </h>
  112. // <h>System Configuration
  113. // =======================
  114. //
  115. // <e>Round-Robin Thread switching
  116. // ===============================
  117. //
  118. // <i> Enables Round-Robin Thread switching.
  119. #ifndef OS_ROBIN
  120. #define OS_ROBIN 1
  121. #endif
  122. // <o>Round-Robin Timeout [ticks] <1-1000>
  123. // <i> Defines how long a thread will execute before a thread switch.
  124. // <i> Default: 5
  125. #ifndef OS_ROBINTOUT
  126. #define OS_ROBINTOUT 5
  127. #endif
  128. // </e>
  129. // <e>User Timers
  130. // ==============
  131. // <i> Enables user Timers
  132. #ifndef OS_TIMERS
  133. #define OS_TIMERS 1
  134. #endif
  135. // <o>Timer Thread Priority
  136. // <1=> Low
  137. // <2=> Below Normal <3=> Normal <4=> Above Normal
  138. // <5=> High
  139. // <6=> Realtime (highest)
  140. // <i> Defines priority for Timer Thread
  141. // <i> Default: High
  142. #ifndef OS_TIMERPRIO
  143. #define OS_TIMERPRIO 5
  144. #endif
  145. // <o>Timer Thread stack size [bytes] <64-4096:8><#/4>
  146. // <i> Defines stack size for Timer thread.
  147. // <i> Default: 200
  148. #ifndef OS_TIMERSTKSZ
  149. #define OS_TIMERSTKSZ 200
  150. #endif
  151. // <o>Timer Callback Queue size <1-32>
  152. // <i> Number of concurrent active timer callback functions.
  153. // <i> Default: 4
  154. #ifndef OS_TIMERCBQS
  155. #define OS_TIMERCBQS 8
  156. #endif
  157. // </e>
  158. // <o>ISR FIFO Queue size<4=> 4 entries <8=> 8 entries
  159. // <12=> 12 entries <16=> 16 entries
  160. // <24=> 24 entries <32=> 32 entries
  161. // <48=> 48 entries <64=> 64 entries
  162. // <96=> 96 entries
  163. // <i> ISR functions store requests to this buffer,
  164. // <i> when they are called from the interrupt handler.
  165. // <i> Default: 16 entries
  166. #ifndef OS_FIFOSZ
  167. #define OS_FIFOSZ 16
  168. #endif
  169. // </h>
  170. //------------- <<< end of configuration section >>> -----------------------
  171. // Standard library system mutexes
  172. // ===============================
  173. // Define max. number system mutexes that are used to protect
  174. // the arm standard runtime library. For microlib they are not used.
  175. #ifndef OS_MUTEXCNT
  176. #define OS_MUTEXCNT 8
  177. #endif
  178. /*----------------------------------------------------------------------------
  179. * RTX User configuration part END
  180. *---------------------------------------------------------------------------*/
  181. #define OS_TRV ((uint32_t)(((double)OS_CLOCK*(double)OS_TICK)/1E6+0.5)-1)
  182. /*----------------------------------------------------------------------------
  183. * Global Functions
  184. *---------------------------------------------------------------------------*/
  185. /*--------------------------- os_idle_demon ---------------------------------*/
  186. #define TIMER_MASK 0xFFFFFF
  187. volatile unsigned int rtos_suspend;
  188. void os_idle_demon (void)
  189. {
  190. unsigned int expected_time;
  191. unsigned int prev_time;
  192. NVIC_SetPriority(PendSV_IRQn, NVIC_GetPriority(RTC1_IRQn));
  193. for (;; )
  194. {
  195. rtos_suspend = 1;
  196. expected_time = os_suspend();
  197. expected_time &= TIMER_MASK;
  198. if (expected_time > 2)
  199. {
  200. prev_time = NRF_RTC1->COUNTER;
  201. expected_time += prev_time;
  202. NRF_RTC1->CC[0] =
  203. (expected_time > TIMER_MASK) ? expected_time - TIMER_MASK : expected_time;
  204. NRF_RTC1->INTENCLR = RTC_INTENSET_TICK_Msk;
  205. NVIC_EnableIRQ(RTC1_IRQn);
  206. __disable_irq();
  207. if (rtos_suspend)
  208. {
  209. NRF_RTC1->INTENSET = RTC_INTENSET_COMPARE0_Msk;
  210. __WFI();
  211. NRF_RTC1->EVENTS_COMPARE[0] = 0;
  212. NRF_RTC1->INTENCLR = RTC_INTENSET_COMPARE0_Msk;
  213. }
  214. __enable_irq();
  215. NRF_RTC1->INTENSET = RTC_INTENSET_TICK_Msk;
  216. expected_time = NRF_RTC1->COUNTER;
  217. expected_time = (expected_time >= prev_time)
  218. ? expected_time - prev_time : TIMER_MASK - prev_time + expected_time;
  219. }
  220. os_resume(expected_time);
  221. }
  222. }
  223. #if (OS_SYSTICK == 0) // Functions for alternative timer as RTX kernel timer
  224. /*--------------------------- os_tick_init ----------------------------------*/
  225. // Initialize alternative hardware timer as RTX kernel timer
  226. // Return: IRQ number of the alternative hardware timer
  227. int os_tick_init (void)
  228. {
  229. NRF_CLOCK->LFCLKSRC = (CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos);
  230. NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
  231. NRF_CLOCK->TASKS_LFCLKSTART = 1;
  232. while (NRF_CLOCK->EVENTS_LFCLKSTARTED == 0)
  233. {
  234. // Do nothing.
  235. }
  236. //set the same level as svc calls
  237. NVIC_SetPriority(RTC1_IRQn, 2);
  238. NRF_RTC1->PRESCALER = OS_TRV;
  239. NRF_RTC1->INTENSET = RTC_INTENSET_TICK_Msk;
  240. NRF_RTC1->TASKS_START = 1;
  241. return (RTC1_IRQn);
  242. }
  243. /*--------------------------- os_tick_val -----------------------------------*/
  244. // Get alternative hardware timer current value (0 .. OS_TRV)
  245. uint32_t os_tick_val(void)
  246. {
  247. return NRF_RTC1->COUNTER;
  248. }
  249. /*--------------------------- os_tick_ovf -----------------------------------*/
  250. // Get alternative hardware timer overflow flag
  251. // Return: 1 - overflow, 0 - no overflow
  252. uint32_t os_tick_ovf(void)
  253. {
  254. return NRF_RTC1->EVENTS_OVRFLW;
  255. }
  256. /*--------------------------- os_tick_irqack --------------------------------*/
  257. // Acknowledge alternative hardware timer interrupt
  258. void os_tick_irqack(void)
  259. {
  260. if ((NRF_RTC1->EVENTS_TICK != 0) &&
  261. ((NRF_RTC1->INTENSET & RTC_INTENSET_TICK_Msk) != 0))
  262. {
  263. NRF_RTC1->EVENTS_TICK = 0;
  264. }
  265. }
  266. // This branch a problem with reurn from interrput.
  267. #if defined (__CC_ARM) /* ARM Compiler */
  268. __asm __declspec(noreturn) void RTC1_IRQHandler(void)
  269. {
  270. EXTERN OS_Tick_Handler
  271. BL OS_Tick_Handler
  272. }
  273. #elif defined (__ICCARM__) /* IAR Compiler */
  274. #error "IAR no supported yet."
  275. #elif defined (__GNUC__) /* GNU Compiler */
  276. extern void OS_Tick_Handler(void);
  277. void RTC1_IRQHandler()
  278. __attribute__((naked));
  279. void RTC1_IRQHandler()
  280. {
  281. OS_Tick_Handler();
  282. }
  283. #else
  284. #error "Unknown compiler! Don't know how to create SVC function."
  285. #endif
  286. #endif // (OS_SYSTICK == 0)
  287. /*--------------------------- os_error --------------------------------------*/
  288. /* OS Error Codes */
  289. #define OS_ERROR_STACK_OVF 1
  290. #define OS_ERROR_FIFO_OVF 2
  291. #define OS_ERROR_MBX_OVF 3
  292. extern osThreadId svcThreadGetId (void);
  293. void os_error(uint32_t error_code) {
  294. /* This function is called when a runtime error is detected. */
  295. /* Parameter 'error_code' holds the runtime error code. */
  296. /* HERE: include optional code to be executed on runtime error. */
  297. switch (error_code) {
  298. case OS_ERROR_STACK_OVF:
  299. /* Stack overflow detected for the currently running task. */
  300. /* Thread can be identified by calling svcThreadGetId(). */
  301. break;
  302. case OS_ERROR_FIFO_OVF:
  303. /* ISR FIFO Queue buffer overflow detected. */
  304. break;
  305. case OS_ERROR_MBX_OVF:
  306. /* Mailbox overflow detected. */
  307. break;
  308. }
  309. for (;;);
  310. }
  311. /*----------------------------------------------------------------------------
  312. * RTX Configuration Functions
  313. *---------------------------------------------------------------------------*/
  314. #include "RTX_CM_lib.h"
  315. /*----------------------------------------------------------------------------
  316. * end of file
  317. *---------------------------------------------------------------------------*/