Hi
I'm working on a project to host a HUB under TI_RTOS environment. But I'm facing a lot of problems.
I'm using DK-TM4C129X evaluation kit.
,CCS version 5.4.0
and tirtos_1_21_00_09
I'm trying to imitate the Mouse host example.
my problem with the below is that when i connect the hub it enters the hwi_handler function but after a while it goes to exit function.
thanks in advance
void taskFxnHUB (UArg arg0, UArg arg1)
{
USBHub_State state;
printf("HUB connectivity\n");
while (TRUE) {
/* Block while the device is NOT connected to the USB */
USBHub_waitForConnect(BIOS_WAIT_FOREVER);
/* Determine the status of the mouse */
USBHub_getState(&state);
System_flush();
Task_sleep(100);
}
}
/*
* ======== main ========
*/
Int main(Void)
{
Task_Handle task;
Task_Params taskParams;
Error_Block eb;
/* Call board init functions. */
Board_initGeneral();
// Board_initEMAC();
Board_initGPIO();
// Board_initI2C();
// Board_initSDSPI();
// Board_initSPI();
// Board_initUART();
Board_initUSB(Board_USBHOST);
// Board_initUSBMSCHFatFs();
// Board_initWatchdog();
/* Turn on user LED */
GPIO_write(Board_LED0, Board_LED_ON);
printf("Starting the USB HUB Host example\nSystem provider is set"
" to SysMin. Halt the target and use ROV to view output.\n");
USBHub_init();
Error_init(&eb);
Task_Params_init(&taskParams);
taskParams.priority = 1;
taskParams.stackSize = 1024;
task = Task_create(taskFxnHUB, &taskParams, &eb);
if (task == NULL) {
System_abort("Can't create main task");
}
/* Start BIOS */
BIOS_start();
return (0);
}
typedef volatile enum {
USBHub_NO_DEVICE = 0,
USBHub_INIT,
USBHub_CONNECTED,
USBHub_UNKNOWN,
USBHub_POWER_FAULT
} USBHub_USBState;
#define HCDMEMORYPOOLSIZE 128 /* Memory for the Host Class Driver */
/* Static variables and handles */
static volatile USBHub_USBState state;
static UChar memPoolHCD[HCDMEMORYPOOLSIZE];
/* OS primitives */
static GateMutex_Handle gateUSBWait;
static GateMutex_Handle gateUSBLibAccess;
static Semaphore_Handle semUSBConnected;
static int16_t u16HwiCalls = 0;
tHubInstance * hubInstance;
/* Prototypes */
static Void usbHCDEvents(void *cbData);
static Void USBHub_hwiHandler(UArg arg0);
void
HubCallback(tHubInstance *psHubInstance, uint32_t ui32Event,
uint32_t ui32MsgParam, void *pvMsgData);
/* MACRO to create a generic USB event host driver */
DECLARE_EVENT_DRIVER(USBHub_eventDriver, 0, 0, usbHCDEvents);
/* A list of available Host Class Drivers */
static tUSBHostClassDriver const * const usbHCDDriverList[] = {
&g_sUSBHIDClassDriver,
&USBHub_eventDriver
};
/* Variable containing the number of HCDs in usbHCDDriverList */
const ULong numHostClassDrivers =
sizeof(usbHCDDriverList) / sizeof(tUSBHostClassDriver *);
/*
* ======== cbHubHandler ========
* Callback handler for the USB stack.
*
* Callback handler call by the USB stack to notify us on what has happened in
* regards to the HUB.
*
* @param(cbData) A callback pointer provided by the client.
*
* @param(event) Identifies the event that occurred in regards to
* this device.
*
* @param(eventMsgData) A data value associated with a particular event.
*
* @param(eventMsgPtr) A data pointer associated with a particular event.
*
*/
static void cbHubHandler(tHubInstance *psHubInstance, uint32_t event,
uint32_t eventMsg, Void *eventMsgPtr)
{
printf("\ncbHubHandler\n");
/* Determine what event has happened */
switch (event) {
case USB_EVENT_CONNECTED:
/* Set the HUB state for initialization */
state = USBHub_INIT;
Semaphore_post(semUSBConnected);
break;
case USB_EVENT_DISCONNECTED:
/* Set the HUB state as not connected */
state = USBHub_NO_DEVICE;
break;
default:
break;
}
}
/*
* ======== usbHCDEvents ========
* Generic USB Host Class Driver event callback.
*
* This callback is called to notify the application that a unknown device was
* connected. (e.g. It wasn't a mouse)
*/
static Void usbHCDEvents(void *cbData)
{
tEventInfo *pEventInfo;
printf("\nusbHCDEvents\n");
/* Cast this pointer to its actual type. */
pEventInfo = (tEventInfo *)cbData;
switch (pEventInfo->ui32Event) {
case USB_EVENT_UNKNOWN_CONNECTED:
/* An unknown device was detected. */
state = USBHub_UNKNOWN;
break;
case USB_EVENT_DISCONNECTED:
/* Unknown device has been removed. */
state = USBHub_NO_DEVICE;
break;
case USB_EVENT_POWER_FAULT:
/* No power means no device is present. */
state = USBHub_POWER_FAULT;
break;
default:
break;
}
}
/*
* ======== USBMH_hwiHandler ========
* This function calls the USB library's device interrupt handler.
*/
static Void USBHub_hwiHandler(UArg arg0)
{
u16HwiCalls++;
//printf("\nUSBHub_hwiHandler \n");
USB0HostIntHandler();
}
/*
* ======== serviceUSBHost ========
* Task to periodically service the USB Stack
*
* USBHCDMain handles the USB Stack's state machine. For example it handles the
* enumeration process when a device connects.
* Future USB library improvement goal is to remove this polling requirement..
*/
static Void serviceUSBHost(UArg arg0, UArg arg1)
{
UInt key;
while (TRUE) {
printf("\nserviceUSBHost \n");
key = GateMutex_enter(gateUSBLibAccess);
USBHCDMain();
GateMutex_leave(gateUSBLibAccess, key);
/* Future enhancement to remove the Task_sleep */
Task_sleep(10);
}
}
/*
* ======== USBMH_init ========
*/
Void USBHub_init(Void)
{
Hwi_Handle hwi;
Hwi_Params H_params;
Error_Block eb;
Task_Handle task;
Task_Params taskParams;
Semaphore_Params semParams;
printf("\nUSBHub_init\n");
Error_init(&eb);
/* Initialize the USB stack for host mode. */
USBStackModeSet(0, eUSBModeHost, NULL);
/* Register host class drivers */
USBHCDRegisterDrivers(0, usbHCDDriverList, numHostClassDrivers);
//
// Open a hub instance and provide it with the memory required to hold
// configuration descriptors for each attached device.
//
hubInstance = USBHHubOpen(cbHubHandler);
if(!hubInstance) {
System_abort("Error initializing the HUB Host");
}
Error_init(&eb);
/* Install interrupt handler */
/* dont interrupt it self */
Hwi_Params_init(&H_params);
H_params.maskSetting = Hwi_MaskingOption_SELF;
H_params.enableInt = FALSE;
hwi = Hwi_create(INT_USB0, USBHub_hwiHandler, NULL, &eb);
if (hwi == NULL) {
System_abort("Can't create USB Hwi");
}
/* Initialize USB power configuration */
USBHCDPowerConfigInit(0, USBHCD_VBUS_AUTO_HIGH | USBHCD_VBUS_FILTER);
/* Enable the USB stack */
USBHCDInit(0, memPoolHCD, HCDMEMORYPOOLSIZE);
/* RTOS primitives */
Semaphore_Params_init(&semParams);
semParams.mode = Semaphore_Mode_BINARY;
semUSBConnected = Semaphore_create(0, &semParams, &eb);
if (semUSBConnected == NULL) {
System_abort("Could not create USB Connect semaphore");
}
gateUSBLibAccess = GateMutex_create(NULL, &eb);
if (gateUSBLibAccess == NULL) {
System_abort("Could not create USB Wait gate");
}
gateUSBWait = GateMutex_create(NULL, &eb);
if (gateUSBWait == NULL) {
System_abort("Could not create USB Wait gate");
}
/*
* Note that serviceUSBHost() should not be run until the USB Stack has been
* initialized!!
*/
Task_Params_init(&taskParams);
taskParams.priority = Task_numPriorities - 1;
taskParams.stackSize = 768;
task = Task_create(serviceUSBHost, &taskParams, &eb);
if (task == NULL) {
System_abort("Can't create USB service task");
}
}
//*****************************************************************************
//
// This is the callback from the USB HUB mouse handler.
//
// pvCBData is ignored by this function.
// ui32Event is one of the valid events for a mouse device.
// ui32MsgParam is defined by the event that occurs.
// pvMsgData is a pointer to data that is defined by the event that occurs.
//
// This function will be called to inform the application when a mouse has
// been plugged in or removed and any time mouse movement or button pressed
// is detected.
//
// This function will return 0.
//
//*****************************************************************************
void
HubCallback(tHubInstance *psHubInstance, uint32_t ui32Event,
uint32_t ui32MsgParam, void *pvMsgData)
{
static int x=0;
x++;
printf("\nHubCallback calls= %d\n",x);
}
/*
* ======== USBMH_waitForConnect ========
*/
Bool USBHub_waitForConnect(UInt timeout)
{
Bool ret = TRUE;
UInt key;
printf("\nUSBHub_waitForConnect\n");
/* Need exclusive access to prevent a race condition */
key = GateMutex_enter(gateUSBWait);
if (state == USBHub_NO_DEVICE) {
if (!Semaphore_pend(semUSBConnected, timeout)) {
ret = FALSE;
}
}
GateMutex_leave(gateUSBWait, key);
printf("\nUSBHub_waitForConnect------- after pend\n");
return (ret);
}
/*
* ======== USBMH_getState ========
*/
Void USBHub_getState(USBHub_State *HubState)
{
UInt key;
printf("\nUSBHub_getState\n");
switch (state) {
case USBHub_NO_DEVICE:
break;
case USBHub_INIT:
state = USBHub_CONNECTED;
/* Acquire lock */
key = GateMutex_enter(gateUSBLibAccess);
//USBHMouseInit(hubInstance);
/* Release lock */
GateMutex_leave(gateUSBLibAccess, key);
default:
break;
}
}