USB VID & PID

Designing hardware and software for systems that use the VS1010 MP3 Audio DSP Microcontroller.
User avatar
Panu
VLSI Staff. Currently on holiday.
Posts: 2692
Joined: Tue 2010-06-22 13:43

Re: USB VID & PID

Post by Panu » Sun 2019-05-05 9:53

Uh, a header file update conflict... hmm, i gyess you must try to comment it out from usbmachine.h or vs1010drom.h and see which one works for you.
Info: Line In and Line Out, VS1000 User interface, Overlay howto, Latest VSIDE, MCU Howto, Youtube
Panu-Kristian Poiksalo, VLSI Solution Oy

Arek
Senior User
Posts: 100
Joined: Thu 2016-09-01 10:58

Re: USB VID & PID

Post by Arek » Mon 2019-05-06 12:11

Hi All,

comment out line 65 in usbmachine.h

Code: Select all

//extern USBPacket2 usp;
works, no errors.

Arek
Senior User
Posts: 100
Joined: Thu 2016-09-01 10:58

Re: USB VID & PID

Post by Arek » Tue 2019-05-07 17:49

Hi,

"works" -> no compiler errors.
but...
PID/VID - no any change.

Line 65 in usbmachine.h changed as follow:

Code: Select all

extern USBPacket2; // usp;

Built solution:
Build started. Project: ums, configuration: Emulation-Debug

lcc -g -hhw_desc -O6 -fsmall-code -DDEBUG -Iinclude -IC:\Program\ Files\ (x86)\VSIDE\libvs1010 -o Emulation-Debug\main.o E:\Projekte\PC7\MP3\VLSI_dat\arch-ums-2019-04-30-17-33-32mA_3\main.c

