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