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