o add gnususb as reference bootloader

o refactor boot loader  cpde
o
This commit is contained in:
David Voswinkel
2009-05-06 23:55:56 +02:00
parent c40b9c32b6
commit aedc6dab5b
80 changed files with 19744 additions and 258 deletions

View File

@@ -12,8 +12,10 @@ F_CPU = 16000000 # in Hz
FUSE_L = # see below for fuse values for particular devices
FUSE_H =
AVRDUDE = avrdude -c stk500v2 -p $(DEVICE) -P /dev/tty.PL2303-00002006
#AVRDUDE = avrdude -c stk500v2 -p $(DEVICE) -P /dev/tty.PL2303-00001424
CFLAGS = -Iusbdrv -I. -DDEBUG_LEVEL=0
CFLAGS = -Iusbdrv -I. -DDEBUG_LEVEL=0
#-std=gnu99
OBJECTS = usbdrv/usbdrv.o usbdrv/usbdrvasm.o usbdrv/oddebug.o main.o uart.o fifo.o sram.o crc.o debug.o
COMPILE = avr-gcc -Wall -Os -DF_CPU=$(F_CPU) $(CFLAGS) -mmcu=$(DEVICE)

View File

@@ -1,11 +1,9 @@
/* Name: set-led.c
* Project: custom-class, a basic USB example
* Author: Christian Starkjohann
* Creation Date: 2008-04-10
* Tabsize: 4
* Copyright: (c) 2008 by OBJECTIVE DEVELOPMENT Software GmbH
* License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt)
* This Revision: $Id: set-led.c 692 2008-11-07 15:07:40Z cs $
/*
* Name: set-led.c Project: custom-class, a basic USB example Author:
* Christian Starkjohann Creation Date: 2008-04-10 Tabsize: 4 Copyright: (c)
* 2008 by OBJECTIVE DEVELOPMENT Software GmbH License: GNU GPL v2 (see
* License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt) This
* Revision: $Id: set-led.c 692 2008-11-07 15:07:40Z cs $
*/
/*
@@ -28,184 +26,246 @@ respectively.
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <usb.h> /* this is libusb */
#include "opendevice.h" /* common code moved to separate module */
#include <usb.h> /* this is libusb */
#include "opendevice.h" /* common code moved to separate module */
#include "../requests.h" /* custom request numbers */
#include "../usbconfig.h" /* device's VID/PID and names */
#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<len;i+=16) {
sum = 0;
for (j=0;j<16;j++) {
sum +=packet[i+j];
}
if (!sum){
clear=1;
continue;
}
if (clear){
printf("*\n");
clear = 0;
}
printf("%08x:", addr + i);
for (j=0;j<16;j++) {
printf(" %02x", packet[i+j]);
}
printf(" |");
for (j=0;j<16;j++) {
if (packet[i+j]>=33 && packet[i+j]<=126 )
printf("%c", packet[i+j]);
else
printf(".");
}
printf("|\n");
}
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 < len; i += 16)
{
sum = 0;
for (j = 0; j < 16; j++)
{
sum += packet[i + j];
}
if (!sum)
{
clear = 1;
continue;
}
if (clear)
{
printf ("*\n");
clear = 0;
}
printf ("%08x:", addr + i);
for (j = 0; j < 16; j++)
{
printf (" %02x", packet[i + j]);
}
printf (" |");
for (j = 0; j < 16; j++)
{
if (packet[i + j] >= 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){
uint16_t
crc_xmodem_update (uint16_t crc, uint8_t data)
{
int i;
crc = crc ^ ((uint16_t)data << 8);
for (i=0; i<8; i++)
{
if (crc & 0x8000)
crc = (crc << 1) ^ 0x1021;
else
crc <<= 1;
}
crc = crc ^ ((uint16_t) data << 8);
for (i = 0; i < 8; i++)
{
if (crc & 0x8000)
crc = (crc << 1) ^ 0x1021;
else
crc <<= 1;
}
return crc;
}
uint16_t do_crc(uint8_t * data,uint16_t size){
uint16_t crc =0;
uint16_t i;
for (i=0; i<size; i++){
crc = crc_xmodem_update(crc,data[i]);
}
return crc;
}
uint16_t do_crc_update(uint16_t crc,uint8_t * data,uint16_t size){
uint16_t i;
for (i=0; i<size; i++)
crc = crc_xmodem_update(crc,data[i]);
return crc;
}
static void usage(char *name)
uint16_t
do_crc (uint8_t * data, uint16_t size)
{
fprintf(stderr, "usage:\n");
fprintf(stderr, " %s upload filename.. upload\n", name);
uint16_t crc = 0;
uint16_t i;
for (i = 0; i < size; i++)
{
crc = crc_xmodem_update (crc, data[i]);
}
return crc;
}
int main(int argc, char **argv)
uint16_t
do_crc_update (uint16_t crc, uint8_t * data, uint16_t size)
{
usb_dev_handle *handle = NULL;
const unsigned char rawVid[2] = {USB_CFG_VENDOR_ID}, rawPid[2] = {USB_CFG_DEVICE_ID};
char vendor[] = {USB_CFG_VENDOR_NAME, 0}, product[] = {USB_CFG_DEVICE_NAME, 0};
int cnt, vid, pid;
int cnt_crc = 0;
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 ;
uint16_t i;
for (i = 0; i < size; i++)
crc = crc_xmodem_update (crc, data[i]);
return crc;
}
usb_init();
if(argc < 2){ /* we need at least one argument */
usage(argv[0]);
exit(1);
}
/* compute VID/PID from usbconfig.h so that there is a central source of information */
static void
usage (char *name)
{
fprintf (stderr, "usage:\n");
fprintf (stderr, " %s upload filename.. upload\n", name);
}
int
main (int argc, char **argv)
{
usb_dev_handle *handle = NULL;
const unsigned char rawVid[2] = { USB_CFG_VENDOR_ID }, rawPid[2] =
{
USB_CFG_DEVICE_ID};
char vendor[] = { USB_CFG_VENDOR_NAME, 0 }, product[] =
{
USB_CFG_DEVICE_NAME, 0};
int cnt, vid, pid;
int cnt_crc = 0;
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 ();
if (argc < 2)
{ /* we need at least one argument */
usage (argv[0]);
exit (1);
}
/*
* compute VID/PID from usbconfig.h so that there is a central source
* of information
*/
vid = rawVid[1] * 256 + rawVid[0];
pid = rawPid[1] * 256 + rawPid[0];
/* The following function is in opendevice.c: */
if(usbOpenDevice(&handle, vid, vendor, pid, product, NULL, NULL, NULL) != 0){
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]);
exit(1);
}
fp = fopen (argv[2],"r") ;
if (fp==NULL){
fprintf(stderr, "Cannot open file %s ", argv[2]);
exit(1);
}
read_buffer = (unsigned char*)malloc(READ_BUFFER_SIZE);
crc_buffer = (unsigned char*)malloc(BUFFER_CRC);
memset(crc_buffer,0,BUFFER_CRC);
addr = 0x000000;
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<READ_BUFFER_SIZE; step+=SEND_BUFFER_SIZE){
addr_lo = addr & 0xffff;
addr_hi = (addr>>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;
if (usbOpenDevice (&handle, vid, vendor, pid, product, NULL, NULL, NULL) != 0)
{
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]);
exit (1);
}
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 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;
fp = fopen (argv[2], "r");
if (fp == NULL)
{
fprintf (stderr, "Cannot open file %s ", argv[2]);
exit (1);
}
}
cnt = usb_control_msg(handle,
USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT,
USB_CRC_CHECK, addr_hi, addr_lo,
NULL, 0,
5000);
if(cnt < 1){
if(cnt < 0){
fprintf(stderr, "USB error: %s\n", usb_strerror());
}else{
fprintf(stderr, "only %d bytes received.\n", cnt);
read_buffer = (unsigned char *) malloc (READ_BUFFER_SIZE);
crc_buffer = (unsigned char *) malloc (BUFFER_CRC);
memset (crc_buffer, 0, BUFFER_CRC);
addr = 0x000000;
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 < READ_BUFFER_SIZE; step += SEND_BUFFER_SIZE)
{
addr_lo = addr & 0xffff;
addr_hi = (addr >> 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, 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 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;
}
}
}
}else{
usage(argv[0]);
exit(1);
}
usb_close(handle);
cnt = usb_control_msg (handle,
USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT,
USB_CRC, addr_hi, addr_lo, NULL, 0, 5000);
if (cnt < 1)
{
if (cnt < 0)
{
fprintf (stderr, "USB error: %s\n", usb_strerror ());
}
else
{
fprintf (stderr, "only %d bytes received.\n", cnt);
}
}
}
else if (strcasecmp (argv[1], "crc") == 0)
{
/*
if(argc < 2){
usage(argv[0]);
exit(1);
}
*/
addr = 0x000000;
addr_lo = addr & 0xffff;
addr_hi = (addr >> 16) & 0xff;
printf ("Request CRC for Addr: 0x%06x\n", addr);
cnt = usb_control_msg (handle,
USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT,
USB_CRC_ADDR, addr_hi, addr_lo, NULL, (1 << 15) / 4, 5000);
if (cnt < 1)
{
if (cnt < 0)
{
fprintf (stderr, "USB error: %s\n", usb_strerror ());
}
else
{
fprintf (stderr, "only %d bytes received.\n", cnt);
}
}
}
else
{
usage (argv[0]);
exit (1);
}
usb_close (handle);
return 0;
}

