AnsweredAssumed Answered

Hard Fault using STM32F0x2_USB-FS-Device_Lib V1.0.0 on STM32F072CB

Question asked by Blubimo on Jun 15, 2015
Latest reply on Nov 22, 2016 by Nesrine M

I'm working with the USB Library on a STM32F072CB.
The project I use is the virtual com port example inclueded in the library.

If I run the example without changing anything it works fine. Because the project isn't exactly what I need I have to change it a little bit. Therefor I changed the optimization level from 3 to 0, so I'm able to see all local variables in watch windows and so on.
Apperently this results in getting an hard fault over and over again.
All happens in the "PMAToUserBufferCopy" function in usb_core.c
Exactly the instruction: *(uint16_t*)pbUsrBuf++ = *pdwVal++; in the for loop of this function. 

  * @brief Copy a buffer from user memory area to packet memory area (PMA)
  * @param   pbUsrBuf    = pointer to user memory area.
  * @param   wPMABufAddr: address into PMA.
  * @param   wNBytes: no. of bytes to be copied.
  * @retval None
void PMAToUserBufferCopy(uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes)
  uint32_t n = (wNBytes + 1) >> 1;
  uint32_t i;
  uint16_t *pdwVal;
  pdwVal = (uint16_t *)(wPMABufAddr + PMAAddr);
  for (i = n; i != 0; i--)
    *(uint16_t*)pbUsrBuf++ = *pdwVal++;

I have no idea why this happens with optimization level 0 but not with level 3.

Does anyone has/had this issue?
It would be great if someone could help me out.