]> git.sur5r.net Git - freertos/blob - Demo/ColdFire_MCF5282_Eclipse/RTOSDemo/serial/serial.c
Update to V5.1.2.
[freertos] / Demo / ColdFire_MCF5282_Eclipse / RTOSDemo / 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.\r
55 \r
56 NOTE:  This driver is primarily to test the scheduler functionality.  It does\r
57 not effectively use the buffers or DMA and is therefore not intended to be\r
58 an example of an efficient driver. */\r
59 \r
60 /* Standard include file. */\r
61 #include <stdlib.h>\r
62 \r
63 /* Scheduler include files. */\r
64 #include "FreeRTOS.h"\r
65 #include "queue.h"\r
66 #include "task.h"\r
67 \r
68 /* Demo app include files. */\r
69 #include "serial.h"\r
70 \r
71 /* Hardware definitions. */\r
72 #define serNO_PARITY            ( ( unsigned portCHAR ) 0x02 << 3 )\r
73 #define ser8DATA_BITS           ( ( unsigned portCHAR ) 0x03 )\r
74 #define ser1STOP_BIT            ( ( unsigned portCHAR ) 0x07 )\r
75 #define serSYSTEM_CLOCK         ( ( unsigned portCHAR ) 0xdd )\r
76 #define serTX_OUTPUT            ( ( unsigned portCHAR ) 0x04 )\r
77 #define serRX_INPUT                     ( ( unsigned portCHAR ) 0x08 )\r
78 #define serTX_ENABLE            ( ( unsigned portCHAR ) 0x04 )\r
79 #define serRX_ENABLE            ( ( unsigned portCHAR ) 0x01 )\r
80 #define serTX_INT                       ( ( unsigned portCHAR ) 0x01 )\r
81 #define serRX_INT                       ( ( unsigned portCHAR ) 0x02 )\r
82 \r
83 \r
84 /* The queues used to communicate between tasks and ISR's. */\r
85 static xQueueHandle xRxedChars;\r
86 static xQueueHandle xCharsForTx;\r
87 \r
88 /* Flag used to indicate the tx status. */\r
89 static portBASE_TYPE xTxHasEnded = pdTRUE;\r
90 \r
91 /*-----------------------------------------------------------*/\r
92 \r
93 /* The UART interrupt handler. */\r
94 void __attribute__( ( interrupt ) ) __cs3_isr_interrupt_78( void );\r
95 \r
96 /*-----------------------------------------------------------*/\r
97 \r
98 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
99 {\r
100 const unsigned portLONG ulBaudRateDivisor = ( configCPU_CLOCK_HZ / ( 32UL * ulWantedBaud ) );\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         xTxHasEnded = pdTRUE;\r
107 \r
108         /* Set the pins to UART mode. */\r
109         MCF_PAD_PUAPAR |= ( serTX_OUTPUT | serRX_INPUT );\r
110 \r
111         /* Reset the peripheral. */\r
112         MCF_UART1_UCR = MCF_UART_UCR_RESET_RX;\r
113         MCF_UART1_UCR = MCF_UART_UCR_RESET_TX;\r
114         MCF_UART1_UCR = MCF_UART_UCR_RESET_ERROR;\r
115         MCF_UART1_UCR = MCF_UART_UCR_RESET_BKCHGINT;\r
116         MCF_UART1_UCR = MCF_UART_UCR_RESET_MR | MCF_UART_UCR_RX_DISABLED | MCF_UART_UCR_TX_DISABLED;\r
117 \r
118         /* Configure the UART. */\r
119         MCF_UART1_UMR1 = serNO_PARITY | ser8DATA_BITS;\r
120         MCF_UART1_UMR2 = ser1STOP_BIT;\r
121         MCF_UART1_UCSR = serSYSTEM_CLOCK;\r
122 \r
123         MCF_UART1_UBG1 = ( unsigned portCHAR ) ( ( ulBaudRateDivisor >> 8UL ) & 0xffUL );\r
124         MCF_UART1_UBG2 = ( unsigned portCHAR ) ( ulBaudRateDivisor & 0xffUL );\r
125 \r
126         /* Turn it on. */\r
127         MCF_UART1_UCR = serTX_ENABLE | serRX_ENABLE;\r
128 \r
129         /* Configure the interrupt controller.  Run the UARTs above the kernel\r
130         interrupt priority for demo purposes. */\r
131     MCF_INTC0_ICR14 = ( ( configMAX_SYSCALL_INTERRUPT_PRIORITY - 2  ) << 3 );\r
132     MCF_INTC0_IMRL &= ~( MCF_INTC_IMRL_INT_MASK14 | 0x01 );\r
133 \r
134         /* The Tx interrupt is not enabled until there is data to send. */\r
135         MCF_UART1_UIMR = serRX_INT;\r
136 \r
137         /* Only a single port is implemented so we don't need to return anything. */\r
138         return NULL;\r
139 }\r
140 /*-----------------------------------------------------------*/\r
141 \r
142 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )\r
143 {\r
144         /* Only one port is supported. */\r
145         ( void ) pxPort;\r
146 \r
147         /* Get the next character from the buffer.  Return false if no characters\r
148         are available or arrive before xBlockTime expires. */\r
149         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
150         {\r
151                 return pdTRUE;\r
152         }\r
153         else\r
154         {\r
155                 return pdFALSE;\r
156         }\r
157 }\r
158 /*-----------------------------------------------------------*/\r
159 \r
160 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )\r
161 {\r
162         /* Only one port is supported. */\r
163         ( void ) pxPort;\r
164 \r
165         /* Return false if after the block time there is no room on the Tx queue. */\r
166         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
167         {\r
168                 return pdFAIL;\r
169         }\r
170 \r
171         /* A critical section should not be required as xTxHasEnded will not be\r
172         written to by the ISR if it is already 0 (is this correct?). */\r
173         if( xTxHasEnded != pdFALSE )\r
174         {\r
175                 xTxHasEnded = pdFALSE;\r
176                 MCF_UART1_UIMR = serRX_INT | serTX_INT;\r
177         }\r
178 \r
179         return pdPASS;\r
180 }\r
181 /*-----------------------------------------------------------*/\r
182 \r
183 void vSerialClose( xComPortHandle xPort )\r
184 {\r
185         ( void ) xPort;\r
186 }\r
187 /*-----------------------------------------------------------*/\r
188 \r
189 void __cs3_isr_interrupt_78( void )\r
190 {\r
191 unsigned portCHAR ucChar;\r
192 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE, xDoneSomething = pdTRUE;\r
193 \r
194         while( xDoneSomething != pdFALSE )\r
195         {\r
196                 xDoneSomething = pdFALSE;\r
197 \r
198                 /* Does the tx buffer contain space? */\r
199                 if( ( MCF_UART1_USR & MCF_UART_USR_TXRDY ) != 0x00 )\r
200                 {\r
201                         /* Are there any characters queued to be sent? */\r
202                         if( xQueueReceiveFromISR( xCharsForTx, &ucChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
203                         {\r
204                                 /* Send the next char. */\r
205                                 MCF_UART1_UTB = ucChar;\r
206                                 xDoneSomething = pdTRUE;\r
207                         }\r
208                         else\r
209                         {\r
210                                 /* Turn off the Tx interrupt until such time as another character\r
211                                 is being transmitted. */\r
212                                 MCF_UART1_UIMR = serRX_INT;\r
213                                 xTxHasEnded = pdTRUE;\r
214                         }\r
215                 }\r
216 \r
217                 if( MCF_UART1_USR & MCF_UART_USR_RXRDY )\r
218                 {\r
219                         ucChar = MCF_UART1_URB;\r
220                         xQueueSendFromISR( xRxedChars, &ucChar, &xHigherPriorityTaskWoken );\r
221                         xDoneSomething = pdTRUE;\r
222                 }\r
223         }\r
224 \r
225     portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
226 }\r
227 \r
228 \r
229 \r
230 \r
231 \r
232 \r
233 \r