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 * Atmega32u4 USART in MSPIM (master SPI mode)
24 __xdata uint8_t ao_spi_mutex;
26 /* Send bytes over SPI.
28 * This just polls; the SPI is set to go as fast as possible,
29 * so using interrupts would take way too long
32 ao_spi_send(void __xdata *block, uint16_t len) __reentrant
37 while (!(UCSR1A & (1 << UDRE1)));
39 while (!(UCSR1A & (1 << RXC1)));
44 /* Receive bytes over SPI.
46 * Poll, sending zeros and reading data back
49 ao_spi_recv(void __xdata *block, uint16_t len) __reentrant
53 /* Clear any pending data */
54 while (UCSR1A & (1 << RXC1))
58 while (!(UCSR1A & (1 << UDRE1)));
60 while (!(UCSR1A & (1 << RXC1)));
66 * Initialize USART0 for SPI using config alt 2
72 * Chip select is the responsibility of the caller
76 #define XCK1_PORT PORTD
79 #define XMS1_PORT PORTE
85 /* Ensure the USART is powered */
86 PRR1 &= ~(1 << PRUSART1);
91 XCK1_DDR |= (1 << XCK1);
93 /* Clear chip select (which is negated) */
94 XMS1_PORT |= (1 < XMS1);
95 XMS1_DDR |= (1 << XMS1);
97 /* Set baud register to zero (required before turning transmitter on) */
100 UCSR1C = ((0x3 << UMSEL10) | /* Master SPI mode */
101 (0 << UCSZ10) | /* SPI mode 0 */
102 (0 << UCPOL1)); /* SPI mode 0 */
104 /* Enable transmitter and receiver */
105 UCSR1B = ((1 << RXEN1) |
108 /* It says that 0 is a legal value; we'll see... */