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 HID via usb type C

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



Joined: 24 Apr 2016
Posts: 9

View user's profile Send private message

USB HID via usb type C
PostPosted: Tue Jan 31, 2017 2:39 pm     Reply with quote

Hello!

I'm trying to connect an PIC18F4550 to a Macbook via USB type C. I'm using example code and self-powered USB circuit (100k between Vcc and USB Vcc, 4k7 between USB Vcc and USB Gnd, etc.).

When I connect PIC to a normal PC (Win10 64bit), it works successfully (the pic is recognised as HID device), but when i connect to a Macbook (Win10 64bit using Boot Camp), i get an error: device descriptor request failed.

The Macbook has only one USB-C connector.

Does anyone know what the problem is? Can HID work via USB-C or on Macbook? Or my sample code is wrong?

I'm using MPLAB X IDE v3.51 and CCS v5.011. The PICKIT2 works.

Thank you!

Code:
#include <18F4550.h>
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)

#define PIN_USB_SENSE   PIN_C0

#define USB_CONFIG_HID_TX_SIZE   2
#define USB_CONFIG_HID_RX_SIZE   2

#include <pic18_usb.h> 
#include <usb_desc_hid.h>   //USB Configuration and Device descriptors for this UBS device
#include <usb.c>        //handles usb setup tokens and get descriptor reports

