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