port.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. Changes from V3.2.1
  30. + CallReturn Depth increased from 8 to 10 levels to accomodate wizC/fedC V12.
  31. Changes from V3.2.0
  32. + TBLPTRU is now initialised to zero during the initial stack creation of a new task. This solves
  33. an error on devices with more than 64kB ROM.
  34. Changes from V3.0.0
  35. + ucCriticalNesting is now initialised to 0x7F to prevent interrupts from being
  36. handled before the scheduler is started.
  37. Changes from V3.0.1
  38. */
  39. /* Scheduler include files. */
  40. #include <FreeRTOS.h>
  41. #include <task.h>
  42. #include <malloc.h>
  43. /*---------------------------------------------------------------------------
  44. * Implementation of functions defined in portable.h for the WizC PIC18 port.
  45. *---------------------------------------------------------------------------*/
  46. /*
  47. * We require the address of the pxCurrentTCB variable, but don't want to
  48. * know any details of its type.
  49. */
  50. typedef void TCB_t;
  51. extern volatile TCB_t * volatile pxCurrentTCB;
  52. /*
  53. * Define minimal-stack constants
  54. * -----
  55. * FSR's:
  56. * STATUS, WREG, BSR, PRODH, PRODL, FSR0H, FSR0L,
  57. * FSR1H, FSR1L,TABLAT, (TBLPTRU), TBLPTRH, TBLPTRL,
  58. * (PCLATU), PCLATH
  59. * sfr's within parenthesis only on devices > 64kB
  60. * -----
  61. * Call/Return stack:
  62. * 2 bytes per entry on devices <= 64kB
  63. * 3 bytes per entry on devices > 64kB
  64. * -----
  65. * Other bytes:
  66. * 2 bytes: FunctionParameter for initial taskcode
  67. * 1 byte : Number of entries on call/return stack
  68. * 1 byte : ucCriticalNesting
  69. * 16 bytes: Free space on stack
  70. */
  71. #if _ROMSIZE > 0x8000
  72. #define portSTACK_FSR_BYTES ( 15 )
  73. #define portSTACK_CALLRETURN_ENTRY_SIZE ( 3 )
  74. #else
  75. #define portSTACK_FSR_BYTES ( 13 )
  76. #define portSTACK_CALLRETURN_ENTRY_SIZE ( 2 )
  77. #endif
  78. #define portSTACK_MINIMAL_CALLRETURN_DEPTH ( 10 )
  79. #define portSTACK_OTHER_BYTES ( 20 )
  80. uint16_t usCalcMinStackSize = 0;
  81. /*-----------------------------------------------------------*/
  82. /*
  83. * We initialise ucCriticalNesting to the middle value an
  84. * uint8_t can contain. This way portENTER_CRITICAL()
  85. * and portEXIT_CRITICAL() can be called without interrupts
  86. * being enabled before the scheduler starts.
  87. */
  88. register uint8_t ucCriticalNesting = 0x7F;
  89. /*-----------------------------------------------------------*/
  90. /*
  91. * Initialise the stack of a new task.
  92. * See portSAVE_CONTEXT macro for description.
  93. */
  94. StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
  95. {
  96. uint8_t ucScratch;
  97. /*
  98. * Get the size of the RAMarea in page 0 used by the compiler
  99. * We do this here already to avoid W-register conflicts.
  100. */
  101. _Pragma("asm")
  102. movlw OVERHEADPAGE0-LOCOPTSIZE+MAXLOCOPTSIZE
  103. movwf PRODL,ACCESS ; PRODL is used as temp register
  104. _Pragma("asmend")
  105. ucScratch = PRODL;
  106. /*
  107. * Place a few bytes of known values on the bottom of the stack.
  108. * This is just useful for debugging.
  109. */
  110. // *pxTopOfStack-- = 0x11;
  111. // *pxTopOfStack-- = 0x22;
  112. // *pxTopOfStack-- = 0x33;
  113. /*
  114. * Simulate how the stack would look after a call to vPortYield()
  115. * generated by the compiler.
  116. */
  117. /*
  118. * First store the function parameters. This is where the task expects
  119. * to find them when it starts running.
  120. */
  121. *pxTopOfStack-- = ( StackType_t ) ( (( uint16_t ) pvParameters >> 8) & 0x00ff );
  122. *pxTopOfStack-- = ( StackType_t ) ( ( uint16_t ) pvParameters & 0x00ff );
  123. /*
  124. * Next are all the registers that form part of the task context.
  125. */
  126. *pxTopOfStack-- = ( StackType_t ) 0x11; /* STATUS. */
  127. *pxTopOfStack-- = ( StackType_t ) 0x22; /* WREG. */
  128. *pxTopOfStack-- = ( StackType_t ) 0x33; /* BSR. */
  129. *pxTopOfStack-- = ( StackType_t ) 0x44; /* PRODH. */
  130. *pxTopOfStack-- = ( StackType_t ) 0x55; /* PRODL. */
  131. *pxTopOfStack-- = ( StackType_t ) 0x66; /* FSR0H. */
  132. *pxTopOfStack-- = ( StackType_t ) 0x77; /* FSR0L. */
  133. *pxTopOfStack-- = ( StackType_t ) 0x88; /* FSR1H. */
  134. *pxTopOfStack-- = ( StackType_t ) 0x99; /* FSR1L. */
  135. *pxTopOfStack-- = ( StackType_t ) 0xAA; /* TABLAT. */
  136. #if _ROMSIZE > 0x8000
  137. *pxTopOfStack-- = ( StackType_t ) 0x00; /* TBLPTRU. */
  138. #endif
  139. *pxTopOfStack-- = ( StackType_t ) 0xCC; /* TBLPTRH. */
  140. *pxTopOfStack-- = ( StackType_t ) 0xDD; /* TBLPTRL. */
  141. #if _ROMSIZE > 0x8000
  142. *pxTopOfStack-- = ( StackType_t ) 0xEE; /* PCLATU. */
  143. #endif
  144. *pxTopOfStack-- = ( StackType_t ) 0xFF; /* PCLATH. */
  145. /*
  146. * Next the compiler's scratchspace.
  147. */
  148. while(ucScratch-- > 0)
  149. {
  150. *pxTopOfStack-- = ( StackType_t ) 0;
  151. }
  152. /*
  153. * The only function return address so far is the address of the task entry.
  154. * The order is TOSU/TOSH/TOSL. For devices > 64kB, TOSU is put on the
  155. * stack, too. TOSU is always written as zero here because wizC does not allow
  156. * functionpointers to point above 64kB in ROM.
  157. */
  158. #if _ROMSIZE > 0x8000
  159. *pxTopOfStack-- = ( StackType_t ) 0;
  160. #endif
  161. *pxTopOfStack-- = ( StackType_t ) ( ( ( uint16_t ) pxCode >> 8 ) & 0x00ff );
  162. *pxTopOfStack-- = ( StackType_t ) ( ( uint16_t ) pxCode & 0x00ff );
  163. /*
  164. * Store the number of return addresses on the hardware stack.
  165. * So far only the address of the task entry point.
  166. */
  167. *pxTopOfStack-- = ( StackType_t ) 1;
  168. /*
  169. * The code generated by wizC does not maintain separate
  170. * stack and frame pointers. Therefore the portENTER_CRITICAL macro cannot
  171. * use the stack as per other ports. Instead a variable is used to keep
  172. * track of the critical section nesting. This variable has to be stored
  173. * as part of the task context and is initially set to zero.
  174. */
  175. *pxTopOfStack-- = ( StackType_t ) portNO_CRITICAL_SECTION_NESTING;
  176. return pxTopOfStack;
  177. }
  178. /*-----------------------------------------------------------*/
  179. uint16_t usPortCALCULATE_MINIMAL_STACK_SIZE( void )
  180. {
  181. /*
  182. * Fetch the size of compiler's scratchspace.
  183. */
  184. _Pragma("asm")
  185. movlw OVERHEADPAGE0-LOCOPTSIZE+MAXLOCOPTSIZE
  186. movlb usCalcMinStackSize>>8
  187. movwf usCalcMinStackSize,BANKED
  188. _Pragma("asmend")
  189. /*
  190. * Add minimum needed stackspace
  191. */
  192. usCalcMinStackSize += ( portSTACK_FSR_BYTES )
  193. + ( portSTACK_MINIMAL_CALLRETURN_DEPTH * portSTACK_CALLRETURN_ENTRY_SIZE )
  194. + ( portSTACK_OTHER_BYTES );
  195. return(usCalcMinStackSize);
  196. }
  197. /*-----------------------------------------------------------*/
  198. BaseType_t xPortStartScheduler( void )
  199. {
  200. extern void portSetupTick( void );
  201. /*
  202. * Setup a timer for the tick ISR for the preemptive scheduler.
  203. */
  204. portSetupTick();
  205. /*
  206. * Restore the context of the first task to run.
  207. */
  208. portRESTORE_CONTEXT();
  209. /*
  210. * This point should never be reached during execution.
  211. */
  212. return pdTRUE;
  213. }
  214. /*-----------------------------------------------------------*/
  215. void vPortEndScheduler( void )
  216. {
  217. /*
  218. * It is unlikely that the scheduler for the PIC port will get stopped
  219. * once running. When called a reset is done which is probably the
  220. * most valid action.
  221. */
  222. _Pragma(asmline reset);
  223. }
  224. /*-----------------------------------------------------------*/
  225. /*
  226. * Manual context switch. This is similar to the tick context switch,
  227. * but does not increment the tick count. It must be identical to the
  228. * tick context switch in how it stores the stack of a task.
  229. */
  230. void vPortYield( void )
  231. {
  232. /*
  233. * Save the context of the current task.
  234. */
  235. portSAVE_CONTEXT( portINTERRUPTS_UNCHANGED );
  236. /*
  237. * Switch to the highest priority task that is ready to run.
  238. */
  239. vTaskSwitchContext();
  240. /*
  241. * Start executing the task we have just switched to.
  242. */
  243. portRESTORE_CONTEXT();
  244. }
  245. /*-----------------------------------------------------------*/
  246. #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
  247. void *pvPortMalloc( uint16_t usWantedSize )
  248. {
  249. void *pvReturn;
  250. vTaskSuspendAll();
  251. {
  252. pvReturn = malloc( ( malloc_t ) usWantedSize );
  253. }
  254. xTaskResumeAll();
  255. return pvReturn;
  256. }
  257. #endif /* configSUPPORT_STATIC_ALLOCATION */
  258. /*-----------------------------------------------------------*/
  259. #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
  260. void vPortFree( void *pv )
  261. {
  262. if( pv )
  263. {
  264. vTaskSuspendAll();
  265. {
  266. free( pv );
  267. }
  268. xTaskResumeAll();
  269. }
  270. }
  271. #endif /* configSUPPORT_DYNAMIC_ALLOCATION */