CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

How to Send data through USB CDC from low level interrupt?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
E_Blue



Joined: 13 Apr 2011
Posts: 417

View user's profile Send private message

How to Send data through USB CDC from low level interrupt?
PostPosted: Tue Oct 04, 2011 7:08 am     Reply with quote

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? Question
_________________
Electric Blue
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Tue Oct 04, 2011 7:45 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Oct 04, 2011 8:05 am     Reply with quote

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. Confused
_________________
Electric Blue
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Tue Oct 04, 2011 8:50 am     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Oct 04, 2011 10:58 am     Reply with quote

Nice and easy! Two birds with one shot! Thanks! Wink
_________________
Electric Blue
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group