Merge branch 'master' of ssh://git.gag.com/scm/git/fw/altos
[fw/altos] / src / drivers / ao_pyro_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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include <ao.h>
20 #include <ao_product.h>
21 #include <ao_companion.h>
22 #include <ao_flight.h>
23 #include <ao_pyro.h>
24
25 struct ao_companion_command     ao_companion_command;
26
27 static const struct ao_companion_setup  ao_telepyro_setup = {
28         .board_id               = AO_idProduct_NUMBER,
29         .board_id_inverse       = ~AO_idProduct_NUMBER,
30         .update_period          = 50,
31         .channels               = AO_TELEPYRO_NUM_ADC,
32 };
33
34 struct ao_config ao_config;
35
36 extern volatile uint16_t ao_tick_count;
37 uint16_t ao_boost_tick;
38
39 void ao_spi_slave(void)
40 {
41         if (!ao_spi_slave_recv((uint8_t *) &ao_companion_command,
42                                sizeof (ao_companion_command)))
43                 return;
44
45         /* Figure out the outbound data */
46         switch (ao_companion_command.command) {
47         case AO_COMPANION_SETUP:
48                 ao_spi_slave_send((uint8_t *) &ao_telepyro_setup,
49                                   sizeof (ao_telepyro_setup));
50                 break;
51         case AO_COMPANION_FETCH:
52                 ao_spi_slave_send((uint8_t *) &ao_data_ring[ao_data_ring_prev(ao_data_head)].adc.adc,
53                                   AO_TELEPYRO_NUM_ADC * sizeof (uint16_t));
54                 break;
55         case AO_COMPANION_NOTIFY:
56                 /* Can't use ao_time because we have interrupts suspended */
57                 if (ao_companion_command.flight_state < ao_flight_boost && ao_companion_command.flight_state >= ao_flight_boost)
58                         ao_boost_tick = ao_tick_count;
59                 ao_wakeup(&ao_pyro_wakeup);
60                 break;
61         default:
62                 return;
63         }
64 }