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