port.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 V1.00:
  30. + Call to taskYIELD() from within tick ISR has been replaced by the more
  31. efficient portSWITCH_CONTEXT().
  32. + ISR function definitions renamed to include the prv prefix.
  33. Changes from V1.2.0:
  34. + portRESET_PIC() is now called last thing before the end of the preemptive
  35. tick routine.
  36. Changes from V2.6.1
  37. + Replaced the sUsingPreemption variable with the configUSE_PREEMPTION
  38. macro to be consistent with the later ports.
  39. */
  40. /*-----------------------------------------------------------
  41. * Implementation of functions defined in portable.h for the Flashlite 186
  42. * port.
  43. *----------------------------------------------------------*/
  44. #include <stdlib.h>
  45. #include <i86.h>
  46. #include <dos.h>
  47. #include <setjmp.h>
  48. #include "FreeRTOS.h"
  49. #include "task.h"
  50. #include "portasm.h"
  51. /*lint -e950 Non ANSI reserved words okay in this file only. */
  52. #define portTIMER_EOI_TYPE ( 8 )
  53. #define portRESET_PIC() portOUTPUT_WORD( ( uint16_t ) 0xff22, portTIMER_EOI_TYPE )
  54. #define portTIMER_INT_NUMBER 0x12
  55. #define portTIMER_1_CONTROL_REGISTER ( ( uint16_t ) 0xff5e )
  56. #define portTIMER_0_CONTROL_REGISTER ( ( uint16_t ) 0xff56 )
  57. #define portTIMER_INTERRUPT_ENABLE ( ( uint16_t ) 0x2000 )
  58. /* Setup the hardware to generate the required tick frequency. */
  59. static void prvSetTickFrequency( uint32_t ulTickRateHz );
  60. /* Set the hardware back to the state as per before the scheduler started. */
  61. static void prvExitFunction( void );
  62. #if configUSE_PREEMPTION == 1
  63. /* Tick service routine used by the scheduler when preemptive scheduling is
  64. being used. */
  65. static void __interrupt __far prvPreemptiveTick( void );
  66. #else
  67. /* Tick service routine used by the scheduler when cooperative scheduling is
  68. being used. */
  69. static void __interrupt __far prvNonPreemptiveTick( void );
  70. #endif
  71. /* Trap routine used by taskYIELD() to manually cause a context switch. */
  72. static void __interrupt __far prvYieldProcessor( void );
  73. /*lint -e956 File scopes necessary here. */
  74. /* Set true when the vectors are set so the scheduler will service the tick. */
  75. static int16_t sSchedulerRunning = pdFALSE;
  76. /* Points to the original routine installed on the vector we use for manual context switches. This is then used to restore the original routine during prvExitFunction(). */
  77. static void ( __interrupt __far *pxOldSwitchISR )();
  78. /* Used to restore the original DOS context when the scheduler is ended. */
  79. static jmp_buf xJumpBuf;
  80. /*lint +e956 */
  81. /*-----------------------------------------------------------*/
  82. BaseType_t xPortStartScheduler( void )
  83. {
  84. /* This is called with interrupts already disabled. */
  85. /* Remember what was on the interrupts we are going to use
  86. so we can put them back later if required. */
  87. pxOldSwitchISR = _dos_getvect( portSWITCH_INT_NUMBER );
  88. /* Put our manual switch (yield) function on a known
  89. vector. */
  90. _dos_setvect( portSWITCH_INT_NUMBER, prvYieldProcessor );
  91. #if configUSE_PREEMPTION == 1
  92. {
  93. /* Put our tick switch function on the timer interrupt. */
  94. _dos_setvect( portTIMER_INT_NUMBER, prvPreemptiveTick );
  95. }
  96. #else
  97. {
  98. /* We want the timer interrupt to just increment the tick count. */
  99. _dos_setvect( portTIMER_INT_NUMBER, prvNonPreemptiveTick );
  100. }
  101. #endif
  102. prvSetTickFrequency( configTICK_RATE_HZ );
  103. /* Clean up function if we want to return to DOS. */
  104. if( setjmp( xJumpBuf ) != 0 )
  105. {
  106. prvExitFunction();
  107. sSchedulerRunning = pdFALSE;
  108. }
  109. else
  110. {
  111. sSchedulerRunning = pdTRUE;
  112. /* Kick off the scheduler by setting up the context of the first task. */
  113. portFIRST_CONTEXT();
  114. }
  115. return sSchedulerRunning;
  116. }
  117. /*-----------------------------------------------------------*/
  118. /* The tick ISR used depend on whether or not the preemptive or cooperative
  119. kernel is being used. */
  120. #if configUSE_PREEMPTION == 1
  121. static void __interrupt __far prvPreemptiveTick( void )
  122. {
  123. /* Get the scheduler to update the task states following the tick. */
  124. if( xTaskIncrementTick() != pdFALSE )
  125. {
  126. /* Switch in the context of the next task to be run. */
  127. portSWITCH_CONTEXT();
  128. }
  129. /* Reset the PIC ready for the next time. */
  130. portRESET_PIC();
  131. }
  132. #else
  133. static void __interrupt __far prvNonPreemptiveTick( void )
  134. {
  135. /* Same as preemptive tick, but the cooperative scheduler is being used
  136. so we don't have to switch in the context of the next task. */
  137. xTaskIncrementTick();
  138. portRESET_PIC();
  139. }
  140. #endif
  141. /*-----------------------------------------------------------*/
  142. static void __interrupt __far prvYieldProcessor( void )
  143. {
  144. /* Switch in the context of the next task to be run. */
  145. portSWITCH_CONTEXT();
  146. }
  147. /*-----------------------------------------------------------*/
  148. void vPortEndScheduler( void )
  149. {
  150. /* Jump back to the processor state prior to starting the
  151. scheduler. This means we are not going to be using a
  152. task stack frame so the task can be deleted. */
  153. longjmp( xJumpBuf, 1 );
  154. }
  155. /*-----------------------------------------------------------*/
  156. static void prvExitFunction( void )
  157. {
  158. const uint16_t usTimerDisable = 0x0000;
  159. uint16_t usTimer0Control;
  160. /* Interrupts should be disabled here anyway - but no
  161. harm in making sure. */
  162. portDISABLE_INTERRUPTS();
  163. if( sSchedulerRunning == pdTRUE )
  164. {
  165. /* Put back the switch interrupt routines that was in place
  166. before the scheduler started. */
  167. _dos_setvect( portSWITCH_INT_NUMBER, pxOldSwitchISR );
  168. }
  169. /* Disable the timer used for the tick to ensure the scheduler is
  170. not called before restoring interrupts. There was previously nothing
  171. on this timer so there is no old ISR to restore. */
  172. portOUTPUT_WORD( portTIMER_1_CONTROL_REGISTER, usTimerDisable );
  173. /* Restart the DOS tick. */
  174. usTimer0Control = portINPUT_WORD( portTIMER_0_CONTROL_REGISTER );
  175. usTimer0Control |= portTIMER_INTERRUPT_ENABLE;
  176. portOUTPUT_WORD( portTIMER_0_CONTROL_REGISTER, usTimer0Control );
  177. portENABLE_INTERRUPTS();
  178. }
  179. /*-----------------------------------------------------------*/
  180. static void prvSetTickFrequency( uint32_t ulTickRateHz )
  181. {
  182. const uint16_t usMaxCountRegister = 0xff5a;
  183. const uint16_t usTimerPriorityRegister = 0xff32;
  184. const uint16_t usTimerEnable = 0xC000;
  185. const uint16_t usRetrigger = 0x0001;
  186. const uint16_t usTimerHighPriority = 0x0000;
  187. uint16_t usTimer0Control;
  188. /* ( CPU frequency / 4 ) / clock 2 max count [inpw( 0xff62 ) = 7] */
  189. const uint32_t ulClockFrequency = 0x7f31a0;
  190. uint32_t ulTimerCount = ulClockFrequency / ulTickRateHz;
  191. portOUTPUT_WORD( portTIMER_1_CONTROL_REGISTER, usTimerEnable | portTIMER_INTERRUPT_ENABLE | usRetrigger );
  192. portOUTPUT_WORD( usMaxCountRegister, ( uint16_t ) ulTimerCount );
  193. portOUTPUT_WORD( usTimerPriorityRegister, usTimerHighPriority );
  194. /* Stop the DOS tick - don't do this if you want to maintain a TOD clock. */
  195. usTimer0Control = portINPUT_WORD( portTIMER_0_CONTROL_REGISTER );
  196. usTimer0Control &= ~portTIMER_INTERRUPT_ENABLE;
  197. portOUTPUT_WORD( portTIMER_0_CONTROL_REGISTER, usTimer0Control );
  198. }
  199. /*lint +e950 */