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

Getting ex_usb_mouse.c HID example working
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
lsteele



Joined: 02 Jan 2007
Posts: 18

View user's profile Send private message

Getting ex_usb_mouse.c HID example working
PostPosted: Sun Feb 25, 2007 2:10 pm     Reply with quote

Hi there,

I'm trying to get the supplied ex_usb_mouse.c example working. I don't have the project board so I've built a circuit with an 18f2450, with a 4Mhz ceramic resonator. I've used the NES controller interface at http://www.zero-soft.com/HW/USB_NES/ as a starting point.

So far the circuit flashes the attached leds at the correct speed and outputs serial data which I can see in a terminal window, so I know the MCU's running ok. But when connect to the usb port windows says Device not recognised.

I'm guessing that the oscillator configuration isn't correct, but I'm not sure how to proceed. (No end of pouring over the datasheet makes it any clearer!)

Here's the circuit (doesn't show the serial connection):



And here's the code:

Code:


//set to 1 to use a PIC with an internal USB Peripheral
//set to 0 to use a National USBN960x peripheral
#define __USB_PIC_PERIF__ 1

#include "18f2450.h"
//#fuses NOPROTECT, /*BROWNOUT*/, NOWDT/*WDT1024*/, XTPLL, BORV20, NOLVP, PLL4, CPUDIV4, VREGEN, PUT, /*ADDED:*/ NODEBUG//, USBDIV,
#fuses NOPROTECT, BROWNOUT, HS, BORV20, NOLVP, PLL5, CPUDIV4, VREGEN, PUT
#use delay(clock=1000000)

  ////SLOW SPEED
  //#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV3,VREGEN
  //#use delay(clock=24000000)
  //#define USB_USE_FULL_SPEED  FALSE

#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

/////////////////////////////////////////////////////////////////////////////
//
// CCS Library dynamic defines.  For dynamic configuration of the CCS Library
// for your application several defines need to be made.  See the comments
// at usb.h for more information
//
/////////////////////////////////////////////////////////////////////////////
#DEFINE USB_HID_DEVICE  TRUE  //Tells the CCS PIC USB firmware
                              //to include HID handling code.

//turn on EP1 for IN interrupt transfers.  (IN = PIC -> PC)
#define USB_EP1_TX_ENABLE  USB_ENABLE_INTERRUPT
#define USB_EP1_TX_SIZE 8     //max packet size of this endpoint

/////////////////////////////////////////////////////////////////////////////
//
// If you are using a USB connection sense pin, define it here.  If you are
// not using connection sense, comment out this line.  Without connection
// sense you will not know if the device gets disconnected.
//       (connection sense should look like this:
//                             100k
//            VBUS-----+----/\/\/\/\/\----- (I/O PIN ON PIC)
//                     |
//                     +----/\/\/\/\/\-----GND
//                             100k
//        (where VBUS is pin1 of the USB connector)
//
/////////////////////////////////////////////////////////////////////////////
///only the 18F4550 development kit has this pin
#if __USB_PIC_PERIF__ && defined(__PCH__)
 #define USB_CON_SENSE_PIN PIN_B2
#endif

/////////////////////////////////////////////////////////////////////////////
//
// Include the CCS USB Libraries.  See the comments at the top of these
// files for more information
//
/////////////////////////////////////////////////////////////////////////////

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

/////////////////////////////////////////////////////////////////////////////
//
// Configure the demonstration I/O
//
/////////////////////////////////////////////////////////////////////////////
#define LED1 pin_c0
#define LED2 PIN_c1
#define LED3 PIN_c2
#DEFINE BUTTON PIN_A4
#define LED_ON output_low
#define LED_OFF output_high

void flashLed()
{
   char i;

   for(i=0; i<4> 10) {
            if (++x_seq>=MOUSE_SEQUENCE_STEPS) {x_seq=0;}
            if (++y_seq>=MOUSE_SEQUENCE_STEPS) {y_seq=0;}
            count=0;
         }
         delay_ms(10);
      }
   }
}



Any suggestions are gratefully received!
Ttelmah
Guest







PostPosted: Sun Feb 25, 2007 2:58 pm     Reply with quote

Your problem is the 10MHz resonator.
Look at the data sheet. Look at the section on the oscillator configurations, and note the values used for USB operation. Note that 10MHz, is _not_ there. The problem is that the USB system, _must_ be fed with 4MHz. There are prescalers, available, but only _integer_ ones. You can't develop 4MHz from the 10MHz source.
Be also aware of how tight the timing requirements are for USB. It is a common problem with commercial kit using resonators rather than crystals, to go 'out of spec' with quite small temperature changes. Safer to use a well specified crystal.

