2 * FreeRTOS Kernel V10.2.1
\r
3 * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\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
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\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
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\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
34 /* Scheduler include files. */
\r
35 #include "FreeRTOS.h"
\r
38 #include "croutine.h"
\r
40 /* Demo application include files. */
\r
41 #include "partest.h"
\r
43 /* Library include files. */
\r
44 #include "DriverLib.h"
\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
51 /* The length of the queue used to pass received characters to the Comms Rx
\r
53 #define commsRX_QUEUE_LEN ( 5 )
\r
55 /* The baud rate used by the UART comms tasks/co-routine. */
\r
56 #define commsBAUD_RATE ( 57600 )
\r
58 /* FIFO setting for the UART. The FIFO is not used to create a better test. */
\r
59 #define commsFIFO_SET ( 0x10 )
\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
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
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
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
84 static unsigned portBASE_TYPE uxCommsErrorStatus = pdPASS;
\r
86 /* The queue used to pass characters out of the ISR. */
\r
87 static QueueHandle_t xCommsQueue;
\r
89 /* The next character to transmit. */
\r
90 static char cNextChar;
\r
92 /*-----------------------------------------------------------*/
\r
94 void vSerialInit( void )
\r
96 /* Create the queue used to communicate between the UART ISR and the Comms
\r
98 xCommsQueue = xQueueCreate( commsRX_QUEUE_LEN, sizeof( char ) );
\r
100 /* Enable the UART. GPIOA has already been initialised. */
\r
101 SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
\r
103 /* Set GPIO A0 and A1 as peripheral function. They are used to output the
\r
105 GPIODirModeSet( GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1, GPIO_DIR_MODE_HW );
\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
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
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
119 /*-----------------------------------------------------------*/
\r
121 void vSerialTxCoRoutine( CoRoutineHandle_t xHandle, unsigned portBASE_TYPE uxIndex )
\r
123 TickType_t xDelayPeriod;
\r
124 static unsigned long *pulRandomBytes = commsFIRST_PROGRAM_BYTES;
\r
126 /* Co-routine MUST start with a call to crSTART. */
\r
127 crSTART( xHandle );
\r
131 /* Was the previously transmitted string received correctly? */
\r
132 if( uxCommsErrorStatus != pdPASS )
\r
134 /* An error was encountered so set the error LED. */
\r
135 vParTestSetLED( commsFAIL_LED, pdTRUE );
\r
138 /* The next character to Tx is the first in the string. */
\r
139 cNextChar = commsFIRST_TX_CHAR;
\r
141 UARTIntDisable( UART0_BASE, UART_INT_TX );
\r
143 /* Send the first character. */
\r
144 if( !( HWREG( UART0_BASE + UART_O_FR ) & UART_FR_TXFF ) )
\r
146 HWREG( UART0_BASE + UART_O_DR ) = cNextChar;
\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
153 UARTIntEnable(UART0_BASE, UART_INT_TX);
\r
155 /* Toggle the LED to show a new string is being transmitted. */
\r
156 vParTestToggleLED( commsTX_LED );
\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
163 if( pulRandomBytes > commsTOTAL_PROGRAM_MEMORY )
\r
165 pulRandomBytes = commsFIRST_PROGRAM_BYTES;
\r
168 /* Make sure we don't wait too long... */
\r
169 xDelayPeriod &= commsMAX_TX_DELAY;
\r
171 /* ...but we do want to wait. */
\r
172 if( xDelayPeriod < commsMIN_TX_DELAY )
\r
174 xDelayPeriod = commsMIN_TX_DELAY;
\r
177 /* Block for the random(ish) time. */
\r
178 crDELAY( xHandle, xDelayPeriod );
\r
181 /* Co-routine MUST end with a call to crEND. */
\r
184 /*-----------------------------------------------------------*/
\r
186 void vUART_ISR( void )
\r
188 unsigned long ulStatus;
\r
190 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
192 /* What caused the interrupt. */
\r
193 ulStatus = UARTIntStatus( UART0_BASE, pdTRUE );
\r
195 /* Clear the interrupt. */
\r
196 UARTIntClear( UART0_BASE, ulStatus );
\r
198 /* Was an Rx interrpt pending? */
\r
199 if( ulStatus & UART_INT_RX )
\r
201 if( ( HWREG(UART0_BASE + UART_O_FR ) & UART_FR_RXFF ) )
\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
211 /* Was a Tx interrupt pending? */
\r
212 if( ulStatus & UART_INT_TX )
\r
214 /* Send the next character in the string. We are not using the FIFO. */
\r
215 if( cNextChar <= commsLAST_TX_CHAR )
\r
217 if( !( HWREG( UART0_BASE + UART_O_FR ) & UART_FR_TXFF ) )
\r
219 HWREG( UART0_BASE + UART_O_DR ) = cNextChar;
\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
229 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
231 /*-----------------------------------------------------------*/
\r
233 void vCommsRxTask( void * pvParameters )
\r
235 static char cRxedChar, cExpectedChar;
\r
237 /* Set the char we expect to receive to the start of the string. */
\r
238 cExpectedChar = commsFIRST_TX_CHAR;
\r
242 /* Wait for a character to be received. */
\r
243 xQueueReceive( xCommsQueue, ( void * ) &cRxedChar, commsRX_DELAY );
\r
245 /* Was the character recived (if any) the expected character. */
\r
246 if( cRxedChar != cExpectedChar )
\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
253 /* Resync by waiting for the end of the current string. */
\r
254 while( cRxedChar != commsLAST_TX_CHAR )
\r
256 while( !xQueueReceive( xCommsQueue, ( void * ) &cRxedChar, portMAX_DELAY ) );
\r
259 /* The next expected character is the start of the string again. */
\r
260 cExpectedChar = commsFIRST_TX_CHAR;
\r
264 if( cExpectedChar == commsLAST_TX_CHAR )
\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
270 vParTestToggleLED( commsRX_LED );
\r
271 cExpectedChar = commsFIRST_TX_CHAR;
\r
275 /* We got the expected character, we now expect to receive the
\r
276 next character in the string. */
\r
282 /*-----------------------------------------------------------*/
\r
284 unsigned portBASE_TYPE uxGetCommsStatus( void )
\r
286 return uxCommsErrorStatus;
\r