]> git.sur5r.net Git - freertos/blob - Demo/MCF5235_GCC/system/serial.c
Add PIC24, dsPIC and Coldfire files.
[freertos] / Demo / MCF5235_GCC / system / serial.c
1 /*
2     FreeRTOS MCF5235 port - Copyright (C) 2006 Christian Walter.
3
4     This file is part of the FreeRTOS distribution.
5
6     FreeRTOS is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     FreeRTOS is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with FreeRTOS; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20     A special exception to the GPL can be applied should you wish to distribute
21     a combined work that includes FreeRTOS, without being obliged to provide
22     the source code for any proprietary components.  See the licensing section
23     of http://www.FreeRTOS.org for full details of how and when the exception
24     can be applied.
25
26     ***************************************************************************
27     See http://www.FreeRTOS.org for documentation, latest information, license
28     and contact details.  Please ensure to read the configuration and relevant
29     port sections of the online documentation.
30     ***************************************************************************
31 */
32
33 /* ------------------------ MCF523x includes ------------------------------ */
34 #include "mcf5xxx.h"
35 #include "mcf523x.h"
36
37 /* ------------------------ FreeRTOS includes ----------------------------- */
38 #include "FreeRTOS.h"
39 #include "queue.h"
40 #include "task.h"
41
42 #include "serial.h"
43
44 /* ----------------------- Defines ----------------------------------------- */
45 #define BAUDRATE_VALUE(fsys, baud)      ( ( fsys )/(32UL * baud) )
46 #define MCF_UART_VECTOR                 ( 64 + 13 )
47 #define COM_NIFACE                      1
48 #define COM_BLOCK_RETRYTIME             10
49
50 /* ------------------------ Static functions ------------------------------ */
51 static void     prvSerialISR( void );
52
53 /* ------------------------ Static variables ------------------------------ */
54 typedef struct
55 {
56     portBASE_TYPE xInitialized;
57     xQueueHandle xRXChars;
58     xQueueHandle xTXChars;
59 } xComPortIF_t;
60
61 static xComPortIF_t xComPortIF[ COM_NIFACE ];
62
63 /* ------------------------ Begin implementation -------------------------- */
64 xComPortHandle
65 xSerialPortInitMinimal( unsigned portLONG ulWantedBaud,
66                         unsigned portBASE_TYPE uxQueueLength )
67 {
68     extern void     ( *__RAMVEC[] ) (  );
69     xComPortHandle xReturn;
70     portBASE_TYPE xOldIPL;
71
72     /* Create the queues used to hold Rx and Tx characters. */
73     xComPortIF[ 0 ].xRXChars =
74         xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE )sizeof( signed portCHAR ) );
75     xComPortIF[ 0 ].xTXChars =
76         xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE )sizeof( signed portCHAR ) );
77
78     /* If the queues were created correctly then setup the serial port hardware. */
79     if( ( xComPortIF[ 0 ].xRXChars != 0 ) && ( xComPortIF[ 0 ].xTXChars != 0 ) )
80     {
81         xOldIPL = portSET_IPL( portIPL_MAX );
82
83         /* UART 0: Reset transmitter, receiver and mode register pointer */
84         MCF_UART_UCR0 = MCF_UART_UCR_MISC( 0x3 );
85         MCF_UART_UCR0 = MCF_UART_UCR_MISC( 0x2 );
86         MCF_UART_UCR0 = MCF_UART_UCR_MISC( 0x1 );
87
88         /* Enable receive interrupts. */
89         MCF_UART_UIMR0 = MCF_UART_UIMR_RXRDY_FU;
90
91         /* 8 Databits, 1 Stopbit and no parity */
92         MCF_UART_UMR0 = MCF_UART_UMR_PM( 0x3 ) | MCF_UART_UMR_SB( 0x7 ) | MCF_UART_UMR_BC( 0x3 );
93
94         /* UART 0 Clocking */
95         MCF_UART_UCSR0 = MCF_UART_UCSR_RCS( 0xd ) | MCF_UART_UCSR_TCS( 0xd );
96         MCF_UART_UBG10 = BAUDRATE_VALUE( FSYS_2, ulWantedBaud ) >> 8U;
97         MCF_UART_UBG20 = BAUDRATE_VALUE( FSYS_2, ulWantedBaud ) & 0xFFU;
98
99         /* UART 0: Enable interrupts */
100         __RAMVEC[MCF_UART_VECTOR] = prvSerialISR;
101         MCF_INTC0_ICR13 = MCF_INTC0_ICRn_IL( 0x2 ) | MCF_INTC0_ICRn_IP( 0x1 );
102         MCF_INTC0_IMRL &= ~MCF_INTC0_IMRL_INT_MASK13;
103
104         /* UART 0 Miscellaneous */
105         MCF_UART_UACR0 = 0;
106
107         /* UART 0: Enable pins */
108         MCF_GPIO_PAR_UART = MCF_GPIO_PAR_UART_PAR_U0RXD | MCF_GPIO_PAR_UART_PAR_U0TXD;
109
110         /* Enable the UART. */
111         MCF_UART_UCR0 = MCF_UART_UCR_RXC( 0x1 ) | MCF_UART_UCR_TXC( 0x1 );
112
113         xComPortIF[ 0 ].xInitialized = TRUE;
114         xReturn = ( xComPortHandle ) &xComPortIF[ 0 ];
115
116         ( void )portSET_IPL( xOldIPL );
117     }
118     else
119     {
120         xReturn = ( xComPortHandle ) 0;
121     }
122
123     return xReturn;
124 }
125
126 signed          portBASE_TYPE
127 xSerialGetChar( xComPortHandle pxPort, signed portCHAR * pcRxedChar,
128                 portTickType xBlockTime )
129 {
130     int i;
131     portBASE_TYPE xResult = pdFALSE;
132     /* Lookup the correct interface. */
133     for( i = 0; i < COM_NIFACE; i++ )
134     {
135         if( pxPort == ( xComPortHandle ) &xComPortIF[ i ] )
136         {
137             break;
138         }
139     }
140     /* This COM port is available. */
141     if( ( i != COM_NIFACE ) && xComPortIF[ i ].xInitialized )
142     {
143         /* Get the next character from the buffer.  Return false if no characters
144          * are available, or arrive before xBlockTime expires.
145          */
146         if( xQueueReceive( xComPortIF[ i ].xRXChars, pcRxedChar, xBlockTime ) )
147         {
148             xResult = pdTRUE;
149         }
150     }
151     return xResult;
152 }
153
154 void
155 vSerialPutString( xComPortHandle pxPort, const signed portCHAR *
156                   const pcString, unsigned portSHORT usStringLength )
157 {
158     int i;
159     signed portCHAR *pChNext;
160
161     /* Send each character in the string, one at a time. */
162     pChNext = ( signed portCHAR * )pcString;
163     for( i = 0; i < usStringLength; i++ )
164     {
165         /* Block until character has been transmitted. */
166         while( xSerialPutChar( pxPort, *pChNext, COM_BLOCK_RETRYTIME ) != pdTRUE ); pChNext++;
167     }
168 }
169
170 signed          portBASE_TYPE
171 xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar,
172                 portTickType xBlockTime )
173 {
174     int i;
175     portBASE_TYPE xResult = pdFALSE;
176     portBASE_TYPE xOldIPL;
177     /* Lookup the correct interface. */
178     for( i = 0; i < COM_NIFACE; i++ )
179     {
180         if( pxPort == ( xComPortHandle ) &xComPortIF[ i ] )
181         {
182             break;
183         }
184     }
185     /* This COM port is available. */
186     if( ( i != COM_NIFACE ) && xComPortIF[ i ].xInitialized )
187     {
188         /* Place the character in the queue of characters to be transmitted. */
189         if( xQueueSend( xComPortIF[ i ].xTXChars, &cOutChar, xBlockTime ) == pdPASS )
190         {
191             /* Turn on the Tx interrupt so the ISR will remove the character from the
192              * queue and send it. */
193             MCF_UART_UIMR0 = MCF_UART_UIMR_TXRDY | MCF_UART_UIMR_RXRDY_FU;
194             xResult = pdTRUE;
195         }
196     }
197     return xResult;
198 }
199
200 signed          portBASE_TYPE
201 xSerialPutCharNOISR( xComPortHandle pxPort, signed portCHAR cOutChar )
202 {
203     int i;
204     portBASE_TYPE xResult = pdFALSE;
205     portBASE_TYPE xOldIPL = portSET_IPL( portIPL_MAX );
206     /* Lookup the correct interface. */
207     for( i = 0; i < COM_NIFACE; i++ )
208     {
209         if( pxPort == ( xComPortHandle ) &xComPortIF[ i ] )
210         {
211             break;
212         }
213     }
214     /* This COM port is available. Support for this only available for COM1 right now. */
215     if( ( i != COM_NIFACE ) && ( i == 0 ) )
216     {
217         /* Wait until the transmit buffer is ready. */
218         while( !( MCF_UART_USR0 & MCF_UART_USR_TXRDY ) );
219         /* Place the character in the transmit buffer. */
220         MCF_UART_UTB0 = cOutChar;
221         xResult = pdTRUE;
222     }
223     ( void )portSET_IPL( xOldIPL );
224     return xResult;
225 }
226
227 void
228 vSerialPutStringNOISR( xComPortHandle pxPort, const signed portCHAR *
229                        const pcString, unsigned portSHORT usStringLength )
230 {
231     int i;
232     signed portCHAR *pChNext;
233     portBASE_TYPE xOldIPL = portSET_IPL( portIPL_MAX );
234
235     /* Send each character in the string, one at a time. */
236     pChNext = ( signed portCHAR * )pcString;
237     for( i = 0; i < usStringLength; i++ )
238     {
239         /* Block until character has been transmitted. */
240         while( xSerialPutCharNOISR( pxPort, *pChNext ) != pdTRUE );
241         pChNext++;
242     }
243     ( void )portSET_IPL( xOldIPL );
244 }
245
246 void
247 vSerialClose( xComPortHandle xPort )
248 {
249     /* Not supported as not required by the demo application. */
250 }
251
252 void
253 prvSerialISR( void )
254 {
255     static signed portCHAR cChar;
256     static portBASE_TYPE xTaskWokenByTx = pdFALSE, xTaskWokenByRx = pdFALSE;
257
258     /* We have to remvoe the effect of the GCC. Please note that the
259      * __attribute__ ((interrupt_handler)) does not work here because we
260      * have to do the storing of the registers ourself. Another problem
261      * is the usage of a frame pointer which is unlinked on entry.
262      */
263 #if _GCC_USES_FP == 1
264     asm volatile ( "unlk %fp\n\t" );
265 #endif
266     /* This ISR can cause a context switch, so the first statement must be
267      * a call to the portENTER_SWITCHING_ISR() macro. This must be BEFORE any
268      * variable declarations.
269      */
270     portENTER_SWITCHING_ISR();
271
272     /* Ready to send a character from the buffer. */
273     if( MCF_UART_USR0 & MCF_UART_USR_TXRDY )
274     {
275         /* Transmit buffer is ready. Test if there are characters available. */
276         if( xQueueReceiveFromISR( xComPortIF[ 0 ].xTXChars, &cChar, &xTaskWokenByTx ) ==
277             pdTRUE )
278         {
279             /* A character was retrieved from the queue so can be sent. */
280             MCF_UART_UTB0 = cChar;
281         }
282         else
283         {
284             /* Leave only receiver enabled. */
285             MCF_UART_UIMR0 = MCF_UART_UIMR_RXRDY_FU;
286         }
287     }
288     if( MCF_UART_USR0 & MCF_UART_USR_RXRDY )
289     {
290         cChar = MCF_UART_URB0;
291         xTaskWokenByRx =
292             xQueueSendFromISR( xComPortIF[ 0].xRXChars, &cChar, xTaskWokenByRx );
293     }
294     /* Exit the ISR.  If a task was woken by either a character being
295      * or transmitted then a context switch will occur.
296      */
297     portEXIT_SWITCHING_ISR( ( xTaskWokenByTx || xTaskWokenByRx ) );
298 }