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 uint8_t ao_spi_slave_recv(void)
34 len = sizeof (ao_companion_command);
35 buf = (uint8_t *) &ao_companion_command;
37 while (!(SPSR & (1 << SPIF)))
38 if ((PINB & (1 << PINB0)))
43 /* Figure out the outbound data */
44 switch (ao_companion_command.command) {
45 case AO_COMPANION_SETUP:
46 buf = (uint8_t *) &ao_companion_setup;
47 len = sizeof (ao_companion_setup);
49 case AO_COMPANION_FETCH:
50 buf = (uint8_t *) &ao_adc_ring[ao_adc_ring_prev(ao_adc_head)].adc;
51 len = NUM_ADC * sizeof (uint16_t);
53 case AO_COMPANION_STATE:
60 /* Send the outbound data */
63 while (!(SPSR & (1 << SPIF)))
64 if ((PINB & (1 << PINB0)))
69 ao_log_store.tm_tick = ao_companion_command.tick;
70 if (ao_log_store.tm_state != ao_companion_command.flight_state) {
71 ao_log_store.tm_state = ao_companion_command.flight_state;
77 static uint8_t ao_spi_slave_running;
81 if ((PINB & (1 << PINB0)) == 0) {
82 if (!ao_spi_slave_running) {
84 ao_spi_slave_running = 1;
86 changed = ao_spi_slave_recv();
88 if (changed && ao_flight_boost <= ao_log_store.tm_state) {
89 if (ao_log_store.tm_state < ao_flight_landed)
96 ao_spi_slave_running = 0;
100 void ao_spi_slave_debug(void) {
101 printf ("slave running %d\n", ao_spi_slave_running);
105 ao_spi_slave_init(void)
107 PCMSK0 |= (1 << PCINT0); /* Enable PCINT0 pin change */
108 PCICR |= (1 << PCIE0); /* Enable pin change interrupt */
110 DDRB = (DDRB & 0xf0) | (1 << 3);
111 SPCR = (0 << SPIE) | /* Disable SPI interrupts */
112 (1 << SPE) | /* Enable SPI */
113 (0 << DORD) | /* MSB first */
114 (0 << MSTR) | /* Slave mode */
115 (0 << CPOL) | /* Clock low when idle */
116 (0 << CPHA); /* Sample at leading clock edge */