altos/cc1200: Adjust bit-sync configuration
[fw/altos] / src / drivers / ao_radio_slave.c
1 /*
2  * Copyright © 2012 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_radio_spi.h>
20 #include <ao_radio_cmac.h>
21
22 static __xdata struct ao_radio_spi_reply ao_radio_spi_reply;
23
24 static __xdata struct ao_radio_spi_request ao_radio_spi_request;
25
26 static __xdata uint8_t  slave_state;
27
28 static void
29 ao_radio_slave_low(void)
30 {
31         uint16_t        i;
32
33         if (slave_state != 1)
34                 ao_panic(1);
35         ao_gpio_set(AO_RADIO_SLAVE_INT_PORT, AO_RADIO_SLAVE_INT_BIT, AO_RADIO_SLAVE_INT_PIN, 0);
36         for (i = 0; i < 1000; i++)
37                 ao_arch_nop();
38         slave_state = 0;
39 }
40
41 static void
42 ao_radio_slave_high(void)
43 {
44         if (slave_state != 0)
45                 ao_panic(2);
46         ao_gpio_set(AO_RADIO_SLAVE_INT_PORT, AO_RADIO_SLAVE_INT_BIT, AO_RADIO_SLAVE_INT_PIN, 1);
47         slave_state = 1;
48 }
49
50 static void
51 ao_radio_slave_spi(void)
52 {
53         ao_spi_get_slave(AO_RADIO_SLAVE_BUS);
54         for (;;) {
55                 ao_spi_recv(&ao_radio_spi_request,
56                             (2 << 13) | sizeof (ao_radio_spi_request),
57                             AO_RADIO_SLAVE_BUS);
58                 ao_radio_slave_high();
59                 ao_spi_recv_wait();
60                 switch (ao_radio_spi_request.request) {
61                 case AO_RADIO_SPI_RECV:
62
63                         /* XXX monitor CS to interrupt the receive */
64
65                         ao_config.radio_setting = ao_radio_spi_request.setting;
66                         ao_led_on(AO_LED_RX);
67                         ao_radio_spi_reply.status = ao_radio_recv(&ao_radio_spi_reply.payload,
68                                                                   ao_radio_spi_request.recv_len,
69                                                                   ao_radio_spi_request.timeout);
70                         ao_led_off(AO_LED_RX);
71                         ao_radio_spi_reply.rssi = 0;
72                         ao_spi_send(&ao_radio_spi_reply,
73                                     AO_RADIO_SPI_REPLY_HEADER_LEN + ao_radio_spi_request.recv_len,
74                                     AO_RADIO_SLAVE_BUS);
75                         ao_radio_slave_low();
76                         ao_spi_send_wait();
77                         continue;
78                 case AO_RADIO_SPI_CMAC_RECV:
79                         ao_config.radio_setting = ao_radio_spi_request.setting;
80                         ao_led_on(AO_LED_RX);
81                         ao_radio_spi_reply.status = ao_radio_cmac_recv(&ao_radio_spi_reply.payload,
82                                                                        ao_radio_spi_request.recv_len,
83                                                                        ao_radio_spi_request.timeout);
84                         ao_led_off(AO_LED_RX);
85                         ao_radio_spi_reply.rssi = ao_radio_cmac_rssi;
86                         ao_spi_send(&ao_radio_spi_reply,
87                                     AO_RADIO_SPI_REPLY_HEADER_LEN + ao_radio_spi_request.recv_len,
88                                     AO_RADIO_SLAVE_BUS);
89                         ao_radio_slave_low();
90                         ao_spi_send_wait();
91                         continue;
92                 case AO_RADIO_SPI_SEND:
93                         ao_config.radio_setting = ao_radio_spi_request.setting;
94                         ao_led_on(AO_LED_TX);
95                         ao_radio_send(&ao_radio_spi_request.payload,
96                                       ao_radio_spi_request.len - AO_RADIO_SPI_REQUEST_HEADER_LEN);
97                         ao_led_off(AO_LED_TX);
98                         break;
99
100                 case AO_RADIO_SPI_CMAC_SEND:
101                         ao_config.radio_setting = ao_radio_spi_request.setting;
102                         ao_led_on(AO_LED_TX);
103                         ao_radio_cmac_send(&ao_radio_spi_request.payload,
104                                            ao_radio_spi_request.len - AO_RADIO_SPI_REQUEST_HEADER_LEN);
105                         ao_led_off(AO_LED_TX);
106                         break;
107                         
108                 case AO_RADIO_SPI_CMAC_KEY:
109                         ao_xmemcpy(&ao_config.aes_key, ao_radio_spi_request.payload, AO_AES_LEN);
110                         break;
111
112                 case AO_RADIO_SPI_TEST_ON:
113                         ao_config.radio_setting = ao_radio_spi_request.setting;
114                         ao_radio_test(1);
115                         break;
116
117                 case AO_RADIO_SPI_TEST_OFF:
118                         ao_radio_test(0);
119                         break;
120                 }
121                 ao_radio_slave_low();
122         }
123 }
124
125 static __xdata struct ao_task ao_radio_slave_spi_task;
126
127 void
128 ao_radio_slave_init(void)
129 {
130         ao_add_task(&ao_radio_slave_spi_task, ao_radio_slave_spi, "radio_spi");
131         ao_enable_output(AO_RADIO_SLAVE_INT_PORT, AO_RADIO_SLAVE_INT_BIT, AO_RADIO_SLAVE_INT_PIN, 0);
132 }