]> git.sur5r.net Git - freertos/blob - Demo/ColdFire_MCF5282_Eclipse/RTOSDemo/serial/serial.c
Prepare for V5.3.0 release.
[freertos] / Demo / ColdFire_MCF5282_Eclipse / RTOSDemo / serial / serial.c
1 /*\r
2         FreeRTOS.org V5.3.0 - 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 it\r
7         under the terms of the GNU General Public License (version 2) as published\r
8         by the 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.org without being obliged to provide\r
11         the source code for any proprietary components.  Alternative commercial\r
12         license and support terms are also available upon request.  See the \r
13         licensing section of http://www.FreeRTOS.org for full details.\r
14 \r
15         FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT\r
16         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
17         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
18         more details.\r
19 \r
20         You should have received a copy of the GNU General Public License along\r
21         with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59\r
22         Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
23 \r
24 \r
25         ***************************************************************************\r
26         *                                                                         *\r
27         * Get the FreeRTOS eBook!  See http://www.FreeRTOS.org/Documentation      *\r
28         *                                                                         *\r
29         * This is a concise, step by step, 'hands on' guide that describes both   *\r
30         * general multitasking concepts and FreeRTOS specifics. It presents and   *\r
31         * explains numerous examples that are written using the FreeRTOS API.     *\r
32         * Full source code for all the examples is provided in an accompanying    *\r
33         * .zip file.                                                              *\r
34         *                                                                         *\r
35         ***************************************************************************\r
36 \r
37         1 tab == 4 spaces!\r
38 \r
39         Please ensure to read the configuration and relevant port sections of the\r
40         online documentation.\r
41 \r
42         http://www.FreeRTOS.org - Documentation, latest information, license and\r
43         contact details.\r
44 \r
45         http://www.SafeRTOS.com - A version that is certified for use in safety\r
46         critical systems.\r
47 \r
48         http://www.OpenRTOS.com - Commercial support, development, porting,\r
49         licensing and training services.\r
50 */\r
51 \r
52 \r
53 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER.\r
54 \r
55 NOTE:  This driver is primarily to test the scheduler functionality.  It does\r
56 not effectively use the buffers or DMA and is therefore not intended to be\r
57 an example of an efficient driver. */\r
58 \r
59 /* Standard include file. */\r
60 #include <stdlib.h>\r
61 \r
62 /* Scheduler include files. */\r
63 #include "FreeRTOS.h"\r
64 #include "queue.h"\r
65 #include "task.h"\r
66 \r
67 /* Demo app include files. */\r
68 #include "serial.h"\r
69 \r
70 /* Hardware definitions. */\r
71 #define serNO_PARITY            ( ( unsigned portCHAR ) 0x02 << 3 )\r
72 #define ser8DATA_BITS           ( ( unsigned portCHAR ) 0x03 )\r
73 #define ser1STOP_BIT            ( ( unsigned portCHAR ) 0x07 )\r
74 #define serSYSTEM_CLOCK         ( ( unsigned portCHAR ) 0xdd )\r
75 #define serTX_OUTPUT            ( ( unsigned portCHAR ) 0x04 )\r
76 #define serRX_INPUT                     ( ( unsigned portCHAR ) 0x08 )\r
77 #define serTX_ENABLE            ( ( unsigned portCHAR ) 0x04 )\r
78 #define serRX_ENABLE            ( ( unsigned portCHAR ) 0x01 )\r
79 #define serTX_INT                       ( ( unsigned portCHAR ) 0x01 )\r
80 #define serRX_INT                       ( ( unsigned portCHAR ) 0x02 )\r
81 \r
82 \r
83 /* The queues used to communicate between tasks and ISR's. */\r
84 static xQueueHandle xRxedChars;\r
85 static xQueueHandle xCharsForTx;\r
86 \r
87 /* Flag used to indicate the tx status. */\r
88 static portBASE_TYPE xTxHasEnded = pdTRUE;\r
89 \r
90 /*-----------------------------------------------------------*/\r
91 \r
92 /* The UART interrupt handler. */\r
93 void __attribute__( ( interrupt ) ) __cs3_isr_interrupt_78( void );\r
94 \r
95 /*-----------------------------------------------------------*/\r
96 \r
97 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
98 {\r
99 const unsigned portLONG ulBaudRateDivisor = ( configCPU_CLOCK_HZ / ( 32UL * ulWantedBaud ) );\r
100 \r
101         /* Create the queues used by the com test task. */\r
102         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
103         xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
104 \r
105         xTxHasEnded = pdTRUE;\r
106 \r
107         /* Set the pins to UART mode. */\r
108         MCF_PAD_PUAPAR |= ( serTX_OUTPUT | serRX_INPUT );\r
109 \r
110         /* Reset the peripheral. */\r
111         MCF_UART1_UCR = MCF_UART_UCR_RESET_RX;\r
112         MCF_UART1_UCR = MCF_UART_UCR_RESET_TX;\r
113         MCF_UART1_UCR = MCF_UART_UCR_RESET_ERROR;\r
114         MCF_UART1_UCR = MCF_UART_UCR_RESET_BKCHGINT;\r
115         MCF_UART1_UCR = MCF_UART_UCR_RESET_MR | MCF_UART_UCR_RX_DISABLED | MCF_UART_UCR_TX_DISABLED;\r
116 \r
117         /* Configure the UART. */\r
118         MCF_UART1_UMR1 = serNO_PARITY | ser8DATA_BITS;\r
119         MCF_UART1_UMR2 = ser1STOP_BIT;\r
120         MCF_UART1_UCSR = serSYSTEM_CLOCK;\r
121 \r
122         MCF_UART1_UBG1 = ( unsigned portCHAR ) ( ( ulBaudRateDivisor >> 8UL ) & 0xffUL );\r
123         MCF_UART1_UBG2 = ( unsigned portCHAR ) ( ulBaudRateDivisor & 0xffUL );\r
124 \r
125         /* Turn it on. */\r
126         MCF_UART1_UCR = serTX_ENABLE | serRX_ENABLE;\r
127 \r
128         /* Configure the interrupt controller.  Run the UARTs above the kernel\r
129         interrupt priority for demo purposes. */\r
130     MCF_INTC0_ICR14 = ( ( configMAX_SYSCALL_INTERRUPT_PRIORITY - 2  ) << 3 );\r
131     MCF_INTC0_IMRL &= ~( MCF_INTC_IMRL_INT_MASK14 | 0x01 );\r
132 \r
133         /* The Tx interrupt is not enabled until there is data to send. */\r
134         MCF_UART1_UIMR = serRX_INT;\r
135 \r
136         /* Only a single port is implemented so we don't need to return anything. */\r
137         return NULL;\r
138 }\r
139 /*-----------------------------------------------------------*/\r
140 \r
141 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )\r
142 {\r
143         /* Only one port is supported. */\r
144         ( void ) pxPort;\r
145 \r
146         /* Get the next character from the buffer.  Return false if no characters\r
147         are available or arrive before xBlockTime expires. */\r
148         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
149         {\r
150                 return pdTRUE;\r
151         }\r
152         else\r
153         {\r
154                 return pdFALSE;\r
155         }\r
156 }\r
157 /*-----------------------------------------------------------*/\r
158 \r
159 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )\r
160 {\r
161         /* Only one port is supported. */\r
162         ( void ) pxPort;\r
163 \r
164         /* Return false if after the block time there is no room on the Tx queue. */\r
165         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
166         {\r
167                 return pdFAIL;\r
168         }\r
169 \r
170         /* A critical section should not be required as xTxHasEnded will not be\r
171         written to by the ISR if it is already 0 (is this correct?). */\r
172         if( xTxHasEnded != pdFALSE )\r
173         {\r
174                 xTxHasEnded = pdFALSE;\r
175                 MCF_UART1_UIMR = serRX_INT | serTX_INT;\r
176         }\r
177 \r
178         return pdPASS;\r
179 }\r
180 /*-----------------------------------------------------------*/\r
181 \r
182 void vSerialClose( xComPortHandle xPort )\r
183 {\r
184         ( void ) xPort;\r
185 }\r
186 /*-----------------------------------------------------------*/\r
187 \r
188 void __cs3_isr_interrupt_78( void )\r
189 {\r
190 unsigned portCHAR ucChar;\r
191 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE, xDoneSomething = pdTRUE;\r
192 \r
193         while( xDoneSomething != pdFALSE )\r
194         {\r
195                 xDoneSomething = pdFALSE;\r
196 \r
197                 /* Does the tx buffer contain space? */\r
198                 if( ( MCF_UART1_USR & MCF_UART_USR_TXRDY ) != 0x00 )\r
199                 {\r
200                         /* Are there any characters queued to be sent? */\r
201                         if( xQueueReceiveFromISR( xCharsForTx, &ucChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
202                         {\r
203                                 /* Send the next char. */\r
204                                 MCF_UART1_UTB = ucChar;\r
205                                 xDoneSomething = pdTRUE;\r
206                         }\r
207                         else\r
208                         {\r
209                                 /* Turn off the Tx interrupt until such time as another character\r
210                                 is being transmitted. */\r
211                                 MCF_UART1_UIMR = serRX_INT;\r
212                                 xTxHasEnded = pdTRUE;\r
213                         }\r
214                 }\r
215 \r
216                 if( MCF_UART1_USR & MCF_UART_USR_RXRDY )\r
217                 {\r
218                         ucChar = MCF_UART1_URB;\r
219                         xQueueSendFromISR( xRxedChars, &ucChar, &xHigherPriorityTaskWoken );\r
220                         xDoneSomething = pdTRUE;\r
221                 }\r
222         }\r
223 \r
224     portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
225 }\r
226 \r
227 \r
228 \r
229 \r
230 \r
231 \r
232 \r