221d597939a54c569a95c9a15b361dfdc2f949c2
[fw/altos] / target / ee / ee.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 sfr at 0x80 P0;
22 sfr at 0x90 P1;
23 sfr at 0xA0 P2;
24 sfr at 0xC6 CLKCON;
25
26 sfr at 0xF1 PERCFG;
27 sfr at 0xF2 ADCCFG;
28 sfr at 0xF3 P0SEL;
29 sfr at 0xF4 P1SEL;
30 sfr at 0xF5 P2SEL;
31
32 sfr at 0xFD P0DIR;
33 sfr at 0xFE P1DIR;
34 sfr at 0xFF P2DIR;
35 sfr at 0x8F P0INP;
36 sfr at 0xF6 P1INP;
37 sfr at 0xF7 P2INP;
38
39 sfr at 0x89 P0IFG;
40 sfr at 0x8A P1IFG;
41 sfr at 0x8B P2IFG;
42
43 sbit at 0x90 P1_0;
44 sbit at 0x91 P1_1;
45 sbit at 0x92 P1_2;
46 sbit at 0x93 P1_3;
47 sbit at 0x94 P1_4;
48 sbit at 0x95 P1_5;
49 sbit at 0x96 P1_6;
50 sbit at 0x97 P1_7;
51
52 #define MOSI    P1_5
53 #define MISO    P1_4
54 #define SCK     P1_3
55 #define CS      P1_2
56
57 #define DEBUG   P1_1
58
59 #define nop()   _asm nop _endasm;
60
61 void
62 delay (unsigned char n)
63 {
64         unsigned char i = 0;
65         unsigned char j = 0;
66
67         while (--n != 0)
68                 while (--i != 0)
69                         while (--j != 0)
70                                 nop();
71 }
72
73 void
74 cs(uint8_t b)
75 {
76         CS = b;
77         delay(1);
78 }
79
80 void
81 out_bit(uint8_t b)
82 {
83         MOSI = b;
84         SCK = 1;
85         delay(1);
86         SCK = 0;
87         delay(1);
88 }
89
90 void
91 out_byte(uint8_t byte)
92 {
93         uint8_t s;
94
95         for (s = 0; s < 8; s++) {
96                 uint8_t b = (byte & 0x80) ? 1 : 0;
97                 out_bit(b);
98                 byte <<= 1;
99         }
100 }
101
102 uint8_t
103 in_bit(void)
104 {
105         uint8_t b;
106         SCK = 1;
107         delay(1);
108         b = MISO;
109         SCK = 0;
110         delay(1);
111         return b;
112 }
113
114 uint8_t
115 in_byte(void)
116 {
117         uint8_t byte = 0;
118         uint8_t s;
119         uint8_t b;
120
121         for (s = 0; s < 8; s++) {
122                 b = in_bit();
123                 byte = byte << 1;
124                 byte |= s;
125         }
126         return byte;
127 }
128
129 uint8_t
130 rdsr(void)
131 {
132         uint8_t status;
133         cs(0);
134         out_byte(0x05);
135         status = in_byte();
136         cs(1);
137         return status;
138 }
139
140 void
141 wrsr(uint8_t status)
142 {
143         cs(0);
144         out_byte(0x01);
145         out_byte(status);
146         cs(1);
147 }
148         
149 void
150 wren(void)
151 {
152         cs(0);
153         out_byte(0x06);
154         cs(1);
155 }
156
157 void
158 write(uint32_t addr, uint8_t *bytes, uint16_t len)
159 {
160         wren();
161         cs(0);
162         out_byte(0x02);
163         out_byte(addr >> 16);
164         out_byte(addr >> 8);
165         out_byte(addr);
166         while (len-- > 0)
167                 out_byte(*bytes++);
168         cs(1);
169         for (;;) {
170                 uint8_t status = rdsr();
171                 if ((status & (1 << 0)) == 0)
172                         break;
173         }
174 }
175
176 void
177 read(uint32_t addr, uint8_t *bytes, uint16_t len)
178 {
179         cs(0);
180         out_byte(0x03);
181         out_byte(addr >> 16);
182         out_byte(addr >> 8);
183         out_byte(addr);
184         while (len-- > 0)
185                 *bytes++ = in_byte();
186         cs(1);
187 }
188
189 void
190 debug_byte(uint8_t byte)
191 {
192         uint8_t s;
193
194         for (s = 0; s < 8; s++) {
195                 DEBUG = byte & 1;
196                 delay(2);
197                 byte >>= 1;
198         }
199 }
200
201 #define STRING  "hi"
202 #define LENGTH  2
203
204 main ()
205 {
206         uint8_t status;
207         uint8_t buf[LENGTH];
208         int i;
209
210         CLKCON = 0;
211         
212         CS = 1;
213         SCK = 0;
214         P1DIR = ((1 << 5) |
215                  (0 << 4) |
216                  (1 << 3) |
217                  (1 << 2) |
218                  (1 << 1));
219         status = rdsr();
220         /*
221          * Turn off both block-protect bits
222          */
223         status &= ~((1 << 3) | (1 << 2));
224         /*
225          * Turn off write protect enable
226          */
227         status &= ~(1 << 7);
228         wrsr(status);
229         write(0x0, STRING, LENGTH);
230         for (;;) {
231                 read(0x0, buf, LENGTH);
232                 for (i = 0; i < LENGTH; i++)
233                         debug_byte(buf[i]);
234         }
235 }