// main int main(void) { wdt_enable(WDTO_1S); hardwareInit(); usbInit(); sei(); unsigned int adcValue,replymask,replyshift,replybyte; while(1) { uchar i = 0; wdt_reset(); usbPoll(); PORTD ^= (1 << 6); //jump to bootloader if jumper is HIGH if(bit_is_clear(PIND, 5)) { startBootloader(); } for(i = 0; i < ADC_CHANNELS; i++) { //adcValue = adc_read(ADC_PRESCALER_32, ADC_VREF_AVCC, i); adcValue = i; usb_reply[i] = adcValue >> 2; replybyte = 16 + (i / 4); replyshift = ((i % 4) * 2); replymask = (3 << replyshift); usb_reply[replybyte] = (usb_reply[replybyte] & ~replymask) | (replymask & (adcValue << replyshift)); _delay_us(ADCDELAY); } usb_reply[20] = PINC; } return 0; }
// ------------------------------------------------------------------------------ // - usbFunctionSetup // ------------------------------------------------------------------------------ uchar usbFunctionSetup(uchar data[8]) { if(data[1] == POLL) { // GET ALL ad_values reply[0] = 255; // this comes from the old gnuswitch, mixlevel, does not make sense anymore reply[1] = on_tally_idx; reply[2] = 0; usbMsgPtr = reply; return 3; } else if(data[1] == cmd_StartBootloader) { // Start Bootloader for reprogramming the gnusb startBootloader(); } return 0; }
// // main function // int main(void) { char c; uint16_t loop = 0; uint8_t bootloaderEnableFlag = FALSE; // relocate interrupt vector table to bottom of flash // in case the bootloader will not be started myIVSELREG = _BV(IVCE); myIVSELREG = 0; // init USART myUBRRH = (F_CPU/(BAUDRATE*16L)-1) >> 8; // calculate baudrate and set high byte myUBRRL = (uint8_t)(F_CPU/(BAUDRATE*16L)-1); // and low byte myUCSRB = _BV(myTXEN) | _BV(myRXEN) | _BV(myRXCIE); // enable transmitter and receiver and receiver interrupt myUCSRC = myURSEL | _BV(myUCSZ1) | _BV(myUCSZ0); // 8 bit character size, 1 stop bit, no parity // the bootloader may be activated either if // the character 'i' (interactive mode) was received from USART // or the flash is (still) empty // poll USART receive complete flag 64k times to catch the 'i' reliably do { if(bit_is_set(myUCSRA, myRXC)) if(myUDR == 'i') bootloaderEnableFlag = TRUE; } while(--loop); // test if flash is empty (i.e. flash content is 0xff) if(pgm_read_byte_near(0x0000) == 0xFF) { bootloaderEnableFlag = TRUE; // set enable flag } // check enable flag and start application if FALSE if(bootloaderEnableFlag == FALSE) { myUCSRB = 0; // clear USART register to reset default startApplication(); // start application code } // // now the bootloader code begins // // welcome message and prompt uartPutChar('\r'); uartPutChar('>'); // loop until a valid character is received do { c = uartGetChar(); // read a character if(c == 'f') { // 'f' selects flash programming uartPutChar('f'); // echo the 'f' programThisMemory = FLASH; // set flag } #ifdef EEPROM_CODE if(c == 'e') { // 'e' selects eeprom programming uartPutChar('e'); // echo the 'e' programThisMemory = EEPROM; // set flag } #endif if(c == 'g') { // 'g' starts the application uartPutChar('g'); // echo the 'g' startApplication(); // and jump to 0x0000 } } while(!programThisMemory); // exit loop when a valid key was pressed // move interrupt vector table to boot loader area myIVSELREG = _BV(IVCE); myIVSELREG = _BV(IVSEL); uartPutChar('\r'); // set cursor to next line receiveBufferFull = FALSE; // reset full flag receiveBufferPointer = (char *)receiveBuffer; // reset buffer pointer to start of buffer // enable interrupts sei(); // endless loop while(1) { // if buffer is full, parse the buffer and write to flash if(receiveBufferFull) { cli(); // disable interrupts // if parsing produced an error, restart bootloader if(!parseSrecBuffer(receiveBuffer)) { uartPutChar('\r'); // set cursor to next line startBootloader(); // restart the bootloader } // was an end-of-file s-record found? if(srecEndOfFile) { uartPutChar('O'); // 'OK' indicates successful programming uartPutChar('K'); loop_until_bit_is_set(myUCSRA, myUDRE); // wait until character is transmitted startBootloader(); // restart the bootloader } receiveBufferFull = FALSE; // reset full flag receiveBufferPointer = (char *)receiveBuffer; // reset buffer pointer to start of buffer sei(); // enable interrupts } } }