TARGET_STM : USB FS STM HAL changes

pull/3062/head
Michel Jaouen 2016-10-05 16:44:10 +02:00
parent 9c4e7173a5
commit 182c311fbd
19 changed files with 241 additions and 88 deletions

View File

@ -123,6 +123,27 @@ typedef enum
(__HANDLE__)->Lock = HAL_UNLOCKED; \
}while (0)
#endif /* USE_RTOS */
#if defined (__CC_ARM)
#pragma diag_suppress 3731
#endif
static inline void atomic_set_u32(volatile uint32_t *ptr, uint32_t mask)
{
uint32_t newValue;
do {
newValue = (uint32_t)__LDREXW((volatile unsigned long *)ptr) | mask;
} while (__STREXW(newValue,(volatile unsigned long*) ptr));
}
static inline void atomic_clr_u32(volatile uint32_t *ptr, uint32_t mask)
{
uint32_t newValue;
do {
newValue = (uint32_t)__LDREXW((volatile unsigned long *)ptr) &~mask;
} while (__STREXW(newValue,(volatile unsigned long*) ptr));
}
#if defined ( __GNUC__ )
#ifndef __weak

View File

@ -144,12 +144,18 @@ HAL_StatusTypeDef HAL_PCD_Init(PCD_HandleTypeDef *hpcd)
/* Check the parameters */
assert_param(IS_PCD_ALL_INSTANCE(hpcd->Instance));
if(hpcd->State == HAL_PCD_STATE_RESET)
{
/* Allocate lock resource and initialize it */
hpcd->Lock = HAL_UNLOCKED;
for (i = 0; i < hpcd->Init.dev_endpoints ; i++)
hpcd->EPLock[i].Lock = HAL_UNLOCKED;
/* Init the low level hardware : GPIO, CLOCK, NVIC... */
HAL_PCD_MspInit(hpcd);
}
hpcd->State = HAL_PCD_STATE_BUSY;
/* Init the low level hardware : GPIO, CLOCK, NVIC... */
HAL_PCD_MspInit(hpcd);
/* Disable the Interrupts */
__HAL_PCD_DISABLE(hpcd);
@ -393,7 +399,7 @@ void HAL_PCD_IRQHandler(PCD_HandleTypeDef *hpcd)
if(( epint & USB_OTG_DIEPINT_XFRC) == USB_OTG_DIEPINT_XFRC)
{
fifoemptymsk = 0x1U << epnum;
USBx_DEVICE->DIEPEMPMSK &= ~fifoemptymsk;
atomic_clr_u32(&USBx_DEVICE->DIEPEMPMSK, fifoemptymsk);
CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_XFRC);
@ -973,8 +979,8 @@ HAL_StatusTypeDef HAL_PCD_EP_Receive(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, u
ep->dma_addr = (uint32_t)pBuf;
}
__HAL_LOCK(hpcd);
__HAL_LOCK(&hpcd->EPLock[ep_addr & 0x7F]);
if ((ep_addr & 0x7FU) == 0U )
{
USB_EP0StartXfer(hpcd->Instance , ep, hpcd->Init.dma_enable);
@ -983,7 +989,7 @@ HAL_StatusTypeDef HAL_PCD_EP_Receive(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, u
{
USB_EPStartXfer(hpcd->Instance , ep, hpcd->Init.dma_enable);
}
__HAL_UNLOCK(hpcd);
__HAL_UNLOCK(&hpcd->EPLock[ep_addr & 0x7F]);
return HAL_OK;
}
@ -1024,7 +1030,7 @@ HAL_StatusTypeDef HAL_PCD_EP_Transmit(PCD_HandleTypeDef *hpcd, uint8_t ep_addr,
ep->dma_addr = (uint32_t)pBuf;
}
__HAL_LOCK(hpcd);
__HAL_LOCK(&hpcd->EPLock[ep_addr & 0x7F]);
if ((ep_addr & 0x7FU) == 0U )
{
@ -1035,8 +1041,8 @@ HAL_StatusTypeDef HAL_PCD_EP_Transmit(PCD_HandleTypeDef *hpcd, uint8_t ep_addr,
USB_EPStartXfer(hpcd->Instance , ep, hpcd->Init.dma_enable);
}
__HAL_UNLOCK(hpcd);
__HAL_UNLOCK(&hpcd->EPLock[ep_addr & 0x7F]);
return HAL_OK;
}
@ -1064,13 +1070,13 @@ HAL_StatusTypeDef HAL_PCD_EP_SetStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
ep->is_in = ((ep_addr & 0x80U) == 0x80U);
__HAL_LOCK(hpcd);
__HAL_LOCK(&hpcd->EPLock[ep_addr & 0x7F]);
USB_EPSetStall(hpcd->Instance , ep);
if((ep_addr & 0x7FU) == 0U)
{
USB_EP0_OutStart(hpcd->Instance, hpcd->Init.dma_enable, (uint8_t *)hpcd->Setup);
}
__HAL_UNLOCK(hpcd);
__HAL_UNLOCK(&hpcd->EPLock[ep_addr & 0x7F]);
return HAL_OK;
}
@ -1098,9 +1104,9 @@ HAL_StatusTypeDef HAL_PCD_EP_ClrStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
ep->num = ep_addr & 0x7FU;
ep->is_in = ((ep_addr & 0x80U) == 0x80U);
__HAL_LOCK(hpcd);
__HAL_LOCK(&hpcd->EPLock[ep_addr & 0x7F]);
USB_EPClearStall(hpcd->Instance , ep);
__HAL_UNLOCK(hpcd);
__HAL_UNLOCK(&hpcd->EPLock[ep_addr & 0x7F]);
return HAL_OK;
}
@ -1113,7 +1119,7 @@ HAL_StatusTypeDef HAL_PCD_EP_ClrStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
*/
HAL_StatusTypeDef HAL_PCD_EP_Flush(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
{
__HAL_LOCK(hpcd);
__HAL_LOCK(&hpcd->EPLock[ep_addr & 0x7F]);
if ((ep_addr & 0x80U) == 0x80U)
{
@ -1124,8 +1130,8 @@ HAL_StatusTypeDef HAL_PCD_EP_Flush(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
USB_FlushRxFifo(hpcd->Instance);
}
__HAL_UNLOCK(hpcd);
__HAL_UNLOCK(&hpcd->EPLock[ep_addr & 0x7F]);
return HAL_OK;
}
@ -1247,8 +1253,7 @@ static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t
if(len <= 0U)
{
fifoemptymsk = 0x1U << epnum;
USBx_DEVICE->DIEPEMPMSK &= ~fifoemptymsk;
atomic_clr_u32(&USBx_DEVICE->DIEPEMPMSK, fifoemptymsk);
}
return HAL_OK;

View File

@ -75,6 +75,10 @@ typedef USB_OTG_GlobalTypeDef PCD_TypeDef;
typedef USB_OTG_CfgTypeDef PCD_InitTypeDef;
typedef USB_OTG_EPTypeDef PCD_EPTypeDef ;
typedef struct
{
HAL_LockTypeDef Lock;
} PCD_EPLockDef;
/**
* @brief PCD Handle Structure definition
*/
@ -85,6 +89,7 @@ typedef struct
PCD_EPTypeDef IN_ep[15]; /*!< IN endpoint parameters */
PCD_EPTypeDef OUT_ep[15]; /*!< OUT endpoint parameters */
HAL_LockTypeDef Lock; /*!< PCD peripheral status */
PCD_EPLockDef EPLock[15];
__IO PCD_StateTypeDef State; /*!< PCD communication state */
uint32_t Setup[12]; /*!< Setup packet buffer */
void *pData; /*!< Pointer to upper stack Handler */

View File

@ -590,7 +590,7 @@ HAL_StatusTypeDef USB_EPStartXfer(USB_OTG_GlobalTypeDef *USBx , USB_OTG_EPTypeDe
/* Enable the Tx FIFO Empty Interrupt for this EP */
if (ep->xfer_len > 0)
{
USBx_DEVICE->DIEPEMPMSK |= 1 << ep->num;
atomic_set_u32(&USBx_DEVICE->DIEPEMPMSK, 1 << ep->num);
}
}
}
@ -708,7 +708,7 @@ HAL_StatusTypeDef USB_EP0StartXfer(USB_OTG_GlobalTypeDef *USBx , USB_OTG_EPTypeD
/* Enable the Tx FIFO Empty Interrupt for this EP */
if (ep->xfer_len > 0)
{
USBx_DEVICE->DIEPEMPMSK |= 1 << (ep->num);
atomic_set_u32(&USBx_DEVICE->DIEPEMPMSK, 1 << (ep->num));
}
}

View File

@ -122,6 +122,27 @@ typedef enum
(__HANDLE__)->Lock = HAL_UNLOCKED; \
}while (0)
#endif /* USE_RTOS */
#if defined (__CC_ARM)
#pragma diag_suppress 3731
#endif
static inline void atomic_set_u32(volatile uint32_t *ptr, uint32_t mask)
{
uint32_t newValue;
do {
newValue = (uint32_t)__LDREXW((volatile unsigned long *)ptr) | mask;
} while (__STREXW(newValue,(volatile unsigned long*) ptr));
}
static inline void atomic_clr_u32(volatile uint32_t *ptr, uint32_t mask)
{
uint32_t newValue;
do {
newValue = (uint32_t)__LDREXW((volatile unsigned long *)ptr) &~mask;
} while (__STREXW(newValue,(volatile unsigned long*) ptr));
}
#if defined ( __GNUC__ )
#ifndef __weak

