From 0416a2f53688fc41d3a3321445d526a960804b3b Mon Sep 17 00:00:00 2001 From: Olaf Rempel Date: Sat, 1 Feb 2020 22:45:26 +0100 Subject: [PATCH] Cleanup and reduce size --- README.md | 13 +++++++------ main.c | 46 +++++++++++++++++++++------------------------- 2 files changed, 28 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 207fd99..ec12bbb 100644 --- a/README.md +++ b/README.md @@ -3,17 +3,17 @@ twiboot is a simple/small bootloader for AVR MCUs with integrated TWI peripheral It was originally created to update I2C controlled BLMCs (Brushless Motor Controller) without an AVR ISP adapter. twiboot acts as a slave device on a TWI/I2C bus and allows reading/writing of the internal flash memory. -As a compile time option twiboot also allows reading/writing of the whole internal EEPROM memory. +As a compile time option (EEPROM_SUPPORT) twiboot also allows reading/writing of the whole internal EEPROM memory. The bootloader is not able to update itself (only application flash memory region accessible). Currently the following AVR MCUs are supported: AVR MCU | Flash bytes used (.text + .data) | Bootloader region size --- | --- | --- -atmega8 | 724 (0x2D4) | 512 words -atmega88 | 744 (0x2E8) | 512 words -atmega168 | 744 (0x2E8) | 512 words -atmega328p | 744 (0x2E8) | 512 words +atmega8 | 706 (0x2C2) | 512 words +atmega88 | 726 (0x2D6) | 512 words +atmega168 | 726 (0x2D6) | 512 words +atmega328p | 726 (0x2D6) | 512 words (Compiled on Ubuntu 18.04 LTS (gcc 5.4.0 / avr-libc 2.0.0) with EEPROM and LED support) @@ -33,7 +33,7 @@ A TWI/I2C master can use the protocol to - write the internal eeprom (byte wise) - exit the bootloader and start the application -As a compile time option twiboot can output its state with two LEDs. +As a compile time option (LED_SUPPORT) twiboot can output its state with two LEDs. One LED will flash with a frequency of 20Hz while twiboot is active (including boot wait time). A second LED will flash when the bootloader is addressed on the TWI/I2C bus. @@ -85,6 +85,7 @@ Write 1+ eeprom bytes | **SLA+W**, 0x02, 0x02, addrh, addrl, {* bytes}, **STO** The multiboot_tool repository contains a simple linux application that uses this protocol to access the bootloader over linux i2c device. +The ispprog programming adapter can also be used as a avr910/butterfly to twiboot protocol bridge. ## Development ## Issue reports, feature requests, patches or simply success stories are much appreciated. diff --git a/main.c b/main.c index 48e42c4..0859d5f 100644 --- a/main.c +++ b/main.c @@ -141,26 +141,24 @@ static void write_flash_page(void) uint8_t size = SPM_PAGESIZE; uint8_t *p = buf; - if (pagestart >= BOOTLOADER_START) + if (pagestart < BOOTLOADER_START) { - 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(); } - - 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 */ @@ -168,12 +166,11 @@ static void write_flash_page(void) /* ************************************************************************* * read_eeprom_byte * ************************************************************************* */ -static uint8_t read_eeprom_byte(void) +static uint8_t read_eeprom_byte(uint16_t address) { - EEARL = addr; - EEARH = (addr >> 8); + EEARL = address; + EEARH = (address >> 8); EECR |= (1<