]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/QueueSetPolling.c
Update license information text files for the CLI, TCP and UDP products to be correct...
[freertos] / FreeRTOS / Demo / Common / Minimal / QueueSetPolling.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\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
11  *\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
15  *\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
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /*\r
30  * Tests the use of queue sets.\r
31  *\r
32  * A receive task creates a number of queues and adds them to a queue set before\r
33  * blocking on the queue set receive.  A transmit task and (optionally) an\r
34  * interrupt repeatedly unblocks the receive task by sending messages to the\r
35  * queues in a pseudo random order.  The receive task removes the messages from\r
36  * the queues and flags an error if the received message does not match that\r
37  * expected.  The task sends values in the range 0 to\r
38  * queuesetINITIAL_ISR_TX_VALUE, and the ISR sends value in the range\r
39  * queuesetINITIAL_ISR_TX_VALUE to ULONG_MAX.\r
40  */\r
41 \r
42 \r
43 /* Standard includes. */\r
44 #include <stdlib.h>\r
45 #include <limits.h>\r
46 \r
47 /* Kernel includes. */\r
48 #include "FreeRTOS.h"\r
49 #include "task.h"\r
50 #include "queue.h"\r
51 \r
52 /* Demo includes. */\r
53 #include "QueueSetPolling.h"\r
54 \r
55 /* The length of each created queue. */\r
56 #define setpollQUEUE_LENGTH     10\r
57 \r
58 /* Block times used in this demo.  A block time or 0 means "don't block". */\r
59 #define setpollDONT_BLOCK 0\r
60 \r
61 /* The ISR sends to the queue every setpollISR_TX_PERIOD ticks. */\r
62 #define queuesetISR_TX_PERIOD   ( 50UL )\r
63 \r
64 /*\r
65  * The task that reads from the queue set.\r
66  */\r
67 static void prvQueueSetReceivingTask( void *pvParameters );\r
68 \r
69 /*-----------------------------------------------------------*/\r
70 \r
71 /* The queue that is added to the set. */\r
72 static QueueHandle_t xQueue = NULL;\r
73 \r
74 /* The handle of the queue set to which the queue is added. */\r
75 static QueueSetHandle_t xQueueSet = NULL;\r
76 \r
77 /* Set to pdFAIL if an error is detected by any queue set task.\r
78 ulCycleCounter will only be incremented if xQueueSetTasksSatus equals pdPASS. */\r
79 static volatile BaseType_t xQueueSetPollStatus = pdPASS;\r
80 \r
81 /* Counter used to ensure the task is still running. */\r
82 static uint32_t ulCycleCounter = 0;\r
83 \r
84 /*-----------------------------------------------------------*/\r
85 \r
86 void vStartQueueSetPollingTask( void )\r
87 {\r
88         /* Create the queue that is added to the set, the set, and add the queue to\r
89         the set. */\r
90         xQueue = xQueueCreate( setpollQUEUE_LENGTH, sizeof( uint32_t ) );\r
91         xQueueSet = xQueueCreateSet( setpollQUEUE_LENGTH );\r
92 \r
93         if( ( xQueue != NULL ) && ( xQueueSet != NULL ) )\r
94         {\r
95                 xQueueAddToSet( xQueue, xQueueSet );\r
96 \r
97                 /* Create the task. */\r
98                 xTaskCreate( prvQueueSetReceivingTask, "SetPoll", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
99         }\r
100 }\r
101 /*-----------------------------------------------------------*/\r
102 \r
103 static void prvQueueSetReceivingTask( void *pvParameters )\r
104 {\r
105 uint32_t ulReceived, ulExpected = 0;\r
106 QueueHandle_t xActivatedQueue;\r
107 \r
108         /* Remove compiler warnings. */\r
109         ( void ) pvParameters;\r
110 \r
111         for( ;; )\r
112         {\r
113                 /* Is a message waiting?  A block time is not used to ensure the queue\r
114                 set is polled while it is being written to from an interrupt. */\r
115                 xActivatedQueue = xQueueSelectFromSet( xQueueSet, setpollDONT_BLOCK );\r
116 \r
117                 if( xActivatedQueue != NULL )\r
118                 {\r
119                         /* Reading from the queue should pass with a zero block time as\r
120                         this task will only run when something has been posted to a task\r
121                         in the queue set. */\r
122                         if( xQueueReceive( xActivatedQueue, &ulReceived, setpollDONT_BLOCK ) != pdPASS )\r
123                         {\r
124                                 xQueueSetPollStatus = pdFAIL;\r
125                         }\r
126 \r
127                         if( ulReceived == ulExpected )\r
128                         {\r
129                                 ulExpected++;\r
130                         }\r
131                         else\r
132                         {\r
133                                 xQueueSetPollStatus = pdFAIL;\r
134                         }\r
135 \r
136                         if( xQueueSetPollStatus == pdPASS )\r
137                         {\r
138                                 ulCycleCounter++;\r
139                         }\r
140                 }\r
141         }\r
142 }\r
143 /*-----------------------------------------------------------*/\r
144 \r
145 void vQueueSetPollingInterruptAccess( void )\r
146 {\r
147 static uint32_t ulCallCount = 0, ulValueToSend = 0;\r
148 \r
149         /* It is intended that this function is called from the tick hook\r
150         function, so each call is one tick period apart. */\r
151         ulCallCount++;\r
152         if( ulCallCount > queuesetISR_TX_PERIOD )\r
153         {\r
154                 ulCallCount = 0;\r
155 \r
156                 if( xQueueSendFromISR( xQueue, ( void * ) &ulValueToSend, NULL ) == pdPASS )\r
157                 {\r
158                         /* Send the next value next time. */\r
159                         ulValueToSend++;\r
160                 }\r
161         }\r
162 }\r
163 /*-----------------------------------------------------------*/\r
164 \r
165 BaseType_t xAreQueueSetPollTasksStillRunning( void )\r
166 {\r
167 static uint32_t ulLastCycleCounter = 0;\r
168 \r
169         if( ulLastCycleCounter == ulCycleCounter )\r
170         {\r
171                 xQueueSetPollStatus = pdFAIL;\r
172         }\r
173 \r
174         ulLastCycleCounter = ulCycleCounter;\r
175 \r
176         return xQueueSetPollStatus;\r
177 }\r
178 /*-----------------------------------------------------------*/\r
179 \r
180 \r