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