C:\Program Files (x86)\VSIDE\libvs1010/usblowlib.h(126) : E:\Projekte\PC7\MP3\VLSI_dat\arch-ums-2019-04-30-17-33-32mA_3\main.c(14) : Unknown preprocessor control macro
C:\Program Files (x86)\VSIDE\libvs1010/usblowlib.h(128) : E:\Projekte\PC7\MP3\VLSI_dat\arch-ums-2019-04-30-17-33-32mA_3\main.c(14) : Unknown preprocessor control endm
C:\Program Files (x86)\VSIDE\libvs1010/usblowlib.h(131) : E:\Projekte\PC7\MP3\VLSI_dat\arch-ums-2019-04-30-17-33-32mA_3\main.c(14) : Unknown preprocessor control macro
C:\Program Files (x86)\VSIDE\libvs1010/usblowlib.h(133) : E:\Projekte\PC7\MP3\VLSI_dat\arch-ums-2019-04-30-17-33-32mA_3\main.c(14) : Unknown preprocessor control endm
C:\Program Files (x86)\VSIDE\libvs1010/usblowlib.h(136) : E:\Projekte\PC7\MP3\VLSI_dat\arch-ums-2019-04-30-17-33-32mA_3\main.c(14) : Unknown preprocessor control macro
C:\Program Files (x86)\VSIDE\libvs1010/usblowlib.h(138) : E:\Projekte\PC7\MP3\VLSI_dat\arch-ums-2019-04-30-17-33-32mA_3\main.c(14) : Unknown preprocessor control endm
C:\Program Files (x86)\VSIDE\libvs1010/usblowlib.h(126) : C:\Program Files (x86)\VSIDE\libvs1010/usbmachine.h(11) : E:\Projekte\PC7\MP3\VLSI_dat\arch-ums-2019-04-30-17-33-32mA_3\main.c(15) : Unknown preprocessor control macro
C:\Program Files (x86)\VSIDE\libvs1010/usblowlib.h(128) : C:\Program Files (x86)\VSIDE\libvs1010/usbmachine.h(11) : E:\Projekte\PC7\MP3\VLSI_dat\arch-ums-2019-04-30-17-33-32mA_3\main.c(15) : Unknown preprocessor control endm
C:\Program Files (x86)\VSIDE\libvs1010/usblowlib.h(131) : C:\Program Files (x86)\VSIDE\libvs1010/usbmachine.h(11) : E:\Projekte\PC7\MP3\VLSI_dat\arch-ums-2019-04-30-17-33-32mA_3\main.c(15) : Unknown preprocessor control macro
C:\Program Files (x86)\VSIDE\libvs1010/usblowlib.h(133) : C:\Program Files (x86)\VSIDE\libvs1010/usbmachine.h(11) : E:\Projekte\PC7\MP3\VLSI_dat\arch-ums-2019-04-30-17-33-32mA_3\main.c(15) : Unknown preprocessor control endm
C:\Program Files (x86)\VSIDE\libvs1010/usblowlib.h(136) : C:\Program Files (x86)\VSIDE\libvs1010/usbmachine.h(11) : E:\Projekte\PC7\MP3\VLSI_dat\arch-ums-2019-04-30-17-33-32mA_3\main.c(15) : Unknown preprocessor control macro
C:\Program Files (x86)\VSIDE\libvs1010/usblowlib.h(138) : C:\Program Files (x86)\VSIDE\libvs1010/usbmachine.h(11) : E:\Projekte\PC7\MP3\VLSI_dat\arch-ums-2019-04-30-17-33-32mA_3\main.c(15) : Unknown preprocessor control endm
E:\Projekte\PC7\MP3\VLSI_dat\arch-ums-2019-04-30-17-33-32mA_3\main.c (75) Warning 139: integral value converted to pointer
E:\Projekte\PC7\MP3\VLSI_dat\arch-ums-2019-04-30-17-33-32mA_3\main.c (77) Warning 139: integral value converted to pointer
E:\Projekte\PC7\MP3\VLSI_dat\arch-ums-2019-04-30-17-33-32mA_3\main.c (78) Warning 139: integral value converted to pointer
E:\Projekte\PC7\MP3\VLSI_dat\arch-ums-2019-04-30-17-33-32mA_3\main.c (79) Warning 139: integral value converted to pointer
E:\Projekte\PC7\MP3\VLSI_dat\arch-ums-2019-04-30-17-33-32mA_3\main.c (80) Warning 139: integral value converted to pointer
E:\Projekte\PC7\MP3\VLSI_dat\arch-ums-2019-04-30-17-33-32mA_3\main.c (81) Warning 139: integral value converted to pointer
E:\Projekte\PC7\MP3\VLSI_dat\arch-ums-2019-04-30-17-33-32mA_3\main.c (82) Warning 139: integral value converted to pointer
E:\Projekte\PC7\MP3\VLSI_dat\arch-ums-2019-04-30-17-33-32mA_3\main.c (83) Warning 139: integral value converted to pointer
E:\Projekte\PC7\MP3\VLSI_dat\arch-ums-2019-04-30-17-33-32mA_3\main.c (84) Warning 139: integral value converted to pointer
E:\Projekte\PC7\MP3\VLSI_dat\arch-ums-2019-04-30-17-33-32mA_3\main.c (85) Warning 139: integral value converted to pointer
E:\Projekte\PC7\MP3\VLSI_dat\arch-ums-2019-04-30-17-33-32mA_3\main.c (86) Warning 139: integral value converted to pointer
optimizing (409): 336 325.325.323.323.323
Successfully compiled 11197 lines (186 in source) with 11 warnings.
C 323 CF 0 X 72 Y 126 F 0
voplinkg -k -m mem_user -p 2 -t ums vs1010c_romabs.o Emulation-Debug\main.o Emulation-Debug\vs1010d_romabs.o -o Emulation-Debug\ums.coff -L. -Llib -LC:\Program\ Files\ (x86)\VSIDE\libvs1010 -lc


Total words: I 323, X 72, Y 126.
copy loadable.ap3 ums.dlx /y
1 Datei(en) kopiert.

Finished.

Code: Select all

/// \file main.c VS1010D VSOS Executable main file
/// This is a starting point for developing VS1010D DLX libraries and executables

#include <vo_stdio.h>
#include <volink.h>     // Linker directives like DLLENTRY
#include <apploader.h>  // RunLibraryFunction etc
#include <vs1010dRom.h>
#include <vo_gpio.h>
#include <vs1010c.h>
#include <playerinfo.h>
#include <string.h>
#include <protocol.h>
#include <devSdSd.h>
#include <usblowlib.h>
#include <usbmachine.h>
void UsbBootLoopNoReturn(void) ;


//#define VENDOR_NAME_LENGTH 13
//#define MODEL_NAME_LENGTH 7
#define SERIAL_NUMBER_LENGTH 12



