AnsweredAssumed Answered

STM32F4Discovery and USB

Question asked by hakobyan.davit on Jan 22, 2014
Latest reply on Jan 24, 2014 by hakobyan.davit
Dear All,

I had a workign VCP example on STM32F4Discovery board until some recent changes to my own code (the USB and standard peripheral libraries are not changed).
I use EmBlocks 1.42 IDE for compilation/debugging under Windows.
The problem occurs already at the USBD_Init function call
[b]
USBD_Init(&USB_OTG_dev,
#ifdef USE_USB_OTG_HS
          USB_OTG_HS_CORE_ID,
#else
          USB_OTG_FS_CORE_ID,
#endif // USE_USB_OTG_HS
          &USR_desc,
          &USBD_CDC_cb,
          &USR_cb);
[/b]

where the variable USR_cb which is defined in one of the sources of the USB library contains pointers to some internal functions. The first function pointer should point to an Init function (which is an empty function in my case). The point that at the USBD_Init call the first functions pointer of the USB_cb variable points not to the Init function but has an address 0x04xxxxxx which is obviously outside of the dedicated scope for the program residing in FLASH.

If I add a line USB_cb.Init(); just before calling the USBD_Init(...); then I see that the USB_cb.Init function pointer is properly initialized (has an address 0x08xxxxxx). Tracking down to the point where the USB_cb is initialized one reaches the CopyDataInit: loop in the Reset_Handler routine of the startup*.S file. Already there the initialization of the function pointer of USB_cb (at RAM address 0x2000013C) is done incorrectly and at the offset of 0x13C in the Flash memory where the initial value (correct address of function Init) should be written (at address 0x08014398) another value 0x04xxxxxx is sitting.

The program size is small (~88KB). The linker script seems to also having an adequate values:

[b]Linker script[/b]
[i]
_estack = 0x20020000;    /* end of 128K RAM */

_Min_Heap_Size = 0;      /* required amount of heap  */
_Min_Stack_Size = 0x400; /* required amount of stack */

MEMORY
{
  FLASH (rx)      : ORIGIN = 0x08000000, LENGTH = 1024K
  RAM (xrw)       : ORIGIN = 0x20000000, LENGTH = 128K
  MEMORY_B1 (rx)  : ORIGIN = 0x60000000, LENGTH = 0K
}
[/i]

Interestingly, initially the program was working when its size was twice as bigger as the current one (~150KB). I have tried to decrease/increase the stack size in the linker script without any positive outcome.
The variable USB_OTG_dev is properly aligned by __ALIGN_BEGIN / __ALIGN_END directives. These directives are always defined (regarding whether DMA is enabled or not).

This problem still looks like having some alignment related issues, although I have not changed anything in the USB aligned variables.

Can somewhat advise how may I track down further why is a wrong value in the FLASH is written for initializing the USB_cb variable?

Thanks very much for any help.

Outcomes