8e01f586ed5d959d16485cc8d0e47371291903dd
[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 void 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                         ;
39                 *buf++ = SPDR;
40         }
41
42         /* Figure out the outbound data */
43         switch (ao_companion_command.command) {
44         case AO_COMPANION_SETUP:
45                 buf = (uint8_t *) &ao_companion_setup;
46                 len = sizeof (ao_companion_setup);
47                 break;
48         case AO_COMPANION_FETCH:
49                 buf = (uint8_t *) &ao_adc_ring[ao_adc_ring_prev(ao_adc_head)].adc;
50                 len = NUM_ADC * sizeof (uint16_t);
51                 break;
52         default:
53                 return;
54         }
55
56         /* Send the outbound data */
57         while (len--) {
58                 SPDR = *buf++;
59                 while (!(SPSR & (1 << SPIF)))
60                         ;
61         }
62         (void) SPDR;
63 }
64
65 static uint8_t ao_spi_slave_running;
66
67 ISR(PCINT0_vect)
68 {
69         if ((PINB & (1 << PINB0)) == 0) {
70                 if (!(PCMSK0 & (1 << PCINT1)))
71                         PCMSK0 |= (1 << PCINT1);
72                 else {
73                         PCMSK0 &= ~(1 << PCINT1);
74                         cli();
75                         if (!ao_spi_slave_running) {
76                                 ao_spi_slave_running = 1;
77                                 ao_spi_slave_recv();
78                         }
79                         sei();
80                 }
81         } else {
82                 ao_spi_slave_running = 0;
83         }
84 }
85
86 void ao_spi_slave_debug(void) {
87         printf ("slave running %d\n", ao_spi_slave_running);
88 }
89
90 void
91 ao_spi_slave_init(void)
92 {
93         PCMSK0 |= (1 << PCINT0);        /* Enable PCINT0 pin change */
94         PCICR |= (1 << PCIE0);          /* Enable pin change interrupt */
95
96         DDRB = (DDRB & 0xf0) | (1 << 3);
97         SPCR = (0 << SPIE) |            /* Disable SPI interrupts */
98                 (1 << SPE) |            /* Enable SPI */
99                 (0 << DORD) |           /* MSB first */
100                 (0 << MSTR) |           /* Slave mode */
101                 (0 << CPOL) |           /* Clock low when idle */
102                 (0 << CPHA);            /* Sample at leading clock edge */
103 }