]> git.sur5r.net Git - freertos/blob - Demo/AVR_ATMega323_WinAVR/serial/serial.c
Update in preparation for the V4.3.1 release.
[freertos] / Demo / AVR_ATMega323_WinAVR / serial / serial.c
1 /*\r
2         FreeRTOS.org V4.3.1 - Copyright (C) 2003-2007 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify\r
7         it under the terms of the GNU General Public License as published by\r
8         the Free Software Foundation; either version 2 of the License, or\r
9         (at your option) any later version.\r
10 \r
11         FreeRTOS.org is distributed in the hope that it will be useful,\r
12         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14         GNU General Public License for more details.\r
15 \r
16         You should have received a copy of the GNU General Public License\r
17         along with FreeRTOS.org; if not, write to the Free Software\r
18         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19 \r
20         A special exception to the GPL can be applied should you wish to distribute\r
21         a combined work that includes FreeRTOS.org, without being obliged to provide\r
22         the source code for any proprietary components.  See the licensing section \r
23         of http://www.FreeRTOS.org for full details of how and when the exception\r
24         can be applied.\r
25 \r
26         ***************************************************************************\r
27         See http://www.FreeRTOS.org for documentation, latest information, license \r
28         and contact details.  Please ensure to read the configuration and relevant \r
29         port sections of the online documentation.\r
30 \r
31         Also see http://www.SafeRTOS.com for an IEC 61508 compliant version along\r
32         with commercial development and support options.\r
33         ***************************************************************************\r
34 */\r
35 \r
36 /*\r
37 Changes from V1.2.3\r
38 \r
39         + The function xPortInitMinimal() has been renamed to \r
40           xSerialPortInitMinimal() and the function xPortInit() has been renamed\r
41           to xSerialPortInit().\r
42 \r
43 Changes from V2.0.0\r
44 \r
45         + Delay periods are now specified using variables and constants of\r
46           portTickType rather than unsigned portLONG.\r
47         + xQueueReceiveFromISR() used in place of xQueueReceive() within the ISR.\r
48 \r
49 Changes from V2.6.0\r
50 \r
51         + Replaced the inb() and outb() functions with direct memory\r
52           access.  This allows the port to be built with the 20050414 build of\r
53           WinAVR.\r
54 */\r
55 \r
56 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER. */\r
57 \r
58 \r
59 #include <stdlib.h>\r
60 #include <avr/interrupt.h>\r
61 #include "FreeRTOS.h"\r
62 #include "queue.h"\r
63 #include "task.h"\r
64 #include "serial.h"\r
65 \r
66 #define serBAUD_DIV_CONSTANT                    ( ( unsigned portLONG ) 16 )\r
67 \r
68 /* Constants for writing to UCSRB. */\r
69 #define serRX_INT_ENABLE                                ( ( unsigned portCHAR ) 0x80 )\r
70 #define serRX_ENABLE                                    ( ( unsigned portCHAR ) 0x10 )\r
71 #define serTX_ENABLE                                    ( ( unsigned portCHAR ) 0x08 )\r
72 #define serTX_INT_ENABLE                                ( ( unsigned portCHAR ) 0x20 )\r
73 \r
74 /* Constants for writing to UCSRC. */\r
75 #define serUCSRC_SELECT                                 ( ( unsigned portCHAR ) 0x80 )\r
76 #define serEIGHT_DATA_BITS                              ( ( unsigned portCHAR ) 0x06 )\r
77 \r
78 static xQueueHandle xRxedChars; \r
79 static xQueueHandle xCharsForTx; \r
80 \r
81 #define vInterruptOn()                                                                          \\r
82 {                                                                                                                       \\r
83         unsigned portCHAR ucByte;                                                               \\r
84                                                                                                                         \\r
85         ucByte = UCSRB;                                                                                 \\r
86         ucByte |= serTX_INT_ENABLE;                                                             \\r
87         UCSRB = ucByte;                                                                                 \\r
88 }                                                                                                                                                               \r
89 /*-----------------------------------------------------------*/\r
90 \r
91 #define vInterruptOff()                                                                         \\r
92 {                                                                                                                       \\r
93         unsigned portCHAR ucInByte;                                                             \\r
94                                                                                                                         \\r
95         ucInByte = UCSRB;                                                                               \\r
96         ucInByte &= ~serTX_INT_ENABLE;                                                  \\r
97         UCSRB = ucInByte;                                                                               \\r
98 }\r
99 /*-----------------------------------------------------------*/\r
100 \r
101 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
102 {\r
103 unsigned portLONG ulBaudRateCounter;\r
104 unsigned portCHAR ucByte;\r
105 \r
106         portENTER_CRITICAL();\r
107         {\r
108                 /* Create the queues used by the com test task. */\r
109                 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
110                 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
111 \r
112                 /* Calculate the baud rate register value from the equation in the\r
113                 data sheet. */\r
114                 ulBaudRateCounter = ( configCPU_CLOCK_HZ / ( serBAUD_DIV_CONSTANT * ulWantedBaud ) ) - ( unsigned portLONG ) 1;\r
115 \r
116                 /* Set the baud rate. */        \r
117                 ucByte = ( unsigned portCHAR ) ( ulBaudRateCounter & ( unsigned portLONG ) 0xff );      \r
118                 UBRRL = ucByte;\r
119 \r
120                 ulBaudRateCounter >>= ( unsigned portLONG ) 8;\r
121                 ucByte = ( unsigned portCHAR ) ( ulBaudRateCounter & ( unsigned portLONG ) 0xff );      \r
122                 UBRRH = ucByte;\r
123 \r
124                 /* Enable the Rx interrupt.  The Tx interrupt will get enabled\r
125                 later. Also enable the Rx and Tx. */\r
126                 UCSRB = ( serRX_INT_ENABLE | serRX_ENABLE | serTX_ENABLE );\r
127 \r
128                 /* Set the data bits to 8. */\r
129                 UCSRC = ( serUCSRC_SELECT | serEIGHT_DATA_BITS );\r
130         }\r
131         portEXIT_CRITICAL();\r
132         \r
133         /* Unlike other ports, this serial code does not allow for more than one\r
134         com port.  We therefore don't return a pointer to a port structure and can\r
135         instead just return NULL. */\r
136         return NULL;\r
137 }\r
138 /*-----------------------------------------------------------*/\r
139 \r
140 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )\r
141 {\r
142         /* Only one port is supported. */\r
143         ( void ) pxPort;\r
144 \r
145         /* Get the next character from the buffer.  Return false if no characters\r
146         are available, or arrive before xBlockTime expires. */\r
147         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
148         {\r
149                 return pdTRUE;\r
150         }\r
151         else\r
152         {\r
153                 return pdFALSE;\r
154         }\r
155 }\r
156 /*-----------------------------------------------------------*/\r
157 \r
158 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )\r
159 {\r
160         /* Only one port is supported. */\r
161         ( void ) pxPort;\r
162 \r
163         /* Return false if after the block time there is no room on the Tx queue. */\r
164         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
165         {\r
166                 return pdFAIL;\r
167         }\r
168 \r
169         vInterruptOn();\r
170 \r
171         return pdPASS;\r
172 }\r
173 /*-----------------------------------------------------------*/\r
174 \r
175 void vSerialClose( xComPortHandle xPort )\r
176 {\r
177 unsigned portCHAR ucByte;\r
178 \r
179         /* The parameter is not used. */\r
180         ( void ) xPort;\r
181 \r
182         /* Turn off the interrupts.  We may also want to delete the queues and/or\r
183         re-install the original ISR. */\r
184 \r
185         portENTER_CRITICAL();\r
186         {\r
187                 vInterruptOff();\r
188                 ucByte = UCSRB;\r
189                 ucByte &= ~serRX_INT_ENABLE;\r
190                 UCSRB = ucByte;\r
191         }\r
192         portEXIT_CRITICAL();\r
193 }\r
194 /*-----------------------------------------------------------*/\r
195 \r
196 SIGNAL( SIG_UART_RECV )\r
197 {\r
198 signed portCHAR cChar;\r
199 \r
200         /* Get the character and post it on the queue of Rxed characters.\r
201         If the post causes a task to wake force a context switch as the woken task\r
202         may have a higher priority than the task we have interrupted. */\r
203         cChar = UDR;\r
204 \r
205         if( xQueueSendFromISR( xRxedChars, &cChar, pdFALSE ) )\r
206         {\r
207                 taskYIELD();\r
208         }\r
209 }\r
210 /*-----------------------------------------------------------*/\r
211 \r
212 SIGNAL( SIG_UART_DATA )\r
213 {\r
214 signed portCHAR cChar, cTaskWoken;\r
215 \r
216         if( xQueueReceiveFromISR( xCharsForTx, &cChar, &cTaskWoken ) == pdTRUE )\r
217         {\r
218                 /* Send the next character queued for Tx. */\r
219                 UDR = cChar;\r
220         }\r
221         else\r
222         {\r
223                 /* Queue empty, nothing to send. */\r
224                 vInterruptOff();\r
225         }\r
226 }\r
227 \r