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