2 FreeRTOS.org V4.7.1 - Copyright (C) 2003-2008 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 FreeRTOS.org is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with FreeRTOS.org; if not, write to the Free Software
\r
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 A special exception to the GPL can be applied should you wish to distribute
\r
21 a combined work that includes FreeRTOS.org, without being obliged to provide
\r
22 the source code for any proprietary components. See the licensing section
\r
23 of http://www.FreeRTOS.org for full details of how and when the exception
\r
26 ***************************************************************************
\r
28 Please ensure to read the configuration and relevant port sections of the
\r
29 online documentation.
\r
31 +++ http://www.FreeRTOS.org +++
\r
32 Documentation, latest information, license and contact details.
\r
34 +++ http://www.SafeRTOS.com +++
\r
35 A version that is certified for use in safety critical systems.
\r
37 +++ http://www.OpenRTOS.com +++
\r
38 Commercial support, development, porting, licensing and training services.
\r
40 ***************************************************************************
\r
45 BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART0.
\r
47 This file contains all the serial port components that can be compiled to
\r
48 either ARM or THUMB mode. Components that must be compiled to ARM mode are
\r
49 contained in serialISR.c.
\r
52 /* Standard includes. */
\r
55 /* Scheduler includes. */
\r
56 #include "FreeRTOS.h"
\r
60 /* Demo application includes. */
\r
63 /*-----------------------------------------------------------*/
\r
65 /* Constants to setup and access the UART. */
\r
66 #define serDLAB ( ( unsigned portCHAR ) 0x80 )
\r
67 #define serENABLE_INTERRUPTS ( ( unsigned portCHAR ) 0x03 )
\r
68 #define serNO_PARITY ( ( unsigned portCHAR ) 0x00 )
\r
69 #define ser1_STOP_BIT ( ( unsigned portCHAR ) 0x00 )
\r
70 #define ser8_BIT_CHARS ( ( unsigned portCHAR ) 0x03 )
\r
71 #define serFIFO_ON ( ( unsigned portCHAR ) 0x01 )
\r
72 #define serCLEAR_FIFO ( ( unsigned portCHAR ) 0x06 )
\r
73 #define serWANTED_CLOCK_SCALING ( ( unsigned portLONG ) 16 )
\r
75 /* Constants to setup and access the VIC. */
\r
76 #define serU0VIC_CHANNEL ( ( unsigned portLONG ) 0x0006 )
\r
77 #define serU0VIC_CHANNEL_BIT ( ( unsigned portLONG ) 0x0040 )
\r
78 #define serU0VIC_ENABLE ( ( unsigned portLONG ) 0x0020 )
\r
81 #define serINVALID_QUEUE ( ( xQueueHandle ) 0 )
\r
82 #define serHANDLE ( ( xComPortHandle ) 1 )
\r
83 #define serNO_BLOCK ( ( portTickType ) 0 )
\r
85 /*-----------------------------------------------------------*/
\r
87 /* Queues used to hold received characters, and characters waiting to be
\r
89 static xQueueHandle xRxedChars;
\r
90 static xQueueHandle xCharsForTx;
\r
92 /*-----------------------------------------------------------*/
\r
94 /* Communication flag between the interrupt service routine and serial API. */
\r
95 static volatile portLONG *plTHREEmpty;
\r
98 * The queues are created in serialISR.c as they are used from the ISR.
\r
99 * Obtain references to the queues and THRE Empty flag.
\r
101 extern void vSerialISRCreateQueues( unsigned portBASE_TYPE uxQueueLength, xQueueHandle *pxRxedChars, xQueueHandle *pxCharsForTx, portLONG volatile **pplTHREEmptyFlag );
\r
103 /*-----------------------------------------------------------*/
\r
105 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
107 unsigned portLONG ulDivisor, ulWantedClock;
\r
108 xComPortHandle xReturn = serHANDLE;
\r
110 /* The queues are used in the serial ISR routine, so are created from
\r
111 serialISR.c (which is always compiled to ARM mode). */
\r
112 vSerialISRCreateQueues( uxQueueLength, &xRxedChars, &xCharsForTx, &plTHREEmpty );
\r
115 ( xRxedChars != serINVALID_QUEUE ) &&
\r
116 ( xCharsForTx != serINVALID_QUEUE ) &&
\r
117 ( ulWantedBaud != ( unsigned portLONG ) 0 )
\r
120 portENTER_CRITICAL();
\r
122 /* The reference to the ISR function is required to load into the
\r
123 interrupt controller. The prototype is slightly different
\r
124 depending on whether in ARM or THUMB mode. */
\r
125 #ifdef KEIL_THUMB_INTERWORK
\r
126 extern void ( vUART_ISR )( void ) __arm __task;
\r
128 extern void ( vUART_ISR )( void ) __task;
\r
131 /* Setup the baud rate: Calculate the divisor value. */
\r
132 ulWantedClock = ulWantedBaud * serWANTED_CLOCK_SCALING;
\r
133 ulDivisor = configCPU_CLOCK_HZ / ulWantedClock;
\r
135 /* Set the DLAB bit so we can access the divisor. */
\r
138 /* Setup the divisor. */
\r
139 U0DLL = ( unsigned portCHAR ) ( ulDivisor & ( unsigned portLONG ) 0xff );
\r
141 U0DLM = ( unsigned portCHAR ) ( ulDivisor & ( unsigned portLONG ) 0xff );
\r
143 /* Turn on the FIFO's and clear the buffers. */
\r
144 U0FCR = ( serFIFO_ON | serCLEAR_FIFO );
\r
146 /* Setup transmission format. */
\r
147 U0LCR = serNO_PARITY | ser1_STOP_BIT | ser8_BIT_CHARS;
\r
149 /* Setup the VIC for the UART. */
\r
150 VICIntSelect &= ~( serU0VIC_CHANNEL_BIT );
\r
151 VICIntEnable |= serU0VIC_CHANNEL_BIT;
\r
152 VICVectAddr1 = ( unsigned portLONG ) vUART_ISR;
\r
153 VICVectCntl1 = serU0VIC_CHANNEL | serU0VIC_ENABLE;
\r
155 /* Enable UART0 interrupts. */
\r
156 U0IER |= serENABLE_INTERRUPTS;
\r
158 portEXIT_CRITICAL();
\r
162 xReturn = ( xComPortHandle ) 0;
\r
167 /*-----------------------------------------------------------*/
\r
169 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )
\r
171 /* The port handle is not required as this driver only supports UART0. */
\r
174 /* Get the next character from the buffer. Return false if no characters
\r
175 are available, or arrive before xBlockTime expires. */
\r
176 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
185 /*-----------------------------------------------------------*/
\r
187 void vSerialPutString( xComPortHandle pxPort, const signed portCHAR * const pcString, unsigned portSHORT usStringLength )
\r
189 signed portCHAR *pxNext;
\r
191 /* NOTE: This implementation does not handle the queue being full as no
\r
192 block time is used! */
\r
194 /* The port handle is not required as this driver only supports UART0. */
\r
196 ( void ) usStringLength;
\r
198 /* Send each character in the string, one at a time. */
\r
199 pxNext = ( signed portCHAR * ) pcString;
\r
202 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );
\r
206 /*-----------------------------------------------------------*/
\r
208 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )
\r
210 signed portBASE_TYPE xReturn;
\r
212 /* The port handle is not required as this driver only supports UART0. */
\r
215 portENTER_CRITICAL();
\r
217 /* Is there space to write directly to the UART? */
\r
218 if( *plTHREEmpty == ( portLONG ) pdTRUE )
\r
220 /* We wrote the character directly to the UART, so was
\r
222 *plTHREEmpty = pdFALSE;
\r
228 /* We cannot write directly to the UART, so queue the character.
\r
229 Block for a maximum of xBlockTime if there is no space in the
\r
230 queue. It is ok to block within a critical section as each
\r
231 task has it's own critical section management. */
\r
232 xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );
\r
234 /* Depending on queue sizing and task prioritisation: While we
\r
235 were blocked waiting to post interrupts were not disabled. It is
\r
236 possible that the serial ISR has emptied the Tx queue, in which
\r
237 case we need to start the Tx off again. */
\r
238 if( *plTHREEmpty == ( portLONG ) pdTRUE )
\r
240 xQueueReceive( xCharsForTx, &cOutChar, serNO_BLOCK );
\r
241 *plTHREEmpty = pdFALSE;
\r
246 portEXIT_CRITICAL();
\r
250 /*-----------------------------------------------------------*/
\r