OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [ColdFire_MCF52221_CodeWarrior/] [sources/] [uart_support.c] - Blame information for rev 578

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 578 jeremybenn
/*
2
 * File:        uart_support.c
3
 * Purpose:     Implements UART basic support, Derivative Specific Interrupt handler and need function needed
4
 *              for MSL Support (printf\cout to terminal), defined in <UART.h>
5
 *
6
 * Notes:
7
 *
8
 */
9
#include "support_common.h"
10
#include "uart_support.h"
11
 
12
#if ENABLE_UART_SUPPORT==1
13
 
14
 
15
#if UART_SUPPORT_TYPE==UART_PSC
16
/* 5475 & 5485 boards have different names for uart access registers */
17
void uart_init(int channel, unsigned long systemClockKHz, unsigned long baudRate)
18
{
19
        register uint16 ubgs;
20
 
21
        /*
22
         * On Verdi, only PSC 0 & 1 are brought out to RS232 transceivers
23
         */
24
 
25
        /* Put PSC in UART mode */
26
        MCF_PSC_PSCSICR(channel) = MCF_PSC_PSCSICR_SIM_UART;
27
 
28
        /* Rx and Tx baud rate from timers */
29
        MCF_PSC_PSCCSR(channel) = (0
30
                | MCF_PSC_PSCCSR_RCSEL_SYS_CLK
31
                | MCF_PSC_PSCCSR_TCSEL_SYS_CLK);
32
 
33
        /*
34
         * Calculate baud settings
35
         */
36
        ubgs = (uint16)((systemClockKHz * 1000)/(baudRate * 32));
37
 
38
        MCF_PSC_PSCCTUR(channel) =  (uint8) ((ubgs >> 8) & 0xFF);
39
        MCF_PSC_PSCCTLR(channel) =  (uint8) (ubgs & 0xFF);
40
 
41
        /* Reset transmitter, receiver, mode register, and error conditions */
42
        MCF_PSC_PSCCR(channel) = MCF_PSC_PSCCR_RESET_RX;
43
        MCF_PSC_PSCCR(channel) = MCF_PSC_PSCCR_RESET_TX;
44
        MCF_PSC_PSCCR(channel) = MCF_PSC_PSCCR_RESET_ERROR;
45
        MCF_PSC_PSCCR(channel) = MCF_PSC_PSCCR_RESET_BKCHGINT;
46
        MCF_PSC_PSCCR(channel) = MCF_PSC_PSCCR_RESET_MR;
47
 
48
        /* 8-bit data, no parity */
49
        MCF_PSC_PSCMR(channel) = (0
50
#ifdef UART_HARDWARE_FLOW_CONTROL
51
                | MCF_PSC_PSCMR_RXRTS
52
#endif
53
                | MCF_PSC_PSCMR_PM_NONE
54
                | MCF_PSC_PSCMR_BC_8);
55
 
56
        /* No echo or loopback, 1 stop bit */
57
        MCF_PSC_PSCMR(channel) = (0
58
#ifdef UART_HARDWARE_FLOW_CONTROL
59
                | MCF_PSC_PSCMR_TXCTS
60
#endif 
61
                | MCF_PSC_PSCMR_CM_NORMAL
62
                | MCF_PSC_PSCMR_SB_STOP_BITS_1);
63
 
64
        /* Mask all UART interrupts */
65
        MCF_PSC_PSCIMR(channel) = 0x0000;
66
 
67
        /* Enable RTS to send */
68
        MCF_PSC_PSCOPSET(channel) = MCF_PSC_PSCOPSET_RTS;
69
 
70
        /* Setup FIFO Alarms */
71
        MCF_PSC_PSCRFAR(channel) = MCF_PSC_PSCRFAR_ALARM(248);
72
        MCF_PSC_PSCTFAR(channel) = MCF_PSC_PSCTFAR_ALARM(248);
73
 
74
        /* Enable receiver and transmitter */
75
        MCF_PSC_PSCCR(channel) =(0
76
                | MCF_PSC_PSCCR_RX_ENABLED
77
                | MCF_PSC_PSCCR_TX_ENABLED);
78
}
79
 
