modify usbFunctionRead

General discussions about V-USB, our firmware-only implementation of a low speed USB device on Atmel's AVR microcontrollers
nxtv2
Posts: 15
Joined: Mon Sep 17, 2018 6:56 am

modify usbFunctionRead

Post by nxtv2 » Mon Sep 17, 2018 7:04 am

Hi,
I need usbFunctionRead to be called every 2 bytes instead of 8.
How do i modify source code to do that.

Thanks.

ulao
Rank 4
Rank 4
Posts: 481
Joined: Mon Aug 25, 2008 8:45 pm

Re: modify usbFunctionRead

Post by ulao » Mon Sep 17, 2018 7:32 pm

the entire reason for usbFunctionRead is when data exceeds 8 bytes, so if you have 2 there is no need to call it.

but, if you want to change that you will have to modify usbdrv.cpp. Undesired bugs will occur.

nxtv2
Posts: 15
Joined: Mon Sep 17, 2018 6:56 am

Re: modify usbFunctionRead

Post by nxtv2 » Tue Sep 18, 2018 5:30 am

The request size if bigger than 2,
however i want to configure the adc every 2 Bytes.
so i want usbFunctionRead to be called every 2 bytes.

ulao
Rank 4
Rank 4
Posts: 481
Joined: Mon Aug 25, 2008 8:45 pm

Re: modify usbFunctionRead

Post by ulao » Tue Sep 18, 2018 3:19 pm

Ok but the function is not called based on bytes it's called based on the poll rate and then the size of the payload. I'm not going to make any rash assumptions here but I'm staring to think you're missing how USB works. If you want to configure the ADC on a time interval use timers. If you want to configure ADC based on bytes, just put the logic in the usbFunctionRead itself and stop over complicating it.

USB_PUBLIC uchar usbFunctionRead(uchar *data, uchar len)
{
uchar i;

for(i = 0; dataSent < 1024 && i < len; i++, dataSent++)
data[i] = '0'+i;

if ( n % 2 == 0) //do ADC the number is even, (i.e. ever second byte)

return i; // equals the amount of bytes written
}

nxtv2
Posts: 15
Joined: Mon Sep 17, 2018 6:56 am

Re: modify usbFunctionRead

Post by nxtv2 » Tue Sep 18, 2018 4:46 pm

If it was only that easy.

I need to configure the usn every 2 bytes
and not send the rest till the data is read.
putting it in usbFunctionread
i would miss 3 out of 4 readings

nxtv2
Posts: 15
Joined: Mon Sep 17, 2018 6:56 am

Re: modify usbFunctionRead

Post by nxtv2 » Tue Sep 18, 2018 6:25 pm

i Guess my request wasnt clear enough.

What i am trying to understand is the timing of Vusb.

I want to transmit ADC readings to the PC.

I need to start the ADC every around 100us max.
Than the cpu can continue with Vusb.

What i though was that UsbPoll doesnt retuen untill all data is written.
Then i found out it prepares the data in a buffer then returns.
And the data is sent using interrupt and the assembly routines.

I need to know how many uS will the cpu take in the inteerupts when it is sending
and if i can every 10 bits sent.
Configure the ADC
instead of waiting the whole 64 bits.

Also, if i do a Control request with size aroud 4KB, Vusb transmits at around 32KB/s.
However if i set my request size to 8B , Vusb slows down alot, to around 4KB/s.
why is that?

Thanks.

ulao
Rank 4
Rank 4
Posts: 481
Joined: Mon Aug 25, 2008 8:45 pm

Re: modify usbFunctionRead

Post by ulao » Tue Sep 18, 2018 6:52 pm

Yeah but your still asking to hack up USB, and I'm telling you, you will have a hard time getting away with this. v-usb didn't invent USB so changing the paradigm is unwise. My suggestion is to work with it not against it.

I need to configure the usn every 2 bytes
and not send the rest till the data is read.
Then use padding.....

send in 8 bytes...use 0 as padding.

byte1, btye2,0,0,0,0,0,0
byte3, btye4,0,0,0,0,0,0
byte5, btye6,0,0,0,0,0,0
.
.
.


but with your new post................

