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