Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile.common
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ endif
-Wl,--relax,--gc-sections \
-Wl,--undefined=_mmcu,--section-start=.mmcu=0x910000 \
-I$(SIMAVR)/sim/avr -I../simavr/sim/avr -I../../simavr/sim/avr \
${filter-out %.h, ${^}} -o ${@}
${AVR_LDFLAGS} ${filter-out %.h, ${^}} -o ${@}
@${AVR}size ${@}|sed '1d'

# this rule has precedence
Expand Down
7 changes: 5 additions & 2 deletions examples/board_simduino/simduino.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ int main(int argc, char *argv[])
fprintf(stderr, "%s: Unable to load %s\n", argv[0], boot_path);
exit(1);
}
if (boot_base > 32*1024*1024) {
if (boot_base >= 32*1024) { // past an ATmega328's flash
mmcu = "atmega2560";
freq = 20000000;
}
Expand All @@ -134,12 +134,15 @@ int main(int argc, char *argv[])
avr->custom.init = avr_special_init;
avr->custom.deinit = avr_special_deinit;
avr->custom.data = &flash_data;
uint8_t * fuse = &avr->fuse[avr->boot_fuse];
*fuse &= ~((1 << AVR_FUSE_BOOTRST) | AVR_FUSE_BOOTSZ_MASK);
if (strcmp(mmcu, "atmega2560"))
*fuse |= 1 << AVR_FUSE_BOOTSZ0;
avr_init(avr);
avr->frequency = freq;

memcpy(avr->flash + boot_base, boot, boot_size);
free(boot);
avr->pc = boot_base;
/* end of flash, remember we are writing /code/ */
avr->codeend = avr->flashend;
avr->log = 1 + verbose;
Expand Down
13 changes: 11 additions & 2 deletions simavr/cores/sim_core_declare.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#define _SFR_IO8(v) ((v)+32)
#define _SFR_IO16(v) ((v)+32)
#define _SFR_MEM8(v) (v)
#define _BV(v) (v)
#define _BV(v) (1 << (v))
#define _VECTOR(v) (v)

/*
Expand Down Expand Up @@ -62,7 +62,7 @@
# define _FUSE_HELPER { FUSE1_DEFAULT, FUSE1_DEFAULT, FUSE2_DEFAULT, \
FUSE3_DEFAULT, FUSE4_DEFAULT, FUSE5_DEFAULT }
#elif FUSE_MEMORY_SIZE == 3
# define _FUSE_HELPER { LFUSE_DEFAULT, HFUSE_DEFAULT, EFUSE_DEFAULT }
# define _FUSE_HELPER { (uint8_t)LFUSE_DEFAULT, (uint8_t)HFUSE_DEFAULT, (uint8_t)EFUSE_DEFAULT }
#elif FUSE_MEMORY_SIZE == 2
# define _FUSE_HELPER { LFUSE_DEFAULT, HFUSE_DEFAULT }
#elif FUSE_MEMORY_SIZE == 1
Expand All @@ -71,6 +71,14 @@
# define _FUSE_HELPER { 0 }
#endif

#if defined(IVSEL) && defined(GICR)
# define _IVSEL_HELPER .ivsel = AVR_IO_REGBIT(GICR, IVSEL),
#elif defined(IVSEL)
# define _IVSEL_HELPER .ivsel = AVR_IO_REGBIT(MCUCR, IVSEL),
#else
# define _IVSEL_HELPER
#endif

#ifdef MCUCSR
# define MCU_STATUS_REG MCUCSR
#else
Expand All @@ -85,6 +93,7 @@
.e2end = E2END, \
.vector_size = _vector_size, \
.fuse = _FUSE_HELPER, \
_IVSEL_HELPER \
.signature = { SIGNATURE_0,SIGNATURE_1,SIGNATURE_2 }, \
.lockbits = 0xFF, \
.reset_flags = {\
Expand Down
2 changes: 2 additions & 0 deletions simavr/cores/sim_mega128.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ const struct mcu_t {
.core = {
.mmcu = "atmega128",
DEFAULT_CORE(4),
.boot_min_words = 512,
.boot_fuse = AVR_FUSE_HIGH,

.init = m128_init,
.reset = m128_reset,
Expand Down
2 changes: 2 additions & 0 deletions simavr/cores/sim_mega1280.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ const struct mcu_t {
.core = {
.mmcu = "atmega1280",
DEFAULT_CORE(4),
.boot_min_words = 512,
.boot_fuse = AVR_FUSE_HIGH,

.init = m1280_init,
.reset = m1280_reset,
Expand Down
2 changes: 2 additions & 0 deletions simavr/cores/sim_mega1281.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ const struct mcu_t {
.core = {
.mmcu = "atmega1281",
DEFAULT_CORE(4),
.boot_min_words = 512,
.boot_fuse = AVR_FUSE_HIGH,

.init = m1281_init,
.reset = m1281_reset,
Expand Down
2 changes: 2 additions & 0 deletions simavr/cores/sim_mega128rfa1.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ const struct mcu_t {
.core = {
.mmcu = "atmega128rfa1",
DEFAULT_CORE(4),
.boot_min_words = 512,
.boot_fuse = AVR_FUSE_HIGH,

.init = m128rfa1_init,
.reset = m128rfa1_reset,
Expand Down
2 changes: 2 additions & 0 deletions simavr/cores/sim_mega128rfr2.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ const struct mcu_t {
.core = {
.mmcu = "atmega128rfr2",
DEFAULT_CORE(4),
.boot_min_words = 512,
.boot_fuse = AVR_FUSE_HIGH,

.init = m128rfr2_init,
.reset = m128rfr2_reset,
Expand Down
2 changes: 2 additions & 0 deletions simavr/cores/sim_mega169.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ const struct mcu_t {
.core = {
.mmcu = "atmega169p",
DEFAULT_CORE(4),
.boot_min_words = 128,
.boot_fuse = AVR_FUSE_HIGH,

.init = m169p_init,
.reset = m169p_reset,
Expand Down
2 changes: 2 additions & 0 deletions simavr/cores/sim_mega2560.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ const struct mcu_t {
.core = {
.mmcu = "atmega2560",
DEFAULT_CORE(4),
.boot_min_words = 512,
.boot_fuse = AVR_FUSE_HIGH,

.init = m2560_init,
.reset = m2560_reset,
Expand Down
2 changes: 2 additions & 0 deletions simavr/cores/sim_mega328pb.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ const struct mcu_t {
.core = {
.mmcu = "atmega328pb",
DEFAULT_CORE(4),
.boot_min_words = 256,
.boot_fuse = AVR_FUSE_HIGH,

.init = m328pb_init,
.reset = m328pb_reset,
Expand Down
6 changes: 6 additions & 0 deletions simavr/cores/sim_megax.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ const struct mcu_t SIM_CORENAME = {
.core = {
.mmcu = SIM_MMCU,
DEFAULT_CORE(SIM_VECTOR_SIZE),
#if FLASHEND == 0x7fff // ATmega32
.boot_min_words = 256,
#else // ATmega8/16
.boot_min_words = 128,
#endif
.boot_fuse = AVR_FUSE_HIGH,

.init = mx_init,
.reset = mx_reset,
Expand Down
8 changes: 8 additions & 0 deletions simavr/cores/sim_megax4.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ const struct mcu_t SIM_CORENAME = {
.core = {
.mmcu = SIM_MMCU,
DEFAULT_CORE(4),
#if FLASHEND == 0x3fff // ATmega164
.boot_min_words = 128,
#elif FLASHEND == 0x7fff // ATmega324
.boot_min_words = 256,
#else // ATmega644/1284
.boot_min_words = 512,
#endif
.boot_fuse = AVR_FUSE_HIGH,

.init = mx4_init,
.reset = mx4_reset,
Expand Down
7 changes: 7 additions & 0 deletions simavr/cores/sim_megax8.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ const struct mcu_t SIM_CORENAME = {
.core = {
.mmcu = SIM_MMCU,
DEFAULT_CORE(SIM_VECTOR_SIZE),
#if FLASHEND == 0x7fff // ATmega328
.boot_min_words = 256,
.boot_fuse = AVR_FUSE_HIGH,
#elif FLASHEND > 0xfff // ATmega88/168
.boot_min_words = 128,
.boot_fuse = AVR_FUSE_EXT,
#endif

.init = mx8_init,
.reset = mx8_reset,
Expand Down
6 changes: 6 additions & 0 deletions simavr/cores/sim_megaxm1.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ const struct mcu_t SIM_CORENAME = {
.core = {
.mmcu = SIM_MMCU,
DEFAULT_CORE(SIM_VECTOR_SIZE),
#if FLASHEND == 0x3fff // ATmega16M1
.boot_min_words = 256,
#else // ATmega64M1
.boot_min_words = 512,
#endif
.boot_fuse = AVR_FUSE_HIGH,

.init = mxm1_init,
.reset = mxm1_reset,
Expand Down
2 changes: 2 additions & 0 deletions simavr/cores/sim_megaxu2.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ const struct mcu_t SIM_CORENAME = {
.core = {
.mmcu = SIM_MMCU,
DEFAULT_CORE(4),
.boot_min_words = 256,
.boot_fuse = AVR_FUSE_HIGH,

.init = mxu2_init,
.reset = mxu2_reset,
Expand Down
2 changes: 2 additions & 0 deletions simavr/cores/sim_megaxu4.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ const struct mcu_t SIM_CORENAME = {
.core = {
.mmcu = SIM_MMCU,
DEFAULT_CORE(4),
.boot_min_words = 256,
.boot_fuse = AVR_FUSE_HIGH,

.init = mxu4_init,
.reset = mxu4_reset,
Expand Down
3 changes: 2 additions & 1 deletion simavr/sim/avr_watchdog.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ static void avr_watchdog_reset(avr_io_t * port)
}
/* TODO could now use the two pending/running IRQs to do the same
* as before */
avr_irq_register_notify(p->watchdog.irq, avr_watchdog_irq_notify, p);
if (p->watchdog.vector)
avr_irq_register_notify(p->watchdog.irq, avr_watchdog_irq_notify, p);
}

