]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/AVR_ATMega323_WinAVR/serial/serial.c
d1d57a7e7e6cb224f2b3df380f3246f5f945654a
[freertos] / FreeRTOS / Demo / AVR_ATMega323_WinAVR / 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 Changes from V1.2.3\r
30 \r
31         + The function xPortInitMinimal() has been renamed to \r
32           xSerialPortInitMinimal() and the function xPortInit() has been renamed\r
33           to xSerialPortInit().\r
34 \r
35 Changes from V2.0.0\r
36 \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
40 \r
41 Changes from V2.6.0\r
42 \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
45           WinAVR.\r
46 */\r
47 \r
48 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER. */\r
49 \r
50 \r
51 #include <stdlib.h>\r
52 #include <avr/interrupt.h>\r
53 #include "FreeRTOS.h"\r
54 #include "queue.h"\r
55 #include "task.h"\r
56 #include "serial.h"\r
57 \r
58 #define serBAUD_DIV_CONSTANT                    ( ( unsigned long ) 16 )\r
59 \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
65 \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
69 \r
70 static QueueHandle_t xRxedChars; \r
71 static QueueHandle_t xCharsForTx; \r
72 \r
73 #define vInterruptOn()                                                                          \\r
74 {                                                                                                                       \\r
75         unsigned char ucByte;                                                           \\r
76                                                                                                                         \\r
77         ucByte = UCSRB;                                                                                 \\r
78         ucByte |= serTX_INT_ENABLE;                                                             \\r
79         UCSRB = ucByte;                                                                                 \\r
80 }                                                                                                                                                               \r
81 /*-----------------------------------------------------------*/\r
82 \r
83 #define vInterruptOff()                                                                         \\r
84 {                                                                                                                       \\r
85         unsigned char ucInByte;                                                         \\r
86                                                                                                                         \\r
87         ucInByte = UCSRB;                                                                               \\r
88         ucInByte &= ~serTX_INT_ENABLE;                                                  \\r
89         UCSRB = ucInByte;                                                                               \\r
90 }\r
91 /*-----------------------------------------------------------*/\r
92 \r
93 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
94 {\r
95 unsigned long ulBaudRateCounter;\r
96 unsigned char ucByte;\r
97 \r
98         portENTER_CRITICAL();\r
99         {\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
103 \r
104                 /* Calculate the baud rate register value from the equation in the\r
105                 data sheet. */\r
106                 ulBaudRateCounter = ( configCPU_CLOCK_HZ / ( serBAUD_DIV_CONSTANT * ulWantedBaud ) ) - ( unsigned long ) 1;\r
107 \r
108                 /* Set the baud rate. */        \r
109                 ucByte = ( unsigned char ) ( ulBaudRateCounter & ( unsigned long ) 0xff );      \r
110                 UBRRL = ucByte;\r
111 \r
112                 ulBaudRateCounter >>= ( unsigned long ) 8;\r
113                 ucByte = ( unsigned char ) ( ulBaudRateCounter & ( unsigned long ) 0xff );      \r
114                 UBRRH = ucByte;\r
115 \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
119 \r
120                 /* Set the data bits to 8. */\r
121                 UCSRC = ( serUCSRC_SELECT | serEIGHT_DATA_BITS );\r
122         }\r
123         portEXIT_CRITICAL();\r
124         \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
128         return NULL;\r
129 }\r
130 /*-----------------------------------------------------------*/\r
131 \r
132 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
133 {\r
134         /* Only one port is supported. */\r
135         ( void ) pxPort;\r
136 \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
140         {\r
141                 return pdTRUE;\r
142         }\r
143         else\r
144         {\r
145                 return pdFALSE;\r
146         }\r
147 }\r
148 /*-----------------------------------------------------------*/\r
149 \r
150 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
151 {\r
152         /* Only one port is supported. */\r
153         ( void ) pxPort;\r
154 \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
157         {\r
158                 return pdFAIL;\r
159         }\r
160 \r
161         vInterruptOn();\r
162 \r
163         return pdPASS;\r
164 }\r
165 /*-----------------------------------------------------------*/\r
166 \r
167 void vSerialClose( xComPortHandle xPort )\r
168 {\r
169 unsigned char ucByte;\r
170 \r
171         /* The parameter is not used. */\r
172         ( void ) xPort;\r
173 \r
174         /* Turn off the interrupts.  We may also want to delete the queues and/or\r
175         re-install the original ISR. */\r
176 \r
177         portENTER_CRITICAL();\r
178         {\r
179                 vInterruptOff();\r
180                 ucByte = UCSRB;\r
181                 ucByte &= ~serRX_INT_ENABLE;\r
182                 UCSRB = ucByte;\r
183         }\r
184         portEXIT_CRITICAL();\r
185 }\r
186 /*-----------------------------------------------------------*/\r
187 \r
188 SIGNAL( SIG_UART_RECV )\r
189 {\r
190 signed char cChar;\r
191 signed portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
192 \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
196         cChar = UDR;\r
197 \r
198         xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
199 \r
200         if( xHigherPriorityTaskWoken != pdFALSE )\r
201         {\r
202                 taskYIELD();\r
203         }\r
204 }\r
205 /*-----------------------------------------------------------*/\r
206 \r
207 SIGNAL( SIG_UART_DATA )\r
208 {\r
209 signed char cChar, cTaskWoken;\r
210 \r
211         if( xQueueReceiveFromISR( xCharsForTx, &cChar, &cTaskWoken ) == pdTRUE )\r
212         {\r
213                 /* Send the next character queued for Tx. */\r
214                 UDR = cChar;\r
215         }\r
216         else\r
217         {\r
218                 /* Queue empty, nothing to send. */\r
219                 vInterruptOff();\r
220         }\r
221 }\r
222 \r