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