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