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