80
/********************************************************************/
81
/*
82
 * Wait for a character to be received on the specified UART
83
 *
84
 * Return Values:
85
 *  the received character
86
 */
87
char uart_getchar (int channel)
88
{
89
        /* Wait until character has been received */
90
        while (!(MCF_PSC_PSCSR(channel) & MCF_PSC_PSCSR_RXRDY))
91
        {
92
 
93
        }
94
        return (char)(*((uint8 *) &MCF_PSC_PSCRB_8BIT(channel)));
95
}
96
 
97
/********************************************************************/
98
/*
99
 * Wait for space in the UART Tx FIFO and then send a character
100
 */
101
void uart_putchar (int channel, char ch)
102
{
103
        /* Wait until space is available in the FIFO */
104
        while (!(MCF_PSC_PSCSR(channel) & MCF_PSC_PSCSR_TXRDY))
105
                ;
106
        *((uint8 *) &MCF_PSC_PSCTB_8BIT(channel)) = (uint8)ch;
107
}
108
 
109
 
110
#else /* UART_SUPPORT_TYPE==UART_PSC */
111
 
112
#if UART_SUPPORT_TYPE == UART_5407
113
/********************************************************************/
114
/*
115
 * 5407 derivative doesn't have macros to access URB/UTB by channel number
116
 * because they have different sizes for UART0 & UART1
117
 * But in UART mode only 8 bits of UART1 URB/UTB is used, so define these macros here
118
 *  if they doesn't defined before
119
 */
120
#ifndef MCF_UART_URB
121
#define MCF_UART_URB(x)                      (*(vuint8 *)(&__MBAR[0x1CC + ((x)*0x40)]))
122
#endif /* MCF_UART_URB */
123
 
124
#ifndef MCF_UART_UTB
125
#define MCF_UART_UTB(x)                      (*(vuint8 *)(&__MBAR[0x1CC + ((x)*0x40)]))
126
#endif /* MCF_UART_UTB */
127
 
128
#endif /* UART_SUPPORT_TYPE == UART_5407 */
129
 
130
void uart_init(int channel, unsigned long systemClockKHz, unsigned long baudRate)
131
{
132
        /*
133
         * Initialize UART for serial communications
134
         */
135
 
136
        register uint16 ubgs;
137
 
138
#if UART_SUPPORT_TYPE==UART_54451
139
        uint32 vco;
140
        uint32 divider;
141
        uint32 bus_clk;
142
 
143
        divider = ((MCF_CLOCK_PCR & 0x000000F0) >> 4) + 1;
144
        vco = ((MCF_CLOCK_PCR >> 24) * systemClockKHz * 1000);
145
        bus_clk = (vco / divider);
146
#endif
147
        /*
148
         * Reset Transmitter
149
         */
150
        MCF_UART_UCR(channel) = MCF_UART_UCR_RESET_TX;
151
 
152
        /*
153
         * Reset Receiver
154
         */
155
        MCF_UART_UCR(channel) = MCF_UART_UCR_RESET_RX;
156
 
157
        /*
158
         * Reset Mode Register
159
         */
160
        MCF_UART_UCR(channel) = MCF_UART_UCR_RESET_MR;
161
 
162
        /*
163
         * No parity, 8-bits per character
164
         */
165
        MCF_UART_UMR(channel) = (0
166
                | MCF_UART_UMR_PM_NONE
167
                | MCF_UART_UMR_BC_8 );
168
 
169
        /*
170
         * No echo or loopback, 1 stop bit
171
         */
172
        MCF_UART_UMR(channel) = (0
173
                | MCF_UART_UMR_CM_NORMAL
174
                | MCF_UART_UMR_SB_STOP_BITS_1);
175
 
176
        /*
177
         * Set Rx and Tx baud by SYSTEM CLOCK
178
         */
179
        MCF_UART_UCSR(channel) = (0
180
                | MCF_UART_UCSR_RCS_SYS_CLK
181
                | MCF_UART_UCSR_TCS_SYS_CLK);
182
 
183
        /*
184
         * Mask all UART interrupts
185
         */
186
        MCF_UART_UIMR(channel) = 0;
187
 
188
        /*
189
         * Calculate baud settings
190
         */
191
#if UART_SUPPORT_TYPE==UART_54451
192
        ubgs = (uint16)(((bus_clk >> 5) + (baudRate >> 1)) / baudRate);
193
#else
194
        ubgs = (uint16)((systemClockKHz * 1000)/(baudRate * 32));
195
#endif
196
 
197
#if UART_SUPPORT_TYPE==UART_DIVIDER || UART_SUPPORT_TYPE == UART_5407
198
        MCF_UART_UDU(channel) = (uint8)((ubgs & 0xFF00) >> 8);
199
        MCF_UART_UDL(channel) = (uint8)(ubgs & 0x00FF);
200
#else /* UART_SUPPORT_TYPE!=UART_DIVIDER */
201
        MCF_UART_UBG1(channel) = (uint8)((ubgs & 0xFF00) >> 8);
202
        MCF_UART_UBG2(channel) = (uint8)(ubgs & 0x00FF);
203
#endif /* UART_SUPPORT_TYPE==UART_DIVIDER */
204
 
205
        /*
206
         * Enable receiver and transmitter
207
         */
208
        MCF_UART_UCR(channel) = (0
209
                | MCF_UART_UCR_TX_ENABLED
210
                | MCF_UART_UCR_RX_ENABLED);
211
}
212
 
