Skip to content

Commit

Permalink
Merge pull request Optiboot#248 from majekw/appstart
Browse files Browse the repository at this point in the history
Save 2-6 bytes on appStart function.
  • Loading branch information
WestfW authored Aug 19, 2018
2 parents 862e9dd + bf652ba commit 9b8e1f9
Showing 1 changed file with 29 additions and 18 deletions.
47 changes: 29 additions & 18 deletions optiboot/bootloaders/optiboot/optiboot.c
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,6 @@ static inline void read_mem(uint8_t memtype,
#ifdef SOFT_UART
void uartDelay() __attribute__ ((naked));
#endif
void appStart(uint8_t rstFlags) __attribute__ ((naked));

/*
* RAMSTART should be self-explanatory. It's bigger on parts with a
Expand Down Expand Up @@ -564,7 +563,35 @@ int main(void) {
MCUSR = ~(_BV(WDRF));
#endif
}
appStart(ch);
/*
* save the reset flags in the designated register
* This can be saved in a main program by putting code in .init0 (which
* executes before normal c init code) to save R2 to a global variable.
*/
__asm__ __volatile__ ("mov r2, %0\n" :: "r" (ch));

// switch off watchdog
watchdogConfig(WATCHDOG_OFF);
// Note that appstart_vec is defined so that this works with either
// real or virtual boot partitions.
__asm__ __volatile__ (
// Jump to 'save' or RST vector
#ifdef VIRTUAL_BOOT_PARTITION
// full code version for virtual boot partition
"ldi r30,%[rstvec]\n"
"clr r31\n"
"ijmp\n"::[rstvec] "M"(appstart_vec)
#else
#ifdef RAMPZ
// use absolute jump for devices with lot of flash
"jmp 0\n"::
#else
// use rjmp to go around end of flash to address 0
// it uses fact that optiboot_version constant is 2 bytes before end of flash
"rjmp optiboot_version+2\n"
#endif //RAMPZ
#endif //VIRTUAL_BOOT_PARTITION
);
}
}

Expand Down Expand Up @@ -1020,22 +1047,6 @@ void watchdogConfig(uint8_t x) {
#endif
}

void appStart(uint8_t rstFlags) {
// save the reset flags in the designated register
// This can be saved in a main program by putting code in .init0 (which
// executes before normal c init code) to save R2 to a global variable.
__asm__ __volatile__ ("mov r2, %0\n" :: "r" (rstFlags));

watchdogConfig(WATCHDOG_OFF);
// Note that appstart_vec is defined so that this works with either
// real or virtual boot partitions.
__asm__ __volatile__ (
// Jump to 'save' or RST vector
"ldi r30,%[rstvec]\n"
"clr r31\n"
"ijmp\n"::[rstvec] "M"(appstart_vec)
);
}

/*
* void writebuffer(memtype, buffer, address, length)
Expand Down

0 comments on commit 9b8e1f9

Please sign in to comment.