General discussions about V-USB, our firmware-only implementation of a low speed USB device on Atmel's AVR microcontrollers
-
ankurhanda
Post
by ankurhanda » Mon Nov 05, 2007 2:49 pm
hi ,
i have written this code snippet in the
Code: Select all
USB_PUBLIC uchar usbFunctionSetup(uchar data[8])
function and i am continuously invoking this function through host . The code snippet in this function is
Code: Select all
if ( data[1] == SERIAL_SET_ROBOT_VEL_PC ) /* SETTING THE ROBOT VELOCITY FROM PC*/
{
PORTD = 0 ;
usbMsgPtr = replyBuf;
lsb_fbVel = data[2] ;
msb_fbVel = data[3] ;
lsb_rlVel = data[4] ;
msb_rlVel = data[5] ;
replyBuf[0] = data[4] ;
replyBuf[1] = data[5] ;
//currentState = SERIAL_SET_ROBOT_VEL_PC ;
txChar_poll(SERIAL_SET_ROBOT_VEL_PC);
rec = rxChar_poll();
txChar_poll(lsb_fbVel);
txChar_poll(msb_fbVel);
for ( rec = 0 ; rec < 255 ; rec ++ );
txChar_poll(lsb_rlVel);
txChar_poll(msb_rlVel);
rec = rxChar_poll();
currentState = IDLE ;
return 2 ;
}
the host gets message : No such device after some invocations. What is that is ending the connection ?
-
gert
- Rank 1
- Posts: 27
- Joined: Sat Oct 20, 2007 5:58 pm
Post
by gert » Tue Nov 06, 2007 12:58 am
Do you have any idea how long your procedure occupies the CPU?
-
christian
- Objective Development
- Posts: 1443
- Joined: Thu Nov 09, 2006 11:46 am
Post
by christian » Tue Nov 06, 2007 5:20 pm
I see that you send some data with txChar_poll() and then wait for a reply with rxChar_poll(). I don't have the USB spec in front of me, but I remember that there is a time limit for processing messages. This time limit might be 50 ms.
Please also note that the host polls the USB aggressively while it waits for a reply. This means that the AVR spends most of its time in the USB interrupt.
As a general guideline, you should just store away all data in usbFunctionSetup and process it later in background. If a response must be sent, you can either poll for it or use an interrupt-in endpoint.
-
ankurhanda
Post
by ankurhanda » Wed Nov 07, 2007 8:44 pm
Hi christian ,
I am having problems using this AVR-USB code as a USB to serial converter. The function txChar_poll() sends a char by polling on the UART , the moment it finishes sending a character , i have a function rxChar_poll() which receives a character through polling. Once that is done i transmit 4 character bytes through UART. Now i wanted to know why this just spoils the USB connection , this process doesn't take much time , i think it would be less than 50ms always. But when i store these bytes to be sent and then do this process ( sending one char and receive one char and then send 4 char ) in the main programs , it hangs too. The connection is lost , it sometimes say that device not found or timer expired at other times. I want to use only polling, is there any way out ?
Thanks
-
christian
- Objective Development
- Posts: 1443
- Joined: Thu Nov 09, 2006 11:46 am
Post
by christian » Wed Nov 07, 2007 10:49 pm
If you do the same thing in the main loop, usbPoll() is not called frequently enough.
You CAN and SHOULD poll in the main loop, but you MUST NOT hang waiting for an event. If the UART is busy, just continue with the main loop and try again during the next cycle. Do this for every byte you send and for every byte you receive.