static avr_io_t _io = {
Expand Down
18 changes: 18 additions & 0 deletions simavr/sim/sim_avr.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,23 @@ avr_terminate(
avr->flash = avr->data = NULL;
}

void
avr_update_reset_vector(
avr_t * avr)
{
if (avr->boot_min_words) {
uint8_t fuse = avr->fuse[avr->boot_fuse];
// inverted as fuses read 0 when programmed, 0 is the smallest section
uint8_t bootsz = (~fuse & AVR_FUSE_BOOTSZ_MASK) >> AVR_FUSE_BOOTSZ0;
uint32_t boot_bytes = (avr->boot_min_words * 2) << bootsz;
avr->boot_start = avr->flashend + 1 - boot_bytes;
if (!(fuse & (1 << AVR_FUSE_BOOTRST))) {
avr->reset_pc = avr->boot_start;
avr->pc = avr->reset_pc;
}
}
}

void
avr_reset(
avr_t * avr)
Expand All @@ -234,6 +251,7 @@ avr_reset(
for(int i = 0x20; i <= avr->ioend; i++)
avr->data[i] = 0;
_avr_sp_set(avr, avr->ramend);
avr_update_reset_vector(avr);
avr->pc = avr->reset_pc; // Likely to be zero
for (int i = 0; i < 8; i++)
avr->sreg[i] = 0;
Expand Down
15 changes: 15 additions & 0 deletions simavr/sim/sim_avr.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,11 @@ typedef void (*avr_run_t)(
#define AVR_FUSE_HIGH 1
#define AVR_FUSE_EXT 2

#define AVR_FUSE_BOOTRST 0
#define AVR_FUSE_BOOTSZ0 1
#define AVR_FUSE_BOOTSZ1 2
#define AVR_FUSE_BOOTSZ_MASK ((1 << AVR_FUSE_BOOTSZ1) | (1 << AVR_FUSE_BOOTSZ0))

/*
* Main AVR instance. Some of these fields are set by the AVR "Core" definition files
* the rest is runtime data (as little as possible)
Expand Down Expand Up @@ -308,6 +313,11 @@ typedef struct avr_t {
* allow support for bootloaders
*/
avr_flashaddr_t reset_pc;
// Boot section from the fuses, IVSEL moves the vectors there
uint8_t boot_fuse;
uint16_t boot_min_words;
avr_flashaddr_t boot_start;
avr_regbit_t ivsel;

/* Callback for single-bit instructions (SBI, CBI) on the low 32
* I/O registers. If not set, or the function returns zero, a normal
Expand Down Expand Up @@ -449,6 +459,11 @@ avr_core_allocate(
void
avr_reset(
avr_t * avr);
// sets the boot section from BOOTSZ, and reset_pc and the PC to it when
// BOOTRST is programmed. Otherwise reset_pc and the PC are left alone.
void
avr_update_reset_vector(
avr_t * avr);

// Get a pointer to a core IRQ.

Expand Down
12 changes: 12 additions & 0 deletions simavr/sim/sim_elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ avr_load_firmware(
elf_firmware_t * firmware)
{
fw_chunk_t *chunk, *ochunk;
int fuses = 0;

if (firmware->frequency)
avr->frequency = firmware->frequency;
Expand Down Expand Up @@ -275,6 +276,7 @@ avr_load_firmware(
break;
case FUSES:
do_chunk(chunk, avr->fuse, sizeof avr->fuse, NULL);
fuses = 1;
break;
case LOCK:
do_chunk(chunk, &avr->lockbits, sizeof avr->lockbits, NULL);
Expand All @@ -288,6 +290,16 @@ avr_load_firmware(
chunk = chunk->next;
free(ochunk);
}
if (fuses) {
/*
* avr_init() set the boot section and the reset vector from the
* default fuses of the part, before the firmware's were loaded
* here. Update them, as on a part programmed before power-on, so
* that BOOTRST and BOOTSZ give the reset vector and the boot
* section the firmware asked for.
*/
avr_update_reset_vector(avr);
}
// load the default pull up/down values for ports
for (int i = 0; i < 8 && firmware->external_state[i].port; i++) {
avr_ioport_external_t e = {
Expand Down
2 changes: 2 additions & 0 deletions simavr/sim/sim_interrupts.c
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,8 @@ avr_service_interrupts(
_avr_push_addr(avr, avr->pc);
avr_sreg_set(avr, S_I, 0);
avr->pc = vector->vector * avr->vector_size;
if (avr_regbit_get(avr, avr->ivsel))
avr->pc += avr->boot_start;

avr_raise_irq(vector->irq + AVR_INT_IRQ_RUNNING, 1);
avr_raise_irq(table->irq + AVR_INT_IRQ_RUNNING, vector->vector);
Expand Down
1 change: 1 addition & 0 deletions tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ axf: ${sources:.c=.axf}
hex: atmega88_example.hex

at90usb162_usb.axf atmega32u2_usb.axf atmega32u4_usb.axf: usb_test_fw.h
atmega328p_ivsel_boot.axf: AVR_LDFLAGS = -Wl,--section-start=.text=0x7800

${OBJ}/%.tst: tests.c %.c
ifneq ($(E),)
Expand Down
Loading
Loading