View File

@ -156,7 +156,8 @@ HAL_StatusTypeDef HAL_PCD_Init(PCD_HandleTypeDef *hpcd)
{
/* Allocate lock resource and initialize it */
hpcd->Lock = HAL_UNLOCKED;
for (i = 0; i < hpcd->Init.dev_endpoints ; i++)
hpcd->EPLock[i].Lock = HAL_UNLOCKED;
/* Init the low level hardware : GPIO, CLOCK, NVIC... */
HAL_PCD_MspInit(hpcd);
}
@ -185,8 +186,8 @@ HAL_StatusTypeDef HAL_PCD_Init(PCD_HandleTypeDef *hpcd)
hpcd->OUT_ep[i].maxpacket = 0;
hpcd->OUT_ep[i].xfer_buff = 0;
hpcd->OUT_ep[i].xfer_len = 0;
}
/* Init Device */
/*CNTR_FRES = 1*/
hpcd->Instance->CNTR = USB_CNTR_FRES;

View File

@ -171,6 +171,11 @@ typedef struct
typedef USB_TypeDef PCD_TypeDef;
typedef struct
{
HAL_LockTypeDef Lock;
} PCD_EPLockDef;
/**
* @brief PCD Handle Structure definition
*/
@ -182,6 +187,7 @@ typedef struct
PCD_EPTypeDef IN_ep[15]; /*!< IN endpoint parameters */
PCD_EPTypeDef OUT_ep[15]; /*!< OUT endpoint parameters */
HAL_LockTypeDef Lock; /*!< PCD peripheral status */
PCD_EPLockDef EPLock[15];
__IO PCD_StateTypeDef State; /*!< PCD communication state */
uint32_t Setup[12]; /*!< Setup packet buffer */
void *pData; /*!< Pointer to upper stack Handler */

