uchar ispEnterProgrammingMode_jarodlau() { uchar b; // return ispEnterProgrammingMode();//原来的代码 //根据跳线恢复设置 //跳线设置速度的部分 ispDisconnect(); ledRedOff(); /* set SCK speed */ if((PINC & (1 << PC2)) == 0) { ispSetSCKOption(USBASP_ISP_SCK_8);//低速 } else { ispSetSCKOption(prog_sck);//自动 //默认 prog_sck=USBASP_ISP_SCK_375:硬件SPI, enable SPI, master, 375kHz, XTAL/32 (default) } prog_address_newmode = 0; ledRedOn(); ispConnect(); //加入自动降速的代码 b = ispEnterProgrammingMode(); if(b == 1) { //如果 ispEnterProgrammingMode(); 检测了32次都失败,就降速 ispDisconnect(); ledRedOff(); ispSetSCKOption(USBASP_ISP_SCK_8);//低速 prog_address_newmode = 0; ledRedOn(); ispConnect(); b = ispEnterProgrammingMode(); } return b; }
uchar usbFunctionSetup(uchar data[8]) { uchar len = 0; if (data[1] == USBASP_FUNC_CONNECT) { /* set SCK speed */ if ((PINC & (1 << PC2)) == 0) { ispSetSCKOption(USBASP_ISP_SCK_8); } else { ispSetSCKOption(prog_sck); } /* set compatibility mode of address delivering */ prog_address_newmode = 0; ledRedOn(); ispConnect(); } else if (data[1] == USBASP_FUNC_DISCONNECT) { ispDisconnect(); ledRedOff(); } else if (data[1] == USBASP_FUNC_TRANSMIT) { replyBuffer[0] = ispTransmit(data[2]); replyBuffer[1] = ispTransmit(data[3]); replyBuffer[2] = ispTransmit(data[4]); replyBuffer[3] = ispTransmit(data[5]); len = 4; } else if (data[1] == USBASP_FUNC_READFLASH) { if (!prog_address_newmode) prog_address = (data[3] << 8) | data[2]; prog_nbytes = (data[7] << 8) | data[6]; prog_state = PROG_STATE_READFLASH; len = 0xff; /* multiple in */ } else if (data[1] == USBASP_FUNC_READEEPROM) { if (!prog_address_newmode) prog_address = (data[3] << 8) | data[2]; prog_nbytes = (data[7] << 8) | data[6]; prog_state = PROG_STATE_READEEPROM; len = 0xff; /* multiple in */ } else if (data[1] == USBASP_FUNC_ENABLEPROG) { replyBuffer[0] = ispEnterProgrammingMode(); len = 1; } else if (data[1] == USBASP_FUNC_WRITEFLASH) { if (!prog_address_newmode) prog_address = (data[3] << 8) | data[2]; prog_pagesize = data[4]; prog_blockflags = data[5] & 0x0F; prog_pagesize += (((unsigned int) data[5] & 0xF0) << 4); if (prog_blockflags & PROG_BLOCKFLAG_FIRST) { prog_pagecounter = prog_pagesize; } prog_nbytes = (data[7] << 8) | data[6]; prog_state = PROG_STATE_WRITEFLASH; len = 0xff; /* multiple out */ } else if (data[1] == USBASP_FUNC_WRITEEEPROM) { if (!prog_address_newmode) prog_address = (data[3] << 8) | data[2]; prog_pagesize = 0; prog_blockflags = 0; prog_nbytes = (data[7] << 8) | data[6]; prog_state = PROG_STATE_WRITEEEPROM; len = 0xff; /* multiple out */ } else if (data[1] == USBASP_FUNC_SETLONGADDRESS) { /* set new mode of address delivering (ignore address delivered in commands) */ prog_address_newmode = 1; /* set new address */ prog_address = *((unsigned long*) &data[2]); } else if (data[1] == USBASP_FUNC_SETISPSCK) { /* set sck option */ prog_sck = data[2]; replyBuffer[0] = 0; len = 1; } usbMsgPtr = replyBuffer; return len; }
uint32_t flashAvr( uint8_t *data64, uint8_t *data2561, uint16_t size64, uint16_t size2561 ) { uint32_t i ; uint8_t *data ; uint16_t address ; uint16_t size ; uint8_t *verifyData ; ispConnect() ; clockWait( 64 ) ; // 20mS wdt_reset() ; if ( ( IspStatus = ispEnterProgrammingMode() ) == 0 ) { Signature[0] = ispReadSignature(0) ; Signature[1] = ispReadSignature(1) ; Signature[2] = ispReadSignature(2) ; } // choose file here data = data64 ; size = size64 ; if ( Signature[1] == 0x98 ) { data = data2561 ; size = size2561 ; } verifyData = data ; ispChipErase() ; if ( Signature[1] == 0x98 ) { ispTransmit(0x4D) ; ispTransmit(0) ; ispTransmit(0) ; ispTransmit(0) ; } for ( address = 0 ; address < size ; address += 256 ) { i = 256 ; if ( address + 256 > size ) { i = size - address ; } flashPage( data, address, i ) ; data += 256 ; wdt_reset() ; } for ( i = 0 ; i < 64 ; i += 1 ) { M64Program[i] = ispReadFlash( i ) ; } ispDisconnect() ; clockWait(64) ; wdt_reset() ; for ( i = 0 ; i < 64 ; i += 1 ) { if ( *verifyData++ != M64Program[i] ) { return 1 ; } } return 0 ; }
uchar usbFunctionSetup(uchar data[8]) { uchar len = 0; #ifdef UART_DEBUG TransmitHex(data[1]); #endif if (data[1] == USBASP_FUNC_CONNECT) { #if 0 /* set SCK speed */ if ((PINC & (1 << PC2)) == 0) { ispSetSCKOption(USBASP_ISP_SCK_8); } else { ispSetSCKOption(prog_sck); } #endif /* set compatibility mode of address delivering */ prog_address_newmode = 0; ledRedOn(); ispConnect(); } else if (data[1] == USBASP_FUNC_DISCONNECT) { ispDisconnect(); ledRedOff(); } else if (data[1] == USBASP_FUNC_TRANSMIT) { // debug - print out contents of message #ifdef UART_DEBUG TransmitHex(data[2]); TransmitHex(data[3]); TransmitHex(data[4]); TransmitHex(data[5]); #endif replyBuffer[0] = 0; replyBuffer[1] = 0; replyBuffer[2] = 0; replyBuffer[3] = interpretSPICommand(&data[2]); len = 4; } else if (data[1] == USBASP_FUNC_READFLASH) { if (!prog_address_newmode) prog_address = (data[3] << 8) | data[2]; prog_nbytes = (data[7] << 8) | data[6]; prog_state = PROG_STATE_READFLASH; len = 0xff; /* multiple in */ } else if (data[1] == USBASP_FUNC_READEEPROM) { if (!prog_address_newmode) prog_address = (data[3] << 8) | data[2]; prog_nbytes = (data[7] << 8) | data[6]; prog_state = PROG_STATE_READEEPROM; len = 0xff; /* multiple in */ } else if (data[1] == USBASP_FUNC_ENABLEPROG) { replyBuffer[0] = ispEnterProgrammingMode(); len = 1; } else if (data[1] == USBASP_FUNC_WRITEFLASH) { if (!prog_address_newmode) prog_address = (data[3] << 8) | data[2]; prog_pagesize = data[4]; prog_blockflags = data[5] & 0x0F; prog_pagesize += (((unsigned int) data[5] & 0xF0) << 4); if (prog_blockflags & PROG_BLOCKFLAG_FIRST) { prog_pagecounter = prog_pagesize; } prog_nbytes = (data[7] << 8) | data[6]; prog_state = PROG_STATE_WRITEFLASH; len = 0xff; /* multiple out */ } else if (data[1] == USBASP_FUNC_WRITEEEPROM) { if (!prog_address_newmode) prog_address = (data[3] << 8) | data[2]; prog_pagesize = 0; prog_blockflags = 0; prog_nbytes = (data[7] << 8) | data[6]; prog_state = PROG_STATE_WRITEEEPROM; len = 0xff; /* multiple out */ } else if (data[1] == USBASP_FUNC_SETLONGADDRESS) { /* set new mode of address delivering (ignore address delivered in commands) */ prog_address_newmode = 1; /* set new address */ prog_address = *((unsigned long*) &data[2]); } else if (data[1] == USBASP_FUNC_SETISPSCK) { /* set sck option */ prog_sck = data[2]; replyBuffer[0] = 0; len = 1; #if 0 } else if (data[1] == USBASP_FUNC_TPI_CONNECT) { tpi_dly_cnt = data[2] | (data[3] << 8); /* RST high */ ISP_OUT |= (1 << ISP_RST); ISP_DDR |= (1 << ISP_RST); clockWait(3); /* RST low */ ISP_OUT &= ~(1 << ISP_RST); ledRedOn(); clockWait(16); tpi_init(); } else if (data[1] == USBASP_FUNC_TPI_DISCONNECT) { tpi_send_byte(TPI_OP_SSTCS(TPISR)); tpi_send_byte(0); clockWait(10); /* pulse RST */ ISP_OUT |= (1 << ISP_RST); clockWait(5); ISP_OUT &= ~(1 << ISP_RST); clockWait(5); /* set all ISP pins inputs */ ISP_DDR &= ~((1 << ISP_RST) | (1 << ISP_SCK) | (1 << ISP_MOSI)); /* switch pullups off */ ISP_OUT &= ~((1 << ISP_RST) | (1 << ISP_SCK) | (1 << ISP_MOSI)); ledRedOff(); } else if (data[1] == USBASP_FUNC_TPI_RAWREAD) { replyBuffer[0] = tpi_recv_byte(); len = 1; } else if (data[1] == USBASP_FUNC_TPI_RAWWRITE) { tpi_send_byte(data[2]); } else if (data[1] == USBASP_FUNC_TPI_READBLOCK) { prog_address = (data[3] << 8) | data[2]; prog_nbytes = (data[7] << 8) | data[6]; prog_state = PROG_STATE_TPI_READ; len = 0xff; /* multiple in */ } else if (data[1] == USBASP_FUNC_TPI_WRITEBLOCK) { prog_address = (data[3] << 8) | data[2]; prog_nbytes = (data[7] << 8) | data[6]; prog_state = PROG_STATE_TPI_WRITE; len = 0xff; /* multiple out */ #endif } else if (data[1] == USBASP_FUNC_GETCAPABILITIES) { replyBuffer[0] = USBASP_CAP_0_TPI; replyBuffer[1] = 0; replyBuffer[2] = 0; replyBuffer[3] = 0; len = 4; } usbMsgPtr = replyBuffer; #ifdef UART_DEBUG TransmitString("\r\n"); #endif return len; }
uchar usbFunctionSetup(uchar data[8]) { uchar len = 0; if(data[1] == USBASP_FUNC_CONNECT) { /* set SCK speed */ if ((PINC & (1 << PC2)) == 0) { ispSetSCKOption(ISP_SCK_SLOW); } else { ispSetSCKOption(ISP_SCK_FAST); } /* set compatibility mode of address delivering */ prog_address_newmode = 0; ispConnect(); ledRedOn(); } else if (data[1] == USBASP_FUNC_DISCONNECT) { ispDisconnect(); ledRedOff(); } else if (data[1] == USBASP_FUNC_TRANSMIT) { replyBuffer[0] = ispTransmit(data[2]); replyBuffer[1] = ispTransmit(data[3]); replyBuffer[2] = ispTransmit(data[4]); replyBuffer[3] = ispTransmit(data[5]); len = 4; } else if (data[1] == USBASP_FUNC_READFLASH) { if (!prog_address_newmode) prog_address = (data[3] << 8) | data[2]; prog_nbytes = (data[7] << 8) | data[6]; prog_state = PROG_STATE_READFLASH; len = 0xff; /* multiple in */ } else if (data[1] == USBASP_FUNC_READEEPROM) { if (!prog_address_newmode) prog_address = (data[3] << 8) | data[2]; prog_nbytes = (data[7] << 8) | data[6]; prog_state = PROG_STATE_READEEPROM; len = 0xff; /* multiple in */ } else if (data[1] == USBASP_FUNC_ENABLEPROG) { replyBuffer[0] = ispEnterProgrammingMode();; len = 1; } else if (data[1] == USBASP_FUNC_WRITEFLASH) { if (!prog_address_newmode) prog_address = (data[3] << 8) | data[2]; prog_pagesize = data[4]; prog_blockflags = data[5] & 0x0F; prog_pagesize += (((unsigned int)data[5] & 0xF0)<<4); if (prog_blockflags & PROG_BLOCKFLAG_FIRST) { prog_pagecounter = prog_pagesize; } prog_nbytes = (data[7] << 8) | data[6]; prog_state = PROG_STATE_WRITEFLASH; len = 0xff; /* multiple out */ } else if (data[1] == USBASP_FUNC_WRITEEEPROM) { if (!prog_address_newmode) prog_address = (data[3] << 8) | data[2]; prog_pagesize = 0; prog_blockflags = 0; prog_nbytes = (data[7] << 8) | data[6]; prog_state = PROG_STATE_WRITEEEPROM; len = 0xff; /* multiple out */ } else if(data[1] == USBASP_FUNC_SETLONGADDRESS) { /* set new mode of address delivering (ignore address delivered in commands) */ prog_address_newmode = 1; /* set new address */ prog_address = *((unsigned long*)&data[2]); } else if (data[1] == BYRON_START_BOOTLOADER) { cli(); USB_INTR_ENABLE = 0; USB_INTR_CFG = 0; /* also reset config bits */ GICR = (1 << IVCE); /* enable change of interrupt vectors */ GICR = (1 << IVSEL); /* move interrupts to boot flash section */ bootStart = 0x55AA1177; // Bootloader start from application for(;;); // Reset from watchdog } usbMsgPtr = replyBuffer; return len; }