]> git.sur5r.net Git - freertos/blob - Demo/NEC_78K0R_IAR/main.c
First pass demo for 78K0R.
[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         vPrintInitialise();\r
119 \r
120         /* In this port, to use preemptive scheduler define configUSE_PREEMPTION\r
121          * as 1 in FreeRTOSconfig.h.  To use the cooperative scheduler define\r
122          * configUSE_PREEMPTION as 0.\r
123          */\r
124         vTaskStartScheduler();\r
125 \r
126         return 0;\r
127 }\r
128 /*-----------------------------------------------------------*/\r
129 \r
130 static void vErrorChecks( void *pvParameters )\r
131 {\r
132 portTickType xToggleRate = mainNO_ERROR_TOGGLE_PERIOD, xLastWakeTime;\r
133 \r
134         /* The pointer will only actually be either 3 or 2 bytes, depending on the\r
135         memory model. */\r
136         if( pvParameters != ( void * ) 0x12345678 )\r
137         {\r
138                 xToggleRate = mainERROR_TOGGLE_PERIOD;\r
139         }\r
140 \r
141         xLastWakeTime = xTaskGetTickCount();\r
142 \r
143         /* Cycle for ever, delaying then checking all the other tasks are still\r
144         operating without error. */\r
145         for( ;; )\r
146         {\r
147                 vTaskDelayUntil( &xLastWakeTime, xToggleRate );\r
148 \r
149                 if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
150                 {\r
151                         xToggleRate = mainERROR_TOGGLE_PERIOD;\r
152                 }\r
153 \r
154                 if( xArePollingQueuesStillRunning() != pdTRUE)\r
155                 {\r
156                         xToggleRate = mainERROR_TOGGLE_PERIOD;\r
157                 }\r
158 \r
159                 if( xAreSemaphoreTasksStillRunning() != pdTRUE)\r
160                 {\r
161                         xToggleRate = mainERROR_TOGGLE_PERIOD;\r
162                 }\r
163 \r
164                 if( sRegTestStatus != pdPASS )\r
165                 {\r
166                         xToggleRate = mainERROR_TOGGLE_PERIOD;\r
167                 }\r
168 \r
169                 /* Toggle the LED. */\r
170                 LED01 = !LED01;\r
171         }\r
172 }\r
173 /*-----------------------------------------------------------*/\r
174 \r
175 int __low_level_init(void)\r
176 {\r
177 unsigned portCHAR resetflag = RESF;\r
178 \r
179         portDISABLE_INTERRUPTS();\r
180 \r
181         /*\r
182          * Clock Configuration:\r
183          * In this port, to use the internal high speed clock source of the microcontroller\r
184          * define the configCLOCK_SOURCE as 1 in FreeRTOSConfig.h.  To use an external\r
185          * clock define configCLOCK_SOURCE as 0.\r
186          */\r
187         #if configCLOCK_SOURCE == 1\r
188         {\r
189                 /*\r
190                  * Set XT1 and XT2 in Input Port Mode\r
191                  * Set X1  and X2  in Input Port Mode\r
192                  * High speed oszillation frequency 2MHz <= fMX <= 10MHz\r
193                  */\r
194                 CMC = 0x00;\r
195 \r
196                 /* X1 external oszillation stopped */\r
197                 MSTOP = 1;\r
198 \r
199                 /* enable internal high speed oszillation */\r
200                 HIOSTOP = 0;\r
201                 MCM0 = 0;\r
202 \r
203                 /* stop internal subsystem clock */\r
204                 XTSTOP = 1;\r
205 \r
206                 /* Set clock speed */\r
207                 CSS = 0;\r
208                 CKC &= (unsigned portCHAR)~0x07;\r
209                 CKC |= 0x00;\r
210         }\r
211         #else\r
212         {\r
213                 /*\r
214                  * XT1 and XT2 pin in input port mode\r
215                  * X1  and X2  pin in crystal resonator mode\r
216                  * High speed oszillation frequency 10MHz < fMX <= 20MHz\r
217                  */\r
218                 CMC   = 0x41;\r
219                 \r
220                 /* Set oscillation stabilization time */\r
221                 OSTS  = 0x07;\r
222                 \r
223                 /* Set speed mode: fMX > 10MHz for Flash memory high speed operation */\r
224                 OSMC  = 0x01;\r
225                 \r
226                 /*\r
227                  * Start up X1 oscillator operation\r
228                  * Internal high-speed oscillator operating\r
229                  */\r
230                 MSTOP = 0;\r
231                 \r
232                 /* Check oscillation stabilization time status */\r
233                 while(OSTC < 0x07)\r
234                 {\r
235                         /* wait until X1 clock stabilization time */\r
236                         portNOP();\r
237                 }\r
238                 \r
239                 /* Switch CPU clock to X1 oscillator */\r
240                 MCM0 = 1;\r
241                 while(MCS != 1)\r
242                 {\r
243                         /* wait until CPU and peripherals operate with fX1 clock */\r
244                         portNOP();\r
245                 }\r
246 \r
247                 /* Stop the internal high-speed oscillator operation */\r
248                 HIOSTOP = 1;\r
249                 \r
250                 /* Stop the XT1 oscillator operation */\r
251                 XTSTOP  = 1;\r
252                 \r
253                 /*\r
254                  * operating frequency f = fx\r
255                  * Change clock generator setting, if necessary\r
256                  */\r
257                 CKC &= 0xF8;\r
258 \r
259                 /* From here onwards the X1 oscillator is supplied to the CPU */\r
260         }\r
261         #endif\r
262         \r
263         /* LED Port Initialization - set Port Register */\r
264         P7  = 0x80;\r
265         \r
266         /* Set Port Mode Register */\r
267         PM7 = 0x3F;\r
268         \r
269         /* Switch Pin Initialization - enable pull-up resistor */\r
270         PU12_bit.no0  = 1;\r
271         \r
272         /* INTP0 disable */\r
273         PMK0 = 1;                       \r
274         \r
275         /* INTP0 IF clear */\r
276         PIF0 = 0;                       \r
277         EGN0_bit.no0  = 1;\r
278         \r
279         /* INTP0 priority low */\r
280         PPR10 = 0;\r
281         PPR00 = 1;\r
282         \r
283         /* enable ext. INTP0 interrupt */\r
284         PMK0  = 0;      \r
285 \r
286         return pdTRUE;\r
287 }\r
288 /*-----------------------------------------------------------*/\r
289 \r
290 void vRegTestError( void )\r
291 {\r
292         sRegTestStatus = pdFAIL;\r
293         for( ;; );\r
294 }\r