2 FreeRTOS.org V5.1.2 - 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
\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 ***************************************************************************
\r
29 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
31 * This is a concise, step by step, 'hands on' guide that describes both *
\r
32 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
33 * explains numerous examples that are written using the FreeRTOS API. *
\r
34 * Full source code for all the examples is provided in an accompanying *
\r
37 ***************************************************************************
\r
38 ***************************************************************************
\r
40 Please ensure to read the configuration and relevant port sections of the
\r
41 online documentation.
\r
43 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
46 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
49 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
50 licensing and training services.
\r
56 + The function xPortInitMinimal() has been renamed to
\r
57 xSerialPortInitMinimal() and the function xPortInit() has been renamed
\r
58 to xSerialPortInit().
\r
62 + Delay periods are now specified using variables and constants of
\r
63 portTickType rather than unsigned portLONG.
\r
64 + xQueueReceiveFromISR() used in place of xQueueReceive() within the ISR.
\r
68 + Replaced the inb() and outb() functions with direct memory
\r
69 access. This allows the port to be built with the 20050414 build of
\r
73 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER. */
\r
77 #include <avr/interrupt.h>
\r
78 #include "FreeRTOS.h"
\r
83 #define serBAUD_DIV_CONSTANT ( ( unsigned portLONG ) 16 )
\r
85 /* Constants for writing to UCSRB. */
\r
86 #define serRX_INT_ENABLE ( ( unsigned portCHAR ) 0x80 )
\r
87 #define serRX_ENABLE ( ( unsigned portCHAR ) 0x10 )
\r
88 #define serTX_ENABLE ( ( unsigned portCHAR ) 0x08 )
\r
89 #define serTX_INT_ENABLE ( ( unsigned portCHAR ) 0x20 )
\r
91 /* Constants for writing to UCSRC. */
\r
92 #define serUCSRC_SELECT ( ( unsigned portCHAR ) 0x80 )
\r
93 #define serEIGHT_DATA_BITS ( ( unsigned portCHAR ) 0x06 )
\r
95 static xQueueHandle xRxedChars;
\r
96 static xQueueHandle xCharsForTx;
\r
98 #define vInterruptOn() \
\r
100 unsigned portCHAR ucByte; \
\r
103 ucByte |= serTX_INT_ENABLE; \
\r
106 /*-----------------------------------------------------------*/
\r
108 #define vInterruptOff() \
\r
110 unsigned portCHAR ucInByte; \
\r
112 ucInByte = UCSRB; \
\r
113 ucInByte &= ~serTX_INT_ENABLE; \
\r
114 UCSRB = ucInByte; \
\r
116 /*-----------------------------------------------------------*/
\r
118 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
120 unsigned portLONG ulBaudRateCounter;
\r
121 unsigned portCHAR ucByte;
\r
123 portENTER_CRITICAL();
\r
125 /* Create the queues used by the com test task. */
\r
126 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
\r
127 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
\r
129 /* Calculate the baud rate register value from the equation in the
\r
131 ulBaudRateCounter = ( configCPU_CLOCK_HZ / ( serBAUD_DIV_CONSTANT * ulWantedBaud ) ) - ( unsigned portLONG ) 1;
\r
133 /* Set the baud rate. */
\r
134 ucByte = ( unsigned portCHAR ) ( ulBaudRateCounter & ( unsigned portLONG ) 0xff );
\r
137 ulBaudRateCounter >>= ( unsigned portLONG ) 8;
\r
138 ucByte = ( unsigned portCHAR ) ( ulBaudRateCounter & ( unsigned portLONG ) 0xff );
\r
141 /* Enable the Rx interrupt. The Tx interrupt will get enabled
\r
142 later. Also enable the Rx and Tx. */
\r
143 UCSRB = ( serRX_INT_ENABLE | serRX_ENABLE | serTX_ENABLE );
\r
145 /* Set the data bits to 8. */
\r
146 UCSRC = ( serUCSRC_SELECT | serEIGHT_DATA_BITS );
\r
148 portEXIT_CRITICAL();
\r
150 /* Unlike other ports, this serial code does not allow for more than one
\r
151 com port. We therefore don't return a pointer to a port structure and can
\r
152 instead just return NULL. */
\r
155 /*-----------------------------------------------------------*/
\r
157 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )
\r
159 /* Only one port is supported. */
\r
162 /* Get the next character from the buffer. Return false if no characters
\r
163 are available, or arrive before xBlockTime expires. */
\r
164 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
173 /*-----------------------------------------------------------*/
\r
175 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )
\r
177 /* Only one port is supported. */
\r
180 /* Return false if after the block time there is no room on the Tx queue. */
\r
181 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
190 /*-----------------------------------------------------------*/
\r
192 void vSerialClose( xComPortHandle xPort )
\r
194 unsigned portCHAR ucByte;
\r
196 /* The parameter is not used. */
\r
199 /* Turn off the interrupts. We may also want to delete the queues and/or
\r
200 re-install the original ISR. */
\r
202 portENTER_CRITICAL();
\r
206 ucByte &= ~serRX_INT_ENABLE;
\r
209 portEXIT_CRITICAL();
\r
211 /*-----------------------------------------------------------*/
\r
213 SIGNAL( SIG_UART_RECV )
\r
215 signed portCHAR cChar;
\r
216 signed portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
218 /* Get the character and post it on the queue of Rxed characters.
\r
219 If the post causes a task to wake force a context switch as the woken task
\r
220 may have a higher priority than the task we have interrupted. */
\r
223 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
225 if( xHigherPriorityTaskWoken != pdFALSE )
\r
230 /*-----------------------------------------------------------*/
\r
232 SIGNAL( SIG_UART_DATA )
\r
234 signed portCHAR cChar, cTaskWoken;
\r
236 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &cTaskWoken ) == pdTRUE )
\r
238 /* Send the next character queued for Tx. */
\r
243 /* Queue empty, nothing to send. */
\r