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