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
34 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR DEMO PURPOSES */
\r
36 #include "FreeRTOS.h"
\r
41 /* Constants required to setup the serial control register. */
\r
42 #define ser8_BIT_MODE ( ( unsigned portCHAR ) 0x40 )
\r
43 #define serRX_ENABLE ( ( unsigned portCHAR ) 0x10 )
\r
45 /* Constants to setup the timer used to generate the baud rate. */
\r
46 #define serCLOCK_DIV_48 ( ( unsigned portCHAR ) 0x03 )
\r
47 #define serUSE_PRESCALED_CLOCK ( ( unsigned portCHAR ) 0x10 )
\r
48 #define ser8BIT_WITH_RELOAD ( ( unsigned portCHAR ) 0x20 )
\r
49 #define serSMOD ( ( unsigned portCHAR ) 0x10 )
\r
51 static xQueueHandle xRxedChars;
\r
52 static xQueueHandle xCharsForTx;
\r
54 data static unsigned portBASE_TYPE uxTxEmpty;
\r
56 /*-----------------------------------------------------------*/
\r
58 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
60 unsigned portLONG ulReloadValue;
\r
61 const portFLOAT fBaudConst = ( portFLOAT ) configCPU_CLOCK_HZ * ( portFLOAT ) 2.0;
\r
62 unsigned portCHAR ucOriginalSFRPage;
\r
64 portENTER_CRITICAL();
\r
66 ucOriginalSFRPage = SFRPAGE;
\r
71 /* Create the queues used by the com test task. */
\r
72 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( portCHAR ) );
\r
73 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( portCHAR ) );
\r
75 /* Calculate the baud rate to use timer 1. */
\r
76 ulReloadValue = ( unsigned portLONG ) ( ( ( portFLOAT ) 256 - ( fBaudConst / ( portFLOAT ) ( 32 * ulWantedBaud ) ) ) + ( portFLOAT ) 0.5 );
\r
78 /* Set timer one for desired mode of operation. */
\r
80 TMOD |= ser8BIT_WITH_RELOAD;
\r
83 /* Set the reload and start values for the time. */
\r
84 TL1 = ( unsigned portCHAR ) ulReloadValue;
\r
85 TH1 = ( unsigned portCHAR ) ulReloadValue;
\r
87 /* Setup the control register for standard n, 8, 1 - variable baud rate. */
\r
88 SCON = ser8_BIT_MODE | serRX_ENABLE;
\r
90 /* Enable the serial port interrupts */
\r
93 /* Start the timer. */
\r
96 SFRPAGE = ucOriginalSFRPage;
\r
98 portEXIT_CRITICAL();
\r
100 /* Unlike some ports, this serial code does not allow for more than one
\r
101 com port. We therefore don't return a pointer to a port structure and can
\r
102 instead just return NULL. */
\r
105 /*-----------------------------------------------------------*/
\r
107 void vSerialISR( void ) interrupt 4
\r
110 portBASE_TYPE xTaskWokenByRx = pdFALSE, xTaskWokenByTx = pdFALSE;
\r
112 /* 8051 port interrupt routines MUST be placed within a critical section
\r
113 if taskYIELD() is used within the ISR! */
\r
115 portENTER_CRITICAL();
\r
119 /* Get the character and post it on the queue of Rxed characters.
\r
120 If the post causes a task to wake force a context switch as the woken task
\r
121 may have a higher priority than the task we have interrupted. */
\r
125 if( xQueueSendFromISR( xRxedChars, &cChar, pdFALSE ) )
\r
127 xTaskWokenByRx = ( portBASE_TYPE ) pdTRUE;
\r
133 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWokenByTx ) == ( portBASE_TYPE ) pdTRUE )
\r
135 /* Send the next character queued for Tx. */
\r
140 /* Queue empty, nothing to send. */
\r
141 uxTxEmpty = pdTRUE;
\r
147 if( xTaskWokenByRx || xTaskWokenByTx )
\r
152 portEXIT_CRITICAL();
\r
154 /*-----------------------------------------------------------*/
\r
156 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )
\r
158 /* There is only one port supported. */
\r
161 /* Get the next character from the buffer. Return false if no characters
\r
162 are available, or arrive before xBlockTime expires. */
\r
163 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
165 return ( portBASE_TYPE ) pdTRUE;
\r
169 return ( portBASE_TYPE ) pdFALSE;
\r
172 /*-----------------------------------------------------------*/
\r
174 portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )
\r
176 portBASE_TYPE xReturn;
\r
178 /* There is only one port supported. */
\r
181 portENTER_CRITICAL();
\r
183 if( uxTxEmpty == pdTRUE )
\r
186 uxTxEmpty = pdFALSE;
\r
187 xReturn = ( portBASE_TYPE ) pdTRUE;
\r
191 xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );
\r
193 if( xReturn == ( portBASE_TYPE ) pdFALSE )
\r
195 xReturn = ( portBASE_TYPE ) pdTRUE;
\r
199 portEXIT_CRITICAL();
\r
203 /*-----------------------------------------------------------*/
\r
205 void vSerialClose( xComPortHandle xPort )
\r
207 /* Not implemented in this port. */
\r
210 /*-----------------------------------------------------------*/
\r