Update code style

Update indention to spaces
Fix comments
Add README.md and GPL-2 LICENSE files
This commit is contained in:
Olaf Rempel 2019-11-06 10:03:47 +01:00
parent dd4ca2cb33
commit f4ef7a7b1e
4 changed files with 714 additions and 302 deletions

262
LICENSE Normal file
View File

@ -0,0 +1,262 @@
GNU GENERAL PUBLIC LICENSE
Version 2, June 1991
Copyright (C) 1989, 1991 Free Software Foundation, Inc.
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
Everyone is permitted to copy and distribute verbatim copies of this license
document, but changing it is not allowed.
Preamble
The licenses for most software are designed to take away your freedom to share
and change it. By contrast, the GNU General Public License is intended to
guarantee your freedom to share and change free software--to make sure the
software is free for all its users. This General Public License applies to
most of the Free Software Foundation's software and to any other program whose
authors commit to using it. (Some other Free Software Foundation software
is covered by the GNU Lesser General Public License instead.) You can apply
it to your programs, too.
When we speak of free software, we are referring to freedom, not price. Our
General Public Licenses are designed to make sure that you have the freedom
to distribute copies of free software (and charge for this service if you
wish), that you receive source code or can get it if you want it, that you
can change the software or use pieces of it in new free programs; and that
you know you can do these things.
To protect your rights, we need to make restrictions that forbid anyone to
deny you these rights or to ask you to surrender the rights. These restrictions
translate to certain responsibilities for you if you distribute copies of
the software, or if you modify it.
For example, if you distribute copies of such a program, whether gratis or
for a fee, you must give the recipients all the rights that you have. You
must make sure that they, too, receive or can get the source code. And you
must show them these terms so they know their rights.
We protect your rights with two steps: (1) copyright the software, and (2)
offer you this license which gives you legal permission to copy, distribute
and/or modify the software.
Also, for each author's protection and ours, we want to make certain that
everyone understands that there is no warranty for this free software. If
the software is modified by someone else and passed on, we want its recipients
to know that what they have is not the original, so that any problems introduced
by others will not reflect on the original authors' reputations.
Finally, any free program is threatened constantly by software patents. We
wish to avoid the danger that redistributors of a free program will individually
obtain patent licenses, in effect making the program proprietary. To prevent
this, we have made it clear that any patent must be licensed for everyone's
free use or not licensed at all.
The precise terms and conditions for copying, distribution and modification
follow.
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. This License applies to any program or other work which contains a notice
placed by the copyright holder saying it may be distributed under the terms
of this General Public License. The "Program", below, refers to any such program
or work, and a "work based on the Program" means either the Program or any
derivative work under copyright law: that is to say, a work containing the
Program or a portion of it, either verbatim or with modifications and/or translated
into another language. (Hereinafter, translation is included without limitation
in the term "modification".) Each licensee is addressed as "you".
Activities other than copying, distribution and modification are not covered
by this License; they are outside its scope. The act of running the Program
is not restricted, and the output from the Program is covered only if its
contents constitute a work based on the Program (independent of having been
made by running the Program). Whether that is true depends on what the Program
does.
1. You may copy and distribute verbatim copies of the Program's source code
as you receive it, in any medium, provided that you conspicuously and appropriately
publish on each copy an appropriate copyright notice and disclaimer of warranty;
keep intact all the notices that refer to this License and to the absence
of any warranty; and give any other recipients of the Program a copy of this
License along with the Program.
You may charge a fee for the physical act of transferring a copy, and you
may at your option offer warranty protection in exchange for a fee.
2. You may modify your copy or copies of the Program or any portion of it,
thus forming a work based on the Program, and copy and distribute such modifications
or work under the terms of Section 1 above, provided that you also meet all
of these conditions:
a) You must cause the modified files to carry prominent notices stating that
you changed the files and the date of any change.
b) You must cause any work that you distribute or publish, that in whole or
in part contains or is derived from the Program or any part thereof, to be
licensed as a whole at no charge to all third parties under the terms of this
License.
c) If the modified program normally reads commands interactively when run,
you must cause it, when started running for such interactive use in the most
ordinary way, to print or display an announcement including an appropriate
copyright notice and a notice that there is no warranty (or else, saying that
you provide a warranty) and that users may redistribute the program under
these conditions, and telling the user how to view a copy of this License.
(Exception: if the Program itself is interactive but does not normally print
such an announcement, your work based on the Program is not required to print
an announcement.)
These requirements apply to the modified work as a whole. If identifiable
sections of that work are not derived from the Program, and can be reasonably
considered independent and separate works in themselves, then this License,
and its terms, do not apply to those sections when you distribute them as
separate works. But when you distribute the same sections as part of a whole
which is a work based on the Program, the distribution of the whole must be
on the terms of this License, whose permissions for other licensees extend
to the entire whole, and thus to each and every part regardless of who wrote
it.
Thus, it is not the intent of this section to claim rights or contest your
rights to work written entirely by you; rather, the intent is to exercise
the right to control the distribution of derivative or collective works based
on the Program.
In addition, mere aggregation of another work not based on the Program with
the Program (or with a work based on the Program) on a volume of a storage
or distribution medium does not bring the other work under the scope of this
License.
3. You may copy and distribute the Program (or a work based on it, under Section
2) in object code or executable form under the terms of Sections 1 and 2 above
provided that you also do one of the following:
a) Accompany it with the complete corresponding machine-readable source code,
which must be distributed under the terms of Sections 1 and 2 above on a medium
customarily used for software interchange; or,
b) Accompany it with a written offer, valid for at least three years, to give
any third party, for a charge no more than your cost of physically performing
source distribution, a complete machine-readable copy of the corresponding
source code, to be distributed under the terms of Sections 1 and 2 above on
a medium customarily used for software interchange; or,
c) Accompany it with the information you received as to the offer to distribute
corresponding source code. (This alternative is allowed only for noncommercial
distribution and only if you received the program in object code or executable
form with such an offer, in accord with Subsection b above.)
The source code for a work means the preferred form of the work for making
modifications to it. For an executable work, complete source code means all
the source code for all modules it contains, plus any associated interface
definition files, plus the scripts used to control compilation and installation
of the executable. However, as a special exception, the source code distributed
need not include anything that is normally distributed (in either source or
binary form) with the major components (compiler, kernel, and so on) of the
operating system on which the executable runs, unless that component itself
accompanies the executable.
If distribution of executable or object code is made by offering access to
copy from a designated place, then offering equivalent access to copy the
source code from the same place counts as distribution of the source code,
even though third parties are not compelled to copy the source along with
the object code.
4. You may not copy, modify, sublicense, or distribute the Program except
as expressly provided under this License. Any attempt otherwise to copy, modify,
sublicense or distribute the Program is void, and will automatically terminate
your rights under this License. However, parties who have received copies,
or rights, from you under this License will not have their licenses terminated
so long as such parties remain in full compliance.
5. You are not required to accept this License, since you have not signed
it. However, nothing else grants you permission to modify or distribute the
Program or its derivative works. These actions are prohibited by law if you
do not accept this License. Therefore, by modifying or distributing the Program
(or any work based on the Program), you indicate your acceptance of this License
to do so, and all its terms and conditions for copying, distributing or modifying
the Program or works based on it.
6. Each time you redistribute the Program (or any work based on the Program),
the recipient automatically receives a license from the original licensor
to copy, distribute or modify the Program subject to these terms and conditions.
You may not impose any further restrictions on the recipients' exercise of
the rights granted herein. You are not responsible for enforcing compliance
by third parties to this License.
7. If, as a consequence of a court judgment or allegation of patent infringement
or for any other reason (not limited to patent issues), conditions are imposed
on you (whether by court order, agreement or otherwise) that contradict the
conditions of this License, they do not excuse you from the conditions of
this License. If you cannot distribute so as to satisfy simultaneously your
obligations under this License and any other pertinent obligations, then as
a consequence you may not distribute the Program at all. For example, if a
patent license would not permit royalty-free redistribution of the Program
by all those who receive copies directly or indirectly through you, then the
only way you could satisfy both it and this License would be to refrain entirely
from distribution of the Program.
If any portion of this section is held invalid or unenforceable under any
particular circumstance, the balance of the section is intended to apply and
the section as a whole is intended to apply in other circumstances.
It is not the purpose of this section to induce you to infringe any patents
or other property right claims or to contest validity of any such claims;
this section has the sole purpose of protecting the integrity of the free
software distribution system, which is implemented by public license practices.
Many people have made generous contributions to the wide range of software
distributed through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing to
distribute software through any other system and a licensee cannot impose
that choice.
This section is intended to make thoroughly clear what is believed to be a
consequence of the rest of this License.
8. If the distribution and/or use of the Program is restricted in certain
countries either by patents or by copyrighted interfaces, the original copyright
holder who places the Program under this License may add an explicit geographical
distribution limitation excluding those countries, so that distribution is
permitted only in or among countries not thus excluded. In such case, this
License incorporates the limitation as if written in the body of this License.
9. The Free Software Foundation may publish revised and/or new versions of
the General Public License from time to time. Such new versions will be similar
in spirit to the present version, but may differ in detail to address new
problems or concerns.
Each version is given a distinguishing version number. If the Program specifies
a version number of this License which applies to it and "any later version",
you have the option of following the terms and conditions either of that version
or of any later version published by the Free Software Foundation. If the
Program does not specify a version number of this License, you may choose
any version ever published by the Free Software Foundation.
10. If you wish to incorporate parts of the Program into other free programs
whose distribution conditions are different, write to the author to ask for
permission. For software which is copyrighted by the Free Software Foundation,
write to the Free Software Foundation; we sometimes make exceptions for this.
Our decision will be guided by the two goals of preserving the free status
of all derivatives of our free software and of promoting the sharing and reuse
of software generally.
NO WARRANTY
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR
THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE
STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM
"AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE
OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME
THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE
OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA
OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES
OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH
HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
END OF TERMS AND CONDITIONS

