]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/BCC/16BitDOS/Flsh186/port.c
4e9ffd2f20bf7a2f5eae62ce48f8d1742b3dbb48
[freertos] / FreeRTOS / Source / portable / BCC / 16BitDOS / Flsh186 / port.c
1 /*\r
2  * FreeRTOS Kernel V10.2.0\r
3  * Copyright (C) 2019 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /*\r
29 Changes from V1.00:\r
30 \r
31         + Call to taskYIELD() from within tick ISR has been replaced by the more\r
32           efficient portSWITCH_CONTEXT().\r
33         + ISR function definitions renamed to include the prv prefix.\r
34 \r
35 Changes from V2.6.1\r
36 \r
37         + Replaced the sUsingPreemption variable with the configUSE_PREEMPTION\r
38           macro to be consistent with the later ports.\r
39 */\r
40 \r
41 /*-----------------------------------------------------------\r
42  * Implementation of functions defined in portable.h for the Flashlite 186\r
43  * port.\r
44  *----------------------------------------------------------*/\r
45 \r
46 #include <dos.h>\r
47 #include <stdlib.h>\r
48 #include <setjmp.h>\r
49 \r
50 #include "FreeRTOS.h"\r
51 #include "task.h"\r
52 #include "portasm.h"\r
53 \r
54 /*lint -e950 Non ANSI reserved words okay in this file only. */\r
55 \r
56 #define portTIMER_EOI_TYPE              ( 8 )\r
57 #define portRESET_PIC()                 portOUTPUT_WORD( ( uint16_t ) 0xff22, portTIMER_EOI_TYPE )\r
58 #define portTIMER_INT_NUMBER    0x12\r
59 \r
60 #define portTIMER_1_CONTROL_REGISTER    ( ( uint16_t ) 0xff5e )\r
61 #define portTIMER_0_CONTROL_REGISTER    ( ( uint16_t ) 0xff56 )\r
62 #define portTIMER_INTERRUPT_ENABLE              ( ( uint16_t ) 0x2000 )\r
63 \r
64 /* Setup the hardware to generate the required tick frequency. */\r
65 static void prvSetTickFrequency( uint32_t ulTickRateHz );\r
66 \r
67 /* Set the hardware back to the state as per before the scheduler started. */\r
68 static void prvExitFunction( void );\r
69 \r
70 /* The ISR used depends on whether the preemptive or cooperative scheduler\r
71 is being used. */\r
72 #if( configUSE_PREEMPTION == 1 )\r
73         /* Tick service routine used by the scheduler when preemptive scheduling is\r
74         being used. */\r
75         static void __interrupt __far prvPreemptiveTick( void );\r
76 #else\r
77         /* Tick service routine used by the scheduler when cooperative scheduling is\r
78         being used. */\r
79         static void __interrupt __far prvNonPreemptiveTick( void );\r
80 #endif\r
81 \r
82 /* Trap routine used by taskYIELD() to manually cause a context switch. */\r
83 static void __interrupt __far prvYieldProcessor( void );\r
84 \r
85 /*lint -e956 File scopes necessary here. */\r
86 \r
87 /* Set true when the vectors are set so the scheduler will service the tick. */\r
88 static BaseType_t xSchedulerRunning = pdFALSE;\r
89 \r
90 /* Points to the original routine installed on the vector we use for manual\r
91 context switches.  This is then used to restore the original routine during\r
92 prvExitFunction(). */\r
93 static void ( __interrupt __far *pxOldSwitchISR )();\r
94 \r
95 /* Used to restore the original DOS context when the scheduler is ended. */\r
96 static jmp_buf xJumpBuf;\r
97 \r
98 /*lint +e956 */\r
99 \r
100 /*-----------------------------------------------------------*/\r
101 BaseType_t xPortStartScheduler( void )\r
102 {\r
103         /* This is called with interrupts already disabled. */\r
104 \r
105         /* Remember what was on the interrupts we are going to use\r
106         so we can put them back later if required. */\r
107         pxOldSwitchISR = _dos_getvect( portSWITCH_INT_NUMBER );\r
108 \r
109         /* Put our manual switch (yield) function on a known\r
110         vector. */\r
111         _dos_setvect( portSWITCH_INT_NUMBER, prvYieldProcessor );\r
112 \r
113         #if( configUSE_PREEMPTION == 1 )\r
114         {\r
115                 /* Put our tick switch function on the timer interrupt. */\r
116                 _dos_setvect( portTIMER_INT_NUMBER, prvPreemptiveTick );\r
117         }\r
118         #else\r
119         {\r
120                 /* We want the timer interrupt to just increment the tick count. */\r
121                 _dos_setvect( portTIMER_INT_NUMBER, prvNonPreemptiveTick );\r
122         }\r
123         #endif\r
124 \r
125         prvSetTickFrequency( configTICK_RATE_HZ );\r
126 \r
127         /* Clean up function if we want to return to DOS. */\r
128         if( setjmp( xJumpBuf ) != 0 )\r
129         {\r
130                 prvExitFunction();\r
131                 xSchedulerRunning = pdFALSE;\r
132         }\r
133         else\r
134         {\r
135                 xSchedulerRunning = pdTRUE;\r
136 \r
137                 /* Kick off the scheduler by setting up the context of the first task. */\r
138                 portFIRST_CONTEXT();\r
139         }\r
140 \r
141         return xSchedulerRunning;\r
142 }\r
143 /*-----------------------------------------------------------*/\r
144 \r
145 /* The ISR used depends on whether the preemptive or cooperative scheduler\r
146 is being used. */\r
147 #if( configUSE_PREEMPTION == 1 )\r
148         static void __interrupt __far prvPreemptiveTick( void )\r
149         {\r
150                 /* Get the scheduler to update the task states following the tick. */\r
151                 if( xTaskIncrementTick() != pdFALSE )\r
152                 {\r
153                         /* Switch in the context of the next task to be run. */\r
154                         portSWITCH_CONTEXT();\r
155                 }\r
156 \r
157                 /* Reset the PIC ready for the next time. */\r
158                 portRESET_PIC();\r
159         }\r
160 #else\r
161         static void __interrupt __far prvNonPreemptiveTick( void )\r
162         {\r
163                 /* Same as preemptive tick, but the cooperative scheduler is being used\r
164                 so we don't have to switch in the context of the next task. */\r
165                 xTaskIncrementTick();\r
166                 portRESET_PIC();\r
167         }\r
168 #endif\r
169 /*-----------------------------------------------------------*/\r
170 \r
171 static void __interrupt __far prvYieldProcessor( void )\r
172 {\r
173         /* Switch in the context of the next task to be run. */\r
174         portSWITCH_CONTEXT();\r
175 }\r
176 /*-----------------------------------------------------------*/\r
177 \r
178 void vPortEndScheduler( void )\r
179 {\r
180         /* Jump back to the processor state prior to starting the\r
181         scheduler.  This means we are not going to be using a\r
182         task stack frame so the task can be deleted. */\r
183         longjmp( xJumpBuf, 1 );\r
184 }\r
185 /*-----------------------------------------------------------*/\r
186 \r
187 static void prvExitFunction( void )\r
188 {\r
189 const uint16_t usTimerDisable = 0x0000;\r
190 uint16_t usTimer0Control;\r
191 \r
192         /* Interrupts should be disabled here anyway - but no\r
193         harm in making sure. */\r
194         portDISABLE_INTERRUPTS();\r
195         if( xSchedulerRunning == pdTRUE )\r
196         {\r
197                 /* Put back the switch interrupt routines that was in place\r
198                 before the scheduler started. */\r
199                 _dos_setvect( portSWITCH_INT_NUMBER, pxOldSwitchISR );\r
200         }\r
201 \r
202         /* Disable the timer used for the tick to ensure the scheduler is\r
203         not called before restoring interrupts.  There was previously nothing\r
204         on this timer so there is no old ISR to restore. */\r
205         portOUTPUT_WORD( portTIMER_1_CONTROL_REGISTER, usTimerDisable );\r
206 \r
207         /* Restart the DOS tick. */\r
208         usTimer0Control = portINPUT_WORD( portTIMER_0_CONTROL_REGISTER );\r
209         usTimer0Control |= portTIMER_INTERRUPT_ENABLE;\r
210         portOUTPUT_WORD( portTIMER_0_CONTROL_REGISTER, usTimer0Control );\r
211 \r
212 \r
213         portENABLE_INTERRUPTS();\r
214 }\r
215 /*-----------------------------------------------------------*/\r
216 \r
217 static void prvSetTickFrequency( uint32_t ulTickRateHz )\r
218 {\r
219 const uint16_t usMaxCountRegister = 0xff5a;\r
220 const uint16_t usTimerPriorityRegister = 0xff32;\r
221 const uint16_t usTimerEnable = 0xC000;\r
222 const uint16_t usRetrigger = 0x0001;\r
223 const uint16_t usTimerHighPriority = 0x0000;\r
224 uint16_t usTimer0Control;\r
225 \r
226 /* ( CPU frequency / 4 ) / clock 2 max count [inpw( 0xff62 ) = 7] */\r
227 \r
228 const uint32_t ulClockFrequency = ( uint32_t ) 0x7f31a0UL;\r
229 \r
230 uint32_t ulTimerCount = ulClockFrequency / ulTickRateHz;\r
231 \r
232         portOUTPUT_WORD( portTIMER_1_CONTROL_REGISTER, usTimerEnable | portTIMER_INTERRUPT_ENABLE | usRetrigger );\r
233         portOUTPUT_WORD( usMaxCountRegister, ( uint16_t ) ulTimerCount );\r
234         portOUTPUT_WORD( usTimerPriorityRegister, usTimerHighPriority );\r
235 \r
236         /* Stop the DOS tick - don't do this if you want to maintain a TOD clock. */\r
237         usTimer0Control = portINPUT_WORD( portTIMER_0_CONTROL_REGISTER );\r
238         usTimer0Control &= ~portTIMER_INTERRUPT_ENABLE;\r
239         portOUTPUT_WORD( portTIMER_0_CONTROL_REGISTER, usTimer0Control );\r
240 }\r
241 \r
242 \r
243 /*lint +e950 */\r
244 \r