2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
32 + The function xPortInitMinimal() has been renamed to
\r
33 xSerialPortInitMinimal() and the function xPortInit() has been renamed
\r
34 to xSerialPortInit().
\r
38 + Delay periods are now specified using variables and constants of
\r
39 TickType_t rather than unsigned long.
\r
40 + xQueueReceiveFromISR() used in place of xQueueReceive() within the ISR.
\r
44 + Replaced the inb() and outb() functions with direct memory
\r
45 access. This allows the port to be built with the 20050414 build of
\r
49 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER. */
\r
53 #include <avr/interrupt.h>
\r
54 #include "FreeRTOS.h"
\r
59 #define serBAUD_DIV_CONSTANT ( ( unsigned long ) 16 )
\r
61 /* Constants for writing to UCSRB. */
\r
62 #define serRX_INT_ENABLE ( ( unsigned char ) 0x80 )
\r
63 #define serRX_ENABLE ( ( unsigned char ) 0x10 )
\r
64 #define serTX_ENABLE ( ( unsigned char ) 0x08 )
\r
65 #define serTX_INT_ENABLE ( ( unsigned char ) 0x20 )
\r
67 /* Constants for writing to UCSRC. */
\r
68 #define serUCSRC_SELECT ( ( unsigned char ) 0x80 )
\r
69 #define serEIGHT_DATA_BITS ( ( unsigned char ) 0x06 )
\r
71 static QueueHandle_t xRxedChars;
\r
72 static QueueHandle_t xCharsForTx;
\r
74 #define vInterruptOn() \
\r
76 unsigned char ucByte; \
\r
79 ucByte |= serTX_INT_ENABLE; \
\r
82 /*-----------------------------------------------------------*/
\r
84 #define vInterruptOff() \
\r
86 unsigned char ucInByte; \
\r
89 ucInByte &= ~serTX_INT_ENABLE; \
\r
92 /*-----------------------------------------------------------*/
\r
94 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
96 unsigned long ulBaudRateCounter;
\r
97 unsigned char ucByte;
\r
99 portENTER_CRITICAL();
\r
101 /* Create the queues used by the com test task. */
\r
102 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
103 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
105 /* Calculate the baud rate register value from the equation in the
\r
107 ulBaudRateCounter = ( configCPU_CLOCK_HZ / ( serBAUD_DIV_CONSTANT * ulWantedBaud ) ) - ( unsigned long ) 1;
\r
109 /* Set the baud rate. */
\r
110 ucByte = ( unsigned char ) ( ulBaudRateCounter & ( unsigned long ) 0xff );
\r
113 ulBaudRateCounter >>= ( unsigned long ) 8;
\r
114 ucByte = ( unsigned char ) ( ulBaudRateCounter & ( unsigned long ) 0xff );
\r
117 /* Enable the Rx interrupt. The Tx interrupt will get enabled
\r
118 later. Also enable the Rx and Tx. */
\r
119 UCSRB = ( serRX_INT_ENABLE | serRX_ENABLE | serTX_ENABLE );
\r
121 /* Set the data bits to 8. */
\r
122 UCSRC = ( serUCSRC_SELECT | serEIGHT_DATA_BITS );
\r
124 portEXIT_CRITICAL();
\r
126 /* Unlike other ports, this serial code does not allow for more than one
\r
127 com port. We therefore don't return a pointer to a port structure and can
\r
128 instead just return NULL. */
\r
131 /*-----------------------------------------------------------*/
\r
133 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
\r
135 /* Only one port is supported. */
\r
138 /* Get the next character from the buffer. Return false if no characters
\r
139 are available, or arrive before xBlockTime expires. */
\r
140 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
149 /*-----------------------------------------------------------*/
\r
151 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
\r
153 /* Only one port is supported. */
\r
156 /* Return false if after the block time there is no room on the Tx queue. */
\r
157 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
166 /*-----------------------------------------------------------*/
\r
168 void vSerialClose( xComPortHandle xPort )
\r
170 unsigned char ucByte;
\r
172 /* The parameter is not used. */
\r
175 /* Turn off the interrupts. We may also want to delete the queues and/or
\r
176 re-install the original ISR. */
\r
178 portENTER_CRITICAL();
\r
182 ucByte &= ~serRX_INT_ENABLE;
\r
185 portEXIT_CRITICAL();
\r
187 /*-----------------------------------------------------------*/
\r
189 SIGNAL( SIG_UART_RECV )
\r
192 signed portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
194 /* Get the character and post it on the queue of Rxed characters.
\r
195 If the post causes a task to wake force a context switch as the woken task
\r
196 may have a higher priority than the task we have interrupted. */
\r
199 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
201 if( xHigherPriorityTaskWoken != pdFALSE )
\r
206 /*-----------------------------------------------------------*/
\r
208 SIGNAL( SIG_UART_DATA )
\r
210 signed char cChar, cTaskWoken;
\r
212 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &cTaskWoken ) == pdTRUE )
\r
214 /* Send the next character queued for Tx. */
\r
219 /* Queue empty, nothing to send. */
\r