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