altos-avr: Store TM tick and state in log, get logging on/off working
[fw/altos] / src-avr / ao_spi_slave.c
1 /*
2  * Copyright © 2011 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 #include "ao.h"
19 #include "ao_product.h"
20
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,
25         .update_period          = 50,
26         .channels               = NUM_ADC
27 };
28
29 static uint8_t ao_spi_slave_recv(void)
30 {
31         uint8_t *buf;
32         uint8_t len;
33
34         len = sizeof (ao_companion_command);
35         buf = (uint8_t *) &ao_companion_command;
36         while (len--) {
37                 while (!(SPSR & (1 << SPIF)))
38                         if ((PINB & (1 << PINB0)))
39                                 return 0;
40                 *buf++ = SPDR;
41         }
42
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);
48                 break;
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);
52                 break;
53         case AO_COMPANION_STATE:
54                 break;
55         default:
56                 return 0;
57         }
58
59         if (len) {
60                 /* Send the outbound data */
61                 while (len--) {
62                         SPDR = *buf++;
63                         while (!(SPSR & (1 << SPIF)))
64                                 if ((PINB & (1 << PINB0)))
65                                         return 0;
66                 }
67                 (void) SPDR;
68         }
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;
72                 return 1;
73         }
74         return 0;
75 }
76
77 static uint8_t ao_spi_slave_running;
78
79 ISR(PCINT0_vect)
80 {
81         if ((PINB & (1 << PINB0)) == 0) {
82                 if (!ao_spi_slave_running) {
83                         uint8_t changed;
84                         ao_spi_slave_running = 1;
85                         cli();
86                         changed = ao_spi_slave_recv();
87                         sei();
88                         if (changed && ao_flight_boost <= ao_log_store.tm_state) {
89                                 if (ao_log_store.tm_state < ao_flight_landed)
90                                         ao_log_start();
91                                 else
92                                         ao_log_stop();
93                         }
94                 }
95         } else {
96                 ao_spi_slave_running = 0;
97         }
98 }
99
100 void ao_spi_slave_debug(void) {
101         printf ("slave running %d\n", ao_spi_slave_running);
102 }
103
104 void
105 ao_spi_slave_init(void)
106 {
107         PCMSK0 |= (1 << PCINT0);        /* Enable PCINT0 pin change */
108         PCICR |= (1 << PCIE0);          /* Enable pin change interrupt */
109
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 */
117 }