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 to Serial smallest code...

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



Joined: 26 Nov 2005
Posts: 68

View user's profile Send private message

USB to Serial smallest code...
PostPosted: Wed May 30, 2012 8:10 am     Reply with quote

Hello!!!

I am trying to implement a basic usb to serial using a PIC18F4550 and the ex_usb_serial.c example code. As I've understand so far the smallest code that I can use in order to plug my PIC and recognised by windows as a serial device is this one:

Code:

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

#include <usb_cdc.h>

void usb_debug_task(void) {...}

void main(void)
{
   usb_init_cs();

   while (TRUE)
   {
      usb_task();
      usb_debug_task();
   }
}


The code compiles OK and when I am plugging the PIC to my PC (running a 32bit version of Windows 7 Pro) the USB peripheral recognition and driver installation process starts. After a few seconds Windows saying:

"Device driver software was not successfully installed. No driver found"

From the hardware manager window I can see something indicated like a Serial Demo with an indication mark showing that something is going wrong. So, the windows recognising something but they are unable to find a proper driver.

I have to say that I am using my own circuit in a breadboard with a 20Mhz crystal and MpLab 8. Probably the code is missing important things... or there is a hardware problem (probably not)... or just the driver is missing (that I don't thing so as in all the other USB topics they are saying that the driver is already inside windows)... any help?
Ttelmah



Joined: 11 Mar 2010
Posts: 19339

View user's profile Send private message

PostPosted: Wed May 30, 2012 8:37 am     Reply with quote

If you look in the PICC directory (in program files), 'Drivers', 'NT,2000,XP,VISTA,7', there is an .inf file. This is not the driver (the driver is part of Windows), but is the file that _tells_ Widows what driver to use.
So, select 'update driver', Then say that you want to manually select the 'driver', and point Windows at this file (cdc_NT_XPVista.inf). Windows looks at this, and then says 'Oh, now I know what driver to use'. Smile

Best Wishes
Lykos1986



Joined: 26 Nov 2005
Posts: 68

View user's profile Send private message

PostPosted: Wed May 30, 2012 2:46 pm     Reply with quote

Thank you very much!!! I really appreciate your help! Windows recognise the PIC and I will try to implement a few examples tomorrow...
Lykos1986



Joined: 26 Nov 2005
Posts: 68

View user's profile Send private message

PostPosted: Thu May 31, 2012 4:11 am     Reply with quote

I am back again! It seems that probably I am having problem with the detach of the USB.

If I turn on my PC and plug my circuit (the same circuit and code as the first post), windows recognise it as a USB to Serial peripheral and create a serial port that I can use (with TeraTerm for example).

If I unplug my circuit from the PC (with just removing the cable as I can't see the regular icon in the task bar) then if I plug the cable back again I am getting the message:

"USB Device not Recognized. One of the USB devices attached to this computer has malfunctioned, and Windows does not recognize it."

and I can't see the virtual serial port.

In order to start working again with my circuit I have to restart my PC. I guess that I messing around with the Vbus functionality somewhere...
temtronic



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

View user's profile Send private message

PostPosted: Thu May 31, 2012 5:11 am     Reply with quote

Welcome to the Whacky World of Windows and the Useless Serial Bus...
...sadly PCs went way downhill when USB came out and REAL comports were made extinct(so much for 'legacy compliance...).
The good news is, it's NOT a PIC or CCS code problem!
I run XPP-sp2 and Delphi and do not have any usb 'issues' for the past 2 years.I'm NOT 'upgrading' as this is a stable platform. Perhaps others with your combo can reply as to how to get Windows/USB to actually work for you ?
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Thu May 31, 2012 6:42 am     Reply with quote

@temtronic: i totally agree with you. 232=legacy= still the best.
_________________
CCS PCM 5.078 & CCS PCH 5.093
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Thu May 31, 2012 7:27 am     Reply with quote

I guess your board is working as a self-powered USB device. Does it use a VBUS sense pin? Did you check if the device is recognized again after resetting the processor?
Ttelmah



Joined: 11 Mar 2010
Posts: 19339

View user's profile Send private message

PostPosted: Thu May 31, 2012 7:59 am     Reply with quote

Handling disconnect, is one of the really 'fun' (sarcasm mode) bits of USB....

There are two basically different types of disconnect. You have the hardware one (+5v disappears, and if you have the CS line this goes low), and the software one (enumeration). Problem is that most code can't handle the second being 'lost', and unless you handle the first carefully, the device still thinks it is 'enumerated', and leaves you up the Swanee....

The thing that makes the biggest difference to getting this all to work, is to take advantage of DTE. By default terminal programs turn this on when they make a connection. If it goes off, then it signals that the host has lost communication. Combine this with the hardware test, and you can make usb connect/disconnect pretty reliably:

Code:

//You must implement the CS line to the hardware
   int1 usb_cdc_oldconnected=FALSE;

//Then in main
   usb_init_cs();
   do {
      if (usb_attached()) {
         usb_task();
         if (usb_enumerated()){
            if (usb_cdc_carrier.dte_present) {
               if (usb_cdc_oldconnected==FALSE) {
                  printf(usb_cdc_putc,"Your 'connected' message");
                  usb_cdc_oldconnected=TRUE;
               }
               //Then main code testing for kbhit etc...

            }
            else
               usb_cdc_oldconnected=FALSE;
         }
         else
            usb_cdc_oldconnected=FALSE;
      }
   } while (TRUE);
}

Then your code, needs to set DTE when it opens the serial port, and look for the message reply from the unit 'your connected message'. Obviously you make this something sensible for your unit.
If it doesn't get this message, drop DTE,. and raise it again.
In your PC code, if comms are lost, close the port, and open it again. Since this drops and raises DTE, the unit sees the connection again.
Because of the tests for attached, and enumerated, the code will correctly send the message again, if reconnected, both from a hardware loos of connection, and from the port being closed etc..

Best Wishes
Lykos1986



Joined: 26 Nov 2005
Posts: 68

View user's profile Send private message

PostPosted: Sat Jun 02, 2012 6:55 am     Reply with quote

Really good advices from all of you! I will do a few tests but probably in 3 weeks time (as I am flying abroad for a job)... Hopefully I will have good news and interesting findings!

Ttelmah: Definitely I will try your code...

FvM: Yes, it is a self-powered USB device. In the Vbus pin I am having only a capacitor (the one recommended from the datasheet) and the code is the one that I am posting above (first post).
No, I haven't try to reset the processor (and it sounds really stupid)!
JAM2014



Joined: 24 Apr 2014
Posts: 138

View user's profile Send private message

PostPosted: Fri Jun 12, 2015 3:52 pm     Reply with quote

Hi All,

I'm working on making a PIC 18F25K50 based design more robust when it comes to handling disconnects, and I've made a lot of progress based on the code posted in this thread!

My device is not powered from the USB line, and can run doing other things when the USB line is not connected.

I have a VB6 application that communicates with the USB device, and all is working great under 'normal' circumstances. I am asserting DTE whenever the COM port is open, and this seems to be working correctly, with the 'Connected' message being passed correctly.

Now, to the problem I'm seeing. First, let me mention what works. If my VB6 application is not running, or the COM port is not open, I can disconnect and connect the USB line to my device, and the next time the application is run or the COM port is opened, all is well. But, if I disconnect the USB line while the COM port is open I can never get USB communications to work again until I power cycle my device. This is in spite of the fact that the USB COM port shows up in the Windows 7 Device Manager every time my device is powered and the USB line is plugged in, even though my application may not be able to communicate with it.....

I'm thinking that this might actually be some sort of a Windows-side issue, but I'm not sure?

For completeness, here is the code in the top of Main() that is dealing with the USB communications:

Code:

   //Here we initialize the hardware USB cable detection line.....
   usb_init_cs();

     while (1)
   {
      if (usb_attached())
      {
         usb_task();
         if (usb_enumerated())
         {
             if (usb_cdc_carrier.dte_present)
            {
               if (usb_cdc_oldconnected==FALSE)
               {
                        printf(usb_cdc_putc,"USB Connected Msg!");
                  fprintf(Console, "USB Connected Msg!\n\r"); //debug print Msg....
                        usb_cdc_oldconnected=TRUE;
                     }

                  if (usb_cdc_kbhit())
                 {


Any thoughts on what might be going on?

Thanks,

Jack
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