AnsweredHot!Problems with CDC data.

Author
vini_i
Super Member
  • Total Posts : 398
  • Reward points : 0
  • Joined: 2014/01/16 17:51:55
  • Location: Ohio, United States
  • Status: offline
2018/07/10 11:27:14 (permalink)
0

Problems with CDC data.

I'm using a PIC32MZ2048EFH064 on a custom board. 
 
I'm very new to harmony. This is actually my first true project with it. I've been really struggling with the documentation. There seem to be lots of little pieces but there is no larger view. I'm trying to set up the chip to send data up to a computer. I've got the oscillator configured and verified, it is set up for USB operation. I got the USB service set up as a CDC device. I get no enumeration. After digging at the documentation for a while I find that I need a USB event handler. After crunching together some example code I got the device to enumerate. Then I found that for CDC operation I needed a CDC event handler. This was harder to put together from example code. After I got it compiling I ran the following code as an example. It gets transmitted every one second. 
 
// This example assumes that the maxPacketSize is 6.
USB_DEVICE_CDC_TRANSFER_HANDLE transferHandle;
USB_DEVICE_CDC_INDEX instance;
USB_DEVICE_CDC_RESULT writeRequestResult;
uint8_t data[] = {'A','B','C','D','E','F'};
// In this example we want to send 6 bytes only.
writeRequestResult = USB_DEVICE_CDC_Write(instance,&transferHandle, data, 6,
 USB_DEVICE_CDC_TRANSFER_FLAGS_DATA_COMPLETE);
if(USB_DEVICE_CDC_RESULT_OK != writeRequestResult)
{
 //Do Error handling here
}

 
The terminal on the computer would receive 6 bytes of data once a second as expected but the data was not what was sent and would change every time the device was unplugged and plugged back in. I'm more than willing to admit that I screwed something up. 
 
After some more digging I found that if I configure the USB service and the console service the device enumerates without changing any code at all. With a fresh project, I ran the same code. Again I get 6 bytes of data every second but again it is not the data that I sent. This time the data is consistent after unplugging and plugging the device back in. For instance, the first character comes out as 0x76 when it should be a 0x41. Just as a test I changed the characters to their hex equivalent and the same data came out. 
 
I feel like I'm missing something fundamental as to why the data is coming out garbled but I don't know what it might be and even where to look. 
 
 
 
#1
qɥb
Monolothic Member
  • Total Posts : 3329
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: Problems with CDC data. 2018/07/10 15:17:18 (permalink) ☼ Best Answerby vini_i 2018/07/10 18:05:20
4 (1)
I haven't used any of those libraries at all, but at a guess, you are calling a routine which queues up data to be sent later, but you are passing it the address of a local variable that dies as soon as your function exits.
Try doing it with a global buffer instead.
 

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#2
vini_i
Super Member
  • Total Posts : 398
  • Reward points : 0
  • Joined: 2014/01/16 17:51:55
  • Location: Ohio, United States
  • Status: offline
Re: Problems with CDC data. 2018/07/10 18:08:51 (permalink)
3 (1)
qɥb
I haven't used any of those libraries at all, but at a guess, you are calling a routine which queues up data to be sent later, but you are passing it the address of a local variable that dies as soon as your function exits.
Try doing it with a global buffer instead.



You hit the nail on the head. I had the declaration in between the start of the main and the main loop. It didn't dawn on me that this may be called from someplace else. Moving it to outside the main making it global did the trick. 
 
Thanks
#3
NKurzman
A Guy on the Net
  • Total Posts : 16574
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: offline
Re: Problems with CDC data. 2018/07/10 18:53:05 (permalink)
3 (1)
Making it "Static" would also work.
#4
vini_i
Super Member
  • Total Posts : 398
  • Reward points : 0
  • Joined: 2014/01/16 17:51:55
  • Location: Ohio, United States
  • Status: offline
Re: Problems with CDC data. 2018/07/12 02:51:23 (permalink)
0
To be even more specific I found in the documentation the following. 
 
 
While the using the CDC Function Driver with the PIC32MZ USB module, the transmit buffer provided to the USB_DEVICE_CDC_Write function should be placed in coherent memory and aligned at a 16 byte boundary. This can be done by declaring the buffer using the __attribute__((coherent, aligned(16))) attribute. An example is shown here
uint8_t data[256] __attribute__((coherent, aligned(16)));

#5
Jump to:
© 2018 APG vNext Commercial Version 4.5