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