• PIC24 Topics
  • Why does USBHostInit() functions returns false?
2020/11/21 04:47:37
Hi everyone,
I'm trying to make working the usb host hid mouse of the mla on a PIC24FJ64GB002 on a breadboard. So I've adapted the code of the example because I don't have all the required HW (LCD for example) and I adapted it to output some states on LEDs.
If I'm posting here, it means I have not made it working yet.
Actually, I'm stucked at the very beggining of the program. The USBHostInit() function actually returns false, which means that memory could not be allocated to the endpoint0 of the host, according to the code and the mla usb documentation.
Has someone been through this problem before?
As I'm new to PIC programming, I'm not aware of every subtility that exist in this field. However I understood that Oscillator configuration seems to be a pretty big deal for USB management.
As I don't own crystal oscillator, I've decided to cofnigure internal oscillator, as described on PIC24FJGB004 family reference manual.
The configuration bits are below. I think it's a good configuration. I get 32MHz oscillator from FRC oscillator aided with PLL, which means FCY is 16MHz.
#pragma config DSWDTPS = DSWDTPS3       // DSWDT Postscale Select (1:128 (132 ms))
#pragma config DSWDTOSC = LPRC          // Deep Sleep Watchdog Timer Oscillator Select (DSWDT uses Low Power RC Oscillator (LPRC))
#pragma config RTCOSC = LPRC            // RTCC Reference Oscillator  Select (RTCC uses Low Power RC Oscillator (LPRC))
#pragma config DSBOREN = OFF            // Deep Sleep BOR Enable bit (BOR disabled in Deep Sleep)
#pragma config DSWDTEN = OFF            // Deep Sleep Watchdog Timer (DSWDT disabled)

#pragma config WPFP = WPFP0             // Write Protection Flash Page Segment Boundary (Page 0 (0x0))
#pragma config SOSCSEL = SOSC           // Secondary Oscillator Pin Mode Select (SOSC pins in Default (high drive-strength) Oscillator Mode)
#pragma config WUTSEL = LEG             // Voltage Regulator Wake-up Time Select (Default regulator start-up time used)
#pragma config WPDIS = WPDIS            // Segment Write Protection Disable (Segmented code protection disabled)
#pragma config WPCFG = WPCFGDIS         // Write Protect Configuration Page Select (Last page and Flash Configuration words are unprotected)
#pragma config WPEND = WPENDMEM         // Segment Write Protection End Page Select (Write Protect from WPFP to the last page of memory)

#pragma config POSCMOD = NONE           // Primary Oscillator Select (Primary Oscillator disabled)
#pragma config I2C1SEL = PRI            // I2C1 Pin Select bit (Use default SCL1/SDA1 pins for I2C1 )
#pragma config IOL1WAY = OFF            // IOLOCK One-Way Set Enable (The IOLOCK bit can be set and cleared using the unlock sequence)
#pragma config OSCIOFNC = OFF           // OSCO Pin Configuration (OSCO pin functions as clock output (CLKO))
#pragma config FCKSM = CSDCMD           // Clock Switching and Fail-Safe Clock Monitor (Sw Disabled, Mon Disabled)
#pragma config FNOSC = FRCPLL           // Initial Oscillator Select (Fast RC Oscillator with Postscaler and PLL module (FRCPLL))
#pragma config PLL96MHZ = ON            // 96MHz PLL Startup Select (96 MHz PLL Startup is enabled automatically on start-up)
#pragma config PLLDIV = NODIV           // USB 96 MHz PLL Prescaler Select (Oscillator input used directly (4 MHz input))
#pragma config IESO = OFF               // Internal External Switchover (IESO mode (Two-Speed Start-up) disabled)

#pragma config WDTPS = PS1              // Watchdog Timer Postscaler (1:1)
#pragma config FWPSA = PR32             // WDT Prescaler (Prescaler ratio of 1:32)
#pragma config WINDIS = OFF             // Windowed WDT (Standard Watchdog Timer enabled,(Windowed-mode is disabled))
#pragma config FWDTEN = OFF             // Watchdog Timer (Watchdog Timer is disabled)
#pragma config ICS = PGx1               // Emulator Pin Placement Select bits (Emulator functions are shared with PGEC1/PGED1)
#pragma config GWRP = ON                // General Segment Write Protect (Writes to program memory are disabled)
#pragma config GCP = OFF                // General Segment Code Protect (Code protection is disabled)
#pragma config JTAGEN = OFF             // JTAG Port Enable (JTAG port is disabled)
Moreover, my power management seems to be OK too:
- wiring respected for Vdd (3.3V) and Vss (GND)
- Vbus is 5V and Vusb is 3.3V
Any idea what's going on ?
2020/11/21 05:13:51
I've not used that device, but I am extreme;ly sceptical it is possible to implement a USB host with the internal oscillator. A host requires a very precise clock.
If you are brand new to PICs, you should be starting with something much simpler. A USB host is a very complex device.
2020/11/21 09:49:49
A USB host is a very complex device.

Rubbish, it's quite simple, easier than a device. The smaller pics can run with no ext oscillator, though limited to USB 1.0. Even as a device they are limited to USB2.0 full speed.
Understanding the USB protocal and structure in detail will help more. It's a very strange, and quite poor means of communication.
2020/11/22 06:22:37
Hi everyone!
Finally got an answer to my problem.
UsbHostInit() will fail when the program cannot allocate enough heap memory for this application.
The solution I found is to allow 1024 bytes of heap memory in the compiler options in MPLAB. It may be too much and could certainly be reduced but the code is now working, which is great!!
Hope it will be useful to people trying to program usb host apps.
2020/11/22 12:52:36
The FAT file system code has something similar where you need to specify the heap size greater than default.
2020/11/23 08:18:20
Good to see those that are too thick to understand the usb host code giving a 1 start rating, eh susan?
2020/11/29 03:46:40
[corrected based on Ric feedback below]Hi Ric,
I've not used that device, but I am extreme;ly sceptical it is possible to implement a USB host with the internal oscillator. A host requires a very precise clock.

You are quite right RIC. Extract from USB specification :
The USB 2.0 Specification defines the tolerances for data rates on the USB bus, including all drift sources.  For low-speed communications, the data rate is specified to be 1.5 Mbps +/- 1.5%.  For full-speed communications, the data rate is specified to be 12 Mbps +/- 0.25%.
If an external clock source is used as the USB clock, it must meet the above specifications for reliable USB communications.  The internal oscillator on the device family can be used for either low or full-speed USB.  If the internal oscillator is used for full-speed USB communications, 'clock recovery' must be enabled to ensure the data rate tolerance specification of +/- 0.25%.
Hi aurelien,
If you look at the PIC24FJ64GB002 specification, table 29-19, you' ll see that the oscillator has a tolerance of : 
-1.25% to +1% over the -40C to +85C and 3V to 3.6V Vdd
It means that if you only use as a device a Mouse which is typically a Low Speed device you should be OK because the USB low speed requires +/-1.5% precision on the clock  
2020/11/29 04:56:36
Isn't all that "clock tuning" for slave devices to lock to the host?
The question was about a host device.
2020/11/29 05:09:04
Thanks for your answer and the highlights you brought.
I did succeed at implementing a usb host application with PIC24FJ64GB002 without an external oscillator. Actually, this app just converts mouse button click to gate. It seems that for this particular application, an external clock is not necessary, at least for a hobby purpose.
Do you know what errors or weird behaviour would happen with temperature drift?
© 2021 APG vNext Commercial Version 4.5

Use My Existing Forum Account