View previous topic :: View next topic |
Author |
Message |
E_Blue
Joined: 13 Apr 2011 Posts: 417
|
How to Send data through USB CDC from low level interrupt? |
Posted: Tue Oct 04, 2011 7:08 am |
|
|
I just need to send some data through USB CDC port but from a low level interrupt and I read that is not possible in usb_cdc.h file
Quote: | usb_cdc_putc() will not work correctly if interrupts are disabled, this
also means it will not work if you call usb_cdc_putc() from inside another
interrupt.
|
But in the same file it says.
Quote: |
Workaround is to call _usb_cdc_putc_fast_noflush() instead. This routine
will erase last character if you put too many characters into the endpoint
buffer. Since this routine doesn't flush the endpoint buffer, you will
also have to add this code in your main loop to check if buffer needs
to be transmitted:
Code: | if (usb_cdc_put_buffer_nextin && usb_cdc_put_buffer_free())
{
usb_cdc_flush_out_buffer();
} |
|
But how I know if the buffer full, half or empty?
Anybody know how to do it properly? _________________ Electric Blue |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Tue Oct 04, 2011 7:45 am |
|
|
The whole point of the test, is the second part tests the transmit buffer empty flag _in the hardware_, while the first is non zero, if anything is waiting to send. So the test translates as:
if anything is waiting to send and hardware can take the data send data
The variable 'usb_cdc_put_buffer_nextin', contains the current count of characters waiting to send, so is 'zero' if the buffer is empty.
Why would you care if the buffer is full or half full?. The point is that if _anything_ is waiting to send, and the hardware can take it, send the data.
If you want to test in your send routine, that there is space in the buffer to take the data, this is what 'usb_cdc_putready()' does. It returns 'true' if there is space to take a character in the buffer.
Best Wishes |
|
|
E_Blue
Joined: 13 Apr 2011 Posts: 417
|
|
Posted: Tue Oct 04, 2011 8:05 am |
|
|
Thanks for your answer.
I need to send data from several subroutines that answer commands received from USB CDC, and most of them need to send more than 64 bytes, a couple of them sends about 250 bytes, this combined with full duplex can be a disaster if I try to send data while the bus is busy.
So, i.e., if a subroutine needs to sends 101 bytes it must fill the outbuffer with 64 bytes and wait, but both, the USB interrupt and the subroutine are inside the interrupt, and thats my second problem.
At this moment I don't know how to resolve this. _________________ Electric Blue |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Tue Oct 04, 2011 8:50 am |
|
|
Simple answer.
Build your _own_ buffer, large enough for the worst case from the interrupts.
In your interrupt routines just write to this, not the USB.
Then in your main code, send this to the USB.
Best Wishes |
|
|
E_Blue
Joined: 13 Apr 2011 Posts: 417
|
|
Posted: Tue Oct 04, 2011 10:58 am |
|
|
Nice and easy! Two birds with one shot! Thanks! _________________ Electric Blue |
|
|
|