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