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 struct ao_companion_command ao_companion_command;
23 static const struct ao_companion_setup ao_telescience_setup = {
24 .board_id = AO_idProduct_NUMBER,
25 .board_id_inverse = ~AO_idProduct_NUMBER,
27 .channels = AO_LOG_TELESCIENCE_NUM_ADC,
31 ao_spi_read(uint8_t *buf, uint8_t len)
34 while (!(SPSR & (1 << SPIF)))
35 if ((PINB & (1 << PINB0)))
43 ao_spi_write(uint8_t *buf, uint8_t len)
47 while (!(SPSR & (1 << SPIF)))
48 if ((PINB & (1 << PINB0)))
51 /* Clear pending SPIF bit by reading */
55 static uint8_t ao_spi_slave_recv(void)
57 if (!ao_spi_read((uint8_t *) &ao_companion_command,
58 sizeof (ao_companion_command)))
61 /* Figure out the outbound data */
62 switch (ao_companion_command.command) {
63 case AO_COMPANION_SETUP:
64 ao_spi_write((uint8_t *) &ao_telescience_setup,
65 sizeof (ao_telescience_setup));
67 case AO_COMPANION_FETCH:
68 ao_spi_write((uint8_t *) &ao_adc_ring[ao_adc_ring_prev(ao_adc_head)].adc,
69 AO_LOG_TELESCIENCE_NUM_ADC * sizeof (uint16_t));
71 case AO_COMPANION_NOTIFY:
77 ao_log_store.tm_tick = ao_companion_command.tick;
78 if (ao_log_store.tm_state != ao_companion_command.flight_state) {
79 ao_log_store.tm_state = ao_companion_command.flight_state;
85 static uint8_t ao_spi_slave_running;
89 if ((PINB & (1 << PINB0)) == 0) {
90 if (!ao_spi_slave_running) {
92 ao_spi_slave_running = 1;
94 changed = ao_spi_slave_recv();
96 if (changed && ao_flight_boost <= ao_log_store.tm_state) {
97 if (ao_log_store.tm_state < ao_flight_landed)
104 ao_spi_slave_running = 0;
108 void ao_spi_slave_debug(void) {
109 printf ("slave running %d\n", ao_spi_slave_running);
113 ao_spi_slave_init(void)
115 PCMSK0 |= (1 << PCINT0); /* Enable PCINT0 pin change */
116 PCICR |= (1 << PCIE0); /* Enable pin change interrupt */
118 DDRB = ((DDRB & 0xf0) |
119 (1 << 3) | /* MISO, output */
120 (0 << 2) | /* MOSI, input */
121 (0 << 1) | /* SCK, input */
122 (0 << 0)); /* SS, input */
124 /* We'd like to have a pull-up on SS so that disconnecting the
125 * TM would cause any SPI transaction to abort. However, when
126 * I tried that, SPI transactions would spontaneously abort,
127 * making me assume that we needed a less aggressive pull-up
128 * than is offered inside the AVR
130 PORTB = ((PORTB & 0xf0) |
131 (1 << 3) | /* MISO, output */
132 (0 << 2) | /* MOSI, no pull-up */
133 (0 << 1) | /* SCK, no pull-up */
134 (0 << 0)); /* SS, no pull-up */
136 SPCR = (0 << SPIE) | /* Disable SPI interrupts */
137 (1 << SPE) | /* Enable SPI */
138 (0 << DORD) | /* MSB first */
139 (0 << MSTR) | /* Slave mode */
140 (0 << CPOL) | /* Clock low when idle */
141 (0 << CPHA); /* Sample at leading clock edge */