2 * Copyright © 2011 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 * ATtiny USI as a SPI interface
26 * Transfer one byte in/out of the USI interface
30 #define SPI_USICR_WM ((0 << USIWM1) | (1 << USIWM0))
31 #define SPI_USICR_CS ((1 << USICS1) | (0 << USICS0) | (1 << USICLK))
33 #define SPI_USICR_FAST_2 ((1 << USIWM0) | (1 << USITC))
34 #define SPI_USICR_FAST_1 ((1 << USIWM0) | (1 << USITC) | (1 << USICLK))
38 ao_spi_transfer(uint8_t i)
40 /* Load data register */
44 USISR = (1 << USIOIF);
46 USICR = SPI_USICR_WM | SPI_USICR_CS | (1 << USITC);
47 } while ((USISR & (1 << USIOIF)) == 0);
50 USICR = SPI_USICR_FAST_1;
51 USICR = SPI_USICR_FAST_2;
53 USICR = SPI_USICR_FAST_1;
54 USICR = SPI_USICR_FAST_2;
56 USICR = SPI_USICR_FAST_1;
57 USICR = SPI_USICR_FAST_2;
59 USICR = SPI_USICR_FAST_1;
60 USICR = SPI_USICR_FAST_2;
62 USICR = SPI_USICR_FAST_1;
63 USICR = SPI_USICR_FAST_2;
65 USICR = SPI_USICR_FAST_1;
66 USICR = SPI_USICR_FAST_2;
68 USICR = SPI_USICR_FAST_1;
69 USICR = SPI_USICR_FAST_2;
71 USICR = SPI_USICR_FAST_1;
72 USICR = SPI_USICR_FAST_2;
75 /* Pull data from the port */
79 /* Send bytes over SPI.
81 * This just polls; the SPI is set to go as fast as possible,
82 * so using interrupts would take way too long
85 ao_spi_send_bus(void *block, uint16_t len)
90 ao_spi_transfer (*d++);
93 /* Receive bytes over SPI.
95 * Poll, sending zeros and reading data back
98 ao_spi_recv_bus(void *block, uint16_t len)
103 *d++ = ao_spi_transfer (0xff);
109 * Chip select is the responsibility of the caller
116 USICR = (1 << USIWM0) | (1 << USICS1) | (1 << USICLK);
118 USICR = SPI_USICR_FAST_2;
120 SPI_DIR &= ~(1 << DDB0); /* DI */
121 SPI_DIR |= (1 << DDB1); /* DO */
122 SPI_DIR |= (1 << DDB2); /* SCLK */