2 FreeRTOS.org V4.0.5 - Copyright (C) 2003-2006 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
30 ***************************************************************************
\r
34 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR IAR AVR PORT. */
\r
38 #include "FreeRTOS.h"
\r
43 #define serBAUD_DIV_CONSTANT ( ( unsigned portLONG ) 16 )
\r
45 /* Constants for writing to UCSRB. */
\r
46 #define serRX_INT_ENABLE ( ( unsigned portCHAR ) 0x80 )
\r
47 #define serRX_ENABLE ( ( unsigned portCHAR ) 0x10 )
\r
48 #define serTX_ENABLE ( ( unsigned portCHAR ) 0x08 )
\r
49 #define serTX_INT_ENABLE ( ( unsigned portCHAR ) 0x20 )
\r
51 /* Constants for writing to UCSRC. */
\r
52 #define serUCSRC_SELECT ( ( unsigned portCHAR ) 0x80 )
\r
53 #define serEIGHT_DATA_BITS ( ( unsigned portCHAR ) 0x06 )
\r
55 static xQueueHandle xRxedChars;
\r
56 static xQueueHandle xCharsForTx;
\r
58 #define vInterruptOn() \
\r
60 unsigned portCHAR ucByte; \
\r
63 ucByte |= serTX_INT_ENABLE; \
\r
64 outb( UCSRB, ucByte ); \
\r
66 /*-----------------------------------------------------------*/
\r
68 #define vInterruptOff() \
\r
70 unsigned portCHAR ucByte; \
\r
73 ucByte &= ~serTX_INT_ENABLE; \
\r
74 outb( UCSRB, ucByte ); \
\r
76 /*-----------------------------------------------------------*/
\r
78 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
80 unsigned portLONG ulBaudRateCounter;
\r
81 unsigned portCHAR ucByte;
\r
83 portENTER_CRITICAL();
\r
85 /* Create the queues used by the com test task. */
\r
86 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
\r
87 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
\r
89 /* Calculate the baud rate register value from the equation in the
\r
91 ulBaudRateCounter = ( configCPU_CLOCK_HZ / ( serBAUD_DIV_CONSTANT * ulWantedBaud ) ) - ( unsigned portLONG ) 1;
\r
93 /* Set the baud rate. */
\r
94 ucByte = ( unsigned portCHAR ) ( ulBaudRateCounter & ( unsigned portLONG ) 0xff );
\r
95 outb( UBRRL, ucByte );
\r
97 ulBaudRateCounter >>= ( unsigned portLONG ) 8;
\r
98 ucByte = ( unsigned portCHAR ) ( ulBaudRateCounter & ( unsigned portLONG ) 0xff );
\r
99 outb( UBRRH, ucByte );
\r
101 /* Enable the Rx interrupt. The Tx interrupt will get enabled
\r
102 later. Also enable the Rx and Tx. */
\r
103 outb( UCSRB, serRX_INT_ENABLE | serRX_ENABLE | serTX_ENABLE );
\r
105 /* Set the data bits to 8. */
\r
106 outb( UCSRC, serUCSRC_SELECT | serEIGHT_DATA_BITS );
\r
108 portEXIT_CRITICAL();
\r
110 /* Unlike other ports, this serial code does not allow for more than one
\r
111 com port. We therefore don't return a pointer to a port structure and can
\r
112 instead just return NULL. */
\r
115 /*-----------------------------------------------------------*/
\r
117 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )
\r
119 /* Get the next character from the buffer. Return false if no characters
\r
120 are available, or arrive before xBlockTime expires. */
\r
121 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
130 /*-----------------------------------------------------------*/
\r
132 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )
\r
134 /* Return false if after the block time there is no room on the Tx queue. */
\r
135 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
144 /*-----------------------------------------------------------*/
\r
146 void vSerialClose( xComPortHandle xPort )
\r
148 unsigned portCHAR ucByte;
\r
150 /* Turn off the interrupts. We may also want to delete the queues and/or
\r
151 re-install the original ISR. */
\r
153 portENTER_CRITICAL();
\r
157 ucByte &= ~serRX_INT_ENABLE;
\r
158 outb( UCSRB, ucByte );
\r
160 portEXIT_CRITICAL();
\r
162 /*-----------------------------------------------------------*/
\r
164 __interrupt void SIG_UART_RECV( void )
\r
166 signed portCHAR cChar;
\r
168 /* Get the character and post it on the queue of Rxed characters.
\r
169 If the post causes a task to wake force a context switch as the woken task
\r
170 may have a higher priority than the task we have interrupted. */
\r
173 if( xQueueSendFromISR( xRxedChars, &cChar, pdFALSE ) )
\r
178 /*-----------------------------------------------------------*/
\r
180 __interrupt void SIG_UART_DATA( void )
\r
182 signed portCHAR cChar, cTaskWoken;
\r
184 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &cTaskWoken ) == pdTRUE )
\r
186 /* Send the next character queued for Tx. */
\r
187 outb( UDR, cChar );
\r
191 /* Queue empty, nothing to send. */
\r