]> begriffs open source - cmsis-freertos/blob - Demo/AVR_ATMega323_IAR/serial/serial.c
Update README.md - branch main is now the base branch
[cmsis-freertos] / Demo / AVR_ATMega323_IAR / serial / serial.c
1 /*
2  * FreeRTOS V202111.00
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
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:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
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.
21  *
22  * http://www.FreeRTOS.org
23  * http://aws.amazon.com/freertos
24  *
25  * 1 tab == 4 spaces!
26  */
27
28
29 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR IAR AVR PORT. */
30
31
32 #include <stdlib.h>
33 #include "FreeRTOS.h"
34 #include "queue.h"
35 #include "task.h"
36 #include "serial.h"
37
38 #define serBAUD_DIV_CONSTANT                    ( ( unsigned long ) 16 )
39
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 )
45
46 /* Constants for writing to UCSRC. */
47 #define serUCSRC_SELECT                                 ( ( unsigned char ) 0x80 )
48 #define serEIGHT_DATA_BITS                              ( ( unsigned char ) 0x06 )
49
50 static QueueHandle_t xRxedChars;
51 static QueueHandle_t xCharsForTx;
52
53 #define vInterruptOn()                                                                          \
54 {                                                                                                                       \
55         unsigned char ucByte;                                                           \
56                                                                                                                         \
57         ucByte = UCSRB;                                                                                 \
58         ucByte |= serTX_INT_ENABLE;                                                             \
59         outb( UCSRB, ucByte );                                                                  \
60 }                                                                                                                                                               
61 /*-----------------------------------------------------------*/
62
63 #define vInterruptOff()                                                                         \
64 {                                                                                                                       \
65         unsigned char ucByte;                                                           \
66                                                                                                                         \
67         ucByte = UCSRB;                                                                                 \
68         ucByte &= ~serTX_INT_ENABLE;                                                    \
69         outb( UCSRB, ucByte );                                                                  \
70 }
71 /*-----------------------------------------------------------*/
72
73 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
74 {
75 unsigned long ulBaudRateCounter;
76 unsigned char ucByte;
77
78         portENTER_CRITICAL();
79         {
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 ) );
83
84                 /* Calculate the baud rate register value from the equation in the
85                 data sheet. */
86                 ulBaudRateCounter = ( configCPU_CLOCK_HZ / ( serBAUD_DIV_CONSTANT * ulWantedBaud ) ) - ( unsigned long ) 1;
87
88                 /* Set the baud rate. */        
89                 ucByte = ( unsigned char ) ( ulBaudRateCounter & ( unsigned long ) 0xff );      
90                 outb( UBRRL, ucByte );
91
92                 ulBaudRateCounter >>= ( unsigned long ) 8;
93                 ucByte = ( unsigned char ) ( ulBaudRateCounter & ( unsigned long ) 0xff );      
94                 outb( UBRRH, ucByte );
95
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 );
99
100                 /* Set the data bits to 8. */
101                 outb( UCSRC, serUCSRC_SELECT | serEIGHT_DATA_BITS );
102         }
103         portEXIT_CRITICAL();
104         
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. */
108         return NULL;
109 }
110 /*-----------------------------------------------------------*/
111
112 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
113 {
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 ) )
117         {
118                 return pdTRUE;
119         }
120         else
121         {
122                 return pdFALSE;
123         }
124 }
125 /*-----------------------------------------------------------*/
126
127 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
128 {
129         /* Return false if after the block time there is no room on the Tx queue. */
130         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
131         {
132                 return pdFAIL;
133         }
134
135         vInterruptOn();
136
137         return pdPASS;
138 }
139 /*-----------------------------------------------------------*/
140
141 void vSerialClose( xComPortHandle xPort )
142 {
143 unsigned char ucByte;
144
145         /* Turn off the interrupts.  We may also want to delete the queues and/or
146         re-install the original ISR. */
147
148         portENTER_CRITICAL();
149         {
150                 vInterruptOff();
151                 ucByte = UCSRB;
152                 ucByte &= ~serRX_INT_ENABLE;
153                 outb( UCSRB, ucByte );
154         }
155         portEXIT_CRITICAL();
156 }
157 /*-----------------------------------------------------------*/
158
159 __interrupt void SIG_UART_RECV( void )
160 {
161 signed char ucChar, xHigherPriorityTaskWoken = pdFALSE;
162
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. */
166         ucChar = UDR;
167
168         xQueueSendFromISR( xRxedChars, &ucChar, &xHigherPriorityTaskWoken );
169
170         if( xHigherPriorityTaskWoken != pdFALSE )
171         {
172                 taskYIELD();
173         }
174 }
175 /*-----------------------------------------------------------*/
176
177 __interrupt void SIG_UART_DATA( void )
178 {
179 signed char cChar, cTaskWoken = pdFALSE;
180
181         if( xQueueReceiveFromISR( xCharsForTx, &cChar, &cTaskWoken ) == pdTRUE )
182         {
183                 /* Send the next character queued for Tx. */
184                 outb( UDR, cChar );
185         }
186         else
187         {
188                 /* Queue empty, nothing to send. */
189                 vInterruptOff();
190         }
191 }
192