int main() { unsigned int i=0; unsigned int num_pages; // Initial state: flash is insactive, and the big guy just pulled down the // MISO pin. last_f = 1; if (READ_MISO_PIN()) last_b = 0; else last_b = 1; if (last_b == 0) { CLR_BIG_GUY_RESET_PIN(); MAKE_BIG_GUY_RESET_OUTPUT(); spi_init(); CLR_SCK_PIN(); MAKE_BIG_GUY_RESET_OUTPUT(); pulse_flash_cs(); wait_ms(20); // read the program legth num_pages = read_params(); //num_pages=575; num_pages += 0xff; num_pages >>= 8; error_code(num_pages); if (program_enable()) { program_erase(); } else { error_code(0x10); hibernate(); } // wait a little bit wait_ms(120); pulse_flash_cs(); // pulse the reset pin //wait_ms(20); // upload the program if (program_enable()) { for (i=0; i < num_pages; i++) { program_page(i); wait_ms(56); } } else { error_code(0x20); } }
/*---------------------------------------------------------------------------*/ int xmem_pwrite(const void *_buf, int size, unsigned long addr) { const unsigned char *p = _buf; const unsigned long end = addr + size; unsigned long i, next_page; ENERGEST_ON(ENERGEST_TYPE_FLASH_WRITE); for(i = addr; i < end;) { next_page = (i | 0xff) + 1; if(next_page > end) { next_page = end; } p = program_page(i, p, next_page - i); i = next_page; } ENERGEST_OFF(ENERGEST_TYPE_FLASH_WRITE); return size; }
void avrisp(int ReceivedByte){ // is pmode active? if (ram.isp.pmode) LEDs_TurnOnLEDs(LEDS_PMODE); else LEDs_TurnOffLEDs(LEDS_PMODE); // is there an error? if (ram.isp.error) LEDs_TurnOnLEDs(LEDS_ERR); else LEDs_TurnOffLEDs(LEDS_ERR); // read in bytes from the CDC interface if (!(ReceivedByte < 0)){ switch (ReceivedByte) { case STK_GET_SYNC: ram.isp.error = 0; replyOK(); break; case STK_GET_SIGNON: if (getch() == CRC_EOP) { sendCDCbyte(STK_INSYNC); sendCDCbyte('A'); sendCDCbyte('V'); sendCDCbyte('R'); sendCDCbyte(' '); sendCDCbyte('I'); sendCDCbyte('S'); sendCDCbyte('P'); sendCDCbyte(STK_OK); } break; case STK_GET_PARM: get_parameters(getch()); break; case STK_SET_PARM: set_parameters(); replyOK(); break; case STK_SET_PARM_EXT: // extended parameters - ignore for now fill(5); replyOK(); break; case STK_PMODE_START: start_pmode(); replyOK(); break; case STK_SET_ADDR: ram.isp._addr = getch(); ram.isp._addr += 256 * getch(); replyOK(); break; case STK_PROG_FLASH: //uint8_t low = getch(); getch(); //uint8_t high = getch(); getch(); replyOK(); break; case STK_PROG_DATA: //uint8_t data = getch(); getch(); replyOK(); break; case STK_PROG_PAGE: program_page(); break; case STK_READ_PAGE: read_page(); break; case STK_UNIVERSAL: universal(); break; case STK_PMODE_END: ram.isp.error = 0; end_pmode(); replyOK(); break; case STK_READ_SIGN: read_signature(); break; // expecting a command, not CRC_EOP // this is how we can get back in sync case CRC_EOP: ram.isp.error++; sendCDCbyte(STK_NOSYNC); break; // anything else we will return STK_UNKNOWN default: ram.isp.error++; if (CRC_EOP == getch()) sendCDCbyte(STK_UNKNOWN); else sendCDCbyte(STK_NOSYNC); } } }