2 FreeRTOS.org V4.2.1 - Copyright (C) 2003-2007 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
27 See http://www.FreeRTOS.org for documentation, latest information, license
\r
28 and contact details. Please ensure to read the configuration and relevant
\r
29 port sections of the online documentation.
\r
31 Also see http://www.SafeRTOS.com for an IEC 61508 compliant version along
\r
32 with commercial development and support options.
\r
33 ***************************************************************************
\r
38 BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART0.
\r
40 This file contains all the serial port components that can be compiled to
\r
41 either ARM or THUMB mode. Components that must be compiled to ARM mode are
\r
42 contained in serialISR.c.
\r
45 /* Standard includes. */
\r
48 /* Scheduler includes. */
\r
49 #include "FreeRTOS.h"
\r
53 /* Demo application includes. */
\r
56 /*-----------------------------------------------------------*/
\r
58 /* Constants to setup and access the UART. */
\r
59 #define serDLAB ( ( unsigned portCHAR ) 0x80 )
\r
60 #define serENABLE_INTERRUPTS ( ( unsigned portCHAR ) 0x03 )
\r
61 #define serNO_PARITY ( ( unsigned portCHAR ) 0x00 )
\r
62 #define ser1_STOP_BIT ( ( unsigned portCHAR ) 0x00 )
\r
63 #define ser8_BIT_CHARS ( ( unsigned portCHAR ) 0x03 )
\r
64 #define serFIFO_ON ( ( unsigned portCHAR ) 0x01 )
\r
65 #define serCLEAR_FIFO ( ( unsigned portCHAR ) 0x06 )
\r
66 #define serWANTED_CLOCK_SCALING ( ( unsigned portLONG ) 16 )
\r
68 /* Constants to setup and access the VIC. */
\r
69 #define serU0VIC_CHANNEL ( ( unsigned portLONG ) 0x0006 )
\r
70 #define serU0VIC_CHANNEL_BIT ( ( unsigned portLONG ) 0x0040 )
\r
71 #define serU0VIC_ENABLE ( ( unsigned portLONG ) 0x0020 )
\r
74 #define serINVALID_QUEUE ( ( xQueueHandle ) 0 )
\r
75 #define serHANDLE ( ( xComPortHandle ) 1 )
\r
76 #define serNO_BLOCK ( ( portTickType ) 0 )
\r
78 /*-----------------------------------------------------------*/
\r
80 /* Queues used to hold received characters, and characters waiting to be
\r
82 static xQueueHandle xRxedChars;
\r
83 static xQueueHandle xCharsForTx;
\r
85 /*-----------------------------------------------------------*/
\r
87 /* Communication flag between the interrupt service routine and serial API. */
\r
88 static volatile portLONG *plTHREEmpty;
\r
91 * The queues are created in serialISR.c as they are used from the ISR.
\r
92 * Obtain references to the queues and THRE Empty flag.
\r
94 extern void vSerialISRCreateQueues( unsigned portBASE_TYPE uxQueueLength, xQueueHandle *pxRxedChars, xQueueHandle *pxCharsForTx, portLONG volatile **pplTHREEmptyFlag );
\r
96 /*-----------------------------------------------------------*/
\r
98 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
100 unsigned portLONG ulDivisor, ulWantedClock;
\r
101 xComPortHandle xReturn = serHANDLE;
\r
103 /* The queues are used in the serial ISR routine, so are created from
\r
104 serialISR.c (which is always compiled to ARM mode). */
\r
105 vSerialISRCreateQueues( uxQueueLength, &xRxedChars, &xCharsForTx, &plTHREEmpty );
\r
108 ( xRxedChars != serINVALID_QUEUE ) &&
\r
109 ( xCharsForTx != serINVALID_QUEUE ) &&
\r
110 ( ulWantedBaud != ( unsigned portLONG ) 0 )
\r
113 portENTER_CRITICAL();
\r
115 /* The reference to the ISR function is required to load into the
\r
116 interrupt controller. The prototype is slightly different
\r
117 depending on whether in ARM or THUMB mode. */
\r
118 #ifdef KEIL_THUMB_INTERWORK
\r
119 extern void ( vUART_ISR )( void ) __arm __task;
\r
121 extern void ( vUART_ISR )( void ) __task;
\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 U0DLL = ( unsigned portCHAR ) ( ulDivisor & ( unsigned portLONG ) 0xff );
\r
134 U0DLM = ( unsigned portCHAR ) ( ulDivisor & ( unsigned portLONG ) 0xff );
\r
136 /* Turn on the FIFO's and clear the buffers. */
\r
137 U0FCR = ( serFIFO_ON | serCLEAR_FIFO );
\r
139 /* Setup transmission format. */
\r
140 U0LCR = serNO_PARITY | ser1_STOP_BIT | ser8_BIT_CHARS;
\r
142 /* Setup the VIC for the UART. */
\r
143 VICIntSelect &= ~( serU0VIC_CHANNEL_BIT );
\r
144 VICIntEnable |= serU0VIC_CHANNEL_BIT;
\r
145 VICVectAddr1 = ( unsigned portLONG ) vUART_ISR;
\r
146 VICVectCntl1 = serU0VIC_CHANNEL | serU0VIC_ENABLE;
\r
148 /* Enable UART0 interrupts. */
\r
149 U0IER |= serENABLE_INTERRUPTS;
\r
151 portEXIT_CRITICAL();
\r
155 xReturn = ( xComPortHandle ) 0;
\r
160 /*-----------------------------------------------------------*/
\r
162 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType 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 portCHAR * const pcString, unsigned portSHORT usStringLength )
\r
182 signed portCHAR *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 portCHAR * ) pcString;
\r
195 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );
\r
199 /*-----------------------------------------------------------*/
\r
201 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType 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( *plTHREEmpty == ( portLONG ) pdTRUE )
\r
213 /* We wrote the character directly to the UART, so was
\r
215 *plTHREEmpty = 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( *plTHREEmpty == ( portLONG ) pdTRUE )
\r
233 xQueueReceive( xCharsForTx, &cOutChar, serNO_BLOCK );
\r
234 *plTHREEmpty = pdFALSE;
\r
239 portEXIT_CRITICAL();
\r
243 /*-----------------------------------------------------------*/
\r