]> git.sur5r.net Git - freertos/blobdiff - FreeRTOS/Demo/PIC32MEC14xx_MPLAB/src/Blinky_Demo/main_blinky.c
Add PIC32MEC14xx port and demo application.
[freertos] / FreeRTOS / Demo / PIC32MEC14xx_MPLAB / src / Blinky_Demo / main_blinky.c
diff --git a/FreeRTOS/Demo/PIC32MEC14xx_MPLAB/src/Blinky_Demo/main_blinky.c b/FreeRTOS/Demo/PIC32MEC14xx_MPLAB/src/Blinky_Demo/main_blinky.c
new file mode 100644 (file)
index 0000000..28c3359
--- /dev/null
@@ -0,0 +1,290 @@
+/*\r
+    FreeRTOS V8.2.2 - Copyright (C) 2015 Real Time Engineers Ltd.\r
+    All rights reserved\r
+\r
+    VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\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
+\r
+    ***************************************************************************\r
+    >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
+    >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
+    >>!   obliged to provide the source code for proprietary components     !<<\r
+    >>!   outside of the FreeRTOS kernel.                                   !<<\r
+    ***************************************************************************\r
+\r
+    FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
+    WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
+    FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
+    link: http://www.freertos.org/a00114.html\r
+\r
+    ***************************************************************************\r
+     *                                                                       *\r
+     *    FreeRTOS provides completely free yet professionally developed,    *\r
+     *    robust, strictly quality controlled, supported, and cross          *\r
+     *    platform software that is more than just the market leader, it     *\r
+     *    is the industry's de facto standard.                               *\r
+     *                                                                       *\r
+     *    Help yourself get started quickly while simultaneously helping     *\r
+     *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
+     *    tutorial book, reference manual, or both:                          *\r
+     *    http://www.FreeRTOS.org/Documentation                              *\r
+     *                                                                       *\r
+    ***************************************************************************\r
+\r
+    http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
+    the FAQ page "My application does not run, what could be wrong?".  Have you\r
+    defined configASSERT()?\r
+\r
+    http://www.FreeRTOS.org/support - In return for receiving this top quality\r
+    embedded software for free we request you assist our global community by\r
+    participating in the support forum.\r
+\r
+    http://www.FreeRTOS.org/training - Investing in training allows your team to\r
+    be as productive as possible as early as possible.  Now you can receive\r
+    FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
+    Ltd, and the world's leading authority on the world's leading RTOS.\r
+\r
+    http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
+    including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
+    compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
+\r
+    http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
+    Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
+\r
+    http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
+    Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
+    licenses offer ticketed support, indemnification and commercial middleware.\r
+\r
+    http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
+    engineered and independently SIL3 certified version for use in safety and\r
+    mission critical applications that require provable dependability.\r
+\r
+    1 tab == 4 spaces!\r
+*/\r
+\r
+/******************************************************************************\r
+ * NOTE 1:  This project provides two demo applications.  A simple blinky style\r
+ * project, and a more comprehensive test and demo application.  The\r
+ * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select\r
+ * between the two.  See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY\r
+ * in main.c.  This file implements the simply blinky style version.\r
+ *\r
+ * NOTE 2:  This file only contains the source code that is specific to the\r
+ * basic demo.  Generic functions, such FreeRTOS hook functions, and functions\r
+ * required to configure the hardware, are defined in main.c.\r
+ ******************************************************************************\r
+ *\r
+ * main_blinky() creates one queue, two tasks, and one software timer.  It then\r
+ * starts the scheduler.\r
+ *\r
+ * The Blinky Software Timer:\r
+ * This demonstrates an auto-reload software timer.  The timer callback function\r
+ * does nothing but toggle an LED.\r
+ *\r
+ * The Queue Send Task:\r
+ * The queue send task is implemented by prvQueueSendTask() in main_blinky.c.\r
+ * prvQueueSendTask() repeatedly blocks for 200 milliseconds before sending the\r
+ * value 100 to the queue that was created in main_blinky().\r
+ *\r
+ * The Queue Receive Task:\r
+ * The queue receive task is implemented by prvQueueReceiveTask() in\r
+ * main_blinky.c.  prvQueueReceiveTask() repeatedly blocks on attempts to read\r
+ * from the queue that was created in main_blinky(), toggling an LED each time\r
+ * data is received. The queue send task sends data to the queue every 200\r
+ * milliseconds, so the LED will toggle every 200 milliseconds.\r
+ */\r
+\r
+/* Standard includes. */\r
+#include <stdio.h>\r
+\r
+/* Kernel includes. */\r
+#include "FreeRTOS.h"\r
+#include "task.h"\r
+#include "queue.h"\r
+#include "timers.h"\r
+\r
+/* Priorities at which the tasks are created. */\r
+#define mainQUEUE_SEND_TASK_PRIORITY   ( tskIDLE_PRIORITY + 1 )\r
+#define mainQUEUE_RECEIVE_TASK_PRIORITY        ( tskIDLE_PRIORITY + 2 )\r
+\r
+/* The rate at which data is sent to the queue.  The 200ms value is converted\r
+to ticks using the portTICK_PERIOD_MS constant. */\r
+#define mainQUEUE_SEND_FREQUENCY_MS            ( 200 / portTICK_PERIOD_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
+/* Values passed to the two tasks just to check the task parameter\r
+functionality. */\r
+#define mainQUEUE_SEND_PARAMETER               ( 0x1111UL )\r
+#define mainQUEUE_RECEIVE_PARAMETER            ( 0x22UL )\r
+\r
+/* The period of the blinky software timer.  The period is specified in ms and\r
+converted to ticks using the portTICK_PERIOD_MS constant. */\r
+#define mainBLINKY_TIMER_PERIOD                        ( 50 / portTICK_PERIOD_MS )\r
+\r
+/* The LED used by the communicating tasks and the blinky timer respectively. */\r
+#define mainTASKS_LED                                  ( 0 )\r
+#define mainTIMER_LED                                  ( 1 )\r
+\r
+/* Misc. */\r
+#define mainDONT_BLOCK                                 ( 0 )\r
+\r
+/*-----------------------------------------------------------*/\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 callback function for the blinky software timer, as described at the top\r
+ * of this file.\r
+ */\r
+static void prvBlinkyTimerCallback( TimerHandle_t xTimer );\r
+\r
+/*\r
+ * Called by main() to create the simply blinky style application if\r
+ * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.\r
+ */\r
+void main_blinky( void );\r
+\r
+/*\r
+ * LED toggle function that uses a critical section to ensure thread safety.\r
+ */\r
+extern void vToggleLED( uint8_t ucLED );\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+/* The queue used by both tasks. */\r
+static QueueHandle_t xQueue = NULL;\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+void main_blinky( void )\r
+{\r
+TimerHandle_t xTimer;\r
+\r
+       /* Create the queue. */\r
+       xQueue = xQueueCreate( mainQUEUE_LENGTH, sizeof( unsigned long ) );\r
+       configASSERT( xQueue );\r
+\r
+       if( xQueue != NULL )\r
+       {\r
+               /* Create the two tasks as described in the comments at the top of this\r
+               file. */\r
+               xTaskCreate(    prvQueueReceiveTask,                                    /* The function that implements the task. */\r
+                                               "Rx",                                                                   /* The text name assigned to the task - for debug only as it is not used by the kernel. */\r
+                                               configMINIMAL_STACK_SIZE,                               /* The size of the stack to allocate to the task. */\r
+                                               ( void * ) mainQUEUE_RECEIVE_PARAMETER, /* The parameter passed to the task - just to check the functionality. */\r
+                                               mainQUEUE_RECEIVE_TASK_PRIORITY,                /* The priority assigned to the task. */\r
+                                               NULL );                                                                 /* The task handle is not required, so NULL is passed. */\r
+\r
+               xTaskCreate( prvQueueSendTask, "TX", configMINIMAL_STACK_SIZE, ( void * ) mainQUEUE_SEND_PARAMETER, mainQUEUE_SEND_TASK_PRIORITY, NULL );\r
+\r
+\r
+               /* Create the blinky software timer as described at the top of this file. */\r
+               xTimer = xTimerCreate(  "Blinky",                                       /* A text name, purely to help debugging. */\r
+                                                               ( mainBLINKY_TIMER_PERIOD ),/* The timer period. */\r
+                                                               pdTRUE,                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
+                                                               ( void * ) 0,                           /* The ID is not used, so can be set to anything. */\r
+                                                               prvBlinkyTimerCallback );       /* The callback function that inspects the status of all the other tasks. */\r
+               configASSERT( xTimer );\r
+\r
+               if( xTimer != NULL )\r
+               {\r
+                       xTimerStart( xTimer, mainDONT_BLOCK );\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\r
+       line will never be reached.  If the following line does execute, then\r
+       there was insufficient FreeRTOS heap memory available for the idle and/or\r
+       timer tasks     to be created.  See the memory management section on the\r
+       FreeRTOS web site for more details. http://www.freertos.org/a00111.html */\r
+       for( ;; );\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+static void prvQueueSendTask( void *pvParameters )\r
+{\r
+TickType_t xNextWakeTime;\r
+const unsigned long ulValueToSend = 100UL;\r
+\r
+       /* Remove compiler warnings in the case that configASSERT() is not defined. */\r
+       ( void ) pvParameters;\r
+\r
+       /* Check the task parameter is as expected. */\r
+       configASSERT( ( ( unsigned long ) pvParameters ) == mainQUEUE_SEND_PARAMETER );\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 the 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, 0U );\r
+       }\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+static void prvQueueReceiveTask( void *pvParameters )\r
+{\r
+unsigned long ulReceivedValue;\r
+\r
+       /* Remove compiler warnings in the case where configASSERT() is not defined. */\r
+       ( void ) pvParameters;\r
+\r
+       /* Check the task parameter is as expected. */\r
+       configASSERT( ( ( unsigned long ) pvParameters ) == mainQUEUE_RECEIVE_PARAMETER );\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
+                       vToggleLED( mainTASKS_LED );\r
+                       ulReceivedValue = 0U;\r
+               }\r
+       }\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+static void prvBlinkyTimerCallback( TimerHandle_t xTimer )\r
+{\r
+       /* Avoid compiler warnings. */\r
+       ( void ) xTimer;\r
+\r
+       /* This function is called when the blinky software time expires.  All the\r
+       function does is toggle the LED.  LED mainTIMER_LED should therefore toggle\r
+       with the period set by mainBLINKY_TIMER_PERIOD. */\r
+       vToggleLED( mainTIMER_LED );\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r