]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/MB96340_Softune/FreeRTOS_96348hs_SK16FX100PMC/Src/crflash_sk16fx100mpc.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / MB96340_Softune / FreeRTOS_96348hs_SK16FX100PMC / Src / crflash_sk16fx100mpc.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  * This demo application file demonstrates the use of queues to pass data\r
31  * between co-routines.\r
32  *\r
33  * N represents the number of 'fixed delay' co-routines that are created and\r
34  * is set during initialisation.\r
35  *\r
36  * N 'fixed delay' co-routines are created that just block for a fixed\r
37  * period then post the number of an LED onto a queue.  Each such co-routine\r
38  * uses a different block period.  A single 'flash' co-routine is also created\r
39  * that blocks on the same queue, waiting for the number of the next LED it\r
40  * should flash.  Upon receiving a number it simply toggle the instructed LED\r
41  * then blocks on the queue once more.  In this manner each LED from LED 0 to\r
42  * LED N-1 is caused to flash at a different rate.\r
43  *\r
44  * The 'fixed delay' co-routines are created with co-routine priority 0.  The\r
45  * flash co-routine is created with co-routine priority 1.  This means that\r
46  * the queue should never contain more than a single item.  This is because\r
47  * posting to the queue will unblock the 'flash' co-routine, and as this has\r
48  * a priority greater than the tasks posting to the queue it is guaranteed to\r
49  * have emptied the queue and blocked once again before the queue can contain\r
50  * any more date.  An error is indicated if an attempt to post data to the\r
51  * queue fails - indicating that the queue is already full.\r
52  *\r
53  */\r
54 \r
55 /* Scheduler includes. */\r
56 #include "FreeRTOS.h"\r
57 #include "croutine.h"\r
58 #include "queue.h"\r
59 \r
60 /* Demo application includes. */\r
61 #include "partest.h"\r
62 #include "crflash.h"\r
63 \r
64 /* The queue should only need to be of length 1.  See the description at the\r
65 top of the file. */\r
66 #define crfQUEUE_LENGTH         1\r
67 \r
68 #define crfFIXED_DELAY_PRIORITY         0\r
69 #define crfFLASH_PRIORITY                       1\r
70 \r
71 /* Only one flash co-routine is created so the index is not significant. */\r
72 #define crfFLASH_INDEX                          0\r
73 \r
74 /* Don't allow more than crfMAX_FLASH_TASKS 'fixed delay' co-routines to be\r
75 created. */\r
76 #define crfMAX_FLASH_TASKS                      8\r
77 \r
78 /* We don't want to block when posting to the queue. */\r
79 #define crfPOSTING_BLOCK_TIME           0\r
80 \r
81 /* Added by MPi, this define is added in order to make the vParTestToggleLED()\r
82 work. This basically differentiates the PDR09 from PDR00. 7-seg display LEDs connected \r
83 to PDR09 (SEG1) are used by the prvFlashCoRoutine() and PDR00 (SEG2) are used by tasks. */ \r
84 #define PDR00_Offset    8\r
85 \r
86 /*\r
87  * The 'fixed delay' co-routine as described at the top of the file.\r
88  */\r
89 static void prvFixedDelayCoRoutine( CoRoutineHandle_t xHandle, unsigned portBASE_TYPE uxIndex );\r
90 \r
91 /*\r
92  * The 'flash' co-routine as described at the top of the file.\r
93  */\r
94 static void prvFlashCoRoutine( CoRoutineHandle_t xHandle, unsigned portBASE_TYPE uxIndex );\r
95 \r
96 /* The queue used to pass data between the 'fixed delay' co-routines and the\r
97 'flash' co-routine. */\r
98 static QueueHandle_t xFlashQueue;\r
99 \r
100 /* This will be set to pdFALSE if we detect an error. */\r
101 static unsigned portBASE_TYPE uxCoRoutineFlashStatus = pdPASS;\r
102 \r
103 /*-----------------------------------------------------------*/\r
104 \r
105 /*\r
106  * See the header file for details.\r
107  */\r
108 void vStartFlashCoRoutines( unsigned portBASE_TYPE uxNumberToCreate )\r
109 {\r
110 unsigned portBASE_TYPE uxIndex;\r
111 \r
112         if( uxNumberToCreate > crfMAX_FLASH_TASKS )\r
113         {\r
114                 uxNumberToCreate = crfMAX_FLASH_TASKS;\r
115         }\r
116 \r
117         /* Create the queue used to pass data between the co-routines. */\r
118         xFlashQueue = xQueueCreate( crfQUEUE_LENGTH, sizeof( unsigned portBASE_TYPE ) );\r
119 \r
120         if( xFlashQueue )\r
121         {\r
122                 /* Create uxNumberToCreate 'fixed delay' co-routines. */\r
123                 for( uxIndex = 0; uxIndex < uxNumberToCreate; uxIndex++ )\r
124                 {\r
125                         xCoRoutineCreate( prvFixedDelayCoRoutine, crfFIXED_DELAY_PRIORITY, uxIndex );\r
126                 }\r
127 \r
128                 /* Create the 'flash' co-routine. */\r
129                 xCoRoutineCreate( prvFlashCoRoutine, crfFLASH_PRIORITY, crfFLASH_INDEX );\r
130         }\r
131 }\r
132 /*-----------------------------------------------------------*/\r
133 \r
134 static void prvFixedDelayCoRoutine( CoRoutineHandle_t xHandle, unsigned portBASE_TYPE uxIndex )\r
135 {\r
136 /* Even though this is a co-routine the xResult variable does not need to be\r
137 static as we do not need it to maintain its state between blocks. */\r
138 signed portBASE_TYPE xResult;\r
139 /* The uxIndex parameter of the co-routine function is used as an index into\r
140 the xFlashRates array to obtain the delay period to use. */\r
141 static const TickType_t xFlashRates[ crfMAX_FLASH_TASKS ] = { 150 / portTICK_PERIOD_MS,\r
142                                                                                                                                 200 / portTICK_PERIOD_MS,\r
143                                                                                                                                 250 / portTICK_PERIOD_MS,\r
144                                                                                                                                 300 / portTICK_PERIOD_MS,\r
145                                                                                                                                 350 / portTICK_PERIOD_MS,\r
146                                                                                                                                 400 / portTICK_PERIOD_MS,\r
147                                                                                                                                 450 / portTICK_PERIOD_MS,\r
148                                                                                                                                 500  / portTICK_PERIOD_MS };\r
149 \r
150         /* Co-routines MUST start with a call to crSTART. */\r
151         crSTART( xHandle );\r
152 \r
153         for( ;; )\r
154         {\r
155                 /* Post our uxIndex value onto the queue.  This is used as the LED to\r
156                 flash. */\r
157                 crQUEUE_SEND( xHandle, xFlashQueue, ( void * ) &uxIndex, crfPOSTING_BLOCK_TIME, &xResult );\r
158 \r
159                 if( xResult != pdPASS )\r
160                 {\r
161                         /* For the reasons stated at the top of the file we should always\r
162                         find that we can post to the queue.  If we could not then an error\r
163                         has occurred. */\r
164                         uxCoRoutineFlashStatus = pdFAIL;\r
165                 }\r
166 \r
167                 crDELAY( xHandle, xFlashRates[ uxIndex ] );\r
168         }\r
169 \r
170         /* Co-routines MUST end with a call to crEND. */\r
171         crEND();\r
172 }\r
173 /*-----------------------------------------------------------*/\r
174 \r
175 static void prvFlashCoRoutine( CoRoutineHandle_t xHandle, unsigned portBASE_TYPE uxIndex )\r
176 {\r
177 /* Even though this is a co-routine the variable do not need to be\r
178 static as we do not need it to maintain their state between blocks. */\r
179 signed portBASE_TYPE xResult;\r
180 unsigned portBASE_TYPE uxLEDToFlash;\r
181 \r
182         /* Co-routines MUST start with a call to crSTART. */\r
183         crSTART( xHandle );\r
184         ( void ) uxIndex;\r
185         \r
186         for( ;; )\r
187         {\r
188                 /* Block to wait for the number of the LED to flash. */\r
189                 crQUEUE_RECEIVE( xHandle, xFlashQueue, &uxLEDToFlash, portMAX_DELAY, &xResult );                \r
190 \r
191                 if( xResult != pdPASS )\r
192                 {\r
193                         /* We would not expect to wake unless we received something. */\r
194                         uxCoRoutineFlashStatus = pdFAIL;\r
195                 }\r
196                 else\r
197                 {\r
198                         /* We received the number of an LED to flash - flash it! */\r
199                         /* Added by MPi, PDR00_Offset is added in order to make the \r
200                         vParTestToggleLED() work. */ \r
201                         vParTestToggleLED( uxLEDToFlash +  PDR00_Offset );\r
202                 }\r
203         }\r
204 \r
205         /* Co-routines MUST end with a call to crEND. */\r
206         crEND();\r
207 }\r
208 /*-----------------------------------------------------------*/\r
209 \r
210 portBASE_TYPE xAreFlashCoRoutinesStillRunning( void )\r
211 {\r
212         /* Return pdPASS or pdFAIL depending on whether an error has been detected\r
213         or not. */\r
214         return uxCoRoutineFlashStatus;\r
215 }\r
216 \r