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