-
Notifications
You must be signed in to change notification settings - Fork 3k
STM32L4: Add support of the new USB Device API #8665
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
0xc0170
merged 4 commits into
ARMmbed:feature-hal-spec-usb-device
from
bcostm:L4_feature-hal-spec-usb-device
Jan 22, 2019
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -113,6 +113,7 @@ | |
*/ | ||
#if defined (USB_OTG_FS) | ||
static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t epnum); | ||
static HAL_StatusTypeDef PCD_ReadRxFifo(PCD_HandleTypeDef *hpcd); // MBED PATCH | ||
#endif /* USB_OTG_FS */ | ||
#if defined (USB) | ||
static HAL_StatusTypeDef PCD_EP_ISR_Handler(PCD_HandleTypeDef *hpcd); | ||
|
@@ -162,11 +163,9 @@ HAL_StatusTypeDef HAL_PCD_Init(PCD_HandleTypeDef *hpcd) | |
{ | ||
/* Allocate lock resource and initialize it */ | ||
hpcd->Lock = HAL_UNLOCKED; | ||
|
||
// Added for MBED PR #3062 | ||
for (index = 0; index < hpcd->Init.dev_endpoints ; index++) | ||
hpcd->EPLock[index].Lock = HAL_UNLOCKED; | ||
|
||
for (index = 0; index < hpcd->Init.dev_endpoints ; index++) { // MBED PATCH | ||
hpcd->EPLock[index].Lock = HAL_UNLOCKED; | ||
} | ||
/* Init the low level hardware : GPIO, CLOCK, NVIC... */ | ||
HAL_PCD_MspInit(hpcd); | ||
} | ||
|
@@ -311,10 +310,10 @@ __weak void HAL_PCD_MspDeInit(PCD_HandleTypeDef *hpcd) | |
*/ | ||
HAL_StatusTypeDef HAL_PCD_Start(PCD_HandleTypeDef *hpcd) | ||
{ | ||
__HAL_LOCK(hpcd); | ||
// MBED PATCH __HAL_LOCK(hpcd); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why was the lock removed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It has been removed in the F4 file so I just did the same... |
||
USB_DevConnect (hpcd->Instance); | ||
__HAL_PCD_ENABLE(hpcd); | ||
__HAL_UNLOCK(hpcd); | ||
// MBED PATCH __HAL_UNLOCK(hpcd); | ||
return HAL_OK; | ||
} | ||
|
||
|
@@ -325,13 +324,14 @@ HAL_StatusTypeDef HAL_PCD_Start(PCD_HandleTypeDef *hpcd) | |
*/ | ||
HAL_StatusTypeDef HAL_PCD_Stop(PCD_HandleTypeDef *hpcd) | ||
{ | ||
__HAL_LOCK(hpcd); | ||
// MBED PATCH __HAL_LOCK(hpcd); | ||
__HAL_PCD_DISABLE(hpcd); | ||
USB_StopDevice(hpcd->Instance); | ||
USB_DevDisconnect (hpcd->Instance); | ||
__HAL_UNLOCK(hpcd); | ||
// MBED PATCH __HAL_UNLOCK(hpcd); | ||
return HAL_OK; | ||
} | ||
|
||
#if defined (USB_OTG_FS) | ||
/** | ||
* @brief Handles PCD interrupt request. | ||
|
@@ -431,6 +431,13 @@ void HAL_PCD_IRQHandler(PCD_HandleTypeDef *hpcd) | |
} | ||
} | ||
|
||
// MBED PATCH | ||
if (( epint & USB_OTG_DOEPINT_EPDISD) == USB_OTG_DOEPINT_EPDISD) | ||
{ | ||
CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_EPDISD); | ||
} | ||
// MBED PATCH | ||
|
||
if(( epint & USB_OTG_DOEPINT_STUP) == USB_OTG_DOEPINT_STUP) | ||
{ | ||
/* Inform the upper layer that a setup packet is available */ | ||
|
@@ -473,8 +480,7 @@ void HAL_PCD_IRQHandler(PCD_HandleTypeDef *hpcd) | |
{ | ||
fifoemptymsk = 0x1 << epnum; | ||
|
||
// Added for MBED PR #3062 | ||
atomic_clr_u32(&USBx_DEVICE->DIEPEMPMSK, fifoemptymsk); | ||
atomic_clr_u32(&USBx_DEVICE->DIEPEMPMSK, fifoemptymsk); // MBED PATCH | ||
|
||
CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_XFRC); | ||
|
||
|
@@ -690,27 +696,7 @@ void HAL_PCD_IRQHandler(PCD_HandleTypeDef *hpcd) | |
/* Handle RxQLevel Interrupt */ | ||
if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_RXFLVL)) | ||
{ | ||
USB_MASK_INTERRUPT(hpcd->Instance, USB_OTG_GINTSTS_RXFLVL); | ||
|
||
temp = USBx->GRXSTSP; | ||
|
||
ep = &hpcd->OUT_ep[temp & USB_OTG_GRXSTSP_EPNUM]; | ||
|
||
if(((temp & USB_OTG_GRXSTSP_PKTSTS) >> 17) == STS_DATA_UPDT) | ||
{ | ||
if((temp & USB_OTG_GRXSTSP_BCNT) != 0) | ||
{ | ||
USB_ReadPacket(USBx, ep->xfer_buff, (temp & USB_OTG_GRXSTSP_BCNT) >> 4); | ||
ep->xfer_buff += (temp & USB_OTG_GRXSTSP_BCNT) >> 4; | ||
ep->xfer_count += (temp & USB_OTG_GRXSTSP_BCNT) >> 4; | ||
} | ||
} | ||
else if (((temp & USB_OTG_GRXSTSP_PKTSTS) >> 17) == STS_SETUP_UPDT) | ||
{ | ||
USB_ReadPacket(USBx, (uint8_t *)hpcd->Setup, 8); | ||
ep->xfer_count += (temp & USB_OTG_GRXSTSP_BCNT) >> 4; | ||
} | ||
USB_UNMASK_INTERRUPT(hpcd->Instance, USB_OTG_GINTSTS_RXFLVL); | ||
PCD_ReadRxFifo(hpcd); // MBED PATCH | ||
} | ||
|
||
/* Handle SOF Interrupt */ | ||
|
@@ -755,6 +741,86 @@ void HAL_PCD_IRQHandler(PCD_HandleTypeDef *hpcd) | |
} | ||
} | ||
|
||
// MBED PATCH | ||
/** | ||
* @brief Abort a transaction. | ||
* @param hpcd: PCD handle | ||
* @param ep_addr: endpoint address | ||
* @retval HAL status | ||
*/ | ||
HAL_StatusTypeDef HAL_PCD_EP_Abort(PCD_HandleTypeDef *hpcd, uint8_t ep_addr) | ||
{ | ||
USB_OTG_GlobalTypeDef *USBx = hpcd->Instance; | ||
HAL_StatusTypeDef ret = HAL_OK; | ||
USB_OTG_EPTypeDef *ep; | ||
|
||
if ((0x80 & ep_addr) == 0x80) | ||
{ | ||
ep = &hpcd->IN_ep[ep_addr & 0x7F]; | ||
} | ||
else | ||
{ | ||
ep = &hpcd->OUT_ep[ep_addr]; | ||
} | ||
|
||
__HAL_LOCK(&hpcd->EPLock[ep_addr & 0x7F]); | ||
|
||
ep->num = ep_addr & 0x7F; | ||
ep->is_in = ((ep_addr & 0x80) == 0x80); | ||
|
||
USB_EPSetNak(hpcd->Instance, ep); | ||
|
||
if ((0x80 & ep_addr) == 0x80) | ||
{ | ||
ret = USB_EPStopXfer(hpcd->Instance , ep); | ||
if (ret == HAL_OK) | ||
{ | ||
ret = USB_FlushTxFifo(hpcd->Instance, ep_addr & 0x7F); | ||
} | ||
} | ||
else | ||
{ | ||
/* Set global NAK */ | ||
USBx_DEVICE->DCTL |= USB_OTG_DCTL_SGONAK; | ||
|
||
/* Read all entries from the fifo so global NAK takes effect */ | ||
while (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_RXFLVL)) | ||
{ | ||
PCD_ReadRxFifo(hpcd); | ||
} | ||
|
||
/* Stop the transfer */ | ||
ret = USB_EPStopXfer(hpcd->Instance , ep); | ||
if (ret == HAL_BUSY) | ||
{ | ||
/* If USB_EPStopXfer returns HAL_BUSY then a setup packet | ||
* arrived after the rx fifo was processed but before USB_EPStopXfer | ||
* was called. Process the rx fifo one more time to read the | ||
* setup packet. | ||
* | ||
* Note - after the setup packet has been received no further | ||
* packets will be received over USB. This is because the next | ||
* phase (data or status) of the control transfer started by | ||
* the setup packet will be naked until global nak is cleared. | ||
*/ | ||
while (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_RXFLVL)) | ||
{ | ||
PCD_ReadRxFifo(hpcd); | ||
} | ||
|
||
ret = USB_EPStopXfer(hpcd->Instance , ep); | ||
} | ||
|
||
/* Clear global nak */ | ||
USBx_DEVICE->DCTL |= USB_OTG_DCTL_CGONAK; | ||
} | ||
|
||
__HAL_UNLOCK(&hpcd->EPLock[ep_addr & 0x7F]); | ||
|
||
return ret; | ||
} | ||
// MBED PATCH | ||
|
||
#endif /* USB_OTG_FS */ | ||
|
||
#if defined (USB) | ||
|
@@ -1135,9 +1201,11 @@ HAL_StatusTypeDef HAL_PCD_EP_Open(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint | |
ep->maxpacket = ep_mps; | ||
ep->type = ep_type; | ||
|
||
__HAL_LOCK(hpcd); | ||
// MBED PATCH __HAL_LOCK(hpcd); | ||
__HAL_LOCK(&hpcd->EPLock[ep_addr & 0x7FU]); // MBED PATCH | ||
USB_ActivateEndpoint(hpcd->Instance , ep); | ||
__HAL_UNLOCK(hpcd); | ||
// MBED PATCH __HAL_UNLOCK(hpcd); | ||
__HAL_UNLOCK(&hpcd->EPLock[ep_addr & 0x7FU]); // MBED PATCH | ||
return ret; | ||
|
||
} | ||
|
@@ -1165,9 +1233,11 @@ HAL_StatusTypeDef HAL_PCD_EP_Close(PCD_HandleTypeDef *hpcd, uint8_t ep_addr) | |
|
||
ep->is_in = (0x80 & ep_addr) != 0; | ||
|
||
__HAL_LOCK(hpcd); | ||
// MBED PATCH __HAL_LOCK(hpcd); | ||
__HAL_LOCK(&hpcd->EPLock[ep_addr & 0x7FU]); // MBED PATCH | ||
USB_DeactivateEndpoint(hpcd->Instance , ep); | ||
__HAL_UNLOCK(hpcd); | ||
// MBED PATCH __HAL_UNLOCK(hpcd); | ||
__HAL_UNLOCK(&hpcd->EPLock[ep_addr & 0x7FU]); // MBED PATCH | ||
return HAL_OK; | ||
} | ||
|
||
|
@@ -1193,8 +1263,7 @@ HAL_StatusTypeDef HAL_PCD_EP_Receive(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, u | |
ep->is_in = 0; | ||
ep->num = ep_addr & 0x7F; | ||
|
||
// Added for MBED PR #3062 | ||
__HAL_LOCK(&hpcd->EPLock[ep_addr & 0x7F]); | ||
__HAL_LOCK(&hpcd->EPLock[ep_addr & 0x7F]); // MBED PATCH | ||
|
||
if ((ep_addr & 0x7F) == 0 ) | ||
{ | ||
|
@@ -1205,8 +1274,7 @@ HAL_StatusTypeDef HAL_PCD_EP_Receive(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, u | |
USB_EPStartXfer(hpcd->Instance, ep, hpcd->Init.dma_enable); | ||
} | ||
|
||
// Added for MBED PR #3062 | ||
__HAL_UNLOCK(&hpcd->EPLock[ep_addr & 0x7F]); | ||
__HAL_UNLOCK(&hpcd->EPLock[ep_addr & 0x7F]); // MBED PATCH | ||
|
||
return HAL_OK; | ||
} | ||
|
@@ -1242,9 +1310,8 @@ HAL_StatusTypeDef HAL_PCD_EP_Transmit(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, | |
ep->is_in = 1; | ||
ep->num = ep_addr & 0x7F; | ||
|
||
// Added for MBED PR #3062 | ||
__HAL_LOCK(&hpcd->EPLock[ep_addr & 0x7F]); | ||
|
||
__HAL_LOCK(&hpcd->EPLock[ep_addr & 0x7F]); // MBED PATCH | ||
|
||
if ((ep_addr & 0x7F) == 0 ) | ||
{ | ||
USB_EP0StartXfer(hpcd->Instance,ep, hpcd->Init.dma_enable); | ||
|
@@ -1254,9 +1321,8 @@ HAL_StatusTypeDef HAL_PCD_EP_Transmit(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, | |
USB_EPStartXfer(hpcd->Instance, ep, hpcd->Init.dma_enable); | ||
} | ||
|
||
// Added for MBED PR #3062 | ||
__HAL_UNLOCK(&hpcd->EPLock[ep_addr & 0x7F]); | ||
|
||
__HAL_UNLOCK(&hpcd->EPLock[ep_addr & 0x7F]); // MBED PATCH | ||
|
||
return HAL_OK; | ||
} | ||
|
||
|
@@ -1283,17 +1349,15 @@ 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); | ||
|
||
// Added for MBED PR #3062 | ||
__HAL_LOCK(&hpcd->EPLock[ep_addr & 0x7F]); | ||
__HAL_LOCK(&hpcd->EPLock[ep_addr & 0x7F]); // MBED PATCH | ||
|
||
USB_EPSetStall(hpcd->Instance , ep); | ||
if((ep_addr & 0x7F) == 0) | ||
{ | ||
USB_EP0_OutStart(hpcd->Instance, hpcd->Init.dma_enable, (uint8_t *)hpcd->Setup); | ||
} | ||
|
||
// Added for MBED PR #3062 | ||
__HAL_UNLOCK(&hpcd->EPLock[ep_addr & 0x7F]); | ||
__HAL_UNLOCK(&hpcd->EPLock[ep_addr & 0x7F]); // MBED PATCH | ||
|
||
return HAL_OK; | ||
} | ||
|
@@ -1321,13 +1385,11 @@ 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); | ||
|
||
// Added for MBED PR #3062 | ||
__HAL_LOCK(&hpcd->EPLock[ep_addr & 0x7F]); | ||
__HAL_LOCK(&hpcd->EPLock[ep_addr & 0x7F]); // MBED PATCH | ||
|
||
USB_EPClearStall(hpcd->Instance , ep); | ||
|
||
// Added for MBED PR #3062 | ||
__HAL_UNLOCK(&hpcd->EPLock[ep_addr & 0x7F]); | ||
__HAL_UNLOCK(&hpcd->EPLock[ep_addr & 0x7F]); // MBED PATCH | ||
|
||
return HAL_OK; | ||
} | ||
|
@@ -1340,8 +1402,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) | ||
{ | ||
// Added for MBED PR #3062 | ||
__HAL_LOCK(&hpcd->EPLock[ep_addr & 0x7F]); | ||
__HAL_LOCK(&hpcd->EPLock[ep_addr & 0x7F]); // MBED PATCH | ||
|
||
if ((ep_addr & 0x80) == 0x80) | ||
{ | ||
|
@@ -1352,9 +1413,8 @@ HAL_StatusTypeDef HAL_PCD_EP_Flush(PCD_HandleTypeDef *hpcd, uint8_t ep_addr) | |
USB_FlushRxFifo(hpcd->Instance); | ||
} | ||
|
||
// Added for MBED PR #3062 | ||
__HAL_UNLOCK(&hpcd->EPLock[ep_addr & 0x7F]); | ||
|
||
__HAL_UNLOCK(&hpcd->EPLock[ep_addr & 0x7F]); // MBED PATCH | ||
|
||
return HAL_OK; | ||
} | ||
|
||
|
@@ -1465,13 +1525,50 @@ static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t | |
if (ep->xfer_count >= ep->xfer_len) | ||
{ | ||
fifoemptymsk = 0x1 << epnum; | ||
// Added for MBED PR #3062 | ||
atomic_clr_u32(&USBx_DEVICE->DIEPEMPMSK, fifoemptymsk); | ||
|
||
atomic_clr_u32(&USBx_DEVICE->DIEPEMPMSK, fifoemptymsk); // MBED PATCH | ||
} | ||
|
||
return HAL_OK; | ||
} | ||
|
||
// MBED PATCH | ||
/** | ||
* @brief Process the next RX fifo entry | ||
* @param hpcd: PCD handle | ||
* @retval HAL status | ||
*/ | ||
static HAL_StatusTypeDef PCD_ReadRxFifo(PCD_HandleTypeDef *hpcd) | ||
{ | ||
USB_OTG_GlobalTypeDef *USBx = hpcd->Instance; | ||
USB_OTG_EPTypeDef *ep; | ||
uint32_t temp = 0; | ||
|
||
USB_MASK_INTERRUPT(hpcd->Instance, USB_OTG_GINTSTS_RXFLVL); | ||
|
||
temp = USBx->GRXSTSP; | ||
|
||
ep = &hpcd->OUT_ep[temp & USB_OTG_GRXSTSP_EPNUM]; | ||
|
||
if(((temp & USB_OTG_GRXSTSP_PKTSTS) >> 17U) == STS_DATA_UPDT) | ||
{ | ||
if((temp & USB_OTG_GRXSTSP_BCNT) != 0U) | ||
{ | ||
USB_ReadPacket(USBx, ep->xfer_buff, (temp & USB_OTG_GRXSTSP_BCNT) >> 4U); | ||
ep->xfer_buff += (temp & USB_OTG_GRXSTSP_BCNT) >> 4U; | ||
ep->xfer_count += (temp & USB_OTG_GRXSTSP_BCNT) >> 4U; | ||
} | ||
} | ||
else if (((temp & USB_OTG_GRXSTSP_PKTSTS) >> 17U) == STS_SETUP_UPDT) | ||
{ | ||
USB_ReadPacket(USBx, (uint8_t *)hpcd->Setup, 8U); | ||
ep->xfer_count += (temp & USB_OTG_GRXSTSP_BCNT) >> 4U; | ||
} | ||
USB_UNMASK_INTERRUPT(hpcd->Instance, USB_OTG_GINTSTS_RXFLVL); | ||
|
||
return HAL_OK; | ||
} | ||
// MBED PATCH | ||
|
||
#endif /* USB_OTG_FS */ | ||
|
||
#if defined (USB) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this just cosmetic?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes