2 FreeRTOS V8.2.3 - 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
73 + The function xPortInitMinimal() has been renamed to
\r
74 xSerialPortInitMinimal() and the function xPortInit() has been renamed
\r
75 to xSerialPortInit().
\r
79 + Delay periods are now specified using variables and constants of
\r
80 TickType_t rather than unsigned long.
\r
81 + xQueueReceiveFromISR() used in place of xQueueReceive() within the ISR.
\r
85 + Replaced the inb() and outb() functions with direct memory
\r
86 access. This allows the port to be built with the 20050414 build of
\r
90 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER. */
\r
94 #include <avr/interrupt.h>
\r
95 #include "FreeRTOS.h"
\r
100 #define serBAUD_DIV_CONSTANT ( ( unsigned long ) 16 )
\r
102 /* Constants for writing to UCSRB. */
\r
103 #define serRX_INT_ENABLE ( ( unsigned char ) 0x80 )
\r
104 #define serRX_ENABLE ( ( unsigned char ) 0x10 )
\r
105 #define serTX_ENABLE ( ( unsigned char ) 0x08 )
\r
106 #define serTX_INT_ENABLE ( ( unsigned char ) 0x20 )
\r
108 /* Constants for writing to UCSRC. */
\r
109 #define serUCSRC_SELECT ( ( unsigned char ) 0x80 )
\r
110 #define serEIGHT_DATA_BITS ( ( unsigned char ) 0x06 )
\r
112 static QueueHandle_t xRxedChars;
\r
113 static QueueHandle_t xCharsForTx;
\r
115 #define vInterruptOn() \
\r
117 unsigned char ucByte; \
\r
120 ucByte |= serTX_INT_ENABLE; \
\r
123 /*-----------------------------------------------------------*/
\r
125 #define vInterruptOff() \
\r
127 unsigned char ucInByte; \
\r
129 ucInByte = UCSRB; \
\r
130 ucInByte &= ~serTX_INT_ENABLE; \
\r
131 UCSRB = ucInByte; \
\r
133 /*-----------------------------------------------------------*/
\r
135 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
137 unsigned long ulBaudRateCounter;
\r
138 unsigned char ucByte;
\r
140 portENTER_CRITICAL();
\r
142 /* Create the queues used by the com test task. */
\r
143 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
144 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
146 /* Calculate the baud rate register value from the equation in the
\r
148 ulBaudRateCounter = ( configCPU_CLOCK_HZ / ( serBAUD_DIV_CONSTANT * ulWantedBaud ) ) - ( unsigned long ) 1;
\r
150 /* Set the baud rate. */
\r
151 ucByte = ( unsigned char ) ( ulBaudRateCounter & ( unsigned long ) 0xff );
\r
154 ulBaudRateCounter >>= ( unsigned long ) 8;
\r
155 ucByte = ( unsigned char ) ( ulBaudRateCounter & ( unsigned long ) 0xff );
\r
158 /* Enable the Rx interrupt. The Tx interrupt will get enabled
\r
159 later. Also enable the Rx and Tx. */
\r
160 UCSRB = ( serRX_INT_ENABLE | serRX_ENABLE | serTX_ENABLE );
\r
162 /* Set the data bits to 8. */
\r
163 UCSRC = ( serUCSRC_SELECT | serEIGHT_DATA_BITS );
\r
165 portEXIT_CRITICAL();
\r
167 /* Unlike other ports, this serial code does not allow for more than one
\r
168 com port. We therefore don't return a pointer to a port structure and can
\r
169 instead just return NULL. */
\r
172 /*-----------------------------------------------------------*/
\r
174 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
\r
176 /* Only one port is supported. */
\r
179 /* Get the next character from the buffer. Return false if no characters
\r
180 are available, or arrive before xBlockTime expires. */
\r
181 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
190 /*-----------------------------------------------------------*/
\r
192 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
\r
194 /* Only one port is supported. */
\r
197 /* Return false if after the block time there is no room on the Tx queue. */
\r
198 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
207 /*-----------------------------------------------------------*/
\r
209 void vSerialClose( xComPortHandle xPort )
\r
211 unsigned char ucByte;
\r
213 /* The parameter is not used. */
\r
216 /* Turn off the interrupts. We may also want to delete the queues and/or
\r
217 re-install the original ISR. */
\r
219 portENTER_CRITICAL();
\r
223 ucByte &= ~serRX_INT_ENABLE;
\r
226 portEXIT_CRITICAL();
\r
228 /*-----------------------------------------------------------*/
\r
230 SIGNAL( SIG_UART_RECV )
\r
233 signed portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
235 /* Get the character and post it on the queue of Rxed characters.
\r
236 If the post causes a task to wake force a context switch as the woken task
\r
237 may have a higher priority than the task we have interrupted. */
\r
240 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
242 if( xHigherPriorityTaskWoken != pdFALSE )
\r
247 /*-----------------------------------------------------------*/
\r
249 SIGNAL( SIG_UART_DATA )
\r
251 signed char cChar, cTaskWoken;
\r
253 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &cTaskWoken ) == pdTRUE )
\r
255 /* Send the next character queued for Tx. */
\r
260 /* Queue empty, nothing to send. */
\r