From e013aea33cacc8f4133d9a769cf420511f07806f Mon Sep 17 00:00:00 2001 From: Olaf Rempel Date: Sun, 11 Jul 2010 16:51:50 +0200 Subject: [PATCH] new protocol --- main.c | 248 ++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 157 insertions(+), 91 deletions(-) diff --git a/main.c b/main.c index 5427f5a..b0e95be 100644 --- a/main.c +++ b/main.c @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 09/2007 by Olaf Rempel * + * Copyright (C) 07/2010 by Olaf Rempel * * razzor@kopf-tisch.de * * * * This program is free software; you can redistribute it and/or modify * @@ -24,66 +24,102 @@ #define F_CPU 8000000 #include +/* + * atmega88: + * Fuse E: 0xfa (512 words bootloader) + * Fuse H: 0xdd (2.7V BOD) + * Fuse L: 0xc2 (8Mhz internal RC-Osz.) + */ + +/* 25ms @8MHz */ +#define TIMER_RELOAD (0xFF - 195) + +/* 40 * 25ms */ +#define TIMEOUT 40 + #define LED_RT (1<> 8); + EECR |= (1<> 8); @@ -123,7 +168,9 @@ void write_eeprom_byte(uint8_t val) ISR(TWI_vect) { - static uint8_t bcnt = 0; + static uint8_t bcnt; + uint8_t data; + uint8_t ack = (1< receive data and ACK */ @@ -135,64 +182,87 @@ ISR(TWI_vect) /* prev. SLA+W, data received, ACK returned -> receive data and ACK */ case 0x80: - if (bcnt == 0) { - cmd = TWDR; - switch (cmd) { - case CMD_GET_INFO: - case CMD_GET_SIGNATURE: - case CMD_WRITE_FLASH: - case CMD_READ_FLASH: - case CMD_WRITE_EEPROM: - case CMD_READ_EEPROM: + data = TWDR; + switch (bcnt) { + case 0: + switch (data) { + case CMD_SWITCH_APPLICATION: + case CMD_WRITE_MEMORY: + bcnt++; + /* no break */ + + case CMD_WAIT: /* abort countdown */ boot_timeout = 0; - - case CMD_BOOT_APPLICATION: - bcnt++; break; default: - cmd = CMD_WAIT; - bcnt = 0; + /* boot app now */ + data = CMD_BOOT_APPLICATION; + ack = (0<> 8)) + case CMD_WRITE_MEMORY: bcnt++; - else - bcnt = 0; + if (data == MEMTYPE_SIGNATURE) { + cmd = CMD_WRITE_SIGNATURE; - } else if (bcnt == 4) { - if (TWDR == (COOKIE & 0xFF)) - bcnt++; - else - bcnt = 0; + } else if (data == MEMTYPE_FLASH) { + cmd = CMD_WRITE_FLASH; - } else if (bcnt >= 5 && cmd == CMD_WRITE_FLASH) { - buf[bcnt -5] = TWDR; - if (bcnt < sizeof(buf) +4) { - bcnt++; + } else if (data == MEMTYPE_EEPROM) { + cmd = CMD_WRITE_EEPROM; - } else { - write_flash_page(); - bcnt = 0; + } else { + ack = (0<= 5 && cmd == CMD_WRITE_EEPROM) { - write_eeprom_byte(TWDR); + case 2: + case 3: + addr <<= 8; + addr |= data; bcnt++; + break; + + default: + switch (cmd) { + case CMD_WRITE_FLASH: + buf[bcnt -4] = data; + if (bcnt < sizeof(buf) +3) { + bcnt++; + } else { + write_flash_page(); + ack = (0< send data */ @@ -203,33 +273,30 @@ ISR(TWI_vect) /* prev. SLA+R, data sent, ACK returned -> send data */ case 0xB8: switch (cmd) { - case CMD_GET_INFO: - TWDR = info[bcnt++]; + case CMD_READ_VERSION: + data = info[bcnt++]; bcnt %= sizeof(info); break; - case CMD_GET_SIGNATURE: - TWDR = signature[bcnt++]; + case CMD_READ_SIGNATURE: + data = signature[bcnt++]; bcnt %= sizeof(signature); break; case CMD_READ_FLASH: - TWDR = pgm_read_byte_near(addr++); + data = pgm_read_byte_near(addr++); break; case CMD_READ_EEPROM: - EEARL = addr; - EEARH = (addr >> 8); - EECR |= (1<