From a6a64dbf33d6b8fe0d1345d8c2dc6708500ea34c Mon Sep 17 00:00:00 2001 From: Olaf Rempel Date: Sun, 19 Feb 2012 12:54:54 +0100 Subject: [PATCH] store SPI speed in eeprom --- ispprog.c | 128 ++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 111 insertions(+), 17 deletions(-) diff --git a/ispprog.c b/ispprog.c index 81981d1..b7b9057 100644 --- a/ispprog.c +++ b/ispprog.c @@ -19,9 +19,11 @@ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include +#include #include #include #include +#include #define ARRAY_SIZE(x) (sizeof(x) / sizeof(*x)) @@ -99,9 +101,6 @@ static struct _device devices[] PROGMEM = { { { 0x1E, 0x97, 0x02 }, 0x43, 0x7F, POLL_FF }, /* mega128 */ }; -/* sorted devcodes from devices[], terminated with 0x00 */ -static uint8_t devcodes[] PROGMEM = { 0x13, 0x20, 0x38, 0x41, 0x43, 0x45, 0x5E, 0x72, 0x74, 0x76, 0x00 }; - #define EV_NONE 0 #define EV_STATE_ENTER 1 #define EV_BUTTON_PRESSED 2 @@ -115,6 +114,7 @@ static uint8_t devcodes[] PROGMEM = { 0x13, 0x20, 0x38, 0x41, 0x43, 0x45, 0x5E, #define STATE_SPEED2 4 #define STATE_SPEED3 5 #define STATE_SPEED4 6 +#define STATE_NVRAM_STORE 7 /* start nvram_write (internal state) */ #define LED_OFF 0x00 #define LED_SLOW 0x20 @@ -165,6 +165,76 @@ static uint8_t devcodes[] PROGMEM = { 0x13, 0x20, 0x38, 0x41, 0x43, 0x45, 0x5E, #define CMD_WRITE_FUSE_E_1 0xAC /* not used */ #define CMD_WRITE_FUSE_E_2 0xA4 /* not used */ +struct _nvdata { + uint8_t nvram_size; /* first */ + uint8_t spi_mode; + uint16_t nvram_crc; /* last */ +}; + +static uint8_t nvram_write_pos; +static struct _nvdata nvram_data; +static struct _nvdata nvram_eeprom EEMEM; +static struct _nvdata nvram_defaults PROGMEM = { .spi_mode = SPI_MODE4 }; + +/* create crc and store nvram data to eeprom */ +static void nvram_start_write(void) +{ + uint8_t i; + uint16_t crc = 0x0000; + uint8_t *tmp = (uint8_t *)&nvram_data; + + nvram_data.nvram_size = sizeof(struct _nvdata); + + for (i = 0; i < sizeof(struct _nvdata) -2; i++) { + crc = _crc_ccitt_update(crc, *tmp++); + } + + nvram_data.nvram_crc = crc; + nvram_write_pos = 0; + + EEARL = nvram_write_pos; + EEARH = 0x00; + EEDR = ((uint8_t *)&nvram_data)[nvram_write_pos++]; + EECR |= (1< limit) && (devcode < search)) { + search = devcode; + } + } + + if (search == 0xFF) + break; + + ser_send(search); + limit = search; } + ser_send(0x00); break; } @@ -644,7 +729,7 @@ static void cmdloop(void) } } -static uint16_t statemachine(uint8_t event) +static uint16_t button_statemachine(uint8_t event) { static uint8_t oldstate; uint8_t state = oldstate; @@ -721,7 +806,7 @@ static uint16_t statemachine(uint8_t event) state = STATE_SPEED2; } else if (event == EV_TIMEOUT) { - state = STATE_IDLE; + state = STATE_NVRAM_STORE; SPCR = SPI_MODE1; } break; @@ -735,7 +820,7 @@ static uint16_t statemachine(uint8_t event) state = STATE_SPEED3; } else if (event == EV_TIMEOUT) { - state = STATE_IDLE; + state = STATE_NVRAM_STORE; SPCR = SPI_MODE2; } break; @@ -749,7 +834,7 @@ static uint16_t statemachine(uint8_t event) state = STATE_SPEED4; } else if (event == EV_TIMEOUT) { - state = STATE_IDLE; + state = STATE_NVRAM_STORE; SPCR = SPI_MODE3; } break; @@ -763,7 +848,7 @@ static uint16_t statemachine(uint8_t event) state = STATE_SPEED1; } else if (event == EV_TIMEOUT) { - state = STATE_IDLE; + state = STATE_NVRAM_STORE; SPCR = SPI_MODE4; } break; @@ -773,6 +858,12 @@ static uint16_t statemachine(uint8_t event) break; } + if (state == STATE_NVRAM_STORE) { + state = STATE_IDLE; + nvram_data.spi_mode = SPCR; + nvram_start_write(); + } + if (event == EV_STATE_ENTER) { oldstate = state; } @@ -813,7 +904,7 @@ ISR(TIMER0_OVF_vect) } if (event != EV_NONE) { - uint16_t new_timer = statemachine(event); + uint16_t new_timer = button_statemachine(event); if (new_timer == 0xFFFF) { timer = 0; @@ -832,6 +923,7 @@ ISR(TIMER0_OVF_vect) } } +int main(void) __attribute__ ((noreturn)); int main(void) { /* ISP_RESET and ISP_LED are outputs, pullup SlaveSelect */ @@ -846,8 +938,11 @@ int main(void) UCSRB = (1<