]> git.sur5r.net Git - freertos/blob - Demo/NEC_78K0R_IAR/main.c
Continue 78K0R development.
[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 /* Standard includes. */\r
51 #include <stdlib.h>\r
52 #include <string.h>\r
53 \r
54 /* Scheduler include files. */\r
55 #include "FreeRTOS.h"\r
56 #include "task.h"\r
57 \r
58 /* Demo file headers. */\r
59 #include "int78K0R.h"\r
60 #include "PollQ.h"\r
61 #include "semtest.h"\r
62 #include "GenQTest.h"\r
63 #include "dynamic.h"\r
64 #include "blocktim.h"\r
65 \r
66 /*\r
67  * Priority definitions for most of the tasks in the demo application.  Some\r
68  * tasks just use the idle priority.\r
69  */\r
70 #define mainCHECK_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )\r
71 #define mainQUEUE_POLL_PRIORITY ( tskIDLE_PRIORITY + 1 )\r
72 #define mainSEMTEST_PRIORITY    ( tskIDLE_PRIORITY + 1 )\r
73 #define mainBUTTON_PRIORITY             ( configMAX_PRIORITIES - 1 )\r
74 #define mainGEN_QUEUE_PRIORITY  ( tskIDLE_PRIORITY )\r
75 \r
76 /* The period between executions of the check task. */\r
77 #define mainNO_ERROR_TOGGLE_PERIOD      ( ( portTickType ) 3000 / portTICK_RATE_MS  )\r
78 #define mainERROR_TOGGLE_PERIOD         ( ( portTickType ) 500 / portTICK_RATE_MS  )\r
79 \r
80 #define LED00   P7_bit.no6\r
81 #define LED01   P7_bit.no7\r
82 \r
83 /*\r
84  * 78K0R/Kx3 Option Byte Definition\r
85  * watchdog disabled, LVI enabled, OCD interface enabled\r
86  */\r
87 __root __far const unsigned portCHAR OptionByte[OPT_BYTES_SIZE] @ 0x00C0 =\r
88 {\r
89         WATCHDOG_DISABLED, LVI_ENABLED, RESERVED_FF, OCD_ENABLED\r
90 };\r
91 \r
92 /* Security Byte Definition */\r
93 __root __far const unsigned portCHAR SecuIDCode[SECU_ID_SIZE]   @ 0x00C4 =\r
94 {\r
95         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff\r
96 };\r
97 \r
98 /* The task function for the "Check" task. */\r
99 static void vErrorChecks( void *pvParameters );\r
100 \r
101 \r
102 /* 78K0R/Kx3 low level init Initialization of the System Clock */\r
103 int __low_level_init(void);\r
104 \r
105 extern void vRegTest1( void *pvParameters );\r
106 extern void vRegTest2( void *pvParameters );\r
107 extern void vButtonTask( void *pvParameters );\r
108 \r
109 static short sRegTestStatus = pdPASS;\r
110 \r
111 portSHORT main( void )\r
112 {\r
113         /* Create the standard demo tasks. */\r
114         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
115         vStartSemaphoreTasks(mainSEMTEST_PRIORITY);\r
116         vStartGenericQueueTasks( mainGEN_QUEUE_PRIORITY );\r
117         vStartDynamicPriorityTasks();\r
118         vCreateBlockTimeTasks();\r
119 \r
120         xTaskCreate( vButtonTask, "Button", configMINIMAL_STACK_SIZE, NULL, mainBUTTON_PRIORITY, NULL );\r
121         \r
122         /* Create the tasks defined within this file. */\r
123         xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, (void*)0x12345678, mainCHECK_TASK_PRIORITY, NULL );\r
124 \r
125         xTaskCreate( vRegTest1, "Reg1", configMINIMAL_STACK_SIZE, NULL, 0, NULL );\r
126         xTaskCreate( vRegTest2, "Reg2", configMINIMAL_STACK_SIZE, NULL, 0, NULL );      \r
127 \r
128 \r
129         vTaskStartScheduler();\r
130 \r
131         for( ;; );\r
132 }\r
133 /*-----------------------------------------------------------*/\r
134 \r
135 static void vErrorChecks( void *pvParameters )\r
136 {\r
137 portTickType xToggleRate = mainNO_ERROR_TOGGLE_PERIOD, xLastWakeTime;\r
138 \r
139         /* The pointer will only actually be either 3 or 2 bytes, depending on the\r
140         memory model. */\r
141         if( pvParameters != ( void * ) 0x12345678 )\r
142         {\r
143                 xToggleRate = mainERROR_TOGGLE_PERIOD;\r
144         }\r
145 \r
146         xLastWakeTime = xTaskGetTickCount();\r
147 \r
148         /* Cycle for ever, delaying then checking all the other tasks are still\r
149         operating without error. */\r
150         for( ;; )\r
151         {\r
152                 vTaskDelayUntil( &xLastWakeTime, xToggleRate );\r
153 \r
154                 if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
155                 {\r
156                         xToggleRate = mainERROR_TOGGLE_PERIOD;\r
157                 }\r
158 \r
159                 if( xArePollingQueuesStillRunning() != pdTRUE)\r
160                 {\r
161                         xToggleRate = mainERROR_TOGGLE_PERIOD;\r
162                 }\r
163 \r
164                 if( xAreSemaphoreTasksStillRunning() != pdTRUE)\r
165                 {\r
166                         xToggleRate = mainERROR_TOGGLE_PERIOD;\r
167                 }\r
168                 \r
169                 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
170                 {\r
171                         xToggleRate = mainERROR_TOGGLE_PERIOD;\r
172                 }\r
173 \r
174                 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
175                 {\r
176                         xToggleRate = mainERROR_TOGGLE_PERIOD;\r
177                 }\r
178 \r
179                 if( sRegTestStatus != pdPASS )\r
180                 {\r
181                         xToggleRate = mainERROR_TOGGLE_PERIOD;\r
182                 }\r
183 \r
184                 /* Toggle the LED. */\r
185                 LED00 = !LED00;\r
186         }\r
187 }\r
188 /*-----------------------------------------------------------*/\r
189 \r
190 int __low_level_init(void)\r
191 {\r
192 unsigned portCHAR resetflag = RESF;\r
193 \r
194         portDISABLE_INTERRUPTS();\r
195 \r
196         /*\r
197          * Clock Configuration:\r
198          * In this port, to use the internal high speed clock source of the microcontroller\r
199          * define the configCLOCK_SOURCE as 1 in FreeRTOSConfig.h.  To use an external\r
200          * clock define configCLOCK_SOURCE as 0.\r
201          */\r
202         #if configCLOCK_SOURCE == 1\r
203         {\r
204                 /*\r
205                  * Set XT1 and XT2 in Input Port Mode\r
206                  * Set X1  and X2  in Input Port Mode\r
207                  * High speed oszillation frequency 2MHz <= fMX <= 10MHz\r
208                  */\r
209                 CMC = 0x00;\r
210 \r
211                 /* X1 external oszillation stopped */\r
212                 MSTOP = 1;\r
213 \r
214                 /* enable internal high speed oszillation */\r
215                 HIOSTOP = 0;\r
216                 MCM0 = 0;\r
217 \r
218                 /* stop internal subsystem clock */\r
219                 XTSTOP = 1;\r
220 \r
221                 /* Set clock speed */\r
222                 CSS = 0;\r
223                 CKC &= (unsigned portCHAR)~0x07;\r
224                 CKC |= 0x00;\r
225         }\r
226         #else\r
227         {\r
228                 /*\r
229                  * XT1 and XT2 pin in input port mode\r
230                  * X1  and X2  pin in crystal resonator mode\r
231                  * High speed oszillation frequency 10MHz < fMX <= 20MHz\r
232                  */\r
233                 CMC   = 0x41;\r
234                 \r
235                 /* Set oscillation stabilization time */\r
236                 OSTS  = 0x07;\r
237                 \r
238                 /* Set speed mode: fMX > 10MHz for Flash memory high speed operation */\r
239                 OSMC  = 0x01;\r
240                 \r
241                 /*\r
242                  * Start up X1 oscillator operation\r
243                  * Internal high-speed oscillator operating\r
244                  */\r
245                 MSTOP = 0;\r
246                 \r
247                 /* Check oscillation stabilization time status */\r
248                 while(OSTC < 0x07)\r
249                 {\r
250                         /* wait until X1 clock stabilization time */\r
251                         portNOP();\r
252                 }\r
253                 \r
254                 /* Switch CPU clock to X1 oscillator */\r
255                 MCM0 = 1;\r
256                 while(MCS != 1)\r
257                 {\r
258                         /* wait until CPU and peripherals operate with fX1 clock */\r
259                         portNOP();\r
260                 }\r
261 \r
262                 /* Stop the internal high-speed oscillator operation */\r
263                 HIOSTOP = 1;\r
264                 \r
265                 /* Stop the XT1 oscillator operation */\r
266                 XTSTOP  = 1;\r
267                 \r
268                 /*\r
269                  * operating frequency f = fx\r
270                  * Change clock generator setting, if necessary\r
271                  */\r
272                 CKC &= 0xF8;\r
273 \r
274                 /* From here onwards the X1 oscillator is supplied to the CPU */\r
275         }\r
276         #endif\r
277         \r
278         /* LED Port Initialization - set Port Register */\r
279         P7  = 0x80;\r
280         \r
281         /* Set Port Mode Register */\r
282         PM7 = 0x3F;\r
283         \r
284         /* Switch Pin Initialization - enable pull-up resistor */\r
285         PU12_bit.no0  = 1;\r
286         \r
287         /* INTP0 disable */\r
288         PMK0 = 1;                       \r
289         \r
290         /* INTP0 IF clear */\r
291         PIF0 = 0;                       \r
292         EGN0_bit.no0  = 1;\r
293         \r
294         /* INTP0 priority low */\r
295         PPR10 = 0;\r
296         PPR00 = 1;\r
297         \r
298         /* enable ext. INTP0 interrupt */\r
299         PMK0  = 0;      \r
300 \r
301         return pdTRUE;\r
302 }\r
303 /*-----------------------------------------------------------*/\r
304 \r
305 static void prvLEDInit(void)\r
306 {\r
307 /* LED Port Initialization */\r
308         /* set Port Register */\r
309         P7  = 0x80;\r
310         /* set Port Mode Register */\r
311         PM7 = 0x3F;\r
312 \r
313 /* Switch Pin Initialization */\r
314         /* enable pull-up resistor */\r
315         PU12_bit.no0  = 1;\r
316         /* INTP0 disable */\r
317         PMK0 = 1;                       \r
318         /* INTP0 IF clear */\r
319         PIF0 = 0;                       \r
320         EGN0_bit.no0  = 1;\r
321         /* INTP0 priority low */\r
322         PPR10 = 0;\r
323         PPR00 = 1;\r
324         /* enable ext. INTP0 interrupt */\r
325         PMK0  = 0;\r
326 }\r
327 /*-----------------------------------------------------------*/\r
328 \r
329 void vRegTestError( void )\r
330 {\r
331         sRegTestStatus = pdFAIL;\r
332         for( ;; );\r
333 }\r