#define M_VENDOR_NAME_LENGTH 13
const __y u_int16 M_vendorStringDescriptor[] = {
  ((M_VENDOR_NAME_LENGTH * 2 + 2)<<8) | 0x03,
  'A' <<8, 'A' <<8, 'B' <<8, 'B' <<8, ' ' <<8, 'S' <<8, 'o' <<8, 'l' <<8, 'u' <<8, 't' <<8, 'i' <<8, 'o' <<8, 'n' <<8};

#define M_MODEL_NAME_LENGTH 7
const __y u_int16 M_productStringDescriptor[] = {
  ((M_MODEL_NAME_LENGTH * 2 + 2)<<8) | 0x03,
  'C'<<8, 'C' <<8, 'D' <<8, 'D' <<8, '1' <<8, '0' <<8, 'C' <<8};



const __y u_int16 mydeviceDescriptor[] = { 
  //Device Descriptor	
  0x1201,   // bLength | bDescriptorType
  0x1001,   // bcd USB Version (01.10) Low | high
  0x0000,   // bDeviceClass:    Defined in Interface Descriptor
  0x0040,   // bDeviceProtocol: -''-, Endpoint 0 size 64
  0xABC0,   // idVendorL VLSI 0x19fb
  0x0210,   // idProductL VS1000: 0x00.0x02 (0x02=MassStorage)
  0x0000,   // bcdDeviceL
  0x0102,   // iManufacturerString
  0x0301    // SerialNumber
};

const __y u_int16 myconfigurationDescriptor[] = {
  // Prime Configuration Descriptor        
  0x0902,   //Length
  0x2000,   //Lo(Total length of configuration descriptor (32))
  0x0101,   //Number of interfaces supported (at least bulk only)
  0x0080,   //String index to this configuration
  0x1009,   //Power usage: 32 milliamperes | Length  
  // Interface Descriptor 1 of 1                 
  0x0400,   //Type: Interface Descriptor
  0x0002,   //This is Alternate Setting 0
  0x0806,   //Device Class: Mass Storage (Class 8)
  0x5000,   //Interface Protocol: Bulk Only Transport (0x50)  
  // Endpoint Descriptor 1 of 2, data IN to PC from our device    
  0x0705,   //Length, Endpoint descriptor
  0x8202,   //EP 2 IN, Bulk
  0x4000,   //Lo(Maximum packet size (64 bytes))
  0x0007,   //Polling Interval (0=Not Applicable for bulk endpoints)                  
  // Endpoint Descriptor 2 of 2, data OUT from PC to our device  
  0x0503,   //Endpoint descriptor, EP 3 OUT
  0x0240,   //This is a bulk endpoint
  0x0000,   //Hi(-''-)  
};


const __y void* const mychapter9MSC[] = { //USB chapter 9 protocol
	1, //IS_CHAPTER_9 // limit reply lenght to that requested by chapter 9 request
// recordLenght, matchWords, {data}*, replyEndpoint, replyLength, SideEffectFn, replyPacketPtr 
	7, 1, 0x0005, 0, 0, DoSetAddress, NULL, //Set address (zero reply, special)
	8, 2, 0x8006, 0x0001,  0, 18, DoNothing, mydeviceDescriptor, //Get device descriptor
	8, 2, 0x8006, 0x0002,  0, 32, DoNothing, myconfigurationDescriptor, //Get configuration descriptor
	8, 2, 0x8006, 0x0003,  0, 4, DoNothing, languageDescriptor,
	8, 2, 0x8006, 0x0103,  0, (M_VENDOR_NAME_LENGTH * 2 + 2), DoNothing, M_vendorStringDescriptor,
	8, 2, 0x8006, 0x0203,  0, (M_MODEL_NAME_LENGTH * 2 + 2), DoNothing, M_productStringDescriptor,
	8, 2, 0x8006, 0x0303,  0, (SERIAL_NUMBER_LENGTH * 2 + 2), DoNothing, serialNumberDescriptor,
	7, 1, 0x0009, 0, 0, DoResetDataToggles, NULL, //Set any configuration (zero reply)
	8, 2, 0x0201, 0x0000, 0, 0, DoResetDataToggles, NULL, //Clear endpoint halt (zero reply)
	7, 1, 0xa1fe, 0, 1, DoNothing, tenzeros, //get max lun
	0
};





