]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/BCC/16BitDOS/Flsh186/port.c
75a8ec9f8de3038ae810c79cda5c1cc619356aee
[freertos] / FreeRTOS / Source / portable / BCC / 16BitDOS / Flsh186 / port.c
1 /*\r
2     FreeRTOS V7.5.2 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
5 \r
6     ***************************************************************************\r
7      *                                                                       *\r
8      *    FreeRTOS provides completely free yet professionally developed,    *\r
9      *    robust, strictly quality controlled, supported, and cross          *\r
10      *    platform software that has become a de facto standard.             *\r
11      *                                                                       *\r
12      *    Help yourself get started quickly and support the FreeRTOS         *\r
13      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
14      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
15      *                                                                       *\r
16      *    Thank you!                                                         *\r
17      *                                                                       *\r
18     ***************************************************************************\r
19 \r
20     This file is part of the FreeRTOS distribution.\r
21 \r
22     FreeRTOS is free software; you can redistribute it and/or modify it under\r
23     the terms of the GNU General Public License (version 2) as published by the\r
24     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
25 \r
26     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
27     >>! a combined work that includes FreeRTOS without being obliged to provide\r
28     >>! the source code for proprietary components outside of the FreeRTOS\r
29     >>! kernel.\r
30 \r
31     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
32     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
33     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
34     link: http://www.freertos.org/a00114.html\r
35 \r
36     1 tab == 4 spaces!\r
37 \r
38     ***************************************************************************\r
39      *                                                                       *\r
40      *    Having a problem?  Start by reading the FAQ "My application does   *\r
41      *    not run, what could be wrong?"                                     *\r
42      *                                                                       *\r
43      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
44      *                                                                       *\r
45     ***************************************************************************\r
46 \r
47     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
48     license and Real Time Engineers Ltd. contact details.\r
49 \r
50     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
51     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
52     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
53 \r
54     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
55     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
56     licenses offer ticketed support, indemnification and middleware.\r
57 \r
58     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
59     engineered and independently SIL3 certified version for use in safety and\r
60     mission critical applications that require provable dependability.\r
61 \r
62     1 tab == 4 spaces!\r
63 */\r
64 \r
65 /*\r
66 Changes from V1.00:\r
67 \r
68         + Call to taskYIELD() from within tick ISR has been replaced by the more\r
69           efficient portSWITCH_CONTEXT().\r
70         + ISR function definitions renamed to include the prv prefix.\r
71 \r
72 Changes from V2.6.1\r
73 \r
74         + Replaced the sUsingPreemption variable with the configUSE_PREEMPTION\r
75           macro to be consistent with the later ports.\r
76 */\r
77 \r
78 /*-----------------------------------------------------------\r
79  * Implementation of functions defined in portable.h for the Flashlite 186\r
80  * port.\r
81  *----------------------------------------------------------*/\r
82 \r
83 #include <dos.h>\r
84 #include <stdlib.h>\r
85 #include <setjmp.h>\r
86 \r
87 #include "FreeRTOS.h"\r
88 #include "task.h"\r
89 #include "portasm.h"\r
90 \r
91 /*lint -e950 Non ANSI reserved words okay in this file only. */\r
92 \r
93 #define portTIMER_EOI_TYPE              ( 8 )\r
94 #define portRESET_PIC()                 portOUTPUT_WORD( ( unsigned short ) 0xff22, portTIMER_EOI_TYPE )\r
95 #define portTIMER_INT_NUMBER    0x12\r
96 \r
97 #define portTIMER_1_CONTROL_REGISTER    ( ( unsigned short ) 0xff5e )\r
98 #define portTIMER_0_CONTROL_REGISTER    ( ( unsigned short ) 0xff56 )\r
99 #define portTIMER_INTERRUPT_ENABLE              ( ( unsigned short ) 0x2000 )\r
100 \r
101 /* Setup the hardware to generate the required tick frequency. */\r
102 static void prvSetTickFrequency( unsigned long ulTickRateHz );\r
103 \r
104 /* Set the hardware back to the state as per before the scheduler started. */\r
105 static void prvExitFunction( void );\r
106 \r
107 /* The ISR used depends on whether the preemptive or cooperative scheduler\r
108 is being used. */\r
109 #if( configUSE_PREEMPTION == 1 )\r
110         /* Tick service routine used by the scheduler when preemptive scheduling is\r
111         being used. */\r
112         static void __interrupt __far prvPreemptiveTick( void );\r
113 #else\r
114         /* Tick service routine used by the scheduler when cooperative scheduling is\r
115         being used. */\r
116         static void __interrupt __far prvNonPreemptiveTick( void );\r
117 #endif\r
118 \r
119 /* Trap routine used by taskYIELD() to manually cause a context switch. */\r
120 static void __interrupt __far prvYieldProcessor( void );\r
121 \r
122 /*lint -e956 File scopes necessary here. */\r
123 \r
124 /* Set true when the vectors are set so the scheduler will service the tick. */\r
125 static portBASE_TYPE xSchedulerRunning = pdFALSE;\r
126 \r
127 /* Points to the original routine installed on the vector we use for manual\r
128 context switches.  This is then used to restore the original routine during\r
129 prvExitFunction(). */\r
130 static void ( __interrupt __far *pxOldSwitchISR )();\r
131 \r
132 /* Used to restore the original DOS context when the scheduler is ended. */\r
133 static jmp_buf xJumpBuf;\r
134 \r
135 /*lint +e956 */\r
136 \r
137 /*-----------------------------------------------------------*/\r
138 portBASE_TYPE xPortStartScheduler( void )\r
139 {\r
140         /* This is called with interrupts already disabled. */\r
141 \r
142         /* Remember what was on the interrupts we are going to use\r
143         so we can put them back later if required. */\r
144         pxOldSwitchISR = _dos_getvect( portSWITCH_INT_NUMBER );\r
145 \r
146         /* Put our manual switch (yield) function on a known\r
147         vector. */\r
148         _dos_setvect( portSWITCH_INT_NUMBER, prvYieldProcessor );\r
149 \r
150         #if( configUSE_PREEMPTION == 1 )\r
151         {\r
152                 /* Put our tick switch function on the timer interrupt. */\r
153                 _dos_setvect( portTIMER_INT_NUMBER, prvPreemptiveTick );\r
154         }\r
155         #else\r
156         {\r
157                 /* We want the timer interrupt to just increment the tick count. */\r
158                 _dos_setvect( portTIMER_INT_NUMBER, prvNonPreemptiveTick );\r
159         }\r
160         #endif\r
161 \r
162         prvSetTickFrequency( configTICK_RATE_HZ );\r
163 \r
164         /* Clean up function if we want to return to DOS. */\r
165         if( setjmp( xJumpBuf ) != 0 )\r
166         {\r
167                 prvExitFunction();\r
168                 xSchedulerRunning = pdFALSE;\r
169         }\r
170         else\r
171         {\r
172                 xSchedulerRunning = pdTRUE;\r
173 \r
174                 /* Kick off the scheduler by setting up the context of the first task. */\r
175                 portFIRST_CONTEXT();\r
176         }\r
177 \r
178         return xSchedulerRunning;\r
179 }\r
180 /*-----------------------------------------------------------*/\r
181 \r
182 /* The ISR used depends on whether the preemptive or cooperative scheduler\r
183 is being used. */\r
184 #if( configUSE_PREEMPTION == 1 )\r
185         static void __interrupt __far prvPreemptiveTick( void )\r
186         {\r
187                 /* Get the scheduler to update the task states following the tick. */\r
188                 if( xTaskIncrementTick() != pdFALSE )\r
189                 {\r
190                         /* Switch in the context of the next task to be run. */\r
191                         portSWITCH_CONTEXT();\r
192                 }\r
193 \r
194                 /* Reset the PIC ready for the next time. */\r
195                 portRESET_PIC();\r
196         }\r
197 #else\r
198         static void __interrupt __far prvNonPreemptiveTick( void )\r
199         {\r
200                 /* Same as preemptive tick, but the cooperative scheduler is being used\r
201                 so we don't have to switch in the context of the next task. */\r
202                 xTaskIncrementTick();\r
203                 portRESET_PIC();\r
204         }\r
205 #endif\r
206 /*-----------------------------------------------------------*/\r
207 \r
208 static void __interrupt __far prvYieldProcessor( void )\r
209 {\r
210         /* Switch in the context of the next task to be run. */\r
211         portSWITCH_CONTEXT();\r
212 }\r
213 /*-----------------------------------------------------------*/\r
214 \r
215 void vPortEndScheduler( void )\r
216 {\r
217         /* Jump back to the processor state prior to starting the\r
218         scheduler.  This means we are not going to be using a\r
219         task stack frame so the task can be deleted. */\r
220         longjmp( xJumpBuf, 1 );\r
221 }\r
222 /*-----------------------------------------------------------*/\r
223 \r
224 static void prvExitFunction( void )\r
225 {\r
226 const unsigned short usTimerDisable = 0x0000;\r
227 unsigned short usTimer0Control;\r
228 \r
229         /* Interrupts should be disabled here anyway - but no\r
230         harm in making sure. */\r
231         portDISABLE_INTERRUPTS();\r
232         if( xSchedulerRunning == pdTRUE )\r
233         {\r
234                 /* Put back the switch interrupt routines that was in place\r
235                 before the scheduler started. */\r
236                 _dos_setvect( portSWITCH_INT_NUMBER, pxOldSwitchISR );\r
237         }\r
238 \r
239         /* Disable the timer used for the tick to ensure the scheduler is\r
240         not called before restoring interrupts.  There was previously nothing\r
241         on this timer so there is no old ISR to restore. */\r
242         portOUTPUT_WORD( portTIMER_1_CONTROL_REGISTER, usTimerDisable );\r
243 \r
244         /* Restart the DOS tick. */\r
245         usTimer0Control = portINPUT_WORD( portTIMER_0_CONTROL_REGISTER );\r
246         usTimer0Control |= portTIMER_INTERRUPT_ENABLE;\r
247         portOUTPUT_WORD( portTIMER_0_CONTROL_REGISTER, usTimer0Control );\r
248 \r
249 \r
250         portENABLE_INTERRUPTS();\r
251 }\r
252 /*-----------------------------------------------------------*/\r
253 \r
254 static void prvSetTickFrequency( unsigned long ulTickRateHz )\r
255 {\r
256 const unsigned short usMaxCountRegister = 0xff5a;\r
257 const unsigned short usTimerPriorityRegister = 0xff32;\r
258 const unsigned short usTimerEnable = 0xC000;\r
259 const unsigned short usRetrigger = 0x0001;\r
260 const unsigned short usTimerHighPriority = 0x0000;\r
261 unsigned short usTimer0Control;\r
262 \r
263 /* ( CPU frequency / 4 ) / clock 2 max count [inpw( 0xff62 ) = 7] */\r
264 \r
265 const unsigned long ulClockFrequency = ( unsigned long ) 0x7f31a0UL;\r
266 \r
267 unsigned long ulTimerCount = ulClockFrequency / ulTickRateHz;\r
268 \r
269         portOUTPUT_WORD( portTIMER_1_CONTROL_REGISTER, usTimerEnable | portTIMER_INTERRUPT_ENABLE | usRetrigger );\r
270         portOUTPUT_WORD( usMaxCountRegister, ( unsigned short ) ulTimerCount );\r
271         portOUTPUT_WORD( usTimerPriorityRegister, usTimerHighPriority );\r
272 \r
273         /* Stop the DOS tick - don't do this if you want to maintain a TOD clock. */\r
274         usTimer0Control = portINPUT_WORD( portTIMER_0_CONTROL_REGISTER );\r
275         usTimer0Control &= ~portTIMER_INTERRUPT_ENABLE;\r
276         portOUTPUT_WORD( portTIMER_0_CONTROL_REGISTER, usTimer0Control );\r
277 }\r
278 \r
279 \r
280 /*lint +e950 */\r
281 \r