From 9a0184cacbbb7d4ced27cf45c6bae5a5e3e25c32 Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Sun, 19 Jun 2011 15:37:40 -0300 Subject: [PATCH] atusb/fw/: new mechanism for SPI commands (more general than reg/buf requests) - ep0.c (setup_request): store combined request code in "req", for later reuse - include/ep0.h (ATUSB_SPI_WRITE, ATUSB_SPI_READ1, ATUSB_SPI_READ2), ep0.c (setup_request): new requests that translate easily into general SPI operations --- atusb/fw/ep0.c | 27 ++++++++++++++++++++++++++- atusb/fw/include/atusb/ep0.h | 7 +++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/atusb/fw/ep0.c b/atusb/fw/ep0.c index 95f112f..bb36ad8 100644 --- a/atusb/fw/ep0.c +++ b/atusb/fw/ep0.c @@ -58,11 +58,12 @@ static void do_buf_write(void *user) static int my_setup(const struct setup_request *setup) { + uint16_t req = setup->bmRequestType | setup->bRequest << 8; unsigned tmp; uint8_t i; uint64_t tmp64; - switch (setup->bmRequestType | setup->bRequest << 8) { + switch (req) { case ATUSB_FROM_DEV(ATUSB_ID): debug("ATUSB_ID\n"); if (setup->wLength > 3) @@ -201,6 +202,30 @@ static int my_setup(const struct setup_request *setup) usb_send(&eps[0], buf, setup->wLength, NULL, NULL); return 1; + case ATUSB_TO_DEV(ATUSB_SPI_WRITE): + size = setup->wLength+2; + if (size > sizeof(buf)) + return 0; + buf[0] = setup->wValue; + buf[1] = setup->wIndex; + if (setup->wLength) + usb_recv(&eps[0], buf+2, setup->wLength, + do_buf_write, NULL); + else + do_buf_write(NULL); + return 1; + case ATUSB_FROM_DEV(ATUSB_SPI_READ1): + case ATUSB_FROM_DEV(ATUSB_SPI_READ2): + spi_begin(); + spi_send(setup->wValue); + if (req == ATUSB_SPI_READ2) + spi_send(setup->wIndex); + for (i = 0; i != setup->wLength; i++) + buf[i] = spi_recv(); + spi_end(); + usb_send(&eps[0], buf, setup->wLength, NULL, NULL); + return 1; + default: error("Unrecognized SETUP: 0x%02x 0x%02x ...\n", setup->bmRequestType, setup->bRequest); diff --git a/atusb/fw/include/atusb/ep0.h b/atusb/fw/include/atusb/ep0.h index 00746e1..4775c3c 100644 --- a/atusb/fw/include/atusb/ep0.h +++ b/atusb/fw/include/atusb/ep0.h @@ -34,6 +34,10 @@ * ->host ATUSB_BUF_READ - - #bytes * host-> ATUSB_SRAM_WRITE - addr #bytes * ->host ATUSB_SRAM_READ - addr #bytes + * + * host-> ATUSB_SPI_WRITE byte0 byte1 #bytes + * ->host ATUSB_SPI_READ1 byte0 - #bytes + * ->host ATUSB_SPI_READ2 byte0 byte1 #bytes */ /* @@ -82,6 +86,9 @@ enum atspi_requests { ATUSB_BUF_READ, ATUSB_SRAM_WRITE, ATUSB_SRAM_READ, + ATUSB_SPI_WRITE = 0x30, /* SPI group */ + ATUSB_SPI_READ1, + ATUSB_SPI_READ2, };