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
31 + Made serial ISR handling more complete and robust.
\r
35 + Split serial.c into serial.c and serialISR.c. serial.c can be
\r
36 compiled using ARM or THUMB modes. serialISR.c must always be
\r
37 compiled in ARM mode.
\r
38 + Another small change to cSerialPutChar().
\r
42 + In cSerialPutChar() an extra check is made to ensure the post to
\r
43 the queue was successful if then attempting to retrieve the posted
\r
49 BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART0.
\r
51 This file contains all the serial port components that can be compiled to
\r
52 either ARM or THUMB mode. Components that must be compiled to ARM mode are
\r
53 contained in serialISR.c.
\r
56 /* Standard includes. */
\r
59 /* Scheduler includes. */
\r
60 #include "FreeRTOS.h"
\r
64 /* Demo application includes. */
\r
67 /*-----------------------------------------------------------*/
\r
69 /* Constants to setup and access the UART. */
\r
70 #define serDLAB ( ( unsigned char ) 0x80 )
\r
71 #define serENABLE_INTERRUPTS ( ( unsigned char ) 0x03 )
\r
72 #define serNO_PARITY ( ( unsigned char ) 0x00 )
\r
73 #define ser1_STOP_BIT ( ( unsigned char ) 0x00 )
\r
74 #define ser8_BIT_CHARS ( ( unsigned char ) 0x03 )
\r
75 #define serFIFO_ON ( ( unsigned char ) 0x01 )
\r
76 #define serCLEAR_FIFO ( ( unsigned char ) 0x06 )
\r
77 #define serWANTED_CLOCK_SCALING ( ( unsigned long ) 16 )
\r
79 /* Constants to setup and access the VIC. */
\r
80 #define serUART0_VIC_CHANNEL ( ( unsigned long ) 0x0006 )
\r
81 #define serUART0_VIC_CHANNEL_BIT ( ( unsigned long ) 0x0040 )
\r
82 #define serUART0_VIC_ENABLE ( ( unsigned long ) 0x0020 )
\r
83 #define serCLEAR_VIC_INTERRUPT ( ( unsigned long ) 0 )
\r
85 #define serINVALID_QUEUE ( ( QueueHandle_t ) 0 )
\r
86 #define serHANDLE ( ( xComPortHandle ) 1 )
\r
87 #define serNO_BLOCK ( ( TickType_t ) 0 )
\r
89 /*-----------------------------------------------------------*/
\r
91 /* Queues used to hold received characters, and characters waiting to be
\r
93 static QueueHandle_t xRxedChars;
\r
94 static QueueHandle_t xCharsForTx;
\r
96 /*-----------------------------------------------------------*/
\r
98 /* Communication flag between the interrupt service routine and serial API. */
\r
99 static volatile long *plTHREEmpty;
\r
102 * The queues are created in serialISR.c as they are used from the ISR.
\r
103 * Obtain references to the queues and THRE Empty flag.
\r
105 extern void vSerialISRCreateQueues( unsigned portBASE_TYPE uxQueueLength, QueueHandle_t *pxRxedChars, QueueHandle_t *pxCharsForTx, long volatile **pplTHREEmptyFlag );
\r
107 /*-----------------------------------------------------------*/
\r
109 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
111 unsigned long ulDivisor, ulWantedClock;
\r
112 xComPortHandle xReturn = serHANDLE;
\r
113 extern void ( vUART_ISR_Wrapper )( void );
\r
115 /* The queues are used in the serial ISR routine, so are created from
\r
116 serialISR.c (which is always compiled to ARM mode. */
\r
117 vSerialISRCreateQueues( uxQueueLength, &xRxedChars, &xCharsForTx, &plTHREEmpty );
\r
120 ( xRxedChars != serINVALID_QUEUE ) &&
\r
121 ( xCharsForTx != serINVALID_QUEUE ) &&
\r
122 ( ulWantedBaud != ( unsigned long ) 0 )
\r
125 portENTER_CRITICAL();
\r
127 /* Setup the baud rate: Calculate the divisor value. */
\r
128 ulWantedClock = ulWantedBaud * serWANTED_CLOCK_SCALING;
\r
129 ulDivisor = configCPU_CLOCK_HZ / ulWantedClock;
\r
131 /* Set the DLAB bit so we can access the divisor. */
\r
132 UART0_LCR |= serDLAB;
\r
134 /* Setup the divisor. */
\r
135 UART0_DLL = ( unsigned char ) ( ulDivisor & ( unsigned long ) 0xff );
\r
137 UART0_DLM = ( unsigned char ) ( ulDivisor & ( unsigned long ) 0xff );
\r
139 /* Turn on the FIFO's and clear the buffers. */
\r
140 UART0_FCR = ( serFIFO_ON | serCLEAR_FIFO );
\r
142 /* Setup transmission format. */
\r
143 UART0_LCR = serNO_PARITY | ser1_STOP_BIT | ser8_BIT_CHARS;
\r
145 /* Setup the VIC for the UART. */
\r
146 VICIntSelect &= ~( serUART0_VIC_CHANNEL_BIT );
\r
147 VICIntEnable |= serUART0_VIC_CHANNEL_BIT;
\r
148 VICVectAddr1 = ( long ) vUART_ISR_Wrapper;
\r
149 VICVectCntl1 = serUART0_VIC_CHANNEL | serUART0_VIC_ENABLE;
\r
151 /* Enable UART0 interrupts. */
\r
152 UART0_IER |= serENABLE_INTERRUPTS;
\r
154 portEXIT_CRITICAL();
\r
158 xReturn = ( xComPortHandle ) 0;
\r
163 /*-----------------------------------------------------------*/
\r
165 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
\r
167 /* The port handle is not required as this driver only supports UART0. */
\r
170 /* Get the next character from the buffer. Return false if no characters
\r
171 are available, or arrive before xBlockTime expires. */
\r
172 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
181 /*-----------------------------------------------------------*/
\r
183 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
\r
185 signed char *pxNext;
\r
187 /* NOTE: This implementation does not handle the queue being full as no
\r
188 block time is used! */
\r
190 /* The port handle is not required as this driver only supports UART0. */
\r
192 ( void ) usStringLength;
\r
194 /* Send each character in the string, one at a time. */
\r
195 pxNext = ( signed char * ) pcString;
\r
198 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );
\r
202 /*-----------------------------------------------------------*/
\r
204 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
\r
206 signed portBASE_TYPE xReturn;
\r
208 /* This demo driver only supports one port so the parameter is not used. */
\r
211 portENTER_CRITICAL();
\r
213 /* Is there space to write directly to the UART? */
\r
214 if( *plTHREEmpty == ( long ) pdTRUE )
\r
216 /* We wrote the character directly to the UART, so was
\r
218 *plTHREEmpty = pdFALSE;
\r
219 UART0_THR = cOutChar;
\r
224 /* We cannot write directly to the UART, so queue the character.
\r
225 Block for a maximum of xBlockTime if there is no space in the
\r
227 xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );
\r
229 /* Depending on queue sizing and task prioritisation: While we
\r
230 were blocked waiting to post interrupts were not disabled. It is
\r
231 possible that the serial ISR has emptied the Tx queue, in which
\r
232 case we need to start the Tx off again. */
\r
233 if( ( *plTHREEmpty == ( long ) pdTRUE ) && ( xReturn == pdPASS ) )
\r
235 xQueueReceive( xCharsForTx, &cOutChar, serNO_BLOCK );
\r
236 *plTHREEmpty = pdFALSE;
\r
237 UART0_THR = cOutChar;
\r
241 portEXIT_CRITICAL();
\r
245 /*-----------------------------------------------------------*/
\r
247 void vSerialClose( xComPortHandle xPort )
\r
249 /* Not supported as not required by the demo application. */
\r
252 /*-----------------------------------------------------------*/
\r