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.

TM4C123GH6PM: Calling USBDBulkInit() cause HARD Fault interrupt

Fellow forumers, trying to adopt usb_dev_bulk example code , I figured out, that usblib from 2.1.4.178 kit may have potential issue caused by improper usage of void pointers. As I figured out, calling USBDBulkInit usblib API function cause FAult interrupt code snapshot below:

pvsDevice=USBDBulkInit(0, &g_sBulkDevice);
if(pvsDevice==0)UARTprintf(....")
CallUSBDBulkInit()  cause HARD Fault interrupt with following register values:
{FAULT_ADDR;NVIC_NVIC_FAULT_STAT;NVIC_HFAULT_STAT}={0x4;0x8200;0x0}

Looking on file usbdbulk.c in usblib, I noticed that, probably, following code cause fault interrupt:

 psDevDesc = (tDeviceDescriptor *)g_pui8BulkDeviceDescriptor;
        psDevDesc->idVendor = psBulkDevice->ui16VID;
        psDevDesc->idProduct = psBulkDevice->ui16PID;

Shouldn't it be something like next:

psDevDesc = (tDeviceDescriptor *)pvBulkDevice->g_pui8BulkDeviceDescriptor;
        psDevDesc->idVendor = psDevDesc->ui16VID;
        psDevDesc->idProduct = psDevDesc->ui16PID;

??? appreciate any help, advice, comments

Sincerely,

Alex.

  • Hi Alex,

    Thank you for the feedback. There was a formatting issue with your post. I tried to fix it, but if I inadvertently changed your post, please let me know so I can correct it.

    It is actually a holiday here in the USA. I will have Ralph look at this when he is back in the office as he is much more familiar with the USB drivers than me.

  • Hello Alexander,

    I am not really even following your suggestion because g_pui8BulkDeviceDescriptor is a global variable for the device descriptor so it can't possibly be an element within the locally created pvBulkDevice pointer...

  • Dear Ralph, thank you a lot for taking a look. I do not know why but everything starts work as expected now, my device got enumerated and able to communicate correctly. I know only one, that excessive use of void pointers is very poor practice, since, compilers have no chance to figure out errors at compilation time . I know that structures has to be referred as structures, not as *void to give compiler a chance and to calculate proper offsets.

    Sincerely,

    Alex.

  • Hello Alex,

    Yeah I'm not a fan of the use of void pointers in the USB library either. To clarify though, you are saying that making that adjustment got rid of the fault and the device works, but if you put the original code back in, it breaks?

    It must have to do with the offsets then because it looks like that's how the device descriptor is being passed into the USBDCDInit file.

    However, when I apply the code you posted in order to test it, it throws the compilation error I expect to see which is that the expression must have a pointer to struct or union type. How did you resolve that error?

    I'm suspecting maybe something else got changed in the bulk file that is the root problem here.

    Maybe you can upload your version of usbdbulk.c and I can diff it from there?

  • Ralph, all your "global " variables are stored as a void pointers inside many  structures, defined in usblib! It is poor, hack able and dangerous practice to use global variable with the scope of the whole routine, usually the scope of visibility is limited to modules in c or classes in c++, as a safe coding practice.

    Alex

  • Hello Alex,

    Unfortunately the USB library was written many years ago, and I've just inherited the task of supporting it and fixing what I can. Unfortunately re-working and cutting out some of the bad code practices like void pointers and many of the global variable misuses are tasks that - as the USB library has not caused failures due to them - are beyond the scope of what I can do on my end. That said, I'll be passing your feedback along as a testimony about the state of portions of the library which I also have noticed could be done much cleaner/better.

    Regarding your issue, if you feel it is resolved we can close the thread, but otherwise my offer to review the usbdbulk file to see if there is a better fix available for your issue remains open.