USB hard fault problem on LPC1754

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

USB hard fault problem on LPC1754

884 Views
dan_budd
Contributor I

Hello All,

I'm attempting to get the USB section of my application working.  I am encountering a hardfault and have
not had any success in figuring out where/why this is occurring. The code is my predecessor's that has been left
for me to get working.  Is there sample code available that describes how to program the USB port for the LPC1754FBD0 that I could follow as an example?

Symptoms:
I am developing the application using MCUXpresso IDE v11.0.1 and LPC Link 2 Rev B.
I create the code and debug it on the device. The device works correctly.
Stop the debugger and disconnect LPC Link 2 from the device. The device works correctly.
Disconnect/Reconnect the power to the device. The device works correctly.
Disconnect/Reconnect the power to the device again. The device does not work and has been captured by HardFault_Handler.

What am I doing wrong?

Thanks,
Dan

Here's what I have:

debugTracker is a control path breadcrumb trail that lets me know which routines were entered/exited that
pinpoints where the hard fault occurs.

To make the flow easier to follow, the DIALOG IS ALL CAPS. 
// comments, when part of an #if, is used to contrast which path is taken verses not.

INSIDE MAIN, I MAKE A CALL TO SETUPHARDWARE.
#define DEBUGTRACK(n)   (((n) < DEBUG_TRACKER_MAX) ? debugTracker[n]++ : 0)
static void SetupHardware(void)
{
   DEBUGTRACK(1);
   SystemCoreClockUpdate();
   Board_Init();

  #if USE_USB
   Chip_USB_Init();

   USBapp_Initalize();
   DEBUGTRACK(12);
   //---------------------------------
  Chip_IOCON_PinMux(LPC_IOCON, 1, 22, IOCON_MODE_INACT, IOCON_FUNC0); /* USB_Power

                                    switch controlled as GPIO pin */
  Chip_GPIO_WriteDirBit(LPC_GPIO, 1, 22, false); // Now reconfig as an input
#endif
:
:
:
}

I SUCCESSFULLY ENTERED USBapp_Initialize.
void USBapp_Initalize(void)
{
   //extern bool USB_IsInitialized;

    DEBUGTRACK(2);

    if ( USB_IsInitialized)
      USB_Disable((uint8_t)FlashDisk_MS_Interface.Config.PortNumber, (uint8_t)USB_MODE_Host);

      USB_Init((uint8_t)FlashDisk_MS_Interface.Config.PortNumber, (uint8_t)USB_MODE_Host);   // Library initialize
      DEBUGTRACK(10);
:
:
:
}

I SUCCESSFULLY ENTERED USB_Init.
void USB_Init(uint8_t corenum, uint8_t mode)
{
      DEBUGTRACK(3);
#if defined(USB_CAN_BE_HOST) 
      if (mode == USB_MODE_Host && Mem_IsInitialized == false)
     {
         USB_Memory_Init((uint32_t)USBRAM_BUFFER_SIZE);
         Mem_IsInitialized = true;
     }
#endif
    USB_CurrentMode[corenum] = mode;
    DEBUGTRACK(4);
    DEBUGTRACK(47);
    HAL_USBInit(corenum);
    DEBUGTRACK(8);
    USB_ResetInterface(corenum, mode);
    DEBUGTRACK(9);
    USB_IsInitialized = true;
}

USB_Memeory_Init ENTERS AND EXITS CORRECLTY.
void USB_Memory_Init(uint32_t Memory_Pool_Size)
{
    sMemBlockInfo *head = USB_Mem_Buffer;

    head->next = 0;
    head->size = (Memory_Pool_Size & 0xfffffffc) - HEADER_SIZE ;// align memory size
    head->isFree = 1;

}

HAL_USBinit ENTERS AND EXITS CORRECTLY.
void HAL_USBInit(uint8_t corenum)
{
    DEBUGTRACK(5);
    /* Enable PLL1 for 48MHz output */
    Chip_Clock_EnablePLL(SYSCTL_USB_PLL, SYSCTL_PLL_ENABLE);
#if defined(__LPC175X_6X__)
    while ((Chip_Clock_GetPLLStatus(SYSCTL_USB_PLL) & SYSCTL_PLL1STS_LOCKED) == 0);
#else
    //while ((Chip_Clock_GetPLLStatus(SYSCTL_USB_PLL) & SYSCTL_PLLSTS_LOCKED) == 0);
#endif

    Chip_IOCON_PinMux(LPC_IOCON, 0, 29, IOCON_MODE_INACT, IOCON_FUNC1); /* P0.29 D1+, P0.30 D1- */
    Chip_IOCON_PinMux(LPC_IOCON, 0, 30, IOCON_MODE_INACT, IOCON_FUNC1);

#if defined(USB_CAN_BE_HOST)
    Chip_IOCON_PinMux(LPC_IOCON, 1, 19, IOCON_MODE_INACT, IOCON_FUNC2); /* USB_Power switch */
#endif
    DEBUGTRACK(6);

#if defined(USB_CAN_BE_DEVICE)
    //Chip_IOCON_PinMux(LPC_IOCON, 2, 9, IOCON_MODE_INACT, IOCON_FUNC1); /* USB_SoftConnect */
#endif

    LPC_SYSCTL->PCONP |= (1UL << 31);     /* USB PCLK -> enable USB Per.*/

#if defined(USB_CAN_BE_DEVICE)
    //LPC_USB->USBClkCtrl = 0x12;     /* Dev, PortSel, AHB clock enable */
    //while ((LPC_USB->USBClkSt & 0x12) != 0x12) ;

    //HAL_Reset(corenum);
#endif
    DEBUGTRACK(7);
}

