]> git.sur5r.net Git - freertos/blob - Demo/AVR_ATMega323_IAR/serial/serial.c
Update Cortex M3 ports to ensure 8 byte alignment.
[freertos] / Demo / AVR_ATMega323_IAR / serial / serial.c
1 /*\r
2     FreeRTOS V6.0.1 - Copyright (C) 2009 Real Time Engineers Ltd.\r
3 \r
4     ***************************************************************************\r
5     *                                                                         *\r
6     * If you are:                                                             *\r
7     *                                                                         *\r
8     *    + New to FreeRTOS,                                                   *\r
9     *    + Wanting to learn FreeRTOS or multitasking in general quickly       *\r
10     *    + Looking for basic training,                                        *\r
11     *    + Wanting to improve your FreeRTOS skills and productivity           *\r
12     *                                                                         *\r
13     * then take a look at the FreeRTOS eBook                                  *\r
14     *                                                                         *\r
15     *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *\r
16     *                  http://www.FreeRTOS.org/Documentation                  *\r
17     *                                                                         *\r
18     * A pdf reference manual is also available.  Both are usually delivered   *\r
19     * to your inbox within 20 minutes to two hours when purchased between 8am *\r
20     * and 8pm GMT (although please allow up to 24 hours in case of            *\r
21     * exceptional circumstances).  Thank you for your support!                *\r
22     *                                                                         *\r
23     ***************************************************************************\r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     ***NOTE*** The exception to the GPL is included to allow you to distribute\r
31     a combined work that includes FreeRTOS without being obliged to provide the\r
32     source code for proprietary components outside of the FreeRTOS kernel.\r
33     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT\r
34     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
35     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public \r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it \r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained \r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 \r
55 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR IAR AVR PORT. */\r
56 \r
57 \r
58 #include <stdlib.h>\r
59 #include "FreeRTOS.h"\r
60 #include "queue.h"\r
61 #include "task.h"\r
62 #include "serial.h"\r
63 \r
64 #define serBAUD_DIV_CONSTANT                    ( ( unsigned long ) 16 )\r
65 \r
66 /* Constants for writing to UCSRB. */\r
67 #define serRX_INT_ENABLE                                ( ( unsigned char ) 0x80 )\r
68 #define serRX_ENABLE                                    ( ( unsigned char ) 0x10 )\r
69 #define serTX_ENABLE                                    ( ( unsigned char ) 0x08 )\r
70 #define serTX_INT_ENABLE                                ( ( unsigned char ) 0x20 )\r
71 \r
72 /* Constants for writing to UCSRC. */\r
73 #define serUCSRC_SELECT                                 ( ( unsigned char ) 0x80 )\r
74 #define serEIGHT_DATA_BITS                              ( ( unsigned char ) 0x06 )\r
75 \r
76 static xQueueHandle xRxedChars;\r
77 static xQueueHandle xCharsForTx;\r
78 \r
79 #define vInterruptOn()                                                                          \\r
80 {                                                                                                                       \\r
81         unsigned char ucByte;                                                           \\r
82                                                                                                                         \\r
83         ucByte = UCSRB;                                                                                 \\r
84         ucByte |= serTX_INT_ENABLE;                                                             \\r
85         outb( UCSRB, ucByte );                                                                  \\r
86 }                                                                                                                                                               \r
87 /*-----------------------------------------------------------*/\r
88 \r
89 #define vInterruptOff()                                                                         \\r
90 {                                                                                                                       \\r
91         unsigned char ucByte;                                                           \\r
92                                                                                                                         \\r
93         ucByte = UCSRB;                                                                                 \\r
94         ucByte &= ~serTX_INT_ENABLE;                                                    \\r
95         outb( UCSRB, ucByte );                                                                  \\r
96 }\r
97 /*-----------------------------------------------------------*/\r
98 \r
99 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
100 {\r
101 unsigned long ulBaudRateCounter;\r
102 unsigned char ucByte;\r
103 \r
104         portENTER_CRITICAL();\r
105         {\r
106                 /* Create the queues used by the com test task. */\r
107                 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
108                 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
109 \r
110                 /* Calculate the baud rate register value from the equation in the\r
111                 data sheet. */\r
112                 ulBaudRateCounter = ( configCPU_CLOCK_HZ / ( serBAUD_DIV_CONSTANT * ulWantedBaud ) ) - ( unsigned long ) 1;\r
113 \r
114                 /* Set the baud rate. */        \r
115                 ucByte = ( unsigned char ) ( ulBaudRateCounter & ( unsigned long ) 0xff );      \r
116                 outb( UBRRL, ucByte );\r
117 \r
118                 ulBaudRateCounter >>= ( unsigned long ) 8;\r
119                 ucByte = ( unsigned char ) ( ulBaudRateCounter & ( unsigned long ) 0xff );      \r
120                 outb( UBRRH, ucByte );\r
121 \r
122                 /* Enable the Rx interrupt.  The Tx interrupt will get enabled\r
123                 later. Also enable the Rx and Tx. */\r
124                 outb( UCSRB, serRX_INT_ENABLE | serRX_ENABLE | serTX_ENABLE );\r
125 \r
126                 /* Set the data bits to 8. */\r
127                 outb( UCSRC, serUCSRC_SELECT | serEIGHT_DATA_BITS );\r
128         }\r
129         portEXIT_CRITICAL();\r
130         \r
131         /* Unlike other ports, this serial code does not allow for more than one\r
132         com port.  We therefore don't return a pointer to a port structure and can\r
133         instead just return NULL. */\r
134         return NULL;\r
135 }\r
136 /*-----------------------------------------------------------*/\r
137 \r
138 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )\r
139 {\r
140         /* Get the next character from the buffer.  Return false if no characters\r
141         are available, or arrive before xBlockTime expires. */\r
142         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
143         {\r
144                 return pdTRUE;\r
145         }\r
146         else\r
147         {\r
148                 return pdFALSE;\r
149         }\r
150 }\r
151 /*-----------------------------------------------------------*/\r
152 \r
153 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )\r
154 {\r
155         /* Return false if after the block time there is no room on the Tx queue. */\r
156         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
157         {\r
158                 return pdFAIL;\r
159         }\r
160 \r
161         vInterruptOn();\r
162 \r
163         return pdPASS;\r
164 }\r
165 /*-----------------------------------------------------------*/\r
166 \r
167 void vSerialClose( xComPortHandle xPort )\r
168 {\r
169 unsigned char ucByte;\r
170 \r
171         /* Turn off the interrupts.  We may also want to delete the queues and/or\r
172         re-install the original ISR. */\r
173 \r
174         portENTER_CRITICAL();\r
175         {\r
176                 vInterruptOff();\r
177                 ucByte = UCSRB;\r
178                 ucByte &= ~serRX_INT_ENABLE;\r
179                 outb( UCSRB, ucByte );\r
180         }\r
181         portEXIT_CRITICAL();\r
182 }\r
183 /*-----------------------------------------------------------*/\r
184 \r
185 __interrupt void SIG_UART_RECV( void )\r
186 {\r
187 signed char ucChar, xHigherPriorityTaskWoken = pdFALSE;\r
188 \r
189         /* Get the character and post it on the queue of Rxed characters.\r
190         If the post causes a task to wake force a context switch as the woken task\r
191         may have a higher priority than the task we have interrupted. */\r
192         ucChar = UDR;\r
193 \r
194         xQueueSendFromISR( xRxedChars, &ucChar, &xHigherPriorityTaskWoken );\r
195 \r
196         if( xHigherPriorityTaskWoken != pdFALSE )\r
197         {\r
198                 taskYIELD();\r
199         }\r
200 }\r
201 /*-----------------------------------------------------------*/\r
202 \r
203 __interrupt void SIG_UART_DATA( void )\r
204 {\r
205 signed char cChar, cTaskWoken = pdFALSE;\r
206 \r
207         if( xQueueReceiveFromISR( xCharsForTx, &cChar, &cTaskWoken ) == pdTRUE )\r
208         {\r
209                 /* Send the next character queued for Tx. */\r
210                 outb( UDR, cChar );\r
211         }\r
212         else\r
213         {\r
214                 /* Queue empty, nothing to send. */\r
215                 vInterruptOff();\r
216         }\r
217 }\r
218 \r