/** Main program entry point. This routine configures the hardware required by the bootloader, then continuously * runs the bootloader processing routine until instructed to soft-exit. */ int main(void) { /* Disable watchdog if enabled by bootloader/fuses */ MCUSR &= ~(1 << WDRF); wdt_disable(); /* Disable clock division */ clock_prescale_set(clock_div_1); /* Relocate the interrupt vector table to the bootloader section */ MCUCR = (1 << IVCE); MCUCR = (1 << IVSEL); /* Initialize USB subsystem */ USB_Init(); while (RunBootloader) USB_USBTask(); /* Shut down the USB interface, so that the host will register the disconnection */ USB_ShutDown(); /* Wait 100ms to give the host time to register the disconnection */ _delay_ms(100); /* Enable the watchdog and force a timeout to reset the AVR */ wdt_enable(WDTO_250MS); for (;;); }
/** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */ void EVENT_USB_Host_HostError(const uint8_t ErrorCode) { USB_ShutDown(); LEDs_SetAllLEDs(LEDMASK_USB_ERROR); for(;;); }
/** Simple routine which aborts the program execution when a fatal error occurs, and is passed to the * application via an event. When run, this function shuts down the USB interface, indicates an error * via the board LEDs, prints an error message to the USART and then enters an infinite loop, preventing * any more application code (other than interrupts) from executing. */ static void Abort_Program(void) { USB_ShutDown(); LEDs_SetAllLEDs(LEDS_LED1 | LEDS_LED3); puts_P(PSTR(ESC_INVERSE_ON "\r\n**PROGRAM ABORT**")); for (;;); }
/** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */ void EVENT_USB_Host_HostError(const uint8_t ErrorCode) { USB_ShutDown(); printf_P(PSTR(ESC_FG_RED "Host Mode Error\r\n" " -- Error Code %d\r\n" ESC_FG_WHITE), ErrorCode); LEDs_SetAllLEDs(LEDMASK_USB_ERROR); for(;;); }
/** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */ void EVENT_USB_HostError(const uint8_t ErrorCode) { USB_ShutDown(); puts_P(PSTR(ESC_BG_RED "Host Mode Error\r\n")); printf_P(PSTR(" -- Error Code %d\r\n"), ErrorCode); UpdateStatus(Status_HardwareError); for(;;); }
/** Resets all configured hardware required for the bootloader back to their original states. */ void ResetHardware(void) { /* Shut down the USB subsystem */ USB_ShutDown(); /* Relocate the interrupt vector table back to the application section */ MCUCR = (1 << IVCE); MCUCR = 0; /* Re-enable RWW section */ boot_rww_enable(); }
/* * Delay a little (required), shutdown USB, disable watchdog and interrupts, * and jump to the bootloader. */ static void enterBootLoader(void) { // Control request was handled, so ack it Endpoint_ClearSETUP(); while (!Endpoint_IsINReady()) ; Endpoint_ClearIN(); // Now shut things down and jump to the bootloader. DELAY_MS(100); wdt_disable(); USB_ShutDown(); cli(); cpu_bootloader_start(); }
////////////////////////////////////////////////// // boot void prepare_boot(char *in) { uint8_t bl = 0; if(in) fromhex(in+1, &bl, 1); if(bl == 0xff) // Allow testing while(1); #ifdef ARM unsigned char volatile * const ram = (unsigned char *) AT91C_ISRAM; //Reset if(bl) *ram = 0xaa; // Next reboot we'd like to jump to the bootloader. USBD_Disconnect(); my_delay_ms(250); my_delay_ms(250); my_delay_ms(250); my_delay_ms(250); AT91C_BASE_RSTC->RSTC_RCR = AT91C_RSTC_PROCRST | AT91C_RSTC_PERRST | AT91C_RSTC_EXTRST | 0xA5<<24; while (1); #else if(bl) // Next reboot we'd like to jump to the bootloader. ewb( EE_REQBL, 1 ); // Simply jumping to the bootloader from here // wont't work. Neither helps to shutdown USB // first. #ifdef HAS_USB USB_ShutDown(); // ??? Needed? #endif #ifdef HAS_FS fs_sync(&fs); // Sync the filesystem #endif TIMSK0 = 0; // Disable the clock which resets the watchdog cli(); wdt_enable(WDTO_15MS); // Make sure the watchdog is running while (1); // go to bed, the wathchdog will take us to reset #endif }
void Jump_To_Bootloader(void) { // If USB is used, detach from the bus USB_ShutDown(); // Disable all interrupts cli(); // Wait two seconds for the USB detachment to register on the host for (uint8_t i = 0; i < 128; i++) _delay_ms(16); // Set the bootloader key to the magic value and force a reset Boot_Key = MAGIC_BOOT_KEY; wdt_enable(WDTO_250MS); for (;;); }
void USB_Init( #if defined(USB_CAN_BE_BOTH) const uint8_t Mode #endif #if (defined(USB_CAN_BE_BOTH) && !defined(USE_STATIC_OPTIONS)) , #elif (!defined(USB_CAN_BE_BOTH) && defined(USE_STATIC_OPTIONS)) void #endif #if !defined(USE_STATIC_OPTIONS) const uint8_t Options #endif ) { if (USB_IsInitialized) USB_ShutDown(); #if (!defined(USB_HOST_ONLY) && !defined(USB_DEVICE_ONLY)) USB_CurrentMode = Mode; #endif #if defined(USB_DEVICE_ONLY) #if defined(USB_FULL_CONTROLLER) UHWCON |= (1 << UIMOD); #endif #elif defined(USB_HOST_ONLY) UHWCON &= ~(1 << UIMOD); #else if (Mode == USB_MODE_NONE) { RAISE_EVENT(USB_PowerOnFail, POWERON_ERROR_NoUSBModeSpecified); return; } else if (Mode == USB_MODE_UID) { UHWCON |= (1 << UIDE); USB_INT_Clear(USB_INT_IDTI); USB_INT_Enable(USB_INT_IDTI); USB_CurrentMode = USB_GetUSBModeFromUID(); } else if (Mode == USB_MODE_DEVICE) { UHWCON |= (1 << UIMOD); } else if (Mode == USB_MODE_HOST) { UHWCON &= ~(1 << UIMOD); } #endif #if defined(USB_CAN_BE_BOTH) USB_InitTaskPointer(); #else USB_IsInitialized = true; #endif #if defined(USB_CAN_BE_HOST) USB_ControlPipeSize = PIPE_CONTROLPIPE_DEFAULT_SIZE; #endif #if defined(USB_FULL_CONTROLLER) || defined(USB_MODIFIED_FULL_CONTROLLER) USB_OTGPAD_On(); #endif #if !defined(USE_STATIC_OPTIONS) USB_Options = Options; #endif #if defined(USB_DEVICE_ONLY) #if defined(USB_FULL_CONTROLLER) || defined(USB_MODIFIED_FULL_CONTROLLER) USB_INT_Enable(USB_INT_VBUS); #else USB_SetupInterface(); #endif #elif defined(USB_HOST_ONLY) USB_SetupInterface(); #else if (USB_CurrentMode == USB_MODE_DEVICE) USB_INT_Enable(USB_INT_VBUS); else USB_SetupInterface(); #endif sei(); }