/*************************************************************************** * Copyright (C) 11/2019 by Olaf Rempel * * razzor@kopf-tisch.de * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; version 2 of the License, * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include #include #include #include #define VERSION_STRING "TWIBOOT v2.1" #define EEPROM_SUPPORT 1 #define LED_SUPPORT 1 #define USE_CLOCKSTRETCH 0 #define F_CPU 8000000ULL #define TIMER_DIVISOR 1024 #define TIMER_IRQFREQ_MS 25 #define TIMEOUT_MS 1000 #define TIMER_MSEC2TICKS(x) ((x * F_CPU) / (TIMER_DIVISOR * 1000ULL)) #define TIMER_MSEC2IRQCNT(x) (x / TIMER_IRQFREQ_MS) #if LED_SUPPORT #define LED_INIT() DDRB = ((1<> 8) & 0xFF, BOOTLOADER_START & 0xFF, #if (EEPROM_SUPPORT) ((E2END +1) >> 8 & 0xFF), (E2END +1) & 0xFF #else 0x00, 0x00 #endif }; static uint8_t boot_timeout = TIMER_MSEC2IRQCNT(TIMEOUT_MS); static uint8_t cmd = CMD_WAIT; /* flash buffer */ static uint8_t buf[SPM_PAGESIZE]; static uint16_t addr; /* ************************************************************************* * write_flash_page * ************************************************************************* */ static void write_flash_page(void) { uint16_t pagestart = addr; uint8_t size = SPM_PAGESIZE; uint8_t *p = buf; if (pagestart < BOOTLOADER_START) { boot_page_erase(pagestart); boot_spm_busy_wait(); do { uint16_t data = *p++; data |= *p++ << 8; boot_page_fill(addr, data); addr += 2; size -= 2; } while (size); boot_page_write(pagestart); boot_spm_busy_wait(); boot_rww_enable(); } } /* write_flash_page */ #if (EEPROM_SUPPORT) /* ************************************************************************* * read_eeprom_byte * ************************************************************************* */ static uint8_t read_eeprom_byte(uint16_t address) { EEARL = address; EEARH = (address >> 8); EECR |= (1<> 8); EEDR = val; addr++; #if defined (EEWE) EECR |= (1<= (sizeof(buf) -2)) { ack = 0x00; } if ((cmd == CMD_ACCESS_FLASH) && (pos >= (sizeof(buf) -1)) ) { #if (USE_CLOCKSTRETCH) write_flash_page(); #else cmd = CMD_WRITE_FLASH_PAGE; #endif } break; } default: ack = 0x00; break; } break; } return ack; } /* TWI_data_write */ /* ************************************************************************* * TWI_data_read * ************************************************************************* */ static uint8_t TWI_data_read(uint8_t bcnt) { uint8_t data; switch (cmd) { case CMD_READ_VERSION: bcnt %= sizeof(info); data = info[bcnt]; break; case CMD_ACCESS_CHIPINFO: bcnt %= sizeof(chipinfo); data = chipinfo[bcnt]; break; case CMD_ACCESS_FLASH: data = pgm_read_byte_near(addr++); break; #if (EEPROM_SUPPORT) case CMD_ACCESS_EEPROM: data = read_eeprom_byte(addr++); break; #endif /* (EEPROM_SUPPORT) */ default: data = 0xFF; break; } return data; } /* TWI_data_read */ /* ************************************************************************* * TWI_vect * ************************************************************************* */ static void TWI_vect(void) { static uint8_t bcnt; uint8_t control = TWCR; switch (TWSR & 0xF8) { /* SLA+W received, ACK returned -> receive data and ACK */ case 0x60: bcnt = 0; LED_RT_ON(); break; /* prev. SLA+W, data received, ACK returned -> receive data and ACK */ case 0x80: if (TWI_data_write(bcnt++, TWDR) == 0x00) { control &= ~(1< send data */ case 0xA8: bcnt = 0; LED_RT_ON(); /* prev. SLA+R, data sent, ACK returned -> send data */ case 0xB8: TWDR = TWI_data_read(bcnt++); break; /* prev. SLA+W, data received, NACK returned -> IDLE */ case 0x88: TWI_data_write(bcnt++, TWDR); /* fall through */ /* STOP or repeated START -> IDLE */ case 0xA0: #if (USE_CLOCKSTRETCH == 0) if ((cmd == CMD_WRITE_FLASH_PAGE) #if (EEPROM_SUPPORT) || (cmd == CMD_WRITE_EEPROM_PAGE) #endif ) { /* disable ACK for now, re-enable after page write */ control &= ~(1< IDLE */ case 0xC0: LED_RT_OFF(); control |= (1< reset hardware */ default: control |= (1< 1) { boot_timeout--; } else if (boot_timeout == 1) { /* trigger app-boot */ cmd = CMD_BOOT_APPLICATION; } } /* TIMER0_OVF_vect */ static void (*jump_to_app)(void) __attribute__ ((noreturn)) = 0x0000; /* ************************************************************************* * init1 * ************************************************************************* */ void init1(void) __attribute__((naked, section(".init1"))); void init1(void) { /* make sure r1 is 0x00 */ asm volatile ("clr __zero_reg__"); /* on some MCUs the stack pointer defaults NOT to RAMEND */ #if defined(__AVR_ATmega8__) || defined(__AVR_ATmega8515__) || \ defined(__AVR_ATmega8535__) || defined (__AVR_ATmega16__) || \ defined (__AVR_ATmega32__) || defined (__AVR_ATmega64__) || \ defined (__AVR_ATmega128__) || defined (__AVR_ATmega162__) SP = RAMEND; #endif } /* init1 */ /* * For newer devices the watchdog timer remains active even after a * system reset. So disable it as soon as possible. * automagically called on startup */ #if defined (__AVR_ATmega88__) || defined (__AVR_ATmega168__) || \ defined (__AVR_ATmega328P__) /* ************************************************************************* * disable_wdt_timer * ************************************************************************* */ void disable_wdt_timer(void) __attribute__((naked, section(".init3"))); void disable_wdt_timer(void) { MCUSR = 0; WDTCSR = (1<