View File

@@ -14,116 +14,148 @@
#include "crc.h"
#define STATE_IDLE 0
#define STATE_UPLOAD 1
#define REQ_IDLE 0
#define REQ_UPLOAD 1
#define RES_CRC 2
#define BUFFER_SIZE 256
extern FILE uart_stdout;
uint32_t rom_addr;
uint32_t addr;
uint8_t bytes_remaining = 0;
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 */
uint32_t req_addr = 0;
uint32_t req_size;
uint8_t req_bank;
uint32_t req_bank_size;
uint8_t req_state = REQ_IDLE;
uint8_t rx_remaining = 0;
uint8_t tx_remaining = 0;
uint16_t sync_errors = 0;
uint8_t tx_buffer[32];
uint8_t data_buffer[4];
uint32_t addr;
void crc_check_memory(uint32_t top_addr){
uint16_t crc = 0;
uint32_t addr;
req_bank = 0;
for (addr=0x000000; addr < top_addr; addr+=BUFFER_SIZE) {
sram_read_buffer(addr,read_buffer,BUFFER_SIZE);
crc = do_crc_update(crc,read_buffer,BUFFER_SIZE);
if (addr && addr%32768 == 0){
printf("crc_check_memory: req_bank: 0x%x Addr: 0x%lx CRC: %x\n",req_bank,addr,crc);
req_bank++;
crc = 0;
}
}
}
void crc_check_memory_range(uint32_t start_addr,uint32_t size){
uint16_t crc = 0;
uint32_t addr;
req_bank = 0;
for (addr=start_addr; addr < start_addr + size; addr+=BUFFER_SIZE) {
sram_read_buffer(addr,read_buffer,BUFFER_SIZE);
crc = do_crc_update(crc,read_buffer,BUFFER_SIZE);
}
tx_buffer[0] = crc & 0xff;
tx_buffer[1] = (crc >> 8) & 0xff;
printf("crc_check_memory_range: Addr: 0x%lx CRC: %x\n",addr,crc);
}
usbMsgLen_t usbFunctionSetup(uchar data[8]){
usbRequest_t *rq = (void *)data;
uint16_t crc = 0;
uint8_t len = 0;
uint8_t ret_len = 0;
if(rq->bRequest == USB_UPLOAD_INIT){
printf("USB_UPLOAD_INIT: reset values\n");
bank=0;
bytes_remaining=0;
crc=0;
req_bank =0;
rx_remaining=0;
req_bank_size= 1 << rq->wValue.word;
sync_errors=0;
printf("USB_UPLOAD_INIT: bank size %li\n",req_bank_size);
}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){
req_state = REQ_UPLOAD;
req_addr = rq->wValue.word;
req_addr = req_addr << 16;
req_addr = req_addr | rq->wIndex.word;
if (rx_remaining){
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;
printf("USB_UPLOAD_ADDR: Out of sync Addr=0x%lx remain=%i packet=%i sync_error=%i\n",req_addr,rx_remaining,rq->wLength.word,sync_errors );
ret_len=0;
}
bytes_remaining = rq->wLength.word;
len = 0xff;
if (rom_addr && rom_addr%32768 == 0){
printf("USB_UPLOAD_ADDR: Bank: 0x%x Addr: 0x%08lx \n",bank,rom_addr);
bank++;
rx_remaining = rq->wLength.word;
ret_len = 0xff;
if (req_addr && req_addr % req_bank_size== 0){
printf("USB_UPLOAD_ADDR: req_bank: 0x%x Addr: 0x%08lx \n",req_bank,req_addr);
req_bank++;
}
len=0xff;
ret_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;
bank = 0;
crc = 0;
printf("USB_CRC_CHECK: Addr 0x%lx \n", rom_addr);
}else if(rq->bRequest == USB_CRC){
req_addr = rq->wValue.word;
req_addr = req_addr << 16;
req_addr = req_addr | rq->wIndex.word;
printf("USB_CRC: Addr 0x%lx \n", req_addr);
cli();
for (addr=0x000000; addr<rom_addr; addr+=BUFFER_SIZE) {
sram_read_buffer(addr,read_buffer,BUFFER_SIZE);
crc = do_crc_update(crc,read_buffer,BUFFER_SIZE);
if (addr && addr%32768 == 0){
printf("USB_CRC_CHECK: Bank: 0x%x Addr: 0x%lx CRC: %x\n",bank,addr,crc);
bank++;
crc = 0;
}
}
crc_check_memory(req_addr);
sei();
}else if(rq->bRequest == USB_CRC_ADDR){
req_addr = rq->wValue.word;
req_addr = req_addr << 16;
req_addr = req_addr | rq->wIndex.word;
printf("USB_CRC_ADDR: Addr: 0x%lx Size: %i\n", req_addr,rq->wLength.word);
req_size = rq->wLength.word;
req_size = req_size << 2;
tx_remaining = 2;
printf("USB_CRC_ADDR: Addr: 0x%lx Size: %li\n", req_addr,req_size);
cli();
//crc_check_memory_range(req_addr,req_size);
sei();
ret_len=2;
}
usbMsgPtr = dataBuffer;
return len; /* default for not implemented requests: return no data back to host */
usbMsgPtr = data_buffer;
return ret_len; /* default for not implemented requests: return no data back to host */
}
uint8_t usbFunctionWrite(uint8_t *data, uint8_t len)
{
if (len > bytes_remaining){
printf("usbFunctionWrite more data than expected remain: %i len: %i\n",bytes_remaining,len);
len = bytes_remaining;
if (len > rx_remaining){
printf("usbFunctionWrite more data than expected remain: %i len: %i\n",rx_remaining,len);
len = rx_remaining;
}
if (state==STATE_UPLOAD){
if (req_state==REQ_UPLOAD){
bytes_remaining -= len;
#if 0
printf("Addr: 0x%08lx Len: %i\n",rom_addr,len);
rx_remaining -= len;
#if 1
printf("usbFunctionWrite addr: 0x%08lx len: %i rx_remaining=%i\n",req_addr,len,rx_remaining);
#endif
cli();
sram_copy(rom_addr,data,len);
sram_copy(req_addr,data,len);
sei();
rom_addr +=len;
req_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);
uint8_t i;
if(len > tx_remaining)
len = tx_remaining;
tx_remaining -= len;
#if 1
printf("usbFunctionRead len=%i tx_remaining=%i \n",len,tx_remaining);
#endif
for (i = 0; i < len; i++) {
*data = tx_buffer[len];
data++;
flash_address.word++;
}
/* flash led on activity */
DLED_TGL;
return len;
}

View File

@@ -16,10 +16,11 @@
#ifndef __REQUESTS_H_INCLUDED__
#define __REQUESTS_H_INCLUDED__
#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
#define USB_UPLOAD_INIT 0
#define USB_UPLOAD_ADDR 1
#define USB_DOWNLOAD_INIT 2
#define USB_DOWNLOAD_ADDR 3
#define USB_CRC 4
#define USB_CRC_ADDR 5
#endif /* __REQUESTS_H_INCLUDED__ */