View File

@ -16,7 +16,9 @@ AVRDUDE_PROG := -c avr910 -b 115200 -P /dev/ttyUSB0
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
ifeq ($(MCU), atmega8) ifeq ($(MCU), atmega8)
# (8Mhz internal RC-Osz., 2.7V BOD) # atmega8:
# Fuse L: 0x84 (8Mhz internal RC-Osz., 2.7V BOD)
# Fuse H: 0xda (512 words bootloader)
AVRDUDE_MCU=m8 AVRDUDE_MCU=m8
AVRDUDE_FUSES=lfuse:w:0x84:m hfuse:w:0xda:m AVRDUDE_FUSES=lfuse:w:0x84:m hfuse:w:0xda:m
@ -24,7 +26,10 @@ BOOTLOADER_START=0x1C00
endif endif
ifeq ($(MCU), atmega88) ifeq ($(MCU), atmega88)
# (8Mhz internal RC-Osz., 2.7V BOD) # atmega88:
# Fuse L: 0xc2 (8Mhz internal RC-Osz.)
# Fuse H: 0xdd (2.7V BOD)
# Fuse E: 0xfa (512 words bootloader)
AVRDUDE_MCU=m88 AVRDUDE_MCU=m88
AVRDUDE_FUSES=lfuse:w:0xc2:m hfuse:w:0xdd:m efuse:w:0xfa:m AVRDUDE_FUSES=lfuse:w:0xc2:m hfuse:w:0xdd:m efuse:w:0xfa:m
@ -32,7 +37,10 @@ BOOTLOADER_START=0x1C00
endif endif
ifeq ($(MCU), atmega168) ifeq ($(MCU), atmega168)
# (8Mhz internal RC-Osz., 2.7V BOD) # atmega168:
# Fuse L: 0xc2 (8Mhz internal RC-Osz.)
# Fuse H: 0xdd (2.7V BOD)
# Fuse E: 0xfa (512 words bootloader)
AVRDUDE_MCU=m168 -F AVRDUDE_MCU=m168 -F
AVRDUDE_FUSES=lfuse:w:0xc2:m hfuse:w:0xdd:m efuse:w:0xfa:m AVRDUDE_FUSES=lfuse:w:0xc2:m hfuse:w:0xdd:m efuse:w:0xfa:m
@ -40,7 +48,10 @@ BOOTLOADER_START=0x3C00
endif endif
ifeq ($(MCU), atmega328p) ifeq ($(MCU), atmega328p)
# (8Mhz internal RC-Osz., 2.7V BOD) # atmega328p:
# Fuse L: 0xc2 (8Mhz internal RC-Osz.)
# Fuse H: 0xdc (512 words bootloader)
# Fuse E: 0xfd (2.7V BOD)
AVRDUDE_MCU=m328p -F AVRDUDE_MCU=m328p -F
AVRDUDE_FUSES=lfuse:w:0xc2:m hfuse:w:0xdc:m efuse:w:0xfd:m AVRDUDE_FUSES=lfuse:w:0xc2:m hfuse:w:0xdc:m efuse:w:0xfd:m