USB_ResetInt IS CALLED FROM WITHIN USB_Init.
THIS ROUTINE IS SUCCESSFULLY ENTERED.
void USB_ResetInterface(uint8_t corenum, uint8_t mode)
{
   // if (mode == USB_MODE_Device) {
   //  #if defined(USB_CAN_BE_DEVICE)
   //  USB_Init_Device(corenum);
   //  #endif
   // }
 else if (mode == USB_MODE_Host) {
     #if defined(USB_CAN_BE_HOST)
     USB_Init_Host(corenum);
     #endif
 }
}

USB_Init_Host ENTERS CORRECTLY.
#if defined(USB_CAN_BE_HOST)
static void USB_Init_Host(uint8_t corenum)  USBMEMORY.C
{
    // uint8_t i;

    // for(i=0;i<PIPE_TOTAL_PIPES;i++) PipeInfo[i].PipeHandle=0;

    pipeselected[corenum] = PIPE_CONTROLPIPE;

    USB_HostState[corenum]   = HOST_STATE_Unattached;
    USB_Host_ControlPipeSize[corenum] = PIPE_CONTROLPIPE_DEFAULT_SIZE;

    DEBUGTRACK(60);
    if (HcdInitDriver(corenum) == HCD_STATUS_OK) {
     USB_IsInitialized = true;
     HAL_EnableUSBInterrupt(corenum);
    }
    else {
     USB_IsInitialized = false;
     HcdDeInitDriver(corenum);
    }
}
#endif


HcdInitDriver ENTERS CORRECTLY.
HCD_STATUS HcdInitDriver(uint8_t HostID)
{
    DEBUGTRACK(61);

    USB_REG(HostID)->OTGClkCtrl = 0x00000019;   /* enable Host clock, OTG clock and AHB clock */
    while ((USB_REG(HostID)->OTGClkSt & 0x00000019) != 0x00000019) ;
   #if defined(__LPC175X_6X__)
    USB_REG(HostID)->StCtrl = 0x3;     /* ??? */
   #elif defined(__LPC177X_8X__) || defined(__LPC407X_8X__)
    USB_REG(HostID)->StCtrl = 0x1;     /* Port 1 is host */
   #endif
    DEBUGTRACK(62);
    OHciHostReset(HostID); /* Software Reset */
    DEBUGTRACK(63);
    return OHciHostInit(HostID);
}

DEBUGTRACK(62) IS THE LAST BREADCRUMB DEPOSITED BEFORE BEING TRAPPED BY HardFault_Handler.
static __INLINE HCD_STATUS OHciHostReset(uint8_t HostID)
{
    USB_REG(HostID)->CommandStatus = HC_COMMAND_STATUS_HostControllerReset;
    while ( USB_REG(HostID)->CommandStatus & HC_COMMAND_STATUS_HostControllerReset) {}

                                                   /* FIXME Wait indefinitely (may need a time-out here) */

    return HCD_STATUS_OK;
}

Labels (1)
0 Kudos
3 Replies

744 Views
Alexis_A
NXP TechSupport
NXP TechSupport

Dear Dan,

I will suggest to check the following link:

LPCOpen Software for LPC17XX | NXP 

Here you can find some USB examples that could help you with your application.

Best Regards,

Alexis Andalon

0 Kudos

744 Views
dan_budd
Contributor I

Hi Alexis,

This is my problem that Adam submitted. I saw your following request, but

didn't know how to reply short of replying to an email that you sent me

earlier.

"I have some questions about this problem:

- 1: The problem is presented even if you don't attach the debugger?

- 2: Are you using some flash operation or did your program store some data

in the flash?"

1: From the MCUXpresso IDE: compile, link, download, and debug the code.

The device runs fine.

Terminate the debugger and disconnect from the device. The device runs fine.

Bounce power to the device. The device runs fine.

Bounce power to the device again. The device does not run.

From the MCUXpresso IDE: modify the debug settings to attach only and

reconnect the device.

Start debugging and press pause. The IDE shows that the code is in the hard

fault handler.

I have already documented how it got there. To address your question: It's

a requirement that the debugger not be connected.

2: No flash operation and not storing data in the flash - to the best of my

knowledge.

I inherited the code and haven't been through it all. From the

startup until the fault handler, it's straight forward.

Thanks,

Dan

0 Kudos

744 Views
dan_budd
Contributor I

Alexis,

Thank you for the suggestion. The website shows LPCOpen2 libraries. One

for the LPC1788 and the other for the LPC1769.

Which one should be used for the LPC1754 processor?

Do they both work with MCUXpresso?

Dan

0 Kudos