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.
19 #include "ao_product.h"
21 static struct ao_companion_command ao_companion_command;
22 static const struct ao_companion_setup ao_companion_setup = {
23 .board_id = AO_idProduct_NUMBER,
24 .board_id_inverse = ~AO_idProduct_NUMBER,
29 static void ao_spi_slave_recv(void)
34 len = sizeof (ao_companion_command);
35 buf = (uint8_t *) &ao_companion_command;
37 while (!(SPSR & (1 << SPIF)))
42 /* Figure out the outbound data */
43 switch (ao_companion_command.command) {
44 case AO_COMPANION_SETUP:
45 buf = (uint8_t *) &ao_companion_setup;
46 len = sizeof (ao_companion_setup);
48 case AO_COMPANION_FETCH:
49 buf = (uint8_t *) &ao_adc_ring[ao_adc_ring_prev(ao_adc_head)].adc;
50 len = NUM_ADC * sizeof (uint16_t);
56 /* Send the outbound data */
59 while (!(SPSR & (1 << SPIF)))
65 static uint8_t ao_spi_slave_running;
69 if ((PINB & (1 << PINB0)) == 0) {
70 if (!(PCMSK0 & (1 << PCINT1)))
71 PCMSK0 |= (1 << PCINT1);
73 PCMSK0 &= ~(1 << PCINT1);
75 if (!ao_spi_slave_running) {
76 ao_spi_slave_running = 1;
82 ao_spi_slave_running = 0;
86 void ao_spi_slave_debug(void) {
87 printf ("slave running %d\n", ao_spi_slave_running);
91 ao_spi_slave_init(void)
93 PCMSK0 |= (1 << PCINT0); /* Enable PCINT0 pin change */
94 PCICR |= (1 << PCIE0); /* Enable pin change interrupt */
96 DDRB = (DDRB & 0xf0) | (1 << 3);
97 SPCR = (0 << SPIE) | /* Disable SPI interrupts */
98 (1 << SPE) | /* Enable SPI */
99 (0 << DORD) | /* MSB first */
100 (0 << MSTR) | /* Slave mode */
101 (0 << CPOL) | /* Clock low when idle */
102 (0 << CPHA); /* Sample at leading clock edge */