Release v1.4.3
diff --git a/Inc/stm32g0xx_hal_hcd.h b/Inc/stm32g0xx_hal_hcd.h index d849cde..b0f29b9 100644 --- a/Inc/stm32g0xx_hal_hcd.h +++ b/Inc/stm32g0xx_hal_hcd.h
@@ -90,6 +90,7 @@ HCD_TypeDef *Instance; /*!< Register base address */ HCD_InitTypeDef Init; /*!< HCD required parameters */ HCD_HCTypeDef hc[16]; /*!< Host channels parameters */ + uint32_t ep0_PmaAllocState; /*!< EP0 PMA allocation State (allocated, virtual Ch, EP0 direction) */ uint16_t phy_chin_state[8]; /*!< Physical Channel in State (Used/Free) */ uint16_t phy_chout_state[8]; /*!< Physical Channel out State (Used/Free)*/ uint32_t PMALookupTable[PMA_BLOCKS]; /*PMA LookUp Table */ @@ -362,7 +363,7 @@ * @param bChNum, bDir * @retval None */ -#define HCD_FreeUserBuffer USB_DRD_FreeUserBuffer +#define HCD_FREE_USER_BUFFER USB_DRD_FREE_USER_BUFFER /** * @brief Set the Setup bit in the corresponding channel, when a Setup @@ -400,12 +401,15 @@ #define HCD_GET_CH_TX_STATUS USB_DRD_GET_CHEP_TX_STATUS #define HCD_GET_CH_RX_STATUS USB_DRD_GET_CHEP_RX_STATUS /** - * @brief Sets/clears directly EP_KIND bit in the endpoint register. + * @brief Sets/clears CH_KIND bit in the Channel register. * @param USBx USB peripheral instance register address. * @param bChNum Endpoint Number. * @retval None */ -#define HCD_SET_CH_DBUF USB_DRD_SET_CH_KIND +#define HCD_SET_CH_KIND USB_DRD_SET_CH_KIND +#define HCD_CLEAR_CH_KIND USB_DRD_CLEAR_CH_KIND +#define HCD_SET_BULK_CH_DBUF HCD_SET_CH_KIND +#define HCD_CLEAR_BULK_CH_DBUF HCD_CLEAR_CH_KIND /** * @brief Clears bit ERR_RX in the Channel register
diff --git a/Inc/stm32g0xx_hal_pcd.h b/Inc/stm32g0xx_hal_pcd.h index 5f3e294..4f48c94 100644 --- a/Inc/stm32g0xx_hal_pcd.h +++ b/Inc/stm32g0xx_hal_pcd.h
@@ -188,16 +188,16 @@ */ -#define __HAL_PCD_ENABLE(__HANDLE__) (void)USB_EnableGlobalInt ((__HANDLE__)->Instance) -#define __HAL_PCD_DISABLE(__HANDLE__) (void)USB_DisableGlobalInt ((__HANDLE__)->Instance) -#define __HAL_PCD_GET_FLAG(__HANDLE__, __INTERRUPT__) ((USB_ReadInterrupts((__HANDLE__)->Instance)\ - & (__INTERRUPT__)) == (__INTERRUPT__)) +#define __HAL_PCD_ENABLE(__HANDLE__) (void)USB_EnableGlobalInt ((__HANDLE__)->Instance) +#define __HAL_PCD_DISABLE(__HANDLE__) (void)USB_DisableGlobalInt ((__HANDLE__)->Instance) +#define __HAL_PCD_GET_FLAG(__HANDLE__, __INTERRUPT__) ((USB_ReadInterrupts((__HANDLE__)->Instance)\ + & (__INTERRUPT__)) == (__INTERRUPT__)) -#define __HAL_PCD_CLEAR_FLAG(__HANDLE__, __INTERRUPT__) (((__HANDLE__)->Instance->ISTR)\ - &= (uint16_t)(~(__INTERRUPT__))) +#define __HAL_PCD_CLEAR_FLAG(__HANDLE__, __INTERRUPT__) (((__HANDLE__)->Instance->ISTR)\ + &= (uint16_t)(~(__INTERRUPT__))) -#define __HAL_USB_WAKEUP_EXTI_ENABLE_IT() EXTI->IMR2 |= USB_WAKEUP_EXTI_LINE -#define __HAL_USB_WAKEUP_EXTI_DISABLE_IT() EXTI->IMR2 &= ~(USB_WAKEUP_EXTI_LINE) +#define __HAL_USB_WAKEUP_EXTI_ENABLE_IT() EXTI->IMR2 |= USB_WAKEUP_EXTI_LINE +#define __HAL_USB_WAKEUP_EXTI_DISABLE_IT() EXTI->IMR2 &= ~(USB_WAKEUP_EXTI_LINE) /** @@ -341,13 +341,12 @@ HAL_StatusTypeDef HAL_PCD_EP_Transmit(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len); - HAL_StatusTypeDef HAL_PCD_EP_SetStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr); 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_StatusTypeDef HAL_PCD_EP_Abort(PCD_HandleTypeDef *hpcd, uint8_t ep_addr); HAL_StatusTypeDef HAL_PCD_ActivateRemoteWakeup(PCD_HandleTypeDef *hpcd); HAL_StatusTypeDef HAL_PCD_DeActivateRemoteWakeup(PCD_HandleTypeDef *hpcd); - uint32_t HAL_PCD_EP_GetRxCount(PCD_HandleTypeDef *hpcd, uint8_t ep_addr); /** * @} @@ -441,7 +440,7 @@ * @param bEpNum, bDir * @retval None */ -#define PCD_FreeUserBuffer USB_DRD_FreeUserBuffer +#define PCD_FREE_USER_BUFFER USB_DRD_FREE_USER_BUFFER /** * @brief sets the status for tx transfer (bits STAT_TX[1:0]). @@ -467,8 +466,11 @@ * @param bEpNum Endpoint Number. * @retval None */ -#define PCD_SET_EP_DBUF USB_DRD_SET_CHEP_KIND -#define PCD_CLEAR_EP_DBUF USB_DRD_CLEAR_CHEP_KIND +#define PCD_SET_EP_KIND USB_DRD_SET_CHEP_KIND +#define PCD_CLEAR_EP_KIND USB_DRD_CLEAR_CHEP_KIND +#define PCD_SET_BULK_EP_DBUF PCD_SET_EP_KIND +#define PCD_CLEAR_BULK_EP_DBUF PCD_CLEAR_EP_KIND + /** * @brief Clears bit CTR_RX / CTR_TX in the endpoint register.
diff --git a/Inc/stm32g0xx_hal_pcd_ex.h b/Inc/stm32g0xx_hal_pcd_ex.h index 952abbc..6b3a439 100644 --- a/Inc/stm32g0xx_hal_pcd_ex.h +++ b/Inc/stm32g0xx_hal_pcd_ex.h
@@ -23,7 +23,7 @@ #ifdef __cplusplus extern "C" { -#endif +#endif /* __cplusplus */ /* Includes ------------------------------------------------------------------*/ #include "stm32g0xx_hal_def.h" @@ -83,7 +83,7 @@ #ifdef __cplusplus } -#endif +#endif /* __cplusplus */ #endif /* STM32G0xx_HAL_PCD_EX_H */
diff --git a/Inc/stm32g0xx_ll_usb.h b/Inc/stm32g0xx_ll_usb.h index 058bcf5..e0e1641 100644 --- a/Inc/stm32g0xx_ll_usb.h +++ b/Inc/stm32g0xx_ll_usb.h
@@ -23,7 +23,7 @@ #ifdef __cplusplus extern "C" { -#endif +#endif /* __cplusplus */ /* Includes ------------------------------------------------------------------*/ #include "stm32g0xx_hal_def.h" @@ -80,14 +80,6 @@ HC_ACK } USB_DRD_HCStateTypeDef; -/** - * USB HOST Double buffer Isochronous State - */ -typedef enum -{ - USB_PMABUF_EMPTY = 0U, - USB_PMABUF_NOT_EMPTY = 1U -} USB_BuffStateTypedDef; /** * @brief USB Instance Initialization Structure definition @@ -106,6 +98,8 @@ This parameter can be any value of @ref PCD_Speed/HCD_Speed (HCD_SPEED_xxx, HCD_SPEED_xxx) */ + uint32_t dma_enable; /*!< dma_enable state unused, DMA not supported by FS instance */ + uint32_t ep0_mps; /*!< Set the Endpoint 0 Max Packet size. */ uint32_t phy_itface; /*!< Select the used PHY interface. @@ -141,16 +135,16 @@ This parameter can be any value of @ref USB_LL_EP_Type */ uint16_t pmaadress; /*!< PMA Address - This parameter can be any value between Min_addr = 0 and Max_addr = 1K */ + This parameter can be any value between Min_addr = 0 and Max_addr = 1K */ uint16_t pmaaddr0; /*!< PMA Address0 - This parameter can be any value between Min_addr = 0 and Max_addr = 1K */ + This parameter can be any value between Min_addr = 0 and Max_addr = 1K */ uint16_t pmaaddr1; /*!< PMA Address1 - This parameter can be any value between Min_addr = 0 and Max_addr = 1K */ + This parameter can be any value between Min_addr = 0 and Max_addr = 1K */ uint8_t doublebuffer; /*!< Double buffer enable - This parameter can be 0 or 1 */ + This parameter can be 0 or 1 */ uint8_t data_pid_start; /*!< Initial data PID This parameter must be a number between Min_Data = 0 and Max_Data = 1 */ @@ -213,23 +207,19 @@ uint8_t toggle_out; /*!< OUT transfer current toggle flag This parameter must be a number between Min_Data = 0 and Max_Data = 1 */ - uint32_t ErrCnt; /*!< Host channel error count. */ + uint32_t ErrCnt; /*!< Host channel error count. */ uint16_t pmaadress; /*!< PMA Address - This parameter can be any value between Min_addr = 0 and Max_addr = 1K */ + This parameter can be any value between Min_addr = 0 and Max_addr = 1K */ uint16_t pmaaddr0; /*!< PMA Address0 - This parameter can be any value between Min_addr = 0 and Max_addr = 1K */ + This parameter can be any value between Min_addr = 0 and Max_addr = 1K */ uint16_t pmaaddr1; /*!< PMA Address1 - This parameter can be any value between Min_addr = 0 and Max_addr = 1K */ + This parameter can be any value between Min_addr = 0 and Max_addr = 1K */ uint8_t doublebuffer; /*!< Double buffer enable - This parameter can be 0 or 1 */ - - USB_BuffStateTypedDef pmabuff0_state; /*!< PMA Buffer0 State empty / notEmpty : used in double buffer OUT isochronous */ - - USB_BuffStateTypedDef pmabuff1_state; /*!< PMA Buffer1 State empty / notEmpty : used in double buffer OUT isochronous */ + This parameter can be 0 or 1 */ USB_DRD_URBStateTypeDef urb_state; /*!< URB state. This parameter can be any value of @ref USB_OTG_URBStateTypeDef */ @@ -283,6 +273,10 @@ #define EP_ADDR_MSK 0x7U +#ifndef USE_USB_DOUBLE_BUFFER +#define USE_USB_DOUBLE_BUFFER 1U +#endif /* USE_USB_DOUBLE_BUFFER */ + /*!< USB Speed */ #define USB_DRD_SPEED_FS 1U #define USB_DRD_SPEED_LS 2U @@ -303,17 +297,17 @@ #define USB_DRD_USED_CHANNELS 8U #endif /* USB_DRD_USED_CHANNELS */ -/* First available address in PMA */ -#define PMA_START_ADDR (0x10U + (8U *(USB_DRD_USED_CHANNELS - 2U))) -#define PMA_END_ADDR USB_DRD_PMA_SIZE - - /** * used for USB_HC_DoubleBuffer API */ -#define USB_DRD_DBUFF_ENBALE 1U -#define USB_DRD_DBUFF_DISABLE 2U +#define USB_DRD_BULK_DBUFF_ENBALE 1U +#define USB_DRD_BULK_DBUFF_DISABLE 2U +#define USB_DRD_ISOC_DBUFF_ENBALE 3U +#define USB_DRD_ISOC_DBUFF_DISABLE 4U +/* First available address in PMA */ +#define PMA_START_ADDR (0x10U + (8U *(USB_DRD_USED_CHANNELS - 2U))) +#define PMA_END_ADDR USB_DRD_PMA_SIZE /* Exported macro ------------------------------------------------------------*/ /** @@ -339,7 +333,7 @@ * @param bEpChNum, bDir * @retval None */ -#define USB_DRD_FreeUserBuffer(USBx, bEpChNum, bDir) \ +#define USB_DRD_FREE_USER_BUFFER(USBx, bEpChNum, bDir) \ do { \ if ((bDir) == 0U) \ { \ @@ -668,18 +662,22 @@ uint32_t wNBlocks; \ \ (pdwReg) &= USB_PMA_RXBD_COUNTMSK; \ - if ((wCount) == 0U) \ + \ + if ((wCount) > 62U) \ { \ - (pdwReg) &= (uint32_t)~USB_CNTRX_NBLK_MSK; \ - (pdwReg) |= USB_CNTRX_BLSIZE; \ - } \ - else if((wCount) <= 62U) \ - { \ - USB_DRD_CALC_BLK2((pdwReg), (wCount), wNBlocks); \ + USB_DRD_CALC_BLK32((pdwReg), (wCount), wNBlocks); \ } \ else \ { \ - USB_DRD_CALC_BLK32((pdwReg), (wCount), wNBlocks); \ + if ((wCount) == 0U) \ + { \ + (pdwReg) &= (uint32_t)~USB_CNTRX_NBLK_MSK; \ + (pdwReg) |= USB_CNTRX_BLSIZE; \ + } \ + else \ + { \ + USB_DRD_CALC_BLK2((pdwReg), (wCount), wNBlocks); \ + } \ } \ } while(0) /* USB_DRD_SET_CHEP_CNT_RX_REG */ @@ -700,13 +698,11 @@ (USB_DRD_PMA_BUFF + (bEpChNum))->TXBD |= (uint32_t)((uint32_t)(wCount) << 16U); \ } while(0) +#define USB_DRD_SET_CHEP_RX_DBUF0_CNT(USBx, bEpChNum, wCount) \ + USB_DRD_SET_CHEP_CNT_RX_REG(((USB_DRD_PMA_BUFF + (bEpChNum))->TXBD), (wCount)) -#define USB_DRD_SET_CHEP_RX_DBUF0_CNT(USBx, bEpChNum, wCount) USB_DRD_SET_CHEP_CNT_RX_REG(((USB_DRD_PMA_BUFF\ - + (bEpChNum))->TXBD), (wCount)) - -#define USB_DRD_SET_CHEP_RX_CNT(USBx, bEpChNum, wCount) USB_DRD_SET_CHEP_CNT_RX_REG(((USB_DRD_PMA_BUFF\ - + (bEpChNum))->RXBD), (wCount)) - +#define USB_DRD_SET_CHEP_RX_CNT(USBx, bEpChNum, wCount) \ + USB_DRD_SET_CHEP_CNT_RX_REG(((USB_DRD_PMA_BUFF + (bEpChNum))->RXBD), (wCount)) /** * @brief gets counter of the tx buffer. @@ -833,7 +829,8 @@ HAL_StatusTypeDef USB_EPStartXfer(USB_DRD_TypeDef *USBx, USB_DRD_EPTypeDef *ep); HAL_StatusTypeDef USB_EPSetStall(USB_DRD_TypeDef *USBx, USB_DRD_EPTypeDef *ep); HAL_StatusTypeDef USB_EPClearStall(USB_DRD_TypeDef *USBx, USB_DRD_EPTypeDef *ep); -#endif /* HAL_PCD_MODULE_ENABLED */ +HAL_StatusTypeDef USB_EPStopXfer(USB_DRD_TypeDef *USBx, USB_DRD_EPTypeDef *ep); +#endif /* defined (HAL_PCD_MODULE_ENABLED) */ HAL_StatusTypeDef USB_SetDevAddress(USB_DRD_TypeDef *USBx, uint8_t address); HAL_StatusTypeDef USB_DevConnect(USB_DRD_TypeDef *USBx); @@ -882,7 +879,7 @@ #ifdef __cplusplus } -#endif +#endif /* __cplusplus */ #endif /* STM32G0xx_LL_USB_H */
diff --git a/README.md b/README.md index e8de06c..1ca31cb 100644 --- a/README.md +++ b/README.md
@@ -40,11 +40,12 @@ HAL Driver G0 | CMSIS Device G0 | CMSIS Core | Was delivered in the full MCU package ------------- | --------------- | -------------- | ----------------------------------------------------- -Tag v1.2.0 | Tag v1.2.0 | Tag v4.5_cm0 | Tag v1.2.0 (and following, if any, till next new tag) -Tag v1.3.0 | Tag v1.3.0 | Tag v5.4.0_cm0 | Tag v1.3.0 (and following, if any, till next new tag) -Tag v1.4.0 | Tag v1.4.0 | Tag v5.6.0_cm0 | Tag v1.4.0 (and following, if any, till next new tag) -Tag v1.4.1 | Tag v1.4.0 | Tag v5.6.0_cm0 | Tag v1.4.1 (and following, if any, till next new tag) -Tag v1.4.2 | Tag v1.4.1 | Tag v5.6.0_cm0 | Tag v1.5.0 (and following, if any, till next new tag) +Tag v1.2.0 | Tag v1.2.0 | Tag v4.5_cm0 | Tag v1.2.0 (and following, if any, till next HAL tag) +Tag v1.3.0 | Tag v1.3.0 | Tag v5.4.0_cm0 | Tag v1.3.0 (and following, if any, till next HAL tag) +Tag v1.4.0 | Tag v1.4.0 | Tag v5.6.0_cm0 | Tag v1.4.0 (and following, if any, till next HAL tag) +Tag v1.4.1 | Tag v1.4.0 | Tag v5.6.0_cm0 | Tag v1.4.1 (and following, if any, till next HAL tag) +Tag v1.4.2 | Tag v1.4.1 | Tag v5.6.0_cm0 | Tag v1.5.0 (and following, if any, till next HAL tag) +Tag v1.4.3 | Tag v1.4.1 | Tag v5.6.0_cm0 | None (will be integrated into next HAL tag v1.4.4) The full **STM32CubeG0** MCU package is available [here](https://github.com/STMicroelectronics/STM32CubeG0).
diff --git a/Release_Notes.html b/Release_Notes.html index 1150f2b..2f55b1f 100644 --- a/Release_Notes.html +++ b/Release_Notes.html
@@ -45,11 +45,52 @@ <section id="update-history" class="col-sm-12 col-lg-8"> <h1>Update History</h1> <div class="collapse"> -<input type="checkbox" id="collapse-section5" checked aria-hidden="true"> <label for="collapse-section5" aria-hidden="true">V1.4.2 / 17-Juin-2021</label> +<input type="checkbox" id="collapse-section6" checked aria-hidden="true"> <label for="collapse-section6" aria-hidden="true">V1.4.3 / 15-December-2021</label> <div> <h2 id="main-changes">Main Changes</h2> <h3 id="maintenance-release">Maintenance release</h3> <ul> +<li><strong>HAL/LL Drivers</strong> updates +<ul> +<li><strong>HAL USB</strong> update +<ul> +<li>Footprint optimization in case USB double buffer mode not used, code protected with new define USE_USB_DOUBLE_BUFFER</li> +<li>Update to avoid multiple reads of USB ISR register</li> +<li>Add new API HAL_PCD_EP_Abort() to abort current USB endpoint transfer.</li> +<li>Adding support of Azure USBx middleware stack</li> +</ul></li> +</ul></li> +</ul> +<h2 id="known-limitations">Known limitations</h2> +<ul> +<li>USB HAL limitation: Double buffer mode is not functional with isochronous data transfers in host mode.</li> +</ul> +<h2 id="development-toolchains-and-compilers">Development Toolchains and Compilers</h2> +<ul> +<li>IAR Embedded Workbench for ARM (EWARM) toolchain V8.50.9</li> +<li>RealView Microcontroller Development Kit (MDK-ARM) toolchain V5.31</li> +<li>STM32CubeIDE toolchain v1.7.0</li> +</ul> +<h2 id="supported-devices-and-boards">Supported Devices and boards</h2> +<p>Supported Devices:</p> +<ul> +<li>STM32G0C1xx, STM32G0B1xx, STM32G0B0xx</li> +<li>STM32G061xx, STM32G051xx, STM32G050xx</li> +<li>STM32G081xx, STM32G071xx, STM32G070xx</li> +<li>STM32G041xx, STM32G031xx, STM32G030xx ## Backward Compatibility</li> +</ul> +<p>This release is compatible with the previous versions.</p> +<h2 id="dependencies">Dependencies</h2> +<p><br /> +</p> +</div> +</div> +<div class="collapse"> +<input type="checkbox" id="collapse-section5" aria-hidden="true"> <label for="collapse-section5" aria-hidden="true">V1.4.2 / 17-Juin-2021</label> +<div> +<h2 id="main-changes-1">Main Changes</h2> +<h3 id="maintenance-release-1">Maintenance release</h3> +<ul> <li><p>General updates to fix known defects and enhancements implementation.</p></li> <li><p><strong>HAL/LL Drivers</strong> updates</p></li> <li><p><strong>HAL CEC</strong> update</p> @@ -178,17 +219,17 @@ <li>Add fix transfer complete for IN Interrupt transaction in single buffer mode.</li> </ul></li> </ul> -<h2 id="known-limitations">Known limitations</h2> +<h2 id="known-limitations-1">Known limitations</h2> <ul> <li>USB HAL limitation: Double buffer mode is not functional with isochronous data transfers in host mode.</li> </ul> -<h2 id="development-toolchains-and-compilers">Development Toolchains and Compilers</h2> +<h2 id="development-toolchains-and-compilers-1">Development Toolchains and Compilers</h2> <ul> <li>IAR Embedded Workbench for ARM (EWARM) toolchain V8.50.9</li> <li>RealView Microcontroller Development Kit (MDK-ARM) toolchain V5.31</li> <li>STM32CubeIDE toolchain v1.7.0</li> </ul> -<h2 id="supported-devices-and-boards">Supported Devices and boards</h2> +<h2 id="supported-devices-and-boards-1">Supported Devices and boards</h2> <p>Supported Devices:</p> <ul> <li>STM32G0C1xx, STM32G0B1xx, STM32G0B0xx</li> @@ -197,7 +238,7 @@ <li>STM32G041xx, STM32G031xx, STM32G030xx ## Backward Compatibility</li> </ul> <p>This release is compatible with the previous versions.</p> -<h2 id="dependencies">Dependencies</h2> +<h2 id="dependencies-1">Dependencies</h2> <p><br /> </p> </div> @@ -205,7 +246,7 @@ <div class="collapse"> <input type="checkbox" id="collapse-section6" aria-hidden="true"> <label for="collapse-section6" aria-hidden="true">V1.4.1 / 25-February-2021</label> <div> -<h2 id="main-changes-1">Main Changes</h2> +<h2 id="main-changes-2">Main Changes</h2> <h3 id="patch-release">Patch release</h3> <p>Patch release of <strong>HAL and Low Layer</strong> drivers</p> <p><strong>Additional features</strong></p> @@ -221,17 +262,17 @@ </tr> </tbody> </table> -<h2 id="known-limitations-1">Known limitations</h2> +<h2 id="known-limitations-2">Known limitations</h2> <ul> <li>USB HAL limitation: Double buffer mode is not functional with isochronous data transfers in host mode.</li> </ul> -<h2 id="development-toolchains-and-compilers-1">Development Toolchains and Compilers</h2> +<h2 id="development-toolchains-and-compilers-2">Development Toolchains and Compilers</h2> <ul> <li>IAR Embedded Workbench for ARM (EWARM) toolchain V8.30.1</li> <li>RealView Microcontroller Development Kit (MDK-ARM) toolchain V5.29</li> <li>STM32CubeIDE toolchain v1.5.0</li> </ul> -<h2 id="supported-devices-and-boards-1">Supported Devices and boards</h2> +<h2 id="supported-devices-and-boards-2">Supported Devices and boards</h2> <p>Supported Devices:</p> <ul> <li>STM32G0C1xx, STM32G0B1xx, STM32G0B0xx</li> @@ -241,7 +282,7 @@ </ul> <h2 id="backward-compatibility">Backward Compatibility</h2> <p>This release is compatible with the previous versions.</p> -<h2 id="dependencies-1">Dependencies</h2> +<h2 id="dependencies-2">Dependencies</h2> <p><br /> </p> </div> @@ -249,7 +290,7 @@ <div class="collapse"> <input type="checkbox" id="collapse-section5" aria-hidden="true"> <label for="collapse-section5" aria-hidden="true">V1.4.0 / 29-October-2020</label> <div> -<h2 id="main-changes-2">Main Changes</h2> +<h2 id="main-changes-3">Main Changes</h2> <h3 id="maintenance-release-and-product-update">Maintenance release and Product Update</h3> <p><strong>Official release</strong> of HAL and Low layers drivers introducing <strong>STM32G0C1xx/STM32G0B1xx/STM32G0B0xx</strong> devices and <strong>STM32G061xx/STM32G051xx/STM32G050xx</strong> devices.</p> <p>Maintenance release of HAL and Low layers drivers supporting STM32G041xx/STM32G031xx/STM32G030xx and STM32G081xx/STM32G071xx/STM32G070xx devices.</p> @@ -716,17 +757,17 @@ </tr> </tbody> </table> -<h2 id="known-limitations-2">Known limitations</h2> +<h2 id="known-limitations-3">Known limitations</h2> <ul> <li>USB HAL limitation: Double buffer mode is not functional with isochronous data transfers in host mode.</li> </ul> -<h2 id="development-toolchains-and-compilers-2">Development Toolchains and Compilers</h2> +<h2 id="development-toolchains-and-compilers-3">Development Toolchains and Compilers</h2> <ul> <li>IAR Embedded Workbench for ARM (EWARM) toolchain V8.30.1</li> <li>RealView Microcontroller Development Kit (MDK-ARM) toolchain V5.29</li> <li>STM32CubeIDE toolchain v1.5.0</li> </ul> -<h2 id="supported-devices-and-boards-2">Supported Devices and boards</h2> +<h2 id="supported-devices-and-boards-3">Supported Devices and boards</h2> <p>Supported Devices:</p> <ul> <li><strong>STM32G0C1xx, STM32G0B1xx, STM32G0B0xx</strong></li> @@ -736,7 +777,7 @@ </ul> <h2 id="backward-compatibility-1">Backward Compatibility</h2> <p>This release is compatible with the previous versions.</p> -<h2 id="dependencies-2">Dependencies</h2> +<h2 id="dependencies-3">Dependencies</h2> <p><br /> </p> </div> @@ -744,8 +785,8 @@ <div class="collapse"> <input type="checkbox" id="collapse-section4" aria-hidden="true"> <label for="collapse-section4" aria-hidden="true">V1.3.0 / 25-June-2019</label> <div> -<h2 id="main-changes-3">Main Changes</h2> -<h3 id="maintenance-release-1">Maintenance release</h3> +<h2 id="main-changes-4">Main Changes</h2> +<h3 id="maintenance-release-2">Maintenance release</h3> <p>Maintenance release of HAL and Low layers drivers supporting STM32G041xx/STM32G031xx/STM32G030xx and STM32G081xx/STM32G071xx/STM32G070xx devices.</p> <p><strong>Fixed bugs list</strong></p> <table> @@ -814,13 +855,13 @@ </tr> </tbody> </table> -<h2 id="development-toolchains-and-compilers-3">Development Toolchains and Compilers</h2> +<h2 id="development-toolchains-and-compilers-4">Development Toolchains and Compilers</h2> <ul> <li>IAR Embedded Workbench for ARM (EWARM) toolchain V8.20.2</li> <li>RealView Microcontroller Development Kit (MDK-ARM) toolchain V5.25</li> <li>System Workbench STM32 (SW4STM32) toolchain V2.7.2</li> </ul> -<h2 id="supported-devices-and-boards-3">Supported Devices and boards</h2> +<h2 id="supported-devices-and-boards-4">Supported Devices and boards</h2> <p>Supported Devices:</p> <ul> <li>STM32G081xx, STM32G071xx, STM32G070xx</li> @@ -828,13 +869,13 @@ </ul> <h2 id="backward-compatibility-2">Backward Compatibility</h2> <p>This release is compatible with the previous versions.</p> -<h2 id="dependencies-3">Dependencies</h2> +<h2 id="dependencies-4">Dependencies</h2> </div> </div> <div class="collapse"> <input type="checkbox" id="collapse-section3" aria-hidden="true"> <label for="collapse-section3" aria-hidden="true">V1.2.0 / 05-April-2019</label> <div> -<h2 id="main-changes-4">Main Changes</h2> +<h2 id="main-changes-5">Main Changes</h2> <h3 id="maintenance-release-and-product-update-1">Maintenance release and product update</h3> <p>First release of HAL and Low layers drivers to introduce support of <strong>STM32G041xx/STM32G031xx/STM32G030xx devices</strong>.</p> <p>Maintenance release of HAL and Low layers drivers supporting STM32G081xx/STM32G071xx/STM32G070xx devices.</p> @@ -921,13 +962,13 @@ </tr> </tbody> </table> -<h2 id="development-toolchains-and-compilers-4">Development Toolchains and Compilers</h2> +<h2 id="development-toolchains-and-compilers-5">Development Toolchains and Compilers</h2> <ul> <li>IAR Embedded Workbench for ARM (EWARM) toolchain V8.20.2</li> <li>RealView Microcontroller Development Kit (MDK-ARM) toolchain V5.25</li> <li>System Workbench STM32 (SW4STM32) toolchain V2.7.2</li> </ul> -<h2 id="supported-devices-and-boards-4">Supported Devices and boards</h2> +<h2 id="supported-devices-and-boards-5">Supported Devices and boards</h2> <p>Supported Devices:</p> <ul> <li>STM32G081xx, STM32G071xx, STM32G070xx</li> @@ -935,13 +976,13 @@ </ul> <h2 id="backward-compatibility-3">Backward Compatibility</h2> <p>This release is compatible with the previous versions.</p> -<h2 id="dependencies-4">Dependencies</h2> +<h2 id="dependencies-5">Dependencies</h2> </div> </div> <div class="collapse"> <input type="checkbox" id="collapse-section2" aria-hidden="true"> <label for="collapse-section2" aria-hidden="true">V1.1.0 / 06-February-2019</label> <div> -<h2 id="main-changes-5">Main Changes</h2> +<h2 id="main-changes-6">Main Changes</h2> <h3 id="maintenance-release-and-product-update-2">Maintenance release and product update</h3> <p>Maintenance release of HAL and Low layers drivers to support STM32G071xx/STM32G081xx/STM32G070xx devices.</p> <p><strong>Additional features</strong></p> @@ -1030,7 +1071,7 @@ </tr> </tbody> </table> -<h2 id="supported-devices-and-boards-5">Supported Devices and boards</h2> +<h2 id="supported-devices-and-boards-6">Supported Devices and boards</h2> <p>Supported Devices:</p> <ul> <li>STM32G0<strong>81</strong>xx</li> @@ -1039,23 +1080,23 @@ </ul> <h2 id="backward-compatibility-4">Backward Compatibility</h2> <p>This release is compatible with the previous versions.</p> -<h2 id="dependencies-5">Dependencies</h2> +<h2 id="dependencies-6">Dependencies</h2> </div> </div> <div class="collapse"> <input type="checkbox" id="collapse-section1" aria-hidden="true"> <label for="collapse-section1" aria-hidden="true">V1.0.0 / 26-October-2018</label> <div> -<h2 id="main-changes-6">Main Changes</h2> +<h2 id="main-changes-7">Main Changes</h2> <h3 id="first-release">First release</h3> <p>First official release of HAL and Low layers drivers to support STM32G071xx/STM32G081xx/STM32G070xx</p> -<h2 id="supported-devices-and-boards-6">Supported Devices and boards</h2> +<h2 id="supported-devices-and-boards-7">Supported Devices and boards</h2> <p>Supported Devices:</p> <ul> <li>STM32G0<strong>81</strong>xx</li> <li>STM32G0<strong>71</strong>xx</li> <li>STM32G0<strong>70</strong>xx</li> </ul> -<h2 id="dependencies-6">Dependencies</h2> +<h2 id="dependencies-7">Dependencies</h2> </div> </div> </section>
diff --git a/Src/stm32g0xx_hal.c b/Src/stm32g0xx_hal.c index e2208fa..efa598b 100644 --- a/Src/stm32g0xx_hal.c +++ b/Src/stm32g0xx_hal.c
@@ -57,7 +57,7 @@ */ #define __STM32G0xx_HAL_VERSION_MAIN (0x01U) /*!< [31:24] main version */ #define __STM32G0xx_HAL_VERSION_SUB1 (0x04U) /*!< [23:16] sub1 version */ -#define __STM32G0xx_HAL_VERSION_SUB2 (0x02U) /*!< [15:8] sub2 version */ +#define __STM32G0xx_HAL_VERSION_SUB2 (0x03U) /*!< [15:8] sub2 version */ #define __STM32G0xx_HAL_VERSION_RC (0x00U) /*!< [7:0] release candidate */ #define __STM32G0xx_HAL_VERSION ((__STM32G0xx_HAL_VERSION_MAIN << 24U)\ |(__STM32G0xx_HAL_VERSION_SUB1 << 16U)\
diff --git a/Src/stm32g0xx_hal_hcd.c b/Src/stm32g0xx_hal_hcd.c index 55aee18..f62c075 100644 --- a/Src/stm32g0xx_hal_hcd.c +++ b/Src/stm32g0xx_hal_hcd.c
@@ -81,8 +81,11 @@ static uint8_t HAL_HCD_Check_usedChannel(HCD_HandleTypeDef *hhcd, uint8_t ch_num); static uint8_t HAL_HCD_Get_FreePhyChannel(HCD_HandleTypeDef *hhcd, uint8_t ch_num, uint8_t epnum, uint8_t ep_type); +#if (USE_USB_DOUBLE_BUFFER == 1U) static void HCD_HC_IN_BulkDb(HCD_HandleTypeDef *hhcd, uint8_t ch_num, uint8_t phy_chnum, uint32_t regvalue); static void HCD_HC_OUT_BulkDb(HCD_HandleTypeDef *hhcd, uint8_t ch_num, uint8_t phy_chnum, uint32_t regvalue); +#endif /* (USE_USB_DOUBLE_BUFFER == 1U) */ + static uint16_t HAL_HCD_GetFreePMA(HCD_HandleTypeDef *hhcd, uint16_t mps); static HAL_StatusTypeDef HAL_HCD_PMAFree(HCD_HandleTypeDef *hhcd, uint32_t pma_base, uint16_t mps); static void inline HCD_HC_IN_ISO(HCD_HandleTypeDef *hhcd, uint8_t ch_num, uint8_t phy_chnum, uint32_t regvalue); @@ -154,24 +157,27 @@ /* Disable the Interrupts */ (void)__HAL_HCD_DISABLE(hhcd); - /*Init the Core (common init.) */ + /* Dma not supported, force to zero */ + hhcd->Init.dma_enable = 0U; + + /* Init the Core (common init.) */ (void)USB_CoreInit(hhcd->Instance, hhcd->Init); - /* Force Host Mode*/ + /* Force Host Mode */ (void)USB_SetCurrentMode(hhcd->Instance, USB_HOST_MODE); /* Init Host */ (void)USB_HostInit(hhcd->Instance, hhcd->Init); - /* Deactivate the power down*/ + /* Deactivate the power down */ hhcd->Instance->CNTR &= ~USB_CNTR_PDWN; hhcd->State = HAL_HCD_STATE_READY; - /*Host Port State*/ + /* Host Port State */ hhcd->HostState = HCD_HCD_STATE_DISCONNECTED; - /*Init PMA Address */ + /* Init PMA Address */ (void)HAL_HCD_PMAReset(hhcd); hhcd->State = HAL_HCD_STATE_READY; @@ -209,19 +215,20 @@ { HAL_StatusTypeDef status; uint8_t used_channel; + uint8_t ep0_virtual_channel; __HAL_LOCK(hhcd); /* Check if the logical channel are already allocated */ used_channel = HAL_HCD_Check_usedChannel(hhcd, ch_num); - /*Check if the channel is not already opened */ + /* Check if the channel is not already opened */ if (used_channel == 0U) { - /*Allocate New Physical channel */ + /* Allocate New Physical channel */ hhcd->hc[ch_num & 0xFU].phy_ch_num = HAL_HCD_Get_FreePhyChannel(hhcd, ch_num, epnum, ep_type); - /*if it is not able to allocate channel */ + /* No free Channel available, return error */ if (hhcd->hc[ch_num & 0xFU].phy_ch_num == HCD_FREE_CH_NOT_FOUND) { return HAL_ERROR; @@ -230,7 +237,7 @@ /* Channel already opened */ else { - /*Get Physical Channel number */ + /* Get Physical Channel number */ hhcd->hc[ch_num & 0xFU].phy_ch_num = (used_channel & 0xF0U) >> 4U; } @@ -249,7 +256,7 @@ hhcd->hc[ch_num & 0xFU].ep_num = epnum & 0x7FU; hhcd->hc[ch_num & 0xFU].speed = speed; - /*Check if the channel is not already opened */ + /* Check if the channel is not already opened */ if (used_channel == 0U) { if (((ep_type == EP_TYPE_ISOC) && (hhcd->Init.iso_singlebuffer_enable == 0U)) || @@ -257,19 +264,118 @@ { /* PMA Dynamic Allocation */ status = HAL_HCD_PMAlloc(hhcd, ch_num, HCD_DBL_BUF, mps); + if (status == HAL_ERROR) { return HAL_ERROR; } + + /* Clear Channel DTOG_TX */ + HCD_CLEAR_TX_DTOG(hhcd->Instance, hhcd->hc[ch_num & 0xFU].phy_ch_num); + + /* Clear Channel DTOG RX */ + HCD_CLEAR_RX_DTOG(hhcd->Instance, hhcd->hc[ch_num & 0xFU].phy_ch_num); + } else { - /* PMA Dynamic Allocation */ - status = HAL_HCD_PMAlloc(hhcd, ch_num, HCD_SNG_BUF, mps); - if (status == HAL_ERROR) + if (hhcd->hc[ch_num & 0xFU].ep_num != 0U) { - return HAL_ERROR; + status = HAL_HCD_PMAlloc(hhcd, ch_num, HCD_SNG_BUF, mps); + + if (status == HAL_ERROR) + { + return HAL_ERROR; + } } + else + { + if (ch_num == 0U) + { + ep0_virtual_channel = (uint8_t)(hhcd->ep0_PmaAllocState & 0xFU); + + if ((ep0_virtual_channel != 0U) && (((hhcd->ep0_PmaAllocState & 0xF0U) >> 4) == CH_IN_DIR)) + { + if (hhcd->hc[ch_num & 0xFU].ch_dir == CH_OUT_DIR) + { + status = HAL_HCD_PMAlloc(hhcd, ch_num, HCD_SNG_BUF, 64U); + + if (status == HAL_ERROR) + { + return HAL_ERROR; + } + } + else + { + return HAL_ERROR; + } + } + else + { + /* PMA Dynamic Allocation for EP0 OUT direction */ + hhcd->hc[ch_num & 0xFU].ch_dir = CH_OUT_DIR; + status = HAL_HCD_PMAlloc(hhcd, ch_num, HCD_SNG_BUF, 64U); + + if (status == HAL_ERROR) + { + return HAL_ERROR; + } + + /* PMA Dynamic Allocation for EP0 IN direction */ + hhcd->hc[ch_num & 0xFU].ch_dir = CH_IN_DIR; + status = HAL_HCD_PMAlloc(hhcd, ch_num, HCD_SNG_BUF, 64U); + + if (status == HAL_ERROR) + { + return HAL_ERROR; + } + } + } + else + { + if (((hhcd->ep0_PmaAllocState & 0xF00U) >> 8) == 1U) + { + ep0_virtual_channel = (uint8_t)(hhcd->ep0_PmaAllocState & 0xFU); + + if (((hhcd->ep0_PmaAllocState & 0xF0U) >> 4) == CH_IN_DIR) + { + hhcd->hc[ch_num & 0xFU].pmaaddr1 = hhcd->hc[ep0_virtual_channel & 0xFU].pmaaddr1; + } + else + { + hhcd->hc[ch_num & 0xFU].pmaaddr0 = hhcd->hc[ep0_virtual_channel & 0xFU].pmaaddr0; + } + } + else + { + status = HAL_HCD_PMAlloc(hhcd, ch_num, HCD_SNG_BUF, 64U); + + if (status == HAL_ERROR) + { + return HAL_ERROR; + } + } + } + } + } + } + + if ((epnum & 0x80U) != 0U) + { + hhcd->hc[ch_num & 0xFU].ch_dir = CH_IN_DIR; + + if (hhcd->hc[ch_num & 0xFU].ep_num == 0U) + { + hhcd->hc[ch_num & 0xFU].pmaadress = hhcd->hc[ch_num & 0xFU].pmaaddr1; + } + } + else + { + hhcd->hc[ch_num & 0xFU].ch_dir = CH_OUT_DIR; + + if (hhcd->hc[ch_num & 0xFU].ep_num == 0U) + { + hhcd->hc[ch_num & 0xFU].pmaadress = hhcd->hc[ch_num & 0xFU].pmaaddr0; } } @@ -282,11 +388,9 @@ { if (hhcd->Init.iso_singlebuffer_enable == 1U) { - (void)USB_HC_DoubleBuffer(hhcd->Instance, hhcd->hc[ch_num & 0xFU].phy_ch_num, USB_DRD_DBUFF_ENBALE); + (void)USB_HC_DoubleBuffer(hhcd->Instance, hhcd->hc[ch_num & 0xFU].phy_ch_num, + USB_DRD_ISOC_DBUFF_DISABLE); } - /*Init Buffer State */ - hhcd->hc[ch_num & 0xFU].pmabuff0_state = USB_PMABUF_EMPTY; - hhcd->hc[ch_num & 0xFU].pmabuff1_state = USB_PMABUF_EMPTY; } /* Bulk double buffer check */ @@ -294,9 +398,11 @@ { if (hhcd->Init.bulk_doublebuffer_enable == 1U) { - (void)USB_HC_DoubleBuffer(hhcd->Instance, hhcd->hc[ch_num & 0xFU].phy_ch_num, USB_DRD_DBUFF_ENBALE); + (void)USB_HC_DoubleBuffer(hhcd->Instance, hhcd->hc[ch_num & 0xFU].phy_ch_num, + USB_DRD_BULK_DBUFF_ENBALE); } } + __HAL_UNLOCK(hhcd); return status; @@ -379,6 +485,9 @@ hhcd->phy_chout_state[idx] = 0U; } + /* reset Ep0 Pma allocation state */ + hhcd->ep0_PmaAllocState = 0U; + return HAL_OK; } @@ -600,6 +709,7 @@ uint8_t phy_chnum; uint8_t chnum; uint32_t epch_reg; + uint32_t wIstr = USB_ReadInterrupts(hhcd->Instance); /* check if this is an USB pending IT */ if ((SYSCFG->IT_LINE_SR[8] & (0x1U << 2)) == 0U) @@ -608,22 +718,24 @@ } /* Port Change Detected (Connection/Disconnection) */ - if (__HAL_HCD_GET_FLAG(hhcd, USB_ISTR_DCON)) + if ((wIstr & USB_ISTR_DCON) == USB_ISTR_DCON) { /* Clear Flag */ __HAL_HCD_CLEAR_FLAG(hhcd, USB_ISTR_DCON); /* Call Port IRQHandler */ HCD_Port_IRQHandler(hhcd); + + return; } /* Correct Transaction Detected -------*/ - if (__HAL_HCD_GET_FLAG(hhcd, USB_ISTR_CTR)) + if ((wIstr & USB_ISTR_CTR) == USB_ISTR_CTR) { /* Handle Host channel Interrupt */ for (phy_chnum = 0U; phy_chnum < hhcd->Init.Host_channels; phy_chnum++) { - if ((HCD_GET_CHANNEL(hhcd->Instance, phy_chnum) &USB_CH_VTRX) != 0U) + if ((HCD_GET_CHANNEL(hhcd->Instance, phy_chnum) & USB_CH_VTRX) != 0U) { /* Get Logical channel to check if the channel is already opened */ chnum = HAL_HCD_GetLogical_Channel(hhcd, phy_chnum, 1U); @@ -644,9 +756,9 @@ } } - if ((HCD_GET_CHANNEL(hhcd->Instance, phy_chnum) &USB_CH_VTTX) != 0U) + if ((HCD_GET_CHANNEL(hhcd->Instance, phy_chnum) & USB_CH_VTTX) != 0U) { - /*Get Logical channel to check if the channel is already opened*/ + /* Get Logical channel to check if the channel is already opened */ chnum = HAL_HCD_GetLogical_Channel(hhcd, phy_chnum, 0U); if (chnum != HCD_LOGICAL_CH_NOT_OPENED) @@ -656,7 +768,7 @@ } else { - /*Clear Error & unwanted VTTX or Channel was not closed correctly */ + /* Clear Error & unwanted VTTX or Channel was not closed correctly */ epch_reg = HCD_GET_CHANNEL(hhcd->Instance, phy_chnum); epch_reg = (epch_reg & (USB_CHEP_REG_MASK & (~USB_CH_ERRTX) & (~USB_CH_VTTX))) | (USB_CH_VTRX | USB_CH_ERRRX); @@ -666,13 +778,11 @@ } } - /* Clear Correct Transfer */ - __HAL_HCD_CLEAR_FLAG(hhcd, USB_ISTR_CTR); - + return; } /* Wakeup Flag Detected */ - if (__HAL_HCD_GET_FLAG(hhcd, USB_ISTR_WKUP)) + if ((wIstr & USB_ISTR_WKUP) == USB_ISTR_WKUP) { if (hhcd->HostState == HCD_HCD_STATE_SUSPEND) { @@ -688,27 +798,33 @@ } else { - /*Clear the wake-up flag*/ + /* Clear the wake-up flag */ __HAL_HCD_CLEAR_FLAG(hhcd, USB_ISTR_WKUP); } + + return; } - /*Global Error Flag Detected */ - if (__HAL_HCD_GET_FLAG(hhcd, USB_ISTR_ERR)) + /* Global Error Flag Detected */ + if ((wIstr & USB_ISTR_ERR) == USB_ISTR_ERR) { __HAL_HCD_CLEAR_FLAG(hhcd, USB_ISTR_ERR); + + return; } /* PMA Overrun detected */ - if (__HAL_HCD_GET_FLAG(hhcd, USB_ISTR_PMAOVR)) + if ((wIstr & USB_ISTR_PMAOVR) == USB_ISTR_PMAOVR) { __HAL_HCD_CLEAR_FLAG(hhcd, USB_ISTR_PMAOVR); + + return; } /* Suspend Detected */ - if (__HAL_HCD_GET_FLAG(hhcd, USB_ISTR_SUSP)) + if ((wIstr & USB_ISTR_SUSP) == USB_ISTR_SUSP) { - /*Set HAL State to Suspend */ + /* Set HAL State to Suspend */ hhcd->HostState = HCD_HCD_STATE_SUSPEND; /* Force low-power mode in the macrocell */ @@ -717,12 +833,14 @@ /* clear of the ISTR bit must be done after setting of CNTR_FSUSP */ __HAL_HCD_CLEAR_FLAG(hhcd, USB_ISTR_SUSP); - /*Call suspend Callback*/ + /* Call suspend Callback */ HAL_HCD_SuspendCallback(hhcd); + + return; } - /*Start Of Frame Detected */ - if (__HAL_HCD_GET_FLAG(hhcd, USB_ISTR_SOF)) + /* Start Of Frame Detected */ + if ((wIstr & USB_ISTR_SOF) == USB_ISTR_SOF) { #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U) hhcd->SOFCallback(hhcd); @@ -737,8 +855,15 @@ { /* HAL State */ hhcd->HostState = HCD_HCD_STATE_RUN; + +#if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U) + hhcd->PortEnabledCallback(hhcd); +#else HAL_HCD_PortEnabled_Callback(hhcd); +#endif /* USE_HAL_HCD_REGISTER_CALLBACKS */ } + + return; } } @@ -970,7 +1095,7 @@ /* Process locked */ __HAL_LOCK(hhcd); - /* Setup Legacy weak Callbacks */ + /* Setup Legacy weak Callbacks */ if (hhcd->State == HAL_HCD_STATE_READY) { switch (CallbackID) @@ -1183,6 +1308,9 @@ /* Reset the PMA current pointer */ (void)HAL_HCD_PMAReset(hhcd); + /* reset Ep0 Pma allocation state */ + hhcd->ep0_PmaAllocState = 0U; + __HAL_UNLOCK(hhcd); return HAL_OK; } @@ -1194,7 +1322,7 @@ */ HAL_StatusTypeDef HAL_HCD_Suspend(HCD_HandleTypeDef *hhcd) { - uint32_t count = 0U; + __IO uint32_t count = 0U; /* Set Suspend Mode */ hhcd->Instance->CNTR |= USB_CNTR_SUSPEN; @@ -1369,6 +1497,7 @@ return (USB_GetHostSpeed(hhcd->Instance)); } +#if (USE_USB_DOUBLE_BUFFER == 1U) /** * @brief Handle Host Channel OUT Double Buffer Bulk requests. * @param hhcd HCD handle @@ -1407,7 +1536,9 @@ if ((regvalue & USB_CH_DTOG_RX) != 0U) { /* Toggle SwBuff */ - HCD_FreeUserBuffer(hhcd->Instance, phy_chnum, 1U); + HCD_CLEAR_TX_DTOG(hhcd->Instance, phy_chnum); + HCD_CLEAR_RX_DTOG(hhcd->Instance, phy_chnum); + HCD_TX_DTOG(hhcd->Instance, phy_chnum); } /* hhcd->hc[ch_num&0xFU].xfer_len_db==0 ==> when all data are written in the PMA to yet transferred */ @@ -1428,7 +1559,7 @@ } /* Write remaining data to Buffer0 */ - HCD_SET_CH_TX_CNT(hhcd->Instance, phy_chnum, (uint16_t)len); + HCD_SET_CH_DBUF0_CNT(hhcd->Instance, phy_chnum, 1U, (uint16_t)len); USB_WritePMA(hhcd->Instance, hhcd->hc[ch_num & 0xFU].xfer_buff, hhcd->hc[ch_num & 0xFU].pmaaddr0, (uint16_t)len); } @@ -1466,7 +1597,9 @@ if ((regvalue & USB_CH_DTOG_RX) == 0U) { /* Toggle SwBuff */ - HCD_FreeUserBuffer(hhcd->Instance, phy_chnum, 1U); + HCD_CLEAR_TX_DTOG(hhcd->Instance, phy_chnum); + HCD_CLEAR_RX_DTOG(hhcd->Instance, phy_chnum); + HCD_RX_DTOG(hhcd->Instance, phy_chnum); } /* hhcd->hc[ch_num&0xFU].xfer_len_db==0 ==> when all data are written in the PMA to yet transferred */ @@ -1487,11 +1620,12 @@ } /* Write remaining data to Buffer0 */ - HCD_SET_CH_DBUF1_CNT(hhcd->Instance, phy_chnum, 0U, (uint16_t)len); + HCD_SET_CH_DBUF1_CNT(hhcd->Instance, phy_chnum, 1U, (uint16_t)len); USB_WritePMA(hhcd->Instance, hhcd->hc[ch_num & 0xFU].xfer_buff, hhcd->hc[ch_num & 0xFU].pmaaddr1, (uint16_t)len); } + /* start a new transfer */ HCD_SET_CH_TX_STATUS(hhcd->Instance, phy_chnum, USB_CH_TX_VALID); } @@ -1502,6 +1636,7 @@ hhcd->hc[ch_num & 0xFU].state = HC_XFRC; hhcd->hc[ch_num & 0xFU].urb_state = URB_DONE; hhcd->hc[ch_num & 0xFU].toggle_out ^= 1U; + /* Close the channel */ HCD_SET_CH_TX_STATUS(hhcd->Instance, phy_chnum, USB_CH_TX_DIS); } @@ -1510,6 +1645,96 @@ /** + * @brief Handle Host Channel IN Double Buffer Bulk requests. + * @param hhcd HCD handle + * @param ch_num Channel number: This parameter can be a value from 1 to 15 + * @param phy_chnum Physical Channel number [0..7] + * @param regvalue contain Snapshot of the EPCHn register when ISR is detected + * @retval none + */ +static void HCD_HC_IN_BulkDb(HCD_HandleTypeDef *hhcd, + uint8_t ch_num, uint8_t phy_chnum, uint32_t regvalue) +{ + uint16_t received_bytes; + + /* Read from Buffer 0 */ + if ((regvalue & USB_CH_DTOG_RX) != 0U) + { + received_bytes = (uint16_t)HCD_GET_CH_DBUF0_CNT(hhcd->Instance, phy_chnum); + + if (hhcd->hc[ch_num & 0xFU].xfer_len <= received_bytes) + { + hhcd->hc[ch_num & 0xFU].xfer_len = 0U; + } + else + { + hhcd->hc[ch_num & 0xFU].xfer_len -= received_bytes; + } + + /* Check if we Need to free the other buffer for the IP */ + if ((hhcd->hc[ch_num & 0xFU].xfer_len != 0U) && ((regvalue & USB_CH_DTOG_TX) != 0U)) + { + /* Toggle SwBuff to Allow the IP to submit a new IN */ + HCD_FREE_USER_BUFFER(hhcd->Instance, phy_chnum, 0U); + } + + /* Read the byte from PMA to user Buffer(System Memory) */ + USB_ReadPMA(hhcd->Instance, hhcd->hc[ch_num & 0xFU].xfer_buff, + hhcd->hc[ch_num & 0xFU].pmaaddr0, (uint16_t)received_bytes); + } + else + { + /* Read from Buffer 1 */ + received_bytes = (uint16_t) HCD_GET_CH_DBUF1_CNT(hhcd->Instance, phy_chnum); + + if (hhcd->hc[ch_num & 0xFU].xfer_len <= received_bytes) + { + hhcd->hc[ch_num & 0xFU].xfer_len = 0U; + } + else + { + hhcd->hc[ch_num & 0xFU].xfer_len -= received_bytes; + } + + /* Check if we Need to free the other buffer for the IP */ + if ((hhcd->hc[ch_num & 0xFU].xfer_len != 0U) && ((regvalue & USB_CH_DTOG_TX) == 0U)) + { + /* Toggle SwBuff */ + HCD_FREE_USER_BUFFER(hhcd->Instance, phy_chnum, 0U); + } + + /* Read the byte from PMA to user Buffer(System Memory) */ + USB_ReadPMA(hhcd->Instance, hhcd->hc[ch_num & 0xFU].xfer_buff, + hhcd->hc[ch_num & 0xFU].pmaaddr1, (uint16_t)received_bytes); + } + + /* update the global number of all received bytes */ + hhcd->hc[ch_num & 0xFU].xfer_count += received_bytes; + + /* Transfer complete state */ + hhcd->hc[ch_num & 0xFU].state = HC_ACK; + hhcd->hc[ch_num & 0xFU].ErrCnt = 0U; + + if ((hhcd->hc[ch_num & 0xFU].xfer_len == 0U) || + ((received_bytes < hhcd->hc[ch_num & 0xFU].max_packet))) + { + hhcd->hc[ch_num & 0xFU].urb_state = URB_DONE; + hhcd->hc[ch_num & 0xFU].state = HC_XFRC; + + /* disable channel */ + HCD_SET_CH_RX_STATUS(hhcd->Instance, phy_chnum, USB_CH_RX_DIS); + } + else + { + hhcd->hc[ch_num & 0xFU].xfer_buff += received_bytes; + + /* Reactivate the Channel Submit an other URB since the Transfer is not yet completed */ + HCD_SET_CH_RX_STATUS(hhcd->Instance, phy_chnum, USB_CH_RX_STRX); + } +} +#endif /* (USE_USB_DOUBLE_BUFFER == 1U) */ + +/** * @brief Handle Host Channel IN Isochronous Transaction * @param hhcd HCD handle * @param ch_num Channel number: This parameter can be a value from 1 to 15 @@ -1521,7 +1746,21 @@ uint8_t phy_chnum, uint32_t regvalue) { /* Check if Double buffer isochronous */ - if ((regvalue & USB_CH_KIND) == 0U) + if ((regvalue & USB_CH_KIND) != 0U) + { + /* Get Data IN Packet */ + hhcd->hc[ch_num & 0xFU].xfer_count = HCD_GET_CH_RX_CNT(hhcd->Instance, phy_chnum); + if (hhcd->hc[ch_num & 0xFU].xfer_count != 0U) + { + USB_ReadPMA(hhcd->Instance, hhcd->hc[ch_num & 0xFU].xfer_buff, + hhcd->hc[ch_num & 0xFU].pmaadress, + (uint16_t)hhcd->hc[ch_num & 0xFU].xfer_count); + + hhcd->hc[ch_num & 0xFU].urb_state = URB_DONE; + } + } +#if (USE_USB_DOUBLE_BUFFER == 1U) + else /* double buffer isochronous */ { /* Read from Buffer0 */ if ((regvalue & USB_CH_DTOG_RX) != 0U) @@ -1555,112 +1794,13 @@ } } } - else /* single buffer isochronous */ - { - /* Get Control Data OUT Packet*/ - hhcd->hc[ch_num & 0xFU].xfer_count = HCD_GET_CH_RX_CNT(hhcd->Instance, phy_chnum); - if (hhcd->hc[ch_num & 0xFU].xfer_count != 0U) - { - USB_ReadPMA(hhcd->Instance, hhcd->hc[ch_num & 0xFU].xfer_buff, - hhcd->hc[ch_num & 0xFU].pmaadress, - (uint16_t)hhcd->hc[ch_num & 0xFU].xfer_count); - - hhcd->hc[ch_num & 0xFU].urb_state = URB_DONE; - } - } +#endif /* (USE_USB_DOUBLE_BUFFER == 1U) */ /* Transfer complete state */ hhcd->hc[ch_num & 0xFU].state = HC_XFRC; /* Clear VTRX */ HCD_CLEAR_RX_CH_CTR(hhcd->Instance, phy_chnum); - -} -/** - * @brief Handle Host Channel IN Double Buffer Bulk requests. - * @param hhcd HCD handle - * @param ch_num Channel number: This parameter can be a value from 1 to 15 - * @param phy_chnum Physical Channel number [0..7] - * @param regvalue contain Snapshot of the EPCHn register when ISR is detected - * @retval none - */ -static void HCD_HC_IN_BulkDb(HCD_HandleTypeDef *hhcd, - uint8_t ch_num, uint8_t phy_chnum, uint32_t regvalue) -{ - uint16_t received_bytes; - - /* Read from Buffer 0 */ - if ((regvalue & USB_CH_DTOG_RX) != 0U) - { - received_bytes = (uint16_t)HCD_GET_CH_DBUF0_CNT(hhcd->Instance, phy_chnum); - - if (hhcd->hc[ch_num & 0xFU].xfer_len <= received_bytes) - { - hhcd->hc[ch_num & 0xFU].xfer_len = 0U; - } - else - { - hhcd->hc[ch_num & 0xFU].xfer_len -= received_bytes; - } - /* Check if we Need to free the other buffer for the IP */ - if ((hhcd->hc[ch_num & 0xFU].xfer_len != 0U) && ((regvalue & USB_CH_DTOG_TX) != 0U)) - { - /* Toggle SwBuff */ - HCD_FreeUserBuffer(hhcd->Instance, phy_chnum, 0U);/* Allow the IP to submit a new IN */ - } - /* Read the byte from PMA to user Buffer(System Memory) */ - USB_ReadPMA(hhcd->Instance, hhcd->hc[ch_num & 0xFU].xfer_buff, - hhcd->hc[ch_num & 0xFU].pmaaddr0, (uint16_t)received_bytes); - } - else - { - /* Read from Buffer 1 */ - received_bytes = (uint16_t) HCD_GET_CH_DBUF1_CNT(hhcd->Instance, phy_chnum); - - if (hhcd->hc[ch_num & 0xFU].xfer_len <= received_bytes) - { - hhcd->hc[ch_num & 0xFU].xfer_len = 0U; - } - else - { - hhcd->hc[ch_num & 0xFU].xfer_len -= received_bytes; - } - - /* Check if we Need to free the other buffer for the IP */ - if ((hhcd->hc[ch_num & 0xFU].xfer_len != 0U) && ((regvalue & USB_CH_DTOG_TX) == 0U)) - { - /* Toggle SwBuff */ - HCD_FreeUserBuffer(hhcd->Instance, phy_chnum, 0U); - } - - /* Read the byte from PMA to user Buffer(System Memory) */ - USB_ReadPMA(hhcd->Instance, hhcd->hc[ch_num & 0xFU].xfer_buff, - hhcd->hc[ch_num & 0xFU].pmaaddr1, (uint16_t)received_bytes); - } - - /* update the global number of all received bytes */ - hhcd->hc[ch_num & 0xFU].xfer_count += received_bytes; - - /* Transfer complete state */ - hhcd->hc[ch_num & 0xFU].state = HC_ACK; - hhcd->hc[ch_num & 0xFU].ErrCnt = 0U; - - if ((hhcd->hc[ch_num & 0xFU].xfer_len == 0U) || - ((received_bytes < hhcd->hc[ch_num & 0xFU].max_packet))) - { - hhcd->hc[ch_num & 0xFU].urb_state = URB_DONE; - hhcd->hc[ch_num & 0xFU].state = HC_XFRC; - - /* disable channel */ - HCD_SET_CH_RX_STATUS(hhcd->Instance, phy_chnum, USB_CH_RX_DIS); - } - else - { - hhcd->hc[ch_num & 0xFU].xfer_buff += received_bytes; - - /* Reactivate the Channel Submit an other URB since the Transfer is not yet completed */ - HCD_SET_CH_RX_STATUS(hhcd->Instance, phy_chnum, USB_CH_RX_STRX); - } } /** @@ -1704,6 +1844,7 @@ /* Transfer complete state */ hhcd->hc[ch_num & 0xFU].state = HC_ACK; hhcd->hc[ch_num & 0xFU].ErrCnt = 0U; + if (hhcd->hc[ch_num & 0xFU].xfer_len <= received_bytes) { hhcd->hc[ch_num & 0xFU].xfer_len = 0U; @@ -1712,6 +1853,7 @@ { hhcd->hc[ch_num & 0xFU].xfer_len -= received_bytes; } + if ((hhcd->hc[ch_num & 0xFU].xfer_len == 0U) || ((received_bytes < hhcd->hc[ch_num & 0xFU].max_packet))) { @@ -1725,13 +1867,13 @@ /* Reactivate the Channel to Submit another URB since the Transfer is not yet completed */ HCD_SET_CH_RX_STATUS(hhcd->Instance, phy_chnum, USB_CH_RX_STRX); } + if ((hhcd->hc[ch_num & 0xFU].ep_type == EP_TYPE_BULK) || (hhcd->hc[ch_num & 0xFU].ep_type == EP_TYPE_INTR)) { hhcd->hc[ch_num & 0xFU].toggle_out ^= 1U; } } - /* manage NACK Response */ else if (((ch_reg & USB_CH_RX_STRX) == USB_CH_RX_NAK) && (hhcd->hc[ch_num & 0xFU].urb_state != URB_DONE)) @@ -1740,7 +1882,6 @@ hhcd->hc[ch_num & 0xFU].ErrCnt = 0U; hhcd->hc[ch_num & 0xFU].state = HC_NAK; } - /* manage STALL Response */ else if ((ch_reg & USB_CH_RX_STRX) == USB_CH_RX_STALL) { @@ -1751,7 +1892,7 @@ /* Close the channel */ HCD_SET_CH_RX_STATUS(hhcd->Instance, phy_chnum, USB_CH_RX_DIS); } - +#if (USE_USB_DOUBLE_BUFFER == 1U) /* Double Buffer Management in case of Bulk Transaction */ else if (((ch_reg & USB_CH_RX_STRX) == USB_CH_RX_ACK_DBUF) && ((ch_reg & USB_CH_KIND) != 0U)) @@ -1759,13 +1900,18 @@ /* Bulk IN Double Buffer ISR */ HCD_HC_IN_BulkDb(hhcd, ch_num, phy_chnum, ch_reg); } +#endif /* (USE_USB_DOUBLE_BUFFER == 1U) */ else { /*....*/ /* not defined state: STRX=11 in single buffer no iso is not defined */ } - HAL_HCD_HC_NotifyURBChange_Callback(hhcd, (uint8_t)ch_num, - hhcd->hc[ch_num & 0xFU].urb_state); + +#if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U) + hhcd->HC_NotifyURBChangeCallback(hhcd, (uint8_t)ch_num, hhcd->hc[ch_num & 0xFU].urb_state); +#else + HAL_HCD_HC_NotifyURBChange_Callback(hhcd, (uint8_t)ch_num, hhcd->hc[ch_num & 0xFU].urb_state); +#endif /* USE_HAL_HCD_REGISTER_CALLBACKS */ /*Clear VTRX */ HCD_CLEAR_RX_CH_CTR(hhcd->Instance, phy_chnum); @@ -1790,8 +1936,12 @@ /* Clear pending err_tx */ HCD_CLEAR_RX_CH_ERR(hhcd->Instance, phy_chnum); } - HAL_HCD_HC_NotifyURBChange_Callback(hhcd, (uint8_t)ch_num, - hhcd->hc[ch_num & 0xFU].urb_state); + +#if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U) + hhcd->HC_NotifyURBChangeCallback(hhcd, (uint8_t)ch_num, hhcd->hc[ch_num & 0xFU].urb_state); +#else + HAL_HCD_HC_NotifyURBChange_Callback(hhcd, (uint8_t)ch_num, hhcd->hc[ch_num & 0xFU].urb_state); +#endif /* USE_HAL_HCD_REGISTER_CALLBACKS */ } } @@ -1806,12 +1956,13 @@ static void HCD_HC_OUT_IRQHandler(HCD_HandleTypeDef *hhcd, uint8_t chnum) { uint16_t data_xfr; + __IO uint32_t WregCh; /* Get Physical Channel number */ uint32_t phy_chnum = (uint8_t)__HAL_HCD_GET_CHNUM(hhcd); /* Take a Flag snapshot from the CHEP register, due to STRX bits are used for both control &status */ - __IO uint32_t ch_reg = *(__IO uint32_t *)(&(hhcd->Instance->CHEP0R) + phy_chnum); + uint32_t ch_reg = *(__IO uint32_t *)(&(hhcd->Instance->CHEP0R) + phy_chnum); /*------ Manage Correct Transaction ------*/ if ((ch_reg & USB_CH_ERRTX) == 0U) @@ -1822,24 +1973,28 @@ /* correct transaction */ if ((hhcd->Instance->ISTR & USB_ISTR_ERR) == 0U) { - /* Double buffer isochronous out*/ - if ((ch_reg & USB_CH_KIND) == 0U) + /* Double buffer isochronous out */ + if ((ch_reg & USB_CH_KIND) != 0U) + { + HCD_SET_CH_TX_CNT(hhcd->Instance, phy_chnum, 0U); + } +#if (USE_USB_DOUBLE_BUFFER == 1U) + else /* double buffer isochronous out */ { /* Odd Transaction */ if ((ch_reg & USB_CH_DTOG_TX) != 0U) { - hhcd->hc[chnum & 0xFU].pmabuff0_state = USB_PMABUF_EMPTY; + HCD_SET_CH_TX_CNT(hhcd->Instance, phy_chnum, 0U); } /* Even Transaction */ else { - hhcd->hc[chnum & 0xFU].pmabuff1_state = USB_PMABUF_EMPTY; + HCD_SET_CH_RX_CNT(hhcd->Instance, phy_chnum, 0U); } + + USB_DRD_SET_CHEP_TX_STATUS(hhcd->Instance, phy_chnum, USB_CH_TX_DIS); } - else /* single buffer isochronous out */ - { - HCD_SET_CH_TX_CNT(hhcd->Instance, phy_chnum, (uint16_t)0U); - } +#endif /* (USE_USB_DOUBLE_BUFFER == 1U) */ /* Transfer complete state */ hhcd->hc[chnum & 0xFU].state = HC_XFRC; @@ -1848,6 +2003,14 @@ /*Clear Correct Transfer */ HCD_CLEAR_TX_CH_CTR(hhcd->Instance, phy_chnum); + + /*TX COMPLETE*/ +#if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U) + hhcd->HC_NotifyURBChangeCallback(hhcd, (uint8_t)chnum, hhcd->hc[chnum & 0xFU].urb_state); +#else + HAL_HCD_HC_NotifyURBChange_Callback(hhcd, (uint8_t)chnum, hhcd->hc[chnum & 0xFU].urb_state); +#endif /* USE_HAL_HCD_REGISTER_CALLBACKS */ + } else /* Manage all Non Isochronous Transaction */ { @@ -1881,6 +2044,7 @@ hhcd->hc[chnum & 0xFU].xfer_count += data_xfr; hhcd->hc[chnum & 0xFU].state = HC_XFRC; hhcd->hc[chnum & 0xFU].urb_state = URB_DONE; + if ((hhcd->hc[chnum & 0xFU].ep_type == EP_TYPE_BULK) || (hhcd->hc[chnum & 0xFU].ep_type == EP_TYPE_INTR)) { @@ -1888,40 +2052,65 @@ } } } - /* Check NACK Response */ - else if ((ch_reg & USB_CH_TX_STTX) == USB_CH_TX_NAK) + else if (((ch_reg & USB_CHEP_NAK) == USB_CHEP_NAK) || + ((ch_reg & USB_CH_TX_STTX) == USB_CH_TX_NAK)) { + /* Update Channel status */ + hhcd->hc[chnum & 0xFU].state = HC_NAK; hhcd->hc[chnum & 0xFU].urb_state = URB_NOTREADY; hhcd->hc[chnum & 0xFU].ErrCnt = 0U; - hhcd->hc[chnum & 0xFU].state = HC_NAK; - } - /*Check STALL Response*/ + /* Get Channel register value */ + WregCh = *(__IO uint32_t *)(&(hhcd->Instance->CHEP0R) + phy_chnum); + + /*clear NAK status*/ + WregCh &= ~USB_CHEP_NAK & USB_CHEP_REG_MASK; + + /* Update channel register Value */ + HCD_SET_CHANNEL(hhcd->Instance, phy_chnum, WregCh); + + if (hhcd->hc[chnum & 0xFU].doublebuffer == 0U) + { +#if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U) + hhcd->HC_NotifyURBChangeCallback(hhcd, (uint8_t)chnum, hhcd->hc[chnum & 0xFU].urb_state); +#else + HAL_HCD_HC_NotifyURBChange_Callback(hhcd, (uint8_t)chnum, hhcd->hc[chnum & 0xFU].urb_state); +#endif /* USE_HAL_HCD_REGISTER_CALLBACKS */ + } + } + /* Check STALL Response */ else if ((ch_reg & USB_CH_TX_STTX) == USB_CH_TX_STALL) { (void) HAL_HCD_HC_Halt(hhcd, (uint8_t)chnum); hhcd->hc[chnum & 0xFU].state = HC_STALL; hhcd->hc[chnum & 0xFU].urb_state = URB_STALL; } - - /*Check double buffer ACK in case of bulk transaction */ +#if (USE_USB_DOUBLE_BUFFER == 1U) + /* Check double buffer ACK in case of bulk transaction */ else if ((ch_reg & USB_CH_TX_STTX) == USB_CH_TX_ACK_DBUF) { - /*Double buffer management Bulk Out*/ + /* Double buffer management Bulk Out */ (void) HCD_HC_OUT_BulkDb(hhcd, chnum, (uint8_t)phy_chnum, ch_reg); } +#endif /* (USE_USB_DOUBLE_BUFFER == 1U) */ else { /*...*/ } - HAL_HCD_HC_NotifyURBChange_Callback(hhcd, (uint8_t)chnum, - hhcd->hc[chnum & 0xFU].urb_state); + if ((ch_reg & USB_CH_TX_STTX) != USB_CH_TX_NAK) + { +#if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U) + hhcd->HC_NotifyURBChangeCallback(hhcd, (uint8_t)chnum, hhcd->hc[chnum & 0xFU].urb_state); +#else + HAL_HCD_HC_NotifyURBChange_Callback(hhcd, (uint8_t)chnum, hhcd->hc[chnum & 0xFU].urb_state); +#endif /* USE_HAL_HCD_REGISTER_CALLBACKS */ + } + HCD_CLEAR_TX_CH_CTR(hhcd->Instance, phy_chnum); } /* end no isochronous */ } - /*------ Manage Transaction Error------*/ else { @@ -1940,8 +2129,12 @@ /*Clear ERR_TX*/ HCD_CLEAR_TX_CH_ERR(hhcd->Instance, phy_chnum); - HAL_HCD_HC_NotifyURBChange_Callback(hhcd, (uint8_t)chnum, - hhcd->hc[chnum & 0xFU].urb_state); + +#if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U) + hhcd->HC_NotifyURBChangeCallback(hhcd, (uint8_t)chnum, hhcd->hc[chnum & 0xFU].urb_state); +#else + HAL_HCD_HC_NotifyURBChange_Callback(hhcd, (uint8_t)chnum, hhcd->hc[chnum & 0xFU].urb_state); +#endif /* USE_HAL_HCD_REGISTER_CALLBACKS */ } } @@ -1953,52 +2146,76 @@ */ static void HCD_Port_IRQHandler(HCD_HandleTypeDef *hhcd) { - /* J-state detected & LastState=Disconnected */ - if (((hhcd->Instance->FNR & USB_FNR_RXDP) != 0U) && - ((hhcd->HostState == HCD_HCD_STATE_DISCONNECTED) != 0U)) - { - HAL_HCD_Connect_Callback(hhcd); - hhcd->HostState = HCD_HCD_STATE_CONNECTED; - } - /* K-state detected & LastState=Disconnected */ - else if (((hhcd->Instance->ISTR & USB_ISTR_LS_DCONN) != 0U) - && ((hhcd->HostState == HCD_HCD_STATE_DISCONNECTED) != 0U)) - { - HAL_HCD_Connect_Callback(hhcd); - hhcd->HostState = HCD_HCD_STATE_CONNECTED; - } + uint32_t FnrReg = hhcd->Instance->FNR; + uint32_t IstrReg = hhcd->Instance->ISTR; + /* SE0 detected USB Disconnected state */ - else if ((hhcd->Instance->FNR & (USB_FNR_RXDP | USB_FNR_RXDM)) == 0U) + if ((FnrReg & (USB_FNR_RXDP | USB_FNR_RXDM)) == 0U) { - /*Host Port State*/ + /* Host Port State */ hhcd->HostState = HCD_HCD_STATE_DISCONNECTED; - /*Disconnection Callback */ + + /* clear all allocated virtual channel */ + HAL_HCD_ClearPhyChannel(hhcd); + + /* Reset the PMA current pointer */ + (void)HAL_HCD_PMAReset(hhcd); + + /* reset Ep0 Pma allocation state */ + hhcd->ep0_PmaAllocState = 0U; + + /* Disconnection Callback */ +#if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U) + hhcd->DisconnectCallback(hhcd); +#else HAL_HCD_Disconnect_Callback(hhcd); +#endif /* USE_HAL_HCD_REGISTER_CALLBACKS */ + + return; } - /* J-state detected & lastState=Connected: a Missing disconnection is detected */ - else if (((hhcd->Instance->FNR & USB_FNR_RXDP) != 0U) && - ((hhcd->HostState == HCD_HCD_STATE_CONNECTED) != 0U)) + + if ((hhcd->HostState == HCD_HCD_STATE_DISCONNECTED) != 0U) { - /*Disconnection Callback */ - HAL_HCD_Disconnect_Callback(hhcd); - /*Host Port State*/ - hhcd->HostState = HCD_HCD_STATE_DISCONNECTED; - } - /* K-state detected & LastState=Disconnected: a Missing disconnection is detected */ - else if (((hhcd->Instance->ISTR & USB_ISTR_LS_DCONN) != 0U) - && ((hhcd->HostState == HCD_HCD_STATE_CONNECTED) != 0U)) - { - /*Disconnection Callback */ - HAL_HCD_Disconnect_Callback(hhcd); - /*Host Port State*/ - hhcd->HostState = HCD_HCD_STATE_DISCONNECTED; + /* J-state or K-state detected & LastState=Disconnected */ + if (((FnrReg & USB_FNR_RXDP) != 0U) || ((IstrReg & USB_ISTR_LS_DCONN) != 0U)) + { + hhcd->HostState = HCD_HCD_STATE_CONNECTED; + +#if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U) + hhcd->ConnectCallback(hhcd); +#else + HAL_HCD_Connect_Callback(hhcd); +#endif /* USE_HAL_HCD_REGISTER_CALLBACKS */ + } } else { - /* should not be here */ + /* J-state or K-state detected & lastState=Connected: a Missed disconnection is detected */ + if (((FnrReg & USB_FNR_RXDP) != 0U) || ((IstrReg & USB_ISTR_LS_DCONN) != 0U)) + { + /* Host Port State */ + hhcd->HostState = HCD_HCD_STATE_DISCONNECTED; + + /* clear all allocated virtual channel */ + HAL_HCD_ClearPhyChannel(hhcd); + + /* Reset the PMA current pointer */ + (void)HAL_HCD_PMAReset(hhcd); + + /* reset Ep0 PMA allocation state */ + hhcd->ep0_PmaAllocState = 0U; + + /* Disconnection Callback */ +#if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U) + hhcd->DisconnectCallback(hhcd); +#else + HAL_HCD_Disconnect_Callback(hhcd); +#endif /* USE_HAL_HCD_REGISTER_CALLBACKS */ + } } } + /** * @brief Check if the ch_num are already reserved to a physical channel * @param hhcd HCD handle @@ -2013,11 +2230,14 @@ /* Check if the logical channel are already opened */ for (idx = 0U; idx < hhcd->Init.Host_channels; idx++) { - if ((((hhcd->phy_chin_state[idx] & 0x00F0U) >> 4U) == ((uint16_t)ch_num + 1U)) && (hhcd->phy_chin_state[idx] != 0U)) + if ((((hhcd->phy_chin_state[idx] & 0xF0U) >> 4U) == ((uint16_t)ch_num + 1U)) && + (hhcd->phy_chin_state[idx] != 0U)) { return (1U | (idx << 4U)); } - if ((((hhcd->phy_chout_state[idx] & 0x00F0U) >> 4U) == ((uint16_t)ch_num + 1U)) && (hhcd->phy_chout_state[idx] != 0U)) + + if ((((hhcd->phy_chout_state[idx] & 0xF0U) >> 4U) == ((uint16_t)ch_num + 1U)) && + (hhcd->phy_chout_state[idx] != 0U)) { return (1U | (idx << 4U)); } @@ -2026,6 +2246,7 @@ return 0U; } + /** * @brief Get a Logical Channel number from physical Channel * @param hhcd HCD handle @@ -2048,7 +2269,7 @@ } else { - /*Channel not registered Error*/ + /* Channel not registered Error */ return HCD_LOGICAL_CH_NOT_OPENED; } } @@ -2061,11 +2282,13 @@ } else { - /*Channel not registered Error*/ + /* Channel not registered Error */ return HCD_LOGICAL_CH_NOT_OPENED; } } } + + /** * @brief Get a free physical Channel number according to the direction * @param hhcd HCD handle @@ -2087,17 +2310,66 @@ { uint8_t idx; + if ((epnum & 0x7FU) == 0U) + { + idx = 0U; + + if (ch_num == 0U) + { + if (hhcd->phy_chin_state[idx] == 0U) + { + /* chin_state will store the ep_type to be used for the same channel in OUT direction */ + hhcd->phy_chin_state[idx] = (((uint16_t)ch_num + 1U) << 4U) | + ((uint16_t)ep_type + 1U) | /* ep_type(0/1/2/3) so adding + 1 to avoid having a 0 value */ + (((uint16_t)epnum & 0x0FU) << 8U); + } + + if (hhcd->phy_chout_state[idx] == 0U) + { + /* chout_state will store the ep_type to be used for the same channel in IN direction */ + hhcd->phy_chout_state[idx] = (((uint16_t)ch_num + 1U) << 4U) | + ((uint16_t)ep_type + 1U) | /* ep_type(0/1/2/3) so adding + 1 to avoid having a 0 value */ + (((uint16_t)epnum & 0x0FU) << 8U); + } + } + else + { + if ((epnum & 0x80U) != 0U) + { + if (((hhcd->phy_chin_state[idx] & 0xF0U) >> 4U) != ((uint16_t)ch_num + 1U)) + { + /* chin_state will store the ep_type to be used for the same channel in OUT direction */ + hhcd->phy_chin_state[idx] = (((uint16_t)ch_num + 1U) << 4U) | + ((uint16_t)ep_type + 1U) | /* ep_type(0/1/2/3) so adding + 1 to avoid having a 0 value */ + (((uint16_t)epnum & 0x0FU) << 8U); + } + } + else + { + if (((hhcd->phy_chout_state[idx] & 0xF0U) >> 4U) != ((uint16_t)ch_num + 1U)) + { + /* chout_state will store the ep_type to be used for the same channel in IN direction */ + hhcd->phy_chout_state[idx] = (((uint16_t)ch_num + 1U) << 4U) | + ((uint16_t)ep_type + 1U) | /* ep_type(0/1/2/3) so adding + 1 to avoid having a 0 value */ + (((uint16_t)epnum & 0x0FU) << 8U); + } + } + } + + return idx; + } + if ((epnum & 0x80U) != 0U) { /* Find a new available physical in channel */ - for (idx = 0U; idx < hhcd->Init.Host_channels; idx++) + for (idx = 1U; idx < hhcd->Init.Host_channels; idx++) { if ((hhcd->phy_chin_state[idx] == 0U) && /* if PhysicalChannelx(EPCHidx) IN is Free */ ((((hhcd->phy_chout_state[idx] & 0x000FU) == ((uint16_t)ep_type + 1U)) && /* if the same (EPCHidx) Channelx OUT is already opened check if the same type is allocated */ (((hhcd->phy_chout_state[idx] & 0x0F00U) == ((uint16_t)epnum & 0x0FU)))) || /* Check if the same Epnum is allocated then allocate the same physical channelOUT for IN Logical Channel */ (hhcd->phy_chout_state[idx] == 0U))) /* OR if physical Channel OUT (EPCHidx)is free */ { - /* chin_state will store the ep_type to be used for the same channel in out direction */ + /* chin_state will store the ep_type to be used for the same channel in OUT direction */ hhcd->phy_chin_state[idx] = (((uint16_t)ch_num + 1U) << 4U) | ((uint16_t)ep_type + 1U) | /* ep_type(0/1/2/3) so adding + 1 to avoid having a 0 value */ (((uint16_t)epnum & 0x0FU) << 8U); @@ -2109,7 +2381,7 @@ else { /* Find a new available physical out channel */ - for (idx = 0U; idx < hhcd->Init.Host_channels; idx++) + for (idx = 1U; idx < hhcd->Init.Host_channels; idx++) { /* if there is a free out channel and the same channel is used for the in direction then check if same ep_type used */ if ((hhcd->phy_chout_state[idx] == 0U) && @@ -2117,7 +2389,7 @@ ((hhcd->phy_chin_state[idx] & 0x0F00U) == ((uint16_t)epnum & 0x0FU))) || (hhcd->phy_chin_state[idx] == 0U))) { - /* chin_state will store the ep_type to be used for the same channel in out direction */ + /* chout_state will store the ep_type to be used for the same channel in IN direction */ hhcd->phy_chout_state[idx] = (((uint16_t)ch_num + 1U) << 4U) | ((uint16_t)ep_type + 1U) | /* ep_type(0/1/2/3) so adding +1 to avoid having a 0 value */ (((uint16_t)epnum & 0x0FU) << 8U); @@ -2127,7 +2399,7 @@ } } - /*in case of Error */ + /* in case of Error */ return HCD_FREE_CH_NOT_FOUND; } @@ -2171,8 +2443,10 @@ */ static uint16_t HAL_HCD_GetFreePMA(HCD_HandleTypeDef *hhcd, uint16_t mps) { - uint32_t Entry, FreeBlocks = 0U; - uint8_t FirstFreeBlock_col = 0U, FirstFreeBlock_line = 0U; + uint32_t Entry; + uint32_t FreeBlocks = 0U; + uint8_t FirstFreeBlock_col = 0U; + uint8_t FirstFreeBlock_line = 0U; uint8_t ColIndex; uint16_t NbrReqBlocks; uint16_t mps_t = mps; @@ -2278,7 +2552,9 @@ uint16_t ch_kind, uint16_t mps) { uint16_t pma_addr0; +#if (USE_USB_DOUBLE_BUFFER == 1U) uint16_t pma_addr1; /* used for double buffer mode if enabled */ +#endif /* (USE_USB_DOUBLE_BUFFER == 1U) */ /* Host Channel */ HCD_HCTypeDef *hc = &(hhcd->hc[ch_num]); @@ -2299,19 +2575,33 @@ /* Single Buffer */ hc->doublebuffer = 0U; + if (hc->ep_num == 0U) + { + hhcd->ep0_PmaAllocState = ch_num; + hhcd->ep0_PmaAllocState |= (1U << 8); + } + /* Configure the PMA */ if (hc->ch_dir == CH_IN_DIR) { - (USB_DRD_PMA_BUFF + hc->phy_ch_num)->RXBD = pma_addr0; + hc->pmaaddr1 = pma_addr0; + (USB_DRD_PMA_BUFF + hc->phy_ch_num)->RXBD = hc->pmaaddr1; + + if (hc->ep_num == 0U) + { + hhcd->ep0_PmaAllocState |= (CH_IN_DIR << 4); + } } else { - (USB_DRD_PMA_BUFF + hc->phy_ch_num)->TXBD = pma_addr0; + hc->pmaaddr0 = pma_addr0; + (USB_DRD_PMA_BUFF + hc->phy_ch_num)->TXBD = hc->pmaaddr0; } - /* Set the Pmaaddress */ + /* Set the PmaAddress */ hc->pmaadress = pma_addr0; } +#if (USE_USB_DOUBLE_BUFFER == 1U) else /* USB_DBL_BUF */ { /* Double Buffer Endpoint */ @@ -2349,6 +2639,7 @@ } } } +#endif /* (USE_USB_DOUBLE_BUFFER == 1U) */ } return HAL_OK; @@ -2362,8 +2653,10 @@ */ HAL_StatusTypeDef HAL_HCD_PMADeAlloc(HCD_HandleTypeDef *hhcd, uint8_t ch_num) { - HAL_StatusTypeDef status; + HAL_StatusTypeDef status = HAL_OK; +#if (USE_USB_DOUBLE_BUFFER == 1U) uint8_t Err = 0U; +#endif /* (USE_USB_DOUBLE_BUFFER == 1U) */ /* Host Channel */ HCD_HCTypeDef *hc = &(hhcd->hc[ch_num]); @@ -2373,6 +2666,7 @@ { status = HAL_HCD_PMAFree(hhcd, hc->pmaadress, hc->max_packet); } +#if (USE_USB_DOUBLE_BUFFER == 1U) else /* Double buffer */ { status = HAL_HCD_PMAFree(hhcd, hc->pmaaddr0, hc->max_packet); @@ -2392,6 +2686,7 @@ return HAL_ERROR; } } +#endif /* (USE_USB_DOUBLE_BUFFER == 1U) */ return status; } @@ -2420,7 +2715,7 @@ } /** - * @brief PMA FRee + * @brief PMA Free * @param hhcd Host instance * @param pma_base PMA base offset stored in hhcd->hc.pmaaddr * @param mps Max Packet Size
diff --git a/Src/stm32g0xx_hal_pcd.c b/Src/stm32g0xx_hal_pcd.c index 7e21083..48cceb9 100644 --- a/Src/stm32g0xx_hal_pcd.c +++ b/Src/stm32g0xx_hal_pcd.c
@@ -88,8 +88,10 @@ */ static HAL_StatusTypeDef PCD_EP_ISR_Handler(PCD_HandleTypeDef *hpcd); +#if (USE_USB_DOUBLE_BUFFER == 1U) static HAL_StatusTypeDef HAL_PCD_EP_DB_Transmit(PCD_HandleTypeDef *hpcd, PCD_EPTypeDef *ep, uint16_t wEPVal); static uint16_t HAL_PCD_EP_DB_Receive(PCD_HandleTypeDef *hpcd, PCD_EPTypeDef *ep, uint16_t wEPVal); +#endif /* (USE_USB_DOUBLE_BUFFER == 1U) */ /** * @} @@ -167,6 +169,9 @@ hpcd->State = HAL_PCD_STATE_BUSY; + /* DMA Not supported for FS instance, Force to Zero */ + hpcd->Init.dma_enable = 0U; + /* Disable the Interrupts */ __HAL_PCD_DISABLE(hpcd); @@ -396,7 +401,7 @@ /** * @brief Unregister an USB PCD Callback - * USB PCD callabck is redirected to the weak predefined callback + * USB PCD callback is redirected to the weak predefined callback * @param hpcd USB PCD handle * @param CallbackID ID of the callback to be unregistered * This parameter can be one of the following values: @@ -1005,20 +1010,24 @@ */ void HAL_PCD_IRQHandler(PCD_HandleTypeDef *hpcd) { + uint32_t wIstr = USB_ReadInterrupts(hpcd->Instance); + /* check if this is an USB pending IT */ if ((SYSCFG->IT_LINE_SR[8] & (0x1U << 2)) == 0U) { return; } - if (__HAL_PCD_GET_FLAG(hpcd, USB_ISTR_CTR)) + if ((wIstr & USB_ISTR_CTR) == USB_ISTR_CTR) { /* servicing of the endpoint correct transfer interrupt */ /* clear of the CTR flag into the sub */ (void)PCD_EP_ISR_Handler(hpcd); + + return; } - if (__HAL_PCD_GET_FLAG(hpcd, USB_ISTR_RESET)) + if ((wIstr & USB_ISTR_RESET) == USB_ISTR_RESET) { __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_RESET); @@ -1029,19 +1038,25 @@ #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */ (void)HAL_PCD_SetAddress(hpcd, 0U); + + return; } - if (__HAL_PCD_GET_FLAG(hpcd, USB_ISTR_PMAOVR)) + if ((wIstr & USB_ISTR_PMAOVR) == USB_ISTR_PMAOVR) { __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_PMAOVR); + + return; } - if (__HAL_PCD_GET_FLAG(hpcd, USB_ISTR_ERR)) + if ((wIstr & USB_ISTR_ERR) == USB_ISTR_ERR) { __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_ERR); + + return; } - if (__HAL_PCD_GET_FLAG(hpcd, USB_ISTR_WKUP)) + if ((wIstr & USB_ISTR_WKUP) == USB_ISTR_WKUP) { hpcd->Instance->CNTR &= ~(USB_CNTR_SUSPRDY); hpcd->Instance->CNTR &= ~(USB_CNTR_SUSPEN); @@ -1063,9 +1078,11 @@ #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */ __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_WKUP); + + return; } - if (__HAL_PCD_GET_FLAG(hpcd, USB_ISTR_SUSP)) + if ((wIstr & USB_ISTR_SUSP) == USB_ISTR_SUSP) { /* Force low-power mode in the macrocell */ hpcd->Instance->CNTR |= USB_CNTR_SUSPEN; @@ -1080,10 +1097,12 @@ #else HAL_PCD_SuspendCallback(hpcd); #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */ + + return; } /* Handle LPM Interrupt */ - if (__HAL_PCD_GET_FLAG(hpcd, USB_ISTR_L1REQ)) + if ((wIstr & USB_ISTR_L1REQ) == USB_ISTR_L1REQ) { __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_L1REQ); if (hpcd->LPM_State == LPM_L0) @@ -1108,9 +1127,11 @@ HAL_PCD_SuspendCallback(hpcd); #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */ } + + return; } - if (__HAL_PCD_GET_FLAG(hpcd, USB_ISTR_SOF)) + if ((wIstr & USB_ISTR_SOF) == USB_ISTR_SOF) { __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_SOF); @@ -1119,12 +1140,16 @@ #else HAL_PCD_SOFCallback(hpcd); #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */ + + return; } - if (__HAL_PCD_GET_FLAG(hpcd, USB_ISTR_ESOF)) + if ((wIstr & USB_ISTR_ESOF) == USB_ISTR_ESOF) { /* clear ESOF flag in ISTR */ __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_ESOF); + + return; } } @@ -1592,6 +1617,32 @@ } /** + * @brief Abort an USB EP 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) +{ + HAL_StatusTypeDef ret; + PCD_EPTypeDef *ep; + + if ((0x80U & ep_addr) == 0x80U) + { + ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK]; + } + else + { + ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK]; + } + + /* Stop Xfer */ + ret = USB_EPStopXfer(hpcd->Instance, ep); + + return ret; +} + +/** * @brief Flush an endpoint * @param hpcd PCD handle * @param ep_addr endpoint address @@ -1677,7 +1728,10 @@ static HAL_StatusTypeDef PCD_EP_ISR_Handler(PCD_HandleTypeDef *hpcd) { PCD_EPTypeDef *ep; - uint16_t count, wIstr, wEPVal, TxByteNbre; + uint16_t count; + uint16_t wIstr; + uint16_t wEPVal; + uint16_t TxPctSize; uint8_t epindex; /* stay in loop while pending interrupts */ @@ -1797,6 +1851,7 @@ USB_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaadress, count); } } +#if (USE_USB_DOUBLE_BUFFER == 1U) else { /* manage double buffer bulk out */ @@ -1807,7 +1862,7 @@ else /* manage double buffer iso out */ { /* free EP OUT Buffer */ - PCD_FreeUserBuffer(hpcd->Instance, ep->num, 0U); + PCD_FREE_USER_BUFFER(hpcd->Instance, ep->num, 0U); if ((PCD_GET_ENDPOINT(hpcd->Instance, ep->num) & USB_EP_DTOG_RX) != 0U) { @@ -1831,6 +1886,8 @@ } } } +#endif /* (USE_USB_DOUBLE_BUFFER == 1U) */ + /* multi-packet on the NON control OUT endpoint */ ep->xfer_count += count; ep->xfer_buff += count; @@ -1848,7 +1905,6 @@ { (void) USB_EPStartXfer(hpcd->Instance, ep); } - } if ((wEPVal & USB_EP_VTTX) != 0U) @@ -1858,44 +1914,73 @@ /* clear int flag */ PCD_CLEAR_TX_EP_CTR(hpcd->Instance, epindex); - /* Manage all non bulk/isoc transaction Bulk Single Buffer Transaction */ - if ((ep->type == EP_TYPE_INTR) || (ep->type == EP_TYPE_CTRL) || - ((ep->type == EP_TYPE_BULK) && ((wEPVal & USB_EP_KIND) == 0U))) + if (ep->type != EP_TYPE_BULK) { - /* multi-packet on the NON control IN endpoint */ - TxByteNbre = (uint16_t)PCD_GET_EP_TX_CNT(hpcd->Instance, ep->num); + ep->xfer_len = 0U; - if (ep->xfer_len > TxByteNbre) +#if (USE_USB_DOUBLE_BUFFER == 1U) + if (ep->doublebuffer != 0U) { - ep->xfer_len -= TxByteNbre; + if ((wEPVal & USB_EP_DTOG_TX) != 0U) + { + PCD_SET_EP_DBUF0_CNT(hpcd->Instance, ep->num, ep->is_in, 0U); + } + else + { + PCD_SET_EP_DBUF1_CNT(hpcd->Instance, ep->num, ep->is_in, 0U); + } } - else - { - ep->xfer_len = 0U; - } +#endif /* (USE_USB_DOUBLE_BUFFER == 1U) */ - /* Zero Length Packet? */ - if (ep->xfer_len == 0U) - { - /* TX COMPLETE */ + /* TX COMPLETE */ #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U) - hpcd->DataInStageCallback(hpcd, ep->num); + hpcd->DataInStageCallback(hpcd, ep->num); #else - HAL_PCD_DataInStageCallback(hpcd, ep->num); + HAL_PCD_DataInStageCallback(hpcd, ep->num); #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */ - } - else - { - /* Transfer is not yet Done */ - ep->xfer_buff += TxByteNbre; - ep->xfer_count += TxByteNbre; - (void)USB_EPStartXfer(hpcd->Instance, ep); - } } - /* Double Buffer Iso/bulk IN (bulk transfer Len > Ep_Mps) */ else { - (void)HAL_PCD_EP_DB_Transmit(hpcd, ep, wEPVal); + /* Manage Bulk Single Buffer Transaction */ + if ((wEPVal & USB_EP_KIND) == 0U) + { + /* multi-packet on the NON control IN endpoint */ + TxPctSize = (uint16_t)PCD_GET_EP_TX_CNT(hpcd->Instance, ep->num); + + if (ep->xfer_len > TxPctSize) + { + ep->xfer_len -= TxPctSize; + } + else + { + ep->xfer_len = 0U; + } + + /* Zero Length Packet? */ + if (ep->xfer_len == 0U) + { + /* TX COMPLETE */ +#if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U) + hpcd->DataInStageCallback(hpcd, ep->num); +#else + HAL_PCD_DataInStageCallback(hpcd, ep->num); +#endif /* USE_HAL_PCD_REGISTER_CALLBACKS */ + } + else + { + /* Transfer is not yet Done */ + ep->xfer_buff += TxPctSize; + ep->xfer_count += TxPctSize; + (void)USB_EPStartXfer(hpcd->Instance, ep); + } + } +#if (USE_USB_DOUBLE_BUFFER == 1U) + /* Double Buffer bulk IN (bulk transfer Len > Ep_Mps) */ + else + { + (void)HAL_PCD_EP_DB_Transmit(hpcd, ep, wEPVal); + } +#endif /* (USE_USB_DOUBLE_BUFFER == 1U) */ } } } @@ -1905,6 +1990,7 @@ } +#if (USE_USB_DOUBLE_BUFFER == 1U) /** * @brief Manage double buffer bulk out transaction from ISR * @param hpcd PCD handle @@ -1938,10 +2024,10 @@ PCD_SET_EP_RX_STATUS(hpcd->Instance, ep->num, USB_EP_RX_NAK); } - /* Check if Buffer1 is in blocked sate which requires to toggle */ + /* Check if Buffer1 is in blocked state which requires to toggle */ if ((wEPVal & USB_EP_DTOG_TX) != 0U) { - PCD_FreeUserBuffer(hpcd->Instance, ep->num, 0U); + PCD_FREE_USER_BUFFER(hpcd->Instance, ep->num, 0U); } if (count != 0U) @@ -1973,7 +2059,7 @@ /*Need to FreeUser Buffer*/ if ((wEPVal & USB_EP_DTOG_TX) == 0U) { - PCD_FreeUserBuffer(hpcd->Instance, ep->num, 0U); + PCD_FREE_USER_BUFFER(hpcd->Instance, ep->num, 0U); } if (count != 0U) @@ -1997,22 +2083,23 @@ PCD_EPTypeDef *ep, uint16_t wEPVal) { uint32_t len; - uint16_t TxByteNbre; + uint16_t TxPctSize; /* Data Buffer0 ACK received */ if ((wEPVal & USB_EP_DTOG_TX) != 0U) { /* multi-packet on the NON control IN endpoint */ - TxByteNbre = (uint16_t)PCD_GET_EP_DBUF0_CNT(hpcd->Instance, ep->num); + TxPctSize = (uint16_t)PCD_GET_EP_DBUF0_CNT(hpcd->Instance, ep->num); - if (ep->xfer_len > TxByteNbre) + if (ep->xfer_len > TxPctSize) { - ep->xfer_len -= TxByteNbre; + ep->xfer_len -= TxPctSize; } else { ep->xfer_len = 0U; } + /* Transfer is completed */ if (ep->xfer_len == 0U) { @@ -2028,7 +2115,7 @@ if ((wEPVal & USB_EP_DTOG_RX) != 0U) { - PCD_FreeUserBuffer(hpcd->Instance, ep->num, 1U); + PCD_FREE_USER_BUFFER(hpcd->Instance, ep->num, 1U); } } else /* Transfer is not yet Done */ @@ -2036,14 +2123,14 @@ /* need to Free USB Buff */ if ((wEPVal & USB_EP_DTOG_RX) != 0U) { - PCD_FreeUserBuffer(hpcd->Instance, ep->num, 1U); + PCD_FREE_USER_BUFFER(hpcd->Instance, ep->num, 1U); } /* Still there is data to Fill in the next Buffer */ if (ep->xfer_fill_db == 1U) { - ep->xfer_buff += TxByteNbre; - ep->xfer_count += TxByteNbre; + ep->xfer_buff += TxPctSize; + ep->xfer_count += TxPctSize; /* Calculate the len of the new buffer to fill */ if (ep->xfer_len_db >= ep->maxpacket) @@ -2053,7 +2140,7 @@ } else if (ep->xfer_len_db == 0U) { - len = TxByteNbre; + len = TxPctSize; ep->xfer_fill_db = 0U; } else @@ -2075,11 +2162,11 @@ else /* Data Buffer1 ACK received */ { /* multi-packet on the NON control IN endpoint */ - TxByteNbre = (uint16_t)PCD_GET_EP_DBUF1_CNT(hpcd->Instance, ep->num); + TxPctSize = (uint16_t)PCD_GET_EP_DBUF1_CNT(hpcd->Instance, ep->num); - if (ep->xfer_len >= TxByteNbre) + if (ep->xfer_len >= TxPctSize) { - ep->xfer_len -= TxByteNbre; + ep->xfer_len -= TxPctSize; } else { @@ -2102,7 +2189,7 @@ /* need to Free USB Buff */ if ((wEPVal & USB_EP_DTOG_RX) == 0U) { - PCD_FreeUserBuffer(hpcd->Instance, ep->num, 1U); + PCD_FREE_USER_BUFFER(hpcd->Instance, ep->num, 1U); } } else /* Transfer is not yet Done */ @@ -2110,14 +2197,14 @@ /* need to Free USB Buff */ if ((wEPVal & USB_EP_DTOG_RX) == 0U) { - PCD_FreeUserBuffer(hpcd->Instance, ep->num, 1U); + PCD_FREE_USER_BUFFER(hpcd->Instance, ep->num, 1U); } /* Still there is data to Fill in the next Buffer */ if (ep->xfer_fill_db == 1U) { - ep->xfer_buff += TxByteNbre; - ep->xfer_count += TxByteNbre; + ep->xfer_buff += TxPctSize; + ep->xfer_count += TxPctSize; /* Calculate the len of the new buffer to fill */ if (ep->xfer_len_db >= ep->maxpacket) @@ -2127,7 +2214,7 @@ } else if (ep->xfer_len_db == 0U) { - len = TxByteNbre; + len = TxPctSize; ep->xfer_fill_db = 0U; } else @@ -2151,6 +2238,7 @@ return HAL_OK; } +#endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
diff --git a/Src/stm32g0xx_hal_pcd_ex.c b/Src/stm32g0xx_hal_pcd_ex.c index c88c44e..3448b2a 100644 --- a/Src/stm32g0xx_hal_pcd_ex.c +++ b/Src/stm32g0xx_hal_pcd_ex.c
@@ -101,6 +101,7 @@ /* Configure the PMA */ ep->pmaadress = (uint16_t)pmaadress; } +#if (USE_USB_DOUBLE_BUFFER == 1U) else /* USB_DBL_BUF */ { /* Double Buffer Endpoint */ @@ -109,6 +110,7 @@ ep->pmaaddr0 = (uint16_t)(pmaadress & 0xFFFFU); ep->pmaaddr1 = (uint16_t)((pmaadress & 0xFFFF0000U) >> 16); } +#endif /* (USE_USB_DOUBLE_BUFFER == 1U) */ return HAL_OK; } @@ -160,7 +162,7 @@ USB_DRD_TypeDef *USBx = hpcd->Instance; uint32_t tickstart = HAL_GetTick(); - /* Wait Detect flag or a timeout is happen*/ + /* Wait Detect flag or a timeout is happen */ while ((USBx->BCDR & USB_BCDR_DCDET) == 0U) { /* Check for the Timeout */
diff --git a/Src/stm32g0xx_ll_usb.c b/Src/stm32g0xx_ll_usb.c index a83ddc8..be46615 100644 --- a/Src/stm32g0xx_ll_usb.c +++ b/Src/stm32g0xx_ll_usb.c
@@ -53,8 +53,8 @@ /* Private function prototypes -----------------------------------------------*/ /* Private functions ---------------------------------------------------------*/ - static HAL_StatusTypeDef USB_CoreReset(USB_DRD_TypeDef *USBx); +#if (USE_USB_DOUBLE_BUFFER == 1U) static HAL_StatusTypeDef USB_HC_BULK_DB_StartXfer(USB_DRD_TypeDef *USBx, USB_DRD_HCTypeDef *hc, uint32_t ch_reg, @@ -63,6 +63,7 @@ static HAL_StatusTypeDef USB_HC_ISO_DB_StartXfer(USB_DRD_TypeDef *USBx, USB_DRD_HCTypeDef *hc, uint32_t len); +#endif /* (USE_USB_DOUBLE_BUFFER == 1U) */ /** * @brief Reset the USB Core (needed after USB clock settings change) @@ -260,22 +261,31 @@ } else { - /*Set the endpoint Receive buffer address */ + /* Set the endpoint Receive buffer address */ PCD_SET_EP_RX_ADDRESS(USBx, ep->num, ep->pmaadress); - /*Set the endpoint Receive buffer counter*/ + /* Set the endpoint Receive buffer counter */ PCD_SET_EP_RX_CNT(USBx, ep->num, ep->maxpacket); PCD_CLEAR_RX_DTOG(USBx, ep->num); - /* Configure VALID status for the Endpoint*/ + /* Configure VALID status for the Endpoint */ PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_VALID); } } - /*Double Buffer*/ +#if (USE_USB_DOUBLE_BUFFER == 1U) + /* Double Buffer */ else { - /* Set the endpoint as double buffered */ - PCD_SET_EP_DBUF(USBx, ep->num); + if (ep->type == EP_TYPE_BULK) + { + /* Set bulk endpoint as double buffered */ + PCD_SET_BULK_EP_DBUF(USBx, ep->num); + } + else + { + /* Set the ISOC endpoint in double buffer mode */ + PCD_CLEAR_EP_KIND(USBx, ep->num); + } /* Set buffer address for double buffered mode */ PCD_SET_EP_DBUF_ADDR(USBx, ep->num, ep->pmaaddr0, ep->pmaaddr1); @@ -309,6 +319,7 @@ PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_DIS); } } +#endif /* (USE_USB_DOUBLE_BUFFER == 1U) */ return ret; } @@ -327,18 +338,20 @@ { PCD_CLEAR_TX_DTOG(USBx, ep->num); - /* Configure DISABLE status for the Endpoint*/ + /* Configure DISABLE status for the Endpoint */ PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_DIS); } + else { PCD_CLEAR_RX_DTOG(USBx, ep->num); - /* Configure DISABLE status for the Endpoint*/ + /* Configure DISABLE status for the Endpoint */ PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_DIS); } } - /*Double Buffer*/ +#if (USE_USB_DOUBLE_BUFFER == 1U) + /* Double Buffer */ else { if (ep->is_in == 0U) @@ -365,6 +378,7 @@ PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_DIS); } } +#endif /* (USE_USB_DOUBLE_BUFFER == 1U) */ return HAL_OK; } @@ -378,8 +392,10 @@ HAL_StatusTypeDef USB_EPStartXfer(USB_DRD_TypeDef *USBx, USB_DRD_EPTypeDef *ep) { uint32_t len; +#if (USE_USB_DOUBLE_BUFFER == 1U) uint16_t pmabuffer; uint16_t wEPVal; +#endif /* (USE_USB_DOUBLE_BUFFER == 1U) */ /* IN endpoint */ if (ep->is_in == 1U) @@ -400,6 +416,7 @@ USB_WritePMA(USBx, ep->xfer_buff, ep->pmaadress, (uint16_t)len); PCD_SET_EP_TX_CNT(USBx, ep->num, len); } +#if (USE_USB_DOUBLE_BUFFER == 1U) else { /* double buffer bulk management */ @@ -408,7 +425,7 @@ if (ep->xfer_len_db > ep->maxpacket) { /* enable double buffer */ - PCD_SET_EP_DBUF(USBx, ep->num); + PCD_SET_BULK_EP_DBUF(USBx, ep->num); /* each Time to write in PMA xfer_len_db will */ ep->xfer_len_db -= len; @@ -474,8 +491,8 @@ { len = ep->xfer_len_db; - /* disable double buffer mode */ - PCD_CLEAR_EP_DBUF(USBx, ep->num); + /* disable double buffer mode for Bulk endpoint */ + PCD_CLEAR_BULK_EP_DBUF(USBx, ep->num); /* Set Tx count with nbre of byte to be transmitted */ PCD_SET_EP_TX_CNT(USBx, ep->num, len); @@ -484,14 +501,9 @@ /* Write the user buffer to USB PMA */ USB_WritePMA(USBx, ep->xfer_buff, pmabuffer, (uint16_t)len); } - }/* end if bulk double buffer */ - - /* manage isochronous double buffer IN mode */ - else + } + else /* manage isochronous double buffer IN mode */ { - /* enable double buffer */ - PCD_SET_EP_DBUF(USBx, ep->num); - /* each Time to write in PMA xfer_len_db will */ ep->xfer_len_db -= len; @@ -504,27 +516,6 @@ /* Write the user buffer to USB PMA */ USB_WritePMA(USBx, ep->xfer_buff, pmabuffer, (uint16_t)len); - ep->xfer_buff += len; - - if (ep->xfer_len_db > ep->maxpacket) - { - ep->xfer_len_db -= len; - } - else - { - len = ep->xfer_len_db; - ep->xfer_len_db = 0U; - } - - if (len > 0U) - { - /* Set the Double buffer counter for pmabuffer0 */ - PCD_SET_EP_DBUF0_CNT(USBx, ep->num, ep->is_in, len); - pmabuffer = ep->pmaaddr0; - - /* Write the user buffer to USB PMA */ - USB_WritePMA(USBx, ep->xfer_buff, pmabuffer, (uint16_t)len); - } } else { @@ -534,30 +525,10 @@ /* Write the user buffer to USB PMA */ USB_WritePMA(USBx, ep->xfer_buff, pmabuffer, (uint16_t)len); - ep->xfer_buff += len; - - if (ep->xfer_len_db > ep->maxpacket) - { - ep->xfer_len_db -= len; - } - else - { - len = ep->xfer_len_db; - ep->xfer_len_db = 0U; - } - - if (len > 0U) - { - /* Set the Double buffer counter for pmabuffer1 */ - PCD_SET_EP_DBUF1_CNT(USBx, ep->num, ep->is_in, len); - pmabuffer = ep->pmaaddr1; - - /* Write the user buffer to USB PMA */ - USB_WritePMA(USBx, ep->xfer_buff, pmabuffer, (uint16_t)len); - } } } } +#endif /* (USE_USB_DOUBLE_BUFFER == 1U) */ PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_VALID); } @@ -579,6 +550,7 @@ /* configure and validate Rx endpoint */ PCD_SET_EP_RX_CNT(USBx, ep->num, len); } +#if (USE_USB_DOUBLE_BUFFER == 1U) else { /* First Transfer Coming From HAL_PCD_EP_Receive & From ISR */ @@ -597,7 +569,7 @@ if ((((wEPVal & USB_EP_DTOG_RX) != 0U) && ((wEPVal & USB_EP_DTOG_TX) != 0U)) || (((wEPVal & USB_EP_DTOG_RX) == 0U) && ((wEPVal & USB_EP_DTOG_TX) == 0U))) { - PCD_FreeUserBuffer(USBx, ep->num, 0U); + PCD_FREE_USER_BUFFER(USBx, ep->num, 0U); } } } @@ -622,6 +594,7 @@ return HAL_ERROR; } } +#endif /* (USE_USB_DOUBLE_BUFFER == 1U) */ PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_VALID); } @@ -681,7 +654,52 @@ return HAL_OK; } -#endif /* HAL_PCD_MODULE_ENABLED */ + +/** + * @brief USB_EPStoptXfer Stop transfer on an EP + * @param USBx usb device instance + * @param ep pointer to endpoint structure + * @retval HAL status + */ +HAL_StatusTypeDef USB_EPStopXfer(USB_DRD_TypeDef *USBx, USB_DRD_EPTypeDef *ep) +{ + /* IN endpoint */ + if (ep->is_in == 1U) + { + if (ep->doublebuffer == 0U) + { + if (ep->type != EP_TYPE_ISOC) + { + /* Configure NAK status for the Endpoint */ + PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_NAK); + } + else + { + /* Configure TX Endpoint to disabled state */ + PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_DIS); + } + } + } + else /* OUT endpoint */ + { + if (ep->doublebuffer == 0U) + { + if (ep->type != EP_TYPE_ISOC) + { + /* Configure NAK status for the Endpoint */ + PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_NAK); + } + else + { + /* Configure RX Endpoint to disabled state */ + PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_DIS); + } + } + } + + return HAL_OK; +} +#endif /* defined (HAL_PCD_MODULE_ENABLED) */ /** * @brief USB_StopDevice Stop the usb device mode @@ -794,7 +812,8 @@ void USB_WritePMA(USB_DRD_TypeDef *USBx, uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes) { UNUSED(USBx); - uint32_t count, tmp; + uint32_t tmp; + uint32_t count; __IO uint32_t *pdwVal; uint32_t NbWords = ((uint32_t)wNBytes + 3U) >> 2U; /* Due to the PMA access 32bit only so the last non word data should be processed alone */ @@ -850,7 +869,8 @@ void USB_ReadPMA(USB_DRD_TypeDef *USBx, uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes) { UNUSED(USBx); - uint32_t count, tmp; + uint32_t count; + uint32_t tmp; __IO uint32_t *pdwVal; uint32_t NbWords = ((uint32_t)wNBytes + 3U) >> 2U; /*Due to the PMA access 32bit only so the last non word data should be processed alone */ @@ -985,7 +1005,7 @@ * @brief Set the channel Kind (Single/double buffer mode) * @param USBx Selected device * @param phy_ch_num Selected device - * @param db_state double state can be USB_DRD_DBUFF_ENBALE/USB_DRD_DBUFF_DISABLE + * @param db_state double state can be USB_DRD_XXX_DBUFF_ENBALE/USB_DRD_XXX_DBUFF_DISABLE * @retval HAL status */ HAL_StatusTypeDef USB_HC_DoubleBuffer(USB_DRD_TypeDef *USBx, @@ -993,7 +1013,7 @@ { uint32_t tmp; - if (db_state == USB_DRD_DBUFF_ENBALE) + if ((db_state == USB_DRD_BULK_DBUFF_ENBALE) || (db_state == USB_DRD_ISOC_DBUFF_DISABLE)) { tmp = (USB_DRD_GET_CHEP(USBx, phy_ch_num) | USB_CH_KIND) & USB_CHEP_DB_MSK; } @@ -1039,6 +1059,8 @@ uint32_t wChRegVal; uint32_t HostCoreSpeed; + UNUSED(mps); + wChRegVal = USB_DRD_GET_CHEP(USBx, phy_ch_num) & USB_CH_T_MASK; /* initialize host Channel */ @@ -1081,9 +1103,6 @@ /* Set the dev_address & ep type */ USB_DRD_SET_CHEP(USBx, phy_ch_num, (wChRegVal | USB_CH_VTRX | USB_CH_VTTX)); - /* Set the Rx Count */ - USB_DRD_SET_CHEP_RX_CNT(USBx, phy_ch_num, mps); - return ret; } @@ -1093,12 +1112,13 @@ * @param hc pointer to host channel structure * @retval HAL state */ -HAL_StatusTypeDef USB_HC_StartXfer(USB_DRD_TypeDef *USBx, - USB_DRD_HCTypeDef *hc) +HAL_StatusTypeDef USB_HC_StartXfer(USB_DRD_TypeDef *USBx, USB_DRD_HCTypeDef *hc) { uint32_t len; uint32_t phy_ch_num = (uint32_t)hc->phy_ch_num; +#if (USE_USB_DOUBLE_BUFFER == 1U) uint32_t ch_reg = USB_DRD_GET_CHEP(USBx, phy_ch_num); +#endif /* USE_USB_DOUBLE_BUFFER */ if (hc->ch_dir == CH_IN_DIR) /* In Channel */ { @@ -1117,19 +1137,21 @@ /* Set RX buffer count */ USB_DRD_SET_CHEP_RX_CNT(USBx, phy_ch_num, len); } +#if (USE_USB_DOUBLE_BUFFER == 1U) else if (hc->ep_type == EP_TYPE_BULK) { /* Double buffer activated */ if ((hc->xfer_len > hc->max_packet)) { - (void)USB_HC_DoubleBuffer(USBx, (uint8_t)phy_ch_num, USB_DRD_DBUFF_ENBALE); + (void)USB_HC_DoubleBuffer(USBx, (uint8_t)phy_ch_num, USB_DRD_BULK_DBUFF_ENBALE); /*Set the Double buffer counter*/ - USB_DRD_SET_CHEP_DBUF_CNT(USBx, phy_ch_num, hc->ch_dir, len); + USB_DRD_SET_CHEP_DBUF0_CNT(USBx, phy_ch_num, 0U, len); + USB_DRD_SET_CHEP_DBUF1_CNT(USBx, phy_ch_num, 0U, len); } else /* switch to single buffer mode */ { - (void)USB_HC_DoubleBuffer(USBx, (uint8_t)phy_ch_num, USB_DRD_DBUFF_DISABLE); + (void)USB_HC_DoubleBuffer(USBx, (uint8_t)phy_ch_num, USB_DRD_BULK_DBUFF_DISABLE); /* Set RX buffer count */ USB_DRD_SET_CHEP_RX_CNT(USBx, phy_ch_num, len); @@ -1137,9 +1159,11 @@ } else /* isochronous */ { - /*Set the Double buffer counter*/ - USB_DRD_SET_CHEP_DBUF_CNT(USBx, phy_ch_num, hc->ch_dir, len); + /* Set the Double buffer counter */ + USB_DRD_SET_CHEP_DBUF0_CNT(USBx, phy_ch_num, 0U, len); + USB_DRD_SET_CHEP_DBUF1_CNT(USBx, phy_ch_num, 0U, len); } +#endif /* USE_USB_DOUBLE_BUFFER */ /*Enable host channel */ USB_DRD_SET_CHEP_RX_STATUS(USBx, phy_ch_num, USB_CHEP_RX_STRX); @@ -1161,12 +1185,14 @@ { USB_WritePMA(USBx, hc->xfer_buff, hc->pmaadress, (uint16_t)len); USB_DRD_SET_CHEP_TX_CNT(USBx, phy_ch_num, (uint16_t)len); + /*SET PID SETUP */ if ((hc->data_pid) == HC_PID_SETUP) { USB_DRD_CHEP_TX_SETUP(USBx, phy_ch_num); } } +#if (USE_USB_DOUBLE_BUFFER == 1U) else if (hc->ep_type == EP_TYPE_BULK) { (void)USB_HC_BULK_DB_StartXfer(USBx, hc, ch_reg, &len); @@ -1175,6 +1201,7 @@ { (void)USB_HC_ISO_DB_StartXfer(USBx, hc, len); } +#endif /* (USE_USB_DOUBLE_BUFFER == 1U) */ /* Enable host channel */ USB_DRD_SET_CHEP_TX_STATUS(USBx, hc->phy_ch_num, USB_CH_TX_VALID); @@ -1182,6 +1209,8 @@ return HAL_OK; } + +#if (USE_USB_DOUBLE_BUFFER == 1U) /** * @brief Start Transfer of Channel isochronous out double buffer * @param USBx Selected device @@ -1199,57 +1228,16 @@ /* check the DTOG_TX to determine in which buffer we should write */ if ((USB_DRD_GET_CHEP(USBx, phy_ch_num) & USB_CH_DTOG_TX) != 0U) { - if (hc->pmabuff1_state == USB_PMABUF_EMPTY) - { - /* Set the Double buffer counter for pmabuffer1 */ - USB_DRD_SET_CHEP_DBUF1_CNT(USBx, phy_ch_num, hc->ch_dir, len); - hc->pmabuff1_state = USB_PMABUF_NOT_EMPTY; - USB_WritePMA(USBx, hc->xfer_buff, hc->pmaaddr1, (uint16_t)len); - - if (hc->pmabuff0_state == USB_PMABUF_EMPTY) - { - hc->urb_state = URB_DONE; - } - } - else if (hc->pmabuff0_state == USB_PMABUF_EMPTY) - { - /* Set the Double buffer counter for pmabuffer0 */ - USB_DRD_SET_CHEP_DBUF0_CNT(USBx, phy_ch_num, hc->ch_dir, len); - USB_WritePMA(USBx, hc->xfer_buff, hc->pmaaddr0, (uint16_t)len); - hc->pmabuff0_state = USB_PMABUF_NOT_EMPTY; - } - else - { - /* both buffer are not empty, should not be in this condition */ - /*...*/ - } + USB_DRD_SET_CHEP_DBUF0_CNT(USBx, phy_ch_num, 1U, len); + USB_WritePMA(USBx, hc->xfer_buff, hc->pmaaddr0, (uint16_t)len); } else { /* DTOGTX=0 */ - if (hc->pmabuff0_state == USB_PMABUF_EMPTY) - { - /* Set the Double buffer counter for pmabuffer0 */ - USB_DRD_SET_CHEP_DBUF0_CNT(USBx, phy_ch_num, hc->ch_dir, len); - USB_WritePMA(USBx, hc->xfer_buff, hc->pmaaddr0, (uint16_t)len); - hc->pmabuff0_state = USB_PMABUF_NOT_EMPTY; - if (hc->pmabuff1_state == USB_PMABUF_EMPTY) - { - hc->urb_state = URB_DONE; - } + /* Set the Double buffer counter for pmabuffer0 */ + USB_DRD_SET_CHEP_DBUF1_CNT(USBx, phy_ch_num,1U, len); + USB_WritePMA(USBx, hc->xfer_buff, hc->pmaaddr1, (uint16_t)len); } - else if (hc->pmabuff1_state == USB_PMABUF_EMPTY) - { - /* Set the Double buffer counter for pmabuffer1 */ - USB_DRD_SET_CHEP_DBUF1_CNT(USBx, phy_ch_num, hc->ch_dir, len); - hc->pmabuff1_state = USB_PMABUF_NOT_EMPTY; - USB_WritePMA(USBx, hc->xfer_buff, hc->pmaaddr1, (uint16_t)len); - } - else - { - /*...*/ - } - } return HAL_OK; } @@ -1274,7 +1262,7 @@ if (hc->xfer_len_db > hc->max_packet) { /* enable double buffer mode */ - (void)USB_HC_DoubleBuffer(USBx, (uint8_t)phy_ch_num, USB_DRD_DBUFF_ENBALE); + (void)USB_HC_DoubleBuffer(USBx, (uint8_t)phy_ch_num, USB_DRD_BULK_DBUFF_ENBALE); *len = hc->max_packet; hc->xfer_len_db -= *len; @@ -1282,13 +1270,13 @@ if ((ch_reg & USB_CH_DTOG_TX) == 0U) { /* Write Buffer0 */ - USB_DRD_SET_CHEP_TX_CNT(USBx, phy_ch_num, (uint16_t)*len); + USB_DRD_SET_CHEP_DBUF0_CNT(USBx, phy_ch_num, 1U, (uint16_t)*len); USB_WritePMA(USBx, hc->xfer_buff, hc->pmaaddr0, (uint16_t)*len); } else { /* Write Buffer1 */ - USB_DRD_SET_CHEP_DBUF1_CNT(USBx, (uint8_t) phy_ch_num, 0U, (uint16_t)*len); + USB_DRD_SET_CHEP_DBUF1_CNT(USBx, phy_ch_num, 1U, (uint16_t)*len); USB_WritePMA(USBx, hc->xfer_buff, hc->pmaaddr1, (uint16_t)*len); } @@ -1308,27 +1296,28 @@ if ((ch_reg & USB_CH_DTOG_TX) == 0U) { /* Write Buffer1 */ - USB_DRD_SET_CHEP_DBUF1_CNT(USBx, phy_ch_num, 0U, (uint16_t)*len); + USB_DRD_SET_CHEP_DBUF1_CNT(USBx, phy_ch_num, 1U, (uint16_t)*len); USB_WritePMA(USBx, hc->xfer_buff, hc->pmaaddr1, (uint16_t)*len); } else { /* Write Buffer0 */ - USB_DRD_SET_CHEP_TX_CNT(USBx, phy_ch_num, (uint16_t)*len); + USB_DRD_SET_CHEP_DBUF0_CNT(USBx, phy_ch_num, 1U, (uint16_t)*len); USB_WritePMA(USBx, hc->xfer_buff, hc->pmaaddr0, (uint16_t)*len); } } else { - /* Disable double buffer mode */ - (void)USB_HC_DoubleBuffer(USBx, (uint8_t)phy_ch_num, USB_DRD_DBUFF_DISABLE); - + /* Disable bulk double buffer mode */ + (void)USB_HC_DoubleBuffer(USBx, (uint8_t)phy_ch_num, USB_DRD_BULK_DBUFF_DISABLE); USB_WritePMA(USBx, hc->xfer_buff, hc->pmaaddr0, (uint16_t)*len); USB_DRD_SET_CHEP_TX_CNT(USBx, phy_ch_num, (uint16_t)*len); } return HAL_OK; } +#endif /* (USE_USB_DOUBLE_BUFFER == 1U) */ + /** * @brief Halt a host channel in @@ -1344,6 +1333,8 @@ return HAL_OK; } + + /** * @brief Halt a host channel out * @param USBx Selected device