Various USB issues on PIC32MZ

General discussion on mikroPascal PRO for PIC32.
Post Reply
Author
Message
VCC
Posts: 463
Joined: 08 Jun 2009 18:31
Location: Romania

Various USB issues on PIC32MZ

#1 Post by VCC » 15 Mar 2021 12:04

Hi,
mostly @mikroe team:
Since the network/ethernet transfer is not reliable (see viewtopic.php?f=172&t=76904), I was looking to alternatives, such as PIC32MZ and even USB. My current experiment is to use the mentioned project on PIC32MZ2048EFM144 (some old Microchip board), with both networking and USB (compiler: mP for PIC32 v4.0.0). For now, I want to use USB as a debugging channel, which sends debugging data to PC "as fast as possible".

First encountered problem was that the USB Device Library (https://libstock.mikroe.com/projects/vi ... ce-library) does not support PIC32MZ. So I went on looking at the built-in USB HID library.
The only option which works (and yes, this is also unstable), is the polling mode. If I want to use the interrupt mode (enabled from USB_dsc), the PC (Win 8.1) does not recognize the device. It appears that the PIC enters some loop, because on Windows, it keeps connecting and disconnecting. Although the help mentions only one type of USB interrupts, the library exposes a second one, with dma. So either using USB_Interrupt_Proc or USB_Dma_Interrupt_Proc, allocated at the proper vector addresses, the behaviour is the same as described above. The example, provided by the compiler installation, has some additional interrupt priority and DMA settings, which are not mentioned in the help.
I went back to using polling, because this is the only working option, however sending data from PIC to PC, the transfer misses data. I don't know at which side the problem is, but it looks like some of the HID_Write calls are ignored.
The mP help mentions using a while loop and call HID_Write until success, but it is a blocking call from which it cannot recover. The mentioned solution is to call USB_Break, which is not available on PIC32MZ.
Anyway, USB_Break should be called from an interrupt, but by using polling, which interrupt should be used? The help does not mention if an USB interrupt should be used or a timer interrupt.
Also, the help says that that HID_Write and Gen_Write are blocking calls. Not sure about Gen_Write, but HID_Write by itself is not blocking. Only using the mentioned while loop, which calls HID_Write, is blocking.
Another issue with the help is that it misses Gen_RegisterEndpoint routine. I may not use it, but I thought to mention it.
BTW, the USB HID bootloader, works just fine. It does not appear to miss any data.

I want my application (at least this experiment) to send data to PC as fast as possible, so it won't interfere with the network transfer. So, the big question, will we see support for the USB Device library for PIC32MZ? I hope that by using CDC, the speed will be higher. These devices (MZ) support Hi-Speed, so that should do it.

I don't have a working project at the moment, but for the record, here is how I use the HID_Write call, without the blocking loop:

Code: Select all

var
  USBRecBuffer, USBSendBuffer: array[64] of Byte;
  USBBufferSuccessfullySentToPC: Boolean;
  ItemRemovedFromFIFO: Boolean;

procedure SendDynTFTFIFOItemToUSB;
begin
  if USBBufferSuccessfullySentToPC then
    ItemRemovedFromFIFO := GetItemFromFIFO(@USBSendBuffer);
    
  if ItemRemovedFromFIFO then
  begin
    USBBufferSuccessfullySentToPC := False;

    if HID_Write(@USBSendBuffer, 64) <> 0 then
    begin
      USBBufferSuccessfullySentToPC := True;
      ItemRemovedFromFIFO := False;
    end;
  end;
  
  LED_R := not LED_R;
end;


//main loop:
repeat
  //other stuff
  Net_Ethernet_Intern_doPacket;

  USB_Polling_Proc;
  SendDynTFTFIFOItemToUSB;
until False;
I am using a pretty big FIFO between the debugging data and the USB transfer, and it fills pretty quickly because of low USB transfer rate. See https://libstock.mikroe.com/projects/view/918/fixedfifo for GetItemFromFIFO function. (And yes, I had to rename some of the FIFO functions to be compatible with PIC32MZ).

Thank you :)

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: Various USB issues on PIC32MZ

#2 Post by filip » 16 Mar 2021 14:54

Hi,

Thank you for your extensive report.
BTW, the USB HID bootloader, works just fine. It does not appear to miss any data.
Since the USB HID bootloader is working fine, and it transfers a lot of information from/to MCU, maybe there some issues with your initial project ?

Regards,
Filip.

VCC
Posts: 463
Joined: 08 Jun 2009 18:31
Location: Romania

Re: Various USB issues on PIC32MZ

#3 Post by VCC » 16 Mar 2021 18:02

Since the USB HID bootloader is working fine, and it transfers a lot of information from/to MCU, maybe there some issues with your initial project
What I believe the issues are, come from the fact that I rely on the result of HID_Write for flow control. USB HID Bootloader has only two calls of HID_Write, and none of those actually verify the result. The flow control in this bootloader, is based on software, which requires the PC application to request data. This is slower than what I want, but may be the only option left.
The bootloader transfers more data to the MCU than from it, which is not what I want from this particular experiment. I want more data towards the PC.

Why doesn't the bootloader code rely on the HID_Write result as stated in the help? Is that result unreliable? It is like it expects that the call will always succeed.

Is there a problem that the USB_Polling_Proc routine is called as often as HID_Write? Does it invalidate/reset some flag used by HID_Write, which affects transfer or returned result? :|

Thank you.

Post Reply

Return to “mikroPascal PRO for PIC32 General”