]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/IAR/78K0R/port.c
Add xEventGroupClearBitsFromISR() and xEventGroupGetBitsFromISR() functions.
[freertos] / FreeRTOS / Source / portable / IAR / 78K0R / port.c
1 /*\r
2     FreeRTOS V7.6.0 - Copyright (C) 2013 Real Time Engineers Ltd. \r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
28     >>! a combined work that includes FreeRTOS without being obliged to provide\r
29     >>! the source code for proprietary components outside of the FreeRTOS\r
30     >>! kernel.\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 /* Standard includes. */\r
67 #include <stdlib.h>\r
68 \r
69 /* Scheduler includes. */\r
70 #include "FreeRTOS.h"\r
71 #include "task.h"\r
72 \r
73 /* The critical nesting value is initialised to a non zero value to ensure\r
74 interrupts don't accidentally become enabled before the scheduler is started. */\r
75 #define portINITIAL_CRITICAL_NESTING  (( uint16_t ) 10)\r
76 \r
77 /* Initial PSW value allocated to a newly created task.\r
78  *   1100011000000000\r
79  *   ||||||||-------------- Fill byte\r
80  *   |||||||--------------- Carry Flag cleared\r
81  *   |||||----------------- In-service priority Flags set to low level\r
82  *   ||||------------------ Register bank Select 0 Flag cleared\r
83  *   |||------------------- Auxiliary Carry Flag cleared\r
84  *   ||-------------------- Register bank Select 1 Flag cleared\r
85  *   |--------------------- Zero Flag set\r
86  *   ---------------------- Global Interrupt Flag set (enabled)\r
87  */\r
88 #define portPSW           (0xc6UL)\r
89 \r
90 /* We require the address of the pxCurrentTCB variable, but don't want to know\r
91 any details of its type. */\r
92 typedef void TCB_t;\r
93 extern volatile TCB_t * volatile pxCurrentTCB;\r
94 \r
95 /* Most ports implement critical sections by placing the interrupt flags on\r
96 the stack before disabling interrupts.  Exiting the critical section is then\r
97 simply a case of popping the flags from the stack.  As 78K0 IAR does not use\r
98 a frame pointer this cannot be done as modifying the stack will clobber all\r
99 the stack variables.  Instead each task maintains a count of the critical\r
100 section nesting depth.  Each time a critical section is entered the count is\r
101 incremented.  Each time a critical section is left the count is decremented -\r
102 with interrupts only being re-enabled if the count is zero.\r
103 \r
104 usCriticalNesting will get set to zero when the scheduler starts, but must\r
105 not be initialised to zero as this will cause problems during the startup\r
106 sequence. */\r
107 volatile uint16_t usCriticalNesting = portINITIAL_CRITICAL_NESTING;\r
108 /*-----------------------------------------------------------*/\r
109 \r
110 /*\r
111  * Sets up the periodic ISR used for the RTOS tick.\r
112  */\r
113 static void prvSetupTimerInterrupt( void );\r
114 /*-----------------------------------------------------------*/\r
115 \r
116 /*\r
117  * Initialise the stack of a task to look exactly as if a call to\r
118  * portSAVE_CONTEXT had been called.\r
119  *\r
120  * See the header file portable.h.\r
121  */\r
122 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
123 {\r
124 uint32_t *pulLocal;\r
125 \r
126         #if configMEMORY_MODE == 1\r
127         {\r
128                 /* Parameters are passed in on the stack, and written using a 32bit value\r
129                 hence a space is left for the second two bytes. */\r
130                 pxTopOfStack--;\r
131 \r
132                 /* Write in the parameter value. */\r
133                 pulLocal =  ( uint32_t * ) pxTopOfStack;\r
134                 *pulLocal = ( uint32_t ) pvParameters;\r
135                 pxTopOfStack--;\r
136 \r
137                 /* These values are just spacers.  The return address of the function\r
138                 would normally be written here. */\r
139                 *pxTopOfStack = ( StackType_t ) 0xcdcd;\r
140                 pxTopOfStack--;\r
141                 *pxTopOfStack = ( StackType_t ) 0xcdcd;\r
142                 pxTopOfStack--;\r
143 \r
144                 /* The start address / PSW value is also written in as a 32bit value,\r
145                 so leave a space for the second two bytes. */\r
146                 pxTopOfStack--;\r
147         \r
148                 /* Task function start address combined with the PSW. */\r
149                 pulLocal = ( uint32_t * ) pxTopOfStack;\r
150                 *pulLocal = ( ( ( uint32_t ) pxCode ) | ( portPSW << 24UL ) );\r
151                 pxTopOfStack--;\r
152 \r
153                 /* An initial value for the AX register. */\r
154                 *pxTopOfStack = ( StackType_t ) 0x1111;\r
155                 pxTopOfStack--;\r
156         }\r
157         #else\r
158         {\r
159                 /* Task function address is written to the stack first.  As it is\r
160                 written as a 32bit value a space is left on the stack for the second\r
161                 two bytes. */\r
162                 pxTopOfStack--;\r
163 \r
164                 /* Task function start address combined with the PSW. */\r
165                 pulLocal = ( uint32_t * ) pxTopOfStack;\r
166                 *pulLocal = ( ( ( uint32_t ) pxCode ) | ( portPSW << 24UL ) );\r
167                 pxTopOfStack--;\r
168 \r
169                 /* The parameter is passed in AX. */\r
170                 *pxTopOfStack = ( StackType_t ) pvParameters;\r
171                 pxTopOfStack--;\r
172         }\r
173         #endif\r
174 \r
175         /* An initial value for the HL register. */\r
176         *pxTopOfStack = ( StackType_t ) 0x2222;\r
177         pxTopOfStack--;\r
178 \r
179         /* CS and ES registers. */\r
180         *pxTopOfStack = ( StackType_t ) 0x0F00;\r
181         pxTopOfStack--;\r
182 \r
183         /* Finally the remaining general purpose registers DE and BC */\r
184         *pxTopOfStack = ( StackType_t ) 0xDEDE;\r
185         pxTopOfStack--;\r
186         *pxTopOfStack = ( StackType_t ) 0xBCBC;\r
187         pxTopOfStack--;\r
188 \r
189         /* Finally the critical section nesting count is set to zero when the task\r
190         first starts. */\r
191         *pxTopOfStack = ( StackType_t ) portNO_CRITICAL_SECTION_NESTING;        \r
192 \r
193         /* Return a pointer to the top of the stack we have generated so this can\r
194         be stored in the task control block for the task. */\r
195         return pxTopOfStack;\r
196 }\r
197 /*-----------------------------------------------------------*/\r
198 \r
199 BaseType_t xPortStartScheduler( void )\r
200 {\r
201         /* Setup the hardware to generate the tick.  Interrupts are disabled when\r
202         this function is called. */\r
203         prvSetupTimerInterrupt();\r
204 \r
205         /* Restore the context of the first task that is going to run. */\r
206         vPortStart();\r
207 \r
208         /* Should not get here as the tasks are now running! */\r
209         return pdTRUE;\r
210 }\r
211 /*-----------------------------------------------------------*/\r
212 \r
213 void vPortEndScheduler( void )\r
214 {\r
215         /* It is unlikely that the 78K0R port will get stopped.  If required simply\r
216         disable the tick interrupt here. */\r
217 }\r
218 /*-----------------------------------------------------------*/\r
219 \r
220 static void prvSetupTimerInterrupt( void )\r
221 {\r
222         /* Setup channel 5 of the TAU to generate the tick interrupt. */\r
223 \r
224         /* First the Timer Array Unit has to be enabled. */\r
225         TAU0EN = 1;\r
226 \r
227         /* To configure the Timer Array Unit all Channels have to first be stopped. */\r
228         TT0 = 0xff;\r
229 \r
230         /* Interrupt of Timer Array Unit Channel 5 is disabled to set the interrupt\r
231         priority. */\r
232         TMMK05 = 1;\r
233 \r
234         /* Clear Timer Array Unit Channel 5 interrupt flag. */  \r
235         TMIF05 = 0;\r
236 \r
237         /* Set Timer Array Unit Channel 5 interrupt priority */\r
238         TMPR005 = 0;\r
239         TMPR105 = 0;\r
240 \r
241         /* Set Timer Array Unit Channel 5 Mode as interval timer. */\r
242         TMR05 = 0x0000;\r
243 \r
244         /* Set the compare match value according to the tick rate we want. */\r
245         TDR05 = ( TickType_t ) ( configCPU_CLOCK_HZ / configTICK_RATE_HZ );\r
246 \r
247         /* Set Timer Array Unit Channel 5 output mode */\r
248         TOM0 &= ~0x0020;\r
249 \r
250         /* Set Timer Array Unit Channel 5 output level */       \r
251         TOL0 &= ~0x0020;\r
252 \r
253         /* Set Timer Array Unit Channel 5 output enable */      \r
254         TOE0 &= ~0x0020;\r
255 \r
256         /* Interrupt of Timer Array Unit Channel 5 enabled */\r
257         TMMK05 = 0;\r
258 \r
259         /* Start Timer Array Unit Channel 5.*/\r
260         TS0 |= 0x0020;\r
261 }\r
262 /*-----------------------------------------------------------*/\r
263 \r