1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
/*
* cpu/mc9328/serial.c
*
* (c) Copyright 2004
* Techware Information Technology, Inc.
* http://www.techware.com.tw/
*
* Ming-Len Wu <minglen_wu@techware.com.tw>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*/
#include <common.h>
#include <mc9328.h>
#if defined(CONFIG_UART1)
/* GPIO PORT B */
#define reg_GIUS MX1_GIUS_C
#define reg_GPR MX1_GPR_B
#define GPIO_MASK 0xFFFFE1FF
#define UART_BASE 0x00206000
#elif defined (CONFIG_UART2)
/* GPIO PORT C */
#define reg_GIUS MX1_GIUS_C
#define reg_GPR MX1_GPR_C
#define GPIO_MASK 0x0FFFFFFF
#define UART_BASE 0x207000
#endif
#define reg_URXD (*((volatile u32 *)(UART_BASE+0x00)))
#define reg_UTXD (*((volatile u32 *)(UART_BASE+0x40)))
#define reg_UCR1 (*((volatile u32 *)(UART_BASE+0x80)))
#define reg_UCR2 (*((volatile u32 *)(UART_BASE+0x84)))
#define reg_UCR3 (*((volatile u32 *)(UART_BASE+0x88)))
#define reg_UCR4 (*((volatile u32 *)(UART_BASE+0x8C)))
#define reg_UFCR (*((volatile u32 *)(UART_BASE+0x90)))
#define reg_USR1 (*((volatile u32 *)(UART_BASE+0x94)))
#define reg_USR2 (*((volatile u32 *)(UART_BASE+0x98)))
#define reg_UESC (*((volatile u32 *)(UART_BASE+0x9C)))
#define reg_UTIM (*((volatile u32 *)(UART_BASE+0xA0)))
#define reg_UBIR (*((volatile u32 *)(UART_BASE+0xA4)))
#define reg_UBMR (*((volatile u32 *)(UART_BASE+0xA8)))
#define reg_UBRC (*((volatile u32 *)(UART_BASE+0xAC)))
#define TXFE_MASK 0x4000 /* Tx buffer empty */
#define RDR_MASK 0x0001 /* receive data ready */
void serial_setbrg (void) {
/* config I/O pins for UART */
reg_GIUS &= GPIO_MASK;
reg_GPR &= GPIO_MASK;
/* config UART */
reg_UCR1 = 5;
reg_UCR2 = 0x4027;
reg_UCR4 = 1;
reg_UFCR = 0xA81;
reg_UBIR = 0xF;
reg_UBMR = 0x8A;
reg_UBRC = 8;
}
/*
* Initialise the serial port with the given baudrate. The settings
* are always 8 data bits, no parity, 1 stop bit, no start bits.
*
*/
int serial_init (void) {
serial_setbrg ();
return (0);
}
/*
* Read a single byte from the serial port. Returns 1 on success, 0
* otherwise. When the function is succesfull, the character read is
* written into its argument c.
*/
int serial_getc (void) {
while (!(reg_USR2 & RDR_MASK)) ; /* wait until RDR bit set */
return (u8)reg_URXD;
}
/*
* Output a single byte to the serial port.
*/
void serial_putc (const char c) {
while (!(reg_USR2 & TXFE_MASK)); /* wait until TXFE bit set */
reg_UTXD = (u16) c;
if (c == '\n') { /* carriage return ? append line-feed */
while (!(reg_USR2 & TXFE_MASK)); /* wait until TXFE bit set */
reg_UTXD = '\r';
}
}
/*
* Test whether a character is in the RX buffer
*/
int serial_tstc (void) {
return reg_USR2 & RDR_MASK;
}
void serial_puts (const char *s) {
while (*s) {
serial_putc (*s++);
}
}
|