int main() { log_init(); // If bootloader mode not requested, go immediately to app. if (!ShouldEnterBootloader()) { OscCalibrateCached(); log_printf("Running app..."); __asm__("goto __APP_RESET"); } // We need to enter bootloader mode, wait for the boot pin to be released. while (!led_read()); // Now we can start! led_init(); #ifdef SIGNAL_AFTER_BAD_RESET if (RCON & 0b1100001001000000) { SignalRcon(); } #endif log_printf("Hello from Bootloader!!!"); if (IsPin1Grounded()) { log_printf("Erasing config."); EraseConfig(); } OscCalibrateCached(); Blink(5); USBInitialize(); while (1) { // Wait for connection while (!(USBGetDeviceState() == CONFIGURED_STATE && CDCIsDtePresent())) USBTasks(); log_printf("Connected!"); BootProtocolInit(); while (USBGetDeviceState() == CONFIGURED_STATE && CDCIsDtePresent()) { static char in_buf[64]; USBTasks(); BYTE size = getsUSBUSART(in_buf, sizeof(in_buf)); if (!BootProtocolProcess(in_buf, size)) { log_printf("Protocol error. Will detach / re-attach."); USBSoftDetach(); __delay_ms(2000); USBDeviceAttach(); break; } BootProtocolTasks(); } log_printf("Disconnected!"); } return 0; }
void ChipKITUSBSoftDetach(void) { USBSoftDetach(); }