]> git.sur5r.net Git - freertos/blob - Demo/ColdFire_MCF5282_Eclipse/RTOSDemo/serial/serial.c
324ee63824be3142a633194f758551c7d6d512de
[freertos] / Demo / ColdFire_MCF5282_Eclipse / RTOSDemo / serial / serial.c
1 /*\r
2         FreeRTOS.org V5.0.3 - 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 ) 0x10 << 3 )\r
70 #define ser8DATA_BITS           ( ( unsigned portCHAR ) 0x11 )\r
71 #define ser1STOP_BIT            ( ( unsigned portCHAR ) 0x111 )\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;\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 / ( 32 * 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         /* Set the pins to UART mode. */\r
104         MCF_PAD_PUAPAR |= ( serTX_OUTPUT | serRX_INPUT );\r
105 \r
106         /* Configure the UART. */\r
107         MCF_UART1_UMR1 = serNO_PARITY | ser8DATA_BITS;\r
108         MCF_UART1_UMR2 = ser1STOP_BIT;\r
109         MCF_UART1_UCSR = serSYSTEM_CLOCK;\r
110 \r
111         MCF_UART1_UBG1 = ( unsigned portCHAR ) ( ( ulBaudRateDivisor >> 8UL ) & 0xffUL );\r
112         MCF_UART1_UBG2 = ( unsigned portCHAR ) ( ulBaudRateDivisor & 0xffUL );\r
113 \r
114         /* Reset the peripheral before turning it on. */\r
115         MCF_UART1_UCR = MCF_UART_UCR_RESET_MR;\r
116         MCF_UART1_UCR = MCF_UART_UCR_RESET_RX;\r
117         MCF_UART1_UCR = MCF_UART_UCR_RESET_TX;\r
118         MCF_UART1_UCR = MCF_UART_UCR_RESET_ERROR;\r
119         MCF_UART1_UCR = serTX_ENABLE | serRX_ENABLE;\r
120 \r
121         /* Configure the interrupt controller.  Run the UARTs above the kernel\r
122         interrupt priority for demo purposes. */\r
123     MCF_INTC0_ICR14 = ( ( configKERNEL_INTERRUPT_PRIORITY + 1 ) | ( 1 << 3 ) );\r
124     MCF_INTC0_IMRL &= ~MCF_INTC_IMRL_INT_MASK14;\r
125 \r
126         /* The Tx interrupt is not enabled until there is data to send. */\r
127         MCF_UART0_UIMR = serRX_INT;\r
128 \r
129         /* Only a single port is implemented so we don't need to return anything. */\r
130         return NULL;\r
131 }\r
132 /*-----------------------------------------------------------*/\r
133 \r
134 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )\r
135 {\r
136         /* Only one port is supported. */\r
137         ( void ) pxPort;\r
138 \r
139         /* Get the next character from the buffer.  Return false if no characters\r
140         are available or arrive before xBlockTime expires. */\r
141         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
142         {\r
143                 return pdTRUE;\r
144         }\r
145         else\r
146         {\r
147                 return pdFALSE;\r
148         }\r
149 }\r
150 /*-----------------------------------------------------------*/\r
151 \r
152 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )\r
153 {\r
154         /* Only one port is supported. */\r
155         ( void ) pxPort;\r
156 \r
157         /* Return false if after the block time there is no room on the Tx queue. */\r
158         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
159         {\r
160                 return pdFAIL;\r
161         }\r
162 \r
163         /* A critical section should not be required as xTxHasEnded will not be\r
164         written to by the ISR if it is already 0 (is this correct?). */\r
165         if( xTxHasEnded )\r
166         {\r
167                 xTxHasEnded = pdFALSE;\r
168                 MCF_UART0_UIMR = serRX_INT | serTX_INT;\r
169         }\r
170 \r
171         return pdPASS;\r
172 }\r
173 /*-----------------------------------------------------------*/\r
174 \r
175 void vSerialClose( xComPortHandle xPort )\r
176 {\r
177         ( void ) xPort;\r
178 }\r
179 /*-----------------------------------------------------------*/\r
180 \r
181 void __cs3_isr_interrupt_78( void )\r
182 {\r
183 unsigned portCHAR ucChar;\r
184 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE, xDoneSomething = pdTRUE;\r
185 \r
186         while( xDoneSomething != pdFALSE )\r
187         {\r
188                 xDoneSomething = pdFALSE;\r
189 \r
190                 /* Does the tx buffer contain space? */\r
191                 if( ( MCF_UART0_USR & MCF_UART_USR_TXRDY ) != 0x00 )\r
192                 {\r
193                         /* Are there any characters queued to be sent? */\r
194                         if( xQueueReceiveFromISR( xCharsForTx, &ucChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
195                         {\r
196                                 /* Send the next char. */\r
197                                 MCF_UART0_UTB = ucChar;\r
198                                 xDoneSomething = pdTRUE;\r
199                         }\r
200                         else\r
201                         {\r
202                                 /* Turn off the Tx interrupt until such time as another character\r
203                                 is being transmitted. */
204                                 MCF_UART0_UIMR = serRX_INT;\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