2 * FreeRTOS Kernel V10.0.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. 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
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
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
30 * This is a very simple demo that creates two tasks, one queue, and one
\r
31 * software timer. For a much more complete and complex example select either
\r
32 * the Debug or Debug_with_optimisation build configurations within the HEW,
\r
33 * which build main_full.c in place of this file.
\r
35 * One task (the queue receive task) blocks on the queue to wait for data to
\r
36 * arrive, toggling LED0 each time '100' is received. The other task (the
\r
37 * queue send task) repeatedly blocks for a fixed period before sending '100'
\r
38 * to the queue (causing the first task to toggle the LED).
\r
40 * The software timer is configured to auto-reload. The timer callback
\r
41 * function periodically toggles LED1.
\r
44 /* Kernel includes. */
\r
45 #include "FreeRTOS.h"
\r
50 /* Priorities at which the tasks are created. */
\r
51 #define configQUEUE_RECEIVE_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
52 #define configQUEUE_SEND_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
54 /* The rate at which data is sent to the queue, specified in milliseconds. */
\r
55 #define mainQUEUE_SEND_PERIOD_MS ( 500 / portTICK_PERIOD_MS )
\r
57 /* The period of the software timer, specified in milliseconds. */
\r
58 #define mainSOFTWARE_TIMER_PERIOD_MS ( 150 / portTICK_PERIOD_MS )
\r
60 /* The number of items the queue can hold. This is 1 as the receive task
\r
61 will remove items as they are added so the send task should always find the
\r
63 #define mainQUEUE_LENGTH ( 1 )
\r
65 /* The LEDs toggle by the task and timer respectively. */
\r
66 #define mainTASK_LED ( 0 )
\r
67 #define mainTIMER_LED ( 1 )
\r
70 * The tasks as defined at the top of this file.
\r
72 static void prvQueueReceiveTask( void *pvParameters );
\r
73 static void prvQueueSendTask( void *pvParameters );
\r
76 * The callback function used by the software timer.
\r
78 static void prvBlinkyTimerCallback( TimerHandle_t xTimer );
\r
80 /* The queue used by both tasks. */
\r
81 static QueueHandle_t xQueue = NULL;
\r
83 /* This variable is not used by this simple Blinky example. It is defined
\r
84 purely to allow the project to link as it is used by the full project. */
\r
85 volatile unsigned long ulHighFrequencyTickCount = 0UL;
\r
86 /*-----------------------------------------------------------*/
\r
90 TimerHandle_t xTimer;
\r
92 /* Turn all LEDs off. */
\r
93 vParTestInitialise();
\r
95 /* Create the queue. */
\r
96 xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );
\r
98 /* Create the software timer, as described at the top of this file. */
\r
99 xTimer = xTimerCreate( "BlinkyTimer", /* Just a text name to make debugging easier - not used by the scheduler. */
\r
100 mainSOFTWARE_TIMER_PERIOD_MS, /* The timer period. */
\r
101 pdTRUE, /* Set to pdTRUE for periodic timer, or pdFALSE for one-shot timer. */
\r
102 NULL, /* The timer ID is not required. */
\r
103 prvBlinkyTimerCallback ); /* The function executed when the timer expires. */
\r
105 if( xTimer != NULL )
\r
107 /* Start the timer - it will not actually start running until the
\r
108 scheduler has started. The block time is set to 0, although, because
\r
109 xTimerStart() is being called before the scheduler has been started,
\r
110 the any block time specified would be ignored anyway. */
\r
111 xTimerStart( xTimer, 0UL );
\r
114 if( xQueue != NULL )
\r
116 /* Start the two tasks as described at the top of this file. */
\r
117 xTaskCreate( prvQueueReceiveTask, /* The function that implements the task. */
\r
118 "Rx", /* Just a text name to make debugging easier - not used by the scheduler. */
\r
119 configMINIMAL_STACK_SIZE, /* The size of the task stack, in words. */
\r
120 NULL, /* The task parameter is not used. */
\r
121 configQUEUE_RECEIVE_TASK_PRIORITY, /* The priority assigned to the task when it is created. */
\r
122 NULL ); /* The task handle is not used. */
\r
124 xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, NULL, configQUEUE_SEND_TASK_PRIORITY, NULL );
\r
126 /* Start the tasks running. */
\r
127 vTaskStartScheduler();
\r
130 /* If all is well we will never reach here as the scheduler will now be
\r
131 running. If we do reach here then it is likely that there was insufficient
\r
132 heap available for the idle task to be created. */
\r
135 /*-----------------------------------------------------------*/
\r
137 static void prvQueueSendTask( void *pvParameters )
\r
139 TickType_t xNextWakeTime;
\r
140 const unsigned long ulValueToSend = 100UL;
\r
142 /* Initialise xNextWakeTime - this only needs to be done once. */
\r
143 xNextWakeTime = xTaskGetTickCount();
\r
147 /* Place this task in the blocked state until it is time to run again.
\r
148 The block state is specified in ticks, the constant used converts ticks
\r
150 vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_PERIOD_MS );
\r
152 /* Send to the queue - causing the queue receive task to flash its LED. 0
\r
153 is used so the send does not block - it shouldn't need to as the queue
\r
154 should always be empty here. */
\r
155 xQueueSend( xQueue, &ulValueToSend, 0 );
\r
158 /*-----------------------------------------------------------*/
\r
160 static void prvQueueReceiveTask( void *pvParameters )
\r
162 unsigned long ulReceivedValue;
\r
166 /* Wait until something arives in the queue - this will block
\r
167 indefinitely provided INCLUDE_vTaskSuspend is set to 1 in
\r
168 FreeRTOSConfig.h. */
\r
169 xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );
\r
171 /* To get here something must have arrived, but is it the expected
\r
172 value? If it is, toggle the LED. */
\r
173 if( ulReceivedValue == 100UL )
\r
175 vParTestToggleLED( mainTASK_LED );
\r
179 /*-----------------------------------------------------------*/
\r
181 static void prvBlinkyTimerCallback( TimerHandle_t xTimer )
\r
183 /* The software timer does nothing but toggle an LED. */
\r
184 vParTestToggleLED( mainTIMER_LED );
\r
186 /*-----------------------------------------------------------*/
\r
188 void vApplicationSetupTimerInterrupt( void )
\r
190 /* Enable compare match timer 0. */
\r
193 /* Interrupt on compare match. */
\r
194 CMT0.CMCR.BIT.CMIE = 1;
\r
196 /* Set the compare match value. */
\r
197 CMT0.CMCOR = ( unsigned short ) ( ( ( configPERIPHERAL_CLOCK_HZ / configTICK_RATE_HZ ) -1 ) / 8 );
\r
199 /* Divide the PCLK by 8. */
\r
200 CMT0.CMCR.BIT.CKS = 0;
\r
202 /* Enable the interrupt... */
\r
203 _IEN( _CMT0_CMI0 ) = 1;
\r
205 /* ...and set its priority to the application defined kernel priority. */
\r
206 _IPR( _CMT0_CMI0 ) = configKERNEL_INTERRUPT_PRIORITY;
\r
208 /* Start the timer. */
\r
209 CMT.CMSTR0.BIT.STR0 = 1;
\r
211 /*-----------------------------------------------------------*/
\r
213 /* This function is explained by the comments above its prototype at the top
\r
215 void vApplicationMallocFailedHook( void )
\r
219 /*-----------------------------------------------------------*/
\r
221 /* This function is explained by the comments above its prototype at the top
\r
223 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
\r
227 /*-----------------------------------------------------------*/
\r
229 /* This function is explained by the comments above its prototype at the top
\r
231 void vApplicationIdleHook( void )
\r
233 /* Just to prevent the variable getting optimised away. */
\r
234 ( void ) ulHighFrequencyTickCount;
\r
236 /*-----------------------------------------------------------*/
\r