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
29 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR IAR AVR PORT. */
\r
33 #include "FreeRTOS.h"
\r
38 #define serBAUD_DIV_CONSTANT ( ( unsigned long ) 16 )
\r
40 /* Constants for writing to UCSRB. */
\r
41 #define serRX_INT_ENABLE ( ( unsigned char ) 0x80 )
\r
42 #define serRX_ENABLE ( ( unsigned char ) 0x10 )
\r
43 #define serTX_ENABLE ( ( unsigned char ) 0x08 )
\r
44 #define serTX_INT_ENABLE ( ( unsigned char ) 0x20 )
\r
46 /* Constants for writing to UCSRC. */
\r
47 #define serUCSRC_SELECT ( ( unsigned char ) 0x80 )
\r
48 #define serEIGHT_DATA_BITS ( ( unsigned char ) 0x06 )
\r
50 static QueueHandle_t xRxedChars;
\r
51 static QueueHandle_t xCharsForTx;
\r
53 #define vInterruptOn() \
\r
55 unsigned char ucByte; \
\r
58 ucByte |= serTX_INT_ENABLE; \
\r
59 outb( UCSRB, ucByte ); \
\r
61 /*-----------------------------------------------------------*/
\r
63 #define vInterruptOff() \
\r
65 unsigned char ucByte; \
\r
68 ucByte &= ~serTX_INT_ENABLE; \
\r
69 outb( UCSRB, ucByte ); \
\r
71 /*-----------------------------------------------------------*/
\r
73 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
75 unsigned long ulBaudRateCounter;
\r
76 unsigned char ucByte;
\r
78 portENTER_CRITICAL();
\r
80 /* Create the queues used by the com test task. */
\r
81 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
82 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
84 /* Calculate the baud rate register value from the equation in the
\r
86 ulBaudRateCounter = ( configCPU_CLOCK_HZ / ( serBAUD_DIV_CONSTANT * ulWantedBaud ) ) - ( unsigned long ) 1;
\r
88 /* Set the baud rate. */
\r
89 ucByte = ( unsigned char ) ( ulBaudRateCounter & ( unsigned long ) 0xff );
\r
90 outb( UBRRL, ucByte );
\r
92 ulBaudRateCounter >>= ( unsigned long ) 8;
\r
93 ucByte = ( unsigned char ) ( ulBaudRateCounter & ( unsigned long ) 0xff );
\r
94 outb( UBRRH, ucByte );
\r
96 /* Enable the Rx interrupt. The Tx interrupt will get enabled
\r
97 later. Also enable the Rx and Tx. */
\r
98 outb( UCSRB, serRX_INT_ENABLE | serRX_ENABLE | serTX_ENABLE );
\r
100 /* Set the data bits to 8. */
\r
101 outb( UCSRC, serUCSRC_SELECT | serEIGHT_DATA_BITS );
\r
103 portEXIT_CRITICAL();
\r
105 /* Unlike other ports, this serial code does not allow for more than one
\r
106 com port. We therefore don't return a pointer to a port structure and can
\r
107 instead just return NULL. */
\r
110 /*-----------------------------------------------------------*/
\r
112 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
\r
114 /* Get the next character from the buffer. Return false if no characters
\r
115 are available, or arrive before xBlockTime expires. */
\r
116 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
125 /*-----------------------------------------------------------*/
\r
127 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
\r
129 /* Return false if after the block time there is no room on the Tx queue. */
\r
130 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
139 /*-----------------------------------------------------------*/
\r
141 void vSerialClose( xComPortHandle xPort )
\r
143 unsigned char ucByte;
\r
145 /* Turn off the interrupts. We may also want to delete the queues and/or
\r
146 re-install the original ISR. */
\r
148 portENTER_CRITICAL();
\r
152 ucByte &= ~serRX_INT_ENABLE;
\r
153 outb( UCSRB, ucByte );
\r
155 portEXIT_CRITICAL();
\r
157 /*-----------------------------------------------------------*/
\r
159 __interrupt void SIG_UART_RECV( void )
\r
161 signed char ucChar, xHigherPriorityTaskWoken = pdFALSE;
\r
163 /* Get the character and post it on the queue of Rxed characters.
\r
164 If the post causes a task to wake force a context switch as the woken task
\r
165 may have a higher priority than the task we have interrupted. */
\r
168 xQueueSendFromISR( xRxedChars, &ucChar, &xHigherPriorityTaskWoken );
\r
170 if( xHigherPriorityTaskWoken != pdFALSE )
\r
175 /*-----------------------------------------------------------*/
\r
177 __interrupt void SIG_UART_DATA( void )
\r
179 signed char cChar, cTaskWoken = pdFALSE;
\r
181 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &cTaskWoken ) == pdTRUE )
\r
183 /* Send the next character queued for Tx. */
\r
184 outb( UDR, cChar );
\r
188 /* Queue empty, nothing to send. */
\r