]> git.sur5r.net Git - freertos/blob - Demo/ColdFire_MCF52221_CodeWarrior/sources/serial/serial.c
c553bd49fe67deffd0694d0d807d084b8a05ed88
[freertos] / Demo / ColdFire_MCF52221_CodeWarrior / sources / serial / serial.c
1 /*\r
2         FreeRTOS.org V5.0.4 - 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_ENABLE            ( ( unsigned portCHAR ) 0x04 )\r
74 #define serRX_ENABLE            ( ( unsigned portCHAR ) 0x01 )\r
75 #define serTX_INT                       ( ( unsigned portCHAR ) 0x01 )\r
76 #define serRX_INT                       ( ( unsigned portCHAR ) 0x02 )\r
77 \r
78 \r
79 /* The queues used to communicate between tasks and ISR's. */\r
80 static xQueueHandle xRxedChars;\r
81 static xQueueHandle xCharsForTx;\r
82 \r
83 /* Flag used to indicate the tx status. */\r
84 static portBASE_TYPE xTxHasEnded = pdTRUE;\r
85 \r
86 /*-----------------------------------------------------------*/\r
87 \r
88 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
89 {\r
90 const unsigned portLONG ulBaudRateDivisor = ( configCPU_CLOCK_HZ / ( 32UL * ulWantedBaud ) );\r
91 \r
92         /* Create the queues used by the com test task. */\r
93         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
94         xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
95 \r
96         xTxHasEnded = pdTRUE;\r
97 \r
98         /* Set the pins to UART mode. */\r
99         MCF_GPIO_PUAPAR |= MCF_GPIO_PUAPAR_UTXD0_UTXD0;\r
100         MCF_GPIO_PUAPAR |= MCF_GPIO_PUAPAR_URXD0_URXD0;\r
101 \r
102         /* Reset the peripheral. */\r
103         MCF_UART0_UCR = MCF_UART_UCR_RESET_RX;\r
104         MCF_UART0_UCR = MCF_UART_UCR_RESET_TX;\r
105         MCF_UART0_UCR = MCF_UART_UCR_RESET_ERROR;\r
106         MCF_UART0_UCR = MCF_UART_UCR_RESET_BKCHGINT;\r
107         MCF_UART0_UCR = MCF_UART_UCR_RESET_MR | MCF_UART_UCR_RX_DISABLED | MCF_UART_UCR_TX_DISABLED;\r
108 \r
109         /* Configure the UART. */\r
110         MCF_UART0_UMR1 = serNO_PARITY | ser8DATA_BITS;\r
111         MCF_UART0_UMR2 = ser1STOP_BIT;\r
112         MCF_UART0_UCSR = serSYSTEM_CLOCK;\r
113 \r
114         MCF_UART0_UBG1 = ( unsigned portCHAR ) ( ( ulBaudRateDivisor >> 8UL ) & 0xffUL );\r
115         MCF_UART0_UBG2 = ( unsigned portCHAR ) ( ulBaudRateDivisor & 0xffUL );\r
116 \r
117         /* Turn it on. */\r
118         MCF_UART0_UCR = serTX_ENABLE | serRX_ENABLE;\r
119 \r
120         /* Configure the interrupt controller.  Run the UARTs above the kernel\r
121         interrupt priority for demo purposes. */\r
122     MCF_INTC0_ICR13 = ( ( configMAX_SYSCALL_INTERRUPT_PRIORITY - 1  ) << 3 );\r
123     MCF_INTC0_IMRL &= ~( MCF_INTC_IMRL_INT_MASK13 | 0x01 );\r
124 \r
125         /* The Tx interrupt is not enabled until there is data to send. */\r
126         MCF_UART0_UIMR = serRX_INT;\r
127 \r
128         /* Only a single port is implemented so we don't need to return anything. */\r
129         return NULL;\r
130 }\r
131 /*-----------------------------------------------------------*/\r
132 \r
133 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )\r
134 {\r
135         /* Only one port is supported. */\r
136         ( void ) pxPort;\r
137 \r
138         /* Get the next character from the buffer.  Return false if no characters\r
139         are available or arrive before xBlockTime expires. */\r
140         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
141         {\r
142                 return pdTRUE;\r
143         }\r
144         else\r
145         {\r
146                 return pdFALSE;\r
147         }\r
148 }\r
149 /*-----------------------------------------------------------*/\r
150 \r
151 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )\r
152 {\r
153         /* Only one port is supported. */\r
154         ( void ) pxPort;\r
155 \r
156         /* Return false if after the block time there is no room on the Tx queue. */\r
157         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
158         {\r
159                 return pdFAIL;\r
160         }\r
161 \r
162         /* A critical section should not be required as xTxHasEnded will not be\r
163         written to by the ISR if it is already 0 (is this correct?). */\r
164         if( xTxHasEnded != pdFALSE )\r
165         {\r
166                 xTxHasEnded = pdFALSE;\r
167                 MCF_UART0_UIMR = serRX_INT | serTX_INT;\r
168         }\r
169 \r
170         return pdPASS;\r
171 }\r
172 /*-----------------------------------------------------------*/\r
173 \r
174 void vSerialClose( xComPortHandle xPort )\r
175 {\r
176         ( void ) xPort;\r
177 }\r
178 /*-----------------------------------------------------------*/\r
179 \r
180 __declspec(interrupt:0) void vUART0InterruptHandler( void )\r
181 {\r
182 unsigned portCHAR ucChar;\r
183 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE, xDoneSomething = pdTRUE;\r
184 \r
185         while( xDoneSomething != pdFALSE )\r
186         {\r
187                 xDoneSomething = pdFALSE;\r
188 \r
189                 /* Does the tx buffer contain space? */\r
190                 if( ( MCF_UART0_USR & MCF_UART_USR_TXRDY ) != 0x00 )\r
191                 {\r
192                         /* Are there any characters queued to be sent? */\r
193                         if( xQueueReceiveFromISR( xCharsForTx, &ucChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
194                         {\r
195                                 /* Send the next char. */\r
196                                 MCF_UART0_UTB = ucChar;\r
197                                 xDoneSomething = pdTRUE;\r
198                         }\r
199                         else\r
200                         {\r
201                                 /* Turn off the Tx interrupt until such time as another character\r
202                                 is being transmitted. */\r
203                                 MCF_UART0_UIMR = serRX_INT;\r
204                                 xTxHasEnded = pdTRUE;\r
205                         }\r
206                 }\r
207 \r
208                 if( MCF_UART0_USR & MCF_UART_USR_RXRDY )\r
209                 {\r
210                         ucChar = MCF_UART0_URB;\r
211                         xQueueSendFromISR( xRxedChars, &ucChar, &xHigherPriorityTaskWoken );\r
212                         xDoneSomething = pdTRUE;\r
213                 }\r
214         }\r
215 \r
216     portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
217 }\r
218 \r
219 \r
220 \r
221 \r
222 \r
223 \r
224 \r