From a37bba8755e7aa6a45eb32d29458f296a6975e82 Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Sun, 19 Jun 2011 15:11:30 -0300 Subject: [PATCH] tools/lib: added USB-SPI driver for ATUSB - atusb-spi.c: ATUSB driver that uses the ATUSB_SPI_* functions instead of ATUSB_{REG,BUF,SRAM}_* - driver.h, atrf.c (drivers, atrf_usb_handle): added atusb_spi_driver - Makefile (OBJS): added atusb-spi.o --- tools/lib/Makefile | 2 +- tools/lib/atrf.c | 3 +- tools/lib/atusb-spi.c | 171 ++++++++++++++++++++++++++++++++++++++++++ tools/lib/driver.h | 1 + 4 files changed, 175 insertions(+), 2 deletions(-) create mode 100644 tools/lib/atusb-spi.c diff --git a/tools/lib/Makefile b/tools/lib/Makefile index 41de1f9..53f0ac8 100644 --- a/tools/lib/Makefile +++ b/tools/lib/Makefile @@ -15,7 +15,7 @@ LIB = libatrf.a include ../Makefile.common -OBJS_host = atusb.o atusb-common.o usbopen.o +OBJS_host = atusb.o atusb-spi.o atusb-common.o usbopen.o OBJS_ben_jlime = atben.o OBJS_ben_openwrt = atben.o diff --git a/tools/lib/atrf.c b/tools/lib/atrf.c index fafe63f..f88ec88 100644 --- a/tools/lib/atrf.c +++ b/tools/lib/atrf.c @@ -35,6 +35,7 @@ static const struct atrf_driver *drivers[] = { #endif #ifdef HAVE_USB &atusb_driver, + &atusb_spi_driver, #endif &atnet_driver, NULL @@ -44,7 +45,7 @@ static const struct atrf_driver *drivers[] = { void *atrf_usb_handle(struct atrf_dsc *dsc) { #ifdef HAVE_USB - if (dsc->driver == &atusb_driver) + if (dsc->driver == &atusb_driver || dsc->driver == &atusb_spi_driver) return atusb_dev_handle(dsc->handle); #endif return NULL; diff --git a/tools/lib/atusb-spi.c b/tools/lib/atusb-spi.c new file mode 100644 index 0000000..c120b1c --- /dev/null +++ b/tools/lib/atusb-spi.c @@ -0,0 +1,171 @@ +/* + * lib/atusb_spi.c - ATRF access functions library (USB-SPI version) + * + * Written 2010-2011 by Werner Almesberger + * Copyright 2010-2011 Werner Almesberger + * + * 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. + */ + + +#include +#include +#include +#include +#include + +#include "atusb/ep0.h" +#include "atusb/usb-ids.h" + +#include "usbopen.h" +#include "at86rf230.h" +#include "driver.h" +#include "atusb-common.h" + + +static void atusb_spi_reg_write(void *handle, uint8_t reg, uint8_t v) +{ + struct atusb_dsc *dsc = handle; + int res; + + if (dsc->error) + return; + + res = usb_control_msg(dsc->dev, TO_DEV, ATUSB_SPI_WRITE, + AT86RF230_REG_WRITE | reg, v, NULL, 0, 1000); + if (res < 0) { + fprintf(stderr, "ATUSB_SPI_WRITE: %d\n", res); + dsc->error = 1; + } +} + + +static uint8_t atusb_spi_reg_read(void *handle, uint8_t reg) +{ + struct atusb_dsc *dsc = handle; + uint8_t value = 0; + int res; + + if (dsc->error) + return 0; + + res = usb_control_msg(dsc->dev, FROM_DEV, ATUSB_SPI_READ1, + AT86RF230_REG_READ | reg, 0, (void *) &value, 1, 1000); + if (res < 0) { + fprintf(stderr, "ATUSB_SPI_READ1: %d\n", res); + dsc->error = 1; + } + return value; +} + + +static void atusb_spi_buf_write(void *handle, const void *buf, int size) +{ + struct atusb_dsc *dsc = handle; + int res; + + if (dsc->error) + return; + + res = usb_control_msg(dsc->dev, TO_DEV, ATUSB_SPI_WRITE, + AT86RF230_BUF_WRITE, size, (void *) buf, size, 1000); + if (res < 0) { + fprintf(stderr, "ATUSB_SPI_WRITE: %d\n", res); + dsc->error = 1; + } +} + + +static int atusb_spi_buf_read(void *handle, void *buf, int size) +{ + struct atusb_dsc *dsc = handle; + uint8_t tmp[SRAM_SIZE]; + int res, got; + + if (dsc->error) + return -1; + + res = usb_control_msg(dsc->dev, FROM_DEV, ATUSB_SPI_READ1, + AT86RF230_BUF_READ, 0, tmp, sizeof(tmp), 1000); + if (res < 0) { + fprintf(stderr, "ATUSB_SPI_READ1: %d\n", res); + dsc->error = 1; + return res; + } + got = tmp[0]+1; /* +1 for LQI */ + if (got >= res) { + fprintf(stderr, + "atusb_spi_buf_read: got %d >= received %d bytes\n", + got, res); + return -1; + } + if (got > size) { + fprintf(stderr, "atusb_spi_buf_read: got %d > max %d bytes\n", + got, size); + return -1; + } + memcpy(buf, tmp+1, got); + + return got; +} + + +static void atusb_spi_sram_write(void *handle, uint8_t addr, uint8_t v) +{ + struct atusb_dsc *dsc = handle; + int res; + + if (dsc->error) + return; + + res = usb_control_msg(dsc->dev, TO_DEV, ATUSB_SPI_WRITE, + AT86RF230_SRAM_WRITE, addr, &v, 1, 1000); + if (res < 0) { + fprintf(stderr, "ATUSB_SPI_WRITE: %d\n", res); + dsc->error = 1; + } +} + + +static uint8_t atusb_spi_sram_read(void *handle, uint8_t addr) +{ + struct atusb_dsc *dsc = handle; + uint8_t value = 0; + int res; + + if (dsc->error) + return 0; + + res = usb_control_msg(dsc->dev, FROM_DEV, ATUSB_SPI_READ2, + AT86RF230_SRAM_READ, addr, (void *) &value, 1, 1000); + if (res < 0) { + fprintf(stderr, "ATUSB_SPI_READ2: %d\n", res); + dsc->error = 1; + } + return value; +} + + +/* ----- Driver interface -------------------------------------------------- */ + + +struct atrf_driver atusb_spi_driver = { + .name = "usb-spi", + .open = atusb_open, + .close = atusb_close, + .reset = NULL, + .reset_rf = atusb_reset_rf, + .test_mode = NULL, + .slp_tr = atusb_slp_tr, + .set_clkm = atusb_set_clkm, + .reg_write = atusb_spi_reg_write, + .reg_read = atusb_spi_reg_read, + .buf_write = atusb_spi_buf_write, + .buf_read = atusb_spi_buf_read, + .sram_write = atusb_spi_sram_write, + .sram_read = atusb_spi_sram_read, + .interrupt = atusb_interrupt, +}; diff --git a/tools/lib/driver.h b/tools/lib/driver.h index 31b766c..cf05a9f 100644 --- a/tools/lib/driver.h +++ b/tools/lib/driver.h @@ -40,6 +40,7 @@ struct atrf_driver { extern struct atrf_driver atusb_driver; +extern struct atrf_driver atusb_spi_driver; extern struct atrf_driver atben_driver; extern struct atrf_driver atnet_driver;