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