2 FreeRTOS V5.4.0 - Copyright (C) 2003-2009 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 it under
\r
7 the terms of the GNU General Public License (version 2) as published by the
\r
8 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 without being obliged to provide the
\r
11 source code for proprietary components outside of the FreeRTOS kernel.
\r
12 Alternative commercial license and support terms are also available upon
\r
13 request. See the licensing section of http://www.FreeRTOS.org for full
\r
16 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
21 You should have received a copy of the GNU General Public License along
\r
22 with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59
\r
23 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
26 ***************************************************************************
\r
28 * Looking for a quick start? Then check out the FreeRTOS eBook! *
\r
29 * See http://www.FreeRTOS.org/Documentation for details *
\r
31 ***************************************************************************
\r
35 Please ensure to read the configuration and relevant port sections of the
\r
36 online documentation.
\r
38 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
41 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
44 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
45 licensing and training services.
\r
51 + The function xPortInitMinimal() has been renamed to
\r
52 xSerialPortInitMinimal() and the function xPortInit() has been renamed
\r
53 to xSerialPortInit().
\r
57 + Delay periods are now specified using variables and constants of
\r
58 portTickType rather than unsigned portLONG.
\r
59 + xQueueReceiveFromISR() used in place of xQueueReceive() within the ISR.
\r
63 + Replaced the inb() and outb() functions with direct memory
\r
64 access. This allows the port to be built with the 20050414 build of
\r
68 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER. */
\r
72 #include <avr/interrupt.h>
\r
73 #include "FreeRTOS.h"
\r
78 #define serBAUD_DIV_CONSTANT ( ( unsigned portLONG ) 16 )
\r
80 /* Constants for writing to UCSRB. */
\r
81 #define serRX_INT_ENABLE ( ( unsigned portCHAR ) 0x80 )
\r
82 #define serRX_ENABLE ( ( unsigned portCHAR ) 0x10 )
\r
83 #define serTX_ENABLE ( ( unsigned portCHAR ) 0x08 )
\r
84 #define serTX_INT_ENABLE ( ( unsigned portCHAR ) 0x20 )
\r
86 /* Constants for writing to UCSRC. */
\r
87 #define serUCSRC_SELECT ( ( unsigned portCHAR ) 0x80 )
\r
88 #define serEIGHT_DATA_BITS ( ( unsigned portCHAR ) 0x06 )
\r
90 static xQueueHandle xRxedChars;
\r
91 static xQueueHandle xCharsForTx;
\r
93 #define vInterruptOn() \
\r
95 unsigned portCHAR ucByte; \
\r
98 ucByte |= serTX_INT_ENABLE; \
\r
101 /*-----------------------------------------------------------*/
\r
103 #define vInterruptOff() \
\r
105 unsigned portCHAR ucInByte; \
\r
107 ucInByte = UCSRB; \
\r
108 ucInByte &= ~serTX_INT_ENABLE; \
\r
109 UCSRB = ucInByte; \
\r
111 /*-----------------------------------------------------------*/
\r
113 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
115 unsigned portLONG ulBaudRateCounter;
\r
116 unsigned portCHAR ucByte;
\r
118 portENTER_CRITICAL();
\r
120 /* Create the queues used by the com test task. */
\r
121 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
\r
122 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
\r
124 /* Calculate the baud rate register value from the equation in the
\r
126 ulBaudRateCounter = ( configCPU_CLOCK_HZ / ( serBAUD_DIV_CONSTANT * ulWantedBaud ) ) - ( unsigned portLONG ) 1;
\r
128 /* Set the baud rate. */
\r
129 ucByte = ( unsigned portCHAR ) ( ulBaudRateCounter & ( unsigned portLONG ) 0xff );
\r
132 ulBaudRateCounter >>= ( unsigned portLONG ) 8;
\r
133 ucByte = ( unsigned portCHAR ) ( ulBaudRateCounter & ( unsigned portLONG ) 0xff );
\r
136 /* Enable the Rx interrupt. The Tx interrupt will get enabled
\r
137 later. Also enable the Rx and Tx. */
\r
138 UCSRB = ( serRX_INT_ENABLE | serRX_ENABLE | serTX_ENABLE );
\r
140 /* Set the data bits to 8. */
\r
141 UCSRC = ( serUCSRC_SELECT | serEIGHT_DATA_BITS );
\r
143 portEXIT_CRITICAL();
\r
145 /* Unlike other ports, this serial code does not allow for more than one
\r
146 com port. We therefore don't return a pointer to a port structure and can
\r
147 instead just return NULL. */
\r
150 /*-----------------------------------------------------------*/
\r
152 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )
\r
154 /* Only one port is supported. */
\r
157 /* Get the next character from the buffer. Return false if no characters
\r
158 are available, or arrive before xBlockTime expires. */
\r
159 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
168 /*-----------------------------------------------------------*/
\r
170 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )
\r
172 /* Only one port is supported. */
\r
175 /* Return false if after the block time there is no room on the Tx queue. */
\r
176 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
185 /*-----------------------------------------------------------*/
\r
187 void vSerialClose( xComPortHandle xPort )
\r
189 unsigned portCHAR ucByte;
\r
191 /* The parameter is not used. */
\r
194 /* Turn off the interrupts. We may also want to delete the queues and/or
\r
195 re-install the original ISR. */
\r
197 portENTER_CRITICAL();
\r
201 ucByte &= ~serRX_INT_ENABLE;
\r
204 portEXIT_CRITICAL();
\r
206 /*-----------------------------------------------------------*/
\r
208 SIGNAL( SIG_UART_RECV )
\r
210 signed portCHAR cChar;
\r
211 signed portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
213 /* Get the character and post it on the queue of Rxed characters.
\r
214 If the post causes a task to wake force a context switch as the woken task
\r
215 may have a higher priority than the task we have interrupted. */
\r
218 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
220 if( xHigherPriorityTaskWoken != pdFALSE )
\r
225 /*-----------------------------------------------------------*/
\r
227 SIGNAL( SIG_UART_DATA )
\r
229 signed portCHAR cChar, cTaskWoken;
\r
231 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &cTaskWoken ) == pdTRUE )
\r
233 /* Send the next character queued for Tx. */
\r
238 /* Queue empty, nothing to send. */
\r