void FlushStorage() {
	if (currentBlockLoaded) { //Unsaved SPI flash block in memory
		printf ("SPI Flush\n");
		SaveBlock(&spiBus);
	}
	if (sdcard.hardwareInfo[0] & SDSD_F_MULTIPLE_BLOCK_WRITE) { //SD write in progress
		printf("SD Flush\n");
		VODEV('U')->BlockRead(VODEV('U'),0,0,NULL);
	}
}

s_int32 frameLength = -10000;
int MyUsbHandler (int i) {
	static u_int32 flushCounter = 0;
	static u_int16 lastSdBlockLSW = 0;
	static u_int16 thisFrame = 0;
	static u_int16 lastFrame = 0;
	static u_int16 ejected = 0;

	
	PERIP(ANA_CF3) |= ANA_CF3_UTM_ENA | ANA_CF3_480_ENA | ANA_CF3_UTMBIAS;
	
	
	// Frame length tracking for detection of non-activity
	if ((usp[1] == 3) && (usp[3] == 0x5553) && (usp[3+7] == 0x061b) && (usp[3+9]==2)) ejected = 1; // SCSI eject detected
	if (!ejected && (PERIP(USB_STATUS) & (1 << USB_ST_SOF_B))) { //Start Of Frame detected
		thisFrame++; //increase frame number
		frameLength = 0;
		PERIP(USB_STATUS) = (1 << USB_ST_SOF_B); //Clear SOF flag
	}
	frameLength++;

	// Fall-back for dropping out of High Speed if the connection is not working properly
	if ((usbResetCounter) && (PERIP(USB_ST) & USB_ST_BRST)) usbResetCounter++; //USB reset detected
	if ((usbResetCounter > 8) || (runlevel < 12)) { // Don't use high speed after too many resets and not for non-highspeed runlevels
		PERIP(USB_CF) |= USB_CF_NOHIGHSPEED; // disable high speed after 8th bus reset
	}
	
	
	// Track disk activity for flushing writes left hanging		
	if ((VODEV('U') == &spiFlash) && (currentBlockLoaded == 0)) flushCounter = 0; //activity in SPI flash	
	if (sdcard.hardwareInfo[4] != lastSdBlockLSW) { //activity in SD card
		lastSdBlockLSW = sdcard.hardwareInfo[4];
		flushCounter = 0;
	}	
	if (flushCounter++ > 100000) { //So many iterations since last write activity
		FlushStorage();
		flushCounter = 0;
	}	
	 
	// Exit conditions
	if (lastReceivedCharUart0 == 27) {
		printf("ESC");
		appFlags |= APP_FLAG_QUIT;
	}				
	if (lastReceivedCharUart0 == 's') {
		printf("Shell");
		appFlags |= APP_FLAG_QUIT;
	}				
	if (frameLength > 200000) {
		printf("USB inactive");
		appFlags |= APP_FLAG_QUIT;
	}

	return 0;
}



ioresult main (char *params) {
	u_int32 usbHandlerSave = ReadFromProgramRam(109);
	
	if (sbs[2].f) return SysError("Too many files open");
	PERIP(ANA_CF1) |= ANA_CF1_BTNDIS;		
	PERIP(USB_CF) = (USB_CF_USBENA | USB_CF_DTOGERR);
	SetJmpiHookFunction((void*)109,MyUsbHandler);
	VODEV('U') = vo_pdevices[(params[0]|32)-'a'];
	if (!sdSize) {
		sdSize=1;
		VODEV('U') = &spiFlash;
	}
	appFlags = 0;
	chapter9 = mychapter9MSC;
	UsbBootLoopNoReturn(); // Call USB MSC handler in ROM. In VS1010D it will return if APP_FLAG_QUIT flag is high.
	FlushStorage();
	WriteToProgramRam(109, usbHandlerSave); //Restore original USB handler
	PERIP(USB_CF) = 0; //Disable USB
	if (lastReceivedCharUart0 == 's') {
		SimpleShell();
	}
	return S_OK;
}

File "ums.dlx" copied in SPI memory.

From command line started
VS1010>s:ums.dlx d
s:ums.dlx d
Relocked (3008)
USB port shows my disc (SD Card) but still with VLSI PID, VID name etc.

What make i wrong???

Hannu
Senior User
Posts: 72
Joined: Mon 2016-05-30 11:54

Re: USB VID & PID

Post by Hannu » Wed 2019-05-08 7:37

Panu will know better but I just looked around. In ROM code the void UsbBootLoopNoReturn(void) starts

Code: Select all

