2 FreeRTOS V7.2.0 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\r
60 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
61 the code with commercial support, indemnification, and middleware, under
\r
62 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
63 provide a safety engineered and independently SIL3 certified version under
\r
64 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
68 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR DEMO PURPOSES */
\r
70 #include "FreeRTOS.h"
\r
75 /* Constants required to setup the serial control register. */
\r
76 #define ser8_BIT_MODE ( ( unsigned char ) 0x40 )
\r
77 #define serRX_ENABLE ( ( unsigned char ) 0x10 )
\r
79 /* Constants to setup the timer used to generate the baud rate. */
\r
80 #define serCLOCK_DIV_48 ( ( unsigned char ) 0x03 )
\r
81 #define serUSE_PRESCALED_CLOCK ( ( unsigned char ) 0x10 )
\r
82 #define ser8BIT_WITH_RELOAD ( ( unsigned char ) 0x20 )
\r
83 #define serSMOD ( ( unsigned char ) 0x10 )
\r
85 static xQueueHandle xRxedChars;
\r
86 static xQueueHandle xCharsForTx;
\r
88 data static unsigned portBASE_TYPE uxTxEmpty;
\r
90 /*-----------------------------------------------------------*/
\r
92 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
94 unsigned long ulReloadValue;
\r
95 const portFLOAT fBaudConst = ( portFLOAT ) configCPU_CLOCK_HZ * ( portFLOAT ) 2.0;
\r
96 unsigned char ucOriginalSFRPage;
\r
98 portENTER_CRITICAL();
\r
100 ucOriginalSFRPage = SFRPAGE;
\r
103 uxTxEmpty = pdTRUE;
\r
105 /* Create the queues used by the com test task. */
\r
106 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( char ) );
\r
107 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( char ) );
\r
109 /* Calculate the baud rate to use timer 1. */
\r
110 ulReloadValue = ( unsigned long ) ( ( ( portFLOAT ) 256 - ( fBaudConst / ( portFLOAT ) ( 32 * ulWantedBaud ) ) ) + ( portFLOAT ) 0.5 );
\r
112 /* Set timer one for desired mode of operation. */
\r
114 TMOD |= ser8BIT_WITH_RELOAD;
\r
117 /* Set the reload and start values for the time. */
\r
118 TL1 = ( unsigned char ) ulReloadValue;
\r
119 TH1 = ( unsigned char ) ulReloadValue;
\r
121 /* Setup the control register for standard n, 8, 1 - variable baud rate. */
\r
122 SCON = ser8_BIT_MODE | serRX_ENABLE;
\r
124 /* Enable the serial port interrupts */
\r
127 /* Start the timer. */
\r
130 SFRPAGE = ucOriginalSFRPage;
\r
132 portEXIT_CRITICAL();
\r
134 /* Unlike some ports, this serial code does not allow for more than one
\r
135 com port. We therefore don't return a pointer to a port structure and can
\r
136 instead just return NULL. */
\r
139 /*-----------------------------------------------------------*/
\r
141 void vSerialISR( void ) interrupt 4
\r
144 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
146 /* 8051 port interrupt routines MUST be placed within a critical section
\r
147 if taskYIELD() is used within the ISR! */
\r
149 portENTER_CRITICAL();
\r
153 /* Get the character and post it on the queue of Rxed characters.
\r
154 If the post causes a task to wake force a context switch if the woken task
\r
155 has a higher priority than the task we have interrupted. */
\r
159 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
164 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == ( portBASE_TYPE ) pdTRUE )
\r
166 /* Send the next character queued for Tx. */
\r
171 /* Queue empty, nothing to send. */
\r
172 uxTxEmpty = pdTRUE;
\r
178 if( xHigherPriorityTaskWoken )
\r
183 portEXIT_CRITICAL();
\r
185 /*-----------------------------------------------------------*/
\r
187 portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )
\r
189 /* There is only one port supported. */
\r
192 /* Get the next character from the buffer. Return false if no characters
\r
193 are available, or arrive before xBlockTime expires. */
\r
194 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
196 return ( portBASE_TYPE ) pdTRUE;
\r
200 return ( portBASE_TYPE ) pdFALSE;
\r
203 /*-----------------------------------------------------------*/
\r
205 portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )
\r
207 portBASE_TYPE xReturn;
\r
209 /* There is only one port supported. */
\r
212 portENTER_CRITICAL();
\r
214 if( uxTxEmpty == pdTRUE )
\r
217 uxTxEmpty = pdFALSE;
\r
218 xReturn = ( portBASE_TYPE ) pdTRUE;
\r
222 xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );
\r
224 if( xReturn == ( portBASE_TYPE ) pdFALSE )
\r
226 xReturn = ( portBASE_TYPE ) pdTRUE;
\r
230 portEXIT_CRITICAL();
\r
234 /*-----------------------------------------------------------*/
\r
236 void vSerialClose( xComPortHandle xPort )
\r
238 /* Not implemented in this port. */
\r
241 /*-----------------------------------------------------------*/
\r