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