if (!deviceDescriptor[0]) { //nobody has pre-initialized device descriptor
                memcpyYY (deviceDescriptor, deviceDescriptorMSC, sizeof(deviceDescriptor));
                chapter9 = chapter9MSC; //protocol table
        }

Try if everything breaks by copying your descriptor to deviceDescriptor

arlaor
User
Posts: 7
Joined: Tue 2019-04-23 21:54

Re: USB VID & PID

Post by arlaor » Wed 2019-05-08 8:11

Panu wrote:
Wed 2019-04-24 22:34
it is a varistor.

https://www.avnet.com/shop/us/products/ ... 634761212/

Var MOV 4VAC/5.5VDC 30A 9.6V 0603 SMD Paper T/R
thnanks Panu

Arek
Senior User
Posts: 100
Joined: Thu 2016-09-01 10:58

Re: USB VID & PID

Post by Arek » Wed 2019-05-08 10:48

Thanks.
Hmm. Everything looks ok. "deviceDescriptor[0]" is defined.
I have no idea.
Perhaps kind of collision with OGG driver in memory???

Hannu
Senior User
Posts: 72
Joined: Mon 2016-05-30 11:54

Re: USB VID & PID

Post by Hannu » Wed 2019-05-08 12:03

Try to put line

Code: Select all

memcpyYY (deviceDescriptor, mydeviceDescriptor, sizeof(deviceDescriptor));
above

Code: Select all

chapter9 = mychapter9MSC;
in your main() so that the first if statement which I previously quoted isn't true in UsbBootLoopNoReturn()
At least I couldn't find any other part of ROM code which would change the VID and PID.

Arek
Senior User
Posts: 100
Joined: Thu 2016-09-01 10:58

Re: USB VID & PID

Post by Arek » Wed 2019-05-08 14:25

Hi Hannu,

many thanks for your help.
Yes, it was also my idea to add this line. Unfortunately I get only VLSI VID/PID.
No any change.

With new PCB - the same.

Arek
Senior User
Posts: 100
Joined: Thu 2016-09-01 10:58

Re: USB VID & PID

Post by Arek » Wed 2019-05-08 15:27

Update
With added line "memcpyYY..." VID is changed but Vendor name, Product Name is still VLSI and VS1010C

Arek
Senior User
Posts: 100
Joined: Thu 2016-09-01 10:58

Re: USB VID & PID changed OK, Vendor name still VLSI

Post by Arek » Mon 2019-05-13 16:04

Hi Panu,

what is wrong in my code?
(see attached pictures)
- VID is changed (0xAABB)
- PID is changed (0xCCDD)
- Serial Nr is changed (01234567)

but...

- Vendor name is not changed (still VLSI)
- Product name is not changed (still VS1010C)

Code:

Code: Select all

/// \file main.c VS1010D VSOS Executable main file
/// This is a starting point for developing VS1010D DLX libraries and executables

// Rock VID & PID OK 

#include <vo_stdio.h>
#include <volink.h>     // Linker directives like DLLENTRY
#include <apploader.h>  // RunLibraryFunction etc
#include <vs1010dRom.h>
#include <vo_gpio.h>
#include <vs1010c.h>
#include <playerinfo.h>
#include <string.h>
#include <protocol.h>
#include <devSdSd.h>
#include <usblowlib.h>
#include <usbmachine.h>

#include <dos.h>

void UsbBootLoopNoReturn(void) ;


#define MY_VENDOR_NAME_LENGTH 13
const __y u_int16 myvendorStringDescriptor[] = {
  ((MY_VENDOR_NAME_LENGTH * 2 + 2)<<8) | 0x03,
  'A' <<8, 'A' <<8, 'B' <<8, 'B' <<8, ' ' <<8, 'S' <<8, 'o' <<8, 'l' <<8, 'u' <<8, 't' <<8, 'i' <<8, 'o' <<8, 'n' <<8};

#define MY_MODEL_NAME_LENGTH 7
const __y u_int16 myproductStringDescriptor[] = {
  ((MY_MODEL_NAME_LENGTH * 2 + 2)<<8) | 0x03,
  'C'<<8, 'C' <<8, 'D' <<8, 'D' <<8, '1' <<8, '0' <<8, 'C' <<8};

