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