2 FreeRTOS V8.2.2 - Copyright (C) 2015 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
71 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR IAR AVR PORT. */
\r
75 #include "FreeRTOS.h"
\r
80 #define serBAUD_DIV_CONSTANT ( ( unsigned long ) 16 )
\r
82 /* Constants for writing to UCSRB. */
\r
83 #define serRX_INT_ENABLE ( ( unsigned char ) 0x80 )
\r
84 #define serRX_ENABLE ( ( unsigned char ) 0x10 )
\r
85 #define serTX_ENABLE ( ( unsigned char ) 0x08 )
\r
86 #define serTX_INT_ENABLE ( ( unsigned char ) 0x20 )
\r
88 /* Constants for writing to UCSRC. */
\r
89 #define serUCSRC_SELECT ( ( unsigned char ) 0x80 )
\r
90 #define serEIGHT_DATA_BITS ( ( unsigned char ) 0x06 )
\r
92 static QueueHandle_t xRxedChars;
\r
93 static QueueHandle_t xCharsForTx;
\r
95 #define vInterruptOn() \
\r
97 unsigned char ucByte; \
\r
100 ucByte |= serTX_INT_ENABLE; \
\r
101 outb( UCSRB, ucByte ); \
\r
103 /*-----------------------------------------------------------*/
\r
105 #define vInterruptOff() \
\r
107 unsigned char ucByte; \
\r
110 ucByte &= ~serTX_INT_ENABLE; \
\r
111 outb( UCSRB, ucByte ); \
\r
113 /*-----------------------------------------------------------*/
\r
115 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
117 unsigned long ulBaudRateCounter;
\r
118 unsigned char ucByte;
\r
120 portENTER_CRITICAL();
\r
122 /* Create the queues used by the com test task. */
\r
123 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
124 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
126 /* Calculate the baud rate register value from the equation in the
\r
128 ulBaudRateCounter = ( configCPU_CLOCK_HZ / ( serBAUD_DIV_CONSTANT * ulWantedBaud ) ) - ( unsigned long ) 1;
\r
130 /* Set the baud rate. */
\r
131 ucByte = ( unsigned char ) ( ulBaudRateCounter & ( unsigned long ) 0xff );
\r
132 outb( UBRRL, ucByte );
\r
134 ulBaudRateCounter >>= ( unsigned long ) 8;
\r
135 ucByte = ( unsigned char ) ( ulBaudRateCounter & ( unsigned long ) 0xff );
\r
136 outb( UBRRH, ucByte );
\r
138 /* Enable the Rx interrupt. The Tx interrupt will get enabled
\r
139 later. Also enable the Rx and Tx. */
\r
140 outb( UCSRB, serRX_INT_ENABLE | serRX_ENABLE | serTX_ENABLE );
\r
142 /* Set the data bits to 8. */
\r
143 outb( UCSRC, serUCSRC_SELECT | serEIGHT_DATA_BITS );
\r
145 portEXIT_CRITICAL();
\r
147 /* Unlike other ports, this serial code does not allow for more than one
\r
148 com port. We therefore don't return a pointer to a port structure and can
\r
149 instead just return NULL. */
\r
152 /*-----------------------------------------------------------*/
\r
154 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
\r
156 /* Get the next character from the buffer. Return false if no characters
\r
157 are available, or arrive before xBlockTime expires. */
\r
158 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
167 /*-----------------------------------------------------------*/
\r
169 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
\r
171 /* Return false if after the block time there is no room on the Tx queue. */
\r
172 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
181 /*-----------------------------------------------------------*/
\r
183 void vSerialClose( xComPortHandle xPort )
\r
185 unsigned char ucByte;
\r
187 /* Turn off the interrupts. We may also want to delete the queues and/or
\r
188 re-install the original ISR. */
\r
190 portENTER_CRITICAL();
\r
194 ucByte &= ~serRX_INT_ENABLE;
\r
195 outb( UCSRB, ucByte );
\r
197 portEXIT_CRITICAL();
\r
199 /*-----------------------------------------------------------*/
\r
201 __interrupt void SIG_UART_RECV( void )
\r
203 signed char ucChar, xHigherPriorityTaskWoken = pdFALSE;
\r
205 /* Get the character and post it on the queue of Rxed characters.
\r
206 If the post causes a task to wake force a context switch as the woken task
\r
207 may have a higher priority than the task we have interrupted. */
\r
210 xQueueSendFromISR( xRxedChars, &ucChar, &xHigherPriorityTaskWoken );
\r
212 if( xHigherPriorityTaskWoken != pdFALSE )
\r
217 /*-----------------------------------------------------------*/
\r
219 __interrupt void SIG_UART_DATA( void )
\r
221 signed char cChar, cTaskWoken = pdFALSE;
\r
223 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &cTaskWoken ) == pdTRUE )
\r
225 /* Send the next character queued for Tx. */
\r
226 outb( UDR, cChar );
\r
230 /* Queue empty, nothing to send. */
\r