What i though was that UsbPoll doesnt retuen untill all data is written.
No the USB controls the device, the device does S it's told. So you're going to get scheduled poll rates based on the host. Normally 8 ms apart ( doc says 10 ).

need to know how many uS will the cpu take in the inteerupts when it is sending
and if i can every 10 bits sent.
No again, you're not in control here, the host decides it's packet scheduling. All you know is when the poll is ready. This is defined by the OS and is normally 8 ms apart with high speed usb 1.1.

You can send a total of 8 bytes if you need more you stack it.

while (!usbInterruptIsReady()) usbPoll(); usbSetInterrupt( reportBuffer, 8 ); //first 8
while (!usbInterruptIsReady()) usbPoll(); usbSetInterrupt((void *)&reportBuffer + 8, 6); //next 6

Keep in mind the second set of 6 bytes are 8 ms after the first 8. So it takes a round trip of 16ms.


Control transfers with v-usb are a waste of time. It works yes, but it's extremely wasteful. v-usb spends most of its time appeasing the host with nak and has no time for control transfers. That is in a timely matter.. You can do control transfers but it could take up to 1 second to finish.


You may just need to schedual your reads.

while (!usbInterruptIsReady()) usbPoll(); usbSetInterrupt( reportBuffer, 8 );
//you have 8 ms to play.


now if you need to send 10 bits and do work in between these bits, yes you can use the function write. that is how its intended to be used. but you can not send a partial payload to the host. You do not make that decision ,it does. If you use control transfers, this will work but will not be in a timely manner.


I didn't really follow all of what you posted but this should clear up a few things. If not clear just re-ask.

nxtv2
Posts: 15
Joined: Mon Sep 17, 2018 6:56 am

Re: modify usbFunctionRead

Post by nxtv2 » Wed Sep 19, 2018 5:06 am

Why is control requests no fast.From my testing they are faster than interupts.I was able to reach 32KB/s with control.i canmot use padding as that would be very slow..

I dont wanr to send partial frames.i want execute code every 10 bits sent.then continue sending normally.

ulao
Rank 4
Rank 4
Posts: 481
Joined: Mon Aug 25, 2008 8:45 pm

Re: modify usbFunctionRead

Post by ulao » Wed Sep 19, 2018 2:16 pm

Your picking apart my post without understanding it. let me put it another way,

Interrupts, .............8ms (poll), .............8ms (poll), .............8ms (poll), .............8ms (poll)
Will always poll and will always get data if the device is ready. The host does not wait.

control transfers.
Host, you have data for me?
device NAK
Host, you have data for me?
device NAK
Host, you have data for me?
device NAK
Host, you have data for me?
device NAK
Host, you have data for me?
device NAK
Host, you have data for me?
device NAK
Host, you have data for me?
device NAK
Host, you have data for me?
device ACK, I have data.
Host ok what kind.
NAK
Host ok what kind.
NAK
Host ok what kind.
NAK
Host ok what kind.
NAK
Host ok what kind.
NAK
Host ok what kind.
NAK
Host ok what kind.
NAK
Host ok what kind.
NAK
Host ok what kind.
NAK
Host ok what kind.
NAK
Host ok what kind.
NAK
Host ok what kind.
NAK
Host ok what kind.
NAK
Host ok what kind.
NAK
Host ok what kind.
Its this kind.
Host ok, what data.
NAK
Host ok, what data.
NAK
Host ok, what data.
NAK
Host ok, what data.
NAK
Host ok, what data.
NAK
Host ok, what data.
NAK
Host ok, what data.
NAK
Host ok, what data.
NAK
Host ok, what data.
NAK
Host ok, what data.
NAK
Host ok, what data.
NAK
Host ok, what data.
NAK
Host ok, what data.
NAK
Host ok, what data.
NAK
Host ok, what data.
NAK
Host ok, what data.
NAK
Host ok, what data.
NAK
Host ok, what data.
NAK
Host ok, what data.
This data, 1,2,3,4,5,6,and more.
Host, ok what is the rest.
NAK
Host, ok what is the rest.
NAK
Host, ok what is the rest.
NAK
Host, ok what is the rest.
NAK
Host, ok what is the rest.
NAK
Host, ok what is the rest.
NAK
Host, ok what is the rest.
NAK
Host, ok what is the rest.
NAK
Host, ok what is the rest.
NAK
Host, ok what is the rest.
NAK
Host, ok what is the rest.
NAK
Host, ok what is the rest.
NAK
Host, ok what is the rest.
Here it is.

