2 * Copyright © 2008 Keith Packard <keithp@keithp.com>
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.
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.
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.
22 * Validate the SPI-connected EEPROM
31 #define PERCFG_T1CFG_ALT_1 (0 << 6)
32 #define PERCFG_T1CFG_ALT_2 (1 << 6)
34 #define PERCFG_T3CFG_ALT_1 (0 << 5)
35 #define PERCFG_T3CFG_ALT_2 (1 << 5)
37 #define PERCFG_T4CFG_ALT_1 (0 << 4)
38 #define PERCFG_T4CFG_ALT_2 (1 << 4)
40 #define PERCFG_U1CFG_ALT_1 (0 << 1)
41 #define PERCFG_U1CFG_ALT_2 (1 << 1)
43 #define PERCFG_U0CFG_ALT_1 (0 << 0)
44 #define PERCFG_U0CFG_ALT_2 (1 << 0)
78 # define UxCSR_MODE_UART (1 << 7)
79 # define UxCSR_MODE_SPI (0 << 7)
80 # define UxCSR_RE (1 << 6)
81 # define UxCSR_SLAVE (1 << 5)
82 # define UxCSR_MASTER (0 << 5)
83 # define UxCSR_FE (1 << 4)
84 # define UxCSR_ERR (1 << 3)
85 # define UxCSR_RX_BYTE (1 << 2)
86 # define UxCSR_TX_BYTE (1 << 1)
87 # define UxCSR_ACTIVE (1 << 0)
95 # define UxGCR_CPOL_NEGATIVE (0 << 7)
96 # define UxGCR_CPOL_POSITIVE (1 << 7)
97 # define UxGCR_CPHA_FIRST_EDGE (0 << 6)
98 # define UxGCR_CPHA_SECOND_EDGE (1 << 6)
99 # define UxGCR_ORDER_LSB (0 << 5)
100 # define UxGCR_ORDER_MSB (1 << 5)
101 # define UxGCR_BAUD_E_MASK (0x1f)
102 # define UxGCR_BAUD_E_SHIFT 0
119 #define nop() _asm nop _endasm;
122 delay (unsigned char n)
136 * This version directly manipulates the GPIOs to synthesize SPI
140 bitbang_cs(uint8_t b)
148 bitbang_out_bit(uint8_t b)
158 bitbang_out_byte(uint8_t byte)
162 for (s = 0; s < 8; s++) {
163 uint8_t b = (byte & 0x80) ? 1 : 0;
183 bitbang_in_byte(void)
189 for (s = 0; s < 8; s++) {
190 b = bitbang_in_bit();
209 #define spi_init() bitbang_init()
210 #define spi_out_byte(b) bitbang_out_byte(b)
211 #define spi_in_byte() bitbang_in_byte()
212 #define spi_cs(b) bitbang_cs(b)
218 * This version uses the USART in SPI mode
224 * Configure our chip select line
229 * Configure the peripheral pin choices
230 * for both of the serial ports
232 * Note that telemetrum will use U1CFG_ALT_2
233 * but that overlaps with SPI ALT_2, so until
234 * we can test that this works, we'll set this
237 PERCFG = (PERCFG_U1CFG_ALT_1 |
241 * Make the SPI pins controlled by the SPI
244 P1SEL |= ((1 << 5) | (1 << 4) | (1 << 3));
249 U0CSR = (UxCSR_MODE_SPI |
253 * The cc1111 is limited to a 24/8 MHz SPI clock,
254 * while the 25LC1024 is limited to 20MHz. So,
255 * use the 3MHz clock (BAUD_E 17, BAUD_M 0)
258 U0GCR = (UxGCR_CPOL_NEGATIVE |
259 UxGCR_CPHA_FIRST_EDGE |
261 (17 << UxGCR_BAUD_E_SHIFT));
271 usart_in_out(uint8_t byte)
274 while ((U0CSR & UxCSR_TX_BYTE) == 0)
276 U0CSR &= ~UxCSR_TX_BYTE;
280 usart_out_byte(uint8_t byte)
282 (void) usart_in_out(byte);
288 return usart_in_out(0xff);
291 #define spi_init() usart_init()
292 #define spi_out_byte(b) usart_out_byte(b)
293 #define spi_in_byte() usart_in_byte()
294 #define spi_cs(b) usart_cs(b)
304 status = spi_in_byte();
314 spi_out_byte(status);
327 write(uint32_t addr, uint8_t *bytes, uint16_t len)
332 spi_out_byte(addr >> 16);
333 spi_out_byte(addr >> 8);
336 spi_out_byte(*bytes++);
339 uint8_t status = rdsr();
340 if ((status & (1 << 0)) == 0)
346 read(uint32_t addr, uint8_t *bytes, uint16_t len)
350 spi_out_byte(addr >> 16);
351 spi_out_byte(addr >> 8);
354 *bytes++ = spi_in_byte();
359 debug_byte(uint8_t byte)
363 for (s = 0; s < 8; s++) {
370 #define STRING "\360\252"
386 * Turn off both block-protect bits
388 status &= ~((1 << 3) | (1 << 2));
390 * Turn off write protect enable
394 write(0x0, STRING, LENGTH);
396 read(0x0, buf, LENGTH);
397 for (i = 0; i < LENGTH; i++)