2 FreeRTOS V6.0.0 - Copyright (C) 2009 Real Time Engineers Ltd.
\r
4 ***************************************************************************
\r
8 * + New to FreeRTOS, *
\r
9 * + Wanting to learn FreeRTOS or multitasking in general quickly *
\r
10 * + Looking for basic training, *
\r
11 * + Wanting to improve your FreeRTOS skills and productivity *
\r
13 * then take a look at the FreeRTOS eBook *
\r
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
\r
16 * http://www.FreeRTOS.org/Documentation *
\r
18 * A pdf reference manual is also available. Both are usually delivered *
\r
19 * to your inbox within 20 minutes to two hours when purchased between 8am *
\r
20 * and 8pm GMT (although please allow up to 24 hours in case of *
\r
21 * exceptional circumstances). Thank you for your support! *
\r
23 ***************************************************************************
\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 exception to the GPL is included to allow you to distribute
\r
31 a combined work that includes FreeRTOS without being obliged to provide the
\r
32 source code for proprietary components outside of the FreeRTOS kernel.
\r
33 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
34 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
35 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 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
55 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR IAR AVR PORT. */
\r
59 #include "FreeRTOS.h"
\r
64 #define serBAUD_DIV_CONSTANT ( ( unsigned long ) 16 )
\r
66 /* Constants for writing to UCSRB. */
\r
67 #define serRX_INT_ENABLE ( ( unsigned char ) 0x80 )
\r
68 #define serRX_ENABLE ( ( unsigned char ) 0x10 )
\r
69 #define serTX_ENABLE ( ( unsigned char ) 0x08 )
\r
70 #define serTX_INT_ENABLE ( ( unsigned char ) 0x20 )
\r
72 /* Constants for writing to UCSRC. */
\r
73 #define serUCSRC_SELECT ( ( unsigned char ) 0x80 )
\r
74 #define serEIGHT_DATA_BITS ( ( unsigned char ) 0x06 )
\r
76 static xQueueHandle xRxedChars;
\r
77 static xQueueHandle xCharsForTx;
\r
79 #define vInterruptOn() \
\r
81 unsigned char ucByte; \
\r
84 ucByte |= serTX_INT_ENABLE; \
\r
85 outb( UCSRB, ucByte ); \
\r
87 /*-----------------------------------------------------------*/
\r
89 #define vInterruptOff() \
\r
91 unsigned char ucByte; \
\r
94 ucByte &= ~serTX_INT_ENABLE; \
\r
95 outb( UCSRB, ucByte ); \
\r
97 /*-----------------------------------------------------------*/
\r
99 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
101 unsigned long ulBaudRateCounter;
\r
102 unsigned char ucByte;
\r
104 portENTER_CRITICAL();
\r
106 /* Create the queues used by the com test task. */
\r
107 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
108 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
110 /* Calculate the baud rate register value from the equation in the
\r
112 ulBaudRateCounter = ( configCPU_CLOCK_HZ / ( serBAUD_DIV_CONSTANT * ulWantedBaud ) ) - ( unsigned long ) 1;
\r
114 /* Set the baud rate. */
\r
115 ucByte = ( unsigned char ) ( ulBaudRateCounter & ( unsigned long ) 0xff );
\r
116 outb( UBRRL, ucByte );
\r
118 ulBaudRateCounter >>= ( unsigned long ) 8;
\r
119 ucByte = ( unsigned char ) ( ulBaudRateCounter & ( unsigned long ) 0xff );
\r
120 outb( UBRRH, ucByte );
\r
122 /* Enable the Rx interrupt. The Tx interrupt will get enabled
\r
123 later. Also enable the Rx and Tx. */
\r
124 outb( UCSRB, serRX_INT_ENABLE | serRX_ENABLE | serTX_ENABLE );
\r
126 /* Set the data bits to 8. */
\r
127 outb( UCSRC, serUCSRC_SELECT | serEIGHT_DATA_BITS );
\r
129 portEXIT_CRITICAL();
\r
131 /* Unlike other ports, this serial code does not allow for more than one
\r
132 com port. We therefore don't return a pointer to a port structure and can
\r
133 instead just return NULL. */
\r
136 /*-----------------------------------------------------------*/
\r
138 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )
\r
140 /* Get the next character from the buffer. Return false if no characters
\r
141 are available, or arrive before xBlockTime expires. */
\r
142 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
151 /*-----------------------------------------------------------*/
\r
153 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )
\r
155 /* Return false if after the block time there is no room on the Tx queue. */
\r
156 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
165 /*-----------------------------------------------------------*/
\r
167 void vSerialClose( xComPortHandle xPort )
\r
169 unsigned char ucByte;
\r
171 /* Turn off the interrupts. We may also want to delete the queues and/or
\r
172 re-install the original ISR. */
\r
174 portENTER_CRITICAL();
\r
178 ucByte &= ~serRX_INT_ENABLE;
\r
179 outb( UCSRB, ucByte );
\r
181 portEXIT_CRITICAL();
\r
183 /*-----------------------------------------------------------*/
\r
185 __interrupt void SIG_UART_RECV( void )
\r
187 signed char ucChar, xHigherPriorityTaskWoken = pdFALSE;
\r
189 /* Get the character and post it on the queue of Rxed characters.
\r
190 If the post causes a task to wake force a context switch as the woken task
\r
191 may have a higher priority than the task we have interrupted. */
\r
194 xQueueSendFromISR( xRxedChars, &ucChar, &xHigherPriorityTaskWoken );
\r
196 if( xHigherPriorityTaskWoken != pdFALSE )
\r
201 /*-----------------------------------------------------------*/
\r
203 __interrupt void SIG_UART_DATA( void )
\r
205 signed char cChar, cTaskWoken = pdFALSE;
\r
207 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &cTaskWoken ) == pdTRUE )
\r
209 /* Send the next character queued for Tx. */
\r
210 outb( UDR, cChar );
\r
214 /* Queue empty, nothing to send. */
\r