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