void main(void)
{
   int8 out_data[USB_CONFIG_HID_TX_SIZE];
   int8 in_data[USB_CONFIG_HID_RX_SIZE];
   int8 send_timer=0;
   int8 send_timer2=0;
   
   usb_init_cs();
   
   
   
   while (TRUE)
   {
      usb_task();
     
      usb_attached();
      usb_enumerated();
     
     
      delay_ms(1);
      send_timer++;
     
      if (send_timer == 500)
      {
          output_toggle(PIN_D1);
          send_timer = 0;
      }
   }
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19295

View user's profile Send private message

PostPosted: Tue Jan 31, 2017 2:48 pm     Reply with quote

Stick a USB-2 bub between the devices.

USB-C just like USB-3, requires such fast switching capabilities from the drivers, that even when it is switched 'down' to USB-2 mode, it can cause signalling problems if the bus impedance is not well matched.

Your code _must_ include usb_task in the loop. It is this that handles the 'housekeeping' for the USB. It only needs to be called at intervals, but it must be called. In this case it might well start working if you do this, since part of the negotiations carried out by the USB-C device will involve it disconnecting, and re-enumerating. Without usb_task being called again, this won't happen.
temtronic



Joined: 01 Jul 2010
Posts: 9149
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Jan 31, 2017 2:54 pm     Reply with quote

Since your PIC with USB code works fine connected to a PC,I'd say the MAC is at fault. Kinda 'old school' logic...
Since I never heard of USB-C I did a quick google. yeesh...the USB-C cable has a chip INSIDE it according to one source.....so who KNOWs what's going on...let alone 'compatibility'.
Last MAC I worked on ,was the originals, did memory upgrades for 10% of what Apple was charging....
Now you're trying to get a MAC to 'emulate' Windows...that should be 'fun'...but IF you had USBView, I'd run it to see what 'USB' devices the MAC thinks you have.

Jay
anemo



Joined: 24 Apr 2016
Posts: 9

View user's profile Send private message

PostPosted: Tue Jan 31, 2017 3:08 pm     Reply with quote

Ttelmah wrote:
Stick a USB-2 bub between the devices.


It means hub? i hope, beacuse I've just tried with passive usb hub, but it doesn't work, unfortunately.

Thank you.
anemo



Joined: 24 Apr 2016
Posts: 9

View user's profile Send private message

PostPosted: Tue Jan 31, 2017 3:17 pm     Reply with quote

temtronic wrote:
Since your PIC with USB code works fine connected to a PC,I'd say the MAC is at fault. Kinda 'old school' logic...
Since I never heard of USB-C I did a quik google. yeesh...the USB-C cable has a chip INSIDE it according to one source.....so who KNOWs what's going on...let alone 'compatibility'.
Last MAC I worked on ,was the originals, did memory upgrades for 10% of what Apple was charging....
Now you're trying to get a MAC to 'emulate' Windows...that should be 'fun'...but IF you had USBView, I'd run it to see what 'USB' devices the MAC thinks you have.

Jay


It sounds like not good. Actually it's not emulating. OS X has a basic app to install Windows... it creates a second partition on your ssd for windows.

Thanks.
Ttelmah



Joined: 11 Mar 2010
Posts: 19295

View user's profile Send private message

PostPosted: Wed Feb 01, 2017 3:19 am     Reply with quote

Add the usb_task to the loop.
This is _required_.

Basically 'usb_task' does two jobs.
On a system not using interrupts it handles everything, and must be called very frequently (max about 10mSec between calls). On a standard system using interrupts to handle the data transactions it must still be called perhaps a couple of times a second to do the USB 'housekeeping'.
Now on USB-C hosts, the host actually disconnects and re-connects during the initialisation, as it 'works out' what capabilities the device has. USB_task must be called to handle this. Since you only call it once, I'd expect it not to work.

I'm typing this on a Retina Imac, with USB-C ports, and currently debugging a PIC system, using an ICD, two USB connections, and virtual W-XP, W7 & W10, to test the code. All run fine. The PIC is happily handled by all four OS's (the Mac included).
One other thing though, is a comment I made a few days ago. When connecting to these higher speed busses, it does show any impedance problems more than on standard USB busses. Systems that did work fine sometimes give problems and may need small 'series' resistors in the two USB lines, to reduce transmission line problems. However this shouldn't apply if going via a hub.
anemo



Joined: 24 Apr 2016
Posts: 9

View user's profile Send private message

PostPosted: Wed Feb 01, 2017 10:58 am     Reply with quote

I call usb_task() inside while(true) loop with 1 ms interval, as you can see at embedded code, or you thought something else?

Thanks.
Ttelmah



Joined: 11 Mar 2010
Posts: 19295

View user's profile Send private message

PostPosted: Wed Feb 01, 2017 11:24 am     Reply with quote

OK.

I was not reading this as being the loop.

There are several things wrong then.

usb_enumerated is a routine you call to _test_ if the system is enumerated. The same applies for usb_attached.

You should be testing usb_attached, and if it is attached, then test if it is enumerated. Only if it is both attached and enumerated, then you loop calling task _while_ these remain true.

Look at the examples, and re-write your code like them.
anemo



Joined: 24 Apr 2016
Posts: 9

View user's profile Send private message

PostPosted: Wed Feb 01, 2017 12:36 pm     Reply with quote

I did, but nothing changed (still "device descriptor request failed"). I tried with and without hub also. Remark: i have to use apple usb-C/usb-A adapter between mac and pic, because not have special cable.. it may be causing something.

This is the code i've just used. (According to example, the usb_task() is the first, then attached, enum..)

Code:
#include <18F4550.h>
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)

#define PIN_USB_SENSE   PIN_C0

#define USB_CONFIG_HID_TX_SIZE   2
#define USB_CONFIG_HID_RX_SIZE   2

#include <pic18_usb.h> 
#include <usb_desc_hid.h>   //USB Configuration and Device descriptors for this UBS device
#include <usb.c>        //handles usb setup tokens and get descriptor reports

void main(void)
{
   delay_ms(1000);
   
   int8 out_data[USB_CONFIG_HID_TX_SIZE];
   int8 in_data[USB_CONFIG_HID_RX_SIZE];
   int8 send_timer=0;
   
   usb_init_cs();
   
   while (TRUE)
   {
       usb_task();
       
       if (usb_attached())
       {
           if (usb_enumerated())
           {

                send_timer++;

                if (send_timer == 500)
                {
                    output_toggle(PIN_D1);
                    send_timer = 0;
                }
           }
       }

        delay_ms(1);
           
   }
}
[/code]
anemo



Joined: 24 Apr 2016
Posts: 9

View user's profile Send private message

PostPosted: Fri Feb 03, 2017 2:50 am     Reply with quote

Problem solved. The capacitor between gnd and VUSB pin was wrong, i fixed it and now Mac can recognise pic. Thank you.
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