USB Interrupts and Attach Detection
Hi!
I'm using in my project the USB Host Stack library for communicate a PIC24F with a generic client device. I'm using the circuit attached in the image, based in the datasheet's recomendation (except for the A/D pin tied to resistor, because i don´t know what to do with this signal).
Just for start, I'm monitoring the different events in the "USB_ApplicationEventHandler" and turning on some LEDS, just for give me an idea about what is happening in the system. I was expecting that the indicator corresponding with the 'EVENT_GENERIC_ATTACH' will turning on when I connect my device, but nothing happens.
For this reason, I started to check the 'usb_host.c', where is "the heart" of the stack. With some similar tests, I realized that the main state machine in the USBTasks() function, came and stay in the STATE_DETACHED and SUBSTATE_WAIT_FOR_DEVICE. In this moment, the only way that the system can jump to the next stage (STATE_ATTACHED) is with the 'U1IRbits.ATTACHIF' interrupt, but even connecting the device this interruption is never generated. Even, I tried to place in the circuit a fixed "emulator" of a full speed connected device, with a pullup 1.5K resistor from D+ to Vbus, but still nothing.
According to my readings, this interruption is generated by hardware (doesn't depend of the stack) when the USB OTG module detects that the bus state is not SE0 and there has been no bus activity for 2.5 microseconds. The curious is that I'm also monitoryng the SE0 signal with the U1CONbits.SE0, and I can see how this signal turn OFF when i connect the device to the bus, and keep in this way until I disconnect, but the interruption still not being generated.
This is my code:
#include <stdio.h>
#include <stdlib.h>
#include "GenericTypeDefs.h"
#include "HardwareProfile.h"
#include "usb_config.h"
#include "USB/usb.h"
#include "USB/usb_host_generic.h"
_CONFIG1(JTAGEN_OFF & GCP_OFF & GWRP_OFF & ICS_PGx1 & FWDTEN_OFF )
_CONFIG2(IESO_OFF & PLLDIV_DIV2 & PLL96MHZ_ON & FNOSC_FRCPLL & FCKSM_CSDCMD & OSCIOFNC_ON & POSCMOD_NONE)
_CONFIG3( SOSCSEL_IO)
BYTE deviceAddress;
BOOL InitializeSystem ( void ){
_COSC=1;
_NOSC=1;
_RCDIV=0;
_CPDIV=3;
_PLLEN=1;
_PCFG = 0xffff;
_TRISA0= 0; _TRISA1= 0; _TRISA2= 0; _TRISA3= 0; _TRISA4= 0;
_TRISB0= 0; _TRISB1= 0; _TRISB2= 0; _TRISB3= 0; _TRISB4= 0;
_TRISB5= 0; _TRISB7= 0; _TRISB8= 0; _TRISB9= 0; _TRISB13= 0;
_TRISB14= 0; _TRISB15= 0;
return TRUE;} // InitializeSystem
BOOL USB_ApplicationEventHandler ( BYTE address, USB_EVENT event, void *data, DWORD size ){
// Handle specific events
switch ( (INT)event ) {
case EVENT_GENERIC_ATTACH:
if (size == sizeof(GENERIC_DEVICE_ID)) {
deviceAddress = ((GENERIC_DEVICE_ID *)data)->deviceAddress;
_LATA0=1;
return TRUE; }
break;
case EVENT_GENERIC_DETACH:
_LATA1=1;
deviceAddress = 0;
return TRUE;
case EVENT_GENERIC_TX_DONE: // Not relevant in this moment.
case EVENT_GENERIC_RX_DONE:
return TRUE;
case EVENT_VBUS_REQUEST_POWER:
_LATA2=1;
return TRUE;
case EVENT_VBUS_RELEASE_POWER: // We aren't keeping track of power.
_LATA3=1;
return TRUE;
case EVENT_HUB_ATTACH:
return TRUE;
break;
case EVENT_UNSUPPORTED_DEVICE:
_LATB1=1;
return TRUE;
break;
case EVENT_CANNOT_ENUMERATE:
_LATB2=1;
return TRUE;
break;
case EVENT_CLIENT_INIT_ERROR:
return TRUE;
break;
case EVENT_OUT_OF_MEMORY:
return TRUE;
break;
case EVENT_UNSPECIFIED_ERROR: // This should never be generated.
_LATB3=1;
return TRUE;
break;
case EVENT_SUSPEND:
case EVENT_DETACH:
case EVENT_RESUME:
case EVENT_BUS_ERROR:
_LATB4=1;
return TRUE;
break;
default:
break; }
return FALSE;
} // USB_ApplicationEventHandler
int main(int argc, char** argv) {
// Initialize the processor and peripherals.
if ( InitializeSystem() != TRUE ) {
while (1); } //Stay here
if ( USBHostInit(0) == TRUE ) {
_LATB5 = 1;
}
else
{
while (1); //Stay here
}
while(1){ //Infinite cicle
USBHostTasks(); //USB state machine
_LATB7=_JSTATE; //Monitorying the state of the bus
_LATB8=_SE0;
}
return (EXIT_SUCCESS);}
The results when the device is conected, is JSTATE=1, SE0=0, but the 'U1IRbits.ATTACHIF' interrupt is never activated, and the Stack stay machine stay in the STATE_DETACHED and SUBSTATE_WAIT_FOR_DEVICE.
Any idea about what could be the problem? Something is missing in my "test code"?
Thanks.
Attached Image(s)
