/** * Enters the boot loader. */ static void enterBootloader() { /* Invalidate the checksum in flash */ flashChecksum = FLASH_CHECKSUM ^ 0xff; writeFlashChecksum(); /* Enable the watchdog and enter an endless loop. This will reboot the device */ cli(); wdt_enable(WDTO_500MS); while (1); }
/** * Main program. */ int main() { uchar i, errorCode; uint8_t oldRunning = 0, running; /* Disable the watchdog */ MCUSR = 0; wdt_disable(); /* Read the serial number from flash */ readSerial(); /* Read the flash checksum from EEPROM */ readFlashChecksum(); /* Initialize USB and enable global interrupts */ initUSB(); /* First initialize the PWM so all LEDs are off. */ pwmInit(prescalingIndex); pwmSet(CHANNEL0, 0, 0); pwmSet(CHANNEL1, 0, 0); pwmSet(CHANNEL2, 0, 0); pwmSet(CHANNEL3, 0, 0); pwmSet(CHANNEL4, 0, 0); pwmSet(CHANNEL5, 0, 0); /* Initialize the blinks memory from eeprom */ eeprom_busy_wait(); eeprom_read_block(memory, (uint8_t*) 1, BLINKS_MEM_SIZE); /* Make sure bits 7 is cleared and bit 6 is set */ memory[0] &= 0x7f; memory[0] |= 0x40; /* Initialize blinks */ blinksScript.data = BLINKS_SCRIPT(memory); blinksScript.size = BLINKS_SCRIPT_SIZE; errorCode = 1; /* Infinite program loop */ i = 0; uint16_t shutdown = 0; while ((memory[0] & 0x40) || (--shutdown)) { /* Re-init PWM if needed */ if (prescalingIndex != getPrescalingIndex()) { prescalingIndex = getPrescalingIndex(); pwmInit(prescalingIndex); } /* Process USB events */ usbPoll(); /* Execute special commands */ switch (command) { case CMD_SAVE: eeprom_busy_wait(); eeprom_write_block(memory, (uint8_t*) 1, BLINKS_MEM_SIZE); break; } command = 0; /* Reset script when running flag has changed or an error occurred */ running = BLINKS_FLAGS(memory) & BLINKS_FLAG_RUNNING; if (running != oldRunning || errorCode) { errorCode = blinksReset(blinksScript); oldRunning = running; } /* Execute next step in program when program is running an no error occurred. */ if (running && !errorCode) errorCode = blinksStep(blinksScript); /* Apply channel values */ if (!errorCode) { unsigned char outputs = blinksCountOutputs(blinksScript); unsigned char invert = isInverted(); if (outputs > 0) pwmSet(CHANNEL0, blinksGetOutput(blinksScript, 0), invert); if (outputs > 1) pwmSet(CHANNEL1, blinksGetOutput(blinksScript, 1), invert); if (outputs > 2) pwmSet(CHANNEL2, blinksGetOutput(blinksScript, 2), invert); if (outputs > 3) pwmSet(CHANNEL3, blinksGetOutput(blinksScript, 3), invert); if (outputs > 4) pwmSet(CHANNEL4, blinksGetOutput(blinksScript, 4), invert); if (outputs > 5) pwmSet(CHANNEL5, blinksGetOutput(blinksScript, 5), invert); } /* When this loop has been executed for 100 times then mark the flash as OK */ if ((flashChecksum != FLASH_CHECKSUM) && (i < 100)) { i++; if (i == 100) { flashChecksum = FLASH_CHECKSUM; writeFlashChecksum(); } } } enterBootloader(); return 0; }
/** * Main program. */ int main() { uchar i; /* Disable the watchdog */ MCUSR = 0; wdt_disable(); /* Read the flash checksum from EEPROM */ readFlashChecksum(); /* Initialize USB and enable global interrupts */ initUSB(); /* First initialize the PWM so all LEDs are off. */ pwmInit(0); pwmSet(CHANNEL0, 0, 0); pwmSet(CHANNEL1, 0, 0); pwmSet(CHANNEL2, 255, 0); pwmSet(CHANNEL3, 0, 0); pwmSet(CHANNEL4, 0, 0); pwmSet(CHANNEL5, 0, 0); /* Initialize the blinks memory from eeprom */ eeprom_busy_wait(); eeprom_read_block(memory, (uint8_t*) 1, MEM_SIZE); /* Make sure bits 7 is cleared and bit 6 is set */ memory[0] &= 0x7f; memory[0] |= 0x40; /* Infinite program loop */ i = 0; while (memory[0] & 0x40) { restart = 50; while (restart) { /* Process USB events */ usbPoll(); int len = buffer_write_index - buffer_read_index; if (len > 0 && usbInterruptIsReady()) { if (len > 8) len = 8; usbSetInterrupt(&buffer[buffer_read_index], len); buffer_read_index += len; to_read -= len; if (!to_read) { buffer_read_index = 0; buffer_write_index = 0; } } if (restart < 50) restart--; /* When this loop has been executed for 100 times then mark the flash as OK */ if ((flashChecksum != FLASH_CHECKSUM) && (i < 100)) { i++; if (i == 100) { flashChecksum = FLASH_CHECKSUM; writeFlashChecksum(); } } } reenumerate(500); } enterBootloader(); return 0; }