]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_LM3S316_IAR/commstest.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / CORTEX_LM3S316_IAR / commstest.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  * The comms test Rx and Tx task and co-routine.  See the comments at the top\r
31  * of main.c for full information.\r
32  */\r
33 \r
34 \r
35 /* Scheduler include files. */\r
36 #include "FreeRTOS.h"\r
37 #include "task.h"\r
38 #include "queue.h"\r
39 #include "croutine.h"\r
40 \r
41 /* Demo application include files. */\r
42 #include "partest.h"\r
43 \r
44 /* Library include files. */\r
45 #include "DriverLib.h"\r
46 \r
47 /* The LED's toggled by the various tasks. */\r
48 #define commsFAIL_LED                   ( 7 )\r
49 #define commsRX_LED                     ( 6 )\r
50 #define commsTX_LED                     ( 5 )\r
51 \r
52 /* The length of the queue used to pass received characters to the Comms Rx\r
53 task. */\r
54 #define commsRX_QUEUE_LEN                       ( 5 )\r
55 \r
56 /* The baud rate used by the UART comms tasks/co-routine. */\r
57 #define commsBAUD_RATE                          ( 57600 )\r
58 \r
59 /* FIFO setting for the UART.  The FIFO is not used to create a better test. */\r
60 #define commsFIFO_SET                           ( 0x10 )\r
61 \r
62 /* The string that is transmitted on the UART contains sequentially the\r
63 characters from commsFIRST_TX_CHAR to commsLAST_TX_CHAR. */\r
64 #define commsFIRST_TX_CHAR '0'\r
65 #define commsLAST_TX_CHAR 'z'\r
66 \r
67 /* Just used to walk through the program memory in order that some random data\r
68 can be generated. */\r
69 #define commsTOTAL_PROGRAM_MEMORY ( ( unsigned long * ) ( 8 * 1024 ) )\r
70 #define commsFIRST_PROGRAM_BYTES ( ( unsigned long * ) 4 )\r
71 \r
72 /* The time between transmissions of the string on UART 0.   This is pseudo\r
73 random in order to generate a bit or randomness to when the interrupts occur.*/\r
74 #define commsMIN_TX_DELAY                       ( 40 / portTICK_PERIOD_MS )\r
75 #define commsMAX_TX_DELAY                       ( ( TickType_t ) 0x7f )\r
76 #define commsOFFSET_TIME                        ( ( TickType_t ) 3 )\r
77 \r
78 /* The time the Comms Rx task should wait to receive a character.  This should\r
79 be slightly longer than the time between transmissions.  If we do not receive\r
80 a character after this time then there must be an error in the transmission or\r
81 the timing of the transmission. */\r
82 #define commsRX_DELAY                   ( commsMAX_TX_DELAY + 20 )\r
83 \r
84 \r
85 static unsigned portBASE_TYPE uxCommsErrorStatus = pdPASS;\r
86 \r
87 /* The queue used to pass characters out of the ISR. */\r
88 static QueueHandle_t xCommsQueue;\r
89 \r
90 /* The next character to transmit. */\r
91 static char cNextChar;\r
92 \r
93 /*-----------------------------------------------------------*/\r
94 \r
95 void vSerialInit( void )\r
96 {\r
97         /* Create the queue used to communicate between the UART ISR and the Comms\r
98         Rx task. */\r
99         xCommsQueue = xQueueCreate( commsRX_QUEUE_LEN, sizeof( char ) );\r
100 \r
101         /* Enable the UART.  GPIOA has already been initialised. */\r
102         SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);\r
103 \r
104         /* Set GPIO A0 and A1 as peripheral function.  They are used to output the\r
105         UART signals. */\r
106         GPIODirModeSet( GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1, GPIO_DIR_MODE_HW );\r
107 \r
108         /* Configure the UART for 8-N-1 operation. */\r
109         UARTConfigSetExpClk( UART0_BASE, SysCtlClockGet(), commsBAUD_RATE, UART_CONFIG_WLEN_8 | UART_CONFIG_PAR_NONE | UART_CONFIG_STOP_ONE );\r
110 \r
111         /* We dont want to use the fifo.  This is for test purposes to generate\r
112         as many interrupts as possible. */\r
113         HWREG( UART0_BASE + UART_O_LCR_H ) &= ~commsFIFO_SET;\r
114 \r
115         /* Enable both Rx and Tx interrupts. */\r
116         HWREG( UART0_BASE + UART_O_IM ) |= ( UART_INT_TX | UART_INT_RX );\r
117         IntPrioritySet( INT_UART0, configKERNEL_INTERRUPT_PRIORITY );\r
118         IntEnable( INT_UART0 );\r
119 }\r
120 /*-----------------------------------------------------------*/\r
121 \r
122 void vSerialTxCoRoutine( CoRoutineHandle_t xHandle, unsigned portBASE_TYPE uxIndex )\r
123 {\r
124 TickType_t xDelayPeriod;\r
125 static unsigned long *pulRandomBytes = commsFIRST_PROGRAM_BYTES;\r
126 \r
127         /* Co-routine MUST start with a call to crSTART. */\r
128         crSTART( xHandle );\r
129 \r
130         for(;;)\r
131     {   \r
132                 /* Was the previously transmitted string received correctly? */\r
133                 if( uxCommsErrorStatus != pdPASS )\r
134                 {\r
135                         /* An error was encountered so set the error LED. */\r
136                         vParTestSetLED( commsFAIL_LED, pdTRUE );\r
137                 }\r
138 \r
139                 /* The next character to Tx is the first in the string. */\r
140                 cNextChar = commsFIRST_TX_CHAR;\r
141 \r
142                 UARTIntDisable( UART0_BASE, UART_INT_TX );\r
143                 {\r
144                         /* Send the first character. */\r
145                         if( !( HWREG( UART0_BASE + UART_O_FR ) & UART_FR_TXFF ) )\r
146                         {\r
147                                 HWREG( UART0_BASE + UART_O_DR ) = cNextChar;\r
148                         }\r
149 \r
150                         /* Move the variable to the char to Tx on so the ISR transmits\r
151                         the next character in the string once this one has completed. */\r
152                         cNextChar++;\r
153                 }\r
154                 UARTIntEnable(UART0_BASE, UART_INT_TX);\r
155 \r
156                 /* Toggle the LED to show a new string is being transmitted. */\r
157         vParTestToggleLED( commsTX_LED );\r
158 \r
159                 /* Delay before we start the string off again.  A pseudo-random delay\r
160                 is used as this will provide a better test. */\r
161                 xDelayPeriod = xTaskGetTickCount() + ( *pulRandomBytes );\r
162 \r
163                 pulRandomBytes++;\r
164                 if( pulRandomBytes > commsTOTAL_PROGRAM_MEMORY )\r
165                 {\r
166                         pulRandomBytes = commsFIRST_PROGRAM_BYTES;\r
167                 }\r
168 \r
169                 /* Make sure we don't wait too long... */\r
170                 xDelayPeriod &= commsMAX_TX_DELAY;\r
171 \r
172                 /* ...but we do want to wait. */\r
173                 if( xDelayPeriod < commsMIN_TX_DELAY )\r
174                 {\r
175                         xDelayPeriod = commsMIN_TX_DELAY;\r
176                 }\r
177 \r
178                 /* Block for the random(ish) time. */\r
179                 crDELAY( xHandle, xDelayPeriod );\r
180     }\r
181 \r
182         /* Co-routine MUST end with a call to crEND. */\r
183         crEND();\r
184 }\r
185 /*-----------------------------------------------------------*/\r
186 \r
187 void vUART_ISR( void )\r
188 {\r
189 unsigned long ulStatus;\r
190 char cRxedChar;\r
191 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
192 \r
193         /* What caused the interrupt. */\r
194         ulStatus = UARTIntStatus( UART0_BASE, pdTRUE );\r
195 \r
196         /* Clear the interrupt. */\r
197         UARTIntClear( UART0_BASE, ulStatus );\r
198 \r
199         /* Was an Rx interrpt pending? */\r
200         if( ulStatus & UART_INT_RX )\r
201         {\r
202                 if( ( HWREG(UART0_BASE + UART_O_FR ) & UART_FR_RXFF ) )\r
203                 {\r
204                         /* Get the char from the buffer and post it onto the queue of\r
205                         Rxed chars.  Posting the character should wake the task that is\r
206                         blocked on the queue waiting for characters. */\r
207                         cRxedChar = ( char ) HWREG( UART0_BASE + UART_O_DR );\r
208                         xQueueSendFromISR( xCommsQueue, &cRxedChar, &xHigherPriorityTaskWoken );\r
209                 }               \r
210         }\r
211 \r
212         /* Was a Tx interrupt pending? */\r
213         if( ulStatus & UART_INT_TX )\r
214         {\r
215                 /* Send the next character in the string.  We are not using the FIFO. */\r
216                 if( cNextChar <= commsLAST_TX_CHAR )\r
217                 {\r
218                         if( !( HWREG( UART0_BASE + UART_O_FR ) & UART_FR_TXFF ) )\r
219                         {\r
220                                 HWREG( UART0_BASE + UART_O_DR ) = cNextChar;\r
221                         }\r
222                         cNextChar++;\r
223                 }\r
224         }\r
225         \r
226         /* If a task was woken by the character being received then we force\r
227         a context switch to occur in case the task is of higher priority than\r
228         the currently executing task (i.e. the task that this interrupt\r
229         interrupted.) */\r
230         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
231 }\r
232 /*-----------------------------------------------------------*/\r
233 \r
234 void vCommsRxTask( void * pvParameters )\r
235 {\r
236 static char cRxedChar, cExpectedChar;\r
237 \r
238         /* Set the char we expect to receive to the start of the string. */\r
239         cExpectedChar = commsFIRST_TX_CHAR;\r
240 \r
241         for( ;; )\r
242         {\r
243                 /* Wait for a character to be received. */\r
244                 xQueueReceive( xCommsQueue, ( void * ) &cRxedChar, commsRX_DELAY );\r
245 \r
246                 /* Was the character recived (if any) the expected character. */\r
247                 if( cRxedChar != cExpectedChar )\r
248                 {\r
249                         /* Got an unexpected character.  This can sometimes occur when\r
250                         reseting the system using the debugger leaving characters already\r
251                         in the UART regsters. */\r
252                         uxCommsErrorStatus = pdFAIL;\r
253 \r
254                         /* Resync by waiting for the end of the current string. */\r
255                         while( cRxedChar != commsLAST_TX_CHAR )\r
256                         {\r
257                                 while( !xQueueReceive( xCommsQueue, ( void * ) &cRxedChar, portMAX_DELAY ) );\r
258                         }\r
259 \r
260                         /* The next expected character is the start of the string again. */\r
261                         cExpectedChar = commsFIRST_TX_CHAR;\r
262                 }\r
263                 else\r
264                 {\r
265                         if( cExpectedChar == commsLAST_TX_CHAR )\r
266                         {\r
267                                 /* We have reached the end of the string - we now expect to\r
268                                 receive the first character in the string again.   The LED is\r
269                                 toggled to indicate that the entire string was received without\r
270                                 error. */\r
271                                 vParTestToggleLED( commsRX_LED );\r
272                                 cExpectedChar = commsFIRST_TX_CHAR;\r
273                         }\r
274                         else\r
275                         {\r
276                                 /* We got the expected character, we now expect to receive the\r
277                                 next character in the string. */\r
278                                 cExpectedChar++;\r
279                         }\r
280                 }\r
281         }\r
282 }\r
283 /*-----------------------------------------------------------*/\r
284 \r
285 unsigned portBASE_TYPE uxGetCommsStatus( void )\r
286 {\r
287         return uxCommsErrorStatus;\r
288 }\r