This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

RTOS/MSP432E401Y: Launchpad - USB Host CDC Example

Part Number: MSP432E401Y


Tool/software: TI-RTOS

Hi,

I'm trying to get the usb_cdc_serial_host_MSP_EXP432E401Y_tirtos_ccs sample to work. A CDC Device is recognized and the usbHCDEvents() is successfully called. However, back in the main task the program does not advance past the USBHCDCGetDataFromDevice() function. The function is called, but never exits, which results in the code being stuck. When I physically remove the CDC Device, USBHCDCGetDataFromDevice() quits and executes the following lines. When excluding USBHCDCGetDataFromDevice() from the source code then I am able to send Data to my CDC Device with USBHCDCSendDataToDevice(). 

UPDATE: I've narrowed down the problem. USBHCDPipeRead() in usbhostenum.c is called, however, the state of g_sUSBHCD.psUSBINPipes[ui32PipeIdx].iState is always "ePipeReadDMAWait". When reaching line 2241, USBLibDMAChannelStatus(g_sUSBHCD.psDMAInstance, g_sUSBHCD.psUSBINPipes[ui32PipeIdx].ui8DMAChannel) is called which returns "USBLIBSTATUS_DMA_PENDING". USBLibDMAChannelStatus() is found in usbdma.c and executes function iDMAUSBChannelStatus() on line 68. 

I have slightly modified the usbcdcserialhost.c file, as I am trying to implement a USB Hub. The Hub is recognized, but CDC Devices connected to the Hub are only recognized as unknown devices. The rest of the project files are unmodified. 

UPDATE: Increased task stack size. 

/*
 *  ======== usbcdcserialhost.c ========
 */

/*Standard C Header files */
#include <stdio.h>
#include <stdbool.h>

/*TI driver header files*/
#include <ti/drivers/UART.h>
#include <ti/drivers/dpl/HwiP.h>

/* POSIX Header file */
#include <pthread.h>

/* driverlib Header files */
#include "ti/devices/msp432e4/driverlib/driverlib.h"

/* usblib Header files */
#include <ti/usblib/msp432e4/usb-ids.h>
#include <ti/usblib/msp432e4/usblib.h>
#include <ti/usblib/msp432e4/usbcdc.h>
#include <ti/usblib/msp432e4/host/usbhost.h>
#include <ti/usblib/msp432e4/host/usbhcdc.h>
#include <ti/usblib/msp432e4/host/usbhcdcserial.h>
#include <usbcdcserialhost.h>

#include "Board.h"
#include "MSP_EXP432E401Y_usb.h"

#include <ti/usblib/msp432e4/host/usbhhub.h>

/* Example Header file */
#include "usbcdcserialhost.h"

typedef tUSBHCDCSerial         *USBCDCHType;


/* Defines */

/* Memory for the Host Class Driver */
#define HCDMEMORYPOOLSIZE   128


// The size of the host controller's memory pool in bytes.
#define HUB_POOL_SIZE       (HCDMEMORYPOOLSIZE * MAX_USB_DEVICES)

// (MAX_USB_DEVICES * (largest expected configuration descriptor)) bytes.
uint8_t g_pui8HubPool[HUB_POOL_SIZE];

/* The bulk enpoint interface number*/
#define INTERFACE_1      1

/* UART tx and rx array size*/
#define DATA_SIZE      64


/* Typedefs */
/* CDC states*/
typedef volatile enum {
    USBCDCH_NO_DEVICE = 0,
    USBCDCH_INIT,
    USBCDCH_CONNECTED,
    USBCDCH_UNKNOWN,
    USBCDCH_POWER_FAULT
} USBCDCH_USBState;


/* Static variables and handles */

/* Device states */
static volatile USBCDCH_USBState state;

/* Array for CDC descriptor data */
static unsigned char    memPoolHCD[HCDMEMORYPOOLSIZE];

/* The instance data for the CDC driver.*/
tUSBHCDCSerial *g_psCDCSerialInstance = 0;

tHubInstance *g_sHubInstance;

