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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
|
/*
* Copyright © 2008 Keith Packard <keithp@keithp.com>
*
* 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 <stdint.h>
/*
* Validate the SPI-connected EEPROM
*/
sfr at 0x80 P0;
sfr at 0x90 P1;
sfr at 0xA0 P2;
sfr at 0xC6 CLKCON;
sfr at 0xbe SLEEP;
# define SLEEP_USB_EN (1 << 7)
# define SLEEP_XOSC_STB (1 << 6)
sfr at 0xF1 PERCFG;
#define PERCFG_T1CFG_ALT_1 (0 << 6)
#define PERCFG_T1CFG_ALT_2 (1 << 6)
#define PERCFG_T3CFG_ALT_1 (0 << 5)
#define PERCFG_T3CFG_ALT_2 (1 << 5)
#define PERCFG_T4CFG_ALT_1 (0 << 4)
#define PERCFG_T4CFG_ALT_2 (1 << 4)
#define PERCFG_U1CFG_ALT_1 (0 << 1)
#define PERCFG_U1CFG_ALT_2 (1 << 1)
#define PERCFG_U0CFG_ALT_1 (0 << 0)
#define PERCFG_U0CFG_ALT_2 (1 << 0)
sfr at 0xF2 ADCCFG;
sfr at 0xF3 P0SEL;
sfr at 0xF4 P1SEL;
sfr at 0xF5 P2SEL;
sfr at 0xFD P0DIR;
sfr at 0xFE P1DIR;
sfr at 0xFF P2DIR;
sfr at 0x8F P0INP;
sfr at 0xF6 P1INP;
sfr at 0xF7 P2INP;
sfr at 0x89 P0IFG;
sfr at 0x8A P1IFG;
sfr at 0x8B P2IFG;
sbit at 0x90 P1_0;
sbit at 0x91 P1_1;
sbit at 0x92 P1_2;
sbit at 0x93 P1_3;
sbit at 0x94 P1_4;
sbit at 0x95 P1_5;
sbit at 0x96 P1_6;
sbit at 0x97 P1_7;
/*
* UART registers
*/
sfr at 0x86 U0CSR;
sfr at 0xF8 U1CSR;
# define UxCSR_MODE_UART (1 << 7)
# define UxCSR_MODE_SPI (0 << 7)
# define UxCSR_RE (1 << 6)
# define UxCSR_SLAVE (1 << 5)
# define UxCSR_MASTER (0 << 5)
# define UxCSR_FE (1 << 4)
# define UxCSR_ERR (1 << 3)
# define UxCSR_RX_BYTE (1 << 2)
# define UxCSR_TX_BYTE (1 << 1)
# define UxCSR_ACTIVE (1 << 0)
sfr at 0xc4 U0UCR;
sfr at 0xfb U1UCR;
sfr at 0xc5 U0GCR;
sfr at 0xfc U1GCR;
# define UxGCR_CPOL_NEGATIVE (0 << 7)
# define UxGCR_CPOL_POSITIVE (1 << 7)
# define UxGCR_CPHA_FIRST_EDGE (0 << 6)
# define UxGCR_CPHA_SECOND_EDGE (1 << 6)
# define UxGCR_ORDER_LSB (0 << 5)
# define UxGCR_ORDER_MSB (1 << 5)
# define UxGCR_BAUD_E_MASK (0x1f)
# define UxGCR_BAUD_E_SHIFT 0
sfr at 0xc1 U0DBUF;
sfr at 0xf9 U1DBUF;
sfr at 0xc2 U0BAUD;
sfr at 0xfa U1BAUD;
#define MOSI P1_5
#define MISO P1_4
#define SCK P1_3
#define CS P1_2
#define DEBUG P1_1
#define BITBANG 0
#define USART 1
#define nop() _asm nop _endasm;
void
delay (unsigned char n)
{
unsigned char i = 0;
unsigned char j = 0;
while (--n != 0)
while (--i != 0)
while (--j != 0)
nop();
}
#if BITBANG
/*
* This version directly manipulates the GPIOs to synthesize SPI
*/
void
bitbang_cs(uint8_t b)
{
SCK = 0;
CS = b;
delay(1);
}
void
bitbang_out_bit(uint8_t b)
{
MOSI = b;
delay(1);
SCK = 1;
delay(1);
SCK = 0;
}
void
bitbang_out_byte(uint8_t byte)
{
uint8_t s;
for (s = 0; s < 8; s++) {
uint8_t b = (byte & 0x80) ? 1 : 0;
bitbang_out_bit(b);
byte <<= 1;
}
}
uint8_t
bitbang_in_bit(void)
{
uint8_t b;
delay(1);
SCK = 1;
delay(1);
b = MISO;
SCK = 0;
return b;
}
uint8_t
bitbang_in_byte(void)
{
uint8_t byte = 0;
uint8_t s;
uint8_t b;
for (s = 0; s < 8; s++) {
b = bitbang_in_bit();
byte = byte << 1;
byte |= b;
}
return byte;
}
void
bit_bang_init(void)
{
CS = 1;
SCK = 0;
P1DIR = ((1 << 5) |
(0 << 4) |
(1 << 3) |
(1 << 2) |
(1 << 1));
}
#define spi_init() bitbang_init()
#define spi_out_byte(b) bitbang_out_byte(b)
#define spi_in_byte() bitbang_in_byte()
#define spi_cs(b) bitbang_cs(b)
#endif
#if USART
/*
* This version uses the USART in SPI mode
*/
void
usart_init(void)
{
/*
* Configure our chip select line
*/
CS = 1;
P1DIR |= (1 << 2);
/*
* Configure the peripheral pin choices
* for both of the serial ports
*
* Note that telemetrum will use U1CFG_ALT_2
* but that overlaps with SPI ALT_2, so until
* we can test that this works, we'll set this
* to ALT_1
*/
PERCFG = (PERCFG_U1CFG_ALT_1 |
PERCFG_U0CFG_ALT_2);
/*
* Make the SPI pins controlled by the SPI
* hardware
*/
P1SEL |= ((1 << 5) | (1 << 4) | (1 << 3));
/*
* SPI in master mode
*/
U0CSR = (UxCSR_MODE_SPI |
UxCSR_MASTER);
/*
* The cc1111 is limited to a 24/8 MHz SPI clock,
* while the 25LC1024 is limited to 20MHz. So,
* use the 3MHz clock (BAUD_E 17, BAUD_M 0)
*/
U0BAUD = 0;
U0GCR = (UxGCR_CPOL_NEGATIVE |
UxGCR_CPHA_FIRST_EDGE |
UxGCR_ORDER_MSB |
(17 << UxGCR_BAUD_E_SHIFT));
}
void
usart_cs(uint8_t b)
{
CS = b;
}
uint8_t
usart_in_out(uint8_t byte)
{
U0DBUF = byte;
while ((U0CSR & UxCSR_TX_BYTE) == 0)
;
U0CSR &= ~UxCSR_TX_BYTE;
return U0DBUF;
}
void
usart_out_byte(uint8_t byte)
{
(void) usart_in_out(byte);
}
uint8_t
usart_in_byte(void)
{
return usart_in_out(0xff);
}
#define spi_init() usart_init()
#define spi_out_byte(b) usart_out_byte(b)
#define spi_in_byte() usart_in_byte()
#define spi_cs(b) usart_cs(b)
#endif
uint8_t
rdsr(void)
{
uint8_t status;
spi_cs(0);
spi_out_byte(0x05);
status = spi_in_byte();
spi_cs(1);
return status;
}
void
wrsr(uint8_t status)
{
spi_cs(0);
spi_out_byte(0x01);
spi_out_byte(status);
spi_cs(1);
}
void
wren(void)
{
spi_cs(0);
spi_out_byte(0x06);
spi_cs(1);
}
void
write(uint32_t addr, uint8_t *bytes, uint16_t len)
{
wren();
spi_cs(0);
spi_out_byte(0x02);
spi_out_byte(addr >> 16);
spi_out_byte(addr >> 8);
spi_out_byte(addr);
while (len-- > 0)
spi_out_byte(*bytes++);
spi_cs(1);
for (;;) {
uint8_t status = rdsr();
if ((status & (1 << 0)) == 0)
break;
}
}
void
read(uint32_t addr, uint8_t *bytes, uint16_t len)
{
spi_cs(0);
spi_out_byte(0x03);
spi_out_byte(addr >> 16);
spi_out_byte(addr >> 8);
spi_out_byte(addr);
while (len-- > 0)
*bytes++ = spi_in_byte();
spi_cs(1);
}
void
debug_byte(uint8_t byte)
{
uint8_t s;
for (s = 0; s < 8; s++) {
DEBUG = byte & 1;
delay(5);
byte >>= 1;
}
}
#define STRING "\360\252"
#define LENGTH 2
main ()
{
uint8_t status;
uint8_t buf[LENGTH];
int i;
P1DIR |= 2;
CLKCON = 0;
while (!(SLEEP & SLEEP_XOSC_STB))
;
spi_init();
status = rdsr();
/*
* Turn off both block-protect bits
*/
status &= ~((1 << 3) | (1 << 2));
/*
* Turn off write protect enable
*/
status &= ~(1 << 7);
wrsr(status);
write(0x0, STRING, LENGTH);
for (;;) {
read(0x0, buf, LENGTH);
for (i = 0; i < LENGTH; i++)
debug_byte(buf[i]);
}
}
|