My point is that this type of data depends on many things and the device will NAK most of the time. In theory it could go very fast but is not always guaranteed. With v-usb bit banging the crap out of the wire there is no time to send data so it just NAK's all day long. If you use a 20Mhz clock you have a lot more room. The more device on the wires, the more the OS has to do, and the more the HOST needs to talk, the less time for control transfers there is.

nxtv2
Posts: 15
Joined: Mon Sep 17, 2018 6:56 am

Re: modify usbFunctionRead

Post by nxtv2 » Wed Sep 19, 2018 3:17 pm

Ok so i was getting it wrong.
What is the fastest poll rate for interrupts.
The fastest i got is 1ms.
Can i go faster?
Also what is the max number of endpoints IN (Host in device out)
2 are doucmented.Can i get more?
And if i set multible endpoints, will the host poll them in order? and with known pace?

Thanks for helping out.

ulao
Rank 4
Rank 4
Posts: 481
Joined: Mon Aug 25, 2008 8:45 pm

Re: modify usbFunctionRead

Post by ulao » Wed Sep 19, 2018 6:03 pm

What is the fastest poll rate for interrupts.
On v-ubs you must use 1.1 high speed spec. That is going to be 10ms. but all Os's show 8ms in my experience.

The fastest i got is 1ms.
For what? You will never see this on usb 1.1 interrupt data and most certainty not on v-usb. Maybe with control transfers but only on occasion other times a lot more (up to seconds depending on the payload).
Can i go faster?
No
Also what is the max number of endpoints IN (Host in device out)
2 are doucmented.Can i get more?
And if i set multible endpoints, will the host poll them in order? and with known pace?
I forget at the moment but its document somewhere. I don't see why you need anymore then the 1 default? Each endpoint has the rate specified init. I'm pretty sure it is order independent.

For what you are doing you are better off with a ps/2 mouse interface. 1ms is very achievable with driver hacks.

nxtv2
Posts: 15
Joined: Mon Sep 17, 2018 6:56 am

Re: modify usbFunctionRead

Post by nxtv2 » Wed Sep 19, 2018 6:57 pm

i need to send around 20KB/s.
I was able to get 1ms poll with interrupts by simply setting the refresh rate to 1.
how many endpoints can i use?
Also , how much time does it take tk send the interrupt endpoint data ( is it 70us like a normal frame?)

Thanks

ulao
Rank 4
Rank 4
Posts: 481
Joined: Mon Aug 25, 2008 8:45 pm

Re: modify usbFunctionRead

Post by ulao » Wed Sep 19, 2018 8:43 pm

The only way to change the poll rate is to hack the driver. So what do you mean my refresh rate, there is no such setting?

v-usb Supports multiple endpoints: one control endpoint, two interrupt/bulk-in endpoints and up to 7 interrupt/bulk-out endpoints

I have not clocked the usb data trasfer but I think 70 us is close.

nxtv2
Posts: 15
Joined: Mon Sep 17, 2018 6:56 am

Re: modify usbFunctionRead

Post by nxtv2 » Wed Sep 19, 2018 9:47 pm

I mean the refresh rate.

Can i use more than 2 interrupt-IN endpoints?
If not then why?
As up to 7 InT-OUT are usable.

Regards

ulao
Rank 4
Rank 4
Posts: 481
Joined: Mon Aug 25, 2008 8:45 pm

Re: modify usbFunctionRead

Post by ulao » Thu Sep 20, 2018 1:12 pm

I mean the refresh rate.
There is no such thing as a USB refresh rate, this is rubbish, define what you claim if you want help.The default USB polling rate is 125 Hz, which means every 8 milliseconds but it is possible to increase the USB polling rate via driver hacking.
Can i use more than 2 interrupt-IN endpoints?
no
If not then why?
The author left no explanation. It is a limit of v-usb.

Post Reply