AnsweredAssumed Answered

Different macro definition for stm32f0xx_hal_pcd.h

Question asked by einSTein on Nov 9, 2017

Hi,

 

I'm wondering why a different macro definition is used for the stm32f0xx_hal_pcd.h header file of the USB HAL driver than the other HAL drivers. Macro definition of multiple C statements shall be placed within a "do {} while()" dummy loop as it is already done for all the HAL drivers except for PCD driver. In the case of HAL_PCD driver, macros are defined using enclosing curly brackets. This may cause some C translation units to not compile. Someone may think that the below line of C code should compile correctly but it will not do to the way the function-like macro is defined.

// This will not compile

if(1==1) PCD_FreeUserBuffer(USB,1,1); else PCD_FreeUserBuffer(USB,1,1);

 

// HAL_PCD wrong function-like macro definition

#define PCD_FreeUserBuffer(USBx, bEpNum, bDir)\
{\
  if ((bDir) == PCD_EP_DBUF_OUT)\
  { /* OUT double buffered endpoint */\
    PCD_TX_DTOG((USBx), (bEpNum));\
  }\
  else if ((bDir) == PCD_EP_DBUF_IN)\
  { /* IN double buffered endpoint */\
    PCD_RX_DTOG((USBx), (bEpNum));\
  }\
}

 

// correct function-like macro definition (as used on other HAL driver headers)

 

#define PCD_FreeUserBuffer(USBx, bEpNum, bDir)\
do {\
  if ((bDir) == PCD_EP_DBUF_OUT)\
  { /* OUT double buffered endpoint */\
    PCD_TX_DTOG((USBx), (bEpNum));\
  }\
  else if ((bDir) == PCD_EP_DBUF_IN)\
  { /* IN double buffered endpoint */\
    PCD_RX_DTOG((USBx), (bEpNum));\
  }\
} while(0U)

 

Best Regards,

Outcomes