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