2 FreeRTOS.org V4.4.0 - 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
37 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR DEMO PURPOSES */
\r
39 #include "FreeRTOS.h"
\r
44 /* Constants required to setup the serial control register. */
\r
45 #define ser8_BIT_MODE ( ( unsigned portCHAR ) 0x40 )
\r
46 #define serRX_ENABLE ( ( unsigned portCHAR ) 0x10 )
\r
48 /* Constants to setup the timer used to generate the baud rate. */
\r
49 #define serCLOCK_DIV_48 ( ( unsigned portCHAR ) 0x03 )
\r
50 #define serUSE_PRESCALED_CLOCK ( ( unsigned portCHAR ) 0x10 )
\r
51 #define ser8BIT_WITH_RELOAD ( ( unsigned portCHAR ) 0x20 )
\r
52 #define serSMOD ( ( unsigned portCHAR ) 0x10 )
\r
54 static xQueueHandle xRxedChars;
\r
55 static xQueueHandle xCharsForTx;
\r
57 data static unsigned portBASE_TYPE uxTxEmpty;
\r
59 /*-----------------------------------------------------------*/
\r
61 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
63 unsigned portLONG ulReloadValue;
\r
64 const portFLOAT fBaudConst = ( portFLOAT ) configCPU_CLOCK_HZ * ( portFLOAT ) 2.0;
\r
65 unsigned portCHAR ucOriginalSFRPage;
\r
67 portENTER_CRITICAL();
\r
69 ucOriginalSFRPage = SFRPAGE;
\r
74 /* Create the queues used by the com test task. */
\r
75 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( portCHAR ) );
\r
76 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( portCHAR ) );
\r
78 /* Calculate the baud rate to use timer 1. */
\r
79 ulReloadValue = ( unsigned portLONG ) ( ( ( portFLOAT ) 256 - ( fBaudConst / ( portFLOAT ) ( 32 * ulWantedBaud ) ) ) + ( portFLOAT ) 0.5 );
\r
81 /* Set timer one for desired mode of operation. */
\r
83 TMOD |= ser8BIT_WITH_RELOAD;
\r
86 /* Set the reload and start values for the time. */
\r
87 TL1 = ( unsigned portCHAR ) ulReloadValue;
\r
88 TH1 = ( unsigned portCHAR ) ulReloadValue;
\r
90 /* Setup the control register for standard n, 8, 1 - variable baud rate. */
\r
91 SCON = ser8_BIT_MODE | serRX_ENABLE;
\r
93 /* Enable the serial port interrupts */
\r
96 /* Start the timer. */
\r
99 SFRPAGE = ucOriginalSFRPage;
\r
101 portEXIT_CRITICAL();
\r
103 /* Unlike some ports, this serial code does not allow for more than one
\r
104 com port. We therefore don't return a pointer to a port structure and can
\r
105 instead just return NULL. */
\r
108 /*-----------------------------------------------------------*/
\r
110 void vSerialISR( void ) interrupt 4
\r
113 portBASE_TYPE xTaskWokenByRx = pdFALSE, xTaskWokenByTx = pdFALSE;
\r
115 /* 8051 port interrupt routines MUST be placed within a critical section
\r
116 if taskYIELD() is used within the ISR! */
\r
118 portENTER_CRITICAL();
\r
122 /* Get the character and post it on the queue of Rxed characters.
\r
123 If the post causes a task to wake force a context switch as the woken task
\r
124 may have a higher priority than the task we have interrupted. */
\r
128 if( xQueueSendFromISR( xRxedChars, &cChar, pdFALSE ) )
\r
130 xTaskWokenByRx = ( portBASE_TYPE ) pdTRUE;
\r
136 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWokenByTx ) == ( portBASE_TYPE ) pdTRUE )
\r
138 /* Send the next character queued for Tx. */
\r
143 /* Queue empty, nothing to send. */
\r
144 uxTxEmpty = pdTRUE;
\r
150 if( xTaskWokenByRx || xTaskWokenByTx )
\r
155 portEXIT_CRITICAL();
\r
157 /*-----------------------------------------------------------*/
\r
159 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )
\r
161 /* There is only one port supported. */
\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
168 return ( portBASE_TYPE ) pdTRUE;
\r
172 return ( portBASE_TYPE ) pdFALSE;
\r
175 /*-----------------------------------------------------------*/
\r
177 portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )
\r
179 portBASE_TYPE xReturn;
\r
181 /* There is only one port supported. */
\r
184 portENTER_CRITICAL();
\r
186 if( uxTxEmpty == pdTRUE )
\r
189 uxTxEmpty = pdFALSE;
\r
190 xReturn = ( portBASE_TYPE ) pdTRUE;
\r
194 xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );
\r
196 if( xReturn == ( portBASE_TYPE ) pdFALSE )
\r
198 xReturn = ( portBASE_TYPE ) pdTRUE;
\r
202 portEXIT_CRITICAL();
\r
206 /*-----------------------------------------------------------*/
\r
208 void vSerialClose( xComPortHandle xPort )
\r
210 /* Not implemented in this port. */
\r
213 /*-----------------------------------------------------------*/
\r