// Task dispatcher void main(void) { // Initialize Global States // Sleep = FALSE; // Disable sleep mode Rwuen = FALSE; // Disable remote wakeup Selfpwr = FALSE; // Disable self powered GotSUD = FALSE; // Clear "Got setup data" flag // Initialize user device TD_Init(); EA=0; pDeviceDscr = (WORD)&DeviceDscr; pDeviceQualDscr = (WORD)&DeviceQualDscr; pHighSpeedConfigDscr = (WORD)&HighSpeedConfigDscr; pFullSpeedConfigDscr = (WORD)&FullSpeedConfigDscr; pStringDscr = (WORD)&StringDscr; EZUSB_IRQ_ENABLE(); // Enable USB interrupt (INT2) // EZUSB_ENABLE_RSMIRQ(); // Wake-up interrupt INTSETUP |= (bmAV2EN | bmAV4EN); // Enable INT 2 & 4 autovectoring //USBIE |= bmSUDAV | bmSUTOK | bmSUSP | bmURES | bmHSGRANT; // Enable selected interrupts USBIE |= bmSUDAV | bmSUTOK | bmURES | bmHSGRANT; // Enable selected interrupts EA = 1; // Enable 8051 interrupts #ifndef NO_RENUM // Renumerate if necessary. Do this by checking the renum bit. If it // is already set, there is no need to renumerate. The renum bit will // already be set if this firmware was loaded from an eeprom. if(!(USBCS & bmRENUM)) { EZUSB_Discon(TRUE); // renumerate } #endif // unconditionally re-connect. If we loaded from eeprom we are // disconnected and need to connect. If we just renumerated this // is not necessary but doesn't hurt anything USBCS &=~bmDISCON; CKCON = (CKCON&(~bmSTRETCH)) | FW_STRETCH_VALUE; // Set stretch // clear the Sleep flag. // Sleep = FALSE; //download the serial number from the EEPROM //EZUSB_InitI2C(); // already done in TD_init which was called above downloadSerialNumberFromEEPROM(); // Task Dispatcher while(TRUE) // Main Loop { // Poll User Device TD_Poll(); // Check for pending SETUP if(GotSUD) { SetupCommand(); // Implement setup command GotSUD = FALSE; // Clear SETUP flag } // check for and handle suspend. // NOTE: Idle mode stops the processor clock. There are only two // ways out of idle mode, the WAKEUP pin, and detection of the USB // resume state on the USB bus. The timers will stop and the // processor will not wake up on any other interrupts. if (Sleep) // the device doesn't start if this is checked { if(TD_Suspend()) { Sleep = FALSE; // Clear the "go to sleep" flag. Do it here to prevent any race condition between wakeup and the next sleep. do { EZUSB_Susp(); // Place processor in idle mode. } while(!Rwuen && EZUSB_EXTWAKEUP()); // above. Must continue to go back into suspend if the host has disabled remote wakeup // *and* the wakeup was caused by the external wakeup pin. // 8051 activity will resume here due to USB bus or Wakeup# pin activity. EZUSB_Resume(); // If source is the Wakeup# pin, signal the host to Resume. TD_Resume(); } } } }
//[YourCompany]%DeviceDesc%=CyLoad, USB\VID_04B4&PID_0084 // Task dispatcher void main(void) { // DWORD i; // WORD offset; // DWORD DevDescrLen; DWORD j=0; // WORD IntDescrAddr; // WORD ExtDescrAddr; // Initialize Global States Sleep = FALSE; // Disable sleep mode Rwuen = FALSE; // Disable remote wakeup Selfpwr = FALSE; // Disable self powered GotSUD = FALSE; // Clear "Got setup data" flag //========================================================== IFCONFIG = 0xE3; //1110 0011 SYNCDELAY; EP2CFG = 0xA2; //out 512 bytes, 2x, bulk SYNCDELAY; EP6CFG = 0xE2; // in 512 bytes, 2x, bulk SYNCDELAY; EP4CFG = 0xE2; // in 512 bytes, 2x, bulk SYNCDELAY; EP8CFG = 0x02; //clear valid bit SYNCDELAY; FIFOPINPOLAR = 0x00; SYNCDELAY; PINFLAGSAB = 0x00; // FLAGA - EP6FF SYNCDELAY; PINFLAGSCD = 0x00; // FLAGD - EP2EF SYNCDELAY; PORTACFG |= 0x80; // port A configuration reg SYNCDELAY; FIFORESET = 0x80; // activate NAK-ALL to avoid race conditions SYNCDELAY; // see TRM section 15.14 FIFORESET = 0x02; // reset, FIFO 2 SYNCDELAY; // FIFORESET = 0x04; // reset, FIFO 4 SYNCDELAY; // FIFORESET = 0x06; // reset, FIFO 6 SYNCDELAY; // FIFORESET = 0x08; // reset, FIFO 8 SYNCDELAY; // FIFORESET = 0x00; // deactivate NAK-ALL SYNCDELAY; // EP2FIFOCFG = 0x10; // AUTOOUT=1, WORDWIDE=0 SYNCDELAY; // EP4FIFOCFG = 0x0C; // AUTOIN=1, ZEROLENIN=1, WORDWIDE=0 SYNCDELAY; EP6FIFOCFG = 0x0C; // AUTOIN=1, ZEROLENIN=1, WORDWIDE=0 SYNCDELAY; EP8FIFOCFG = 0x00; // disabled SYNCDELAY; EP2AUTOINLENH = 0x02; // EZ-USB automatically commits data in 512-byte chunks SYNCDELAY; EP2AUTOINLENL = 0x00; SYNCDELAY; EP4AUTOINLENH = 0x02; // EZ-USB automatically commits data in 512-byte chunks SYNCDELAY; EP4AUTOINLENL = 0x00; SYNCDELAY; EP6AUTOINLENH = 0x02; // EZ-USB automatically commits data in 512-byte chunks SYNCDELAY; EP6AUTOINLENL = 0x00; SYNCDELAY; //============================================================================== // PORTACFG = 0x00; // // SYNCDELAY; // OEA = 0xFF; // IOA = 0x00; // OED = 0xFF; // IOD = 0x00; //========================================================== // Initialize user device // TD_Init(); // CY_IOInit(); // The following section of code is used to relocate the descriptor table. // The frameworks uses SUDPTRH and SUDPTRL to automate the SETUP requests // for descriptors. These registers only work with memory locations // in the EZ-USB internal RAM. Therefore, if the descriptors are located // in external RAM, they must be copied to in internal RAM. // The descriptor table is relocated by the frameworks ONLY if it is found // to be located in external memory. pDeviceDscr = (WORD)&DeviceDscr; pDeviceQualDscr = (WORD)&DeviceQualDscr; pHighSpeedConfigDscr = (WORD)&HighSpeedConfigDscr; pFullSpeedConfigDscr = (WORD)&FullSpeedConfigDscr; pStringDscr = (WORD)&StringDscr; pUserDscr = (WORD)&UserDscr; //pVSUserDscr = (WORD) & VSUserDscr; // Is the descriptor table in external RAM (> 16Kbytes)? If yes, // then relocate. // Note that this code only checks if the descriptors START in // external RAM. It will not work if the descriptor table spans // internal and external RAM. /* if ((WORD)&DeviceDscr & 0xC000) { // first, relocate the descriptors IntDescrAddr = INTERNAL_DSCR_ADDR; ExtDescrAddr = (WORD)&DeviceDscr; DevDescrLen = (WORD)&UserDscr - (WORD)&DeviceDscr + 2; for (i = 0; i < DevDescrLen; i++) *((BYTE xdata *)IntDescrAddr+i) = *((BYTE xdata *)ExtDescrAddr+i); // update all of the descriptor pointers pDeviceDscr = IntDescrAddr; offset = (WORD)&DeviceDscr - INTERNAL_DSCR_ADDR; pDeviceQualDscr -= offset; pConfigDscr -= offset; pOtherConfigDscr -= offset; pHighSpeedConfigDscr -= offset; pFullSpeedConfigDscr -= offset; pStringDscr -= offset; } */ EZUSB_IRQ_ENABLE(); // Enable USB interrupt (INT2) EZUSB_ENABLE_RSMIRQ(); // Wake-up interrupt INTSETUP |= (bmAV2EN | bmAV4EN); // Enable INT 2 & 4 autovectoring USBIE |= bmSUDAV | bmSUTOK | bmSUSP | bmURES | bmHSGRANT; // Enable selected interrupts EA = 1; // Enable 8051 interrupts #ifndef NO_RENUM // Renumerate if necessary. Do this by checking the renum bit. If it // is already set, there is no need to renumerate. The renum bit will // already be set if this firmware was loaded from an eeprom. if(!(USBCS & bmRENUM)) { EZUSB_Discon(TRUE); // renumerate } #endif // unconditionally re-connect. If we loaded from eeprom we are // disconnected and need to connect. If we just renumerated this // is not necessary but doesn't hurt anything USBCS &=~bmDISCON; CKCON = (CKCON&(~bmSTRETCH)) | FW_STRETCH_VALUE; // Set stretch // clear the Sleep flag. Sleep = FALSE; GotSUD = FALSE; // Clear SETUP flag // Task Dispatcher while(TRUE) // Main Loop { // Poll User Device //TD_Poll(); // Check for pending SETUP if(GotSUD) { SetupCommand(); // Implement setup command GotSUD = FALSE; // Clear SETUP flag } // check for and handle suspend. // NOTE: Idle mode stops the processor clock. There are only two // ways out of idle mode, the WAKEUP pin, and detection of the USB // resume state on the USB bus. The timers will stop and the // processor will not wake up on any other interrupts. if (Sleep) { if(TD_Suspend()) { Sleep = FALSE; // Clear the "go to sleep" flag. Do it here to prevent any race condition between wakeup and the next sleep. do { EZUSB_Susp(); // Place processor in idle mode. } while(!Rwuen && EZUSB_EXTWAKEUP()); // above. Must continue to go back into suspend if the host has disabled remote wakeup // *and* the wakeup was caused by the external wakeup pin. // 8051 activity will resume here due to USB bus or Wakeup# pin activity. EZUSB_Resume(); // If source is the Wakeup# pin, signal the host to Resume. TD_Resume(); } } } }
// Task dispatcher void main(void) { DWORD i; WORD offset; DWORD DevDescrLen; DWORD j=0; WORD IntDescrAddr; WORD ExtDescrAddr; DWORD tCount=0; // Initialize Global States Sleep = FALSE; // Disable sleep mode Rwuen = FALSE; // Disable remote wakeup Selfpwr = FALSE; // Disable self powered GotSUD = FALSE; // Clear "Got setup data" flag // Initialize user device TD_Init(); //JTAG Enable and SYNC signals for ZTEX Spartan 6 module 1.1 (FGPA+FX2LP setup) OEA|=0x02; //Declare PA.1 as output SYNCDELAY; IOA|=0x02; //output 1 on PA.1 SYNCDELAY; OEC|=0x01; //PC.0 as output (SYNC signal) SYNCDELAY; IOC|=0x00; //output 0 on PC.0...SYNC signal is LOW SYNCDELAY; OEC&=0xFD; //PC.1 as input (Clock changing signal) SYNCDELAY; // The following section of code is used to relocate the descriptor table. // Since the SUDPTRH and SUDPTRL are assigned the address of the descriptor // table, the descriptor table must be located in on-part memory. // The 4K demo tools locate all code sections in external memory. // The descriptor table is relocated by the frameworks ONLY if it is found // to be located in external memory. pDeviceDscr = (WORD)&DeviceDscr; pDeviceQualDscr = (WORD)&DeviceQualDscr; pHighSpeedConfigDscr = (WORD)&HighSpeedConfigDscr; pFullSpeedConfigDscr = (WORD)&FullSpeedConfigDscr; pStringDscr = (WORD)&StringDscr; if (EZUSB_HIGHSPEED()) { pConfigDscr = pHighSpeedConfigDscr; pOtherConfigDscr = pFullSpeedConfigDscr; } else { pConfigDscr = pFullSpeedConfigDscr; pOtherConfigDscr = pHighSpeedConfigDscr; } if ((WORD)&DeviceDscr & 0xe000) { IntDescrAddr = INTERNAL_DSCR_ADDR; ExtDescrAddr = (WORD)&DeviceDscr; DevDescrLen = (WORD)&UserDscr - (WORD)&DeviceDscr + 2; for (i = 0; i < DevDescrLen; i++) *((BYTE xdata *)IntDescrAddr+i) = 0xCD; for (i = 0; i < DevDescrLen; i++) *((BYTE xdata *)IntDescrAddr+i) = *((BYTE xdata *)ExtDescrAddr+i); pDeviceDscr = IntDescrAddr; offset = (WORD)&DeviceDscr - INTERNAL_DSCR_ADDR; pDeviceQualDscr -= offset; pConfigDscr -= offset; pOtherConfigDscr -= offset; pHighSpeedConfigDscr -= offset; pFullSpeedConfigDscr -= offset; pStringDscr -= offset; } EZUSB_IRQ_ENABLE(); // Enable USB interrupt (INT2) EZUSB_ENABLE_RSMIRQ(); // Wake-up interrupt INTSETUP |= (bmAV2EN | bmAV4EN); // Enable INT 2 & 4 autovectoring USBIE |= bmSUDAV | bmSUTOK | bmSUSP | bmURES | bmHSGRANT; // Enable selected interrupts EA = 1; // Enable 8051 interrupts #ifndef NO_RENUM // Renumerate if necessary. Do this by checking the renum bit. If it // is already set, there is no need to renumerate. The renum bit will // already be set if this firmware was loaded from an eeprom. if(!(USBCS & bmRENUM)) { EZUSB_Discon(TRUE); // renumerate } #endif // unconditionally re-connect. If we loaded from eeprom we are // disconnected and need to connect. If we just renumerated this // is not necessary but doesn't hurt anything USBCS &=~bmDISCON; CKCON = (CKCON&(~bmSTRETCH)) | FW_STRETCH_VALUE; // Set stretch to 0 (after renumeration) // clear the Sleep flag. Sleep = FALSE; // Task Dispatcher while(TRUE) // Main Loop { if(GotSUD) // Wait for SUDAV { SetupCommand(); // Implement setup command GotSUD = FALSE; // Clear SUDAV flag } // Poll User Device // NOTE: Idle mode stops the processor clock. There are only two // ways out of idle mode, the WAKEUP pin, and detection of the USB // resume state on the USB bus. The timers will stop and the // processor will not wake up on any other interrupts. if (Sleep) { if(TD_Suspend()) { Sleep = FALSE; // Clear the "go to sleep" flag. Do it here to prevent any race condition between wakeup and the next sleep. do { EZUSB_Susp(); // Place processor in idle mode. } while(!Rwuen && EZUSB_EXTWAKEUP()); // Must continue to go back into suspend if the host has disabled remote wakeup // *and* the wakeup was caused by the external wakeup pin. // 8051 activity will resume here due to USB bus or Wakeup# pin activity. EZUSB_Resume(); // If source is the Wakeup# pin, signal the host to Resume. TD_Resume(); } } TD_Poll(); } }
// Task dispatcher void main(void) { DWORD i; WORD offset; DWORD DevDescrLen; DWORD j=0; WORD IntDescrAddr; WORD ExtDescrAddr; // Initialize Global States Sleep = FALSE; // Disable sleep mode Rwuen = FALSE; // Disable remote wakeup Selfpwr = FALSE; // Disable self powered GotSUD = FALSE; // Clear "Got setup data" flag // Initialize user device TD_Init(); // The following section of code is used to relocate the descriptor table. // The frameworks uses SUDPTRH and SUDPTRL to automate the SETUP requests // for descriptors. These registers only work with memory locations // in the EZ-USB internal RAM. Therefore, if the descriptors are located // in external RAM, they must be copied to in internal RAM. // The descriptor table is relocated by the frameworks ONLY if it is found // to be located in external memory. pDeviceDscr = (WORD)&DeviceDscr; pDeviceQualDscr = (WORD)&DeviceQualDscr; pHighSpeedConfigDscr = (WORD)&HighSpeedConfigDscr; pFullSpeedConfigDscr = (WORD)&FullSpeedConfigDscr; pStringDscr = (WORD)&StringDscr; // Is the descriptor table in external RAM (> 16Kbytes)? If yes, // then relocate. // Note that this code only checks if the descriptors START in // external RAM. It will not work if the descriptor table spans // internal and external RAM. if ((WORD)&DeviceDscr & 0xC000) { // first, relocate the descriptors IntDescrAddr = INTERNAL_DSCR_ADDR; ExtDescrAddr = (WORD)&DeviceDscr; DevDescrLen = (WORD)&UserDscr - (WORD)&DeviceDscr + 2; for (i = 0; i < DevDescrLen; i++) *((BYTE xdata *)IntDescrAddr+i) = *((BYTE xdata *)ExtDescrAddr+i); // update all of the descriptor pointers pDeviceDscr = IntDescrAddr; offset = (WORD)&DeviceDscr - INTERNAL_DSCR_ADDR; pDeviceQualDscr -= offset; pConfigDscr -= offset; pOtherConfigDscr -= offset; pHighSpeedConfigDscr -= offset; pFullSpeedConfigDscr -= offset; pStringDscr -= offset; } EZUSB_IRQ_ENABLE(); // Enable USB interrupt (INT2) EZUSB_ENABLE_RSMIRQ(); // Wake-up interrupt INTSETUP |= (bmAV2EN | bmAV4EN); // Enable INT 2 & 4 autovectoring USBIE |= bmSUDAV | bmSUTOK | bmSUSP | bmURES | bmHSGRANT; // Enable selected interrupts EA = 1; // Enable 8051 interrupts #ifndef NO_RENUM // Renumerate if necessary. Do this by checking the renum bit. If it // is already set, there is no need to renumerate. The renum bit will // already be set if this firmware was loaded from an eeprom. if(!(USBCS & bmRENUM)) { EZUSB_Discon(TRUE); // renumerate } #endif // unconditionally re-connect. If we loaded from eeprom we are // disconnected and need to connect. If we just renumerated this // is not necessary but doesn't hurt anything USBCS &=~bmDISCON; CKCON = (CKCON&(~bmSTRETCH)) | FW_STRETCH_VALUE; // Set stretch // clear the Sleep flag. Sleep = FALSE; // Task Dispatcher while(TRUE) // Main Loop { // Poll User Device TD_Poll(); // Check for pending SETUP if(GotSUD) { SetupCommand(); // Implement setup command GotSUD = FALSE; // Clear SETUP flag } // check for and handle suspend. // NOTE: Idle mode stops the processor clock. There are only two // ways out of idle mode, the WAKEUP pin, and detection of the USB // resume state on the USB bus. The timers will stop and the // processor will not wake up on any other interrupts. if (Sleep) { if(TD_Suspend()) { Sleep = FALSE; // Clear the "go to sleep" flag. Do it here to prevent any race condition between wakeup and the next sleep. do { EZUSB_Susp(); // Place processor in idle mode. } while(!Rwuen && EZUSB_EXTWAKEUP()); // above. Must continue to go back into suspend if the host has disabled remote wakeup // *and* the wakeup was caused by the external wakeup pin. // 8051 activity will resume here due to USB bus or Wakeup# pin activity. EZUSB_Resume(); // If source is the Wakeup# pin, signal the host to Resume. TD_Resume(); } } } }
// Task dispatcher void main(void) { DWORD i; WORD offset; DWORD DevDescrLen; DWORD j=0; WORD IntDescrAddr; WORD ExtDescrAddr; // Initialize Global States Sleep = FALSE; // Disable sleep mode Rwuen = FALSE; // Disable remote wakeup Selfpwr = FALSE; // Disable self powered GotSUD = FALSE; // Clear "Got setup data" flag // The following section of code is used to relocate the descriptor table. // Since the SUDPTRH and SUDPTRL are assigned the address of the descriptor // table, the descriptor table must be located in on-part memory. // The 4K demo tools locate all code sections in external memory. // The descriptor table is relocated by the frameworks ONLY if it is found // to be located in external memory. pDeviceDscr = (WORD)&DeviceDscr; pDeviceQualDscr = (WORD)&DeviceQualDscr; pHighSpeedConfigDscr = (WORD)&HighSpeedConfigDscr; pFullSpeedConfigDscr = (WORD)&FullSpeedConfigDscr; pStringDscr = (WORD)&StringDscr; if (EZUSB_HIGHSPEED()) { pConfigDscr = pHighSpeedConfigDscr; pOtherConfigDscr = pFullSpeedConfigDscr; } else { pConfigDscr = pFullSpeedConfigDscr; pOtherConfigDscr = pHighSpeedConfigDscr; } if ((WORD)&DeviceDscr & 0xe000) { IntDescrAddr = INTERNAL_DSCR_ADDR; ExtDescrAddr = (WORD)&DeviceDscr; DevDescrLen = (WORD)&UserDscr - (WORD)&DeviceDscr + 2; for (i = 0; i < DevDescrLen; i++) *((BYTE xdata *)IntDescrAddr+i) = 0xCD; for (i = 0; i < DevDescrLen; i++) *((BYTE xdata *)IntDescrAddr+i) = *((BYTE xdata *)ExtDescrAddr+i); pDeviceDscr = IntDescrAddr; offset = (WORD)&DeviceDscr - INTERNAL_DSCR_ADDR; pDeviceQualDscr -= offset; pConfigDscr -= offset; pOtherConfigDscr -= offset; pHighSpeedConfigDscr -= offset; pFullSpeedConfigDscr -= offset; pStringDscr -= offset; } EZUSB_IRQ_ENABLE(); // Enable USB interrupt (INT2) EZUSB_ENABLE_RSMIRQ(); // Wake-up interrupt INTSETUP |= (bmAV2EN | bmAV4EN); // Enable INT 2 & 4 autovectoring USBIE |= bmSUDAV | bmSUTOK | bmSUSP | bmURES | bmHSGRANT; // Enable selected interrupts EA = 1; // Enable 8051 interrupts #ifndef NO_RENUM // Note: at full speed, high speed hosts may take 5 sec to detect device EZUSB_Discon(TRUE); // Renumerate #endif CKCON = (CKCON&(~bmSTRETCH)) | FW_STRETCH_VALUE; // Set stretch to 0 (after renumeration) // clear the Sleep flag. Sleep = FALSE; // Initialize user device TD_Init(); // Task Dispatcher while(TRUE) // Main Loop { if(GotSUD) // Wait for SUDAV { SetupCommand(); // Implement setup command GotSUD = FALSE; // Clear SUDAV flag } else { TD_Poll(); } // Poll User Device // NOTE: Idle mode stops the processor clock. There are only two // ways out of idle mode, the WAKEUP pin, and detection of the USB // resume state on the USB bus. The timers will stop and the // processor will not wake up on any other interrupts. if (Sleep) { if(TD_Suspend()) { Sleep = FALSE; // Clear the "go to sleep" flag. Do it here to prevent any race condition between wakeup and the next sleep. do { EZUSB_Susp(); // Place processor in idle mode. } while(!Rwuen && EZUSB_EXTWAKEUP()); // Must continue to go back into suspend if the host has disabled remote wakeup // *and* the wakeup was caused by the external wakeup pin. // 8051 activity will resume here due to USB bus or Wakeup# pin activity. EZUSB_Resume(); // If source is the Wakeup# pin, signal the host to Resume. TD_Resume(); } } } }
// *************************************************************************** // Routine : DecodeFirmwareSysex() // Input : // Output : must send ack bool // Process : decode the sysex dedicated to firmware // *************************************************************************** void DecodeFirmwareSysex(Midi_In_Struct *midi_struct) { BYTE SYSXID0; WORD Address; BYTE i, j, tmp1, tmp2, tmp3, lenght_div_2; BYTE result = 0; bit must_send_ack = FALSE; BYTE fw_checksum = 0; SYSXID0 = midi_struct->buffer[6]; // check the received sysex is dedicated to firmware otherwise return // also check it is a programming sysex : ack request must be set for firmware programming if((SYSXID0 & (FRAME_TO_Z8_TYPE_MSK | acknoledge_sysxid0)) == (/*v1.1 FRAME_IS_FIRMWARE*/FRAME_IS_FIRMWARE_OR_PURE_MIDI | acknoledge_sysxid0)) { must_send_ack = TRUE; // must finish firmware upgrade ? if(SYSXID0 & fw_znext_finish_sysxid0) { // bootloader_state = TERMINATE_FW_UPGRADE; bootloader_state++; result = acknoledge_sysxid0; // pass to z8 programming ? if(SYSXID0 & prog_ez_z8_sysxid0) { must_program_z8 = TRUE; // reset crc z8 // z8_flash_crc = 0; } } else { // here we have to program EZ or Z8 firmware data // extract address from sysex Address = midi_struct->buffer[8] << 14; Address += (midi_struct->buffer[9] << 7); Address += (midi_struct->buffer[10]); // un nibble data, decode it, calculate checksum fw_checksum = 0; j= FW_SYSX_DATA_START; for(i=0; i < midi_struct->buffer[7]; i=i+2) { tmp1 = midi_struct->buffer[FW_SYSX_DATA_START+i]; tmp2 = midi_struct->buffer[FW_SYSX_DATA_START+i+1]; fw_checksum = (fw_checksum + tmp1) & 0x7F; fw_checksum = (fw_checksum + tmp2) & 0x7F; midi_struct->buffer[j] = ( (tmp1 << 4) + (tmp2 & 0x0F)) ^ 0x55; j++; } // compare checksum if(fw_checksum == midi_struct->buffer[FW_SYSX_DATA_START+i]) { lenght_div_2 = midi_struct->buffer[7] >> 1; // NEED TO PROGRAM EZ USB EEPROM ? if(!(SYSXID0 & prog_ez_z8_sysxid0)) { if(Address < EE24C64_SIZE) //if(!(LSB(Address) % 32)) { EZUSB_IRQ_DISABLE(); CKCON |= 0x07; // Set stretch to Non zero when accessing I2C (insert 7 wait states) // write and verify eeprom result=0 if error result = EEPROMWriteVerify(Address, lenght_div_2, &midi_struct->buffer[FW_SYSX_DATA_START]); CKCON &= 0xF8; // Set stretch to 0 after i2c access (required if using paired endpoints : see chip errata) EZUSB_IRQ_ENABLE(); } } // OR NEED TO PROGRAM Z8 ? else { // calculated checksum is ok to start result = acknoledge_sysxid0; // ///////////////// PROGRAM Z8 ///////////////// // Send write command Send_To_Serial_Z8(OCD_WRITE_PRG_MEM_CMD); // Send address msb to write Send_To_Serial_Z8(Address >> 8); // Send address msb to write Send_To_Serial_Z8(Address & 0xFF); // Send data_lenght msb to write Send_To_Serial_Z8(0x00); // Send data_lenght lsb to write Send_To_Serial_Z8(lenght_div_2); // Now send datas for(i=0; i < lenght_div_2; i++) { Send_To_Serial_Z8(midi_struct->buffer[FW_SYSX_DATA_START+i]); } // V1.4 Wait for the transmit buffer being empty (everything sent to Z8) while(tx_to_serial_z8_busy); // wait raw time write EZUSB_Delay(50); // V1.4 10 // V1.4 ///////////////// READ BACK Z8 AND CHECK IF SAME ///////////////// // Send write command Send_To_Serial_Z8(OCD_READ_PRG_MEM_CMD); // Send address msb to write Send_To_Serial_Z8(Address >> 8); // Send address msb to write Send_To_Serial_Z8(Address & 0xFF); // Send data_lenght msb to write Send_To_Serial_Z8(0x00); // Send data_lenght lsb to write Send_To_Serial_Z8(lenght_div_2); // V1.4 Wait for the transmit buffer being empty (everything sent to Z8) while(tx_to_serial_z8_busy); // V1.4 enable rx1 for reading back datas from z8 SCON1 |= 0x50; /* SCON1: mode 1, 8-bit UART, enable rcvr */ // V1.4 wait for all data being received from Z8 while(Serial_In_From_Z8.current_index < lenght_div_2); // EZUSB_Delay(50); // Send_To_Midi_Out_Con(0xF0); // Send_To_Midi_Out_Con(lenght_div_2); // Send_To_Midi_Out_Con(Serial_In_From_Z8.start_index); // Send_To_Midi_Out_Con(Serial_In_From_Z8.current_index); // Send_To_Midi_Out_Con(Serial_In_From_Z8.end_index); // Send_To_Midi_Out_Con(00); // Send_To_Midi_Out_Con(00); // Send_To_Midi_Out_Con(0xF7); // V1.4 Now read datas from serial_in_from_z8 input buffer for(i=0; i < lenght_div_2; i++) { // read data received from serial port buffer tmp3 = Serial_In_From_Z8.buffer[i]; // Send_To_Midi_Out_Con(0xF0); // Send_To_Midi_Out_Con(midi_struct->buffer[FW_SYSX_DATA_START+i] >> 4); // Send_To_Midi_Out_Con(midi_struct->buffer[FW_SYSX_DATA_START+i] & 0x0F); // Send_To_Midi_Out_Con(tmp3 >> 4); // Send_To_Midi_Out_Con(tmp3 & 0x0F); // Send_To_Midi_Out_Con(0xF7); // compare data read back with data written if(tmp3 != midi_struct->buffer[FW_SYSX_DATA_START+i]) { // a byte doesnt correspond to what was programmed : send ack error result = 0; break; } } // V1.4 disable rx1 SCON1 &= 0xEF; /* SCON1: mode 1, 8-bit UART, disable rcvr */ } // end prog z8 // 10ms delay after ez or z8 programming //EZUSB_Delay(10); } // end checksum ok } // end program instead of terminate