]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/IAR/78K0R/port.c
Add additional critical section to the default tickless implementations.
[freertos] / FreeRTOS / Source / portable / IAR / 78K0R / 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 /* Standard includes. */\r
66 #include <stdlib.h>\r
67 \r
68 /* Scheduler includes. */\r
69 #include "FreeRTOS.h"\r
70 #include "task.h"\r
71 \r
72 /* The critical nesting value is initialised to a non zero value to ensure\r
73 interrupts don't accidentally become enabled before the scheduler is started. */\r
74 #define portINITIAL_CRITICAL_NESTING  (( unsigned short ) 10)\r
75 \r
76 /* Initial PSW value allocated to a newly created task.\r
77  *   1100011000000000\r
78  *   ||||||||-------------- Fill byte\r
79  *   |||||||--------------- Carry Flag cleared\r
80  *   |||||----------------- In-service priority Flags set to low level\r
81  *   ||||------------------ Register bank Select 0 Flag cleared\r
82  *   |||------------------- Auxiliary Carry Flag cleared\r
83  *   ||-------------------- Register bank Select 1 Flag cleared\r
84  *   |--------------------- Zero Flag set\r
85  *   ---------------------- Global Interrupt Flag set (enabled)\r
86  */\r
87 #define portPSW           (0xc6UL)\r
88 \r
89 /* We require the address of the pxCurrentTCB variable, but don't want to know\r
90 any details of its type. */\r
91 typedef void tskTCB;\r
92 extern volatile tskTCB * volatile pxCurrentTCB;\r
93 \r
94 /* Most ports implement critical sections by placing the interrupt flags on\r
95 the stack before disabling interrupts.  Exiting the critical section is then\r
96 simply a case of popping the flags from the stack.  As 78K0 IAR does not use\r
97 a frame pointer this cannot be done as modifying the stack will clobber all\r
98 the stack variables.  Instead each task maintains a count of the critical\r
99 section nesting depth.  Each time a critical section is entered the count is\r
100 incremented.  Each time a critical section is left the count is decremented -\r
101 with interrupts only being re-enabled if the count is zero.\r
102 \r
103 usCriticalNesting will get set to zero when the scheduler starts, but must\r
104 not be initialised to zero as this will cause problems during the startup\r
105 sequence. */\r
106 volatile unsigned short usCriticalNesting = portINITIAL_CRITICAL_NESTING;\r
107 /*-----------------------------------------------------------*/\r
108 \r
109 /*\r
110  * Sets up the periodic ISR used for the RTOS tick.\r
111  */\r
112 static void prvSetupTimerInterrupt( void );\r
113 /*-----------------------------------------------------------*/\r
114 \r
115 /*\r
116  * Initialise the stack of a task to look exactly as if a call to\r
117  * portSAVE_CONTEXT had been called.\r
118  *\r
119  * See the header file portable.h.\r
120  */\r
121 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
122 {\r
123 unsigned long *pulLocal;\r
124 \r
125         #if configMEMORY_MODE == 1\r
126         {\r
127                 /* Parameters are passed in on the stack, and written using a 32bit value\r
128                 hence a space is left for the second two bytes. */\r
129                 pxTopOfStack--;\r
130 \r
131                 /* Write in the parameter value. */\r
132                 pulLocal =  ( unsigned long * ) pxTopOfStack;\r
133                 *pulLocal = ( unsigned long ) pvParameters;\r
134                 pxTopOfStack--;\r
135 \r
136                 /* These values are just spacers.  The return address of the function\r
137                 would normally be written here. */\r
138                 *pxTopOfStack = ( portSTACK_TYPE ) 0xcdcd;\r
139                 pxTopOfStack--;\r
140                 *pxTopOfStack = ( portSTACK_TYPE ) 0xcdcd;\r
141                 pxTopOfStack--;\r
142 \r
143                 /* The start address / PSW value is also written in as a 32bit value,\r
144                 so leave a space for the second two bytes. */\r
145                 pxTopOfStack--;\r
146         \r
147                 /* Task function start address combined with the PSW. */\r
148                 pulLocal = ( unsigned long * ) pxTopOfStack;\r
149                 *pulLocal = ( ( ( unsigned long ) pxCode ) | ( portPSW << 24UL ) );\r
150                 pxTopOfStack--;\r
151 \r
152                 /* An initial value for the AX register. */\r
153                 *pxTopOfStack = ( portSTACK_TYPE ) 0x1111;\r
154                 pxTopOfStack--;\r
155         }\r
156         #else\r
157         {\r
158                 /* Task function address is written to the stack first.  As it is\r
159                 written as a 32bit value a space is left on the stack for the second\r
160                 two bytes. */\r
161                 pxTopOfStack--;\r
162 \r
163                 /* Task function start address combined with the PSW. */\r
164                 pulLocal = ( unsigned long * ) pxTopOfStack;\r
165                 *pulLocal = ( ( ( unsigned long ) pxCode ) | ( portPSW << 24UL ) );\r
166                 pxTopOfStack--;\r
167 \r
168                 /* The parameter is passed in AX. */\r
169                 *pxTopOfStack = ( portSTACK_TYPE ) pvParameters;\r
170                 pxTopOfStack--;\r
171         }\r
172         #endif\r
173 \r
174         /* An initial value for the HL register. */\r
175         *pxTopOfStack = ( portSTACK_TYPE ) 0x2222;\r
176         pxTopOfStack--;\r
177 \r
178         /* CS and ES registers. */\r
179         *pxTopOfStack = ( portSTACK_TYPE ) 0x0F00;\r
180         pxTopOfStack--;\r
181 \r
182         /* Finally the remaining general purpose registers DE and BC */\r
183         *pxTopOfStack = ( portSTACK_TYPE ) 0xDEDE;\r
184         pxTopOfStack--;\r
185         *pxTopOfStack = ( portSTACK_TYPE ) 0xBCBC;\r
186         pxTopOfStack--;\r
187 \r
188         /* Finally the critical section nesting count is set to zero when the task\r
189         first starts. */\r
190         *pxTopOfStack = ( portSTACK_TYPE ) portNO_CRITICAL_SECTION_NESTING;     \r
191 \r
192         /* Return a pointer to the top of the stack we have generated so this can\r
193         be stored in the task control block for the task. */\r
194         return pxTopOfStack;\r
195 }\r
196 /*-----------------------------------------------------------*/\r
197 \r
198 portBASE_TYPE xPortStartScheduler( void )\r
199 {\r
200         /* Setup the hardware to generate the tick.  Interrupts are disabled when\r
201         this function is called. */\r
202         prvSetupTimerInterrupt();\r
203 \r
204         /* Restore the context of the first task that is going to run. */\r
205         vPortStart();\r
206 \r
207         /* Should not get here as the tasks are now running! */\r
208         return pdTRUE;\r
209 }\r
210 /*-----------------------------------------------------------*/\r
211 \r
212 void vPortEndScheduler( void )\r
213 {\r
214         /* It is unlikely that the 78K0R port will get stopped.  If required simply\r
215         disable the tick interrupt here. */\r
216 }\r
217 /*-----------------------------------------------------------*/\r
218 \r
219 static void prvSetupTimerInterrupt( void )\r
220 {\r
221         /* Setup channel 5 of the TAU to generate the tick interrupt. */\r
222 \r
223         /* First the Timer Array Unit has to be enabled. */\r
224         TAU0EN = 1;\r
225 \r
226         /* To configure the Timer Array Unit all Channels have to first be stopped. */\r
227         TT0 = 0xff;\r
228 \r
229         /* Interrupt of Timer Array Unit Channel 5 is disabled to set the interrupt\r
230         priority. */\r
231         TMMK05 = 1;\r
232 \r
233         /* Clear Timer Array Unit Channel 5 interrupt flag. */  \r
234         TMIF05 = 0;\r
235 \r
236         /* Set Timer Array Unit Channel 5 interrupt priority */\r
237         TMPR005 = 0;\r
238         TMPR105 = 0;\r
239 \r
240         /* Set Timer Array Unit Channel 5 Mode as interval timer. */\r
241         TMR05 = 0x0000;\r
242 \r
243         /* Set the compare match value according to the tick rate we want. */\r
244         TDR05 = ( portTickType ) ( configCPU_CLOCK_HZ / configTICK_RATE_HZ );\r
245 \r
246         /* Set Timer Array Unit Channel 5 output mode */\r
247         TOM0 &= ~0x0020;\r
248 \r
249         /* Set Timer Array Unit Channel 5 output level */       \r
250         TOL0 &= ~0x0020;\r
251 \r
252         /* Set Timer Array Unit Channel 5 output enable */      \r
253         TOE0 &= ~0x0020;\r
254 \r
255         /* Interrupt of Timer Array Unit Channel 5 enabled */\r
256         TMMK05 = 0;\r
257 \r
258         /* Start Timer Array Unit Channel 5.*/\r
259         TS0 |= 0x0020;\r
260 }\r
261 /*-----------------------------------------------------------*/\r
262 \r