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