--- /dev/null
+/*\r
+ FreeRTOS V7.0.1 - Copyright (C) 2011 Real Time Engineers Ltd.\r
+\r
+\r
+ FreeRTOS supports many tools and architectures. V7.0.0 is sponsored by:\r
+ Atollic AB - Atollic provides professional embedded systems development\r
+ tools for C/C++ development, code analysis and test automation.\r
+ See http://www.atollic.com\r
+\r
+\r
+ ***************************************************************************\r
+ * *\r
+ * FreeRTOS tutorial books are available in pdf and paperback. *\r
+ * Complete, revised, and edited pdf reference manuals are also *\r
+ * available. *\r
+ * *\r
+ * Purchasing FreeRTOS documentation will not only help you, by *\r
+ * ensuring you get running as quickly as possible and with an *\r
+ * in-depth knowledge of how to use FreeRTOS, it will also help *\r
+ * the FreeRTOS project to continue with its mission of providing *\r
+ * professional grade, cross platform, de facto standard solutions *\r
+ * for microcontrollers - completely free of charge! *\r
+ * *\r
+ * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *\r
+ * *\r
+ * Thank you for using FreeRTOS, and thank you for your support! *\r
+ * *\r
+ ***************************************************************************\r
+\r
+\r
+ This file is part of the FreeRTOS distribution.\r
+\r
+ FreeRTOS is free software; you can redistribute it and/or modify it under\r
+ the terms of the GNU General Public License (version 2) as published by the\r
+ Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
+ >>>NOTE<<< The modification to the GPL is included to allow you to\r
+ distribute a combined work that includes FreeRTOS without being obliged to\r
+ provide the source code for proprietary components outside of the FreeRTOS\r
+ kernel. FreeRTOS is distributed in the hope that it will be useful, but\r
+ WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
+ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for\r
+ more details. You should have received a copy of the GNU General Public\r
+ License and the FreeRTOS license exception along with FreeRTOS; if not it\r
+ can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
+ by writing to Richard Barry, contact details for whom are available on the\r
+ FreeRTOS WEB site.\r
+\r
+ 1 tab == 4 spaces!\r
+\r
+ http://www.FreeRTOS.org - Documentation, latest information, license and\r
+ contact details.\r
+\r
+ http://www.SafeRTOS.com - A version that is certified for use in safety\r
+ critical systems.\r
+\r
+ http://www.OpenRTOS.com - Commercial support, development, porting,\r
+ licensing and training services.\r
+*/\r
+\r
+/*\r
+ * main-blinky.c is included when the "Blinky" build configuration is used.\r
+ * main-full.c is included when the "Full" build configuration is used.\r
+ *\r
+ * main-blinky.c (this file) defines a very simple demo that creates two tasks,\r
+ * one queue, and one timer. It also demonstrates how Cortex-M3 interrupts can\r
+ * interact with FreeRTOS tasks/timers.\r
+ *\r
+ * This simple demo project runs on the SK-FM3-100PMC evaluation board, which\r
+ * is populated with an MB9B500 microcontroller.\r
+ *\r
+ * The idle hook function:\r
+ * The idle hook function demonstrates how to query the amount of FreeRTOS heap\r
+ * space that is remaining (see vApplicationIdleHook() defined in this file).\r
+ *\r
+ * The main() Function:\r
+ * main() creates one software timer, one queue, and two tasks. It then starts\r
+ * the scheduler.\r
+ *\r
+ * The Queue Send Task:\r
+ * The queue send task is implemented by the prvQueueSendTask() function in\r
+ * this file. prvQueueSendTask() sits in a loop that causes it to repeatedly\r
+ * block for 200 milliseconds, before sending the value 100 to the queue that\r
+ * was created within main(). Once the value is sent, the task loops back\r
+ * around to block for another 200 milliseconds.\r
+ *\r
+ * The Queue Receive Task:\r
+ * The queue receive task is implemented by the prvQueueReceiveTask() function\r
+ * in this file. prvQueueReceiveTask() sits in a loop that causes it to\r
+ * repeatedly attempt to read data from the queue that was created within\r
+ * main(). When data is received, the task checks the value of the data, and\r
+ * if the value equals the expected 100, toggles an LED on the 7 segment\r
+ * display. The 'block time' parameter passed to the queue receive function\r
+ * specifies that the task should be held in the Blocked state indefinitely to\r
+ * wait for data to be available on the queue. The queue receive task will only\r
+ * leave the Blocked state when the queue send task writes to the queue. As the\r
+ * queue send task writes to the queue every 200 milliseconds, the queue receive\r
+ * task leaves the Blocked state every 200 milliseconds, and therefore toggles\r
+ * the LED every 200 milliseconds.\r
+ *\r
+ * The LED Software Timer and the Button Interrupt:\r
+ * The user button SW2 is configured to generate an interrupt each time it is\r
+ * pressed. The interrupt service routine switches an LED in the 7 segment\r
+ * display on, and resets the LED software timer. The LED timer has a 5000\r
+ * millisecond (5 second) period, and uses a callback function that is defined\r
+ * to just turn the LED off again. Therefore, pressing the user button will\r
+ * turn the LED on, and the LED will remain on until a full five seconds pass\r
+ * without the button being pressed.\r
+ */\r
+\r
+/* Kernel includes. */\r
+#include "FreeRTOS.h"\r
+#include "task.h"\r
+#include "queue.h"\r
+#include "timers.h"\r
+\r
+/* Freescale includes. */\r
+#include "common.h"\r
+\r
+/* Priorities at which the tasks are created. */\r
+#define mainQUEUE_RECEIVE_TASK_PRIORITY ( tskIDLE_PRIORITY + 2 )\r
+#define mainQUEUE_SEND_TASK_PRIORITY ( tskIDLE_PRIORITY + 1 )\r
+\r
+/* The rate at which data is sent to the queue, specified in milliseconds, and\r
+converted to ticks using the portTICK_RATE_MS constant. */\r
+#define mainQUEUE_SEND_FREQUENCY_MS ( 200 / portTICK_RATE_MS )\r
+\r
+/* The number of items the queue can hold. This is 1 as the receive task\r
+will remove items as they are added, meaning the send task should always find\r
+the queue empty. */\r
+#define mainQUEUE_LENGTH ( 1 )\r
+\r
+/* The LED toggle by the queue receive task (blue). */\r
+#define mainTASK_CONTROLLED_LED 10\r
+\r
+/* The LED turned on by the button interrupt, and turned off by the LED timer. */\r
+#define mainTIMER_CONTROLLED_LED 29\r
+\r
+#define mainGPIO_E_VECTOR ( 107 - 16 )\r
+\r
+#define GPIO_PIN_MASK 0x1Fu\r
+#define GPIO_PIN( x ) ( ( ( 1 ) << ( x & GPIO_PIN_MASK ) ) )\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+/*\r
+ * Setup the NVIC, LED outputs, and button inputs.\r
+ */\r
+static void prvSetupHardware( void );\r
+\r
+/*\r
+ * The tasks as described in the comments at the top of this file.\r
+ */\r
+static void prvQueueReceiveTask( void *pvParameters );\r
+static void prvQueueSendTask( void *pvParameters );\r
+\r
+/*\r
+ * The LED timer callback function. This does nothing but switch off the\r
+ * LED defined by the mainTIMER_CONTROLLED_LED constant.\r
+ */\r
+static void vLEDTimerCallback( xTimerHandle xTimer );\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+/* The queue used by both tasks. */\r
+static xQueueHandle xQueue = NULL;\r
+\r
+/* The LED software timer. This uses vLEDTimerCallback() as its callback\r
+function. */\r
+static xTimerHandle xLEDTimer = NULL;\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+void main( void )\r
+{\r
+ /* Configure the NVIC, LED outputs and button inputs. */\r
+ prvSetupHardware();\r
+\r
+ /* Create the queue. */\r
+ xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );\r
+\r
+ if( xQueue != NULL )\r
+ {\r
+ /* Start the two tasks as described in the comments at the top of this\r
+ file. */\r
+ xTaskCreate( prvQueueReceiveTask, ( signed char * ) "Rx", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_RECEIVE_TASK_PRIORITY, NULL );\r
+ xTaskCreate( prvQueueSendTask, ( signed char * ) "TX", configMINIMAL_STACK_SIZE, NULL, mainQUEUE_SEND_TASK_PRIORITY, NULL );\r
+\r
+ /* Create the software timer that is responsible for turning off the LED\r
+ if the button is not pushed within 5000ms, as described at the top of\r
+ this file. */\r
+ xLEDTimer = xTimerCreate( ( const signed char * ) "LEDTimer", /* A text name, purely to help debugging. */\r
+ ( 5000 / portTICK_RATE_MS ), /* The timer period, in this case 5000ms (5s). */\r
+ pdFALSE, /* This is a one shot timer, so xAutoReload is set to pdFALSE. */\r
+ ( void * ) 0, /* The ID is not used, so can be set to anything. */\r
+ vLEDTimerCallback /* The callback function that switches the LED off. */\r
+ );\r
+\r
+ /* Start the tasks and timer running. */\r
+ vTaskStartScheduler();\r
+ }\r
+\r
+ /* If all is well, the scheduler will now be running, and the following line\r
+ will never be reached. If the following line does execute, then there was\r
+ insufficient FreeRTOS heap memory available for the idle and/or timer tasks\r
+ to be created. See the memory management section on the FreeRTOS web site\r
+ for more details. */\r
+ for( ;; );\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+static void vLEDTimerCallback( xTimerHandle xTimer )\r
+{\r
+ /* The timer has expired - so no button pushes have occurred in the last\r
+ five seconds - turn the LED off. NOTE - accessing the LED port should use\r
+ a critical section because it is accessed from multiple tasks, and the\r
+ button interrupt - in this trivial case, for simplicity, the critical\r
+ section is omitted. */\r
+ GPIOA_PDOR |= GPIO_PDOR_PDO( GPIO_PIN( mainTIMER_CONTROLLED_LED ) );\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+/* The ISR executed when the user button is pushed. */\r
+void vPort_E_ISRHandler( void )\r
+{\r
+portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
+\r
+ /* The button was pushed, so ensure the LED is on before resetting the\r
+ LED timer. The LED timer will turn the LED off if the button is not\r
+ pushed within 5000ms. */\r
+ GPIOA_PDOR &= ~GPIO_PDOR_PDO( GPIO_PIN( mainTIMER_CONTROLLED_LED ) );\r
+\r
+ /* This interrupt safe FreeRTOS function can be called from this interrupt\r
+ because the interrupt priority is below the\r
+ configMAX_SYSCALL_INTERRUPT_PRIORITY setting in FreeRTOSConfig.h. */\r
+ xTimerResetFromISR( xLEDTimer, &xHigherPriorityTaskWoken );\r
+\r
+ /* Clear the interrupt before leaving. This just clears all the interrupts\r
+ for simplicity, as only one is actually used in this simple demo anyway. */\r
+ PORTE_ISFR = 0xFFFFFFFFUL;\r
+\r
+ /* If calling xTimerResetFromISR() caused a task (in this case the timer\r
+ service/daemon task) to unblock, and the unblocked task has a priority\r
+ higher than or equal to the task that was interrupted, then\r
+ xHigherPriorityTaskWoken will now be set to pdTRUE, and calling\r
+ portEND_SWITCHING_ISR() will ensure the unblocked task runs next. */\r
+ portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+static void prvQueueSendTask( void *pvParameters )\r
+{\r
+portTickType xNextWakeTime;\r
+const unsigned long ulValueToSend = 100UL;\r
+\r
+ /* Initialise xNextWakeTime - this only needs to be done once. */\r
+ xNextWakeTime = xTaskGetTickCount();\r
+\r
+ for( ;; )\r
+ {\r
+ /* Place this task in the blocked state until it is time to run again.\r
+ The block time is specified in ticks, the constant used converts ticks\r
+ to ms. While in the Blocked state this task will not consume any CPU\r
+ time. */\r
+ vTaskDelayUntil( &xNextWakeTime, mainQUEUE_SEND_FREQUENCY_MS );\r
+\r
+ /* Send to the queue - causing the queue receive task to unblock and\r
+ toggle an LED. 0 is used as the block time so the sending operation\r
+ will not block - it shouldn't need to block as the queue should always\r
+ be empty at this point in the code. */\r
+ xQueueSend( xQueue, &ulValueToSend, 0 );\r
+ }\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+static void prvQueueReceiveTask( void *pvParameters )\r
+{\r
+unsigned long ulReceivedValue;\r
+\r
+ for( ;; )\r
+ {\r
+ /* Wait until something arrives in the queue - this task will block\r
+ indefinitely provided INCLUDE_vTaskSuspend is set to 1 in\r
+ FreeRTOSConfig.h. */\r
+ xQueueReceive( xQueue, &ulReceivedValue, portMAX_DELAY );\r
+\r
+ /* To get here something must have been received from the queue, but\r
+ is it the expected value? If it is, toggle the LED. */\r
+ if( ulReceivedValue == 100UL )\r
+ {\r
+ /* NOTE - accessing the LED port should use a critical section\r
+ because it is accessed from multiple tasks, and the button interrupt\r
+ - in this trivial case, for simplicity, the critical section is\r
+ omitted. */\r
+ GPIOA_PTOR |= GPIO_PDOR_PDO( GPIO_PIN( mainTASK_CONTROLLED_LED ) );\r
+ }\r
+ }\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+static void prvSetupHardware( void )\r
+{\r
+ /* Turn on all port clocks */\r
+ SIM_SCGC5 = SIM_SCGC5_PORTA_MASK | SIM_SCGC5_PORTB_MASK | SIM_SCGC5_PORTC_MASK | SIM_SCGC5_PORTD_MASK | SIM_SCGC5_PORTE_MASK;\r
+\r
+ /* Enable the interrupt on SW1. */\r
+ PORTE_PCR26 = PORT_PCR_MUX( 1 ) | PORT_PCR_IRQC( 0xA ) | PORT_PCR_PE_MASK | PORT_PCR_PS_MASK;\r
+\r
+ enable_irq( mainGPIO_E_VECTOR );\r
+ \r
+ /* Set PTA10, PTA11, PTA28, and PTA29 (connected to LED's) for GPIO\r
+ functionality. */\r
+ PORTA_PCR10 = ( 0 | PORT_PCR_MUX( 1 ) );\r
+ PORTA_PCR11 = ( 0 | PORT_PCR_MUX( 1 ) );\r
+ PORTA_PCR28 = ( 0 | PORT_PCR_MUX( 1 ) );\r
+ PORTA_PCR29 = ( 0 | PORT_PCR_MUX( 1 ) );\r
+ \r
+ /* Change PTA10, PTA11, PTA28, PTA29 to outputs. */\r
+ GPIOA_PDDR=GPIO_PDDR_PDD( GPIO_PIN( mainTASK_CONTROLLED_LED ) | GPIO_PIN( mainTIMER_CONTROLLED_LED ) ); \r
+\r
+ /* Start with LEDs off. */\r
+ GPIOA_PTOR = ~0U;\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+void vApplicationMallocFailedHook( void )\r
+{\r
+ /* Called if a call to pvPortMalloc() fails because there is insufficient\r
+ free memory available in the FreeRTOS heap. pvPortMalloc() is called\r
+ internally by FreeRTOS API functions that create tasks, queues, software\r
+ timers, and semaphores. The size of the FreeRTOS heap is set by the\r
+ configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */\r
+ for( ;; );\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName )\r
+{\r
+ ( void ) pcTaskName;\r
+ ( void ) pxTask;\r
+\r
+ /* Run time stack overflow checking is performed if\r
+ configconfigCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook\r
+ function is called if a stack overflow is detected. */\r
+ for( ;; );\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+void vApplicationTickHook( void )\r
+{\r
+ /* A tick hook is used by the "Full" build configuration. The Full and\r
+ blinky build configurations share a FreeRTOSConfig.h header file, so this\r
+ simple build configuration also has to define a tick hook - even though it\r
+ does not actually use it for anything. */\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+void vApplicationIdleHook( void )\r
+{\r
+volatile size_t xFreeHeapSpace;\r
+\r
+ /* This function is called on each cycle of the idle task. In this case it\r
+ does nothing useful, other than report the amount of FreeRTOS heap that\r
+ remains unallocated. */\r
+ xFreeHeapSpace = xPortGetFreeHeapSize();\r
+\r
+ if( xFreeHeapSpace > 100 )\r
+ {\r
+ /* By now, the kernel has allocated everything it is going to, so\r
+ if there is a lot of heap remaining unallocated then\r
+ the value of configTOTAL_HEAP_SIZE in FreeRTOSConfig.h can be\r
+ reduced accordingly. */\r
+ }\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+\r
+\r
+\r