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