altos: Share log code between telescience and telebt. Add telebt log
[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 struct ao_companion_command     ao_companion_command;
22
23 static const struct ao_companion_setup  ao_telescience_setup = {
24         .board_id               = AO_idProduct_NUMBER,
25         .board_id_inverse       = ~AO_idProduct_NUMBER,
26         .update_period          = 50,
27         .channels               = AO_LOG_TELESCIENCE_NUM_ADC,
28 };
29
30 static uint8_t
31 ao_spi_read(uint8_t *buf, uint8_t len)
32 {
33         while (len--) {
34                 while (!(SPSR & (1 << SPIF)))
35                         if ((PINB & (1 << PINB0)))
36                                 return 0;
37                 *buf++ = SPDR;
38         }
39         return 1;
40 }
41
42 static void
43 ao_spi_write(uint8_t *buf, uint8_t len)
44 {
45         while (len--) {
46                 SPDR = *buf++;
47                 while (!(SPSR & (1 << SPIF)))
48                         if ((PINB & (1 << PINB0)))
49                                 return;
50         }
51         /* Clear pending SPIF bit by reading */
52         (void) SPDR;
53 }
54
55 static uint8_t ao_spi_slave_recv(void)
56 {
57         if (!ao_spi_read((uint8_t *) &ao_companion_command,
58                          sizeof (ao_companion_command)))
59                 return 0;
60
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));
66                 break;
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));
70                 break;
71         case AO_COMPANION_NOTIFY:
72                 break;
73         default:
74                 return 0;
75         }
76
77         ao_log_single_write_data.telescience.tm_tick = ao_companion_command.tick;
78         if (ao_log_single_write_data.telescience.tm_state != ao_companion_command.flight_state) {
79                 ao_log_single_write_data.telescience.tm_state = ao_companion_command.flight_state;
80                 return 1;
81         }
82         return 0;
83 }
84
85 static uint8_t ao_spi_slave_running;
86
87 ISR(PCINT0_vect)
88 {
89         if ((PINB & (1 << PINB0)) == 0) {
90                 if (!ao_spi_slave_running) {
91                         uint8_t changed;
92                         ao_spi_slave_running = 1;
93                         cli();
94                         changed = ao_spi_slave_recv();
95                         sei();
96                         if (changed && ao_flight_boost <= ao_log_single_write_data.telescience.tm_state) {
97                                 if (ao_log_single_write_data.telescience.tm_state < ao_flight_landed)
98                                         ao_log_single_start();
99                                 else
100                                         ao_log_single_stop();
101                         }
102                 }
103         } else {
104                 ao_spi_slave_running = 0;
105         }
106 }
107
108 void ao_spi_slave_debug(void) {
109         printf ("slave running %d\n", ao_spi_slave_running);
110 }
111
112 void
113 ao_spi_slave_init(void)
114 {
115         PCMSK0 |= (1 << PCINT0);        /* Enable PCINT0 pin change */
116         PCICR |= (1 << PCIE0);          /* Enable pin change interrupt */
117
118         DDRB = ((DDRB & 0xf0) |
119                 (1 << 3) |              /* MISO, output */
120                 (0 << 2) |              /* MOSI, input */
121                 (0 << 1) |              /* SCK, input */
122                 (0 << 0));              /* SS, input */
123
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
129          */
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 */
135
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 */
142 }