cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L4xx HAL Library USB bug. Function USB_WritePMA does not work properly when compiled with "-O3" option.

Evgeny Sobolev
Associate III

I used latest CubeMX version and found another one bug in stm32l4xx_ll_usb.c generated by it. If you will compile code using "-O3" optimization level, function USB_WritePMA will not work properly.

/**
 * @brief Copy a buffer from user memory area to packet memory area (PMA)
 * @param  USBx USB peripheral instance register address.
 * @param  pbUsrBuf pointer to user memory area.
 * @param  wPMABufAddr address into PMA.
 * @param  wNBytes: no. of bytes to be copied.
 * @retval None
 */
 
void USB_WritePMA(USB_TypeDef *USBx, uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes)
{
 uint32_t n = ((uint32_t)wNBytes + 1U) >> 1;
 uint32_t BaseAddr = (uint32_t)USBx;
 uint32_t i, temp1, temp2;
 uint16_t *pdwVal;
 uint8_t *pBuf = pbUsrBuf;
 pdwVal = (uint16_t *)(BaseAddr + 0x400U + ((uint32_t)wPMABufAddr * PMA_ACCESS));
 
 for (i = n; i != 0U; i--)
 {
  temp1 = (uint16_t) * pBuf;
  pBuf++;
  temp2 = temp1 | ((uint16_t)((uint16_t) * pBuf << 8));
  *pdwVal = (uint16_t)temp2;
  pdwVal++;
 
#if PMA_ACCESS > 1U
  pdwVal++;
#endif
  pBuf++;
 }
}

To fix it, please replace line:

uint16_t *pdwVal;

to

volatile uint16_t *pdwVal;

1 ACCEPTED SOLUTION

Accepted Solutions
Imen.D
ST Employee

Hello @Evgeny Sobolev​ ,

We are aware about this issue and it will be fixed in the coming release of STM32CubeL4 firmware package.

Thanks for your contribution and sorry for the inconvenience this has caused.

Best Regards,

Imen

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen

View solution in original post

1 REPLY 1
Imen.D
ST Employee

Hello @Evgeny Sobolev​ ,

We are aware about this issue and it will be fixed in the coming release of STM32CubeL4 firmware package.

Thanks for your contribution and sorry for the inconvenience this has caused.

Best Regards,

Imen

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen