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