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