2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 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. 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
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
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\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
35 /* Scheduler include files. */
\r
36 #include "FreeRTOS.h"
\r
39 #include "croutine.h"
\r
41 /* Demo application include files. */
\r
42 #include "partest.h"
\r
44 /* Library include files. */
\r
45 #include "DriverLib.h"
\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
52 /* The length of the queue used to pass received characters to the Comms Rx
\r
54 #define commsRX_QUEUE_LEN ( 5 )
\r
56 /* The baud rate used by the UART comms tasks/co-routine. */
\r
57 #define commsBAUD_RATE ( 57600 )
\r
59 /* FIFO setting for the UART. The FIFO is not used to create a better test. */
\r
60 #define commsFIFO_SET ( 0x10 )
\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
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
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
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
85 static unsigned portBASE_TYPE uxCommsErrorStatus = pdPASS;
\r
87 /* The queue used to pass characters out of the ISR. */
\r
88 static QueueHandle_t xCommsQueue;
\r
90 /* The next character to transmit. */
\r
91 static char cNextChar;
\r
93 /*-----------------------------------------------------------*/
\r
95 void vSerialInit( void )
\r
97 /* Create the queue used to communicate between the UART ISR and the Comms
\r
99 xCommsQueue = xQueueCreate( commsRX_QUEUE_LEN, sizeof( char ) );
\r
101 /* Enable the UART. GPIOA has already been initialised. */
\r
102 SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
\r
104 /* Set GPIO A0 and A1 as peripheral function. They are used to output the
\r
106 GPIODirModeSet( GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1, GPIO_DIR_MODE_HW );
\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
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
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
120 /*-----------------------------------------------------------*/
\r
122 void vSerialTxCoRoutine( CoRoutineHandle_t xHandle, unsigned portBASE_TYPE uxIndex )
\r
124 TickType_t xDelayPeriod;
\r
125 static unsigned long *pulRandomBytes = commsFIRST_PROGRAM_BYTES;
\r
127 /* Co-routine MUST start with a call to crSTART. */
\r
128 crSTART( xHandle );
\r
132 /* Was the previously transmitted string received correctly? */
\r
133 if( uxCommsErrorStatus != pdPASS )
\r
135 /* An error was encountered so set the error LED. */
\r
136 vParTestSetLED( commsFAIL_LED, pdTRUE );
\r
139 /* The next character to Tx is the first in the string. */
\r
140 cNextChar = commsFIRST_TX_CHAR;
\r
142 UARTIntDisable( UART0_BASE, UART_INT_TX );
\r
144 /* Send the first character. */
\r
145 if( !( HWREG( UART0_BASE + UART_O_FR ) & UART_FR_TXFF ) )
\r
147 HWREG( UART0_BASE + UART_O_DR ) = cNextChar;
\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
154 UARTIntEnable(UART0_BASE, UART_INT_TX);
\r
156 /* Toggle the LED to show a new string is being transmitted. */
\r
157 vParTestToggleLED( commsTX_LED );
\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
164 if( pulRandomBytes > commsTOTAL_PROGRAM_MEMORY )
\r
166 pulRandomBytes = commsFIRST_PROGRAM_BYTES;
\r
169 /* Make sure we don't wait too long... */
\r
170 xDelayPeriod &= commsMAX_TX_DELAY;
\r
172 /* ...but we do want to wait. */
\r
173 if( xDelayPeriod < commsMIN_TX_DELAY )
\r
175 xDelayPeriod = commsMIN_TX_DELAY;
\r
178 /* Block for the random(ish) time. */
\r
179 crDELAY( xHandle, xDelayPeriod );
\r
182 /* Co-routine MUST end with a call to crEND. */
\r
185 /*-----------------------------------------------------------*/
\r
187 void vUART_ISR( void )
\r
189 unsigned long ulStatus;
\r
191 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
193 /* What caused the interrupt. */
\r
194 ulStatus = UARTIntStatus( UART0_BASE, pdTRUE );
\r
196 /* Clear the interrupt. */
\r
197 UARTIntClear( UART0_BASE, ulStatus );
\r
199 /* Was an Rx interrpt pending? */
\r
200 if( ulStatus & UART_INT_RX )
\r
202 if( ( HWREG(UART0_BASE + UART_O_FR ) & UART_FR_RXFF ) )
\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
212 /* Was a Tx interrupt pending? */
\r
213 if( ulStatus & UART_INT_TX )
\r
215 /* Send the next character in the string. We are not using the FIFO. */
\r
216 if( cNextChar <= commsLAST_TX_CHAR )
\r
218 if( !( HWREG( UART0_BASE + UART_O_FR ) & UART_FR_TXFF ) )
\r
220 HWREG( UART0_BASE + UART_O_DR ) = cNextChar;
\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
230 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
232 /*-----------------------------------------------------------*/
\r
234 void vCommsRxTask( void * pvParameters )
\r
236 static char cRxedChar, cExpectedChar;
\r
238 /* Set the char we expect to receive to the start of the string. */
\r
239 cExpectedChar = commsFIRST_TX_CHAR;
\r
243 /* Wait for a character to be received. */
\r
244 xQueueReceive( xCommsQueue, ( void * ) &cRxedChar, commsRX_DELAY );
\r
246 /* Was the character recived (if any) the expected character. */
\r
247 if( cRxedChar != cExpectedChar )
\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
254 /* Resync by waiting for the end of the current string. */
\r
255 while( cRxedChar != commsLAST_TX_CHAR )
\r
257 while( !xQueueReceive( xCommsQueue, ( void * ) &cRxedChar, portMAX_DELAY ) );
\r
260 /* The next expected character is the start of the string again. */
\r
261 cExpectedChar = commsFIRST_TX_CHAR;
\r
265 if( cExpectedChar == commsLAST_TX_CHAR )
\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
271 vParTestToggleLED( commsRX_LED );
\r
272 cExpectedChar = commsFIRST_TX_CHAR;
\r
276 /* We got the expected character, we now expect to receive the
\r
277 next character in the string. */
\r
283 /*-----------------------------------------------------------*/
\r
285 unsigned portBASE_TYPE uxGetCommsStatus( void )
\r
287 return uxCommsErrorStatus;
\r