2 FreeRTOS V5.4.2 - Copyright (C) 2009 Real Time Engineers Ltd.
\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
49 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR IAR AVR PORT. */
\r
53 #include "FreeRTOS.h"
\r
58 #define serBAUD_DIV_CONSTANT ( ( unsigned portLONG ) 16 )
\r
60 /* Constants for writing to UCSRB. */
\r
61 #define serRX_INT_ENABLE ( ( unsigned portCHAR ) 0x80 )
\r
62 #define serRX_ENABLE ( ( unsigned portCHAR ) 0x10 )
\r
63 #define serTX_ENABLE ( ( unsigned portCHAR ) 0x08 )
\r
64 #define serTX_INT_ENABLE ( ( unsigned portCHAR ) 0x20 )
\r
66 /* Constants for writing to UCSRC. */
\r
67 #define serUCSRC_SELECT ( ( unsigned portCHAR ) 0x80 )
\r
68 #define serEIGHT_DATA_BITS ( ( unsigned portCHAR ) 0x06 )
\r
70 static xQueueHandle xRxedChars;
\r
71 static xQueueHandle xCharsForTx;
\r
73 #define vInterruptOn() \
\r
75 unsigned portCHAR ucByte; \
\r
78 ucByte |= serTX_INT_ENABLE; \
\r
79 outb( UCSRB, ucByte ); \
\r
81 /*-----------------------------------------------------------*/
\r
83 #define vInterruptOff() \
\r
85 unsigned portCHAR ucByte; \
\r
88 ucByte &= ~serTX_INT_ENABLE; \
\r
89 outb( UCSRB, ucByte ); \
\r
91 /*-----------------------------------------------------------*/
\r
93 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
95 unsigned portLONG ulBaudRateCounter;
\r
96 unsigned portCHAR ucByte;
\r
98 portENTER_CRITICAL();
\r
100 /* Create the queues used by the com test task. */
\r
101 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
\r
102 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
\r
104 /* Calculate the baud rate register value from the equation in the
\r
106 ulBaudRateCounter = ( configCPU_CLOCK_HZ / ( serBAUD_DIV_CONSTANT * ulWantedBaud ) ) - ( unsigned portLONG ) 1;
\r
108 /* Set the baud rate. */
\r
109 ucByte = ( unsigned portCHAR ) ( ulBaudRateCounter & ( unsigned portLONG ) 0xff );
\r
110 outb( UBRRL, ucByte );
\r
112 ulBaudRateCounter >>= ( unsigned portLONG ) 8;
\r
113 ucByte = ( unsigned portCHAR ) ( ulBaudRateCounter & ( unsigned portLONG ) 0xff );
\r
114 outb( UBRRH, ucByte );
\r
116 /* Enable the Rx interrupt. The Tx interrupt will get enabled
\r
117 later. Also enable the Rx and Tx. */
\r
118 outb( UCSRB, serRX_INT_ENABLE | serRX_ENABLE | serTX_ENABLE );
\r
120 /* Set the data bits to 8. */
\r
121 outb( UCSRC, serUCSRC_SELECT | serEIGHT_DATA_BITS );
\r
123 portEXIT_CRITICAL();
\r
125 /* Unlike other ports, this serial code does not allow for more than one
\r
126 com port. We therefore don't return a pointer to a port structure and can
\r
127 instead just return NULL. */
\r
130 /*-----------------------------------------------------------*/
\r
132 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )
\r
134 /* Get the next character from the buffer. Return false if no characters
\r
135 are available, or arrive before xBlockTime expires. */
\r
136 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
145 /*-----------------------------------------------------------*/
\r
147 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )
\r
149 /* Return false if after the block time there is no room on the Tx queue. */
\r
150 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
159 /*-----------------------------------------------------------*/
\r
161 void vSerialClose( xComPortHandle xPort )
\r
163 unsigned portCHAR ucByte;
\r
165 /* Turn off the interrupts. We may also want to delete the queues and/or
\r
166 re-install the original ISR. */
\r
168 portENTER_CRITICAL();
\r
172 ucByte &= ~serRX_INT_ENABLE;
\r
173 outb( UCSRB, ucByte );
\r
175 portEXIT_CRITICAL();
\r
177 /*-----------------------------------------------------------*/
\r
179 __interrupt void SIG_UART_RECV( void )
\r
181 signed portCHAR ucChar, xHigherPriorityTaskWoken = pdFALSE;
\r
183 /* Get the character and post it on the queue of Rxed characters.
\r
184 If the post causes a task to wake force a context switch as the woken task
\r
185 may have a higher priority than the task we have interrupted. */
\r
188 xQueueSendFromISR( xRxedChars, &ucChar, &xHigherPriorityTaskWoken );
\r
190 if( xHigherPriorityTaskWoken != pdFALSE )
\r
195 /*-----------------------------------------------------------*/
\r
197 __interrupt void SIG_UART_DATA( void )
\r
199 signed portCHAR cChar, cTaskWoken = pdFALSE;
\r
201 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &cTaskWoken ) == pdTRUE )
\r
203 /* Send the next character queued for Tx. */
\r
204 outb( UDR, cChar );
\r
208 /* Queue empty, nothing to send. */
\r