summaryrefslogtreecommitdiffstats
path: root/firmware/main.c
diff options
context:
space:
mode:
authorcpldcpu <cpldcpu@gmail.com>2013-12-17 19:49:02 +0100
committercpldcpu <cpldcpu@gmail.com>2013-12-17 19:49:02 +0100
commitbd22f84cfae23f84a2d2fee343b937794942b0f1 (patch)
treeae2421755726939f8b5ce2b722ff70cd502e98c4 /firmware/main.c
parentbbcb8b469b8201a7b976852b0934a887e75c3ec1 (diff)
downloadmicronucleus-bd22f84cfae23f84a2d2fee343b937794942b0f1.tar.gz
micronucleus-bd22f84cfae23f84a2d2fee343b937794942b0f1.tar.bz2
micronucleus-bd22f84cfae23f84a2d2fee343b937794942b0f1.zip
firmware: bootloader will only exit when program was loaded
Diffstat (limited to 'firmware/main.c')
-rw-r--r--firmware/main.c56
1 files changed, 30 insertions, 26 deletions
diff --git a/firmware/main.c b/firmware/main.c
index a7deccc..36a42be 100644
--- a/firmware/main.c
+++ b/firmware/main.c
@@ -284,20 +284,23 @@ static inline void leaveBootloader(void) {
}
int main(void) {
- /* initialize */
- #if OSCCAL_RESTORE
- osccal_default = OSCCAL;
- #endif
- #if (!SET_CLOCK_PRESCALER) && LOW_POWER_MODE
- uint8_t prescaler_default = CLKPR;
- #endif
+
+ /* initialize */
+ #if OSCCAL_RESTORE
+ osccal_default = OSCCAL;
+ #endif
+
+ #if (!SET_CLOCK_PRESCALER) && LOW_POWER_MODE
+ uint8_t prescaler_default = CLKPR;
+ #endif
- bootLoaderInit();
+ bootLoaderInit();
-# if AUTO_EXIT_NO_USB_MS
- ((uint8_t*)&idlePolls)[1]=((AUTO_EXIT_MS-AUTO_EXIT_NO_USB_MS) * 10UL)>>8; // write only high byte to save 6 bytes
-# endif
+ #if AUTO_EXIT_NO_USB_MS
+ ((uint8_t*)&idlePolls)[1]=((AUTO_EXIT_MS-AUTO_EXIT_NO_USB_MS) * 10UL)>>8; // write only high byte to save 6 bytes
+ #endif
+
if (bootLoaderStartCondition()) {
initHardware();
@@ -319,14 +322,15 @@ int main(void) {
writeFlashPage();
}
-# if LED_PRESENT
- LED_MACRO( ((uint8_t*)&idlePolls)[1] )
-# endif
+ #if LED_PRESENT
+ LED_MACRO( ((uint8_t*)&idlePolls)[1] )
+ #endif
- // Only try to execute program if reset vector is set - bootloader will not time out with erased memory
- if (!bootLoaderCondition()&&(pgm_read_byte(BOOTLOADER_ADDRESS - TINYVECTOR_RESET_OFFSET + 1)!=0xff)) fireEvent(EVENT_EXECUTE);
-
- } while(!isEvent(EVENT_EXECUTE)); /* main event loop runs as long as program is not executed */
+ // Try to execute program if bootloader exit condition is met
+ if (!bootLoaderCondition()) fireEvent(EVENT_EXECUTE);
+
+ /* main event loop runs as long as no problem is uploaded or existing program is not executed */
+ } while((!isEvent(EVENT_EXECUTE))||(pgm_read_byte(BOOTLOADER_ADDRESS - TINYVECTOR_RESET_OFFSET + 1)==0xff));
}
// set clock prescaler to desired clock speed (changing from clkdiv8, or no division, depending on fuses)
@@ -340,15 +344,15 @@ int main(void) {
#endif
#endif
-# if LED_PRESENT
- LED_EXIT();
-# endif
+ #if LED_PRESENT
+ LED_EXIT();
+ #endif
-# if OSCCAL_RESTORE
- OSCCAL=osccal_default;
- asm volatile("nop"); // NOP to avoid CPU hickup during oscillator stabilization
-# endif
+ #if OSCCAL_RESTORE
+ OSCCAL=osccal_default;
+ asm volatile("nop"); // NOP to avoid CPU hickup during oscillator stabilization
+ #endif
- leaveBootloader();
+ leaveBootloader();
}
/* ------------------------------------------------------------------------ */