/* Function prototypes */
static void cdcSerialHandler(USBCDCHType cdcInstance, uint32_t event,
                                          uint32_t eventMsg, void *eventMsgPtr);
static void USBCDCH_hwiHandler(uintptr_t arg0);
static void usbHCDEvents(void *eventData);

/* MACRO to create a generic USB event host driver */
DECLARE_EVENT_DRIVER(USBCDCH_eventDriver, 0, 0, usbHCDEvents);

/* A list of available Host Class Drivers */
static tUSBHostClassDriver const * const usbHCDDriverList[] = {
    &g_sUSBCDCClassDriver,
    &g_sUSBHubClassDriver,
    &USBCDCH_eventDriver
};

/* Variable containing the number of HCDs in usbHCDDriverList */
const unsigned int numHostClassDrivers =
    sizeof(usbHCDDriverList) / sizeof(tUSBHostClassDriver *);

/*
 *  ======== HubCallbackHandler ========
 *  Callback handler.
 */

static void HubCallback(tHubInstance *psHubInstance,
                        uint32_t ui32Event, uint32_t ui32MsgParam,
                        void *pvMsgData){
    switch(ui32Event){

    }

    /* Print data to UART terminal */
    char buf[DATA_SIZE];
    sprintf(buf, "USB Hub Callback event:%d", ui32Event);
    puts(buf);



}
/*
 *  ======== cdcSerialHandler ========
 *  Callback handler.
 *
 *  Callback handler called by the USB stack to notify us on when
 *  data is received from the device.
 *
 */
static void cdcSerialHandler(USBCDCHType cdcInstance, uint32_t event,
                             uint32_t eventMsg, void *eventMsgPtr)
{
    puts("cdcSerialHandler");
    unsigned char ucChar;
    uint8_t i;


    /* Determine what event has happened */
    switch (event)
    {

        /* Data sent by the device was detected.*/
        case USBH_EVENT_RX_CDC_DATA:
        {
            puts("Data Received");

            char buf[DATA_SIZE];
            int c;
            /* parse through 64 bytes of received data to display*/
            for (i = 0; i<DATA_SIZE; i++)
            {

                /* Retrieve the data value pointed to by i */
                ucChar = (unsigned char)USBHCDCProcessData(g_psCDCSerialInstance, i);

                c += sprintf(buf,"%c",ucChar);
            }

            puts(buf);

            break;
       }
        /* Data sent to device */
       case USBH_EVENT_TX_CDC_DATA:
       {
           puts("Data Sent");
           /*
            *Nothing to process here since we don't care about the result of
            *sending the data to the device.
            */
           break;
       }

       default:
           break;
    }
}


/*
 *  ======== USBCDCH_hwiHandler ========
 *  This function calls the USB library's host interrupt handler.
 */
static void USBCDCH_hwiHandler(uintptr_t arg0)
{
    USB0_IRQHostHandler();
}



/*
 *  ======== usbHCDEvents ========
 *  Generic USB Host Class Driver event callback.
 *
 *  This callback is called to notify the application that a device was
 *  connected.
 */
static void usbHCDEvents(void *eventData)
{
    tEventInfo *pEventInfo;

    /* Cast this pointer to its actual type. */
    pEventInfo = (tEventInfo *)eventData;

    switch (pEventInfo->ui32Event) {

        case USB_EVENT_CONNECTED:
             /*
              * See if this is a CDC device where control interface descriptor
              * (interface 0) is defined as CDC class and interface protocol is
              * Common AT commands (value of 02)
              */
             if((USBHCDDevClass(pEventInfo->ui32Instance, 0) == USB_CLASS_CDC) &&
                (USBHCDDevProtocol(pEventInfo->ui32Instance, 0) ==
                        USB_CDC_PROTOCOL_V25TER))
             {

                puts("CDC Device Connected");


                 /*
                  * Proceed to the USBCDCH_INIT state so that the main
                  * loop can finish initializing the CDC device since
                  * USBCDCSerialInit() cannot be called from within a callback.
                  */
                 state = USBCDCH_INIT;
             }

        break;

        case USB_EVENT_UNKNOWN_CONNECTED:

            /* An unknown device was detected. */
           puts("Unknown device connected");

            state = USBCDCH_UNKNOWN;

            break;

        case USB_EVENT_DISCONNECTED:

            /* Indicate that the device has been disconnected.*/
            puts("Device Disconnected");


            /* Unknown device has been removed. */
            state = USBCDCH_NO_DEVICE;
            break;

        case USB_EVENT_POWER_FAULT:
            /* No power means no device is present. */
            state = USBCDCH_POWER_FAULT;
            break;

        default:
            break;
    }
}


