]> git.sur5r.net Git - freertos/blob - Demo/NEC_78K0R_IAR/main.c
Continued development on NEC ports.
[freertos] / Demo / NEC_78K0R_IAR / main.c
1 /*\r
2         FreeRTOS.org V5.1.1 - Copyright (C) 2003-2009 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify\r
7         it under the terms of the GNU General Public License as published by\r
8         the Free Software Foundation; either version 2 of the License, or\r
9         (at your option) any later version.\r
10 \r
11         FreeRTOS.org is distributed in the hope that it will be useful,\r
12         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14         GNU General Public License for more details.\r
15 \r
16         You should have received a copy of the GNU General Public License\r
17         along with FreeRTOS.org; if not, write to the Free Software\r
18         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19 \r
20         A special exception to the GPL can be applied should you wish to distribute\r
21         a combined work that includes FreeRTOS.org, without being obliged to provide\r
22         the source code for any proprietary components.  See the licensing section\r
23         of http://www.FreeRTOS.org for full details of how and when the exception\r
24         can be applied.\r
25 \r
26     ***************************************************************************\r
27     ***************************************************************************\r
28     *                                                                         *\r
29     * SAVE TIME AND MONEY!  We can port FreeRTOS.org to your own hardware,    *\r
30     * and even write all or part of your application on your behalf.          *\r
31     * See http://www.OpenRTOS.com for details of the services we provide to   *\r
32     * expedite your project.                                                  *\r
33     *                                                                         *\r
34     ***************************************************************************\r
35     ***************************************************************************\r
36 \r
37         Please ensure to read the configuration and relevant port sections of the\r
38         online documentation.\r
39 \r
40         http://www.FreeRTOS.org - Documentation, latest information, license and\r
41         contact details.\r
42 \r
43         http://www.SafeRTOS.com - A version that is certified for use in safety\r
44         critical systems.\r
45 \r
46         http://www.OpenRTOS.com - Commercial support, development, porting,\r
47         licensing and training services.\r
48 */\r
49 \r
50 /*\r
51  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
52  * documentation provides more details of the standard demo application tasks.\r
53  * In addition to the standard demo tasks, the following tasks and tests are\r
54  * defined and/or created within this file:\r
55  *\r
56  * "Check" task -  This only executes every three seconds but has a high priority\r
57  * to ensure it gets processor time.  Its main function is to check that all the\r
58  * standard demo tasks are still operational.  If everything is running as\r
59  * expected then the check task will toggle an LED every 3 seconds.  An error\r
60  * being discovered in any task will cause the toggle rate to increase to 500ms.\r
61  *\r
62  * "Reg test" tasks - These fill the registers with known values, then check\r
63  * that each register still contains its expected value.  Each task uses\r
64  * different values.  The tasks run with very low priority so get preempted very\r
65  * frequently.  A register containing an unexpected value is indicative of an\r
66  * error in the context switching mechanism.\r
67  *\r
68  *\r
69  * Also in addition to the standard demo tasks is a button push task.  This is\r
70  * a very basic task that is included as an example of how to write an interrupt\r
71  * service routine that interacts with a task.  The button on the target board\r
72  * is used to generate an interrupt that 'gives' a semaphore in order to unblock\r
73  * a task.  In doing so the task is synchronised with the interrupt.  Each time\r
74  * the task unblocks it simply toggles an LED before entering the Blocked state\r
75  * again to wait for the next button push.\r
76  */\r
77 \r
78 /* Standard includes. */\r
79 #include <stdlib.h>\r
80 #include <string.h>\r
81 \r
82 /* Scheduler include files. */\r
83 #include "FreeRTOS.h"\r
84 #include "task.h"\r
85 \r
86 /* Standard demo file headers. */\r
87 #include "PollQ.h"\r
88 #include "semtest.h"\r
89 #include "GenQTest.h"\r
90 #include "dynamic.h"\r
91 #include "blocktim.h"\r
92 \r
93 /*\r
94  * Priority definitions for most of the tasks in the demo application.  Some\r
95  * tasks just use the idle priority.\r
96  */\r
97 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )\r
98 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 1 )\r
99 #define mainSEMTEST_PRIORITY    ( tskIDLE_PRIORITY + 1 )\r
100 #define mainBUTTON_PRIORITY             ( configMAX_PRIORITIES - 1 )\r
101 #define mainGEN_QUEUE_PRIORITY  ( tskIDLE_PRIORITY )\r
102 \r
103 /* The period between executions of the check task. */\r
104 #define mainNO_ERROR_TOGGLE_PERIOD      ( ( portTickType ) 3000 / portTICK_RATE_MS  )\r
105 #define mainERROR_TOGGLE_PERIOD         ( ( portTickType ) 500 / portTICK_RATE_MS  )\r
106 \r
107 /* The LED toggled by the check task. */\r
108 #define mainLED_0   P7_bit.no6\r
109 \r
110 /* A value that is passed in as the parameter to the 'check' task.  This is done\r
111 purely to check that the parameter passing mechanism is functioning correctly. */\r
112 #define mainCHECK_PARAMETER_VALUE       ( 0x345678 )\r
113 \r
114 /*-----------------------------------------------------------*/\r
115 \r
116 /*\r
117  * The function that defines the 'check' task as described at the top of this\r
118  * file.\r
119  */\r
120 static void vErrorChecks( void *pvParameters );\r
121 \r
122 \r
123 /*\r
124  * This function is called from the C startup routine to setup the processor -\r
125  * in particular the clock source.\r
126  */\r
127 int __low_level_init(void);\r
128 \r
129 /*\r
130  * Functions that define the RegTest tasks as described at the top of this file.\r
131  */\r
132 extern void vRegTest1( void *pvParameters );\r
133 extern void vRegTest2( void *pvParameters );\r
134 \r
135 /*\r
136  * Function that defines the button push task as described at the top of this\r
137  * file.\r
138  */\r
139 extern void vButtonTask( void *pvParameters );\r
140 \r
141 /*-----------------------------------------------------------*/\r
142 \r
143 /* If an error is discovered by one of the RegTest tasks then this flag is set\r
144 to pdFAIL.  The 'check' task then inspects this flag to detect errors within\r
145 the RegTest tasks. */\r
146 static short sRegTestStatus = pdPASS;\r
147 \r
148 /* 78K0R Option Byte Definition. Watchdog disabled, LVI enabled, OCD interface\r
149 enabled. */\r
150 __root __far const unsigned portCHAR OptionByte[] @ 0x00C0 =\r
151 {\r
152         WATCHDOG_DISABLED, LVI_ENABLED, RESERVED_FF, OCD_ENABLED\r
153 };\r
154 \r
155 /* Security byte definition */\r
156 __root __far const unsigned portCHAR SecuIDCode[]  @ 0x00C4 =\r
157 {\r
158         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff\r
159 };\r
160 \r
161 \r
162 /*-----------------------------------------------------------*/\r
163 \r
164 short main( void )\r
165 {\r
166         /* Creates all the tasks, then starts the scheduler. */\r
167 \r
168         /* First create the 'standard demo' tasks.  These are used to demonstrate\r
169         API functions being used and also to test the kernel port.  More information\r
170         is provided on the FreeRTOS.org WEB site. */\r
171         vStartDynamicPriorityTasks();\r
172         vCreateBlockTimeTasks();\r
173 \r
174         /* Create the button push task as described at the top of this file. */\r
175         xTaskCreate( vButtonTask, "Button", configMINIMAL_STACK_SIZE, NULL, mainBUTTON_PRIORITY, NULL );\r
176 \r
177         /* Create the RegTest tasks as described at the top of this file. */\r
178         xTaskCreate( vRegTest1, "Reg1", configMINIMAL_STACK_SIZE, NULL, 0, NULL );\r
179         xTaskCreate( vRegTest2, "Reg2", configMINIMAL_STACK_SIZE, NULL, 0, NULL );      \r
180         \r
181         /* Create the 'check' task as described at the top of this file. */\r
182         xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, ( void* )mainCHECK_PARAMETER_VALUE, mainCHECK_TASK_PRIORITY, NULL );\r
183 \r
184         #ifdef __IAR_78K0R_Kx3__\r
185         {\r
186                 /* The Kx3 has enough RAM to create more of the standard demo tasks. */\r
187                 vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
188                 vStartSemaphoreTasks(mainSEMTEST_PRIORITY);\r
189                 vStartGenericQueueTasks( mainGEN_QUEUE_PRIORITY );\r
190         }\r
191         #endif\r
192         \r
193         /* Finally start the scheduler running. */\r
194         vTaskStartScheduler();\r
195 \r
196         /* If this line is reached then vTaskStartScheduler() returned because there\r
197         was insufficient heap memory remaining for the idle task to be created. */\r
198         for( ;; );\r
199 }\r
200 /*-----------------------------------------------------------*/\r
201 \r
202 static void vErrorChecks( void *pvParameters )\r
203 {\r
204 portTickType xToggleRate = mainNO_ERROR_TOGGLE_PERIOD, xLastWakeTime;\r
205 \r
206         /* Ensure the parameter was passed in as expected.  This is just a test of\r
207         the kernel port, the parameter is not actually used for anything.  The\r
208         pointer will only actually be either 3 or 2 bytes, depending on the memory\r
209         model. */\r
210         if( pvParameters != ( void * ) mainCHECK_PARAMETER_VALUE )\r
211         {\r
212                 xToggleRate = mainERROR_TOGGLE_PERIOD;\r
213         }\r
214 \r
215         /* Initialise xLastWakeTime before it is used.  After this point it is not\r
216         written to directly. */\r
217         xLastWakeTime = xTaskGetTickCount();\r
218 \r
219         /* Cycle for ever, delaying then checking all the other tasks are still\r
220         operating without error. */\r
221         for( ;; )\r
222         {\r
223                 /* Wait until it is time to check all the other tasks again. */\r
224                 vTaskDelayUntil( &xLastWakeTime, xToggleRate );\r
225 \r
226                 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
227                 {\r
228                         xToggleRate = mainERROR_TOGGLE_PERIOD;\r
229                 }\r
230 \r
231                 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
232                 {\r
233                         xToggleRate = mainERROR_TOGGLE_PERIOD;\r
234                 }\r
235 \r
236                 if( sRegTestStatus != pdPASS )\r
237                 {\r
238                         xToggleRate = mainERROR_TOGGLE_PERIOD;\r
239                 }\r
240 \r
241                 #ifdef __IAR_78K0R_Kx3__\r
242                 {\r
243                         /* Only the Kx3 runs all the tasks. */\r
244                         if( xArePollingQueuesStillRunning() != pdTRUE)\r
245                         {\r
246                                 xToggleRate = mainERROR_TOGGLE_PERIOD;\r
247                         }\r
248                 \r
249                         if( xAreSemaphoreTasksStillRunning() != pdTRUE)\r
250                         {\r
251                                 xToggleRate = mainERROR_TOGGLE_PERIOD;\r
252                         }\r
253                         \r
254                         if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
255                         {\r
256                                 xToggleRate = mainERROR_TOGGLE_PERIOD;\r
257                         }       \r
258                 }\r
259                 #endif\r
260                 \r
261                 /* Toggle the LED.  The toggle rate will depend on whether or not an\r
262                 error has been found in any tasks. */\r
263                 mainLED_0 = !mainLED_0;\r
264         }\r
265 }\r
266 /*-----------------------------------------------------------*/\r
267 \r
268 int __low_level_init(void)\r
269 {\r
270 unsigned portCHAR ucResetFlag = RESF;\r
271 \r
272         portDISABLE_INTERRUPTS();\r
273 \r
274         /* Clock Configuration:\r
275         In this port, to use the internal high speed clock source of the microcontroller\r
276         define the configCLOCK_SOURCE as 1 in FreeRTOSConfig.h.  To use an external\r
277         clock define configCLOCK_SOURCE as 0. */\r
278         #if configCLOCK_SOURCE == 1\r
279         {\r
280                 /* Set XT1 and XT2 in Input Port Mode\r
281                    Set X1  and X2  in Input Port Mode\r
282                    High speed oscillator frequency 2MHz <= fMX <= 10MHz */\r
283                 CMC = 0x00;\r
284 \r
285                 /* X1 external oszillation stopped. */\r
286                 MSTOP = 1;\r
287 \r
288                 /* Enable internal high speed oszillation. */\r
289                 HIOSTOP = 0;\r
290                 MCM0 = 0;\r
291 \r
292                 /* Stop internal subsystem clock. */\r
293                 XTSTOP = 1;\r
294 \r
295                 /* Set clock speed. */\r
296                 CSS = 0;\r
297                 CKC &= (unsigned portCHAR)~0x07;\r
298                 CKC |= 0x00;\r
299         }\r
300         #else\r
301         {\r
302                 /* XT1 and XT2 pin in input port mode\r
303                    X1  and X2  pin in crystal resonator mode\r
304                    High speed oszillation frequency 10MHz < fMX <= 20MHz */\r
305                 CMC   = 0x41;\r
306                 \r
307                 /* Set oscillation stabilization time. */\r
308                 OSTS  = 0x07;\r
309                 \r
310                 /* Set speed mode: fMX > 10MHz for Flash memory high speed operation. */\r
311                 OSMC  = 0x01;\r
312                 \r
313                 /* Start up X1 oscillator operation\r
314                    Internal high-speed oscillator operating. */\r
315                 MSTOP = 0;\r
316                 \r
317                 /* Check oscillation stabilization time status. */\r
318                 while(OSTC < 0x07)\r
319                 {\r
320                         /* Wait until X1 clock stabilization time. */\r
321                         portNOP();\r
322                 }\r
323                 \r
324                 /* Switch CPU clock to X1 oscillator. */\r
325                 MCM0 = 1;\r
326                 while(MCS != 1)\r
327                 {\r
328                         /* Wait until CPU and peripherals operate with fX1 clock. */\r
329                         portNOP();\r
330                 }\r
331 \r
332                 /* Stop the internal high-speed oscillator operation. */\r
333                 HIOSTOP = 1;\r
334                 \r
335                 /* Stop the XT1 oscillator operation. */\r
336                 XTSTOP  = 1;\r
337                 \r
338                 /* Operating frequency f = fx\r
339                    Change clock generator setting, if necessary. */\r
340                 CKC &= 0xF8;\r
341 \r
342                 /* From here onwards the X1 oscillator is supplied to the CPU. */\r
343         }\r
344         #endif\r
345         \r
346         /* LED port initialization - set port register. */\r
347         P7  = 0x80;\r
348         \r
349         /* Set port mode register. */\r
350         PM7 = 0x3F;\r
351         \r
352         /* Switch pin initialization - enable pull-up resistor. */\r
353         PU12_bit.no0  = 1;\r
354 \r
355         /* INTP0 is used by the button on the target board. */\r
356         \r
357         /* INTP0 disable. */\r
358         PMK0 = 1;                       \r
359         \r
360         /* INTP0 IF clear. */\r
361         PIF0 = 0;                       \r
362         EGN0_bit.no0  = 1;\r
363         \r
364         /* INTP0 priority low. */\r
365         PPR10 = 0;\r
366         PPR00 = 1;\r
367         \r
368         /* Enable ext. INTP0 interrupt */\r
369         PMK0  = 0;      \r
370 \r
371         return pdTRUE;\r
372 }\r
373 /*-----------------------------------------------------------*/\r
374 \r
375 void vRegTestError( void )\r
376 {\r
377         /* Called by the RegTest tasks if an error is found.  lRegTestStatus is\r
378         inspected by the check task. */\r
379         sRegTestStatus = pdFAIL;\r
380 \r
381         /* Do not return from here as the reg test tasks clobber all registers so\r
382         function calls may not function correctly. */\r
383         for( ;; );\r
384 }\r
385 /*-----------------------------------------------------------*/\r
386 \r
387 void vApplicationStackOverflowHook( void )\r
388 {\r
389         /* This will get called if an overflow is detected in the stack of a task.\r
390         Inspect pxCurrentTCB to see which was the offending task. */\r
391         for( ;; );\r
392 }\r
393 \r