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