]> git.sur5r.net Git - freertos/blob - Demo/AVR_ATMega323_IAR/serial/serial.c
Update to V4.7.2.
[freertos] / Demo / AVR_ATMega323_IAR / serial / serial.c
1 /*\r
2         FreeRTOS.org V4.7.2 - 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 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR IAR AVR PORT. */\r
45 \r
46 \r
47 #include <stdlib.h>\r
48 #include "FreeRTOS.h"\r
49 #include "queue.h"\r
50 #include "task.h"\r
51 #include "serial.h"\r
52 \r
53 #define serBAUD_DIV_CONSTANT                    ( ( unsigned portLONG ) 16 )\r
54 \r
55 /* Constants for writing to UCSRB. */\r
56 #define serRX_INT_ENABLE                                ( ( unsigned portCHAR ) 0x80 )\r
57 #define serRX_ENABLE                                    ( ( unsigned portCHAR ) 0x10 )\r
58 #define serTX_ENABLE                                    ( ( unsigned portCHAR ) 0x08 )\r
59 #define serTX_INT_ENABLE                                ( ( unsigned portCHAR ) 0x20 )\r
60 \r
61 /* Constants for writing to UCSRC. */\r
62 #define serUCSRC_SELECT                                 ( ( unsigned portCHAR ) 0x80 )\r
63 #define serEIGHT_DATA_BITS                              ( ( unsigned portCHAR ) 0x06 )\r
64 \r
65 static xQueueHandle xRxedChars; \r
66 static xQueueHandle xCharsForTx; \r
67 \r
68 #define vInterruptOn()                                                                          \\r
69 {                                                                                                                       \\r
70         unsigned portCHAR ucByte;                                                               \\r
71                                                                                                                         \\r
72         ucByte = UCSRB;                                                                                 \\r
73         ucByte |= serTX_INT_ENABLE;                                                             \\r
74         outb( UCSRB, ucByte );                                                                  \\r
75 }                                                                                                                                                               \r
76 /*-----------------------------------------------------------*/\r
77 \r
78 #define vInterruptOff()                                                                         \\r
79 {                                                                                                                       \\r
80         unsigned portCHAR ucByte;                                                               \\r
81                                                                                                                         \\r
82         ucByte = UCSRB;                                                                                 \\r
83         ucByte &= ~serTX_INT_ENABLE;                                                    \\r
84         outb( UCSRB, ucByte );                                                                  \\r
85 }\r
86 /*-----------------------------------------------------------*/\r
87 \r
88 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
89 {\r
90 unsigned portLONG ulBaudRateCounter;\r
91 unsigned portCHAR ucByte;\r
92 \r
93         portENTER_CRITICAL();\r
94         {\r
95                 /* Create the queues used by the com test task. */\r
96                 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
97                 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
98 \r
99                 /* Calculate the baud rate register value from the equation in the\r
100                 data sheet. */\r
101                 ulBaudRateCounter = ( configCPU_CLOCK_HZ / ( serBAUD_DIV_CONSTANT * ulWantedBaud ) ) - ( unsigned portLONG ) 1;\r
102 \r
103                 /* Set the baud rate. */        \r
104                 ucByte = ( unsigned portCHAR ) ( ulBaudRateCounter & ( unsigned portLONG ) 0xff );      \r
105                 outb( UBRRL, ucByte );\r
106 \r
107                 ulBaudRateCounter >>= ( unsigned portLONG ) 8;\r
108                 ucByte = ( unsigned portCHAR ) ( ulBaudRateCounter & ( unsigned portLONG ) 0xff );      \r
109                 outb( UBRRH, ucByte );\r
110 \r
111                 /* Enable the Rx interrupt.  The Tx interrupt will get enabled\r
112                 later. Also enable the Rx and Tx. */\r
113                 outb( UCSRB, serRX_INT_ENABLE | serRX_ENABLE | serTX_ENABLE );\r
114 \r
115                 /* Set the data bits to 8. */\r
116                 outb( UCSRC, serUCSRC_SELECT | serEIGHT_DATA_BITS );\r
117         }\r
118         portEXIT_CRITICAL();\r
119         \r
120         /* Unlike other ports, this serial code does not allow for more than one\r
121         com port.  We therefore don't return a pointer to a port structure and can\r
122         instead just return NULL. */\r
123         return NULL;\r
124 }\r
125 /*-----------------------------------------------------------*/\r
126 \r
127 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )\r
128 {\r
129         /* Get the next character from the buffer.  Return false if no characters\r
130         are available, or arrive before xBlockTime expires. */\r
131         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
132         {\r
133                 return pdTRUE;\r
134         }\r
135         else\r
136         {\r
137                 return pdFALSE;\r
138         }\r
139 }\r
140 /*-----------------------------------------------------------*/\r
141 \r
142 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )\r
143 {\r
144         /* Return false if after the block time there is no room on the Tx queue. */\r
145         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
146         {\r
147                 return pdFAIL;\r
148         }\r
149 \r
150         vInterruptOn();\r
151 \r
152         return pdPASS;\r
153 }\r
154 /*-----------------------------------------------------------*/\r
155 \r
156 void vSerialClose( xComPortHandle xPort )\r
157 {\r
158 unsigned portCHAR ucByte;\r
159 \r
160         /* Turn off the interrupts.  We may also want to delete the queues and/or\r
161         re-install the original ISR. */\r
162 \r
163         portENTER_CRITICAL();\r
164         {\r
165                 vInterruptOff();\r
166                 ucByte = UCSRB;\r
167                 ucByte &= ~serRX_INT_ENABLE;\r
168                 outb( UCSRB, ucByte );\r
169         }\r
170         portEXIT_CRITICAL();\r
171 }\r
172 /*-----------------------------------------------------------*/\r
173 \r
174 __interrupt void SIG_UART_RECV( void )\r
175 {\r
176 signed portCHAR cChar;\r
177 \r
178         /* Get the character and post it on the queue of Rxed characters.\r
179         If the post causes a task to wake force a context switch as the woken task\r
180         may have a higher priority than the task we have interrupted. */\r
181         cChar = UDR;\r
182 \r
183         if( xQueueSendFromISR( xRxedChars, &cChar, pdFALSE ) )\r
184         {\r
185                 taskYIELD();\r
186         }\r
187 }\r
188 /*-----------------------------------------------------------*/\r
189 \r
190 __interrupt void SIG_UART_DATA( void )\r
191 {\r
192 signed portCHAR cChar, cTaskWoken;\r
193 \r
194         if( xQueueReceiveFromISR( xCharsForTx, &cChar, &cTaskWoken ) == pdTRUE )\r
195         {\r
196                 /* Send the next character queued for Tx. */\r
197                 outb( UDR, cChar );\r
198         }\r
199         else\r
200         {\r
201                 /* Queue empty, nothing to send. */\r
202                 vInterruptOff();\r
203         }\r
204 }\r
205 \r