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