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
31 BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART0.
\r
34 /* Standard includes. */
\r
37 /* Scheduler includes. */
\r
38 #include "FreeRTOS.h"
\r
42 /* Demo application includes. */
\r
45 /*-----------------------------------------------------------*/
\r
47 /* Constants to setup and access the UART. */
\r
48 #define serDLAB ( ( unsigned char ) 0x80 )
\r
49 #define serENABLE_INTERRUPTS ( ( unsigned char ) 0x03 )
\r
50 #define serNO_PARITY ( ( unsigned char ) 0x00 )
\r
51 #define ser1_STOP_BIT ( ( unsigned char ) 0x00 )
\r
52 #define ser8_BIT_CHARS ( ( unsigned char ) 0x03 )
\r
53 #define serFIFO_ON ( ( unsigned char ) 0x01 )
\r
54 #define serCLEAR_FIFO ( ( unsigned char ) 0x06 )
\r
55 #define serWANTED_CLOCK_SCALING ( ( unsigned long ) 16 )
\r
57 /* Constants to setup and access the VIC. */
\r
58 #define serU0VIC_CHANNEL ( ( unsigned long ) 0x0006 )
\r
59 #define serU0VIC_CHANNEL_BIT ( ( unsigned long ) 0x0040 )
\r
60 #define serU0VIC_ENABLE ( ( unsigned long ) 0x0020 )
\r
61 #define serCLEAR_VIC_INTERRUPT ( ( unsigned long ) 0 )
\r
63 /* Constants to determine the ISR source. */
\r
64 #define serSOURCE_THRE ( ( unsigned char ) 0x02 )
\r
65 #define serSOURCE_RX_TIMEOUT ( ( unsigned char ) 0x0c )
\r
66 #define serSOURCE_ERROR ( ( unsigned char ) 0x06 )
\r
67 #define serSOURCE_RX ( ( unsigned char ) 0x04 )
\r
68 #define serINTERRUPT_SOURCE_MASK ( ( unsigned char ) 0x0f )
\r
71 #define serINVALID_QUEUE ( ( QueueHandle_t ) 0 )
\r
72 #define serHANDLE ( ( xComPortHandle ) 1 )
\r
73 #define serNO_BLOCK ( ( TickType_t ) 0 )
\r
75 /*-----------------------------------------------------------*/
\r
77 /* Queues used to hold received characters, and characters waiting to be
\r
79 static QueueHandle_t xRxedChars;
\r
80 static QueueHandle_t xCharsForTx;
\r
81 static volatile long lTHREEmpty = pdFALSE;
\r
83 /*-----------------------------------------------------------*/
\r
85 /* The ISR. Note that this is called by a wrapper written in the file
\r
86 SerialISR.s79. See the WEB documentation for this port for further
\r
88 __arm void vSerialISR( void );
\r
90 /*-----------------------------------------------------------*/
\r
92 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
94 unsigned long ulDivisor, ulWantedClock;
\r
95 xComPortHandle xReturn = serHANDLE;
\r
96 extern void ( vSerialISREntry) ( void );
\r
98 /* Create the queues used to hold Rx and Tx characters. */
\r
99 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
100 xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
102 /* Initialise the THRE empty flag. */
\r
103 lTHREEmpty = pdTRUE;
\r
106 ( xRxedChars != serINVALID_QUEUE ) &&
\r
107 ( xCharsForTx != serINVALID_QUEUE ) &&
\r
108 ( ulWantedBaud != ( unsigned long ) 0 )
\r
111 portENTER_CRITICAL();
\r
113 /* Setup the baud rate: Calculate the divisor value. */
\r
114 ulWantedClock = ulWantedBaud * serWANTED_CLOCK_SCALING;
\r
115 ulDivisor = configCPU_CLOCK_HZ / ulWantedClock;
\r
117 /* Set the DLAB bit so we can access the divisor. */
\r
120 /* Setup the divisor. */
\r
121 U0DLL = ( unsigned char ) ( ulDivisor & ( unsigned long ) 0xff );
\r
123 U0DLM = ( unsigned char ) ( ulDivisor & ( unsigned long ) 0xff );
\r
125 /* Turn on the FIFO's and clear the buffers. */
\r
126 U0FCR = ( serFIFO_ON | serCLEAR_FIFO );
\r
128 /* Setup transmission format. */
\r
129 U0LCR = serNO_PARITY | ser1_STOP_BIT | ser8_BIT_CHARS;
\r
131 /* Setup the VIC for the UART. */
\r
132 VICIntSelect &= ~( serU0VIC_CHANNEL_BIT );
\r
133 VICIntEnable |= serU0VIC_CHANNEL_BIT;
\r
134 VICVectAddr1 = ( unsigned long ) vSerialISREntry;
\r
135 VICVectCntl1 = serU0VIC_CHANNEL | serU0VIC_ENABLE;
\r
137 /* Enable UART0 interrupts. */
\r
138 U0IER |= serENABLE_INTERRUPTS;
\r
140 portEXIT_CRITICAL();
\r
142 xReturn = ( xComPortHandle ) 1;
\r
146 xReturn = ( xComPortHandle ) 0;
\r
151 /*-----------------------------------------------------------*/
\r
153 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
\r
155 /* The port handle is not required as this driver only supports UART0. */
\r
158 /* Get the next character from the buffer. Return false if no characters
\r
159 are available, or arrive before xBlockTime expires. */
\r
160 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
169 /*-----------------------------------------------------------*/
\r
171 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
\r
173 signed char *pxNext;
\r
175 /* NOTE: This implementation does not handle the queue being full as no
\r
176 block time is used! */
\r
178 /* The port handle is not required as this driver only supports UART0. */
\r
180 ( void ) usStringLength;
\r
182 /* Send each character in the string, one at a time. */
\r
183 pxNext = ( signed char * ) pcString;
\r
186 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );
\r
190 /*-----------------------------------------------------------*/
\r
192 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
\r
194 signed portBASE_TYPE xReturn;
\r
196 /* The port handle is not required as this driver only supports UART0. */
\r
199 portENTER_CRITICAL();
\r
201 /* Is there space to write directly to the UART? */
\r
202 if( lTHREEmpty == ( long ) pdTRUE )
\r
204 /* We wrote the character directly to the UART, so was
\r
206 lTHREEmpty = pdFALSE;
\r
212 /* We cannot write directly to the UART, so queue the character.
\r
213 Block for a maximum of xBlockTime if there is no space in the
\r
214 queue. It is ok to block within a critical section as each
\r
215 task has it's own critical section management. */
\r
216 xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );
\r
218 /* Depending on queue sizing and task prioritisation: While we
\r
219 were blocked waiting to post interrupts were not disabled. It is
\r
220 possible that the serial ISR has emptied the Tx queue, in which
\r
221 case we need to start the Tx off again. */
\r
222 if( lTHREEmpty == ( long ) pdTRUE )
\r
224 xQueueReceive( xCharsForTx, &cOutChar, serNO_BLOCK );
\r
225 lTHREEmpty = pdFALSE;
\r
230 portEXIT_CRITICAL();
\r
234 /*-----------------------------------------------------------*/
\r
236 __arm void vSerialISR( void )
\r
239 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
241 /* What caused the interrupt? */
\r
242 switch( U0IIR & serINTERRUPT_SOURCE_MASK )
\r
244 case serSOURCE_ERROR : /* Not handling this, but clear the interrupt. */
\r
248 case serSOURCE_THRE : /* The THRE is empty. If there is another
\r
249 character in the Tx queue, send it now. */
\r
250 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
256 /* There are no further characters
\r
257 queued to send so we can indicate
\r
258 that the THRE is available. */
\r
259 lTHREEmpty = pdTRUE;
\r
263 case serSOURCE_RX_TIMEOUT :
\r
264 case serSOURCE_RX : /* A character was received. Place it in
\r
265 the queue of received characters. */
\r
267 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
270 default : /* There is nothing to do, leave the ISR. */
\r
274 /* Exit the ISR. If a task was woken by either a character being received
\r
275 or transmitted then a context switch will occur. */
\r
276 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
278 /* Clear the ISR in the VIC. */
\r
279 VICVectAddr = serCLEAR_VIC_INTERRUPT;
\r
281 /*-----------------------------------------------------------*/
\r