3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * http://www.FreeRTOS.org
23 * http://aws.amazon.com/freertos
29 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR IAR AVR PORT. */
38 #define serBAUD_DIV_CONSTANT ( ( unsigned long ) 16 )
40 /* Constants for writing to UCSRB. */
41 #define serRX_INT_ENABLE ( ( unsigned char ) 0x80 )
42 #define serRX_ENABLE ( ( unsigned char ) 0x10 )
43 #define serTX_ENABLE ( ( unsigned char ) 0x08 )
44 #define serTX_INT_ENABLE ( ( unsigned char ) 0x20 )
46 /* Constants for writing to UCSRC. */
47 #define serUCSRC_SELECT ( ( unsigned char ) 0x80 )
48 #define serEIGHT_DATA_BITS ( ( unsigned char ) 0x06 )
50 static QueueHandle_t xRxedChars;
51 static QueueHandle_t xCharsForTx;
53 #define vInterruptOn() \
55 unsigned char ucByte; \
58 ucByte |= serTX_INT_ENABLE; \
59 outb( UCSRB, ucByte ); \
61 /*-----------------------------------------------------------*/
63 #define vInterruptOff() \
65 unsigned char ucByte; \
68 ucByte &= ~serTX_INT_ENABLE; \
69 outb( UCSRB, ucByte ); \
71 /*-----------------------------------------------------------*/
73 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
75 unsigned long ulBaudRateCounter;
80 /* Create the queues used by the com test task. */
81 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
82 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
84 /* Calculate the baud rate register value from the equation in the
86 ulBaudRateCounter = ( configCPU_CLOCK_HZ / ( serBAUD_DIV_CONSTANT * ulWantedBaud ) ) - ( unsigned long ) 1;
88 /* Set the baud rate. */
89 ucByte = ( unsigned char ) ( ulBaudRateCounter & ( unsigned long ) 0xff );
90 outb( UBRRL, ucByte );
92 ulBaudRateCounter >>= ( unsigned long ) 8;
93 ucByte = ( unsigned char ) ( ulBaudRateCounter & ( unsigned long ) 0xff );
94 outb( UBRRH, ucByte );
96 /* Enable the Rx interrupt. The Tx interrupt will get enabled
97 later. Also enable the Rx and Tx. */
98 outb( UCSRB, serRX_INT_ENABLE | serRX_ENABLE | serTX_ENABLE );
100 /* Set the data bits to 8. */
101 outb( UCSRC, serUCSRC_SELECT | serEIGHT_DATA_BITS );
105 /* Unlike other ports, this serial code does not allow for more than one
106 com port. We therefore don't return a pointer to a port structure and can
107 instead just return NULL. */
110 /*-----------------------------------------------------------*/
112 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
114 /* Get the next character from the buffer. Return false if no characters
115 are available, or arrive before xBlockTime expires. */
116 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
125 /*-----------------------------------------------------------*/
127 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
129 /* Return false if after the block time there is no room on the Tx queue. */
130 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
139 /*-----------------------------------------------------------*/
141 void vSerialClose( xComPortHandle xPort )
143 unsigned char ucByte;
145 /* Turn off the interrupts. We may also want to delete the queues and/or
146 re-install the original ISR. */
148 portENTER_CRITICAL();
152 ucByte &= ~serRX_INT_ENABLE;
153 outb( UCSRB, ucByte );
157 /*-----------------------------------------------------------*/
159 __interrupt void SIG_UART_RECV( void )
161 signed char ucChar, xHigherPriorityTaskWoken = pdFALSE;
163 /* Get the character and post it on the queue of Rxed characters.
164 If the post causes a task to wake force a context switch as the woken task
165 may have a higher priority than the task we have interrupted. */
168 xQueueSendFromISR( xRxedChars, &ucChar, &xHigherPriorityTaskWoken );
170 if( xHigherPriorityTaskWoken != pdFALSE )
175 /*-----------------------------------------------------------*/
177 __interrupt void SIG_UART_DATA( void )
179 signed char cChar, cTaskWoken = pdFALSE;
181 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &cTaskWoken ) == pdTRUE )
183 /* Send the next character queued for Tx. */
188 /* Queue empty, nothing to send. */