twiboot/main.c

538 lines
14 KiB
C
Raw Normal View History

2007-09-21 21:39:09 +02:00
/***************************************************************************
* Copyright (C) 11/2019 by Olaf Rempel *
2007-09-21 21:39:09 +02:00
* 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 <avr/io.h>
#include <avr/interrupt.h>
#include <avr/boot.h>
#include <avr/pgmspace.h>
2019-11-03 18:05:29 +01:00
#define VERSION_STRING "TWIBOOT v2.1"
#define EEPROM_SUPPORT 1
#define LED_SUPPORT 1
2010-08-15 17:01:06 +02:00
2020-01-20 23:41:18 +01:00
#define F_CPU 8000000ULL
#define TIMER_DIVISOR 1024
#define TIMER_IRQFREQ_MS 25
#define TIMEOUT_MS 1000
2010-07-11 16:51:50 +02:00
2020-01-20 23:41:18 +01:00
#define TIMER_MSEC2TICKS(x) ((x * F_CPU) / (TIMER_DIVISOR * 1000ULL))
#define TIMER_MSEC2IRQCNT(x) (x / TIMER_IRQFREQ_MS)
2010-07-11 16:51:50 +02:00
2014-10-05 14:32:07 +02:00
#if LED_SUPPORT
#define LED_INIT() DDRB = ((1<<PORTB4) | (1<<PORTB5))
#define LED_RT_ON() PORTB |= (1<<PORTB4)
#define LED_RT_OFF() PORTB &= ~(1<<PORTB4)
#define LED_GN_ON() PORTB |= (1<<PORTB5)
#define LED_GN_OFF() PORTB &= ~(1<<PORTB5)
#define LED_GN_TOGGLE() PORTB ^= (1<<PORTB5)
#define LED_OFF() PORTB = 0x00
2014-10-05 14:32:07 +02:00
#else
#define LED_INIT()
#define LED_RT_ON()
#define LED_RT_OFF()
#define LED_GN_ON()
#define LED_GN_OFF()
#define LED_GN_TOGGLE()
#define LED_OFF()
#endif
2007-09-21 21:39:09 +02:00
2014-10-05 14:32:07 +02:00
#ifndef TWI_ADDRESS
#define TWI_ADDRESS 0x29
2014-10-05 14:32:07 +02:00
#endif
2007-09-21 21:39:09 +02:00
2010-07-11 16:51:50 +02:00
/* SLA+R */
#define CMD_WAIT 0x00
#define CMD_READ_VERSION 0x01
2020-01-20 23:44:22 +01:00
#define CMD_ACCESS_MEMORY 0x02
2010-07-11 16:51:50 +02:00
/* internal mappings */
2020-01-20 23:44:22 +01:00
#define CMD_ACCESS_CHIPINFO (0x10 | CMD_ACCESS_MEMORY)
#define CMD_ACCESS_FLASH (0x20 | CMD_ACCESS_MEMORY)
#define CMD_ACCESS_EEPROM (0x30 | CMD_ACCESS_MEMORY)
2010-07-11 16:51:50 +02:00
/* SLA+W */
#define CMD_SWITCH_APPLICATION CMD_READ_VERSION
2010-07-11 16:51:50 +02:00
/* internal mappings */
#define CMD_BOOT_BOOTLOADER (0x10 | CMD_SWITCH_APPLICATION) /* only in APP */
#define CMD_BOOT_APPLICATION (0x20 | CMD_SWITCH_APPLICATION)
2010-07-11 16:51:50 +02:00
/* CMD_SWITCH_APPLICATION parameter */
#define BOOTTYPE_BOOTLOADER 0x00 /* only in APP */
#define BOOTTYPE_APPLICATION 0x80
2010-07-11 16:51:50 +02:00
/* CMD_{READ|WRITE}_* parameter */
#define MEMTYPE_CHIPINFO 0x00
#define MEMTYPE_FLASH 0x01
#define MEMTYPE_EEPROM 0x02
2007-09-21 21:39:09 +02:00
/*
* LED_GN flashes with 20Hz (while bootloader is running)
* LED_RT flashes on TWI activity
2008-02-17 23:04:52 +01:00
*
2007-09-21 21:39:09 +02:00
* bootloader twi-protocol:
2010-07-11 16:51:50 +02:00
* - abort boot timeout:
* SLA+W, 0x00, STO
2007-09-21 21:39:09 +02:00
*
2010-07-11 16:51:50 +02:00
* - show bootloader version
* SLA+W, 0x01, SLA+R, {16 bytes}, STO
2007-09-21 21:39:09 +02:00
*
2010-07-11 16:51:50 +02:00
* - start application
* SLA+W, 0x01, 0x80, STO
2007-09-21 21:39:09 +02:00
*
2010-08-15 16:44:54 +02:00
* - read chip info: 3byte signature, 1byte page size, 2byte flash size, 2byte eeprom size
* SLA+W, 0x02, 0x00, 0x00, 0x00, SLA+R, {8 bytes}, STO
2007-09-21 21:39:09 +02:00
*
2010-07-11 16:51:50 +02:00
* - read one (or more) flash bytes
* SLA+W, 0x02, 0x01, addrh, addrl, SLA+R, {* bytes}, STO
2007-09-21 21:39:09 +02:00
*
* - read one (or more) eeprom bytes
2010-07-11 16:51:50 +02:00
* SLA+W, 0x02, 0x02, addrh, addrl, SLA+R, {* bytes}, STO
*
* - write one flash page
* SLA+W, 0x02, 0x01, addrh, addrl, {* bytes}, STO
2007-09-21 21:39:09 +02:00
*
2010-07-11 16:51:50 +02:00
* - write one (or more) eeprom bytes
* SLA+W, 0x02, 0x02, addrh, addrl, {* bytes}, STO
2007-09-21 21:39:09 +02:00
*/
const static uint8_t info[16] = VERSION_STRING;
2010-08-15 16:44:54 +02:00
const static uint8_t chipinfo[8] = {
SIGNATURE_0, SIGNATURE_1, SIGNATURE_2,
SPM_PAGESIZE,
2010-08-15 16:44:54 +02:00
(BOOTLOADER_START >> 8) & 0xFF,
BOOTLOADER_START & 0xFF,
2010-08-15 16:44:54 +02:00
2010-08-15 17:01:06 +02:00
#if (EEPROM_SUPPORT)
((E2END +1) >> 8 & 0xFF),
(E2END +1) & 0xFF
2010-08-15 17:01:06 +02:00
#else
0x00, 0x00
2010-08-15 17:01:06 +02:00
#endif
2010-08-15 16:44:54 +02:00
};
2007-09-21 21:39:09 +02:00
2020-01-20 23:41:18 +01:00
static uint8_t boot_timeout = TIMER_MSEC2IRQCNT(TIMEOUT_MS);
static uint8_t cmd = CMD_WAIT;
2007-09-21 21:39:09 +02:00
/* flash buffer */
static uint8_t buf[SPM_PAGESIZE];
static uint16_t addr;
/* *************************************************************************
* write_flash_page
* ************************************************************************* */
2010-07-11 16:51:50 +02:00
static void write_flash_page(void)
2007-09-21 21:39:09 +02:00
{
uint16_t pagestart = addr;
uint8_t size = SPM_PAGESIZE;
uint8_t *p = buf;
2007-09-21 21:39:09 +02:00
if (pagestart >= BOOTLOADER_START)
{
return;
}
2007-09-21 21:39:09 +02:00
boot_page_erase(pagestart);
boot_spm_busy_wait();
2007-09-21 21:39:09 +02:00
do {
uint16_t data = *p++;
data |= *p++ << 8;
boot_page_fill(addr, data);
2007-09-21 21:39:09 +02:00
addr += 2;
size -= 2;
} while (size);
boot_page_write(pagestart);
boot_spm_busy_wait();
boot_rww_enable();
} /* write_flash_page */
2007-09-21 21:39:09 +02:00
2010-08-15 17:01:06 +02:00
#if (EEPROM_SUPPORT)
/* *************************************************************************
* read_eeprom_byte
* ************************************************************************* */
2010-07-11 16:51:50 +02:00
static uint8_t read_eeprom_byte(void)
{
EEARL = addr;
EEARH = (addr >> 8);
EECR |= (1<<EERE);
addr++;
return EEDR;
} /* read_eeprom_byte */
2010-07-11 16:51:50 +02:00
/* *************************************************************************
* write_eeprom_byte
* ************************************************************************* */
2010-07-11 16:51:50 +02:00
static void write_eeprom_byte(uint8_t val)
2007-09-21 21:39:09 +02:00
{
EEARL = addr;
EEARH = (addr >> 8);
EEDR = val;
addr++;
#if defined (EEWE)
EECR |= (1<<EEMWE);
EECR |= (1<<EEWE);
#elif defined (EEPE)
EECR |= (1<<EEMPE);
EECR |= (1<<EEPE);
#else
#error "EEWE/EEPE not defined"
2010-08-15 16:44:54 +02:00
#endif
eeprom_busy_wait();
} /* write_eeprom_byte */
2010-08-15 17:01:06 +02:00
#endif /* EEPROM_SUPPORT */
2007-09-21 21:39:09 +02:00
/* *************************************************************************
* TWI_data_write
* ************************************************************************* */
static uint8_t TWI_data_write(uint8_t bcnt, uint8_t data)
2007-09-21 21:39:09 +02:00
{
uint8_t ack = 0x01;
switch (bcnt)
{
case 0:
switch (data)
{
case CMD_SWITCH_APPLICATION:
2020-01-20 23:44:22 +01:00
case CMD_ACCESS_MEMORY:
/* no break */
case CMD_WAIT:
/* abort countdown */
boot_timeout = 0;
break;
default:
/* boot app now */
cmd = CMD_BOOT_APPLICATION;
ack = 0x00;
break;
}
cmd = data;
break;
case 1:
switch (cmd)
{
case CMD_SWITCH_APPLICATION:
if (data == BOOTTYPE_APPLICATION)
{
cmd = CMD_BOOT_APPLICATION;
}
ack = 0x00;
break;
2020-01-20 23:44:22 +01:00
case CMD_ACCESS_MEMORY:
if (data == MEMTYPE_CHIPINFO)
{
2020-01-20 23:44:22 +01:00
cmd = CMD_ACCESS_CHIPINFO;
}
else if (data == MEMTYPE_FLASH)
{
2020-01-20 23:44:22 +01:00
cmd = CMD_ACCESS_FLASH;
}
2010-08-15 17:01:06 +02:00
#if (EEPROM_SUPPORT)
else if (data == MEMTYPE_EEPROM)
{
2020-01-20 23:44:22 +01:00
cmd = CMD_ACCESS_EEPROM;
}
#endif /* (EEPROM_SUPPORT) */
else
{
ack = 0x00;
}
break;
default:
ack = 0x00;
break;
}
break;
case 2:
case 3:
addr <<= 8;
addr |= data;
break;
default:
switch (cmd)
{
2020-01-20 23:44:22 +01:00
case CMD_ACCESS_FLASH:
buf[bcnt -4] = data;
if (bcnt >= sizeof(buf) +3)
{
write_flash_page();
ack = 0x00;
}
break;
2010-08-15 17:01:06 +02:00
#if (EEPROM_SUPPORT)
2020-01-20 23:44:22 +01:00
case CMD_ACCESS_EEPROM:
write_eeprom_byte(data);
break;
#endif /* (EEPROM_SUPPORT) */
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;
2020-01-20 23:44:22 +01:00
case CMD_ACCESS_CHIPINFO:
bcnt %= sizeof(chipinfo);
data = chipinfo[bcnt];
break;
2020-01-20 23:44:22 +01:00
case CMD_ACCESS_FLASH:
data = pgm_read_byte_near(addr++);
break;
#if (EEPROM_SUPPORT)
2020-01-20 23:44:22 +01:00
case CMD_ACCESS_EEPROM:
data = read_eeprom_byte();
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<<TWEA);
bcnt = 0;
}
break;
/* SLA+R received, ACK returned -> 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:
/* STOP or repeated START -> IDLE */
case 0xA0:
/* prev. SLA+R, data sent, NACK returned -> IDLE */
case 0xC0:
LED_RT_OFF();
control |= (1<<TWEA);
break;
/* illegal state(s) -> reset hardware */
default:
control |= (1<<TWSTO);
break;
}
TWCR = (1<<TWINT) | control;
} /* TWI_vect */
/* *************************************************************************
* TIMER0_OVF_vect
* ************************************************************************* */
static void TIMER0_OVF_vect(void)
2007-09-21 21:39:09 +02:00
{
/* restart timer */
2020-01-20 23:41:18 +01:00
TCNT0 = 0xFF - TIMER_MSEC2TICKS(TIMER_IRQFREQ_MS);
2007-09-21 21:39:09 +02:00
/* blink LED while running */
LED_GN_TOGGLE();
2007-09-21 21:39:09 +02:00
/* count down for app-boot */
if (boot_timeout > 1)
{
boot_timeout--;
}
else if (boot_timeout == 1)
{
/* trigger app-boot */
cmd = CMD_BOOT_APPLICATION;
}
} /* TIMER0_OVF_vect */
2007-09-21 21:39:09 +02:00
2010-08-15 16:44:54 +02:00
static void (*jump_to_app)(void) __attribute__ ((noreturn)) = 0x0000;
2007-09-21 21:39:09 +02:00
/* *************************************************************************
* 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<<WDCE) | (1<<WDE);
WDTCSR = (0<<WDE);
} /* disable_wdt_timer */
2010-08-15 16:44:54 +02:00
#endif
/* *************************************************************************
* main
* ************************************************************************* */
int main(void) __attribute__ ((OS_main, section (".init9")));
2007-09-21 21:39:09 +02:00
int main(void)
{
LED_INIT();
LED_GN_ON();
2007-09-21 21:39:09 +02:00
/* timer0: running with F_CPU/1024 */
#if defined (TCCR0)
TCCR0 = (1<<CS02) | (1<<CS00);
#elif defined (TCCR0B)
TCCR0B = (1<<CS02) | (1<<CS00);
#else
#error "TCCR0(B) not defined"
2010-08-15 16:44:54 +02:00
#endif
2007-09-21 21:39:09 +02:00
/* TWI init: set address, auto ACKs */
TWAR = (TWI_ADDRESS<<1);
TWCR = (1<<TWEA) | (1<<TWEN);
2007-09-21 21:39:09 +02:00
while (cmd != CMD_BOOT_APPLICATION)
{
if (TWCR & (1<<TWINT))
{
TWI_vect();
}
#if defined (TIFR)
if (TIFR & (1<<TOV0))
{
TIMER0_OVF_vect();
TIFR = (1<<TOV0);
}
#elif defined (TIFR0)
if (TIFR0 & (1<<TOV0))
{
TIMER0_OVF_vect();
TIFR0 = (1<<TOV0);
}
#else
#error "TIFR(0) not defined"
#endif
}
2007-09-21 21:39:09 +02:00
/* Disable TWI but keep address! */
TWCR = 0x00;
2007-09-21 21:39:09 +02:00
/* disable timer0 */
#if defined (TCCR0)
TCCR0 = 0x00;
#elif defined (TCCR0B)
TCCR0B = 0x00;
#else
#error "TCCR0(B) not defined"
2010-10-17 18:23:41 +02:00
#endif
2007-09-21 21:39:09 +02:00
LED_OFF();
2007-09-21 21:39:09 +02:00
uint16_t wait = 0x0000;
do {
__asm volatile ("nop");
} while (--wait);
2010-10-17 18:24:12 +02:00
jump_to_app();
} /* main */