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