port.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /*
  2. * FreeRTOS Kernel V10.4.6
  3. * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * SPDX-License-Identifier: MIT
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  8. * this software and associated documentation files (the "Software"), to deal in
  9. * the Software without restriction, including without limitation the rights to
  10. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  11. * the Software, and to permit persons to whom the Software is furnished to do so,
  12. * subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in all
  15. * copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  19. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  20. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  21. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * https://www.FreeRTOS.org
  25. * https://github.com/FreeRTOS
  26. *
  27. */
  28. /*-----------------------------------------------------------
  29. * Implementation of functions defined in portable.h for the ST STR91x ARM9
  30. * port.
  31. *----------------------------------------------------------*/
  32. /* Library includes. */
  33. #include "91x_lib.h"
  34. /* Standard includes. */
  35. #include <stdlib.h>
  36. #include <assert.h>
  37. /* Scheduler includes. */
  38. #include "FreeRTOS.h"
  39. #include "task.h"
  40. #ifndef configUSE_WATCHDOG_TICK
  41. #error configUSE_WATCHDOG_TICK must be set to either 1 or 0 in FreeRTOSConfig.h to use either the Watchdog or timer 2 to generate the tick interrupt respectively.
  42. #endif
  43. /* Constants required to setup the initial stack. */
  44. #ifndef _RUN_TASK_IN_ARM_MODE_
  45. #define portINITIAL_SPSR ( ( StackType_t ) 0x3f ) /* System mode, THUMB mode, interrupts enabled. */
  46. #else
  47. #define portINITIAL_SPSR ( ( StackType_t ) 0x1f ) /* System mode, ARM mode, interrupts enabled. */
  48. #endif
  49. #define portINSTRUCTION_SIZE ( ( StackType_t ) 4 )
  50. /* Constants required to handle critical sections. */
  51. #define portNO_CRITICAL_NESTING ( ( uint32_t ) 0 )
  52. #ifndef abs
  53. #define abs(x) ((x)>0 ? (x) : -(x))
  54. #endif
  55. /**
  56. * Toggle a led using the following algorithm:
  57. * if ( GPIO_ReadBit(GPIO9, GPIO_Pin_2) )
  58. * {
  59. * GPIO_WriteBit( GPIO9, GPIO_Pin_2, Bit_RESET );
  60. * }
  61. * else
  62. * {
  63. * GPIO_WriteBit( GPIO9, GPIO_Pin_2, Bit_RESET );
  64. * }
  65. *
  66. */
  67. #define TOGGLE_LED(port,pin) \
  68. if ( ((((port)->DR[(pin)<<2])) & (pin)) != Bit_RESET ) \
  69. { \
  70. (port)->DR[(pin) <<2] = 0x00; \
  71. } \
  72. else \
  73. { \
  74. (port)->DR[(pin) <<2] = (pin); \
  75. }
  76. /*-----------------------------------------------------------*/
  77. /* Setup the watchdog to generate the tick interrupts. */
  78. static void prvSetupTimerInterrupt( void );
  79. /* ulCriticalNesting will get set to zero when the first task starts. It
  80. cannot be initialised to 0 as this will cause interrupts to be enabled
  81. during the kernel initialisation process. */
  82. uint32_t ulCriticalNesting = ( uint32_t ) 9999;
  83. /* Tick interrupt routines for cooperative and preemptive operation
  84. respectively. The preemptive version is not defined as __irq as it is called
  85. from an asm wrapper function. */
  86. void WDG_IRQHandler( void );
  87. /* VIC interrupt default handler. */
  88. static void prvDefaultHandler( void );
  89. #if configUSE_WATCHDOG_TICK == 0
  90. /* Used to update the OCR timer register */
  91. static u16 s_nPulseLength;
  92. #endif
  93. /*-----------------------------------------------------------*/
  94. /*
  95. * Initialise the stack of a task to look exactly as if a call to
  96. * portSAVE_CONTEXT had been called.
  97. *
  98. * See header file for description.
  99. */
  100. StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
  101. {
  102. StackType_t *pxOriginalTOS;
  103. pxOriginalTOS = pxTopOfStack;
  104. /* To ensure asserts in tasks.c don't fail, although in this case the assert
  105. is not really required. */
  106. pxTopOfStack--;
  107. /* Setup the initial stack of the task. The stack is set exactly as
  108. expected by the portRESTORE_CONTEXT() macro. */
  109. /* First on the stack is the return address - which in this case is the
  110. start of the task. The offset is added to make the return address appear
  111. as it would within an IRQ ISR. */
  112. *pxTopOfStack = ( StackType_t ) pxCode + portINSTRUCTION_SIZE;
  113. pxTopOfStack--;
  114. *pxTopOfStack = ( StackType_t ) 0xaaaaaaaa; /* R14 */
  115. pxTopOfStack--;
  116. *pxTopOfStack = ( StackType_t ) pxOriginalTOS; /* Stack used when task starts goes in R13. */
  117. pxTopOfStack--;
  118. *pxTopOfStack = ( StackType_t ) 0x12121212; /* R12 */
  119. pxTopOfStack--;
  120. *pxTopOfStack = ( StackType_t ) 0x11111111; /* R11 */
  121. pxTopOfStack--;
  122. *pxTopOfStack = ( StackType_t ) 0x10101010; /* R10 */
  123. pxTopOfStack--;
  124. *pxTopOfStack = ( StackType_t ) 0x09090909; /* R9 */
  125. pxTopOfStack--;
  126. *pxTopOfStack = ( StackType_t ) 0x08080808; /* R8 */
  127. pxTopOfStack--;
  128. *pxTopOfStack = ( StackType_t ) 0x07070707; /* R7 */
  129. pxTopOfStack--;
  130. *pxTopOfStack = ( StackType_t ) 0x06060606; /* R6 */
  131. pxTopOfStack--;
  132. *pxTopOfStack = ( StackType_t ) 0x05050505; /* R5 */
  133. pxTopOfStack--;
  134. *pxTopOfStack = ( StackType_t ) 0x04040404; /* R4 */
  135. pxTopOfStack--;
  136. *pxTopOfStack = ( StackType_t ) 0x03030303; /* R3 */
  137. pxTopOfStack--;
  138. *pxTopOfStack = ( StackType_t ) 0x02020202; /* R2 */
  139. pxTopOfStack--;
  140. *pxTopOfStack = ( StackType_t ) 0x01010101; /* R1 */
  141. pxTopOfStack--;
  142. /* When the task starts is will expect to find the function parameter in
  143. R0. */
  144. *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */
  145. pxTopOfStack--;
  146. /* The status register is set for system mode, with interrupts enabled. */
  147. *pxTopOfStack = ( StackType_t ) portINITIAL_SPSR;
  148. pxTopOfStack--;
  149. /* Interrupt flags cannot always be stored on the stack and will
  150. instead be stored in a variable, which is then saved as part of the
  151. tasks context. */
  152. *pxTopOfStack = portNO_CRITICAL_NESTING;
  153. return pxTopOfStack;
  154. }
  155. /*-----------------------------------------------------------*/
  156. BaseType_t xPortStartScheduler( void )
  157. {
  158. extern void vPortStartFirstTask( void );
  159. /* Start the timer that generates the tick ISR. Interrupts are disabled
  160. here already. */
  161. prvSetupTimerInterrupt();
  162. /* Start the first task. */
  163. vPortStartFirstTask();
  164. /* Should not get here! */
  165. return 0;
  166. }
  167. /*-----------------------------------------------------------*/
  168. void vPortEndScheduler( void )
  169. {
  170. /* It is unlikely that the ARM port will require this function as there
  171. is nothing to return to. */
  172. }
  173. /*-----------------------------------------------------------*/
  174. /* This function is called from an asm wrapper, so does not require the __irq
  175. keyword. */
  176. #if configUSE_WATCHDOG_TICK == 1
  177. static void prvFindFactors(u32 n, u16 *a, u32 *b)
  178. {
  179. /* This function is copied from the ST STR7 library and is
  180. copyright STMicroelectronics. Reproduced with permission. */
  181. u32 b0;
  182. u16 a0;
  183. int32_t err, err_min=n;
  184. *a = a0 = ((n-1)/65536ul) + 1;
  185. *b = b0 = n / *a;
  186. for (; *a <= 256; (*a)++)
  187. {
  188. *b = n / *a;
  189. err = (int32_t)*a * (int32_t)*b - (int32_t)n;
  190. if (abs(err) > (*a / 2))
  191. {
  192. (*b)++;
  193. err = (int32_t)*a * (int32_t)*b - (int32_t)n;
  194. }
  195. if (abs(err) < abs(err_min))
  196. {
  197. err_min = err;
  198. a0 = *a;
  199. b0 = *b;
  200. if (err == 0) break;
  201. }
  202. }
  203. *a = a0;
  204. *b = b0;
  205. }
  206. /*-----------------------------------------------------------*/
  207. static void prvSetupTimerInterrupt( void )
  208. {
  209. WDG_InitTypeDef xWdg;
  210. uint16_t a;
  211. uint32_t n = configCPU_PERIPH_HZ / configTICK_RATE_HZ, b;
  212. /* Configure the watchdog as a free running timer that generates a
  213. periodic interrupt. */
  214. SCU_APBPeriphClockConfig( __WDG, ENABLE );
  215. WDG_DeInit();
  216. WDG_StructInit(&xWdg);
  217. prvFindFactors( n, &a, &b );
  218. xWdg.WDG_Prescaler = a - 1;
  219. xWdg.WDG_Preload = b - 1;
  220. WDG_Init( &xWdg );
  221. WDG_ITConfig(ENABLE);
  222. /* Configure the VIC for the WDG interrupt. */
  223. VIC_Config( WDG_ITLine, VIC_IRQ, 10 );
  224. VIC_ITCmd( WDG_ITLine, ENABLE );
  225. /* Install the default handlers for both VIC's. */
  226. VIC0->DVAR = ( uint32_t ) prvDefaultHandler;
  227. VIC1->DVAR = ( uint32_t ) prvDefaultHandler;
  228. WDG_Cmd(ENABLE);
  229. }
  230. /*-----------------------------------------------------------*/
  231. void WDG_IRQHandler( void )
  232. {
  233. {
  234. /* Increment the tick counter. */
  235. if( xTaskIncrementTick() != pdFALSE )
  236. {
  237. /* Select a new task to execute. */
  238. vTaskSwitchContext();
  239. }
  240. /* Clear the interrupt in the watchdog. */
  241. WDG->SR &= ~0x0001;
  242. }
  243. }
  244. #else
  245. static void prvFindFactors(u32 n, u8 *a, u16 *b)
  246. {
  247. /* This function is copied from the ST STR7 library and is
  248. copyright STMicroelectronics. Reproduced with permission. */
  249. u16 b0;
  250. u8 a0;
  251. int32_t err, err_min=n;
  252. *a = a0 = ((n-1)/256) + 1;
  253. *b = b0 = n / *a;
  254. for (; *a <= 256; (*a)++)
  255. {
  256. *b = n / *a;
  257. err = (int32_t)*a * (int32_t)*b - (int32_t)n;
  258. if (abs(err) > (*a / 2))
  259. {
  260. (*b)++;
  261. err = (int32_t)*a * (int32_t)*b - (int32_t)n;
  262. }
  263. if (abs(err) < abs(err_min))
  264. {
  265. err_min = err;
  266. a0 = *a;
  267. b0 = *b;
  268. if (err == 0) break;
  269. }
  270. }
  271. *a = a0;
  272. *b = b0;
  273. }
  274. /*-----------------------------------------------------------*/
  275. static void prvSetupTimerInterrupt( void )
  276. {
  277. uint8_t a;
  278. uint16_t b;
  279. uint32_t n = configCPU_PERIPH_HZ / configTICK_RATE_HZ;
  280. TIM_InitTypeDef timer;
  281. SCU_APBPeriphClockConfig( __TIM23, ENABLE );
  282. TIM_DeInit(TIM2);
  283. TIM_StructInit(&timer);
  284. prvFindFactors( n, &a, &b );
  285. timer.TIM_Mode = TIM_OCM_CHANNEL_1;
  286. timer.TIM_OC1_Modes = TIM_TIMING;
  287. timer.TIM_Clock_Source = TIM_CLK_APB;
  288. timer.TIM_Clock_Edge = TIM_CLK_EDGE_RISING;
  289. timer.TIM_Prescaler = a-1;
  290. timer.TIM_Pulse_Level_1 = TIM_HIGH;
  291. timer.TIM_Pulse_Length_1 = s_nPulseLength = b-1;
  292. TIM_Init (TIM2, &timer);
  293. TIM_ITConfig(TIM2, TIM_IT_OC1, ENABLE);
  294. /* Configure the VIC for the WDG interrupt. */
  295. VIC_Config( TIM2_ITLine, VIC_IRQ, 10 );
  296. VIC_ITCmd( TIM2_ITLine, ENABLE );
  297. /* Install the default handlers for both VIC's. */
  298. VIC0->DVAR = ( uint32_t ) prvDefaultHandler;
  299. VIC1->DVAR = ( uint32_t ) prvDefaultHandler;
  300. TIM_CounterCmd(TIM2, TIM_CLEAR);
  301. TIM_CounterCmd(TIM2, TIM_START);
  302. }
  303. /*-----------------------------------------------------------*/
  304. void TIM2_IRQHandler( void )
  305. {
  306. /* Reset the timer counter to avioid overflow. */
  307. TIM2->OC1R += s_nPulseLength;
  308. /* Increment the tick counter. */
  309. if( xTaskIncrementTick() != pdFALSE )
  310. {
  311. /* Select a new task to run. */
  312. vTaskSwitchContext();
  313. }
  314. /* Clear the interrupt in the watchdog. */
  315. TIM2->SR &= ~TIM_FLAG_OC1;
  316. }
  317. #endif /* USE_WATCHDOG_TICK */
  318. /*-----------------------------------------------------------*/
  319. __arm __interwork void vPortEnterCritical( void )
  320. {
  321. /* Disable interrupts first! */
  322. portDISABLE_INTERRUPTS();
  323. /* Now interrupts are disabled ulCriticalNesting can be accessed
  324. directly. Increment ulCriticalNesting to keep a count of how many times
  325. portENTER_CRITICAL() has been called. */
  326. ulCriticalNesting++;
  327. }
  328. /*-----------------------------------------------------------*/
  329. __arm __interwork void vPortExitCritical( void )
  330. {
  331. if( ulCriticalNesting > portNO_CRITICAL_NESTING )
  332. {
  333. /* Decrement the nesting count as we are leaving a critical section. */
  334. ulCriticalNesting--;
  335. /* If the nesting level has reached zero then interrupts should be
  336. re-enabled. */
  337. if( ulCriticalNesting == portNO_CRITICAL_NESTING )
  338. {
  339. portENABLE_INTERRUPTS();
  340. }
  341. }
  342. }
  343. /*-----------------------------------------------------------*/
  344. static void prvDefaultHandler( void )
  345. {
  346. }