Page 1 of 1

HID_Write behaviour microC Pro Version 4.6

Posted: 14 Feb 2011 20:03
by absalom
I used your HID Read Write Interrupt example for the first USB trials. It worked without major problems - thank you for this.

But when I adapted the example I encountered a problem. I added one line

Code: Select all

      ...
      for(cnt=0;cnt<64;cnt++)
      writebuff[cnt]=readbuff[cnt];

    while(!HID_Write(&writebuff,64));
    writebuff[0] = 66;           // New line, this data will be written!
  }
}
and saw in the HID terminal that the first received character is always the 'B'. But since the writebuff[0] = 66 line is executed after "data is successfuly sent" (citation from help) the behaviour is not correct in my opinion. To make it more clear I add a screenshot.
HID-Example.jpg
HID-Example.jpg (122.69 KiB) Viewed 5061 times
I think the additional line must have no influence. What do you think?

Thanks and Best Regards
absalom

Re: HID_Write behaviour microC Pro Version 4.6

Posted: 14 Feb 2011 20:38
by Dany
I was a little bit afraid this would happen sooner or later. The reason is that the main program uses the USB buffers directly (at 0x500 and 0x540). Additionally I think that "HID_Write" returns true if the sendbuffer is free, that is the previous transmission is executed. It is very well possible that the current one is still pending and changing the buffer contents has indeed some effect...

Re: HID_Write behaviour microC Pro Version 4.6

Posted: 15 Feb 2011 19:17
by absalom
That would be painful! Do you know another way to check if the write buffer is ready for new data (apart from waiting for an acknowledge message from the host)?
Looking forward to read also a statement from the mE team.
Thanks
absalom

Re: HID_Write behaviour microC Pro Version 4.6

Posted: 16 Feb 2011 10:30
by yt7pwr
Function:

Code: Select all

while(!HID_Write(&writebuff,64));
is blocking call and line

Code: Select all

writebuff[0] = 66;           // New line, this data will be written!
have no effect because USB write procedure is ended!
Change write buffer before HID_write call.

Re: HID_Write behaviour microC Pro Version 4.6

Posted: 16 Feb 2011 10:39
by absalom
yt7pwr wrote:Function:

Code: Select all

while(!HID_Write(&writebuff,64));
is blocking call and line

Code: Select all

writebuff[0] = 66;           // New line, this data will be written!
have no effect because USB write procedure is ended!
Change write buffer before HID_write call.
Unfortunately it has an effect as described above. That is the reason for opening this thread.
Or do you test the described example?

Re: HID_Write behaviour microC Pro Version 4.6

Posted: 16 Feb 2011 12:46
by yt7pwr
Maybe I was not clear: your code is wrong.No point to change one write buffer
byte after HID_write when your loop will rewrite all bytes!
Try this:

Code: Select all

while{1}
{
while(!HID_Read());                        // this is blocking call! try polling method instead
    for(cnt=0;cnt<64;cnt++)
      writebuff[cnt]=readbuff[cnt];     // rewrite all bytes!
    writebuff[0] = 66;                      // change only one byte! 
    while(!HID_Write(&writebuff,64)); // send buffer via USB link
    writebuff[0] = 66;                      // this line have no effect!
  }

Re: HID_Write behaviour microC Pro Version 4.6

Posted: 16 Feb 2011 13:26
by Dany
yt7pwr wrote:

Code: Select all

    writebuff[0] = 66;                      // this line have no effect!
The issue here is that it has effect, while no effect is expected.

Re: HID_Write behaviour microC Pro Version 4.6

Posted: 17 Feb 2011 10:22
by slavisa.zlatanovic
Hi!

Currently, I can only agree with Dany and yt7pwr.
The problematic line is obviously written in the wrong place but it should not have any effect.
I'll contact our more experienced colleagues regarding this issue and I'll get back to you.

Re: HID_Write behaviour microC Pro Version 4.6

Posted: 22 Feb 2011 10:11
by android
I reported a very similar (if not identical) problem in September 2010 (ticket number VUT-955119) but it must have slipped under the radar! I did come up with a workaround which I described then as:
I've got another workaround for this problem (of not being able to execute 2
USB HID_Write calls without an intervening 1 ms delay). I've found that if I
code (pseudo code):

Code: Select all

buffer1 = 'x'
while (!HID_Write(buffer1,1));
buffer2 = 'y'
while(!HID_Write(buffer2,1));
...then it works correctly: the host receives 'x' and then 'y'. Also, if I
code a 1 ms delay between 2 sends from the *same* buffer, it works -
although more slowly due to the 1 ms delay.

But if I code:

Code: Select all

buffer = 'x'
while(!HID_Write(buffer,1));
buffer = 'y'
while(!HID_Write(buffer,1));
...then there is a high probability that 'y' is written to the USB host 2
times, and 'x' never reaches the host.

I'm sure (as I can be) that I cannot trust the return value from HID_Write
(the number of bytes written to the USB host) as meaning "I can now update
the send buffer".

When HID_Write returns (indicating the number of bytes sent) it should mean
that I can immediately send the next buffer, yes?

I've attached a minimal project that works (using two buffers)[/color]

Re: HID_Write behaviour microC Pro Version 4.6

Posted: 22 Feb 2011 19:58
by absalom
Thanks for the info android, that is really interesting.
I hope mE handles this topic with more priority in future ..

Re: HID_Write behaviour microC Pro Version 4.6

Posted: 10 Mar 2012 12:46
by Dany
slavisa.zlatanovic wrote:Currently, I can only agree with Dany and yt7pwr.
The problematic line is obviously written in the wrong place but it should not have any effect.
I'll contact our more experienced colleagues regarding this issue and I'll get back to you.
Hi Slavisa, I can not reproduce this problem any more in mP v5.40, I do not know about mC.

Can you confirm it has indeed been solved?
Has the routine "HID_Write" changed to a blocking one?

Thanks in advance! :D

Re: HID_Write behaviour microC Pro Version 4.6

Posted: 10 Mar 2012 13:31
by android
Hi Dany,

By sheer coincidence I came to the conclusion just today that the problem has been fixed by 5.4 ...I no longer need to use two different buffers to send key pressed/key not pressed reports.

:)

Re: HID_Write behaviour microC Pro Version 4.6

Posted: 10 Mar 2012 18:14
by Dany
android wrote:Hi Dany,

By sheer coincidence I came to the conclusion just today that the problem has been fixed by 5.4 ...I no longer need to use two different buffers to send key pressed/key not pressed reports.

:)
Ha! Thanks!! :D :D