From 7b190e711967fdea7ac33400a565dbe2cd86aa22 Mon Sep 17 00:00:00 2001 From: Nidan Date: Sat, 11 Dec 2010 00:06:58 +0000 Subject: [PATCH 1/2] added spi interface for usb.c --- firmware/masterchip/Makefile | 2 +- firmware/masterchip/spi_proto.c | 42 ++++++++++++++++++++++++++------- firmware/masterchip/spi_proto.h | 8 +++++++ firmware/masterchip/usb.c | 12 ++++------ 4 files changed, 47 insertions(+), 17 deletions(-) diff --git a/firmware/masterchip/Makefile b/firmware/masterchip/Makefile index 087798e..c5d5e81 100644 --- a/firmware/masterchip/Makefile +++ b/firmware/masterchip/Makefile @@ -3,7 +3,7 @@ include ../Makefile.inc COMPILE = avr-gcc $(CFLAGS) $(DEFINES) -OBJECTS = usbdrv/usbdrvasm.o usbdrv/usbdrv.o main.o display.o lcd/lcd.o usb.o softtimer.o +OBJECTS = usbdrv/usbdrvasm.o usbdrv/usbdrv.o main.o display.o lcd/lcd.o usb.o softtimer.o spi.o spi_proto.o # symbolic targets: all: firmware.hex diff --git a/firmware/masterchip/spi_proto.c b/firmware/masterchip/spi_proto.c index c12e128..923c5ad 100644 --- a/firmware/masterchip/spi_proto.c +++ b/firmware/masterchip/spi_proto.c @@ -4,31 +4,55 @@ uint8_t read[2]; uint8_t write[4]; -void talk_to_slave(uint8_t opcode, uint8_t addr, uint8_t wlen, uint8_t rlen) +void talk_to_slave(uint8_t opcode, uint8_t addr, uint8_t flags) { write[0] = opcode; write[1] = addr; spi_mst_start_packet(); - spi_mst_write(wlen, write); - spi_mst_read(rlen, read); + spi_mst_write(wlen, flags & SPI_WRITE_DATA? 4 : 2); + spi_mst_read(rlen, flags & SPI_READ_DATA? 2 : 0); spi_mst_end_packet(); } +uint16_t speak_raw(uint8_t opcode, uint8_t number, uint8_t flags, uint16_t value) +{ + write[2] = value >> 8; + write[3] = value & 0xff; + talk_to_slave(opcode, number, flags); + return (read[0] << 8) | read[1]; +} + +uint8_t spi_proto_needs(uint8_t opcode) +{ + uint8_t r = SPI_NONE; + switch(opcode) + { + case 1: case 2: case 3: + r = SPI_READ_DATA; + break; + case 4: case 5: + r = SPI_WRITE_DATA; + break; + } + return r; +} + + uint16_t read_temperature(uint8_t number) { - talk_to_slave(1, number, 2, 2); + talk_to_slave(1, number, SPI_READ_DATA); return (read[0] << 8) | read[1]; } uint8_t read_var8(uint8_t number) { - talk_to_slave(2, number, 2, 2); + talk_to_slave(2, number, SPI_READ_DATA); return read[1]; } uint16_t read_var16(uint8_t number) { - talk_to_slave(3, number, 2, 2); + talk_to_slave(3, number, SPI_READ_DATA); return (read[0] << 8) | read[1]; } @@ -36,17 +60,17 @@ void write_var8(uint8_t number, uint8_t value) { write[2] = 0; write[3] = number; - talk_to_slave(4, number, 4, 0); + talk_to_slave(4, number, SPI_WRITE_DATA); } void write_var16(uint8_t number, uint16_t value) { write[2] = value >> 8; write[3] = value & 0xff; - talk_to_slave(5, number, 4, 0); + talk_to_slave(5, number, SPI_WRITE_DATA); } void call_func(uint8_t number) { - talk_to_slave(6, number, 2, 0); + talk_to_slave(6, number, SPI_NONE); } diff --git a/firmware/masterchip/spi_proto.h b/firmware/masterchip/spi_proto.h index c7861fc..93d0dfa 100644 --- a/firmware/masterchip/spi_proto.h +++ b/firmware/masterchip/spi_proto.h @@ -1,6 +1,14 @@ #ifndef SPI_PROTO_H #define SPI_PROTO_H +#define SPI_WRITE_DATA 0x01 +#define SPI_READ_DATA 0x10 +#define SPI_NONE 0 + +uint16_t speak_raw(uint8_t opcode, uint8_t number, uint8_t flags, uint16_t data); + +uint8_t spi_proto_needs(uint8_t opcode); + uint16_t read_temperature(uint8_t number); uint8_t read_var8(uint8_t number); uint16_t read_var16(uint8_t number); diff --git a/firmware/masterchip/usb.c b/firmware/masterchip/usb.c index 5c9ed9e..fc68300 100644 --- a/firmware/masterchip/usb.c +++ b/firmware/masterchip/usb.c @@ -27,13 +27,11 @@ usbMsgLen_t usbFunctionSetup(uchar data[8]) { uint8_t valH = (uint8_t) rq->wIndex.bytes[0]; uint8_t valL = (uint8_t) rq->wIndex.bytes[1]; - uint8_t send[] = {opcode, addr, valH, valL}; - uint8_t recv[2]; - spi_mst_write(4, send); - if(!spi_proto_needswrite(opcode)) spi_mst_read(2, recv); - - dataBuffer[0] = recv[0]; - dataBuffer[1] = recv[1]; + uint16_t send = (valH << 8) | valL; + uint16_t recv = speak_raw(opcode, addr, spi_proto_needs(opcode), send); + + dataBuffer[0] = (recv >> 8) & 0xff; + dataBuffer[1] = recv & 0xff; dataBuffer[2] = 0x0000; dataBuffer[3] = 0x0000; usbMsgPtr = dataBuffer; From 7e9ad1e109910403ceea74d1c99d268680bd902d Mon Sep 17 00:00:00 2001 From: Dario Ernst Date: Sat, 11 Dec 2010 01:07:17 +0100 Subject: [PATCH 2/2] maybe fixed usb --- cmdline/read-temp.c | 56 +++++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/cmdline/read-temp.c b/cmdline/read-temp.c index 93bc4ad..fb81b3a 100644 --- a/cmdline/read-temp.c +++ b/cmdline/read-temp.c @@ -24,7 +24,7 @@ respectively. #include /* this is libusb */ #include "opendevice.h" /* common code moved to separate module */ -#include "../firmware/usbdrv/usbconfig.h" /* device's VID/PID and names */ +#include "../firmware/masterchip/usbdrv/usbconfig.h" /* device's VID/PID and names */ int main(int argc, char **argv) { @@ -67,25 +67,41 @@ int cnt, vid, pid; int rxValue, rxIndex; int value = 0, index = 0; - /* int i=0; */ - /* cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, 100, value, index, buffer, sizeof(buffer), 5000); */ - /* if(cnt < 0){ */ - /* fprintf(stderr, "\nUSB error in iteration %d: %s\n", i, usb_strerror()); */ - /* } */ - /* rxValue = ((int)buffer[1] & 0xff) | (((int)buffer[0] & 0xff) << 8); */ - /* rxIndex = ((int)buffer[3] & 0xff) | (((int)buffer[2] & 0xff) << 8); */ - /* fprintf(stderr, "%3d.%02d*C ", rxValue/100, rxValue%100); */ - /* fprintf(stderr, "%3d.%02d*C \n", rxIndex/100, rxIndex%100); */ - - /* cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, 101, value, index, buffer, sizeof(buffer), 5000); */ - /* if(cnt < 0){ */ - /* fprintf(stderr, "\nUSB error in iteration %d: %s\n", i, usb_strerror()); */ - /* } */ - /* rxValue = ((int)buffer[1] & 0xff) | (((int)buffer[0] & 0xff) << 8); */ - /* rxIndex = ((int)buffer[3] & 0xff) | (((int)buffer[2] & 0xff) << 8); */ - /* fprintf(stderr, "%3d.%02d*C ", rxValue/100, rxValue%100); */ - /* fprintf(stderr, "%3d.%02d*C \n", rxIndex/100, rxIndex%100); */ - + if(strcasecmp(argv[1], "temp") == 0) { + int i=0; + cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, 100, value, index, buffer, sizeof(buffer), 5000); + if(cnt < 0){ + fprintf(stderr, "\nUSB error in iteration %d: %s\n", i, usb_strerror()); + } + rxValue = ((int)buffer[1] & 0xff) | (((int)buffer[0] & 0xff) << 8); + rxIndex = ((int)buffer[3] & 0xff) | (((int)buffer[2] & 0xff) << 8); + fprintf(stderr, "%3d.%02d*C ", rxValue/100, rxValue%100); + fprintf(stderr, "%3d.%02d*C \n", rxIndex/100, rxIndex%100); + + cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, 101, value, index, buffer, sizeof(buffer), 5000); + if(cnt < 0){ + fprintf(stderr, "\nUSB error in iteration %d: %s\n", i, usb_strerror()); + } + rxValue = ((int)buffer[1] & 0xff) | (((int)buffer[0] & 0xff) << 8); + rxIndex = ((int)buffer[3] & 0xff) | (((int)buffer[2] & 0xff) << 8); + fprintf(stderr, "%3d.%02d*C ", rxValue/100, rxValue%100); + fprintf(stderr, "%3d.%02d*C \n", rxIndex/100, rxIndex%100); + } else { + int opcode, addr, value; + int i=0; + sscanf(argv, "%i %i %i", &opcode, &addr, &value); + cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, opcode, addr, value, buffer, sizeof(buffer), 5000); + if(cnt < 0){ + fprintf(stderr, "\nUSB error in iteration %d: %s\n", i, usb_strerror()); + } + rxValue = ((int)buffer[1] & 0xff) | (((int)buffer[0] & 0xff) << 8); + rxIndex = ((int)buffer[3] & 0xff) | (((int)buffer[2] & 0xff) << 8); + + fprintf(stderr, "request = 0x%04x", opcode); + fprintf(stderr, "rxValue = 0x%04x value = 0x%04x\n", rxValue, addr); + fprintf(stderr, "rxIndex = 0x%04x index = 0x%04x\n", rxIndex, value); + + }