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