96
README.md Normal file
View File

@ -0,0 +1,96 @@
# twiboot - a TWI / I2C bootloader for AVR MCUs ##
twiboot is a simple/small bootloader for AVR MCUs with integrated TWI peripheral written in C.
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.
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 | 900 (0x384) | 512 words
atmega88 | 950 (0x3B6) | 512 words
atmega168 | 1002 (0x3EA) | 512 words
atmega328p | 1002 (0x3EA) | 512 words
(Compiled on Ubuntu 18.04 LTS (gcc 5.4.0 / avr-libc 2.0.0) with EEPROM and LED support)
## Operation ##
twiboot is installed in the bootloader memory region and executed directly after reset (BOOTRST fuse is programmed).
While running, twiboot configures the TWI peripheral as slave device and waits for valid protocol messages
directed to its address on the TWI/I2C bus. The slave address is configured during compile time of twiboot.
When receiving no messages for 1000ms after reset, the bootloader exits and executes the main application at address 0x0000.
A TWI/I2C master can use the protocol to
- abort the boot timeout
- query information about the device (bootloader version, AVR signature bytes, flash/eeprom size, flash page size)
- read internal flash / eeprom memory (byte wise)
- write the internal flash (page wise)
- 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.
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.
## Build and install twiboot ##
twiboot uses gcc, avr-libc and GNU Make for building, avrdude is used for flashing the MCU.
The build and install procedures are only tested under linux.
The selection of the target MCU and the programming interface can be found in the Makefile,
TWI/I2C slave address and optional components (EEPROM / LED support) are configured
in the main.c source.
To build twiboot for the selected target:
``` shell
$ make
```
To install (flash download) twiboot with avrdude on the target:
``` shell
$ make install
```
Set AVR fuses with avrdude on the target (internal RC-Osz, enable BOD, enable BOOTRST):
``` shell
$ make fuses
```
## TWI/I2C Protocol ##
A TWI/I2C master can use the following protocol for accessing the bootloader.
Function | TWI/I2C data | Comment
--- | --- | ---
Abort boot timeout | **SLA+W**, 0x00, **STO** |
Show bootloader version | **SLA+W**, 0x01, **SLA+R**, {16 bytes}, **STO** | ASCII, not null terminated
Start application | **SLA+W**, 0x01, 0x80, **STO** |
Read chip info | **SLA+W**, 0x02, 0x00, 0x00, 0x00, **SLA+R**, {8 bytes}, **STO** | 3byte signature, 1byte page size, 2byte flash size, 2byte eeprom size
Read 1+ flash bytes | **SLA+W**, 0x02, 0x01, addrh, addrl, **SLA+R**, {* bytes}, **STO** |
Read 1+ eeprom bytes | **SLA+W**, 0x02, 0x02, addrh, addrl, **SLA+R**, {* bytes}, **STO** |
Write one flash page | **SLA+W**, 0x02, 0x01, addrh, addrl, {* bytes}, **STO** | page size as indicated in chip info
Write 1+ eeprom bytes | **SLA+W**, 0x02, 0x02, addrh, addrl, {* bytes}, **STO** |
**SLA+R** means Start Condition, Slave Address, Read Access
**SLA+W** means Start Condition, Slave Address, Write Access
**STO** means Stop Condition
The multiboot_tool repository contains a simple linux application that uses
this protocol to access the bootloader over linux i2c device.
## Development ##
Issue reports, feature requests, patches or simply success stories are much appreciated.
## Roadmap ##
Some ideas that I want to investigate / implement in twiboot:
- [ ] do not use interrupts and remove vector table to save flash space
- [ ] find a way to not rely on TWI clock stretching during write access
- [ ] support AVR TINYs (USI peripheral, no bootloader fuse, no Read-While-Write flash)

