-\r
- if(BUTTON_PRESS()) {\r
- UsbStart();\r
- }\r
-\r
- for(;;) {\r
- WDT_HIT();\r
-\r
- UsbPoll(TRUE);\r
-\r
- if(!BUTTON_PRESS()) {\r
- USB_D_PLUS_PULLUP_OFF();\r
- LED_B_ON();\r
-\r
- // jump to Flash address of the osimage entry point (LSBit set for thumb mode)\r
- asm("bx %0\n" : : "r" ( ((int)&_osimage_entry) | 0x1 ) );\r
- }\r
+ \r
+ int common_area_present = 0;\r
+ switch(RSTC_STATUS & RST_STATUS_TYPE_MASK) {\r
+ case RST_STATUS_TYPE_WATCHDOG:\r
+ case RST_STATUS_TYPE_SOFTWARE:\r
+ case RST_STATUS_TYPE_USER:\r
+ /* In these cases the common_area in RAM should be ok, retain it if it's there */\r
+ if(common_area.magic == COMMON_AREA_MAGIC && common_area.version == 1) {\r
+ common_area_present = 1;\r
+ }\r
+ break;\r
+ default: /* Otherwise, initialize it from scratch */\r
+ break;\r
+ }\r
+ \r
+ if(!common_area_present){\r
+ /* Common area not ok, initialize it */\r
+ int i; for(i=0; i<sizeof(common_area); i++) { /* Makeshift memset, no need to drag util.c into this */\r
+ ((char*)&common_area)[i] = 0;\r
+ }\r
+ common_area.magic = COMMON_AREA_MAGIC;\r
+ common_area.version = 1;\r
+ common_area.flags.bootrom_present = 1;\r
+ }\r
+ \r
+ common_area.flags.bootrom_present = 1;\r
+ if(common_area.command == COMMON_AREA_COMMAND_ENTER_FLASH_MODE) {\r
+ common_area.command = COMMON_AREA_COMMAND_NONE;\r
+ flash_mode(1);\r
+ } else if(BUTTON_PRESS()) {\r
+ flash_mode(0);\r
+ } else {\r
+ // jump to Flash address of the osimage entry point (LSBit set for thumb mode)\r
+ asm("bx %0\n" : : "r" ( ((int)&_osimage_entry) | 0x1 ) );\r