2 * Copyright © 2012 Keith Packard <keithp@keithp.com>
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.
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.
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.
19 #include <ao_radio_spi.h>
20 #include <ao_radio_cmac.h>
22 static __xdata struct ao_radio_spi_reply ao_radio_spi_reply;
24 static __xdata struct ao_radio_spi_request ao_radio_spi_request;
26 static __xdata uint8_t slave_state;
29 ao_radio_slave_low(void)
35 ao_led_toggle(AO_LED_GREEN);
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++)
43 ao_radio_slave_high(void)
47 ao_led_toggle(AO_LED_RED);
48 ao_gpio_set(AO_RADIO_SLAVE_INT_PORT, AO_RADIO_SLAVE_INT_BIT, AO_RADIO_SLAVE_INT_PIN, 1);
53 ao_radio_slave_spi(void)
55 ao_spi_get_slave(AO_RADIO_SLAVE_BUS);
57 ao_spi_recv(&ao_radio_spi_request,
58 (2 << 13) | sizeof (ao_radio_spi_request),
60 ao_radio_slave_high();
62 switch (ao_radio_spi_request.request) {
63 case AO_RADIO_SPI_RECV:
65 /* XXX monitor CS to interrupt the receive */
67 ao_config.radio_setting = ao_radio_spi_request.setting;
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_reply.rssi = 0;
71 ao_spi_send(&ao_radio_spi_reply,
72 AO_RADIO_SPI_REPLY_HEADER_LEN + ao_radio_spi_request.recv_len,
77 case AO_RADIO_SPI_CMAC_RECV:
78 ao_config.radio_setting = ao_radio_spi_request.setting;
79 ao_radio_spi_reply.status = ao_radio_cmac_recv(&ao_radio_spi_reply.payload,
80 ao_radio_spi_request.recv_len,
81 ao_radio_spi_request.timeout);
82 ao_radio_spi_reply.rssi = ao_radio_cmac_rssi;
83 ao_spi_send(&ao_radio_spi_reply,
84 AO_RADIO_SPI_REPLY_HEADER_LEN + ao_radio_spi_request.recv_len,
89 case AO_RADIO_SPI_SEND:
90 ao_config.radio_setting = ao_radio_spi_request.setting;
91 ao_radio_send(&ao_radio_spi_request.payload,
92 ao_radio_spi_request.len - AO_RADIO_SPI_REQUEST_HEADER_LEN);
95 case AO_RADIO_SPI_CMAC_SEND:
96 ao_config.radio_setting = ao_radio_spi_request.setting;
97 ao_radio_cmac_send(&ao_radio_spi_request.payload,
98 ao_radio_spi_request.len - AO_RADIO_SPI_REQUEST_HEADER_LEN);
101 case AO_RADIO_SPI_CMAC_KEY:
102 ao_xmemcpy(&ao_config.aes_key, ao_radio_spi_request.payload, AO_AES_LEN);
105 case AO_RADIO_SPI_TEST_ON:
106 ao_config.radio_setting = ao_radio_spi_request.setting;
110 case AO_RADIO_SPI_TEST_OFF:
114 ao_radio_slave_low();
118 static __xdata struct ao_task ao_radio_slave_spi_task;
121 ao_radio_slave_init(void)
123 ao_add_task(&ao_radio_slave_spi_task, ao_radio_slave_spi, "radio_spi");
124 ao_enable_output(AO_RADIO_SLAVE_INT_PORT, AO_RADIO_SLAVE_INT_BIT, AO_RADIO_SLAVE_INT_PIN, 0);