port.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. + prvPortResetPIC() is now called last thing before the end of the
  35. preemptive 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. Changes from V4.0.1
  40. + Add function prvSetTickFrequencyDefault() to set the DOS tick back to
  41. its proper value when the scheduler exits.
  42. */
  43. #include <stdlib.h>
  44. #include <stdio.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. /*-----------------------------------------------------------
  52. * Implementation of functions defined in portable.h for the industrial
  53. * PC port.
  54. *----------------------------------------------------------*/
  55. /*lint -e950 Non ANSI reserved words okay in this file only. */
  56. #define portTIMER_INT_NUMBER 0x08
  57. /* Setup hardware for required tick interrupt rate. */
  58. static void prvSetTickFrequency( uint32_t ulTickRateHz );
  59. /* Restore hardware to as it was prior to starting the scheduler. */
  60. static void prvExitFunction( void );
  61. /* Either chain to the DOS tick (which itself clears the PIC) or clear the PIC
  62. directly. We chain to the DOS tick as close as possible to the standard DOS
  63. tick rate. */
  64. static void prvPortResetPIC( void );
  65. /* The tick ISR used depends on whether the preemptive or cooperative scheduler
  66. is being used. */
  67. #if configUSE_PREEMPTION == 1
  68. /* Tick service routine used by the scheduler when preemptive scheduling is
  69. being used. */
  70. static void __interrupt __far prvPreemptiveTick( void );
  71. #else
  72. /* Tick service routine used by the scheduler when cooperative scheduling is
  73. being used. */
  74. static void __interrupt __far prvNonPreemptiveTick( void );
  75. #endif
  76. /* Trap routine used by taskYIELD() to manually cause a context switch. */
  77. static void __interrupt __far prvYieldProcessor( void );
  78. /* Set the tick frequency back so the floppy drive works correctly when the
  79. scheduler exits. */
  80. static void prvSetTickFrequencyDefault( void );
  81. /*lint -e956 File scopes necessary here. */
  82. /* Used to signal when to chain to the DOS tick, and when to just clear the PIC ourselves. */
  83. static int16_t sDOSTickCounter;
  84. /* Set true when the vectors are set so the scheduler will service the tick. */
  85. static int16_t sSchedulerRunning = pdFALSE;
  86. /* 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(). */
  87. static void ( __interrupt __far *pxOldSwitchISR )();
  88. /* Points to the original routine installed on the vector we use to chain to the DOS tick. This is then used to restore the original routine during prvExitFunction(). */
  89. static void ( __interrupt __far *pxOldSwitchISRPlus1 )();
  90. /* Used to restore the original DOS context when the scheduler is ended. */
  91. static jmp_buf xJumpBuf;
  92. /*lint +e956 */
  93. /*-----------------------------------------------------------*/
  94. BaseType_t xPortStartScheduler( void )
  95. {
  96. pxISR pxOriginalTickISR;
  97. /* This is called with interrupts already disabled. */
  98. /* Remember what was on the interrupts we are going to use
  99. so we can put them back later if required. */
  100. pxOldSwitchISR = _dos_getvect( portSWITCH_INT_NUMBER );
  101. pxOriginalTickISR = _dos_getvect( portTIMER_INT_NUMBER );
  102. pxOldSwitchISRPlus1 = _dos_getvect( portSWITCH_INT_NUMBER + 1 );
  103. prvSetTickFrequency( configTICK_RATE_HZ );
  104. /* Put our manual switch (yield) function on a known
  105. vector. */
  106. _dos_setvect( portSWITCH_INT_NUMBER, prvYieldProcessor );
  107. /* Put the old tick on a different interrupt number so we can
  108. call it when we want. */
  109. _dos_setvect( portSWITCH_INT_NUMBER + 1, pxOriginalTickISR );
  110. #if configUSE_PREEMPTION == 1
  111. {
  112. /* Put our tick switch function on the timer interrupt. */
  113. _dos_setvect( portTIMER_INT_NUMBER, prvPreemptiveTick );
  114. }
  115. #else
  116. {
  117. /* We want the timer interrupt to just increment the tick count. */
  118. _dos_setvect( portTIMER_INT_NUMBER, prvNonPreemptiveTick );
  119. }
  120. #endif
  121. /* Setup a counter that is used to call the DOS interrupt as close
  122. to it's original frequency as can be achieved given our chosen tick
  123. frequency. */
  124. sDOSTickCounter = portTICKS_PER_DOS_TICK;
  125. /* Clean up function if we want to return to DOS. */
  126. if( setjmp( xJumpBuf ) != 0 )
  127. {
  128. prvExitFunction();
  129. sSchedulerRunning = pdFALSE;
  130. }
  131. else
  132. {
  133. sSchedulerRunning = pdTRUE;
  134. /* Kick off the scheduler by setting up the context of the first task. */
  135. portFIRST_CONTEXT();
  136. }
  137. return sSchedulerRunning;
  138. }
  139. /*-----------------------------------------------------------*/
  140. /* The tick ISR used depends on whether the preemptive or cooperative scheduler
  141. is being used. */
  142. #if configUSE_PREEMPTION == 1
  143. /* Tick service routine used by the scheduler when preemptive scheduling is
  144. being used. */
  145. static void __interrupt __far prvPreemptiveTick( void )
  146. {
  147. /* Get the scheduler to update the task states following the tick. */
  148. if( xTaskIncrementTick() != pdFALSE )
  149. {
  150. /* Switch in the context of the next task to be run. */
  151. portSWITCH_CONTEXT();
  152. }
  153. /* Reset the PIC ready for the next time. */
  154. prvPortResetPIC();
  155. }
  156. #else
  157. static void __interrupt __far prvNonPreemptiveTick( void )
  158. {
  159. /* Same as preemptive tick, but the cooperative scheduler is being used
  160. so we don't have to switch in the context of the next task. */
  161. xTaskIncrementTick();
  162. prvPortResetPIC();
  163. }
  164. #endif
  165. /*-----------------------------------------------------------*/
  166. static void __interrupt __far prvYieldProcessor( void )
  167. {
  168. /* Switch in the context of the next task to be run. */
  169. portSWITCH_CONTEXT();
  170. }
  171. /*-----------------------------------------------------------*/
  172. static void prvPortResetPIC( void )
  173. {
  174. /* We are going to call the DOS tick interrupt at as close a
  175. frequency to the normal DOS tick as possible. */
  176. /* WE SHOULD NOT DO THIS IF YIELD WAS CALLED. */
  177. --sDOSTickCounter;
  178. if( sDOSTickCounter <= 0 )
  179. {
  180. sDOSTickCounter = ( int16_t ) portTICKS_PER_DOS_TICK;
  181. __asm{ int portSWITCH_INT_NUMBER + 1 };
  182. }
  183. else
  184. {
  185. /* Reset the PIC as the DOS tick is not being called to
  186. do it. */
  187. __asm
  188. {
  189. mov al, 20H
  190. out 20H, al
  191. };
  192. }
  193. }
  194. /*-----------------------------------------------------------*/
  195. void vPortEndScheduler( void )
  196. {
  197. /* Jump back to the processor state prior to starting the
  198. scheduler. This means we are not going to be using a
  199. task stack frame so the task can be deleted. */
  200. longjmp( xJumpBuf, 1 );
  201. }
  202. /*-----------------------------------------------------------*/
  203. static void prvExitFunction( void )
  204. {
  205. void ( __interrupt __far *pxOriginalTickISR )();
  206. /* Interrupts should be disabled here anyway - but no
  207. harm in making sure. */
  208. portDISABLE_INTERRUPTS();
  209. if( sSchedulerRunning == pdTRUE )
  210. {
  211. /* Set the DOS tick back onto the timer ticker. */
  212. pxOriginalTickISR = _dos_getvect( portSWITCH_INT_NUMBER + 1 );
  213. _dos_setvect( portTIMER_INT_NUMBER, pxOriginalTickISR );
  214. prvSetTickFrequencyDefault();
  215. /* Put back the switch interrupt routines that was in place
  216. before the scheduler started. */
  217. _dos_setvect( portSWITCH_INT_NUMBER, pxOldSwitchISR );
  218. _dos_setvect( portSWITCH_INT_NUMBER + 1, pxOldSwitchISRPlus1 );
  219. }
  220. /* The tick timer is back how DOS wants it. We can re-enable
  221. interrupts without the scheduler being called. */
  222. portENABLE_INTERRUPTS();
  223. }
  224. /*-----------------------------------------------------------*/
  225. static void prvSetTickFrequency( uint32_t ulTickRateHz )
  226. {
  227. const uint16_t usPIT_MODE = ( uint16_t ) 0x43;
  228. const uint16_t usPIT0 = ( uint16_t ) 0x40;
  229. const uint32_t ulPIT_CONST = ( uint32_t ) 1193180;
  230. const uint16_t us8254_CTR0_MODE3 = ( uint16_t ) 0x36;
  231. uint32_t ulOutput;
  232. /* Setup the 8245 to tick at the wanted frequency. */
  233. portOUTPUT_BYTE( usPIT_MODE, us8254_CTR0_MODE3 );
  234. ulOutput = ulPIT_CONST / ulTickRateHz;
  235. portOUTPUT_BYTE( usPIT0, ( uint16_t )( ulOutput & ( uint32_t ) 0xff ) );
  236. ulOutput >>= 8;
  237. portOUTPUT_BYTE( usPIT0, ( uint16_t ) ( ulOutput & ( uint32_t ) 0xff ) );
  238. }
  239. /*-----------------------------------------------------------*/
  240. static void prvSetTickFrequencyDefault( void )
  241. {
  242. const uint16_t usPIT_MODE = ( uint16_t ) 0x43;
  243. const uint16_t usPIT0 = ( uint16_t ) 0x40;
  244. const uint16_t us8254_CTR0_MODE3 = ( uint16_t ) 0x36;
  245. portOUTPUT_BYTE( usPIT_MODE, us8254_CTR0_MODE3 );
  246. portOUTPUT_BYTE( usPIT0,0 );
  247. portOUTPUT_BYTE( usPIT0,0 );
  248. }
  249. /*lint +e950 */