port.c 9.3 KB

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