2 FreeRTOS V4.0.1 - Copyright (C) 2003-2006 Richard Barry.
\r
4 This file is part of the FreeRTOS distribution.
\r
6 FreeRTOS 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 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; 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, 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
30 ***************************************************************************
\r
35 BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART0.
\r
37 This file contains all the serial port components that can be compiled to
\r
38 either ARM or THUMB mode. Components that must be compiled to ARM mode are
\r
39 contained in serialISR.c.
\r
42 /* Standard includes. */
\r
45 /* Scheduler includes. */
\r
46 #include "FreeRTOS.h"
\r
50 /* Demo application includes. */
\r
53 /*-----------------------------------------------------------*/
\r
55 /* Constants to setup and access the UART. */
\r
56 #define serDLAB ( ( unsigned portCHAR ) 0x80 )
\r
57 #define serENABLE_INTERRUPTS ( ( unsigned portCHAR ) 0x03 )
\r
58 #define serNO_PARITY ( ( unsigned portCHAR ) 0x00 )
\r
59 #define ser1_STOP_BIT ( ( unsigned portCHAR ) 0x00 )
\r
60 #define ser8_BIT_CHARS ( ( unsigned portCHAR ) 0x03 )
\r
61 #define serFIFO_ON ( ( unsigned portCHAR ) 0x01 )
\r
62 #define serCLEAR_FIFO ( ( unsigned portCHAR ) 0x06 )
\r
63 #define serWANTED_CLOCK_SCALING ( ( unsigned portLONG ) 16 )
\r
65 /* Constants to setup and access the VIC. */
\r
66 #define serU0VIC_CHANNEL ( ( unsigned portLONG ) 0x0006 )
\r
67 #define serU0VIC_CHANNEL_BIT ( ( unsigned portLONG ) 0x0040 )
\r
68 #define serU0VIC_ENABLE ( ( unsigned portLONG ) 0x0020 )
\r
71 #define serINVALID_QUEUE ( ( xQueueHandle ) 0 )
\r
72 #define serHANDLE ( ( xComPortHandle ) 1 )
\r
73 #define serNO_BLOCK ( ( portTickType ) 0 )
\r
75 /*-----------------------------------------------------------*/
\r
77 /* Queues used to hold received characters, and characters waiting to be
\r
79 static xQueueHandle xRxedChars;
\r
80 static xQueueHandle xCharsForTx;
\r
82 /*-----------------------------------------------------------*/
\r
84 /* Communication flag between the interrupt service routine and serial API. */
\r
85 static volatile portLONG *plTHREEmpty;
\r
88 * The queues are created in serialISR.c as they are used from the ISR.
\r
89 * Obtain references to the queues and THRE Empty flag.
\r
91 extern void vSerialISRCreateQueues( unsigned portBASE_TYPE uxQueueLength, xQueueHandle *pxRxedChars, xQueueHandle *pxCharsForTx, portLONG volatile **pplTHREEmptyFlag );
\r
93 /*-----------------------------------------------------------*/
\r
95 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
97 unsigned portLONG ulDivisor, ulWantedClock;
\r
98 xComPortHandle xReturn = serHANDLE;
\r
100 /* The queues are used in the serial ISR routine, so are created from
\r
101 serialISR.c (which is always compiled to ARM mode). */
\r
102 vSerialISRCreateQueues( uxQueueLength, &xRxedChars, &xCharsForTx, &plTHREEmpty );
\r
105 ( xRxedChars != serINVALID_QUEUE ) &&
\r
106 ( xCharsForTx != serINVALID_QUEUE ) &&
\r
107 ( ulWantedBaud != ( unsigned portLONG ) 0 )
\r
110 portENTER_CRITICAL();
\r
112 /* The reference to the ISR function is required to load into the
\r
113 interrupt controller. The prototype is slightly different
\r
114 depending on whether in ARM or THUMB mode. */
\r
115 #ifdef KEIL_THUMB_INTERWORK
\r
116 extern void ( vUART_ISR )( void ) __arm __task;
\r
118 extern void ( vUART_ISR )( void ) __task;
\r
121 /* Setup the baud rate: Calculate the divisor value. */
\r
122 ulWantedClock = ulWantedBaud * serWANTED_CLOCK_SCALING;
\r
123 ulDivisor = configCPU_CLOCK_HZ / ulWantedClock;
\r
125 /* Set the DLAB bit so we can access the divisor. */
\r
128 /* Setup the divisor. */
\r
129 U0DLL = ( unsigned portCHAR ) ( ulDivisor & ( unsigned portLONG ) 0xff );
\r
131 U0DLM = ( unsigned portCHAR ) ( ulDivisor & ( unsigned portLONG ) 0xff );
\r
133 /* Turn on the FIFO's and clear the buffers. */
\r
134 U0FCR = ( serFIFO_ON | serCLEAR_FIFO );
\r
136 /* Setup transmission format. */
\r
137 U0LCR = serNO_PARITY | ser1_STOP_BIT | ser8_BIT_CHARS;
\r
139 /* Setup the VIC for the UART. */
\r
140 VICIntSelect &= ~( serU0VIC_CHANNEL_BIT );
\r
141 VICIntEnable |= serU0VIC_CHANNEL_BIT;
\r
142 VICVectAddr1 = ( unsigned portLONG ) vUART_ISR;
\r
143 VICVectCntl1 = serU0VIC_CHANNEL | serU0VIC_ENABLE;
\r
145 /* Enable UART0 interrupts. */
\r
146 U0IER |= serENABLE_INTERRUPTS;
\r
148 portEXIT_CRITICAL();
\r
152 xReturn = ( xComPortHandle ) 0;
\r
157 /*-----------------------------------------------------------*/
\r
159 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )
\r
161 /* The port handle is not required as this driver only supports UART0. */
\r
164 /* Get the next character from the buffer. Return false if no characters
\r
165 are available, or arrive before xBlockTime expires. */
\r
166 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
175 /*-----------------------------------------------------------*/
\r
177 void vSerialPutString( xComPortHandle pxPort, const signed portCHAR * const pcString, unsigned portSHORT usStringLength )
\r
179 signed portCHAR *pxNext;
\r
181 /* NOTE: This implementation does not handle the queue being full as no
\r
182 block time is used! */
\r
184 /* The port handle is not required as this driver only supports UART0. */
\r
186 ( void ) usStringLength;
\r
188 /* Send each character in the string, one at a time. */
\r
189 pxNext = ( signed portCHAR * ) pcString;
\r
192 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );
\r
196 /*-----------------------------------------------------------*/
\r
198 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )
\r
200 signed portBASE_TYPE xReturn;
\r
202 /* The port handle is not required as this driver only supports UART0. */
\r
205 portENTER_CRITICAL();
\r
207 /* Is there space to write directly to the UART? */
\r
208 if( *plTHREEmpty == ( portLONG ) pdTRUE )
\r
210 /* We wrote the character directly to the UART, so was
\r
212 *plTHREEmpty = pdFALSE;
\r
218 /* We cannot write directly to the UART, so queue the character.
\r
219 Block for a maximum of xBlockTime if there is no space in the
\r
220 queue. It is ok to block within a critical section as each
\r
221 task has it's own critical section management. */
\r
222 xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );
\r
224 /* Depending on queue sizing and task prioritisation: While we
\r
225 were blocked waiting to post interrupts were not disabled. It is
\r
226 possible that the serial ISR has emptied the Tx queue, in which
\r
227 case we need to start the Tx off again. */
\r
228 if( *plTHREEmpty == ( portLONG ) pdTRUE )
\r
230 xQueueReceive( xCharsForTx, &cOutChar, serNO_BLOCK );
\r
231 *plTHREEmpty = pdFALSE;
\r
236 portEXIT_CRITICAL();
\r
240 /*-----------------------------------------------------------*/
\r