From 914150f707b48bae0b5950424afdd78faf467b54 Mon Sep 17 00:00:00 2001 From: Olaf Rempel Date: Fri, 21 Sep 2007 21:39:09 +0200 Subject: [PATCH] initial commit --- .gitignore | 6 ++ Makefile | 44 ++++++++ main.c | 294 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 344 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 main.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6d36148 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +*.o +*.elf +*.bin +*.hex +*.lst +*.map diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..cb6161a --- /dev/null +++ b/Makefile @@ -0,0 +1,44 @@ +PRG = twiboot +OBJ = main.o +MCU_TARGET = atmega8 +OPTIMIZE = -Os + +DEFS = +LIBS = + +# Override is only needed by avr-lib build system. +override CFLAGS = -g -Wall $(OPTIMIZE) -mmcu=$(MCU_TARGET) $(DEFS) +override LDFLAGS = -Wl,-Map,$(PRG).map,--section-start=.text=0x1C00 + +CC = avr-gcc +OBJCOPY = avr-objcopy +OBJDUMP = avr-objdump +SIZE = avr-size + +all: $(PRG).elf lst text + $(SIZE) -x -A $(PRG).elf + +$(PRG).elf: $(OBJ) + $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS) + +clean: + rm -rf *.o *.lst *.map $(PRG).elf *.hex *.bin + +lst: $(PRG).lst + +%.lst: %.elf + $(OBJDUMP) -h -S $< > $@ + +text: hex bin + +hex: $(PRG).hex +bin: $(PRG).bin + +%.hex: %.elf + $(OBJCOPY) -j .text -j .data -O ihex $< $@ + +%.bin: %.elf + $(OBJCOPY) -j .text -j .data -O binary $< $@ + +install: text + uisp -dprog=avr910 -dserial=/dev/ttyS0 -dspeed=115200 -dpart=M8 --erase --upload if=$(PRG).hex diff --git a/main.c b/main.c new file mode 100644 index 0000000..be91d8c --- /dev/null +++ b/main.c @@ -0,0 +1,294 @@ +/*************************************************************************** + * Copyright (C) 09/2007 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 F_CPU 8000000 +#include + +#define LED_RT (1<= APP_END) + return; + + 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(); +} + +void write_eeprom_byte(uint8_t val) +{ + EEARL = addr; + EEARH = (addr >> 8); + EEDR = val; + addr++; + + EECR |= (1< receive data and ACK */ + case 0x60: + bcnt = 0; + PORTB |= LED_RT; + TWCR |= (1< 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: + /* abort countdown */ + boot_timeout = 0; + + case CMD_BOOT_APPLICATION: + bcnt++; + break; + + default: + cmd = CMD_WAIT; + bcnt = 0; + break; + } + + } else if (bcnt == 1) { + addr = (TWDR << 8); + bcnt++; + + } else if (bcnt == 2) { + addr |= TWDR; + bcnt++; + + } else if (bcnt >= 3 && cmd == CMD_WRITE_FLASH) { + buf[bcnt -3] = TWDR; + if (bcnt < sizeof(buf) +2) { + bcnt++; + + } else { + write_flash_page(); + bcnt = 0; + } + + } else if (bcnt >= 3 && cmd == CMD_WRITE_EEPROM) { + write_eeprom_byte(TWDR); + bcnt++; + } + + TWCR |= (1< send data */ + case 0xA8: + bcnt = 0; + PORTB |= LED_RT; + + /* prev. SLA+R, data sent, ACK returned -> send data */ + case 0xB8: + switch (cmd) { + case CMD_GET_INFO: + TWDR = info[bcnt++]; + bcnt %= sizeof(info); + break; + + case CMD_GET_SIGNATURE: + TWDR = signature[bcnt++]; + bcnt %= sizeof(signature); + break; + + case CMD_READ_FLASH: + TWDR = pgm_read_byte_near(addr++); + break; + + case CMD_READ_EEPROM: + EEARL = addr; + EEARH = (addr >> 8); + EECR |= (1< reset hardware */ + case 0xF8: + TWCR |= (1< 1) + boot_timeout--; + + /* trigger app-boot */ + else if (boot_timeout == 1) + cmd = CMD_BOOT_APPLICATION; +} + +static void (*jump_to_app)(void) = 0x0000; + +int main(void) +{ + DDRB = LED_GN | LED_RT; + PORTB = LED_GN; + + /* move tnterrupt-vectors to bootloader */ + GICR = (1<