View File

@ -122,6 +122,28 @@ typedef enum
do{ \
(__HANDLE__)->Lock = HAL_UNLOCKED; \
}while (0)
#if defined (__CC_ARM)
#pragma diag_suppress 3731
#endif
static inline void atomic_set_u32(volatile uint32_t *ptr, uint32_t mask)
{
uint32_t newValue;
do {
newValue = (uint32_t)__LDREXW((volatile unsigned long *)ptr) | mask;
} while (__STREXW(newValue,(volatile unsigned long*) ptr));
}
static inline void atomic_clr_u32(volatile uint32_t *ptr, uint32_t mask)
{
uint32_t newValue;
do {
newValue = (uint32_t)__LDREXW((volatile unsigned long *)ptr) &~mask;
} while (__STREXW(newValue,(volatile unsigned long*) ptr));
}
#endif /* USE_RTOS */
#if defined ( __GNUC__ )

View File

@ -148,11 +148,17 @@ HAL_StatusTypeDef HAL_PCD_Init(PCD_HandleTypeDef *hpcd)
/* Check the parameters */
assert_param(IS_PCD_ALL_INSTANCE(hpcd->Instance));
if(hpcd->State == HAL_PCD_STATE_RESET)
{
/* Allocate lock resource and initialize it */
hpcd->Lock = HAL_UNLOCKED;
for (i = 0; i < hpcd->Init.dev_endpoints ; i++)
hpcd->EPLock[i].Lock = HAL_UNLOCKED;
/* Init the low level hardware : GPIO, CLOCK, NVIC... */
HAL_PCD_MspInit(hpcd);
}
hpcd->State = HAL_PCD_STATE_BUSY;
/* Init the low level hardware : GPIO, CLOCK, NVIC... */
HAL_PCD_MspInit(hpcd);
/* Disable the Interrupts */
__HAL_PCD_DISABLE(hpcd);
@ -190,7 +196,6 @@ HAL_StatusTypeDef HAL_PCD_Init(PCD_HandleTypeDef *hpcd)
hpcd->Instance->DIEPTXF[i] = 0U;
}
/* Init Device */
USB_DevInit(hpcd->Instance, hpcd->Init);
@ -296,10 +301,10 @@ __weak void HAL_PCD_MspDeInit(PCD_HandleTypeDef *hpcd)
*/
HAL_StatusTypeDef HAL_PCD_Start(PCD_HandleTypeDef *hpcd)
{
__HAL_LOCK(hpcd);
//__HAL_LOCK(hpcd);
USB_DevConnect (hpcd->Instance);
__HAL_PCD_ENABLE(hpcd);
__HAL_UNLOCK(hpcd);
//__HAL_UNLOCK(hpcd);
return HAL_OK;
}
@ -310,11 +315,11 @@ HAL_StatusTypeDef HAL_PCD_Start(PCD_HandleTypeDef *hpcd)
*/
HAL_StatusTypeDef HAL_PCD_Stop(PCD_HandleTypeDef *hpcd)
{
__HAL_LOCK(hpcd);
//__HAL_LOCK(hpcd);
__HAL_PCD_DISABLE(hpcd);
USB_StopDevice(hpcd->Instance);
USB_DevDisconnect(hpcd->Instance);
__HAL_UNLOCK(hpcd);
//__HAL_UNLOCK(hpcd);
return HAL_OK;
}
@ -421,7 +426,8 @@ void HAL_PCD_IRQHandler(PCD_HandleTypeDef *hpcd)
if(( epint & USB_OTG_DIEPINT_XFRC) == USB_OTG_DIEPINT_XFRC)
{
fifoemptymsk = 0x1U << epnum;
USBx_DEVICE->DIEPEMPMSK &= ~fifoemptymsk;
atomic_clr_u32(&USBx_DEVICE->DIEPEMPMSK,fifoemptymsk);
CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_XFRC);
@ -440,7 +446,7 @@ void HAL_PCD_IRQHandler(PCD_HandleTypeDef *hpcd)
/* prepare to rx more setup packets */
USB_EP0_OutStart(hpcd->Instance, 1U, (uint8_t *)hpcd->Setup);
}
}
}
}
if(( epint & USB_OTG_DIEPINT_TOC) == USB_OTG_DIEPINT_TOC)
{
@ -968,9 +974,9 @@ HAL_StatusTypeDef HAL_PCD_EP_Open(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint
ep->data_pid_start = 0U;
}
__HAL_LOCK(hpcd);
__HAL_LOCK(&hpcd->EPLock[ep_addr & 0x7FU]);
USB_ActivateEndpoint(hpcd->Instance , ep);
__HAL_UNLOCK(hpcd);
__HAL_UNLOCK(&hpcd->EPLock[ep_addr & 0x7FU]);
return ret;
}
@ -997,9 +1003,9 @@ HAL_StatusTypeDef HAL_PCD_EP_Close(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
ep->is_in = (0x80U & ep_addr) != 0U;
__HAL_LOCK(hpcd);
__HAL_LOCK(&hpcd->EPLock[ep_addr & 0x7FU]);
USB_DeactivateEndpoint(hpcd->Instance , ep);
__HAL_UNLOCK(hpcd);
__HAL_UNLOCK(&hpcd->EPLock[ep_addr & 0x7FU]);
return HAL_OK;
}
@ -1030,7 +1036,7 @@ HAL_StatusTypeDef HAL_PCD_EP_Receive(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, u
ep->dma_addr = (uint32_t)pBuf;
}
__HAL_LOCK(hpcd);
__HAL_LOCK(&hpcd->EPLock[ep_addr & 0x7FU]);
if ((ep_addr & 0x7FU) == 0U)
{
@ -1040,7 +1046,7 @@ HAL_StatusTypeDef HAL_PCD_EP_Receive(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, u
{
USB_EPStartXfer(hpcd->Instance , ep, hpcd->Init.dma_enable);
}
__HAL_UNLOCK(hpcd);
__HAL_UNLOCK(&hpcd->EPLock[ep_addr & 0x7FU]);
return HAL_OK;
}
@ -1081,7 +1087,7 @@ HAL_StatusTypeDef HAL_PCD_EP_Transmit(PCD_HandleTypeDef *hpcd, uint8_t ep_addr,
ep->dma_addr = (uint32_t)pBuf;
}
__HAL_LOCK(hpcd);
__HAL_LOCK(&hpcd->EPLock[ep_addr & 0x7FU]);
if ((ep_addr & 0x7FU) == 0U)
{
@ -1092,7 +1098,7 @@ HAL_StatusTypeDef HAL_PCD_EP_Transmit(PCD_HandleTypeDef *hpcd, uint8_t ep_addr,
USB_EPStartXfer(hpcd->Instance , ep, hpcd->Init.dma_enable);
}
__HAL_UNLOCK(hpcd);
__HAL_UNLOCK(&hpcd->EPLock[ep_addr & 0x7FU]);
return HAL_OK;
}
@ -1121,13 +1127,13 @@ HAL_StatusTypeDef HAL_PCD_EP_SetStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
ep->is_in = ((ep_addr & 0x80U) == 0x80U);
__HAL_LOCK(hpcd);
__HAL_LOCK(&hpcd->EPLock[ep_addr & 0x7FU]);
USB_EPSetStall(hpcd->Instance , ep);
if((ep_addr & 0x7FU) == 0U)
{
USB_EP0_OutStart(hpcd->Instance, hpcd->Init.dma_enable, (uint8_t *)hpcd->Setup);
}
__HAL_UNLOCK(hpcd);
__HAL_UNLOCK(&hpcd->EPLock[ep_addr & 0x7FU]);
return HAL_OK;
}
@ -1155,9 +1161,9 @@ HAL_StatusTypeDef HAL_PCD_EP_ClrStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
ep->num = ep_addr & 0x7FU;
ep->is_in = ((ep_addr & 0x80U) == 0x80U);
__HAL_LOCK(hpcd);
__HAL_LOCK(&hpcd->EPLock[ep_addr & 0x7FU]);
USB_EPClearStall(hpcd->Instance , ep);
__HAL_UNLOCK(hpcd);
__HAL_UNLOCK(&hpcd->EPLock[ep_addr & 0x7FU]);
return HAL_OK;
}
@ -1170,8 +1176,7 @@ HAL_StatusTypeDef HAL_PCD_EP_ClrStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
*/
HAL_StatusTypeDef HAL_PCD_EP_Flush(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
{
__HAL_LOCK(hpcd);
__HAL_LOCK(&hpcd->EPLock[ep_addr & 0x7FU]);
if ((ep_addr & 0x80U) == 0x80U)
{
USB_FlushTxFifo(hpcd->Instance, ep_addr & 0x7FU);
@ -1181,7 +1186,7 @@ HAL_StatusTypeDef HAL_PCD_EP_Flush(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
USB_FlushRxFifo(hpcd->Instance);
}
__HAL_UNLOCK(hpcd);
__HAL_UNLOCK(&hpcd->EPLock[ep_addr & 0x7FU]);
return HAL_OK;
}
@ -1304,8 +1309,7 @@ static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t
if(len <= 0U)
{
fifoemptymsk = 0x1U << epnum;
USBx_DEVICE->DIEPEMPMSK &= ~fifoemptymsk;
atomic_clr_u32(&USBx_DEVICE->DIEPEMPMSK, fifoemptymsk);
}
return HAL_OK;

View File

@ -92,14 +92,21 @@ typedef USB_OTG_EPTypeDef PCD_EPTypeDef ;
/**
* @brief PCD Handle Structure definition
*/
*/
typedef struct
{
HAL_LockTypeDef Lock;
} PCD_EPLockDef;
typedef struct
{
PCD_TypeDef *Instance; /*!< Register base address */
PCD_InitTypeDef Init; /*!< PCD required parameters */
PCD_EPTypeDef IN_ep[15]; /*!< IN endpoint parameters */
PCD_EPTypeDef OUT_ep[15]; /*!< OUT endpoint parameters */
HAL_LockTypeDef Lock; /*!< PCD peripheral status */
HAL_LockTypeDef Lock; /*!< PCD peripheral status */
PCD_EPLockDef EPLock[15]; /*!< PCD endpoint peripheral status */
__IO PCD_StateTypeDef State; /*!< PCD communication state */
uint32_t Setup[12]; /*!< Setup packet buffer */
#ifdef USB_OTG_GLPMCFG_LPMEN

View File

@ -607,7 +607,7 @@ HAL_StatusTypeDef USB_EPStartXfer(USB_OTG_GlobalTypeDef *USBx , USB_OTG_EPTypeDe
/* Enable the Tx FIFO Empty Interrupt for this EP */
if (ep->xfer_len > 0U)
{
USBx_DEVICE->DIEPEMPMSK |= 1U << ep->num;
atomic_set_u32(&USBx_DEVICE->DIEPEMPMSK, 1U << ep->num);
}
}
}
@ -725,7 +725,7 @@ HAL_StatusTypeDef USB_EP0StartXfer(USB_OTG_GlobalTypeDef *USBx , USB_OTG_EPTypeD
/* Enable the Tx FIFO Empty Interrupt for this EP */
if (ep->xfer_len > 0U)
{
USBx_DEVICE->DIEPEMPMSK |= 1U << (ep->num);
atomic_set_u32(&USBx_DEVICE->DIEPEMPMSK, 1U << (ep->num));
}
}

View File

@ -122,6 +122,28 @@ typedef enum
(__HANDLE__)->Lock = HAL_UNLOCKED; \
}while (0)
#endif /* USE_RTOS */
#if defined (__CC_ARM)
#pragma diag_suppress 3731
#endif
static inline void atomic_set_u32(volatile uint32_t *ptr, uint32_t mask)
{
uint32_t newValue;
do {
newValue = (uint32_t)__LDREXW((volatile unsigned long *)ptr) | mask;
} while (__STREXW(newValue,(volatile unsigned long*) ptr));
}
static inline void atomic_clr_u32(volatile uint32_t *ptr, uint32_t mask)
{
uint32_t newValue;
do {
newValue = (uint32_t)__LDREXW((volatile unsigned long *)ptr) &~mask;
} while (__STREXW(newValue,(volatile unsigned long*) ptr));
}
#if defined ( __GNUC__ )
#ifndef __weak

View File

@ -145,6 +145,16 @@ HAL_StatusTypeDef HAL_PCD_Init(PCD_HandleTypeDef *hpcd)
/* Check the parameters */
assert_param(IS_PCD_ALL_INSTANCE(hpcd->Instance));
if(hpcd->State == HAL_PCD_STATE_RESET)
{
/* Allocate lock resource and initialize it */
hpcd->Lock = HAL_UNLOCKED;
for (i = 0; i < hpcd->Init.dev_endpoints ; i++)
hpcd->EPLock[i].Lock = HAL_UNLOCKED;
/* Init the low level hardware : GPIO, CLOCK, NVIC... */
HAL_PCD_MspInit(hpcd);
}
hpcd->State = HAL_PCD_STATE_BUSY;
/* Init the low level hardware : GPIO, CLOCK, NVIC... */
@ -186,7 +196,6 @@ HAL_StatusTypeDef HAL_PCD_Init(PCD_HandleTypeDef *hpcd)
hpcd->Instance->DIEPTXF[i] = 0;
}
/* Init Device */
USB_DevInit(hpcd->Instance, hpcd->Init);
@ -406,7 +415,7 @@ void HAL_PCD_IRQHandler(PCD_HandleTypeDef *hpcd)
if(( epint & USB_OTG_DIEPINT_XFRC) == USB_OTG_DIEPINT_XFRC)
{
fifoemptymsk = 0x1 << epnum;
USBx_DEVICE->DIEPEMPMSK &= ~fifoemptymsk;
atomic_clr_u32(&USBx_DEVICE->DIEPEMPMSK, fifoemptymsk);
CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_XFRC);
@ -1014,7 +1023,7 @@ HAL_StatusTypeDef HAL_PCD_EP_Receive(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, u
ep->dma_addr = (uint32_t)pBuf;
}
__HAL_LOCK(hpcd);
__HAL_LOCK(&hpcd->EPLock[ep_addr & 0x7F]);
if ((ep_addr & 0x7F) == 0 )
{
@ -1024,7 +1033,7 @@ HAL_StatusTypeDef HAL_PCD_EP_Receive(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, u
{
USB_EPStartXfer(hpcd->Instance , ep, hpcd->Init.dma_enable);
}
__HAL_UNLOCK(hpcd);
__HAL_UNLOCK(&hpcd->EPLock[ep_addr & 0x7F]);
return HAL_OK;
}
@ -1065,7 +1074,7 @@ HAL_StatusTypeDef HAL_PCD_EP_Transmit(PCD_HandleTypeDef *hpcd, uint8_t ep_addr,
ep->dma_addr = (uint32_t)pBuf;
}
__HAL_LOCK(hpcd);
__HAL_LOCK(&hpcd->EPLock[ep_addr & 0x7F]);
if ((ep_addr & 0x7F) == 0 )
{
@ -1076,8 +1085,8 @@ HAL_StatusTypeDef HAL_PCD_EP_Transmit(PCD_HandleTypeDef *hpcd, uint8_t ep_addr,
USB_EPStartXfer(hpcd->Instance , ep, hpcd->Init.dma_enable);
}
__HAL_UNLOCK(hpcd);
__HAL_UNLOCK(&hpcd->EPLock[ep_addr & 0x7F]);
return HAL_OK;
}
@ -1105,13 +1114,13 @@ HAL_StatusTypeDef HAL_PCD_EP_SetStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
ep->is_in = ((ep_addr & 0x80) == 0x80);
__HAL_LOCK(hpcd);
__HAL_LOCK(&hpcd->EPLock[ep_addr & 0x7F]);
USB_EPSetStall(hpcd->Instance , ep);
if((ep_addr & 0x7F) == 0)
{
USB_EP0_OutStart(hpcd->Instance, hpcd->Init.dma_enable, (uint8_t *)hpcd->Setup);
}
__HAL_UNLOCK(hpcd);
__HAL_UNLOCK(&hpcd->EPLock[ep_addr & 0x7F]);
return HAL_OK;
}
@ -1139,9 +1148,9 @@ HAL_StatusTypeDef HAL_PCD_EP_ClrStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
ep->num = ep_addr & 0x7F;
ep->is_in = ((ep_addr & 0x80) == 0x80);
__HAL_LOCK(hpcd);
__HAL_LOCK(&hpcd->EPLock[ep_addr & 0x7F]);
USB_EPClearStall(hpcd->Instance , ep);
__HAL_UNLOCK(hpcd);
__HAL_UNLOCK(&hpcd->EPLock[ep_addr & 0x7F]);
return HAL_OK;
}
@ -1154,7 +1163,7 @@ HAL_StatusTypeDef HAL_PCD_EP_ClrStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
*/
HAL_StatusTypeDef HAL_PCD_EP_Flush(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
{
__HAL_LOCK(hpcd);
__HAL_LOCK(&hpcd->EPLock[ep_addr & 0x7F]);
if ((ep_addr & 0x80) == 0x80)
{
@ -1165,7 +1174,7 @@ HAL_StatusTypeDef HAL_PCD_EP_Flush(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
USB_FlushRxFifo(hpcd->Instance);
}
__HAL_UNLOCK(hpcd);
__HAL_UNLOCK(&hpcd->EPLock[ep_addr & 0x7F]);
return HAL_OK;
}
@ -1288,7 +1297,7 @@ static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t
if(len <= 0)
{
fifoemptymsk = 0x1 << epnum;
USBx_DEVICE->DIEPEMPMSK &= ~fifoemptymsk;
atomic_clr_u32(&USBx_DEVICE->DIEPEMPMSK, fifoemptymsk);
}

View File

@ -83,7 +83,10 @@ typedef enum
typedef USB_OTG_GlobalTypeDef PCD_TypeDef;
typedef USB_OTG_CfgTypeDef PCD_InitTypeDef;
typedef USB_OTG_EPTypeDef PCD_EPTypeDef ;
typedef struct
{
HAL_LockTypeDef Lock;
} PCD_EPLockDef;
/**
* @brief PCD Handle Structure definition
*/
@ -94,6 +97,7 @@ typedef struct
PCD_EPTypeDef IN_ep[15]; /*!< IN endpoint parameters */
PCD_EPTypeDef OUT_ep[15]; /*!< OUT endpoint parameters */
HAL_LockTypeDef Lock; /*!< PCD peripheral status */
PCD_EPLockDef EPLock[15];
__IO PCD_StateTypeDef State; /*!< PCD communication state */
uint32_t Setup[12]; /*!< Setup packet buffer */
PCD_LPM_StateTypeDef LPM_State; /*!< LPM State */

View File

@ -594,7 +594,7 @@ HAL_StatusTypeDef USB_EPStartXfer(USB_OTG_GlobalTypeDef *USBx , USB_OTG_EPTypeDe
/* Enable the Tx FIFO Empty Interrupt for this EP */
if (ep->xfer_len > 0)
{
USBx_DEVICE->DIEPEMPMSK |= 1 << ep->num;
atomic_set_u32(&USBx_DEVICE->DIEPEMPMSK, 1 << ep->num);
}
}
}
@ -712,7 +712,7 @@ HAL_StatusTypeDef USB_EP0StartXfer(USB_OTG_GlobalTypeDef *USBx , USB_OTG_EPTypeD
/* Enable the Tx FIFO Empty Interrupt for this EP */
if (ep->xfer_len > 0)
{
USBx_DEVICE->DIEPEMPMSK |= 1 << (ep->num);
atomic_set_u32(&USBx_DEVICE->DIEPEMPMSK, 1 << (ep->num));
}
}

View File

@ -124,6 +124,28 @@ typedef enum
(__HANDLE__)->Lock = HAL_UNLOCKED; \
}while (0)
#endif /* USE_RTOS */
#if defined (__CC_ARM)
#pragma diag_suppress 3731
#endif
static inline void atomic_set_u32(volatile uint32_t *ptr, uint32_t mask)
{
uint32_t newValue;
do {
newValue = (uint32_t)__LDREXW((volatile unsigned long *)ptr) | mask;
} while (__STREXW(newValue,(volatile unsigned long*) ptr));
}
static inline void atomic_clr_u32(volatile uint32_t *ptr, uint32_t mask)
{
uint32_t newValue;
do {
newValue = (uint32_t)__LDREXW((volatile unsigned long *)ptr) &~mask;
} while (__STREXW(newValue,(volatile unsigned long*) ptr));
}
#if defined ( __GNUC__ )
#ifndef __weak