165
main.c
View File

@ -1,5 +1,5 @@
/*************************************************************************** /***************************************************************************
* Copyright (C) 08/2010 by Olaf Rempel * * Copyright (C) 11/2019 by Olaf Rempel *
* razzor@kopf-tisch.de * * razzor@kopf-tisch.de *
* * * *
* This program is free software; you can redistribute it and/or modify * * This program is free software; you can redistribute it and/or modify *
@ -21,27 +21,6 @@
#include <avr/boot.h> #include <avr/boot.h>
#include <avr/pgmspace.h> #include <avr/pgmspace.h>
/*
* atmega8:
* Fuse H: 0xda (512 words bootloader)
* Fuse L: 0x84 (8Mhz internal RC-Osz., 2.7V BOD)
*
* atmega88:
* Fuse E: 0xfa (512 words bootloader)
* Fuse H: 0xdd (2.7V BOD)
* Fuse L: 0xc2 (8Mhz internal RC-Osz.)
*
* atmega168:
* Fuse E: 0xfa (512 words bootloader)
* Fuse H: 0xdd (2.7V BOD)
* Fuse L: 0xc2 (8Mhz internal RC-Osz.)
*
* atmega328p:
* Fuse E: 0xfd (2.7V BOD)
* Fuse H: 0xdc (512 words bootloader)
* Fuse L: 0xc2 (8Mhz internal RC-Osz.)
*/
#if defined (__AVR_ATmega8__) #if defined (__AVR_ATmega8__)
#define VERSION_STRING "TWIBOOT m8v2.1" #define VERSION_STRING "TWIBOOT m8v2.1"
#define SIGNATURE_BYTES 0x1E, 0x93, 0x07 #define SIGNATURE_BYTES 0x1E, 0x93, 0x07
@ -125,8 +104,8 @@
#define MEMTYPE_PARAMETERS 0x03 /* only in APP */ #define MEMTYPE_PARAMETERS 0x03 /* only in APP */
/* /*
* LED_GN blinks with 20Hz (while bootloader is running) * LED_GN flashes with 20Hz (while bootloader is running)
* LED_RT blinks on TWI activity * LED_RT flashes on TWI activity
* *
* bootloader twi-protocol: * bootloader twi-protocol:
* - abort boot timeout: * - abort boot timeout:
@ -139,7 +118,7 @@
* SLA+W, 0x01, 0x80, STO * SLA+W, 0x01, 0x80, STO
* *
* - read chip info: 3byte signature, 1byte page size, 2byte flash size, 2byte eeprom size * - read chip info: 3byte signature, 1byte page size, 2byte flash size, 2byte eeprom size
* SLA+W, 0x02, 0x00, 0x00, 0x00, SLA+R, {4 bytes}, STO * SLA+W, 0x02, 0x00, 0x00, 0x00, SLA+R, {8 bytes}, STO
* *
* - read one (or more) flash bytes * - read one (or more) flash bytes
* SLA+W, 0x02, 0x01, addrh, addrl, SLA+R, {* bytes}, STO * SLA+W, 0x02, 0x01, addrh, addrl, SLA+R, {* bytes}, STO
@ -147,8 +126,8 @@
* - read one (or more) eeprom bytes * - read one (or more) eeprom bytes
* SLA+W, 0x02, 0x02, addrh, addrl, SLA+R, {* bytes}, STO * SLA+W, 0x02, 0x02, addrh, addrl, SLA+R, {* bytes}, STO
* *
* - write one flash page (64bytes on mega8) * - write one flash page
* SLA+W, 0x02, 0x01, addrh, addrl, {64 bytes}, STO * SLA+W, 0x02, 0x01, addrh, addrl, {* bytes}, STO
* *
* - write one (or more) eeprom bytes * - write one (or more) eeprom bytes
* SLA+W, 0x02, 0x02, addrh, addrl, {* bytes}, STO * SLA+W, 0x02, 0x02, addrh, addrl, {* bytes}, STO
@ -157,11 +136,11 @@
const static uint8_t info[16] = VERSION_STRING; const static uint8_t info[16] = VERSION_STRING;
const static uint8_t chipinfo[8] = { const static uint8_t chipinfo[8] = {
SIGNATURE_BYTES, SIGNATURE_BYTES,
SPM_PAGESIZE, SPM_PAGESIZE,
(BOOTLOADER_START >> 8) & 0xFF, (BOOTLOADER_START >> 8) & 0xFF,
BOOTLOADER_START & 0xFF, BOOTLOADER_START & 0xFF,
#if (EEPROM_SUPPORT) #if (EEPROM_SUPPORT)
((E2END +1) >> 8 & 0xFF), ((E2END +1) >> 8 & 0xFF),
(E2END +1) & 0xFF (E2END +1) & 0xFF
@ -178,6 +157,9 @@ volatile static uint8_t cmd = CMD_WAIT;
static uint8_t buf[SPM_PAGESIZE]; static uint8_t buf[SPM_PAGESIZE];
static uint16_t addr; static uint16_t addr;
/* *************************************************************************
* write_flash_page
* ************************************************************************* */
static void write_flash_page(void) static void write_flash_page(void)
{ {
uint16_t pagestart = addr; uint16_t pagestart = addr;
@ -185,7 +167,9 @@ static void write_flash_page(void)
uint8_t *p = buf; uint8_t *p = buf;
if (pagestart >= BOOTLOADER_START) if (pagestart >= BOOTLOADER_START)
{
return; return;
}
boot_page_erase(pagestart); boot_page_erase(pagestart);
boot_spm_busy_wait(); boot_spm_busy_wait();
@ -202,42 +186,59 @@ static void write_flash_page(void)
boot_page_write(pagestart); boot_page_write(pagestart);
boot_spm_busy_wait(); boot_spm_busy_wait();
boot_rww_enable(); boot_rww_enable();
} } /* write_flash_page */
#if (EEPROM_SUPPORT) #if (EEPROM_SUPPORT)
/* *************************************************************************
* read_eeprom_byte
* ************************************************************************* */
static uint8_t read_eeprom_byte(void) static uint8_t read_eeprom_byte(void)
{ {
EEARL = addr; EEARL = addr;
EEARH = (addr >> 8); EEARH = (addr >> 8);
EECR |= (1<<EERE); EECR |= (1<<EERE);
addr++; addr++;
return EEDR;
}
return EEDR;
} /* read_eeprom_byte */
/* *************************************************************************
* write_eeprom_byte
* ************************************************************************* */
static void write_eeprom_byte(uint8_t val) static void write_eeprom_byte(uint8_t val)
{ {
EEARL = addr; EEARL = addr;
EEARH = (addr >> 8); EEARH = (addr >> 8);
EEDR = val; EEDR = val;
addr++; addr++;
#if defined (__AVR_ATmega8__) #if defined (__AVR_ATmega8__)
EECR |= (1<<EEMWE); EECR |= (1<<EEMWE);
EECR |= (1<<EEWE); EECR |= (1<<EEWE);
#elif defined (__AVR_ATmega88__) || defined (__AVR_ATmega168__) || defined (__AVR_ATmega328P__) #elif defined (__AVR_ATmega88__) || defined (__AVR_ATmega168__) || \
defined (__AVR_ATmega328P__)
EECR |= (1<<EEMPE); EECR |= (1<<EEMPE);
EECR |= (1<<EEPE); EECR |= (1<<EEPE);
#endif #endif
eeprom_busy_wait(); eeprom_busy_wait();
} } /* write_eeprom_byte */
#endif /* EEPROM_SUPPORT */ #endif /* EEPROM_SUPPORT */
/* *************************************************************************
* TWI_vect
* ************************************************************************* */
ISR(TWI_vect) ISR(TWI_vect)
{ {
static uint8_t bcnt; static uint8_t bcnt;
uint8_t data; uint8_t data;
uint8_t ack = (1<<TWEA); uint8_t ack = (1<<TWEA);
switch (TWSR & 0xF8) { switch (TWSR & 0xF8)
{
/* SLA+W received, ACK returned -> receive data and ACK */ /* SLA+W received, ACK returned -> receive data and ACK */
case 0x60: case 0x60:
bcnt = 0; bcnt = 0;
@ -248,9 +249,12 @@ ISR(TWI_vect)
/* prev. SLA+W, data received, ACK returned -> receive data and ACK */ /* prev. SLA+W, data received, ACK returned -> receive data and ACK */
case 0x80: case 0x80:
data = TWDR; data = TWDR;
switch (bcnt) {
switch (bcnt)
{
case 0: case 0:
switch (data) { switch (data)
{
case CMD_SWITCH_APPLICATION: case CMD_SWITCH_APPLICATION:
case CMD_WRITE_MEMORY: case CMD_WRITE_MEMORY:
bcnt++; bcnt++;
@ -267,30 +271,40 @@ ISR(TWI_vect)
ack = (0<<TWEA); ack = (0<<TWEA);
break; break;
} }
cmd = data; cmd = data;
break; break;
case 1: case 1:
switch (cmd) { switch (cmd)
{
case CMD_SWITCH_APPLICATION: case CMD_SWITCH_APPLICATION:
if (data == BOOTTYPE_APPLICATION) { if (data == BOOTTYPE_APPLICATION)
{
cmd = CMD_BOOT_APPLICATION; cmd = CMD_BOOT_APPLICATION;
} }
ack = (0<<TWEA); ack = (0<<TWEA);
break; break;
case CMD_WRITE_MEMORY: case CMD_WRITE_MEMORY:
bcnt++; bcnt++;
if (data == MEMTYPE_CHIPINFO) { if (data == MEMTYPE_CHIPINFO)
{
cmd = CMD_WRITE_CHIPINFO; cmd = CMD_WRITE_CHIPINFO;
}
} else if (data == MEMTYPE_FLASH) { else if (data == MEMTYPE_FLASH)
{
cmd = CMD_WRITE_FLASH; cmd = CMD_WRITE_FLASH;
}
#if (EEPROM_SUPPORT) #if (EEPROM_SUPPORT)
} else if (data == MEMTYPE_EEPROM) { else if (data == MEMTYPE_EEPROM)
{
cmd = CMD_WRITE_EEPROM; cmd = CMD_WRITE_EEPROM;
#endif }
} else { #endif /* (EEPROM_SUPPORT) */
else
{
ack = (0<<TWEA); ack = (0<<TWEA);
} }
break; break;
@ -309,22 +323,28 @@ ISR(TWI_vect)
break; break;
default: default:
switch (cmd) { switch (cmd)
{
case CMD_WRITE_FLASH: case CMD_WRITE_FLASH:
buf[bcnt -4] = data; buf[bcnt -4] = data;
if (bcnt < sizeof(buf) +3) { if (bcnt < sizeof(buf) +3)
{
bcnt++; bcnt++;
} else { }
else
{
write_flash_page(); write_flash_page();
ack = (0<<TWEA); ack = (0<<TWEA);
} }
break; break;
#if (EEPROM_SUPPORT) #if (EEPROM_SUPPORT)
case CMD_WRITE_EEPROM: case CMD_WRITE_EEPROM:
write_eeprom_byte(data); write_eeprom_byte(data);
bcnt++; bcnt++;
break; break;
#endif #endif /* (EEPROM_SUPPORT) */
default: default:
ack = (0<<TWEA); ack = (0<<TWEA);
break; break;
@ -333,7 +353,9 @@ ISR(TWI_vect)
} }
if (ack == 0x00) if (ack == 0x00)
{
bcnt = 0; bcnt = 0;
}
TWCR |= (1<<TWINT) | ack; TWCR |= (1<<TWINT) | ack;
break; break;
@ -345,7 +367,8 @@ ISR(TWI_vect)
/* prev. SLA+R, data sent, ACK returned -> send data */ /* prev. SLA+R, data sent, ACK returned -> send data */
case 0xB8: case 0xB8:
switch (cmd) { switch (cmd)
{
case CMD_READ_VERSION: case CMD_READ_VERSION:
data = info[bcnt++]; data = info[bcnt++];
bcnt %= sizeof(info); bcnt %= sizeof(info);
@ -359,11 +382,13 @@ ISR(TWI_vect)
case CMD_READ_FLASH: case CMD_READ_FLASH:
data = pgm_read_byte_near(addr++); data = pgm_read_byte_near(addr++);
break; break;
#if (EEPROM_SUPPORT) #if (EEPROM_SUPPORT)
case CMD_READ_EEPROM: case CMD_READ_EEPROM:
data = read_eeprom_byte(); data = read_eeprom_byte();
break; break;
#endif #endif /* (EEPROM_SUPPORT) */
default: default:
data = 0xFF; data = 0xFF;
break; break;
@ -373,9 +398,9 @@ ISR(TWI_vect)
TWCR |= (1<<TWINT) | (1<<TWEA); TWCR |= (1<<TWINT) | (1<<TWEA);
break; break;
/* STOP or repeated START */ /* STOP or repeated START -> IDLE */
case 0xA0: case 0xA0:
/* data sent, NACK returned */ /* prev. SLA+R, data sent, NACK returned -> IDLE */
case 0xC0: case 0xC0:
LED_RT_OFF(); LED_RT_OFF();
TWCR |= (1<<TWINT) | (1<<TWEA); TWCR |= (1<<TWINT) | (1<<TWEA);
@ -386,8 +411,12 @@ ISR(TWI_vect)
TWCR |= (1<<TWINT) | (1<<TWSTO) | (1<<TWEA); TWCR |= (1<<TWINT) | (1<<TWSTO) | (1<<TWEA);
break; break;
} }
} } /* TWI_vect */
/* *************************************************************************
* TIMER0_OVF_vect
* ************************************************************************* */
ISR(TIMER0_OVF_vect) ISR(TIMER0_OVF_vect)
{ {
/* restart timer */ /* restart timer */
@ -398,30 +427,42 @@ ISR(TIMER0_OVF_vect)
/* count down for app-boot */ /* count down for app-boot */
if (boot_timeout > 1) if (boot_timeout > 1)
{
boot_timeout--; boot_timeout--;
}
/* trigger app-boot */
else if (boot_timeout == 1) else if (boot_timeout == 1)
{
/* trigger app-boot */
cmd = CMD_BOOT_APPLICATION; cmd = CMD_BOOT_APPLICATION;
} }
} /* TIMER0_OVF_vect */
static void (*jump_to_app)(void) __attribute__ ((noreturn)) = 0x0000; static void (*jump_to_app)(void) __attribute__ ((noreturn)) = 0x0000;
/* /*
* For newer devices (mega88) the watchdog timer remains active even after a * For newer devices the watchdog timer remains active even after a
* system reset. So disable it as soon as possible. * system reset. So disable it as soon as possible.
* automagically called on startup * automagically called on startup
*/ */
#if defined (__AVR_ATmega88__) || defined (__AVR_ATmega168__) || defined (__AVR_ATmega328P__) #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) __attribute__((naked, section(".init3")));
void disable_wdt_timer(void) void disable_wdt_timer(void)
{ {
MCUSR = 0; MCUSR = 0;
WDTCSR = (1<<WDCE) | (1<<WDE); WDTCSR = (1<<WDCE) | (1<<WDE);
WDTCSR = (0<<WDE); WDTCSR = (0<<WDE);
} } /* disable_wdt_timer */
#endif #endif
/* *************************************************************************
* main
* ************************************************************************* */
int main(void) __attribute__ ((noreturn)); int main(void) __attribute__ ((noreturn));
int main(void) int main(void)
{ {
@ -436,7 +477,8 @@ int main(void)
TCCR0 = (1<<CS02) | (1<<CS00); TCCR0 = (1<<CS02) | (1<<CS00);
TIMSK = (1<<TOIE0); TIMSK = (1<<TOIE0);
#elif defined (__AVR_ATmega88__) || defined (__AVR_ATmega168__) || defined (__AVR_ATmega328P__) #elif defined (__AVR_ATmega88__) || defined (__AVR_ATmega168__) || \
defined (__AVR_ATmega328P__)
MCUCR = (1<<IVCE); MCUCR = (1<<IVCE);
MCUCR = (1<<IVSEL); MCUCR = (1<<IVSEL);
@ -463,7 +505,8 @@ int main(void)
GICR = (1<<IVCE); GICR = (1<<IVCE);
GICR = (0<<IVSEL); GICR = (0<<IVSEL);
#elif defined (__AVR_ATmega88__) || defined (__AVR_ATmega168__) || defined (__AVR_ATmega328P__) #elif defined (__AVR_ATmega88__) || defined (__AVR_ATmega168__) || \
defined (__AVR_ATmega328P__)
TIMSK0 = 0x00; TIMSK0 = 0x00;
TCCR0B = 0x00; TCCR0B = 0x00;
@ -479,4 +522,4 @@ int main(void)
} while (--wait); } while (--wait);
jump_to_app(); jump_to_app();
} } /* main */