3
\$\begingroup\$

I'm transmitting a buffer array from inbuilt USB of PIC 18F controllers and sending this to a GUI. I am able to detect the device properly (enumeration). But the GUI crashes some times, without any reason.

I assume it is a transmission/interrupt problem as whenever I'm sending a constant number it does not crash. But whenever I'm doing an ADC operation and sending the corresponding value via USB, it crashes.

This made up my assumption that USB transmission or interrupt handling needs to be handled properly.

I'm new to the USB protocol and tried with this working code.

I have no problem regarding enumeration, but when transmits the crashing occurs.

The format of the code is:

 main()
{   usbinit(); // usb initialization

       while(1)

{         
      x=do_adc();
      UsbTasks(); // does usb works    
      yourtasks();//sends the buffer 

}

}

I would like to understand what might be the cause behind this anomalous behavior.

As noted below in the comments the USB stack is set for polled configuration. How should I manage the ADC read?

If I do:

while(1)

    {         

          UsbTasks(); // does usb works
           x=do_adc();    
          yourtasks();//sends the buffer 

    }

Will it make any change? Or is this not the best way to send ADC values?

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Do you have the stack set for polled or interrupt mode? If it's in polled mode the ADC read is probably stalling the call to UsbTasks for too long. \$\endgroup\$
    – PeterJ
    Feb 12, 2014 at 6:46
  • \$\begingroup\$ Thanks a lot @PeterJ I have to check that.I'm browsing through the code right now. \$\endgroup\$
    – Rookie91
    Feb 12, 2014 at 7:13
  • \$\begingroup\$ If the GUI on the PC crashes that sounds like a software problem, not an electronics one. \$\endgroup\$
    – David
    Feb 12, 2014 at 7:41
  • \$\begingroup\$ @PeterJ The stack is set for polled configuration. How should i manage the adcread then. \$\endgroup\$
    – Rookie91
    Feb 12, 2014 at 9:54

1 Answer 1

4
\$\begingroup\$

According to the USB CDC Class on an Embedded Device application note by Microchip which would mostly apply to other device classes as well:

The USBTasks routine may be used in a polled, cooperative manner as demonstrated. If so, nothing in the main loop should block for more than a few microseconds or events may be lost.

So the main two solutions that come to mind are:

  • Rather than wait for the ADC conversion to finish start the conversion and then use a conversion complete interrupt so that the main processing loop can continue while the ADC is taking a sample.

  • Set the USB stack to use the interrupt-driven mode to remove the requirements to call UsbTasks so often.

For most applications I'd say the latter is probably the easier solution so you don't have to worry so much if your main loop stalls a little while.

\$\endgroup\$
5
  • \$\begingroup\$ adc conversion will take some microseconds i trust :). Could that be also a problem. \$\endgroup\$
    – Rookie91
    Feb 12, 2014 at 12:25
  • \$\begingroup\$ It depends on the part and ADC setup, but I think the typical fastest speed for most PIC18 parts is around 100ksps, so that would be 10us. \$\endgroup\$
    – PeterJ
    Feb 12, 2014 at 12:35
  • \$\begingroup\$ That Won't cause any problem hopefully right :) \$\endgroup\$
    – Rookie91
    Feb 12, 2014 at 12:41
  • 1
    \$\begingroup\$ The Microchip docs do say a 'few' microseconds which while not precise I'd interpret as two or three, maybe a little more so it could well do. There can also be some disavantages to running an ADC at the maximum rate so you'd really be best to avoid reading it in the polled mode and either use interrupts for the USB or ADC side. \$\endgroup\$
    – PeterJ
    Feb 12, 2014 at 12:48
  • \$\begingroup\$ Thank you.I still have a doubt , may be it's due to not so strong basics Forgive me for that .But i would like to ask that if the controller leaves main() and Go to ISR() and if the ISR takes more time will that be a problem>? Or will the main() execute the USBtasks with out any problem. \$\endgroup\$
    – Rookie91
    Feb 12, 2014 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.