2 * FreeRTOS Kernel V10.1.1
3 * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
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:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
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.
22 * http://www.FreeRTOS.org
23 * http://aws.amazon.com/freertos
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:
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.
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.
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.
56 /* Standard includes. */
60 /* Scheduler include files. */
64 /* Standard demo file headers. */
72 * Priority definitions for most of the tasks in the demo application. Some
73 * tasks just use the idle priority.
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 )
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 )
85 /* The LED toggled by the check task. */
86 #define mainLED_0 P7_bit.no6
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 )
92 /*-----------------------------------------------------------*/
95 * The function that defines the 'check' task as described at the top of this
98 static void vErrorChecks( void *pvParameters );
102 * This function is called from the C startup routine to setup the processor -
103 * in particular the clock source.
105 int __low_level_init(void);
108 * Functions that define the RegTest tasks as described at the top of this file.
110 extern void vRegTest1( void *pvParameters );
111 extern void vRegTest2( void *pvParameters );
114 * Function that defines the button push task as described at the top of this
117 extern void vButtonTask( void *pvParameters );
119 /*-----------------------------------------------------------*/
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;
126 /* 78K0R Option Byte Definition. Watchdog disabled, LVI enabled, OCD interface
128 __root __far const unsigned char OptionByte[] @ 0x00C0 =
130 WATCHDOG_DISABLED, LVI_ENABLED, RESERVED_FF, OCD_ENABLED
133 /* Security byte definition */
134 __root __far const unsigned char SecuIDCode[] @ 0x00C4 =
136 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
140 /*-----------------------------------------------------------*/
144 /* Creates all the tasks, then starts the scheduler. */
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();
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 );
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 );
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 );
161 #ifdef __IAR_78K0R_Kx3__
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();
171 /* Finally start the scheduler running. */
172 vTaskStartScheduler();
174 /* If this line is reached then vTaskStartScheduler() returned because there
175 was insufficient heap memory remaining for the idle task to be created. */
178 /*-----------------------------------------------------------*/
180 static void vErrorChecks( void *pvParameters )
182 TickType_t xToggleRate = mainNO_ERROR_TOGGLE_PERIOD, xLastWakeTime;
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
188 if( pvParameters != ( void * ) mainCHECK_PARAMETER_VALUE )
190 xToggleRate = mainERROR_TOGGLE_PERIOD;
193 /* Initialise xLastWakeTime before it is used. After this point it is not
194 written to directly. */
195 xLastWakeTime = xTaskGetTickCount();
197 /* Cycle for ever, delaying then checking all the other tasks are still
198 operating without error. */
201 /* Wait until it is time to check all the other tasks again. */
202 vTaskDelayUntil( &xLastWakeTime, xToggleRate );
204 if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
206 xToggleRate = mainERROR_TOGGLE_PERIOD;
209 if( sRegTestStatus != pdPASS )
211 xToggleRate = mainERROR_TOGGLE_PERIOD;
214 #ifdef __IAR_78K0R_Kx3__
216 /* Only the Kx3 runs all the tasks. */
217 if( xArePollingQueuesStillRunning() != pdTRUE)
219 xToggleRate = mainERROR_TOGGLE_PERIOD;
222 if( xAreSemaphoreTasksStillRunning() != pdTRUE)
224 xToggleRate = mainERROR_TOGGLE_PERIOD;
227 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
229 xToggleRate = mainERROR_TOGGLE_PERIOD;
232 if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
234 xToggleRate = mainERROR_TOGGLE_PERIOD;
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;
244 /*-----------------------------------------------------------*/
246 int __low_level_init(void)
248 unsigned char ucResetFlag = RESF;
250 portDISABLE_INTERRUPTS();
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
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 */
263 /* X1 external oszillation stopped. */
266 /* Enable internal high speed oszillation. */
270 /* Stop internal subsystem clock. */
273 /* Set clock speed. */
275 CKC &= (unsigned char)~0x07;
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 */
285 /* Set oscillation stabilization time. */
288 /* Set speed mode: fMX > 10MHz for Flash memory high speed operation. */
291 /* Start up X1 oscillator operation
292 Internal high-speed oscillator operating. */
295 /* Check oscillation stabilization time status. */
298 /* Wait until X1 clock stabilization time. */
302 /* Switch CPU clock to X1 oscillator. */
306 /* Wait until CPU and peripherals operate with fX1 clock. */
310 /* Stop the internal high-speed oscillator operation. */
313 /* Stop the XT1 oscillator operation. */
316 /* Operating frequency f = fx
317 Change clock generator setting, if necessary. */
320 /* From here onwards the X1 oscillator is supplied to the CPU. */
324 /* LED port initialization - set port register. */
327 /* Set port mode register. */
330 /* Switch pin initialization - enable pull-up resistor. */
333 /* INTP0 is used by the button on the target board. */
338 /* INTP0 IF clear. */
342 /* INTP0 priority low. */
346 /* Enable ext. INTP0 interrupt */
351 /*-----------------------------------------------------------*/
353 void vRegTestError( void )
355 /* Called by the RegTest tasks if an error is found. lRegTestStatus is
356 inspected by the check task. */
357 sRegTestStatus = pdFAIL;
359 /* Do not return from here as the reg test tasks clobber all registers so
360 function calls may not function correctly. */
363 /*-----------------------------------------------------------*/
365 void vApplicationStackOverflowHook( void )
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. */