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