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