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