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 support@ccsinfo.com

Usb mode to transfer data from pic24 to PC ?

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



Joined: 24 May 2010
Posts: 4

View user's profile Send private message

Usb mode to transfer data from pic24 to PC ?
PostPosted: Thu Sep 28, 2017 8:26 pm     Reply with quote

Hello all,
I'm playing with a PIC24FJ64GB002 and USB to check if I can achieve 10Khz 16bits sampling of 4 channels and sending the data over usb.
I use external ADCs (ads8326) and sampling is done on timer1 overflow interrupt.
I checked CCS examples and used hid example, linux on the other side, no specific driver needed.
PIC is sending 9 bytes after each convertion.
I see data incoming, and while I'm not an expert programmer, I suspect that the usb bus is polled on the PC as I can see that not all the data is received.
Only 1 group of 9 bytes every 80.... Lost of bytes lost ! ;)
My question is: what is the most effective way to tranfer 9 bytes every 100us to a PC ? Is it HID, CDC, bulk ?
I never used Usb to send that fast before, only CDC for terminal debug.

Thanks for your help.
Ttelmah



Joined: 11 Mar 2010
Posts: 12960

View user's profile Send private message

PostPosted: Fri Sep 29, 2017 1:10 am     Reply with quote

There is a bit of confusion here over 'device classes' versus transfers.

Might as well use CDC. You are only talking 90000 bytes/second.
CDC uses bulk & interrupt.

A CDC device uses bulk transfers for the payload data, and interrupt transfers for line notifications.

Now on Linux, depending on the core you are using, there is a bug with some chipsets, that limits HID to around 32K/second. It only offers 64K/sec anyway. (1000*64byte packets). This will be what you are hitting.
The point about HID, is it uses interrupt transfers to guarantee low latency (things are responded to quickly), but at a cost in total data rate...

There are similar problems in Windows with the serial CDC driver on versions before W10, that can tend to throttle the throughput on this, though even on these well over 200K/second is achievable. CDC picked up a bit of a bad reputation partially because of this. With a PIC24, running to a Mac, I have managed to push up to 1MB/sec with CDC.

Now the fastest would be MSD, but you'd then have to write the entire driver both to support this. I've seen over 4MB/sec transferring from a PIC 24 using MSD and DMA, but it was a custom driver....
MSD, does not guarantee response times. It uses the 'spare' capacity on the bus, but without the quick response guaranteed by interrupt transfers.

Key thing though is that you need to be ensuring you are not stalling the transfers with your data. So do your readings, and buffer these values to the USB driver.

Do a simple test. Just load the standard serial CDC driver on the PIC, and send a few KB of data. Have your Linux code just stream this into a file, and do a timestamp at each end, so you can see the speed you are getting, and check the data for any missing bytes.
catslab



Joined: 24 May 2010
Posts: 4

View user's profile Send private message

PostPosted: Fri Sep 29, 2017 7:47 am     Reply with quote

Thanks Ttelmah.

Yes, I'm a bit confused about USB. I have to read and learn a lot !

I used the 24bit wizard and ticked HID and interrupts on the pic side.

The main loop is simple:
Code:

while(TRUE)
   {
      if (usb_enumerated() && adcdone )
      {
         output_high(PIN_B7);
         if (compteur++ == 250 )
            compteur = 0;
         adcval[0] = compteur;
         usb_put_packet(USB_HID_ENDPOINT, adcval, USB_CONFIG_HID_TX_SIZE, USB_DTS_TOGGLE);
         output_low(PIN_B7);
         adcdone=0;
      }
   }


The ADC is done in the timer interrupt and the tx size is 9.
May be I should load a full 64 bytes and transfer once it's full.

On the linux side, I used hidapi from signal 11 and modified a bit a test program, but it's polling, not using interrupts as I can see. Might be the trouble.

Data is not supposed to stall the transfer as I measured timings. I used pin_B7 as a marker for the logic analyser and everything seems to be done in time and not overlapping.

I will test CDC right away.
Ttelmah



Joined: 11 Mar 2010
Posts: 12960

View user's profile Send private message

PostPosted: Fri Sep 29, 2017 8:08 am     Reply with quote

Since you are using HID, you will get a maximum of 64000B/sec.

With HID, every 'tick' of the USB driver will check the device and transfer one packet if data is available. This gives the guaranteed response (so low latency), but that is all that gets transferred. So with full speed USB, where the packet is 64bytes, and the tick is 1mSec (minimum that can be specified), you get 64000bytes/sec.
HID is guaranteed to be serviced, CDC is not. CDC instead uses bulk transfers (so if the bus gets busy will have to wait for a timeslot), but can transfer much more data.
I'd get/borrow a copy of 'USB Complete'. It explains the capabilities of the transfers and classes.
catslab



Joined: 24 May 2010
Posts: 4

View user's profile Send private message

PostPosted: Fri Sep 29, 2017 10:42 am     Reply with quote

Thanks for your time and advice.

I checked with CDC, and looks like I get all the data on the linux end. I put a serial in the first byte and saw no disruption in the sequence.
So I will go with that for more in depth testing of my circuit.
Ttelmah



Joined: 11 Mar 2010
Posts: 12960

View user's profile Send private message

PostPosted: Fri Sep 29, 2017 1:54 pm     Reply with quote

Good. Smile
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