Best Wishes
Ttelmah
Guest







PostPosted: Sun Feb 25, 2007 3:45 pm     Reply with quote

Sorry, was reading your clock speed, not your resonator speed. However for a 4MHz resonator, you need PLL1, not PLL5. At present you are taking 4Mhz, and dividing this by 5. to feed the USB stuff. You are also taking the same clock, and dividing this to feed the CPU. You need:

XTPLL, PLL1, CPUDIV4

You need 'PLL', or the CPU clock will come directly from the 4MHz feed, not from the higher frequency clock. CPUDIV4, actually divides by 6 when feeding from the USB clock (don't ask!...). This will then give a CPU clock at 16Mhz, not the 10 you have specified, so change the 'clock' statement to reflect this.
Also, increase the size of the capacitor on Vusb. There are problems, unless this is increased on many chips...

Best Wishes
lsteele



Joined: 02 Jan 2007
Posts: 18

View user's profile Send private message

PostPosted: Sun Feb 25, 2007 6:20 pm     Reply with quote

Ok,

I've now changed the fuses line to

Code:
#fuses NOPROTECT, BROWNOUT, BORV20, NOWDT, NOLVP, VREGEN, PUT, XTPLL, PLL1, CPUDIV4


and I've increased the capacitance on Vusb to 320nF

...but unfortunately I'm getting exactly the same result, namely "USB device not recognised"!

Any other ideas?

Thanks,

Luke
Ttelmah
Guest







PostPosted: Mon Feb 26, 2007 4:23 am     Reply with quote

OK. Clock rates should be right. However question still remains about your resonator. Are you sure this is 'in spec' for USB?. It is required to be withing 0.05% to be in spec (and there is basically no 'margin' on this).
I wasn't talking about a small increase in the capacitor, like this!. Do a search for threads here on USB problems. Some people have found much larger capacitor combinations are necessary to get it working reliably!...
Also, check the obvious (pin order round the USB plug etc...).

Best Wishes
meereck



Joined: 09 Nov 2006
Posts: 173

View user's profile Send private message

PostPosted: Mon Feb 26, 2007 11:42 am     Reply with quote

On Microchip phorum I read that the second Vss should be connected as well.
Anyway do you use the USB sense pin? Usually the pin is not used, so comment out all these 3 lines.
Code:
#if __USB_PIC_PERIF__ && defined(__PCH__)
 #define USB_CON_SENSE_PIN PIN_B2
#endif


Another issue I have noticed is cpu speed. As far as I know : if you want to use integrated USB your cpu should run at 24MHz(low speed) or 48Mhz(high speed).
My fuses for 24Mhz crystal and high speed device(for 4Mhz crystal just change PLL6 to PLL1):
Code:
 #fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL6,CPUDIV1,VREGEN
  #use delay(clock=48000000)
Ttelmah
Guest







PostPosted: Mon Feb 26, 2007 4:37 pm     Reply with quote

You most certainly do need the other Vss pin. Also the comment about Vsense, is important. However you can run the CPU at any frequency you want (even run it asynchronously off the internal RC clock!). The USB speed doesn't change, and you can run 'full speed', even with a 1MHz CPU clock!. With a 4MHz master oscillator, CPU speeds of 1, 1.33, 2, 4, 16, 24, 32, and 48Mhz are supported from the same oscillator.

Best Wishes
lsteele



Joined: 02 Jan 2007
Posts: 18

View user's profile Send private message

PostPosted: Tue Feb 27, 2007 2:41 pm     Reply with quote

The other VSS pin is now connected, and I've experimented with a 470uF capacitor (as I've seen suggested elsewhere on the forum). And I've commented out the USB sense pin line.

Unfortunately still no success. Basically the pic just outputs "USB connected, waiting for enumaration..." to the termial, then windows says usb device isn't working properly.

I thought I'd try a 20Mhz crystal I've got lying around, but I can't for the life of me work out how to correctly configure the fuses - the datasheet is as clear as mud, frankly.

So I'm open to any other suggestions! I'd also appreciate any links to tutorials explaining how to correctly configure the fuses for desired speeds.

Thanks again - it's appreciated!
rberek



Joined: 10 Jan 2005
Posts: 207
Location: Ottawa, Canada

View user's profile Send private message

PostPosted: Tue Feb 27, 2007 2:44 pm     Reply with quote

What are you using for a driver?
lsteele



Joined: 02 Jan 2007
Posts: 18

View user's profile Send private message

PostPosted: Tue Feb 27, 2007 2:45 pm     Reply with quote

