From c40b9c32b6589e35312cebc75e5e7a31822da0d6 Mon Sep 17 00:00:00 2001 From: optixx Date: Mon, 4 May 2009 00:13:24 +0200 Subject: [PATCH] o cleanup code o add more states o decouple read buffer size --- poc/avr_usbload/commandline/snesuploader.c | 109 +++++++++++++++------ poc/avr_usbload/main.c | 94 ++++++++++++------ poc/avr_usbload/requests.h | 21 +--- 3 files changed, 150 insertions(+), 74 deletions(-) diff --git a/poc/avr_usbload/commandline/snesuploader.c b/poc/avr_usbload/commandline/snesuploader.c index f7be0b5..4cbc624 100644 --- a/poc/avr_usbload/commandline/snesuploader.c +++ b/poc/avr_usbload/commandline/snesuploader.c @@ -19,9 +19,10 @@ respectively. */ -#define BUFFER_SIZE 4 -#define BUFFER_CRC (1024 * 64) -#define BANK_SIZE (1<<15) +#define READ_BUFFER_SIZE 1024 +#define SEND_BUFFER_SIZE 128 +#define BUFFER_CRC (1024 * 32) +#define BANK_SIZE (1<<15) #include #include @@ -33,6 +34,42 @@ respectively. #include "../requests.h" /* custom request numbers */ #include "../usbconfig.h" /* device's VID/PID and names */ + +void dump_packet(uint32_t addr,uint32_t len,uint8_t *packet){ + uint16_t i,j; + uint16_t sum = 0; + uint8_t clear=0; + + for (i=0;i=33 && packet[i+j]<=126 ) + printf("%c", packet[i+j]); + else + printf("."); + } + printf("|\n"); + } +} + + uint16_t crc_xmodem_update (uint16_t crc, uint8_t data){ int i; crc = crc ^ ((uint16_t)data << 8); @@ -80,13 +117,14 @@ int main(int argc, char **argv) char vendor[] = {USB_CFG_VENDOR_NAME, 0}, product[] = {USB_CFG_DEVICE_NAME, 0}; int cnt, vid, pid; int cnt_crc = 0; - unsigned char *read_buffer; - unsigned char *crc_buffer; - unsigned char setup_buffer[8]; - unsigned int addr = 0; - unsigned int bank_addr; - unsigned int bank_num; + uint8_t *read_buffer; + uint8_t *crc_buffer; + uint32_t addr = 0; + uint16_t addr_lo = 0; + uint16_t addr_hi = 0; + uint16_t step = 0; uint16_t crc = 0; + uint8_t bank = 0; FILE *fp ; usb_init(); @@ -102,7 +140,7 @@ int main(int argc, char **argv) fprintf(stderr, "Could not find USB device \"%s\" with vid=0x%x pid=0x%x\n", product, vid, pid); exit(1); } - + printf("Open USB device \"%s\" with vid=0x%x pid=0x%x\n", product, vid, pid); if(strcasecmp(argv[1], "upload") == 0){ if(argc < 3){ /* we need at least one argument */ usage(argv[0]); @@ -113,37 +151,48 @@ int main(int argc, char **argv) fprintf(stderr, "Cannot open file %s ", argv[2]); exit(1); } - read_buffer = (unsigned char*)malloc(BUFFER_SIZE); + + read_buffer = (unsigned char*)malloc(READ_BUFFER_SIZE); crc_buffer = (unsigned char*)malloc(BUFFER_CRC); memset(crc_buffer,0,BUFFER_CRC); addr = 0x000000; - bank_addr = addr& 0xffff; - bank_num = (addr>>16) & 0xff; - - while((cnt = fread(read_buffer, BUFFER_SIZE, 1, fp)) > 0){ - bank_addr = addr& 0xffff; - bank_num = (addr>>16) & 0xff; + + usb_control_msg(handle, + USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, + USB_UPLOAD_INIT,0,0, + NULL, 0, + 5000); + + + while((cnt = fread(read_buffer, READ_BUFFER_SIZE, 1, fp)) > 0){ + for (step=0; step>16) & 0xff; + usb_control_msg(handle, + USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, + USB_UPLOAD_ADDR, addr_hi, addr_lo, + (char*) read_buffer+step, SEND_BUFFER_SIZE, + 5000); + #if 0 + dump_packet(addr,SEND_BUFFER_SIZE,read_buffer+step); + #endif + addr+=SEND_BUFFER_SIZE; + } - memcpy(crc_buffer + cnt_crc,read_buffer,BUFFER_SIZE); - cnt_crc += BUFFER_SIZE; - if (cnt_crc == BANK_SIZE){ + memcpy(crc_buffer + cnt_crc,read_buffer,READ_BUFFER_SIZE); + cnt_crc += READ_BUFFER_SIZE; + if (cnt_crc >= BANK_SIZE){ crc = do_crc(crc_buffer,BANK_SIZE); - printf("Addr: 0x%06x Bank: 0x%02x Rom Addr: 0x%04x Addr: 0x%06x Cnt: 0x%04x\n",addr,bank_num, bank_addr, addr,crc); + printf("Addr: 0x%06x Bank: 0x%02x HiAddr: 0x%02x LoAddr: 0x%04x Crc: 0x%04x\n",addr,bank,addr_hi, addr_lo,crc); memset(crc_buffer,0,BUFFER_CRC); + bank++; cnt_crc =0; } - usb_control_msg(handle, - USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, - CUSTOM_RQ_UPLOAD, bank_num, bank_addr, - (char*) read_buffer, BUFFER_SIZE, - 5000); - addr += BUFFER_SIZE; - //printf("Addr: 0x%06x Bank: 0x%02x Rom Addr: 0x%04x Addr: 0x%06x Cnt: 0x%04x cnt: %i\n",addr,bank_num, bank_addr, addr,crc,cnt_crc); } cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, - CUSTOM_RQ_CRC_CHECK, bank_num + 1, 0, - (char*) setup_buffer, sizeof(setup_buffer), + USB_CRC_CHECK, addr_hi, addr_lo, + NULL, 0, 5000); if(cnt < 1){ diff --git a/poc/avr_usbload/main.c b/poc/avr_usbload/main.c index 6b22a29..a07b18d 100644 --- a/poc/avr_usbload/main.c +++ b/poc/avr_usbload/main.c @@ -14,9 +14,9 @@ #include "crc.h" -#define STATE_IDLE 0 -#define STATE_WRITE 1 -#define BUFFER_SIZE 512 +#define STATE_IDLE 0 +#define STATE_UPLOAD 1 +#define BUFFER_SIZE 256 extern FILE uart_stdout; @@ -27,45 +27,59 @@ uint16_t sync_errors = 0; uint8_t read_buffer[BUFFER_SIZE]; uint8_t dataBuffer[4]; /* buffer must stay valid when usbFunctionSetup returns */ uint8_t state = STATE_IDLE; +uint8_t bank; /* buffer must stay valid when usbFunctionSetup returns */ usbMsgLen_t usbFunctionSetup(uchar data[8]){ usbRequest_t *rq = (void *)data; uint16_t crc = 0; uint8_t len = 0; - if(rq->bRequest == CUSTOM_RQ_UPLOAD){ /* echo -- used for reliability tests */ + if(rq->bRequest == USB_UPLOAD_INIT){ + printf("USB_UPLOAD_INIT: reset values\n"); + bank=0; + bytes_remaining=0; + crc=0; + sync_errors=0; + }else if(rq->bRequest == USB_UPLOAD_ADDR){ /* echo -- used for reliability tests */ + state = STATE_UPLOAD; rom_addr = rq->wValue.word; rom_addr = rom_addr << 16; rom_addr = rom_addr | rq->wIndex.word; if (bytes_remaining){ sync_errors++; - printf("CUSTOM_RQ_UPLOAD Out of sync Addr=0x%lx remain=%i packet=%i sync_error=%i\n",rom_addr,bytes_remaining,rq->wLength.word,sync_errors ); + printf("USB_UPLOAD_ADDR: Out of sync Addr=0x%lx remain=%i packet=%i sync_error=%i\n",rom_addr,bytes_remaining,rq->wLength.word,sync_errors ); len=0; } bytes_remaining = rq->wLength.word; - //printf("CUSTOM_RQ_UPLOAD Addr=0x%lx Len=%i\n", rom_addr,bytes_remaining); - state = STATE_WRITE; len = 0xff; - }else if(rq->bRequest == CUSTOM_RQ_DOWNLOAD){ - printf("CUSTOM_RQ_DOWNLOAD\n"); - }else if(rq->bRequest == CUSTOM_RQ_CRC_CHECK){ + if (rom_addr && rom_addr%32768 == 0){ + printf("USB_UPLOAD_ADDR: Bank: 0x%x Addr: 0x%08lx \n",bank,rom_addr); + bank++; + } + len=0xff; + }else if(rq->bRequest == USB_DOWNLOAD_INIT){ + printf("USB_DOWNLOAD_INIT\n"); + }else if(rq->bRequest == USB_DOWNLOAD_ADDR){ + printf("USB_DOWNLOAD_ADDR\n"); + }else if(rq->bRequest ==USB_CRC_CHECK){ rom_addr = rq->wValue.word; rom_addr = rom_addr << 16; rom_addr = rom_addr | rq->wIndex.word; - printf("CUSTOM_RQ_CRC_CHECK Addr 0x%lx \n", rom_addr); - crc = 0; + bank = 0; + crc = 0; + printf("USB_CRC_CHECK: Addr 0x%lx \n", rom_addr); cli(); for (addr=0x000000; addr bytes_remaining) - len = bytes_remaining; - bytes_remaining -= len; - - //printf("usbFunctionWrite addr=%lx len=%i remain=%i\n",rom_addr,len,bytes_remaining); - cli(); - sram_copy(rom_addr,data,len); - sei(); - rom_addr +=len; - //printf("usbFunctionWrite %lx %x\n",rom_addr,len); - //state=STATE_IDLE; + if (len > bytes_remaining){ + printf("usbFunctionWrite more data than expected remain: %i len: %i\n",bytes_remaining,len); + len = bytes_remaining; + } + if (state==STATE_UPLOAD){ + + bytes_remaining -= len; + #if 0 + printf("Addr: 0x%08lx Len: %i\n",rom_addr,len); + #endif + cli(); + sram_copy(rom_addr,data,len); + sei(); + rom_addr +=len; + } return len; } +uint8_t usbFunctionRead(uint8_t *data, uint8_t len) +{ + if(len > bytes_remaining) + len = bytes_remaining; + bytes_remaining -= len; + + for (uint8_t i = 0; i < len; i++) { + if(request == USBASP_FUNC_READEEPROM) + *data = eeprom_read_byte((void *)flash_address.word); + else + *data = pgm_read_byte_near((void *)flash_address.word); + data++; + flash_address.word++; + } + + /* flash led on activity */ + DLED_TGL; + + return len; +} /* ------------------------------------------------------------------------- */ int main(void) { uint8_t i; - //wdt_enable(WDTO_1S); + wdt_enable(WDTO_1S); uart_init(); stdout = &uart_stdout; sram_init(); @@ -115,7 +153,7 @@ int main(void) sei(); printf("USB poll\n"); for(;;){ /* main event loop */ - //wdt_reset(); + wdt_reset(); usbPoll(); } return 0; diff --git a/poc/avr_usbload/requests.h b/poc/avr_usbload/requests.h index daaa31f..5daaef6 100644 --- a/poc/avr_usbload/requests.h +++ b/poc/avr_usbload/requests.h @@ -16,21 +16,10 @@ #ifndef __REQUESTS_H_INCLUDED__ #define __REQUESTS_H_INCLUDED__ -#define CUSTOM_RQ_UPLOAD 0 -/* Request that the device sends back wValue and wIndex. This is used with - * random data to test the reliability of the communication. - */ -#define CUSTOM_RQ_DOWNLOAD 1 -/* Set the LED status. Control-OUT. - * The requested status is passed in the "wValue" field of the control - * transfer. No OUT data is sent. Bit 0 of the low byte of wValue controls - * the LED. - */ - -#define CUSTOM_RQ_CRC_CHECK 2 -/* Get the current LED status. Control-IN. - * This control transfer involves a 1 byte data phase where the device sends - * the current status to the host. The status is in bit 0 of the byte. - */ +#define USB_UPLOAD_INIT 0 +#define USB_UPLOAD_ADDR 1 +#define USB_DOWNLOAD_INIT 2 +#define USB_DOWNLOAD_ADDR 3 +#define USB_CRC_CHECK 4 #endif /* __REQUESTS_H_INCLUDED__ */