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