diff --git a/Makefile b/Makefile index 2633c8d..91303b0 100644 --- a/Makefile +++ b/Makefile @@ -11,8 +11,8 @@ MCU=attiny2313 AVRDUDE_MCU=attiny2313 AVRDUDE_FUSES=lfuse:w:0xff:m hfuse:w:0xdb:m efuse:w:0xff:m -#AVRDUDE_PROG := -c avr910 -b 115200 -P /dev/ispprog -AVRDUDE_PROG := -c dragon_isp -P usb +AVRDUDE_PROG := -c avr910 -b 115200 -P /dev/ispprog +#AVRDUDE_PROG := -c dragon_isp -P usb # --------------------------------------------------------------------------- diff --git a/main.c b/main.c index b0c96ac..f01fe13 100644 --- a/main.c +++ b/main.c @@ -65,11 +65,62 @@ #define UART_CALC_BAUDRATE(baudRate) (((uint32_t)F_CPU) / (((uint32_t)baudRate)*16) -1) +/* ************************************************************************* */ + +#define MSGTYPE_VERSION_REQ 0x01 /* no payload */ +#define MSGTYPE_PAGESIZE_REQ 0x02 /* no payload */ +#define MSGTYPE_CONFIG_REQ 0x03 /* eprom_type(1), pagesize(1), reset_polarity(1) */ +#define MSGTYPE_PROGMODE_REQ 0x04 /* progmode(1) */ +#define MSGTYPE_SETADDRESS_REQ 0x05 /* address(3) msb first */ +#define MSGTYPE_WRITE_REQ 0x06 /* data(0-pagesize) */ +#define MSGTYPE_READ_REQ 0x07 /* length(1) */ + +#define MSGTYPE_ERROR_RSP 0x80 /* error_code(1) */ +#define MSGTYPE_VERSION_RSP 0x81 /* version(?) */ +#define MSGTYPE_PAGESIZE_RSP 0x82 /* pagesize(1) */ +#define MSGTYPE_CONFIG_RSP 0x83 /* no payload */ +#define MSGTYPE_PROGMODE_RSP 0x84 /* no payload */ +#define MSGTYPE_SETADDRESS_RSP 0x85 /* no payload */ +#define MSGTYPE_WRITE_RSP 0x86 /* no payload */ +#define MSGTYPE_READ_RSP 0x87 /* data(0-pagesize) */ + +#define SUCCESS 0x00 +#define ERROR_UNKNOWN_COMMAND 0x01 /* unknown message type */ +#define ERROR_NOT_SUPPORTED 0x02 /* command not supported */ +#define ERROR_INVALID_MODE 0x03 /* invalid progmode */ +#define ERROR_INVALID_PARAMETER 0x04 /* invalid parameter in request */ +#define ERROR_INVALID_ADDRESS 0x05 /* write outside of configured region */ + +#define RESET_POLARITY_LOW 0x00 /* low active reset */ +#define RESET_POLARITY_HIGH 0x01 /* high active reset */ + +#define EPROM_TYPE_2K 0x02 /* 2716 */ +#define EPROM_TYPE_4K 0x04 /* 2732 */ +#define EPROM_TYPE_8K 0x08 /* 2764 */ +#define EPROM_TYPE_16K 0x10 /* 27128 */ +#define EPROM_TYPE_32K 0x20 /* 27256 */ +#define EPROM_TYPE_64K 0x40 /* 27512 */ +#define EPROM_TYPE_128K 0x80 /* 27010 */ + +#define PROGMODE_DISABLED 0x00 /* target running, no write access to RAM */ +#define PROGMODE_ENABLED 0x01 /* target reset, write access to RAM */ + +#define PAGESIZE_MAX 128 + +/* ************************************************************************* */ + struct _globdata { - uint8_t address_mask; - uint8_t reset_polarity; + uint32_t address_max; + uint32_t address; + + uint8_t progmode; + uint8_t reset_polarity; + uint8_t pagesize; + + uint8_t address_mask; }; +static const uint8_t version_str[] = "epromsim v1.00"; static struct _globdata gdata = { 0 }; @@ -100,10 +151,14 @@ static void shift_data(uint8_t data) { uint8_t mask; - for (mask = 0x01; mask != 0; mask <<= 1) { - if (data & mask) { + for (mask = 0x01; mask != 0; mask <<= 1) + { + if (data & mask) + { PORTB |= (1< 0xFFFF) { - PORTD |= (1< 0xFFFF); + + shift_data(ser_recv()); + store_pulse(); + write_pulse(); + address_inc_pulse(); + } +} /* write_data */ + + +/* ************************************************************************* + * fill RAM with 0xFF * ************************************************************************* */ static void do_clear(void) { - do_reset(1); + uint16_t i = 0xFFFF; shift_data(0xFF); - uint16_t i = 0xFFFF; - PORTD &= ~(1< 0) && + (length <= gdata.pagesize) + ) + { + if ((gdata.address >= gdata.address_max) || + ((gdata.address + length) > gdata.address_max) + ) + { + error_code = ERROR_INVALID_ADDRESS; + } + else if (gdata.progmode == PROGMODE_DISABLED) + { + error_code = ERROR_INVALID_MODE; + } + else + { + write_data(length); + + ser_send(MSGTYPE_WRITE_RSP); + ser_send(0x00); + error_code = SUCCESS; + } + } + break; + + case MSGTYPE_READ_REQ: + error_code = ERROR_NOT_SUPPORTED; + break; + default: - ser_send('?'); + error_code = ERROR_UNKNOWN_COMMAND; break; } + + if (error_code != SUCCESS) + { + /* read remaining request */ + while (length--) + { + (void)ser_recv(); + } + + ser_send(MSGTYPE_ERROR_RSP); + ser_send(0x01); + ser_send(error_code); + } } -} +} /* main */