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