View File

@ -157,7 +157,8 @@ HAL_StatusTypeDef HAL_PCD_Init(PCD_HandleTypeDef *hpcd)
{
/* Allocate lock resource and initialize it */
hpcd->Lock = HAL_UNLOCKED;
for (index = 0; index < hpcd->Init.dev_endpoints ; index++)
hpcd->EPLock[index].Lock = HAL_UNLOCKED;
/* Init the low level hardware : GPIO, CLOCK, NVIC... */
HAL_PCD_MspInit(hpcd);
}
@ -198,7 +199,6 @@ HAL_StatusTypeDef HAL_PCD_Init(PCD_HandleTypeDef *hpcd)
hpcd->OUT_ep[index].xfer_buff = 0;
hpcd->OUT_ep[index].xfer_len = 0;
}
/* Init Device */
USB_DevInit(hpcd->Instance, hpcd->Init);
@ -426,7 +426,7 @@ void HAL_PCD_IRQHandler(PCD_HandleTypeDef *hpcd)
if(( epint & USB_OTG_DIEPINT_XFRC) == USB_OTG_DIEPINT_XFRC)
{
fifoemptymsk = 0x1 << epnum;
USBx_DEVICE->DIEPEMPMSK &= ~fifoemptymsk;
atomic_clr_u32(&USBx_DEVICE->DIEPEMPMSK, fifoemptymsk);
CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_XFRC);
@ -1141,7 +1141,7 @@ HAL_StatusTypeDef HAL_PCD_EP_Receive(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, u
ep->is_in = 0;
ep->num = ep_addr & 0x7F;
__HAL_LOCK(hpcd);
__HAL_LOCK(&hpcd->EPLock[ep_addr & 0x7F]);
if ((ep_addr & 0x7F) == 0 )
{
@ -1151,7 +1151,7 @@ HAL_StatusTypeDef HAL_PCD_EP_Receive(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, u
{
USB_EPStartXfer(hpcd->Instance, ep, hpcd->Init.dma_enable);
}
__HAL_UNLOCK(hpcd);
__HAL_UNLOCK(&hpcd->EPLock[ep_addr & 0x7F]);
return HAL_OK;
}
@ -1187,7 +1187,7 @@ HAL_StatusTypeDef HAL_PCD_EP_Transmit(PCD_HandleTypeDef *hpcd, uint8_t ep_addr,
ep->is_in = 1;
ep->num = ep_addr & 0x7F;
__HAL_LOCK(hpcd);
__HAL_LOCK(&hpcd->EPLock[ep_addr & 0x7F]);
if ((ep_addr & 0x7F) == 0 )
{
@ -1198,7 +1198,7 @@ HAL_StatusTypeDef HAL_PCD_EP_Transmit(PCD_HandleTypeDef *hpcd, uint8_t ep_addr,
USB_EPStartXfer(hpcd->Instance, ep, hpcd->Init.dma_enable);
}
__HAL_UNLOCK(hpcd);
__HAL_UNLOCK(&hpcd->EPLock[ep_addr & 0x7F]);
return HAL_OK;
}
@ -1226,13 +1226,13 @@ HAL_StatusTypeDef HAL_PCD_EP_SetStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
ep->num = ep_addr & 0x7F;
ep->is_in = ((ep_addr & 0x80) == 0x80);
__HAL_LOCK(hpcd);
__HAL_LOCK(&hpcd->EPLock[ep_addr & 0x7F]);
USB_EPSetStall(hpcd->Instance , ep);
if((ep_addr & 0x7F) == 0)
{
USB_EP0_OutStart(hpcd->Instance, hpcd->Init.dma_enable, (uint8_t *)hpcd->Setup);
}
__HAL_UNLOCK(hpcd);
__HAL_UNLOCK(&hpcd->EPLock[ep_addr & 0x7F]);
return HAL_OK;
}
@ -1260,9 +1260,9 @@ HAL_StatusTypeDef HAL_PCD_EP_ClrStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
ep->num = ep_addr & 0x7F;
ep->is_in = ((ep_addr & 0x80) == 0x80);
__HAL_LOCK(hpcd);
__HAL_LOCK(&hpcd->EPLock[ep_addr & 0x7F]);
USB_EPClearStall(hpcd->Instance , ep);
__HAL_UNLOCK(hpcd);
__HAL_UNLOCK(&hpcd->EPLock[ep_addr & 0x7F]);
return HAL_OK;
}
@ -1275,8 +1275,7 @@ HAL_StatusTypeDef HAL_PCD_EP_ClrStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
*/
HAL_StatusTypeDef HAL_PCD_EP_Flush(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
{
__HAL_LOCK(hpcd);
__HAL_LOCK(&hpcd->EPLock[ep_addr & 0x7F]);
if ((ep_addr & 0x80) == 0x80)
{
USB_FlushTxFifo(hpcd->Instance, ep_addr & 0x7F);
@ -1286,7 +1285,7 @@ HAL_StatusTypeDef HAL_PCD_EP_Flush(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
USB_FlushRxFifo(hpcd->Instance);
}
__HAL_UNLOCK(hpcd);
__HAL_UNLOCK(&hpcd->EPLock[ep_addr & 0x7F]);
return HAL_OK;
}
@ -1398,7 +1397,7 @@ static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t
if(len <= 0)
{
fifoemptymsk = 0x1 << epnum;
USBx_DEVICE->DIEPEMPMSK &= ~fifoemptymsk;
atomic_clr_u32(&USBx_DEVICE->DIEPEMPMSK, fifoemptymsk);
}

View File

@ -118,6 +118,10 @@ typedef USB_TypeDef PCD_TypeDef;
typedef USB_CfgTypeDef PCD_InitTypeDef;
typedef USB_EPTypeDef PCD_EPTypeDef;
#endif /* USB */
typedef struct
{
HAL_LockTypeDef Lock;
} PCD_EPLockDef;
/**
* @brief PCD Handle Structure definition
@ -130,6 +134,7 @@ typedef struct
PCD_EPTypeDef IN_ep[15]; /*!< IN endpoint parameters */
PCD_EPTypeDef OUT_ep[15]; /*!< OUT endpoint parameters */
HAL_LockTypeDef Lock; /*!< PCD peripheral status */
PCD_EPLockDef EPLock[15];
__IO PCD_StateTypeDef State; /*!< PCD communication state */
uint32_t Setup[12]; /*!< Setup packet buffer */
PCD_LPM_StateTypeDef LPM_State; /*!< LPM State */

View File

@ -571,7 +571,7 @@ HAL_StatusTypeDef USB_EPStartXfer(USB_OTG_GlobalTypeDef *USBx , USB_OTG_EPTypeDe
/* Enable the Tx FIFO Empty Interrupt for this EP */
if (ep->xfer_len > 0)
{
USBx_DEVICE->DIEPEMPMSK |= 1 << ep->num;
atomic_set_u32(&USBx_DEVICE->DIEPEMPMSK, 1 << ep->num);
}
}
@ -677,7 +677,7 @@ HAL_StatusTypeDef USB_EP0StartXfer(USB_OTG_GlobalTypeDef *USBx , USB_OTG_EPTypeD
/* Enable the Tx FIFO Empty Interrupt for this EP */
if (ep->xfer_len > 0)
{
USBx_DEVICE->DIEPEMPMSK |= 1 << (ep->num);
atomic_set_u32(&USBx_DEVICE->DIEPEMPMSK, 1 << (ep->num));
}
/* EP enable, IN data in FIFO */