2 * FreeRTOS Kernel V10.1.0
\r
3 * Copyright (C) 2018 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.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
31 + The function xPortInitMinimal() has been renamed to
\r
32 xSerialPortInitMinimal() and the function xPortInit() has been renamed
\r
33 to xSerialPortInit().
\r
37 + Delay periods are now specified using variables and constants of
\r
38 TickType_t rather than unsigned long.
\r
39 + xQueueReceiveFromISR() used in place of xQueueReceive() within the ISR.
\r
43 + Replaced the inb() and outb() functions with direct memory
\r
44 access. This allows the port to be built with the 20050414 build of
\r
48 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER. */
\r
52 #include <avr/interrupt.h>
\r
53 #include "FreeRTOS.h"
\r
58 #define serBAUD_DIV_CONSTANT ( ( unsigned long ) 16 )
\r
60 /* Constants for writing to UCSRB. */
\r
61 #define serRX_INT_ENABLE ( ( unsigned char ) 0x80 )
\r
62 #define serRX_ENABLE ( ( unsigned char ) 0x10 )
\r
63 #define serTX_ENABLE ( ( unsigned char ) 0x08 )
\r
64 #define serTX_INT_ENABLE ( ( unsigned char ) 0x20 )
\r
66 /* Constants for writing to UCSRC. */
\r
67 #define serUCSRC_SELECT ( ( unsigned char ) 0x80 )
\r
68 #define serEIGHT_DATA_BITS ( ( unsigned char ) 0x06 )
\r
70 static QueueHandle_t xRxedChars;
\r
71 static QueueHandle_t xCharsForTx;
\r
73 #define vInterruptOn() \
\r
75 unsigned char ucByte; \
\r
78 ucByte |= serTX_INT_ENABLE; \
\r
81 /*-----------------------------------------------------------*/
\r
83 #define vInterruptOff() \
\r
85 unsigned char ucInByte; \
\r
88 ucInByte &= ~serTX_INT_ENABLE; \
\r
91 /*-----------------------------------------------------------*/
\r
93 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
95 unsigned long ulBaudRateCounter;
\r
96 unsigned char 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 char ) );
\r
102 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
104 /* Calculate the baud rate register value from the equation in the
\r
106 ulBaudRateCounter = ( configCPU_CLOCK_HZ / ( serBAUD_DIV_CONSTANT * ulWantedBaud ) ) - ( unsigned long ) 1;
\r
108 /* Set the baud rate. */
\r
109 ucByte = ( unsigned char ) ( ulBaudRateCounter & ( unsigned long ) 0xff );
\r
112 ulBaudRateCounter >>= ( unsigned long ) 8;
\r
113 ucByte = ( unsigned char ) ( ulBaudRateCounter & ( unsigned long ) 0xff );
\r
116 /* Enable the Rx interrupt. The Tx interrupt will get enabled
\r
117 later. Also enable the Rx and Tx. */
\r
118 UCSRB = ( serRX_INT_ENABLE | serRX_ENABLE | serTX_ENABLE );
\r
120 /* Set the data bits to 8. */
\r
121 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 char *pcRxedChar, TickType_t xBlockTime )
\r
134 /* Only one port is supported. */
\r
137 /* Get the next character from the buffer. Return false if no characters
\r
138 are available, or arrive before xBlockTime expires. */
\r
139 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
148 /*-----------------------------------------------------------*/
\r
150 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
\r
152 /* Only one port is supported. */
\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 /* The parameter is not used. */
\r
174 /* Turn off the interrupts. We may also want to delete the queues and/or
\r
175 re-install the original ISR. */
\r
177 portENTER_CRITICAL();
\r
181 ucByte &= ~serRX_INT_ENABLE;
\r
184 portEXIT_CRITICAL();
\r
186 /*-----------------------------------------------------------*/
\r
188 SIGNAL( SIG_UART_RECV )
\r
191 signed portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
193 /* Get the character and post it on the queue of Rxed characters.
\r
194 If the post causes a task to wake force a context switch as the woken task
\r
195 may have a higher priority than the task we have interrupted. */
\r
198 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
200 if( xHigherPriorityTaskWoken != pdFALSE )
\r
205 /*-----------------------------------------------------------*/
\r
207 SIGNAL( SIG_UART_DATA )
\r
209 signed char cChar, cTaskWoken;
\r
211 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &cTaskWoken ) == pdTRUE )
\r
213 /* Send the next character queued for Tx. */
\r
218 /* Queue empty, nothing to send. */
\r