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; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 * ATtiny USI as a SPI interface
25 * Transfer one byte in/out of the USI interface
29 #define SPI_USICR_WM ((0 << USIWM1) | (1 << USIWM0))
30 #define SPI_USICR_CS ((1 << USICS1) | (0 << USICS0) | (1 << USICLK))
32 #define SPI_USICR_FAST_2 ((1 << USIWM0) | (1 << USITC))
33 #define SPI_USICR_FAST_1 ((1 << USIWM0) | (1 << USITC) | (1 << USICLK))
37 ao_spi_transfer(uint8_t i)
39 /* Load data register */
43 USISR = (1 << USIOIF);
45 USICR = SPI_USICR_WM | SPI_USICR_CS | (1 << USITC);
46 } while ((USISR & (1 << USIOIF)) == 0);
49 USICR = SPI_USICR_FAST_1;
50 USICR = SPI_USICR_FAST_2;
52 USICR = SPI_USICR_FAST_1;
53 USICR = SPI_USICR_FAST_2;
55 USICR = SPI_USICR_FAST_1;
56 USICR = SPI_USICR_FAST_2;
58 USICR = SPI_USICR_FAST_1;
59 USICR = SPI_USICR_FAST_2;
61 USICR = SPI_USICR_FAST_1;
62 USICR = SPI_USICR_FAST_2;
64 USICR = SPI_USICR_FAST_1;
65 USICR = SPI_USICR_FAST_2;
67 USICR = SPI_USICR_FAST_1;
68 USICR = SPI_USICR_FAST_2;
70 USICR = SPI_USICR_FAST_1;
71 USICR = SPI_USICR_FAST_2;
74 /* Pull data from the port */
78 /* Send bytes over SPI.
80 * This just polls; the SPI is set to go as fast as possible,
81 * so using interrupts would take way too long
84 ao_spi_send_bus(void __xdata *block, uint16_t len) __reentrant
89 ao_spi_transfer (*d++);
92 /* Receive bytes over SPI.
94 * Poll, sending zeros and reading data back
97 ao_spi_recv_bus(void __xdata *block, uint16_t len) __reentrant
102 *d++ = ao_spi_transfer (0xff);
108 * Chip select is the responsibility of the caller
115 USICR = (1 << USIWM0) | (1 << USICS1) | (1 << USICLK);
117 USICR = SPI_USICR_FAST_2;
119 SPI_DIR &= ~(1 << DDB0); /* DI */
120 SPI_DIR |= (1 << DDB1); /* DO */
121 SPI_DIR |= (1 << DDB2); /* SCLK */