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