#define SERIAL_NUMBER_LENGTH 12
const __y u_int16 M_serialNumberDescriptorDefault[] = {
  ((SERIAL_NUMBER_LENGTH * 2 + 2)<<8) | 0x03,
  '0'<<8, // You should put your own serial number here
  '0'<<8,'0'<<8,'0'<<8,'0'<<8,'0'<<8,
  '0'<<8,'0'<<8,'0'<<8,'0'<<8,'0'<<8,
  '1'<<8, // Serial number should be unique for each unit
};


__y u_int16 serialNumberDescriptor[sizeof(M_serialNumberDescriptorDefault)];


void M_UsbSetSerialNumber(u_int32 n) {
	__y char *p = (__y char*)&serialNumberDescriptor[12];
	memcpyYY(serialNumberDescriptor, M_serialNumberDescriptorDefault, sizeof(serialNumberDescriptor));
	while(p>serialNumberDescriptor) {
		*p-- = ((n&0x7)+'0') << 8;
		n >>= 3;
	}
}


const __y u_int16 mydeviceDescriptor[] = { 
  //Device Descriptor	
  0x1201,   // bLength | bDescriptorType
  0x1001,   // bcd USB Version (01.10) Low | high
  0x0000,   // bDeviceClass:    Defined in Interface Descriptor
  0x0040,   // bDeviceProtocol: -''-, Endpoint 0 size 64
  0xBBAA,   // idVendorL  R: 0xAABB,  VLSI 0x19fb
  0xDDCC,   // idProductL R: 0xCCDD,  VS1000: 0x00.0x02 (0x02=MassStorage)
  0x0000,   // bcdDeviceL
  0x0102,   // iManufacturerString
  0x0301    // SerialNumber
};

const __y u_int16 myconfigurationDescriptor[] = {
  // Prime Configuration Descriptor        
  0x0902,   //Length
  0x2000,   //Lo(Total length of configuration descriptor (32))
  0x0101,   //Number of interfaces supported (at least bulk only)
  0x0080,   //String index to this configuration
  0x1009,   //Power usage: 32 milliamperes | Length  
  // Interface Descriptor 1 of 1                 
  0x0400,   //Type: Interface Descriptor
  0x0002,   //This is Alternate Setting 0
  0x0806,   //Device Class: Mass Storage (Class 8)
  0x5000,   //Interface Protocol: Bulk Only Transport (0x50)  
  // Endpoint Descriptor 1 of 2, data IN to PC from our device    
  0x0705,   //Length, Endpoint descriptor
  0x8202,   //EP 2 IN, Bulk
  0x4000,   //Lo(Maximum packet size (64 bytes))
  0x0007,   //Polling Interval (0=Not Applicable for bulk endpoints)                  
  // Endpoint Descriptor 2 of 2, data OUT from PC to our device  
  0x0503,   //Endpoint descriptor, EP 3 OUT
  0x0240,   //This is a bulk endpoint
  0x0000,   //Hi(-''-)  
};




const __y void* const mychapter9MSC[] = { //USB chapter 9 protocol
	1, //IS_CHAPTER_9 // limit reply lenght to that requested by chapter 9 request
// recordLenght, matchWords, {data}*, replyEndpoint, replyLength, SideEffectFn, replyPacketPtr 
	7, 1, 0x0005, 0, 0, DoSetAddress, NULL, //Set address (zero reply, special)
	8, 2, 0x8006, 0x0001,  0, 18, DoNothing, mydeviceDescriptor, //Get device descriptor
	8, 2, 0x8006, 0x0002,  0, 32, DoNothing, myconfigurationDescriptor, //Get configuration descriptor
	8, 2, 0x8006, 0x0003,  0, 4, DoNothing, languageDescriptor,
	8, 2, 0x8006, 0x0103,  0, (MY_VENDOR_NAME_LENGTH * 2 + 2), DoNothing, myvendorStringDescriptor,
	8, 2, 0x8006, 0x0203,  0, (MY_MODEL_NAME_LENGTH * 2 + 2), DoNothing, myproductStringDescriptor,
	8, 2, 0x8006, 0x0303,  0, (SERIAL_NUMBER_LENGTH * 2 + 2), DoNothing, serialNumberDescriptor,
	7, 1, 0x0009, 0, 0, DoResetDataToggles, NULL, //Set any configuration (zero reply)
	8, 2, 0x0201, 0x0000, 0, 0, DoResetDataToggles, NULL, //Clear endpoint halt (zero reply)
	7, 1, 0xa1fe, 0, 1, DoNothing, tenzeros, //get max lun
	0
};





