--- /dev/null
+/****************************************************************************
+ * apps/dev/rf_hopping/main.c
+ *
+ *
+ *
+ * Copyright 2013-2014 Nathael Pajani <nathael.pajani@ed3l.fr>
+ *
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ ****************************************************************************/
+
+
+#include "core/system.h"
+#include "core/systick.h"
+#include "core/user_information_block.h"
+#include "core/iap.h"
+#include "lib/stdio.h"
+#include "lib/stdlib.h"
+#include "drivers/serial.h"
+#include "drivers/gpio.h"
+#include "extdrv/status_led.h"
+#include "drivers/timers.h"
+#include "drivers/ssp.h"
+#include "extdrv/cc1101.h"
+
+
+#define PROG_VERSION "0.2"
+
+#define SELECTED_FREQ FREQ_SEL_48MHz
+
+/******************************************************************************/
+#define BOARD_NAME "USB RF debug uC"
+#define BOARD_VERSION 0x02
+
+#define PROG_NAME "usb_rf"
+
+/******************************************************************************/
+/* Pins configuration */
+/* Pins blocks are passed to set_pins() for pins configuration. */
+const struct pio_config common_pins[] = {
+ /* UART 0 */
+ { LPC_UART0_RX_PIO_0_1, LPC_IO_DIGITAL },
+ { LPC_UART0_TX_PIO_0_2, LPC_IO_DIGITAL },
+ /* SPI */
+ { LPC_SSP0_SCLK_PIO_0_14, LPC_IO_DIGITAL },
+ { LPC_SSP0_MOSI_PIO_0_17, LPC_IO_DIGITAL },
+ { LPC_SSP0_MISO_PIO_0_16, LPC_IO_DIGITAL },
+ ARRAY_LAST_PIO,
+};
+
+const struct pio spi_miso_pin = LPC_SSP0_MISO_PIO_0_16;
+const struct pio spi_cs_pin = LPC_GPIO_0_15;
+const struct pio cc1101_gdo_0 = LPC_GPIO_0_6;
+
+
+const struct pio status_led_green = LPC_GPIO_0_28;
+const struct pio status_led_red = LPC_GPIO_0_29;
+
+struct board_data {
+ /* Address MUST be the first field and uint16_t */
+ uint16_t addr;
+ /* Tx channel number */
+ uint8_t tx_channel;
+ /* Rx channel number */
+ uint8_t rx_channel;
+ /* Board role in the communication chain */
+ uint8_t role;
+ /* Make sure our data has a size which is a multiple of 4 bytes */
+ uint32_t align __attribute__ ((__aligned__(4)));
+};
+
+#define BOARD_IS_RX (0x01 << 0)
+#define BOARD_IS_TX (0x01 << 1)
+
+/* Our board address is always 1 */
+static struct board_data b = {
+ .addr = 1,
+ .tx_channel = 0,
+ .rx_channel = 0,
+ .role = BOARD_IS_RX,
+};
+static struct board_data* bdata = &b;
+
+
+
+/******************************************************************************/
+/* RF Communication */
+static volatile int check_rx = 0;
+void rf_rx_calback(uint32_t gpio)
+{
+ check_rx = 1;
+}
+
+static uint8_t rf_specific_settings[] = {
+ /* Accept all sync, CRC err auto flush, Append, Addr check and Bcast */
+ CC1101_REGS(pkt_ctrl[0]), 0x0F,
+ /* LNA 2 gain decr first, Carrier sense relative threshold set to 10dB increase in RSSI value */
+ CC1101_REGS(agc_ctrl[1]), 0x20,
+ /* PO timeout 149-155us, Auto calibrate every 4th time when going from Rx/Tx to Idle */
+ CC1101_REGS(radio_stm[2]), 0x38,
+};
+static uint8_t rf_specific_for_rx_and_rxtx[] = {
+ /* CCA mode "if RSSI below threshold", Stay in RX, Go to RX (page 81) */
+ CC1101_REGS(radio_stm[1]), 0x3F,
+};
+static uint8_t rf_specific_for_tx[] = {
+ /* CCA mode "if RSSI below threshold", Go to FSTXON, Go to FSTXON (page 81) */
+ CC1101_REGS(radio_stm[1]), 0x35,
+};
+
+/* RF config */
+void rf_config(void)
+{
+ cc1101_init(0, &spi_cs_pin, &spi_miso_pin); /* ssp_num, cs_pin, miso_pin */
+ cc1101_config();
+ /* And change application specific settings */
+ cc1101_update_config(rf_specific_settings, sizeof(rf_specific_settings));
+ if (bdata->role & BOARD_IS_RX) {
+ cc1101_update_config(rf_specific_for_rx_and_rxtx, sizeof(rf_specific_for_rx_and_rxtx));
+ cc1101_set_channel(bdata->rx_channel);
+ } else {
+ cc1101_update_config(rf_specific_for_tx, sizeof(rf_specific_for_tx));
+ cc1101_set_channel(bdata->tx_channel);
+ }
+ cc1101_send_calibration_request();
+ msleep(2);
+ /* Setup pin for GDO_0 interrupt */
+ set_gpio_callback(rf_rx_calback, &cc1101_gdo_0, EDGE_RISING);
+ /* Do we have to use a specific address ? */
+ if (bdata->addr != 1) {
+ cc1101_set_address(bdata->addr & 0xFF);
+ }
+ if (bdata->role & BOARD_IS_RX) {
+ cc1101_enter_rx_mode();
+ } else {
+ cc1101_enter_fstxon_state();
+ }
+}
+
+#define RF_BUFF_LEN 64
+void handle_rf_rx_data(void)
+{
+ uint8_t data[RF_BUFF_LEN];
+ int8_t ret = 0;
+ uint8_t status = 0;
+ uint16_t addr = 0;
+
+ status_led(red_on);
+ /* Check for received packet (and get it if any) */
+ ret = cc1101_receive_packet(data, RF_BUFF_LEN, &status);
+ /* Go back to RX mode */
+ cc1101_enter_rx_mode();
+
+ if (ret > 0) {
+ addr = ((data[3] << 8) | data[2]);
+ uprintf(UART0, "Received msg from %d, ret: %d\n", addr, ret);
+ } else if (ret == 0) {
+ uprintf(UART0, "Rx received nothing, system restarted ?\n");
+ } else {
+ uprintf(UART0, "Rx error : %d\n", ret);
+ }
+}
+
+/******************************************************************************/
+/* Board configuration */
+
+void config_board()
+{
+ /* Get our addr from the flash information block */
+ memcpy(bdata, get_user_info(), sizeof(struct board_data));
+ if (bdata->role == 0) {
+ bdata->role = BOARD_IS_RX;
+ }
+ if (bdata->addr == 0) {
+ bdata->addr = 1; /* Address 1 is for all control boards */
+ }
+ rf_config();
+ uprintf(UART0, "System started. Addr: %d, rx: %d, tx: %d, role: %d\n",
+ bdata->addr, bdata->rx_channel, bdata->tx_channel, bdata->role);
+}
+
+
+/******************************************************************************/
+/* User information block re-programming
+ * Make sure that data is aligned on 4 bytes boundary, and that size is a multiple of 4.
+ */
+int user_flash_update(void* data, int size)
+{
+ int ret = 0;
+ /* Erase the user flash information pages */
+ ret = iap_erase_info_page(0, 2);
+ if (ret != 0) {
+ uprintf(UART0, "Config update error: %d\n", ret);
+ return -1;
+ }
+ ret = iap_copy_ram_to_flash((uint32_t)get_user_info(), (uint32_t)data, size);
+ if (ret != 0) {
+ uprintf(UART0, "Config update error: %d\n", ret);
+ return -1;
+ }
+ return 0;
+}
+
+
+/******************************************************************************/
+/* Handle packets */
+void handle_data(uint8_t* data, int len)
+{
+ uint8_t cc_tx_data[RF_BUFF_LEN];
+ int ret = 0;
+ uint8_t status = 0;
+
+ /* Note : All packets are for us, do not check the address */
+ /* Note : Packets will not respect the sequence numbers, do not check. */
+ status_led(green_on);
+
+ if (data[0] == 'a') {
+ struct board_data tmp_bdata __attribute__ ((__aligned__(4))) = {};
+ bdata->addr = strtoul((char*)&data[2], NULL, 10);
+ memcpy(&tmp_bdata, bdata, sizeof(struct board_data));
+ if (user_flash_update(&tmp_bdata, sizeof(struct board_data)) != 0) {
+ uprintf(UART0, "Address config update error\n");
+ }
+ uprintf(UART0, "Address set to %d\n", bdata->addr);
+ return;
+ }
+
+ if (data[0] == 'C') {
+ struct board_data tmp_bdata __attribute__ ((__aligned__(4))) = {};
+ bdata->rx_channel = (uint8_t)strtoul((char*)&data[2], NULL, 10);
+ bdata->tx_channel = (uint8_t)strtoul((char*)&data[5], NULL, 10);
+ bdata->role = (uint8_t)strtoul((char*)&data[8], NULL, 10);
+ if (bdata->role & BOARD_IS_RX) {
+ cc1101_update_config(rf_specific_for_rx_and_rxtx, sizeof(rf_specific_for_rx_and_rxtx));
+ cc1101_set_channel(bdata->rx_channel);
+ cc1101_enter_rx_mode();
+ } else {
+ cc1101_update_config(rf_specific_for_tx, sizeof(rf_specific_for_tx));
+ cc1101_set_channel(bdata->tx_channel);
+ cc1101_enter_fstxon_state();
+ }
+ memcpy(&tmp_bdata, bdata, sizeof(struct board_data));
+ if (user_flash_update(&tmp_bdata, sizeof(struct board_data)) != 0) {
+ uprintf(UART0, "Rf config update error\n");
+ }
+ uprintf(UART0, "Rf config updated - rx: %d - tx: %d - role: %d\n", bdata->rx_channel, bdata->tx_channel, bdata->role);
+ return;
+ }
+
+ if (data[0] == 's') {
+ uint8_t send_len = 0;
+ if (data[1] == 'v') { /* Send to one vehicule, raw */
+ cc_tx_data[0] = (len - 2);
+ memcpy((cc_tx_data + 1), (data + 2), (len - 2));
+ send_len = len - 1;
+ } else if (data[1] == 'a') { /* Ascii mode */
+ uint16_t addr = (uint16_t)strtoul((char*)&data[3], NULL, 10);
+ cc_tx_data[1] = (addr & 0xFF);
+ cc_tx_data[2] = ((addr >> 8) & 0xFF);
+ cc_tx_data[3] = data[6];
+ send_len = 4;
+ if (data[6] == 'v') {
+ cc_tx_data[4] = (uint8_t)strtoul((char*)&data[8], NULL, 10);
+ send_len++;
+ }
+ cc_tx_data[0] = send_len - 1;
+ } else {
+ cc_tx_data[0] = len + 3;
+ cc_tx_data[1] = 0x00; /* Broadcast to everyone */
+ cc_tx_data[2] = (bdata->addr & 0xFF);
+ cc_tx_data[3] = ((bdata->addr >> 8) & 0xFF);
+ memcpy((cc_tx_data + 4), data, len);
+ send_len = len + 4;
+ }
+ /* Send */
+ /* Change channel if necessary */
+ if ((bdata->role & BOARD_IS_RX) && (bdata->tx_channel != bdata->rx_channel)) {
+ cc1101_set_channel(bdata->tx_channel);
+ }
+ /* Then send. This will enter TX state once the data got sent to the buffer, and
+ * check Clear channel assesment bit before switching to TX state. */
+ ret = cc1101_send_packet(cc_tx_data, send_len);
+ if (ret < 0) {
+ uprintf(UART0, "Tx error : %d\n", ret);
+ } else {
+ uprintf(UART0, "Tx packet sent: %s, %d\n", data, send_len);
+ }
+ /* Do not leave radio in an unknown or unwated state */
+ if (bdata->role & BOARD_IS_RX) {
+ do {
+ status = (cc1101_read_status() & CC1101_STATE_MASK);
+ } while (status == CC1101_STATE_TX);
+ if (bdata->tx_channel != bdata->rx_channel) {
+ cc1101_set_channel(bdata->rx_channel);
+ }
+ cc1101_enter_rx_mode();
+ }
+ }
+}
+
+
+
+/******************************************************************************/
+void system_init()
+{
+ /* Stop the watchdog */
+ startup_watchdog_disable(); /* Do it right now, before it gets a chance to break in */
+ system_brown_out_detection_config(0); /* No ADC used */
+ system_set_default_power_state();
+ clock_config(SELECTED_FREQ);
+ set_pins(common_pins);
+ gpio_on();
+ status_led_config(&status_led_green, &status_led_red);
+ /* System tick timer MUST be configured and running in order to use the sleeping
+ * functions */
+ systick_timer_on(1); /* 1ms */
+ systick_start();
+}
+
+/* Define our fault handler. This one is not mandatory, the dummy fault handler
+ * will be used when it's not overridden here.
+ * Note : The default one does a simple infinite loop. If the watchdog is deactivated
+ * the system will hang.
+ */
+void fault_info(const char* name, uint32_t len)
+{
+ uprintf(UART0, name);
+ while (1);
+}
+
+
+static volatile uint32_t new_packet = 0;
+#define UART_BUFF_SIZE 10
+static volatile uint8_t packet[UART_BUFF_SIZE];
+void uart_decode(uint8_t c)
+{
+ static uint8_t idx = 0;
+ packet[idx++] = c;
+ uprintf(UART0, "%c", c);
+ if ((c == '\n') || (c == '\r') || (c == '\0')) {
+ if (c != '\0') {
+ packet[idx - 1] = '\0';
+ }
+ new_packet = idx;
+ idx = 0;
+ }
+}
+
+/******************************************************************************/
+int main(void)
+{
+ system_init();
+ uart_on(UART0, 115200, uart_decode);
+ ssp_master_on(0, LPC_SSP_FRAME_SPI, 8, 4*1000*1000); /* bus_num, frame_type, data_width, rate */
+
+ /* Configure the board from the persistant user flash and all board specific parts */
+ config_board();
+
+ while (1) {
+ uint8_t status = 0;
+ static uint8_t loop = 0;
+
+ status_led(none);
+ /* Check for new packets on serial link */
+ if (new_packet != 0) {
+ handle_data((uint8_t*)packet, new_packet);
+ new_packet = 0;
+ }
+ /* If we received a packet on RF link, handle it now. */
+ if (check_rx == 1) {
+ check_rx = 0;
+ handle_rf_rx_data();
+ }
+
+ /* Continuously check that we are in the right state */
+ status = (cc1101_read_status() & CC1101_STATE_MASK);
+ if ((bdata->role & BOARD_IS_RX) && (status != CC1101_STATE_RX)) {
+ loop++;
+ if (loop > 10) {
+ if (cc1101_rx_fifo_state() != 0) {
+ cc1101_flush_rx_fifo();
+ }
+ cc1101_set_channel(bdata->rx_channel);
+ cc1101_enter_rx_mode();
+ uprintf(UART0, "Moved to Rx mode.\n");
+ loop = 0;
+ }
+ } else if ((bdata->role == BOARD_IS_TX) && !(status & CC1101_STATE_TX)) {
+ loop++;
+ if (loop > 10) {
+ if (cc1101_tx_fifo_state() != 0) {
+ cc1101_flush_tx_fifo();
+ }
+ cc1101_set_channel(bdata->tx_channel);
+ cc1101_enter_fstxon_state();
+ uprintf(UART0, "Moved to FSTON state. (st: 0x%02x)\n", status);
+ loop = 0;
+ }
+ }
+ }
+
+ return 0;
+}
+
+
+