void enter_ISCP_PIC24K() { int i; enablePGC_D(); //PGC/D output & PGC/D_LOW appropriate VPP_RUNoff(); //MCLR low VDDon(); DelayMs( 10 ); VPP_RUNon(); //VPP to 4.5V for( i = 0; i < 300; i++ ) continue; //aprox 0.5ms //clock_delay(); //P19 = 40ns min //write 0x4D43, high to low, other than the rest of the commands which are low to high... //0x3D43 => 0100 1101 0100 0011 //from low to high => 1100 0010 1011 0010 //0xC2B2 pic_send_word( 0xC2B2 ); //write 0x4851 => 0100 1000 0101 0001 => 1000 1010 0001 0010 => 0x0A12 pic_send_word( 0x8A12 ); DelayMs( 1 ); DelayMs( 25 ); pic_send_n_bits( 5, 0 ); dspic_send_24_bits( 0 ); //send a nop instruction with 5 additional databits dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0x040200 ); //GOTO 0x200 dspic_send_24_bits( 0x000000 ); //NOP }
void enter_ISCP_dsPIC30() { unsigned int i; enablePGC_D(); //PGC/D output & PGC/D_LOW appropriate PGDlow(); // initial value for programming mode PGClow(); // initial value for programming mode clock_delay(); // dummy tempo DelayMs( 100 ); VDDon(); clock_delay(); VPPon(); DelayMs( 26 ); dspic_send_24_bits( 0 ); dspic_send_24_bits( 0 ); dspic_send_24_bits( 0 ); dspic_send_24_bits( 0 ); VPPoff(); VPP_RSTon(); VPP_RSToff(); for( i = 0; i < 1; i++ ) continue; VPPon(); DelayMs( 100 ); }
void enter_ISCP_PIC24() { enablePGC_D(); //PGC/D output & PGC/D_LOW appropriate VPP_RUNoff(); //MCLR low VDDon(); DelayMs( 10 ); VPP_RUNon(); //VPP to 4.5V DelayMs( 1 ); // PIC24E 500us min VPP_RUNoff(); //and immediately back to 0... VPP_RSTon(); DelayMs( 2 ); //P19 = 40ns min PIC24E 1ms min //write 0x4D43, high to low, other than the rest of the commands which are low to high... //0x4D43 => 0100 1101 0100 0011 //from low to high => 1100 0010 1011 0010 //0xC2B2 pic_send_word( 0xC2B2 ); //write 0x4851 => 0100 1000 0101 0001 => 1000 1010 0001 0010 => 0x8A12 pic_send_word( 0x8A12 ); DelayMs( 1 ); VPP_RSToff(); //release from reset VPP_RUNon(); DelayMs( 60 ); //PIC24E 50ms min pic_send_n_bits( 5, 0 ); // dspic_send_24_bits( 0x000000 ); //NOP //PIC24E (doc 70633) says 3 nops // dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0x040200 ); //GOTO 0x200 dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0x000000 ); //NOP }
void write_data_dsPIC30( unsigned long address, unsigned char* data, char blocksize, char lastblock ) { char blockcounter, i; //Step 1: Exit the Reset vector. dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0x040100 ); //GOTO 0x100 dspic_send_24_bits( 0x040100 ); //GOTO 0x100 dspic_send_24_bits( 0x000000 ); //NOP //Step 2: Set the NVMCON to write 16 data words. dspic_send_24_bits( 0x24005A ); //MOV #0x4005, W10 dspic_send_24_bits( 0x883B0A ); //MOV W10, NVMCON for( blockcounter = 0; blockcounter < blocksize; blockcounter += 8 ) { //Step 3: Initialize the write pointer (W7) for TBLWT instruction. //dspic_send_24_bits(0x200000|((blockcounter+address&0xFF0000)>>12)); //MOV #0x7F, W0 dspic_send_24_bits( 0x2007F0 );//|((blockcounter+address&0xFF0000)>>12)); //MOV #0x7F, W0 dspic_send_24_bits( 0x880190 ); //MOV W0, TBLPAG dspic_send_24_bits( 0x2F0007 | ((((unsigned long) blockcounter + address) & 0x0FFF) << 4) ); //MOV #<DestinationAddress15:0>, W7 //Step 4: Load W0:W3 with the next 4 data words to program. dspic_send_24_bits( 0x200000 | (((unsigned long) data[blockcounter]) << 4) | (((unsigned long) data[blockcounter + 1]) << 12) ); dspic_send_24_bits( 0x200001 | (((unsigned long) data[blockcounter + 2]) << 4) | (((unsigned long) data[blockcounter + 3]) << 12) ); dspic_send_24_bits( 0x200002 | (((unsigned long) data[blockcounter + 4]) << 4) | (((unsigned long) data[blockcounter + 5]) << 12) ); dspic_send_24_bits( 0x200003 | (((unsigned long) data[blockcounter + 6]) << 4) | (((unsigned long) data[blockcounter + 7]) << 12) ); /*for(i=0;i<4;i++) { dspic_send_24_bits(0x200000| (((unsigned long)data[blockcounter+(i*2)])<<4)| (((unsigned long)data[blockcounter+(i*2)+1])<<12)| ((unsigned long) i)); }*/ //Step 5: Set the read pointer (W6) and load the (next set of) write latches. dspic_send_24_bits( 0xEB0300 ); //CLR W6 dspic_send_24_bits( 0x000000 ); //NOP for( i = 0; i < 4; i++ ) { dspic_send_24_bits( 0xBB1BB6 ); //TBLWTL [W6++], [W7++] dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0x000000 ); //NOP } }//Step 6: Repeat steps 3-4 four times to load the write latches for 16 data words. //Step 7: Unlock the NVMCON for writing. dspic_send_24_bits( 0x200558 ); //MOV #0x55, W8 dspic_send_24_bits( 0x883B38 ); //MOV W8, NVMKEY dspic_send_24_bits( 0x200AA9 ); //MOV #0xAA, W9 dspic_send_24_bits( 0x883B39 ); //MOV W9, NVMKEY //Step 8: Initiate the write cycle. dspic_send_24_bits( 0xA8E761 ); //BSET NVMCON, #WR dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0x000000 ); //NOP DelayMs( 2 ); //Externally time 2 msec dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0xA9E761 ); //BCLR NVMCON, #WR dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0x000000 ); //NOP //Step 9: Reset device internal PC. dspic_send_24_bits( 0x040100 ); //GOTO 0x100 dspic_send_24_bits( 0x000000 ); //NOP }
void ProcessIO(void) { char oldPGDtris; char PIN; static byte counter=0; int nBytes; unsigned long address; unsigned char i; input_buffer[0]=UART1RX(); //USBGenRead((byte*)input_buffer,64); // if(nBytes>0) // { switch(input_buffer[0]) { case CMD_ERASE: setLeds(LEDS_ON | LEDS_WR); getBytes(1,1);//get more data, #bytes, where to insert in input buffer array output_buffer[0]=bulk_erase(picfamily,pictype,input_buffer[1]); counter=1; setLeds(LEDS_ON); break; case CMD_READ_ID: setLeds(LEDS_ON | LEDS_RD); switch(picfamily) { case DSPIC30: read_code(picfamily,pictype,0xFF0000,(unsigned char*)output_buffer,2,3); break; case PIC18: read_code(picfamily,pictype,0x3FFFFE,(unsigned char*)output_buffer,2,3); //devid is at location 0x3ffffe for PIC18 devices break; case PIC16: set_vdd_vpp(picfamily, pictype, 0); read_code(picfamily,pictype,0x2006,(unsigned char*)output_buffer,2,3); //devid is at location 0x2006 for PIC16 devices break; } counter=2; setLeds(LEDS_ON); break; case CMD_WRITE_CODE: setLeds(LEDS_ON | LEDS_WR); address=((unsigned long)input_buffer[2])<<16| ((unsigned long)input_buffer[3])<<8| ((unsigned long)input_buffer[4]); output_buffer[0]=write_code(picfamily,pictype,address, (unsigned char*)(input_buffer+6),input_buffer[1],input_buffer[5]); counter=1; setLeds(LEDS_ON); break; case CMD_READ_CODE: setLeds(LEDS_ON | LEDS_RD); address=((unsigned long)input_buffer[2])<<16| ((unsigned long)input_buffer[3])<<8| ((unsigned long)input_buffer[4]); read_code(picfamily,pictype,address,(unsigned char*)output_buffer,input_buffer[1],input_buffer[5]); counter=input_buffer[1]; setLeds(LEDS_ON); break; case CMD_WRITE_DATA: setLeds(LEDS_ON | LEDS_WR); address=((unsigned long)input_buffer[2])<<16| ((unsigned long)input_buffer[3])<<8| ((unsigned long)input_buffer[4]); output_buffer[0]=write_data(picfamily,pictype,address, (unsigned char*)(input_buffer+6),input_buffer[1],input_buffer[5]); counter=1; setLeds(LEDS_ON); break; case CMD_READ_DATA: setLeds(LEDS_ON | LEDS_RD); address=((unsigned long)input_buffer[2])<<16| ((unsigned long)input_buffer[3])<<8| ((unsigned long)input_buffer[4]); read_data(picfamily,pictype,address,(unsigned char*)output_buffer,input_buffer[1],input_buffer[5]); counter=input_buffer[1]; setLeds(LEDS_ON); break; case CMD_WRITE_CONFIG: setLeds(LEDS_ON | LEDS_WR); address=((unsigned long)input_buffer[2])<<16| ((unsigned long)input_buffer[3])<<8| ((unsigned long)input_buffer[4]); output_buffer[0]=write_config_bits(picfamily, pictype, address, (unsigned char*)(input_buffer+6),input_buffer[1],input_buffer[5]); counter=1; setLeds(LEDS_ON); break; case CMD_SET_PICTYPE: output_buffer[0]=set_pictype(input_buffer+1); //output_buffer[0]=1; //Ok counter=1; setLeds(LEDS_ON); break; case CMD_FIRMWARE_VERSION: for(counter=0; counter<18; counter++)output_buffer[counter]=upp_version[counter]; counter=18; setLeds(LEDS_ON); break; case CMD_DEBUG: setLeds(LEDS_ON | LEDS_WR | LEDS_RD); switch(input_buffer[1]) { case 0: set_vdd_vpp(dsP30F, DSPIC30, 1); output_buffer[0]=1; counter=1; break; case 1: set_vdd_vpp(dsP30F, DSPIC30, 0); output_buffer[0]=1; counter=1; break; case 2: dspic_send_24_bits(((unsigned long)input_buffer[2])| ((unsigned long)input_buffer[3])<<8| ((unsigned long)input_buffer[4])<<16); output_buffer[0]=1; counter=1; break; case 3: nBytes = dspic_read_16_bits(); output_buffer[0]=(unsigned char)nBytes; output_buffer[1]=(unsigned char)(nBytes>>8); counter=2; break; } break; case CMD_GET_PIN_STATUS: switch(input_buffer[1]) { case SUBCMD_PIN_PGC: if((!TRISPGC_LOW)&&(!PGC_LOW)) //3.3V levels { if(PGC) output_buffer[0] = PIN_STATE_3_3V; else output_buffer[0] = PIN_STATE_0V; } else //5V levels { if(PGC) output_buffer[0] = PIN_STATE_5V; else output_buffer[0] = PIN_STATE_0V; } counter=1; break; case SUBCMD_PIN_PGD: if(TRISPGD)//PGD is input { if(PGD_READ) output_buffer[0] = PIN_STATE_5V; else output_buffer[0] = PIN_STATE_0V; } else { if((!TRISPGD_LOW)&&(!PGD_LOW)) //3.3V levels { if(PGD) output_buffer[0] = PIN_STATE_3_3V; else output_buffer[0] = PIN_STATE_0V; } else //5V levels { if(PGD) output_buffer[0] = PIN_STATE_5V; else output_buffer[0] = PIN_STATE_0V; } } counter=1; break; case SUBCMD_PIN_VDD: //if(VDD) output_buffer[0] = PIN_STATE_FLOAT; //else output_buffer[0] = PIN_STATE_5V; output_buffer[0] = PIN_STATE_5V; counter = 1; break; case SUBCMD_PIN_VPP: counter=1; if(!VPP){output_buffer[0] = PIN_STATE_12V;break;} if(VPP_RST){output_buffer[0] = PIN_STATE_0V;break;} if(VPP_RUN){output_buffer[0] = PIN_STATE_5V;break;} output_buffer[0] = PIN_STATE_FLOAT; break; case SUBCMD_PIN_VPP_VOLTAGE: ReadAdc(output_buffer); counter=2; break; default: output_buffer[0]=3; counter=1; break; } break; case CMD_SET_PIN_STATUS: switch(input_buffer[1]) { case SUBCMD_PIN_PGC: switch(input_buffer[2]) { case PIN_STATE_0V: TRISPGC = 0; PGC = 0; TRISPGC_LOW = 1; PGC_LOW = 0; output_buffer[0]=1;//ok break; case PIN_STATE_3_3V: TRISPGC = 0; PGC = 1; TRISPGC_LOW = 0; PGC_LOW = 0; output_buffer[0]=1;//ok break; case PIN_STATE_5V: TRISPGC = 0; PGC = 1; TRISPGC_LOW = 1; PGC_LOW = 0; output_buffer[0]=1;//ok break; default: output_buffer[0]=3; break; } break; case SUBCMD_PIN_PGD: switch(input_buffer[2]) { case PIN_STATE_0V: TRISPGD = 0; PGD = 0; TRISPGD_LOW = 1; PGD_LOW = 0; output_buffer[0]=1;//ok break; case PIN_STATE_3_3V: TRISPGD = 0; PGD = 1; TRISPGD_LOW = 0; PGD_LOW = 0; output_buffer[0]=1;//ok break; case PIN_STATE_5V: TRISPGD = 0; PGD = 1; TRISPGD_LOW = 1; PGD_LOW = 0; output_buffer[0]=1;//ok break; case PIN_STATE_INPUT: TRISPGD_LOW = 1; TRISPGD = 1; output_buffer[0]=1;//ok break; default: output_buffer[0]=3; break; } break; case SUBCMD_PIN_VDD: switch(input_buffer[2]) { case PIN_STATE_5V: //VDD = 0; output_buffer[0]=1; break; case PIN_STATE_FLOAT: //VDD = 1; output_buffer[0]=1; break; default: output_buffer[0]=3; break; } break; case SUBCMD_PIN_VPP: switch(input_buffer[2]) { case PIN_STATE_0V: VPP = 1; VPP_RST = 1; VPP_RUN = 0; output_buffer[0]=1;//ok break; case PIN_STATE_5V: VPP = 1; VPP_RST = 0; VPP_RUN = 1; output_buffer[0]=1;//ok break; case PIN_STATE_12V: VPP = 0; VPP_RST = 0; VPP_RUN = 0; output_buffer[0]=1;//ok break; case PIN_STATE_FLOAT: VPP = 1; VPP_RST = 0; VPP_RUN = 0; output_buffer[0]=1;//ok break; default: output_buffer[0]=3; break; } break; default: output_buffer[0]=3; } counter=1; break; } //} //if nBytes>0 if(counter != 0) { //if(!mUSBGenTxIsBusy()) //USBGenWrite((byte*)&output_buffer,counter); for(i=0; i<counter; i++) UART1TX(output_buffer[i]); counter=0; } }//end ProcessIO
void ProcessIO(void) { char oldPGDtris; char PIN; static byte counter=0; int nBytes; unsigned long address; // When the device is plugged in, the leds give the numbers 1, 2, 3, 4, 5. //After configured state, the leds are controlled by the next lines in this function if((usb_device_state < CONFIGURED_STATE)||(UCONbits.SUSPND==1)) { BlinkUSBStatus(); return; } nBytes=USBGenRead((byte*)input_buffer,64); if(nBytes>0) { switch(input_buffer[0]) { case CMD_ERASE: setLeds(LEDS_ON | LEDS_WR); output_buffer[0]=bulk_erase(picfamily,pictype,input_buffer[1]); counter=1; setLeds(LEDS_ON); break; case CMD_READ_ID: setLeds(LEDS_ON | LEDS_RD); switch(picfamily) { case PIC24: case dsPIC30: read_code(picfamily,pictype,0xFF0000,(unsigned char*)output_buffer,2,3); break; case PIC18: case PIC18J: case PIC18K: read_code(picfamily,pictype,0x3FFFFE,(unsigned char*)output_buffer,2,3); //devid is at location 0x3ffffe for PIC18 devices break; case PIC16: set_vdd_vpp(picfamily, pictype, 0); read_code(picfamily,pictype,0x2006,(unsigned char*)output_buffer,2,3); //devid is at location 0x2006 for PIC16 devices break; } counter=2; setLeds(LEDS_ON); break; case CMD_WRITE_CODE: setLeds(LEDS_ON | LEDS_WR); address=((unsigned long)input_buffer[2])<<16| ((unsigned long)input_buffer[3])<<8| ((unsigned long)input_buffer[4]); output_buffer[0]=write_code(picfamily,pictype,address, (unsigned char*)(input_buffer+6),input_buffer[1],input_buffer[5]); counter=1; setLeds(LEDS_ON); break; case CMD_READ_CODE: setLeds(LEDS_ON | LEDS_RD); address=((unsigned long)input_buffer[2])<<16| ((unsigned long)input_buffer[3])<<8| ((unsigned long)input_buffer[4]); PIN=read_code(picfamily,pictype,address,(unsigned char*)output_buffer,input_buffer[1],input_buffer[5]); if(PIN==3)output_buffer[0]=0x3; counter=input_buffer[1]; setLeds(LEDS_ON); break; case CMD_WRITE_DATA: setLeds(LEDS_ON | LEDS_WR); address=((unsigned long)input_buffer[2])<<16| ((unsigned long)input_buffer[3])<<8| ((unsigned long)input_buffer[4]); output_buffer[0]=write_data(picfamily,pictype,address, (unsigned char*)(input_buffer+6),input_buffer[1],input_buffer[5]); counter=1; setLeds(LEDS_ON); break; case CMD_READ_DATA: setLeds(LEDS_ON | LEDS_RD); address=((unsigned long)input_buffer[2])<<16| ((unsigned long)input_buffer[3])<<8| ((unsigned long)input_buffer[4]); read_data(picfamily,pictype,address,(unsigned char*)output_buffer,input_buffer[1],input_buffer[5]); counter=input_buffer[1]; setLeds(LEDS_ON); break; case CMD_WRITE_CONFIG: setLeds(LEDS_ON | LEDS_WR); address=((unsigned long)input_buffer[2])<<16| ((unsigned long)input_buffer[3])<<8| ((unsigned long)input_buffer[4]); output_buffer[0]=write_config_bits(picfamily, pictype, address, (unsigned char*)(input_buffer+6),input_buffer[1],input_buffer[5]); counter=1; setLeds(LEDS_ON); break; case CMD_SET_PICTYPE: output_buffer[0]=set_pictype(input_buffer+1); //output_buffer[0]=1; //Ok counter=1; setLeds(LEDS_ON); break; case CMD_FIRMWARE_VERSION: strcpypgm2ram((char*)output_buffer,(const far rom char*)upp_version); counter=18; setLeds(LEDS_ON); break; case CMD_DEBUG: setLeds(LEDS_ON | LEDS_WR | LEDS_RD); switch(input_buffer[1]) { case 0: set_vdd_vpp(dsP30F, dsPIC30, 1); output_buffer[0]=1; counter=1; break; case 1: set_vdd_vpp(dsP30F, dsPIC30, 0); output_buffer[0]=1; counter=1; break; case 2: dspic_send_24_bits(((unsigned long)input_buffer[2])| ((unsigned long)input_buffer[3])<<8| ((unsigned long)input_buffer[4])<<16); output_buffer[0]=1; counter=1; break; case 3: nBytes = dspic_read_16_bits(1); output_buffer[0]=(unsigned char)nBytes; output_buffer[1]=(unsigned char)(nBytes>>8); counter=2; break; } break; case CMD_GET_PIN_STATUS: switch(input_buffer[1]) { case SUBCMD_PIN_PGC: if((!TRISPGC_LOW)&&(!PGC_LOW)) //3.3V levels { if(PGC) output_buffer[0] = PIN_STATE_3_3V; else output_buffer[0] = PIN_STATE_0V; } else //5V levels { if(PGC) output_buffer[0] = PIN_STATE_5V; else output_buffer[0] = PIN_STATE_0V; } counter=1; break; case SUBCMD_PIN_PGD: if(TRISPGD)//PGD is input { if(PGD_READ) output_buffer[0] = PIN_STATE_5V; else output_buffer[0] = PIN_STATE_0V; } else { if((!TRISPGD_LOW)&&(!PGD_LOW)) //3.3V levels { if(PGD) output_buffer[0] = PIN_STATE_3_3V; else output_buffer[0] = PIN_STATE_0V; } else //5V levels { if(PGD) output_buffer[0] = PIN_STATE_5V; else output_buffer[0] = PIN_STATE_0V; } } counter=1; break; case SUBCMD_PIN_VDD: if(VDD) output_buffer[0] = PIN_STATE_FLOAT; else output_buffer[0] = PIN_STATE_5V; counter = 1; break; case SUBCMD_PIN_VPP: counter=1; if(!VPP){output_buffer[0] = PIN_STATE_12V;break;} if(VPP_RST){output_buffer[0] = PIN_STATE_0V;break;} if(VPP_RUN){output_buffer[0] = PIN_STATE_5V;break;} output_buffer[0] = PIN_STATE_FLOAT; break; case SUBCMD_PIN_VPP_VOLTAGE: ReadAdc(output_buffer); counter=2; break; default: output_buffer[0]=3; counter=1; break; } break; case CMD_SET_PIN_STATUS: switch(input_buffer[1]) { case SUBCMD_PIN_PGC: switch(input_buffer[2]) { case PIN_STATE_0V: TRISPGC = 0; PGC = 0; TRISPGC_LOW = 1; PGC_LOW = 0; output_buffer[0]=1;//ok break; case PIN_STATE_3_3V: TRISPGC = 0; PGC = 1; TRISPGC_LOW = 0; PGC_LOW = 0; output_buffer[0]=1;//ok break; case PIN_STATE_5V: TRISPGC = 0; PGC = 1; TRISPGC_LOW = 1; PGC_LOW = 0; output_buffer[0]=1;//ok break; default: output_buffer[0]=3; break; } break; case SUBCMD_PIN_PGD: switch(input_buffer[2]) { case PIN_STATE_0V: TRISPGD = 0; PGD = 0; TRISPGD_LOW = 1; PGD_LOW = 0; output_buffer[0]=1;//ok break; case PIN_STATE_3_3V: TRISPGD = 0; PGD = 1; TRISPGD_LOW = 0; PGD_LOW = 0; output_buffer[0]=1;//ok break; case PIN_STATE_5V: TRISPGD = 0; PGD = 1; TRISPGD_LOW = 1; PGD_LOW = 0; output_buffer[0]=1;//ok break; case PIN_STATE_INPUT: TRISPGD_LOW = 1; TRISPGD = 1; output_buffer[0]=1;//ok break; default: output_buffer[0]=3; break; } break; case SUBCMD_PIN_VDD: switch(input_buffer[2]) { case PIN_STATE_5V: VDD = 0; output_buffer[0]=1; break; case PIN_STATE_FLOAT: VDD = 1; output_buffer[0]=1; break; default: output_buffer[0]=3; break; } break; case SUBCMD_PIN_VPP: switch(input_buffer[2]) { case PIN_STATE_0V: VPP = 1; VPP_RST = 1; VPP_RUN = 0; output_buffer[0]=1;//ok break; case PIN_STATE_5V: VPP = 1; VPP_RST = 0; VPP_RUN = 1; output_buffer[0]=1;//ok break; case PIN_STATE_12V: VPP = 0; VPP_RST = 0; VPP_RUN = 0; output_buffer[0]=1;//ok break; case PIN_STATE_FLOAT: VPP = 1; VPP_RST = 0; VPP_RUN = 0; output_buffer[0]=1;//ok break; default: output_buffer[0]=3; break; } break; default: output_buffer[0]=3; } counter=1; break; } } if(counter != 0) { if(!mUSBGenTxIsBusy()) USBGenWrite((byte*)&output_buffer,counter); counter=0; } }//end ProcessIO
void write_code_dsP30F( unsigned long address, unsigned char* data, char blocksize, char lastblock ) { unsigned int i; char blockcounter; dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0x000000 ); //NOP //Step 1: Exit the Reset vector. dspic_send_24_bits( 0x040100 ); //GOTO 0x100 dspic_send_24_bits( 0x040100 ); //GOTO 0x100 dspic_send_24_bits( 0x000000 ); //NOP //Step 2: Set the NVMCON to program 32 instruction words. dspic_send_24_bits( 0x24001A ); //MOV #0x4001, W10 dspic_send_24_bits( 0x883B0A ); //MOV W10, NVMCON for( blockcounter = 0; blockcounter < blocksize; blockcounter += 12 ) { //Step 3: Initialize the write pointer (W7) for TBLWT instruction. dspic_send_24_bits( 0x200000 | (((((blockcounter + address) * 2) / 3) & 0xFF0000) >> 12) ); //MOV #<DestinationAddress23:16>, W0 dspic_send_24_bits( 0x880190 ); //MOV W0, TBLPAG dspic_send_24_bits( 0x200007 | (((((blockcounter + address) * 2) / 3) & 0x00FFFF) << 4) ); //MOV #<DestinationAddress15:0>, W7 //Step 4: Initialize the read pointer (W6) and load W0:W5 with the next 4 instruction words to program. for( i = 0; i < 6; i++ ) { dspic_send_24_bits( 0x200000 | (((unsigned long) data[blockcounter + (i * 2)]) << 4) | (((unsigned long) data[blockcounter + (i * 2) + 1]) << 12) | ((unsigned long) i) ); /** MOV #<LSW0>, W0 MOV #<MSB1:MSB0>, W1 MOV #<LSW1>, W2 MOV #<LSW2>, W3 MOV #<MSB3:MSB2>, W4 MOV #<LSW3>, W5 */ } //Step 5: Set the read pointer (W6) and load the (next set of) write latches. dspic_send_24_bits( 0xEB0300 ); //CLR W6 dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0xBB0BB6 ); //TBLWTL [W6++], [W7] dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0xBBDBB6 ); //TBLWTH.B [W6++], [W7++] dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0xBBEBB6 ); //TBLWTH.B [W6++], [++W7] dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0xBB1BB6 ); //TBLWTL [W6++], [W7++] dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0xBB0BB6 ); //TBLWTL [W6++], [W7] dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0xBBDBB6 ); //TBLWTH.B [W6++], [W7++] dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0xBBEBB6 ); //TBLWTH.B [W6++], [++W7] dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0xBB1BB6 ); //TBLWTL [W6++], [W7++] dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0x000000 ); //NOP }//Step 6: Repeat steps 3-5 eight times to load the write latches for 32 instructions. //if((address%96)==64) //{ //Step 7: Unlock the NVMCON for writing. dspic_send_24_bits( 0x200558 ); //MOV #0x55, W8 dspic_send_24_bits( 0x883B38 ); //MOV W8, NVMKEY dspic_send_24_bits( 0x200AA9 ); //MOV #0xAA, W9 dspic_send_24_bits( 0x883B39 ); //MOV W9, NVMKEY //Step 8: Initiate the write cycle. dspic_send_24_bits( 0xA8E761 ); //BSET NVMCON, #WR dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0x000000 ); //NOP DelayMs( 3 ); //Externally time 2 msec dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0xA9E761 ); //BCLR NVMCON, #WR dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0x000000 ); //NOP //Step 9: Reset device internal PC. dspic_send_24_bits( 0x040100 ); //GOTO 0x100 dspic_send_24_bits( 0x000000 ); //NOP //} }
void write_code_P24FXXKAXXX( unsigned long address, unsigned char* data, char blocksize, char lastblock ) { unsigned int i, payload; char blockcounter; dspic_send_24_bits( 0x000000 ); //NOP //Step 1: Exit the Reset vector. dspic_send_24_bits( 0x040200 ); //GOTO 0x200 dspic_send_24_bits( 0x000000 ); //NOP //Step 2: Set the NVMCON to program 32 instruction words. dspic_send_24_bits( 0x24004A ); //MOV #0x4004, W10 dspic_send_24_bits( 0x883B0A ); //MOV W10, NVMCON for( blockcounter = 0; blockcounter < blocksize; blockcounter += 12 ) { //Step 3: Initialize the write pointer (W7) for TBLWT instruction. dspic_send_24_bits( 0x200000 | (((((blockcounter + address) * 2) / 3) & 0xFF0000) >> 12) ); //MOV #<DestinationAddress23:16>, W0 dspic_send_24_bits( 0x880190 ); //MOV W0, TBLPAG dspic_send_24_bits( 0x200007 | (((((blockcounter + address) * 2) / 3) & 0x00FFFF) << 4) ); //MOV #<DestinationAddress15:0>, W7 //Step 4: Initialize the read pointer (W6) and load W0:W5 with the next 4 instruction words to program. for( i = 0; i < 6; i++ ) { dspic_send_24_bits( 0x200000 | (((unsigned long) data[blockcounter + (i * 2)]) << 4) | (((unsigned long) data[blockcounter + (i * 2) + 1]) << 12) | ((unsigned long) i) ); /** MOV #<LSW0>, W0 MOV #<MSB1:MSB0>, W1 MOV #<LSW1>, W2 MOV #<LSW2>, W3 MOV #<MSB3:MSB2>, W4 MOV #<LSW3>, W5 */ } //Step 5: Set the read pointer (W6) and load the (next set of) write latches. dspic_send_24_bits( 0xEB0300 ); //CLR W6 dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0xBB0BB6 ); //TBLWTL [W6++], [W7] dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0xBBDBB6 ); //TBLWTH.B [W6++], [W7++] dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0xBBEBB6 ); //TBLWTH.B [W6++], [++W7] dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0xBB1BB6 ); //TBLWTL [W6++], [W7++] dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0xBB0BB6 ); //TBLWTL [W6++], [W7] dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0xBBDBB6 ); //TBLWTH.B [W6++], [W7++] dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0xBBEBB6 ); //TBLWTH.B [W6++], [++W7] dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0xBB1BB6 ); //TBLWTL [W6++], [W7++] dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0x000000 ); //NOP }//Step 6: Repeat steps 3-5 eight times to load the write latches for 32 instructions. //if((address%96)==64) //{ //Step 7: Unlock the NVMCON for writing. dspic_send_24_bits( 0xA8E761 ); //BSET NVMCON, #WR dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0x000000 ); //NOP //Step 8: CHECK bit 15 of NVMCON for( i = 0; i < 20; i++ ) { //step 5 dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0x040200 ); //GOTO 0x200 dspic_send_24_bits( 0x000000 ); //NOP dspic_send_24_bits( 0x803B02 ); //MOV NVMCON, W2 dspic_send_24_bits( 0x883C22 ); //MOV W2, VISI dspic_send_24_bits( 0x000000 ); //NOP payload = dspic_read_16_bits( is3_3V() ); dspic_send_24_bits( 0x000000 ); //NOP if( (payload && 0x8000) == 0 ) break; //programming completed DelayMs( 1 ); }//step 8: repeat step 5-7 //Step 9: Reset device internal PC. dspic_send_24_bits( 0x040200 ); //GOTO 0x200 dspic_send_24_bits( 0x000000 ); //NOP }