/*
 *  ======== USBCDCH_init ========
 */
void USBCDCH_init(bool usbInternal)
{
    HwiP_Handle hwi;

    uint32_t PLLRate;
    uint32_t ui32ULPI;

    /* Initialize the USB stack for host mode. */
    USBStackModeSet(0, eUSBModeHost, NULL);

    /* Register host class drivers */
    USBHCDRegisterDrivers(0, usbHCDDriverList, numHostClassDrivers);

    /* Open an instance of the CDC host driver */
    g_psCDCSerialInstance = USBHCDCSerialOpen(cdcSerialHandler);
    if(!g_psCDCSerialInstance) {

        /*Error initializing the CDC Host*/
        puts("Error initializing the CDC Host.");

        while(1);
    }

    USBHHubOpen(HubCallback);

    /* Install interrupt handler */
    hwi = HwiP_create(INT_USB0, USBCDCH_hwiHandler, NULL);
    if (hwi == NULL) {
        /*Can't create USB Hwi*/
        puts("Can't create USB Hwi.");

        while(1);
    }

    /* Check if the ULPI mode is to be used or not */
    if(!(usbInternal)) {
        ui32ULPI = USBLIB_FEATURE_ULPI_HS;
        USBHCDFeatureSet(0, USBLIB_FEATURE_USBULPI, &ui32ULPI);
    } else {
		/* Initialize USB power configuration */
		USBHCDPowerConfigInit(0, USBHCD_VBUS_AUTO_HIGH | USBHCD_VBUS_FILTER);
	}
  
    /* Tell the USB library the CPU clock and the PLL frequency.*/
    SysCtlVCOGet(SYSCTL_XTAL_25MHZ, &PLLRate);

    USBHCDFeatureSet(0, USBLIB_FEATURE_USBPLL, &PLLRate);

    /* Enable the USB stack */
    USBHCDInit(0, memPoolHCD, HCDMEMORYPOOLSIZE);



    puts("CDC Serial Application");


}

/*
 *  ======== cdcSerialHostFxn ========
 *  Task for this example
 */
void *cdcSerialHostFxn (void *arg0)
{

    SysTickEnable();
    uint8_t dataBuffer[2] = {'T', 'I'};
    uint8_t dataSize = 2;

    bool *usbInternal = (bool *) arg0;

    /* Initialize CDC host*/
    USBCDCH_init(usbInternal);

    while(1)
    {
         /* Call the host library*/
         USBHCDMain();

         switch (state)
         {
             case USBCDCH_INIT:
             {
                 /* Initialize CDC serial */
                 USBHCDCSerialInit(g_psCDCSerialInstance);

                 state = USBCDCH_CONNECTED;
             break;
             }

             case USBCDCH_CONNECTED:
             {
                 /* poll for data from device */
                 //USBHCDCGetDataFromDevice(g_psCDCSerialInstance, INTERFACE_1);




                  /* Send the character to the device */
                  puts("Data trying to send");

                  //This is working
                  USBHCDCSendDataToDevice(g_psCDCSerialInstance, INTERFACE_1, dataBuffer, dataSize);
/*
                  //This is not working
                  puts("Starting Data Polling");
                  USBHCDCGetDataFromDevice(g_psCDCSerialInstance, INTERFACE_1);
                  puts("Data Polling successful");
*/

                 break;
             }
             case USBCDCH_UNKNOWN:
             {
                 break;
             }

             case USBCDCH_NO_DEVICE:
             {
                 break;
             }
             default:
             {
                 break;
             }
         }
    }
}

Any help would be much appreciated.

Best regards

Julius

**Attention** This is a public forum