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