void FlushStorage() {
	if (currentBlockLoaded) { //Unsaved SPI flash block in memory
		printf ("SPI Flush\n");
		SaveBlock(&spiBus);
	}
	if (sdcard.hardwareInfo[0] & SDSD_F_MULTIPLE_BLOCK_WRITE) { //SD write in progress
		printf("SD Flush\n");
		VODEV('U')->BlockRead(VODEV('U'),0,0,NULL);
	}
}

s_int32 frameLength = -10000;
int MyUsbHandler (int i) {
	static u_int32 flushCounter = 0;
	static u_int16 lastSdBlockLSW = 0;
	static u_int16 thisFrame = 0;
	static u_int16 lastFrame = 0;
	static u_int16 ejected = 0;

	
	PERIP(ANA_CF3) |= ANA_CF3_UTM_ENA | ANA_CF3_480_ENA | ANA_CF3_UTMBIAS;
	
	
	// Frame length tracking for detection of non-activity
	if ((usp[1] == 3) && (usp[3] == 0x5553) && (usp[3+7] == 0x061b) && (usp[3+9]==2)) ejected = 1; // SCSI eject detected
	if (!ejected && (PERIP(USB_STATUS) & (1 << USB_ST_SOF_B))) { //Start Of Frame detected
		thisFrame++; //increase frame number
		frameLength = 0;
		PERIP(USB_STATUS) = (1 << USB_ST_SOF_B); //Clear SOF flag
	}
	frameLength++;

	// Fall-back for dropping out of High Speed if the connection is not working properly
	if ((usbResetCounter) && (PERIP(USB_ST) & USB_ST_BRST)) usbResetCounter++; //USB reset detected
	if ((usbResetCounter > 8) || (runlevel < 12)) { // Don't use high speed after too many resets and not for non-highspeed runlevels
		PERIP(USB_CF) |= USB_CF_NOHIGHSPEED; // disable high speed after 8th bus reset
	}
	
	
	// Track disk activity for flushing writes left hanging		
	if ((VODEV('U') == &spiFlash) && (currentBlockLoaded == 0)) flushCounter = 0; //activity in SPI flash	
	if (sdcard.hardwareInfo[4] != lastSdBlockLSW) { //activity in SD card
		lastSdBlockLSW = sdcard.hardwareInfo[4];
		flushCounter = 0;
	}	
	if (flushCounter++ > 100000) { //So many iterations since last write activity
		FlushStorage();
		flushCounter = 0;
	}	
	 
	// Exit conditions
	if (lastReceivedCharUart0 == 27) {
		printf("ESC");
		appFlags |= APP_FLAG_QUIT;
	}				
	if (lastReceivedCharUart0 == 's') {
		printf("Shell");
		appFlags |= APP_FLAG_QUIT;
	}				
	if (frameLength > 200000) {
		printf("USB inactive");
		appFlags |= APP_FLAG_QUIT;
	}

	return 0;
}



ioresult main (char *params) {
u_int32 usbHandlerSave = ReadFromProgramRam(109);
u_int32 serialNr;
FILE *f;
	
	if (sbs[2].f) return SysError("Too many files open");
	PERIP(ANA_CF1) |= ANA_CF1_BTNDIS;		
	PERIP(USB_CF) = (USB_CF_USBENA | USB_CF_DTOGERR);
	SetJmpiHookFunction((void*)109,MyUsbHandler);
	VODEV('U') = vo_pdevices[(params[0]|32)-'a'];
	if (!sdSize) {
		sdSize=1;
		VODEV('U') = &spiFlash;
	}
	appFlags = 0;

	
	
	f = fopen("s:serial.srl", "rb");
	fread(&serialNr, 1, 2, f);
	fclose(f);

	memcpyYY (deviceDescriptor, mydeviceDescriptor, sizeof(deviceDescriptor));	
	UsbSetSerialNumber(serialNr);

	chapter9 = mychapter9MSC;
	
	UsbBootLoopNoReturn(); // Call USB MSC handler in ROM. In VS1010D it will return if APP_FLAG_QUIT flag is high.
	FlushStorage();
	WriteToProgramRam(109, usbHandlerSave); //Restore original USB handler
	PERIP(USB_CF) = 0; //Disable USB
	if (lastReceivedCharUart0 == 's') {
		SimpleShell();
	}
	return S_OK;
}



Any tips?

Post Reply