I'm not - it's supposed to be an HID device, which as I understand it doesn't require a driver.
lsteele



Joined: 02 Jan 2007
Posts: 18

View user's profile Send private message

PostPosted: Tue Feb 27, 2007 2:47 pm     Reply with quote

...Oh, and I've also tried using USB Monitor to see what's going on. Here's the output when I plug the pic in:

Code:
000001: Get Status Request (DOWN), 27.02.2007 19:21:27.7656250 +11.7500000
Get status from Device. Data length 0x2 bytes
000002: Control Transfer (UP), 27.02.2007 19:21:27.7656250 +0.0
Pipe Handle: 0x86de7218

 01 00                                             ..
Setup Packet
 80 00 00 00 00 00 02 00                           €.......
Recipient: Device
Request Type: Standard
Direction: Device->Host
Request: 0x0 (GET_STATUS)
Value: 0x0
Index: 0x0
Length: 0x2
000003: Get Status Request (DOWN), 27.02.2007 19:21:27.7968750 +0.0312500
Get status from Device. Data length 0x2 bytes

000004: Control Transfer (UP), 27.02.2007 19:21:27.7968750 +0.0
Pipe Handle: 0x86de7218

 01 00                                             ..
Setup Packet
 80 00 00 00 00 00 02 00                           €.......
Recipient: Device
Request Type: Standard
Direction: Device->Host
Request: 0x0 (GET_STATUS)
Value: 0x0
Index: 0x0
Length: 0x2


Unfortunately this doesn't mean much to me as yet. I'm about to start reading the USB specs to see if they can illuminate this lot, but maybe someone out there knows what this means...
rberek



Joined: 10 Jan 2005
Posts: 207
Location: Ottawa, Canada

View user's profile Send private message

PostPosted: Tue Feb 27, 2007 2:52 pm     Reply with quote

CCS' USB development kit uses this mouse example, and drivers are supplied with the kit. I am at work and do not have access to the kit documentation, but drivers may be required.

If so, you'd have to email CCS and request some.

Its been over a year since I used it, so I do not recall specifically which examples use which drivers. I had no problem getting any of the examples to work.

r.b.
Guest








PostPosted: Sun Mar 04, 2007 12:06 pm     Reply with quote

Ttelmah wrote:
OK. Clock rates should be right. However question still remains about your resonator. Are you sure this is 'in spec' for USB?. It is required to be withing 0.05% to be in spec (and there is basically no 'margin' on this)

Low speed USB purposely has relaxed frequency requirements to allow inexpensive devices to be built. However, the example as provided uses full speed. You might get away with the resonator if it just happens to be on frequency, but for the first time attempt it would be better to use a crystal, to be sure. Once it's working, you can substitute parts to see what you can "get away with".

On my first go, I didn't have the Vusb cap, and it didn't work until I put one in.

The logic for a USB host is such that when it sees one of the USB lines pulled high, the hub interrupts the host and there's a whole conversation that takes place trying to identify what's there. So you can cause an "unknown device" to appear with just a resistor! Fun, but not much help.

The file mouse_desc.h has a "device descriptor" defined in it (as well as a lot more). It is this that you want to see reported in the USB monitor program, as sent from the device. Of course, once you reach that point, you will likely see the mouse going in a circle, too...

Best of luck. This thing does work once it's built right.

Barry
hunt



Joined: 18 Apr 2007
Posts: 2

View user's profile Send private message

Did you get it working?
PostPosted: Wed Apr 18, 2007 12:48 am     Reply with quote

I'm fighting the same problem - code that runs properly on the USB kit, when compiled for the 2450, returns "USB Device not recognized". On the development board (with appropriate header changes), it works great. What was the final outcome here? (I am running out of time to get USB up on this project.)
Thanks!
lsteele



Joined: 02 Jan 2007
Posts: 18

View user's profile Send private message

PostPosted: Wed Apr 18, 2007 4:02 am     Reply with quote

I'm sorry to say that this problem has temporarily defeated me.

After I posted the messages above I decided to have a shot at getting it working with Microchip's C18 compiler. Initially I tried their HID examples, which I also had a lot of trouble with. I did find a website where the author had a pic/usb tutorial based on a modified version of microchip's code (again with c18), and I managed to get that working. But it was using one of the serial usb protocols, not hid. And it wasn't much of a solution as I want to stick with CCS. (I can't find the link, but if I do I'll post it.)

I presume the problem relates to using the 2450 instead of the one in the examples (a 4550 I seem to remember). But it seems to me it should work.

Which isn't a lot of help to you! If you get anywhere with this I'd really like to know how.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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