2 * FreeRTOS Kernel V10.3.0
\r
3 * Copyright (C) 2020 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
30 BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART0.
\r
32 Note this driver is used to test the FreeRTOS port. It is NOT intended to
\r
33 be an example of an efficient implementation!
\r
36 /* Standard includes. */
\r
39 /* Scheduler includes. */
\r
40 #include "FreeRTOS.h"
\r
44 /* Demo application includes. */
\r
47 /*-----------------------------------------------------------*/
\r
49 /* Constants to setup and access the UART. */
\r
50 #define serDLAB ( ( unsigned char ) 0x80 )
\r
51 #define serENABLE_INTERRUPTS ( ( unsigned char ) 0x03 )
\r
52 #define serNO_PARITY ( ( unsigned char ) 0x00 )
\r
53 #define ser1_STOP_BIT ( ( unsigned char ) 0x00 )
\r
54 #define ser8_BIT_CHARS ( ( unsigned char ) 0x03 )
\r
55 #define serFIFO_ON ( ( unsigned char ) 0x01 )
\r
56 #define serCLEAR_FIFO ( ( unsigned char ) 0x06 )
\r
57 #define serWANTED_CLOCK_SCALING ( ( unsigned long ) 16 )
\r
59 /* Constants to setup and access the VIC. */
\r
60 #define serU1VIC_CHANNEL ( ( unsigned long ) 0x0007 )
\r
61 #define serU1VIC_CHANNEL_BIT ( ( unsigned long ) 0x0080 )
\r
62 #define serU1VIC_ENABLE ( ( unsigned long ) 0x0020 )
\r
65 #define serINVALID_QUEUE ( ( QueueHandle_t ) 0 )
\r
66 #define serHANDLE ( ( xComPortHandle ) 1 )
\r
67 #define serNO_BLOCK ( ( TickType_t ) 0 )
\r
69 /* Constant to access the VIC. */
\r
70 #define serCLEAR_VIC_INTERRUPT ( ( unsigned long ) 0 )
\r
72 /* Constants to determine the ISR source. */
\r
73 #define serSOURCE_THRE ( ( unsigned char ) 0x02 )
\r
74 #define serSOURCE_RX_TIMEOUT ( ( unsigned char ) 0x0c )
\r
75 #define serSOURCE_ERROR ( ( unsigned char ) 0x06 )
\r
76 #define serSOURCE_RX ( ( unsigned char ) 0x04 )
\r
77 #define serINTERRUPT_SOURCE_MASK ( ( unsigned char ) 0x0f )
\r
78 #define serINTERRUPT_IS_PENDING ( ( unsigned char ) 0x01 )
\r
80 /*-----------------------------------------------------------*/
\r
83 * The asm wrapper for the interrupt service routine.
\r
85 extern void vUART_ISREntry( void );
\r
88 * The C function called from the asm wrapper.
\r
90 void vUART_ISRHandler( void );
\r
92 /*-----------------------------------------------------------*/
\r
94 /* Queues used to hold received characters, and characters waiting to be
\r
96 static QueueHandle_t xRxedChars;
\r
97 static QueueHandle_t xCharsForTx;
\r
99 /* Communication flag between the interrupt service routine and serial API. */
\r
100 static volatile long lTHREEmpty;
\r
102 /*-----------------------------------------------------------*/
\r
104 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
106 unsigned long ulDivisor, ulWantedClock;
\r
107 xComPortHandle xReturn = serHANDLE;
\r
109 /* Create the queues used to hold Rx and Tx characters. */
\r
110 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
111 xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
113 /* Initialise the THRE empty flag. */
\r
114 lTHREEmpty = pdTRUE;
\r
117 ( xRxedChars != serINVALID_QUEUE ) &&
\r
118 ( xCharsForTx != serINVALID_QUEUE ) &&
\r
119 ( ulWantedBaud != ( unsigned long ) 0 )
\r
122 portENTER_CRITICAL()
\r
124 /* Setup the baud rate: Calculate the divisor value. */
\r
125 ulWantedClock = ulWantedBaud * serWANTED_CLOCK_SCALING;
\r
126 ulDivisor = configCPU_CLOCK_HZ / ulWantedClock;
\r
128 /* Set the DLAB bit so we can access the divisor. */
\r
131 /* Setup the divisor. */
\r
132 U1DLL = ( unsigned char ) ( ulDivisor & ( unsigned long ) 0xff );
\r
134 U1DLM = ( unsigned char ) ( ulDivisor & ( unsigned long ) 0xff );
\r
136 /* Turn on the FIFO's and clear the buffers. */
\r
137 U1FCR = ( serFIFO_ON | serCLEAR_FIFO );
\r
139 /* Setup transmission format. */
\r
140 U1LCR = serNO_PARITY | ser1_STOP_BIT | ser8_BIT_CHARS;
\r
142 /* Setup the VIC for the UART. */
\r
143 VICIntSelect &= ~( serU1VIC_CHANNEL_BIT );
\r
144 VICIntEnable |= serU1VIC_CHANNEL_BIT;
\r
145 VICVectAddr1 = ( unsigned long ) vUART_ISREntry;
\r
146 VICVectCntl1 = serU1VIC_CHANNEL | serU1VIC_ENABLE;
\r
148 /* Enable UART0 interrupts. */
\r
149 U1IER |= serENABLE_INTERRUPTS;
\r
151 portEXIT_CRITICAL();
\r
155 xReturn = ( xComPortHandle ) 0;
\r
160 /*-----------------------------------------------------------*/
\r
162 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
\r
164 /* The port handle is not required as this driver only supports UART0. */
\r
167 /* Get the next character from the buffer. Return false if no characters
\r
168 are available, or arrive before xBlockTime expires. */
\r
169 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
178 /*-----------------------------------------------------------*/
\r
180 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
\r
182 signed char *pxNext;
\r
184 /* NOTE: This implementation does not handle the queue being full as no
\r
185 block time is used! */
\r
187 /* The port handle is not required as this driver only supports UART0. */
\r
189 ( void ) usStringLength;
\r
191 /* Send each character in the string, one at a time. */
\r
192 pxNext = ( signed char * ) pcString;
\r
195 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );
\r
199 /*-----------------------------------------------------------*/
\r
201 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
\r
203 signed portBASE_TYPE xReturn;
\r
205 /* The port handle is not required as this driver only supports UART0. */
\r
208 portENTER_CRITICAL();
\r
210 /* Is there space to write directly to the UART? */
\r
211 if( lTHREEmpty == ( long ) pdTRUE )
\r
213 /* We wrote the character directly to the UART, so was
\r
215 lTHREEmpty = pdFALSE;
\r
221 /* We cannot write directly to the UART, so queue the character.
\r
222 Block for a maximum of xBlockTime if there is no space in the
\r
223 queue. It is ok to block within a critical section as each
\r
224 task has it's own critical section management. */
\r
225 xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );
\r
227 /* Depending on queue sizing and task prioritisation: While we
\r
228 were blocked waiting to post interrupts were not disabled. It is
\r
229 possible that the serial ISR has emptied the Tx queue, in which
\r
230 case we need to start the Tx off again. */
\r
231 if( lTHREEmpty == ( long ) pdTRUE )
\r
233 xQueueReceive( xCharsForTx, &cOutChar, serNO_BLOCK );
\r
234 lTHREEmpty = pdFALSE;
\r
239 portEXIT_CRITICAL();
\r
243 /*-----------------------------------------------------------*/
\r
245 void vUART_ISRHandler( void )
\r
248 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
249 unsigned char ucInterrupt;
\r
251 ucInterrupt = U1IIR;
\r
253 /* The interrupt pending bit is active low. */
\r
254 while( ( ucInterrupt & serINTERRUPT_IS_PENDING ) == 0 )
\r
256 /* What caused the interrupt? */
\r
257 switch( ucInterrupt & serINTERRUPT_SOURCE_MASK )
\r
259 case serSOURCE_ERROR : /* Not handling this, but clear the interrupt. */
\r
263 case serSOURCE_THRE : /* The THRE is empty. If there is another
\r
264 character in the Tx queue, send it now. */
\r
265 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
271 /* There are no further characters
\r
272 queued to send so we can indicate
\r
273 that the THRE is available. */
\r
274 lTHREEmpty = pdTRUE;
\r
278 case serSOURCE_RX_TIMEOUT :
\r
279 case serSOURCE_RX : /* A character was received. Place it in
\r
280 the queue of received characters. */
\r
282 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
285 default : /* There is nothing to do, leave the ISR. */
\r
289 ucInterrupt = U1IIR;
\r
292 /* Clear the ISR in the VIC. */
\r
293 VICVectAddr = serCLEAR_VIC_INTERRUPT;
\r
295 /* Exit the ISR. If a task was woken by either a character being received
\r
296 or transmitted then a context switch will occur. */
\r
297 portEXIT_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
299 /*-----------------------------------------------------------*/
\r