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