AnsweredAssumed Answered

USB Library - ****** code.

Question asked by M.E on Jan 23, 2016
Latest reply on Feb 16, 2016 by M.E
  * @file    stm32f3xx_hal_pcd_ex.c
  * @author  MCD Application Team
  * @version V1.2.0
  * @date    13-November-2015

/**
  * @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 PCD_WritePMA(USB_TypeDef  *USBx, uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes)
{
  uint32_t n = (wNBytes + 1) >> 1;   /* n = (wNBytes + 1) / 2 */
  uint32_t i, temp1, temp2;
  uint16_t *pdwVal;
  pdwVal = (uint16_t *)(wPMABufAddr * 2 + (uint32_t)USBx + 0x400);
  for (i = n; i != 0; i--)
  {
    temp1 = (uint16_t) * pbUsrBuf;
    pbUsrBuf++;
    temp2 = temp1 | (uint16_t) * pbUsrBuf << 8;
    *pdwVal++ = temp2;
    pdwVal++;
    pbUsrBuf++;
  }
}

Probably this is most critical part for USB performance - data is moved from memory to PMA. Why bit arithmetic is here? What for so many useless variables are here?

my version(works fine)
void PCD_WritePMA(USB_TypeDef  *USBx, uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes)
{
  int n = (wNBytes + 1) >> 1;   // n = (wNBytes + 1) / 2
  __IO uint32_t *pdwVal = (uint32_t *)(wPMABufAddr * 2 + (uint32_t)USBx + 0x400);
 uint16_t * pwUsrBuf = (uint16_t *) pbUsrBuf;
  for (; n != 0; n--)
  {
        *pdwVal = *pwUsrBuf;
        pwUsrBuf++;
        pdwVal++;
  }
}


Outcomes