2 FreeRTOS.org V5.3.0 - Copyright (C) 2003-2009 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 it
\r
7 under the terms of the GNU General Public License (version 2) as published
\r
8 by the Free Software Foundation and modified by the FreeRTOS exception.
\r
9 **NOTE** The exception to the GPL is included to allow you to distribute a
\r
10 combined work that includes FreeRTOS.org without being obliged to provide
\r
11 the source code for any proprietary components. Alternative commercial
\r
12 license and support terms are also available upon request. See the
\r
13 licensing section of http://www.FreeRTOS.org for full details.
\r
15 FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT
\r
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
20 You should have received a copy of the GNU General Public License along
\r
21 with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59
\r
22 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
25 ***************************************************************************
\r
27 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
29 * This is a concise, step by step, 'hands on' guide that describes both *
\r
30 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
31 * explains numerous examples that are written using the FreeRTOS API. *
\r
32 * Full source code for all the examples is provided in an accompanying *
\r
35 ***************************************************************************
\r
39 Please ensure to read the configuration and relevant port sections of the
\r
40 online documentation.
\r
42 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
45 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
48 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
49 licensing and training services.
\r
54 * BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER.
\r
56 * This file only supports UART 2
\r
59 /* Standard includes. */
\r
62 /* Scheduler includes. */
\r
63 #include "FreeRTOS.h"
\r
67 /* Demo application includes. */
\r
70 /* The queue used to hold received characters. */
\r
71 static xQueueHandle xRxedChars;
\r
73 /* The queue used to hold characters waiting transmission. */
\r
74 static xQueueHandle xCharsForTx;
\r
76 static volatile portSHORT sTHREEmpty;
\r
78 /*-----------------------------------------------------------*/
\r
80 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
82 portENTER_CRITICAL();
\r
84 /* Create the queues used by the com test task. */
\r
85 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
\r
86 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
\r
88 /* Initialize UART asynchronous mode */
\r
89 BGR02 = configPER_CLOCK_HZ / ulWantedBaud;
\r
91 SCR02 = 0x17; /* 8N1 */
\r
92 SMR02 = 0x0d; /* enable SOT3, Reset, normal mode */
\r
93 SSR02 = 0x02; /* LSB first, enable receive interrupts */
\r
95 PFR20_D0 = 1; /* enable UART */
\r
96 PFR20_D1 = 1; /* enable UART */
\r
98 EPFR20_D1 = 0; /* enable UART */
\r
100 portEXIT_CRITICAL();
\r
102 /* Unlike other ports, this serial code does not allow for more than one
\r
103 com port. We therefore don't return a pointer to a port structure and can
\r
104 instead just return NULL. */
\r
107 /*-----------------------------------------------------------*/
\r
109 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )
\r
111 /* Get the next character from the buffer. Return false if no characters
\r
112 are available, or arrive before xBlockTime expires. */
\r
113 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
122 /*-----------------------------------------------------------*/
\r
124 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )
\r
126 signed portBASE_TYPE xReturn;
\r
128 /* Transmit a character. */
\r
129 portENTER_CRITICAL();
\r
131 if( sTHREEmpty == pdTRUE )
\r
133 /* If sTHREEmpty is true then the UART Tx ISR has indicated that
\r
134 there are no characters queued to be transmitted - so we can
\r
135 write the character directly to the shift Tx register. */
\r
136 sTHREEmpty = pdFALSE;
\r
142 /* sTHREEmpty is false, so there are still characters waiting to be
\r
143 transmitted. We have to queue this character so it gets
\r
144 transmitted in turn. */
\r
146 /* Return false if after the block time there is no room on the Tx
\r
147 queue. It is ok to block inside a critical section as each task
\r
148 maintains it's own critical section status. */
\r
149 if (xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) == pdTRUE)
\r
159 if (pdPASS == xReturn)
\r
161 /* Turn on the Tx interrupt so the ISR will remove the character from the
\r
162 queue and send it. This does not need to be in a critical section as
\r
163 if the interrupt has already removed the character the next interrupt
\r
164 will simply turn off the Tx interrupt again. */
\r
169 portEXIT_CRITICAL();
\r
173 /*-----------------------------------------------------------*/
\r
176 * UART RX interrupt service routine.
\r
178 __interrupt void UART2_RxISR (void)
\r
180 signed portCHAR cChar;
\r
181 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
183 /* Get the character from the UART and post it on the queue of Rxed
\r
187 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
189 if( xHigherPriorityTaskWoken )
\r
191 /*If the post causes a task to wake force a context switch
\r
192 as the woken task may have a higher priority than the task we have
\r
194 portYIELD_FROM_ISR();
\r
198 /*-----------------------------------------------------------*/
\r
201 * UART Tx interrupt service routine.
\r
203 __interrupt void UART2_TxISR (void)
\r
205 signed portCHAR cChar;
\r
206 signed portBASE_TYPE xTaskWoken = pdFALSE;
\r
208 /* The previous character has been transmitted. See if there are any
\r
209 further characters waiting transmission. */
\r
210 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWoken ) == pdTRUE )
\r
212 /* There was another character queued - transmit it now. */
\r
217 /* There were no other characters to transmit. */
\r
218 sTHREEmpty = pdTRUE;
\r
220 /* Disable transmit interrupts */
\r