589452c0dbb730ba90eed438f52bf5ad60c76b5f
[fw/altos] / target / serial / serial.c
1 /*
2  * Copyright © 2008 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include <stdint.h>
20
21 /*
22  * Validate UART1
23  */
24
25 sfr at 0x80 P0;
26 sfr at 0x90 P1;
27 sfr at 0xA0 P2;
28 sfr at 0xC6 CLKCON;
29 sfr at 0xbe SLEEP;
30
31 # define SLEEP_USB_EN           (1 << 7)
32 # define SLEEP_XOSC_STB         (1 << 6)
33
34 sfr at 0xF1 PERCFG;
35 #define PERCFG_T1CFG_ALT_1      (0 << 6)
36 #define PERCFG_T1CFG_ALT_2      (1 << 6)
37
38 #define PERCFG_T3CFG_ALT_1      (0 << 5)
39 #define PERCFG_T3CFG_ALT_2      (1 << 5)
40
41 #define PERCFG_T4CFG_ALT_1      (0 << 4)
42 #define PERCFG_T4CFG_ALT_2      (1 << 4)
43
44 #define PERCFG_U1CFG_ALT_1      (0 << 1)
45 #define PERCFG_U1CFG_ALT_2      (1 << 1)
46
47 #define PERCFG_U0CFG_ALT_1      (0 << 0)
48 #define PERCFG_U0CFG_ALT_2      (1 << 0)
49
50 sfr at 0xF2 ADCCFG;
51 sfr at 0xF3 P0SEL;
52 sfr at 0xF4 P1SEL;
53 sfr at 0xF5 P2SEL;
54
55 sfr at 0xFD P0DIR;
56 sfr at 0xFE P1DIR;
57 sfr at 0xFF P2DIR;
58 sfr at 0x8F P0INP;
59 sfr at 0xF6 P1INP;
60 sfr at 0xF7 P2INP;
61
62 sfr at 0x89 P0IFG;
63 sfr at 0x8A P1IFG;
64 sfr at 0x8B P2IFG;
65
66 sbit at 0x90 P1_0;
67 sbit at 0x91 P1_1;
68 sbit at 0x92 P1_2;
69 sbit at 0x93 P1_3;
70 sbit at 0x94 P1_4;
71 sbit at 0x95 P1_5;
72 sbit at 0x96 P1_6;
73 sbit at 0x97 P1_7;
74
75 /*
76  * UART registers
77  */
78
79 sfr at 0x86 U0CSR;
80 sfr at 0xF8 U1CSR;
81
82 # define UxCSR_MODE_UART                (1 << 7)
83 # define UxCSR_MODE_SPI                 (0 << 7)
84 # define UxCSR_RE                       (1 << 6)
85 # define UxCSR_SLAVE                    (1 << 5)
86 # define UxCSR_MASTER                   (0 << 5)
87 # define UxCSR_FE                       (1 << 4)
88 # define UxCSR_ERR                      (1 << 3)
89 # define UxCSR_RX_BYTE                  (1 << 2)
90 # define UxCSR_TX_BYTE                  (1 << 1)
91 # define UxCSR_ACTIVE                   (1 << 0)
92
93 sfr at 0xc4 U0UCR;
94 sfr at 0xfb U1UCR;
95
96 # define UxUCR_FLUSH                    (1 << 7)
97 # define UxUCR_FLOW_DISABLE             (0 << 6)
98 # define UxUCR_FLOW_ENABLE              (1 << 6)
99 # define UxUCR_D9_EVEN_PARITY           (0 << 5)
100 # define UxUCR_D9_ODD_PARITY            (1 << 5)
101 # define UxUCR_BIT9_8_BITS              (0 << 4)
102 # define UxUCR_BIT9_9_BITS              (1 << 4)
103 # define UxUCR_PARITY_DISABLE           (0 << 3)
104 # define UxUCR_PARITY_ENABLE            (1 << 3)
105 # define UxUCR_SPB_1_STOP_BIT           (0 << 2)
106 # define UxUCR_SPB_2_STOP_BITS          (1 << 2)
107 # define UxUCR_STOP_LOW                 (0 << 1)
108 # define UxUCR_STOP_HIGH                (1 << 1)
109 # define UxUCR_START_LOW                (0 << 0)
110 # define UxUCR_START_HIGH               (1 << 0)
111
112 sfr at 0xc5 U0GCR;
113 sfr at 0xfc U1GCR;
114
115 # define UxGCR_CPOL_NEGATIVE            (0 << 7)
116 # define UxGCR_CPOL_POSITIVE            (1 << 7)
117 # define UxGCR_CPHA_FIRST_EDGE          (0 << 6)
118 # define UxGCR_CPHA_SECOND_EDGE         (1 << 6)
119 # define UxGCR_ORDER_LSB                (0 << 5)
120 # define UxGCR_ORDER_MSB                (1 << 5)
121 # define UxGCR_BAUD_E_MASK              (0x1f)
122 # define UxGCR_BAUD_E_SHIFT             0
123
124 sfr at 0xc1 U0DBUF;
125 sfr at 0xf9 U1DBUF;
126 sfr at 0xc2 U0BAUD;
127 sfr at 0xfa U1BAUD;
128
129 #define MOSI    P1_5
130 #define MISO    P1_4
131 #define SCK     P1_3
132 #define CS      P1_2
133
134 #define DEBUG   P1_1
135
136 #define USART   1
137
138 #define nop()   _asm nop _endasm;
139
140 void
141 delay (unsigned char n)
142 {
143         unsigned char i = 0;
144         unsigned char j = 0;
145
146         while (--n != 0)
147                 while (--i != 0)
148                         while (--j != 0)
149                                 nop();
150 }
151
152 /*
153  * This version uses the USART in SPI mode
154  */
155 void
156 usart_init(void)
157 {
158         P1DIR |= (1 << 2);
159         /*
160          * Configure the peripheral pin choices
161          * for both of the serial ports
162          *
163          * Note that telemetrum will use U1CFG_ALT_2
164          * but that overlaps with SPI ALT_2, so until
165          * we can test that this works, we'll set this
166          * to ALT_1
167          */
168         PERCFG = (PERCFG_U1CFG_ALT_2 |
169                   PERCFG_U0CFG_ALT_1);
170
171         /*
172          * Make the UART pins controlled by the UART
173          * hardware
174          */
175         P1SEL |= ((1 << 6) | (1 << 7));
176
177         /*
178          * UART mode with the receiver enabled
179          */
180         U1CSR = (UxCSR_MODE_UART |
181                  UxCSR_RE);
182         /*
183          * Pick a 38.4kbaud rate
184          */
185         U1BAUD = 163;
186         U1GCR = 10 << UxGCR_BAUD_E_SHIFT;       /* 38400 */
187 //      U1GCR = 3 << UxGCR_BAUD_E_SHIFT;        /* 300 */
188         /*
189          * Reasonable serial parameters
190          */
191         U1UCR = (UxUCR_FLUSH |
192                  UxUCR_FLOW_DISABLE |
193                  UxUCR_D9_ODD_PARITY |
194                  UxUCR_BIT9_8_BITS |
195                  UxUCR_PARITY_DISABLE |
196                  UxUCR_SPB_2_STOP_BITS |
197                  UxUCR_STOP_HIGH |
198                  UxUCR_START_LOW);
199 }
200
201 void
202 usart_out_byte(uint8_t byte)
203 {
204         U1DBUF = byte;
205         while ((U1CSR & UxCSR_TX_BYTE) == 0)
206                 ;
207         U1CSR &= ~UxCSR_TX_BYTE;
208 }
209
210 uint8_t
211 usart_in_byte(void)
212 {
213         uint8_t b;
214         while ((U1CSR & UxCSR_RX_BYTE) == 0)
215                 ;
216         b = U1DBUF;
217         U1CSR &= ~UxCSR_RX_BYTE;
218         return b;
219 }
220
221 #define spi_init()      usart_init()
222 #define spi_out_byte(b) usart_out_byte(b)
223 #define spi_in_byte()   usart_in_byte()
224
225 static char string[] = "hello world\r\n";
226
227 main ()
228 {
229         uint8_t i;
230
231         P1DIR |= 2;
232         CLKCON = 0;
233         while (!(SLEEP & SLEEP_XOSC_STB))
234                 ;
235         
236         spi_init();
237
238         for (;;) {
239                 usart_out_byte('A');
240                 delay(5);
241                 P1 ^= 2;
242         }
243 }