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