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