Skip to main content
Evgeny Sobolev
Associate II
June 19, 2019
Solved

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

  • June 19, 2019
  • 1 reply
  • 854 views

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;

This topic has been closed for replies.
Best answer by Imen.D

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

1 reply

Imen.DBest answer
ST Technical Moderator
June 19, 2019

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

In order to give better visibility on the answered topics, please click on 'Best answer' on the reply which solved your issue or answered your question. Thanks