213
/********************************************************************/
214
/*
215
 * Wait for a character to be received on the specified UART
216
 *
217
 * Return Values:
218
 *  the received character
219
 */
220
char uart_getchar (int channel)
221
{
222
    /* Wait until character has been received */
223
    while (!(MCF_UART_USR(channel) & MCF_UART_USR_RXRDY))
224
    {
225
 
226
    };
227
 
228
    return (char)MCF_UART_URB(channel);
229
}
230
 
231
/********************************************************************/
232
/*
233
 * Wait for space in the UART Tx FIFO and then send a character
234
 */
235
void uart_putchar (int channel, char ch)
236
{
237
    /* Wait until space is available in the FIFO */
238
    while (!(MCF_UART_USR(channel) & MCF_UART_USR_TXRDY))
239
    {
240
 
241
    };
242
 
243
    /* Send the character */
244
    MCF_UART_UTB(channel) = (uint8)ch;
245
}
246
 
247
#endif /* UART_SUPPORT_TYPE==UART_PSC */
248
/********************************************************************/
249
 
250
/********************************************************************/
251
/** <UART.h> Neeeded functions                                     **/
252
/********************************************************************/
253
 
254
/****************************************************************************/
255
/*
256
 * Implementation for CodeWarror MSL interface to serial device (UART.h).
257
 * Needed for printf, etc...
258
 * Only InitializeUART, ReadUARTN, and WriteUARTN are implemented.
259
 *
260
 */
261
UARTError InitializeUART(UARTBaudRate baudRate)
262
{
263
#if UART_SUPPORT_TYPE==UART_54451
264
        baudRate = kBaud115200;
265
#endif
266
        uart_init(TERMINAL_PORT, SYSTEM_CLOCK_KHZ, baudRate);
267
        return kUARTNoError;
268
}
269
 
270
/****************************************************************************/
271
/*
272
        ReadUARTN
273
 
274
        Read N bytes from the UART.
275
 
276
        bytes                   pointer to result buffer
277
        limit                   size of buffer and # of bytes to read
278
*/
279
/****************************************************************************/
280
UARTError ReadUARTN(void* bytes, unsigned long limit)
281
{
282
        int count;
283
        for (count = 0; count < limit; count++) {
284
        *( (char *)bytes + count ) = uart_getchar(TERMINAL_PORT);
285
        }
286
        return kUARTNoError;
287
}
288
 
289
/****************************************************************************/
290
UARTError WriteUARTN(const void* bytes, unsigned long length)
291
{
292
        int count;
293
        for (count = 0; count < length; count++) {
294
                uart_putchar(TERMINAL_PORT, *( ((char *)bytes) + count));
295
        }
296
        return kUARTNoError;
297
}
298
#endif /* ENABLE_UART_SUPPORT */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.