2 ******************************************************************************
3 * @file stm32f3xx_hal_pcd.c
4 * @author MCD Application Team
7 * @brief PCD HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the USB Peripheral Controller:
10 * + Initialization and de-initialization functions
11 * + IO operation functions
12 * + Peripheral Control functions
13 * + Peripheral State functions
16 ==============================================================================
17 ##### How to use this driver #####
18 ==============================================================================
20 The PCD HAL driver can be used as follows:
22 (#) Declare a PCD_HandleTypeDef handle structure, for example:
23 PCD_HandleTypeDef hpcd;
25 (#) Fill parameters of Init structure in HCD handle
27 (#) Call HAL_PCD_Init() API to initialize the HCD peripheral (Core, Device core, ...)
29 (#) Initialize the PCD low level resources through the HAL_PCD_MspInit() API:
30 (##) Enable the PCD/USB Low Level interface clock using
31 (+++) __USB_CLK_ENABLE);
33 (##) Initialize the related GPIO clocks
34 (##) Configure PCD pin-out
35 (##) Configure PCD NVIC interrupt
37 (#)Associate the Upper USB device stack to the HAL PCD Driver:
38 (##) hpcd.pData = pdev;
40 (#)Enable HCD transmission and reception:
44 ******************************************************************************
47 * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
49 * Redistribution and use in source and binary forms, with or without modification,
50 * are permitted provided that the following conditions are met:
51 * 1. Redistributions of source code must retain the above copyright notice,
52 * this list of conditions and the following disclaimer.
53 * 2. Redistributions in binary form must reproduce the above copyright notice,
54 * this list of conditions and the following disclaimer in the documentation
55 * and/or other materials provided with the distribution.
56 * 3. Neither the name of STMicroelectronics nor the names of its contributors
57 * may be used to endorse or promote products derived from this software
58 * without specific prior written permission.
60 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
61 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
63 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
66 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
67 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
68 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
69 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
71 ******************************************************************************
74 /* Includes ------------------------------------------------------------------*/
75 #include "stm32f3xx_hal.h"
77 /** @addtogroup STM32F3xx_HAL_Driver
81 /** @defgroup PCD PCD HAL module driver
82 * @brief PCD HAL module driver
86 #ifdef HAL_PCD_MODULE_ENABLED
88 #if defined(STM32F302xE) || defined(STM32F303xE) || \
89 defined(STM32F302xC) || defined(STM32F303xC) || \
90 defined(STM32F302x8) || \
93 /* Private typedef -----------------------------------------------------------*/
94 /* Private define ------------------------------------------------------------*/
96 /** @defgroup PCD_Private_Define PCD Private Define
99 #define BTABLE_ADDRESS (0x000)
104 /* Private macro -------------------------------------------------------------*/
105 /* Private variables ---------------------------------------------------------*/
106 /* Private function prototypes -----------------------------------------------*/
107 /** @defgroup PCD_Private_Functions PCD Private Functions
110 static HAL_StatusTypeDef PCD_EP_ISR_Handler(PCD_HandleTypeDef *hpcd);
114 /* Exported functions ---------------------------------------------------------*/
116 /** @defgroup PCD_Exported_Functions PCD Exported Functions
120 /** @defgroup PCD_Exported_Functions_Group1 Initialization and de-initialization functions
121 * @brief Initialization and Configuration functions
124 ===============================================================================
125 ##### Initialization and de-initialization functions #####
126 ===============================================================================
127 [..] This section provides functions allowing to:
134 * @brief Initializes the PCD according to the specified
135 * parameters in the PCD_InitTypeDef and create the associated handle.
136 * @param hpcd: PCD handle
139 HAL_StatusTypeDef HAL_PCD_Init(PCD_HandleTypeDef *hpcd)
143 uint32_t wInterrupt_Mask = 0;
145 /* Check the PCD handle allocation */
151 /* Check the parameters */
152 assert_param(IS_PCD_ALL_INSTANCE(hpcd->Instance));
154 hpcd->State = PCD_BUSY;
156 /* Init the low level hardware : GPIO, CLOCK, NVIC... */
157 HAL_PCD_MspInit(hpcd);
159 /* Init endpoints structures */
160 for (i = 0; i < hpcd->Init.dev_endpoints ; i++)
162 /* Init ep structure */
163 hpcd->IN_ep[i].is_in = 1;
164 hpcd->IN_ep[i].num = i;
165 /* Control until ep is actvated */
166 hpcd->IN_ep[i].type = PCD_EP_TYPE_CTRL;
167 hpcd->IN_ep[i].maxpacket = 0;
168 hpcd->IN_ep[i].xfer_buff = 0;
169 hpcd->IN_ep[i].xfer_len = 0;
172 for (i = 0; i < hpcd->Init.dev_endpoints ; i++)
174 hpcd->OUT_ep[i].is_in = 0;
175 hpcd->OUT_ep[i].num = i;
176 /* Control until ep is activated */
177 hpcd->OUT_ep[i].type = PCD_EP_TYPE_CTRL;
178 hpcd->OUT_ep[i].maxpacket = 0;
179 hpcd->OUT_ep[i].xfer_buff = 0;
180 hpcd->OUT_ep[i].xfer_len = 0;
185 hpcd->Instance->CNTR = USB_CNTR_FRES;
188 hpcd->Instance->CNTR = 0;
190 /*Clear pending interrupts*/
191 hpcd->Instance->ISTR = 0;
193 /*Set Btable Adress*/
194 hpcd->Instance->BTABLE = BTABLE_ADDRESS;
196 /*set wInterrupt_Mask global variable*/
197 wInterrupt_Mask = USB_CNTR_CTRM | USB_CNTR_WKUPM | USB_CNTR_SUSPM | USB_CNTR_ERRM \
198 | USB_CNTR_ESOFM | USB_CNTR_RESETM;
200 /*Set interrupt mask*/
201 hpcd->Instance->CNTR = wInterrupt_Mask;
203 hpcd->USB_Address = 0;
204 hpcd->State= PCD_READY;
210 * @brief DeInitializes the PCD peripheral
211 * @param hpcd: PCD handle
214 HAL_StatusTypeDef HAL_PCD_DeInit(PCD_HandleTypeDef *hpcd)
216 /* Check the PCD handle allocation */
222 hpcd->State = PCD_BUSY;
227 /* DeInit the low level hardware */
228 HAL_PCD_MspDeInit(hpcd);
230 hpcd->State = PCD_READY;
236 * @brief Initializes the PCD MSP.
237 * @param hpcd: PCD handle
240 __weak void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)
242 /* NOTE : This function Should not be modified, when the callback is needed,
243 the HAL_PCD_MspInit could be implenetd in the user file
248 * @brief DeInitializes PCD MSP.
249 * @param hpcd: PCD handle
252 __weak void HAL_PCD_MspDeInit(PCD_HandleTypeDef *hpcd)
254 /* NOTE : This function Should not be modified, when the callback is needed,
255 the HAL_PCD_MspDeInit could be implenetd in the user file
263 /** @defgroup PCD_Exported_Functions_Group2 Input and Output operation functions
264 * @brief Data transfers functions
267 ===============================================================================
268 ##### IO operation functions #####
269 ===============================================================================
271 This subsection provides a set of functions allowing to manage the PCD data
279 * @brief Start The USB OTG Device.
280 * @param hpcd: PCD handle
283 HAL_StatusTypeDef HAL_PCD_Start(PCD_HandleTypeDef *hpcd)
285 /* DP Pull-Down is external */
286 HAL_PCDEx_SetConnectionState (hpcd, 1);
292 * @brief Stop The USB OTG Device.
293 * @param hpcd: PCD handle
296 HAL_StatusTypeDef HAL_PCD_Stop(PCD_HandleTypeDef *hpcd)
300 /* disable all interrupts and force USB reset */
301 hpcd->Instance->CNTR = USB_CNTR_FRES;
303 /* clear interrupt status register */
304 hpcd->Instance->ISTR = 0;
306 /* switch-off device */
307 hpcd->Instance->CNTR = (USB_CNTR_FRES | USB_CNTR_PDWN);
320 /** @addtogroup PCD_Private_Functions PCD Private Functions
324 * @brief This function handles PCD Endpoint interrupt request.
325 * @param hpcd: PCD handle
328 static HAL_StatusTypeDef PCD_EP_ISR_Handler(PCD_HandleTypeDef *hpcd)
334 __IO uint16_t wEPVal = 0;
336 /* stay in loop while pending interrupts */
337 while (((wIstr = hpcd->Instance->ISTR) & USB_ISTR_CTR) != 0)
339 /* extract highest priority endpoint number */
340 EPindex = (uint8_t)(wIstr & USB_ISTR_EP_ID);
344 /* Decode and service control endpoint interrupt */
346 /* DIR bit = origin of the interrupt */
347 if ((wIstr & USB_ISTR_DIR) == 0)
351 /* DIR = 0 => IN int */
352 /* DIR = 0 implies that (EP_CTR_TX = 1) always */
353 PCD_CLEAR_TX_EP_CTR(hpcd->Instance, PCD_ENDP0);
354 ep = &hpcd->IN_ep[0];
356 ep->xfer_count = PCD_GET_EP_TX_CNT(hpcd->Instance, ep->num);
357 ep->xfer_buff += ep->xfer_count;
360 HAL_PCD_DataInStageCallback(hpcd, 0);
363 if((hpcd->USB_Address > 0)&& ( ep->xfer_len == 0))
365 hpcd->Instance->DADDR = (hpcd->USB_Address | USB_DADDR_EF);
366 hpcd->USB_Address = 0;
374 /* DIR = 1 & CTR_RX => SETUP or OUT int */
375 /* DIR = 1 & (CTR_TX | CTR_RX) => 2 int pending */
376 ep = &hpcd->OUT_ep[0];
377 wEPVal = PCD_GET_ENDPOINT(hpcd->Instance, PCD_ENDP0);
379 if ((wEPVal & USB_EP_SETUP) != 0)
381 /* Get SETUP Packet*/
382 ep->xfer_count = PCD_GET_EP_RX_CNT(hpcd->Instance, ep->num);
383 PCD_ReadPMA(hpcd->Instance, (uint8_t*)hpcd->Setup ,ep->pmaadress , ep->xfer_count);
384 /* SETUP bit kept frozen while CTR_RX = 1*/
385 PCD_CLEAR_RX_EP_CTR(hpcd->Instance, PCD_ENDP0);
387 /* Process SETUP Packet*/
388 HAL_PCD_SetupStageCallback(hpcd);
391 else if ((wEPVal & USB_EP_CTR_RX) != 0)
393 PCD_CLEAR_RX_EP_CTR(hpcd->Instance, PCD_ENDP0);
394 /* Get Control Data OUT Packet*/
395 ep->xfer_count = PCD_GET_EP_RX_CNT(hpcd->Instance, ep->num);
397 if (ep->xfer_count != 0)
399 PCD_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaadress, ep->xfer_count);
400 ep->xfer_buff+=ep->xfer_count;
403 /* Process Control Data OUT Packet*/
404 HAL_PCD_DataOutStageCallback(hpcd, 0);
406 PCD_SET_EP_RX_CNT(hpcd->Instance, PCD_ENDP0, ep->maxpacket);
407 PCD_SET_EP_RX_STATUS(hpcd->Instance, PCD_ENDP0, USB_EP_RX_VALID);
414 /* Decode and service non control endpoints interrupt */
416 /* process related endpoint register */
417 wEPVal = PCD_GET_ENDPOINT(hpcd->Instance, EPindex);
418 if ((wEPVal & USB_EP_CTR_RX) != 0)
421 PCD_CLEAR_RX_EP_CTR(hpcd->Instance, EPindex);
422 ep = &hpcd->OUT_ep[EPindex];
424 /* OUT double Buffering*/
425 if (ep->doublebuffer == 0)
427 count = PCD_GET_EP_RX_CNT(hpcd->Instance, ep->num);
430 PCD_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaadress, count);
435 if (PCD_GET_ENDPOINT(hpcd->Instance, ep->num) & USB_EP_DTOG_RX)
437 /*read from endpoint BUF0Addr buffer*/
438 count = PCD_GET_EP_DBUF0_CNT(hpcd->Instance, ep->num);
441 PCD_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr0, count);
446 /*read from endpoint BUF1Addr buffer*/
447 count = PCD_GET_EP_DBUF1_CNT(hpcd->Instance, ep->num);
450 PCD_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr1, count);
453 PCD_FreeUserBuffer(hpcd->Instance, ep->num, PCD_EP_DBUF_OUT);
455 /*multi-packet on the NON control OUT endpoint*/
456 ep->xfer_count+=count;
457 ep->xfer_buff+=count;
459 if ((ep->xfer_len == 0) || (count < ep->maxpacket))
462 HAL_PCD_DataOutStageCallback(hpcd, ep->num);
466 HAL_PCD_EP_Receive(hpcd, ep->num, ep->xfer_buff, ep->xfer_len);
469 } /* if((wEPVal & EP_CTR_RX) */
471 if ((wEPVal & USB_EP_CTR_TX) != 0)
473 ep = &hpcd->IN_ep[EPindex];
476 PCD_CLEAR_TX_EP_CTR(hpcd->Instance, EPindex);
478 /* IN double Buffering*/
479 if (ep->doublebuffer == 0)
481 ep->xfer_count = PCD_GET_EP_TX_CNT(hpcd->Instance, ep->num);
482 if (ep->xfer_count != 0)
484 PCD_WritePMA(hpcd->Instance, ep->xfer_buff, ep->pmaadress, ep->xfer_count);
489 if (PCD_GET_ENDPOINT(hpcd->Instance, ep->num) & USB_EP_DTOG_TX)
491 /*read from endpoint BUF0Addr buffer*/
492 ep->xfer_count = PCD_GET_EP_DBUF0_CNT(hpcd->Instance, ep->num);
493 if (ep->xfer_count != 0)
495 PCD_WritePMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr0, ep->xfer_count);
500 /*read from endpoint BUF1Addr buffer*/
501 ep->xfer_count = PCD_GET_EP_DBUF1_CNT(hpcd->Instance, ep->num);
502 if (ep->xfer_count != 0)
504 PCD_WritePMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr1, ep->xfer_count);
507 PCD_FreeUserBuffer(hpcd->Instance, ep->num, PCD_EP_DBUF_IN);
509 /*multi-packet on the NON control IN endpoint*/
510 ep->xfer_count = PCD_GET_EP_TX_CNT(hpcd->Instance, ep->num);
511 ep->xfer_buff+=ep->xfer_count;
513 /* Zero Length Packet? */
514 if (ep->xfer_len == 0)
517 HAL_PCD_DataInStageCallback(hpcd, ep->num);
521 HAL_PCD_EP_Transmit(hpcd, ep->num, ep->xfer_buff, ep->xfer_len);
532 /** @addtogroup PCD_Exported_Functions
536 /** @defgroup PCD_Exported_Functions_Group2 Input and Output operation functions
541 * @brief This function handles PCD interrupt request.
542 * @param hpcd: PCD handle
545 void HAL_PCD_IRQHandler(PCD_HandleTypeDef *hpcd)
547 uint32_t wInterrupt_Mask = 0;
549 if (__HAL_PCD_GET_FLAG (hpcd, USB_ISTR_CTR))
551 /* servicing of the endpoint correct transfer interrupt */
552 /* clear of the CTR flag into the sub */
553 PCD_EP_ISR_Handler(hpcd);
556 if (__HAL_PCD_GET_FLAG (hpcd, USB_ISTR_RESET))
558 __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_RESET);
559 HAL_PCD_ResetCallback(hpcd);
560 HAL_PCD_SetAddress(hpcd, 0);
563 if (__HAL_PCD_GET_FLAG (hpcd, USB_ISTR_PMAOVRM))
565 __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_PMAOVRM);
567 if (__HAL_PCD_GET_FLAG (hpcd, USB_ISTR_ERR))
569 __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_ERR);
572 if (__HAL_PCD_GET_FLAG (hpcd, USB_ISTR_WKUP))
574 hpcd->Instance->CNTR &= ~(USB_CNTR_LP_MODE);
576 /*set wInterrupt_Mask global variable*/
577 wInterrupt_Mask = USB_CNTR_CTRM | USB_CNTR_WKUPM | USB_CNTR_SUSPM | USB_CNTR_ERRM \
578 | USB_CNTR_ESOFM | USB_CNTR_RESETM;
580 /*Set interrupt mask*/
581 hpcd->Instance->CNTR = wInterrupt_Mask;
583 HAL_PCD_ResumeCallback(hpcd);
585 __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_WKUP);
588 if (__HAL_PCD_GET_FLAG (hpcd, USB_ISTR_SUSP))
590 /* clear of the ISTR bit must be done after setting of CNTR_FSUSP */
591 __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_SUSP);
593 /* Force low-power mode in the macrocell */
594 hpcd->Instance->CNTR |= USB_CNTR_FSUSP;
595 hpcd->Instance->CNTR |= USB_CNTR_LP_MODE;
596 if (__HAL_PCD_GET_FLAG (hpcd, USB_ISTR_WKUP) == 0)
598 HAL_PCD_SuspendCallback(hpcd);
602 if (__HAL_PCD_GET_FLAG (hpcd, USB_ISTR_SOF))
604 __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_SOF);
605 HAL_PCD_SOFCallback(hpcd);
608 if (__HAL_PCD_GET_FLAG (hpcd, USB_ISTR_ESOF))
610 /* clear ESOF flag in ISTR */
611 __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_ESOF);
616 * @brief Data out stage callbacks
617 * @param hpcd: PCD handle
618 * @param epnum: endpoint number
621 __weak void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
623 /* NOTE : This function Should not be modified, when the callback is needed,
624 the HAL_PCD_DataOutStageCallback could be implemented in the user file
629 * @brief Data IN stage callbacks
630 * @param hpcd: PCD handle
631 * @param epnum: endpoint number
634 __weak void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
636 /* NOTE : This function Should not be modified, when the callback is needed,
637 the HAL_PCD_DataInStageCallback could be implemented in the user file
641 * @brief Setup stage callback
642 * @param hpcd: ppp handle
645 __weak void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd)
647 /* NOTE : This function Should not be modified, when the callback is needed,
648 the HAL_PCD_SetupStageCallback could be implemented in the user file
653 * @brief USB Start Of Frame callbacks
654 * @param hpcd: PCD handle
657 __weak void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
659 /* NOTE : This function Should not be modified, when the callback is needed,
660 the HAL_PCD_SOFCallback could be implemented in the user file
665 * @brief USB Reset callbacks
666 * @param hpcd: PCD handle
669 __weak void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd)
671 /* NOTE : This function Should not be modified, when the callback is needed,
672 the HAL_PCD_ResetCallback could be implemented in the user file
677 * @brief Suspend event callbacks
678 * @param hpcd: PCD handle
681 __weak void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
683 /* NOTE : This function Should not be modified, when the callback is needed,
684 the HAL_PCD_SuspendCallback could be implemented in the user file
689 * @brief Resume event callbacks
690 * @param hpcd: PCD handle
693 __weak void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd)
695 /* NOTE : This function Should not be modified, when the callback is needed,
696 the HAL_PCD_ResumeCallback could be implemented in the user file
701 * @brief Incomplete ISO OUT callbacks
702 * @param hpcd: PCD handle
703 * @param epnum: endpoint number
706 __weak void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
708 /* NOTE : This function Should not be modified, when the callback is needed,
709 the HAL_PCD_ISOOUTIncompleteCallback could be implemented in the user file
714 * @brief Incomplete ISO IN callbacks
715 * @param hpcd: PCD handle
716 * @param epnum: endpoint number
719 __weak void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
721 /* NOTE : This function Should not be modified, when the callback is needed,
722 the HAL_PCD_ISOINIncompleteCallback could be implemented in the user file
727 * @brief Connection event callbacks
728 * @param hpcd: PCD handle
731 __weak void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)
733 /* NOTE : This function Should not be modified, when the callback is needed,
734 the HAL_PCD_ConnectCallback could be implemented in the user file
739 * @brief Disconnection event callbacks
740 * @param hpcd: ppp handle
743 __weak void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
745 /* NOTE : This function Should not be modified, when the callback is needed,
746 the HAL_PCD_DisconnectCallback could be implemented in the user file
753 /** @defgroup PCD_Exported_Functions_Group3 Peripheral Control functions
754 * @brief management functions
757 ===============================================================================
758 ##### Peripheral Control functions #####
759 ===============================================================================
761 This subsection provides a set of functions allowing to control the PCD data
769 * @brief Connect the USB device
770 * @param hpcd: PCD handle
773 HAL_StatusTypeDef HAL_PCD_DevConnect(PCD_HandleTypeDef *hpcd)
777 /* Enabling DP Pull-Down bit to Connect internal pull-up on USB DP line */
778 HAL_PCDEx_SetConnectionState(hpcd, 1);
785 * @brief Disconnect the USB device
786 * @param hpcd: PCD handle
789 HAL_StatusTypeDef HAL_PCD_DevDisconnect(PCD_HandleTypeDef *hpcd)
793 /* Disable DP Pull-Down bit*/
794 HAL_PCDEx_SetConnectionState(hpcd, 0);
801 * @brief Set the USB Device address
802 * @param hpcd: PCD handle
803 * @param address: new device address
806 HAL_StatusTypeDef HAL_PCD_SetAddress(PCD_HandleTypeDef *hpcd, uint8_t address)
812 /* set device address and enable function */
813 hpcd->Instance->DADDR = USB_DADDR_EF;
815 else /* USB Address will be applied later */
817 hpcd->USB_Address = address;
824 * @brief Open and configure an endpoint
825 * @param hpcd: PCD handle
826 * @param ep_addr: endpoint address
827 * @param ep_mps: endpoint max packert size
828 * @param ep_type: endpoint type
831 HAL_StatusTypeDef HAL_PCD_EP_Open(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint16_t ep_mps, uint8_t ep_type)
833 HAL_StatusTypeDef ret = HAL_OK;
836 if ((ep_addr & 0x80) == 0x80)
838 ep = &hpcd->IN_ep[ep_addr & 0x7F];
842 ep = &hpcd->OUT_ep[ep_addr & 0x7F];
844 ep->num = ep_addr & 0x7F;
846 ep->is_in = (0x80 & ep_addr) != 0;
847 ep->maxpacket = ep_mps;
852 /* initialize Endpoint */
855 case PCD_EP_TYPE_CTRL:
856 PCD_SET_EPTYPE(hpcd->Instance, ep->num, USB_EP_CONTROL);
858 case PCD_EP_TYPE_BULK:
859 PCD_SET_EPTYPE(hpcd->Instance, ep->num, USB_EP_BULK);
861 case PCD_EP_TYPE_INTR:
862 PCD_SET_EPTYPE(hpcd->Instance, ep->num, USB_EP_INTERRUPT);
864 case PCD_EP_TYPE_ISOC:
865 PCD_SET_EPTYPE(hpcd->Instance, ep->num, USB_EP_ISOCHRONOUS);
869 PCD_SET_EP_ADDRESS(hpcd->Instance, ep->num, ep->num);
871 if (ep->doublebuffer == 0)
875 /*Set the endpoint Transmit buffer address */
876 PCD_SET_EP_TX_ADDRESS(hpcd->Instance, ep->num, ep->pmaadress);
877 PCD_CLEAR_TX_DTOG(hpcd->Instance, ep->num);
878 /* Configure NAK status for the Endpoint*/
879 PCD_SET_EP_TX_STATUS(hpcd->Instance, ep->num, USB_EP_TX_NAK);
883 /*Set the endpoint Receive buffer address */
884 PCD_SET_EP_RX_ADDRESS(hpcd->Instance, ep->num, ep->pmaadress);
885 /*Set the endpoint Receive buffer counter*/
886 PCD_SET_EP_RX_CNT(hpcd->Instance, ep->num, ep->maxpacket);
887 PCD_CLEAR_RX_DTOG(hpcd->Instance, ep->num);
888 /* Configure VALID status for the Endpoint*/
889 PCD_SET_EP_RX_STATUS(hpcd->Instance, ep->num, USB_EP_RX_VALID);
895 /*Set the endpoint as double buffered*/
896 PCD_SET_EP_DBUF(hpcd->Instance, ep->num);
897 /*Set buffer address for double buffered mode*/
898 PCD_SET_EP_DBUF_ADDR(hpcd->Instance, ep->num,ep->pmaaddr0, ep->pmaaddr1);
902 /* Clear the data toggle bits for the endpoint IN/OUT*/
903 PCD_CLEAR_RX_DTOG(hpcd->Instance, ep->num);
904 PCD_CLEAR_TX_DTOG(hpcd->Instance, ep->num);
906 /* Reset value of the data toggle bits for the endpoint out*/
907 PCD_TX_DTOG(hpcd->Instance, ep->num);
909 PCD_SET_EP_RX_STATUS(hpcd->Instance, ep->num, USB_EP_RX_VALID);
910 PCD_SET_EP_TX_STATUS(hpcd->Instance, ep->num, USB_EP_TX_DIS);
914 /* Clear the data toggle bits for the endpoint IN/OUT*/
915 PCD_CLEAR_RX_DTOG(hpcd->Instance, ep->num);
916 PCD_CLEAR_TX_DTOG(hpcd->Instance, ep->num);
917 PCD_RX_DTOG(hpcd->Instance, ep->num);
918 /* Configure DISABLE status for the Endpoint*/
919 PCD_SET_EP_TX_STATUS(hpcd->Instance, ep->num, USB_EP_TX_DIS);
920 PCD_SET_EP_RX_STATUS(hpcd->Instance, ep->num, USB_EP_RX_DIS);
930 * @brief Deactivate an endpoint
931 * @param hpcd: PCD handle
932 * @param ep_addr: endpoint address
935 HAL_StatusTypeDef HAL_PCD_EP_Close(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
939 if ((ep_addr & 0x80) == 0x80)
941 ep = &hpcd->IN_ep[ep_addr & 0x7F];
945 ep = &hpcd->OUT_ep[ep_addr & 0x7F];
947 ep->num = ep_addr & 0x7F;
949 ep->is_in = (0x80 & ep_addr) != 0;
953 if (ep->doublebuffer == 0)
957 PCD_CLEAR_TX_DTOG(hpcd->Instance, ep->num);
958 /* Configure DISABLE status for the Endpoint*/
959 PCD_SET_EP_TX_STATUS(hpcd->Instance, ep->num, USB_EP_TX_DIS);
963 PCD_CLEAR_RX_DTOG(hpcd->Instance, ep->num);
964 /* Configure DISABLE status for the Endpoint*/
965 PCD_SET_EP_RX_STATUS(hpcd->Instance, ep->num, USB_EP_RX_DIS);
973 /* Clear the data toggle bits for the endpoint IN/OUT*/
974 PCD_CLEAR_RX_DTOG(hpcd->Instance, ep->num);
975 PCD_CLEAR_TX_DTOG(hpcd->Instance, ep->num);
977 /* Reset value of the data toggle bits for the endpoint out*/
978 PCD_TX_DTOG(hpcd->Instance, ep->num);
980 PCD_SET_EP_RX_STATUS(hpcd->Instance, ep->num, USB_EP_RX_DIS);
981 PCD_SET_EP_TX_STATUS(hpcd->Instance, ep->num, USB_EP_TX_DIS);
985 /* Clear the data toggle bits for the endpoint IN/OUT*/
986 PCD_CLEAR_RX_DTOG(hpcd->Instance, ep->num);
987 PCD_CLEAR_TX_DTOG(hpcd->Instance, ep->num);
988 PCD_RX_DTOG(hpcd->Instance, ep->num);
989 /* Configure DISABLE status for the Endpoint*/
990 PCD_SET_EP_TX_STATUS(hpcd->Instance, ep->num, USB_EP_TX_DIS);
991 PCD_SET_EP_RX_STATUS(hpcd->Instance, ep->num, USB_EP_RX_DIS);
1001 * @brief Receive an amount of data
1002 * @param hpcd: PCD handle
1003 * @param ep_addr: endpoint address
1004 * @param pBuf: pointer to the reception buffer
1005 * @param len: amount of data to be received
1006 * @retval HAL status
1008 HAL_StatusTypeDef HAL_PCD_EP_Receive(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len)
1013 ep = &hpcd->OUT_ep[ep_addr & 0x7F];
1015 /*setup and start the Xfer */
1016 ep->xfer_buff = pBuf;
1020 ep->num = ep_addr & 0x7F;
1024 /* Multi packet transfer*/
1025 if (ep->xfer_len > ep->maxpacket)
1036 /* configure and validate Rx endpoint */
1037 if (ep->doublebuffer == 0)
1039 /*Set RX buffer count*/
1040 PCD_SET_EP_RX_CNT(hpcd->Instance, ep->num, len);
1044 /*Set the Double buffer counter*/
1045 PCD_SET_EP_DBUF1_CNT(hpcd->Instance, ep->num, ep->is_in, len);
1048 PCD_SET_EP_RX_STATUS(hpcd->Instance, ep->num, USB_EP_RX_VALID);
1056 * @brief Get Received Data Size
1057 * @param hpcd: PCD handle
1058 * @param ep_addr: endpoint address
1061 uint16_t HAL_PCD_EP_GetRxCount(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
1063 return hpcd->OUT_ep[ep_addr & 0x7F].xfer_count;
1066 * @brief Send an amount of data
1067 * @param hpcd: PCD handle
1068 * @param ep_addr: endpoint address
1069 * @param pBuf: pointer to the transmission buffer
1070 * @param len: amount of data to be sent
1071 * @retval HAL status
1073 HAL_StatusTypeDef HAL_PCD_EP_Transmit(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len)
1076 uint16_t pmabuffer = 0;
1078 ep = &hpcd->IN_ep[ep_addr & 0x7F];
1080 /*setup and start the Xfer */
1081 ep->xfer_buff = pBuf;
1085 ep->num = ep_addr & 0x7F;
1089 /*Multi packet transfer*/
1090 if (ep->xfer_len > ep->maxpacket)
1101 /* configure and validate Tx endpoint */
1102 if (ep->doublebuffer == 0)
1104 PCD_WritePMA(hpcd->Instance, ep->xfer_buff, ep->pmaadress, len);
1105 PCD_SET_EP_TX_CNT(hpcd->Instance, ep->num, len);
1109 /*Set the Double buffer counter*/
1110 PCD_SET_EP_DBUF1_CNT(hpcd->Instance, ep->num, ep->is_in, len);
1112 /*Write the data to the USB endpoint*/
1113 if (PCD_GET_ENDPOINT(hpcd->Instance, ep->num)& USB_EP_DTOG_TX)
1115 pmabuffer = ep->pmaaddr1;
1119 pmabuffer = ep->pmaaddr0;
1121 PCD_WritePMA(hpcd->Instance, ep->xfer_buff, pmabuffer, len);
1122 PCD_FreeUserBuffer(hpcd->Instance, ep->num, ep->is_in);
1125 PCD_SET_EP_TX_STATUS(hpcd->Instance, ep->num, USB_EP_TX_VALID);
1133 * @brief Set a STALL condition over an endpoint
1134 * @param hpcd: PCD handle
1135 * @param ep_addr: endpoint address
1136 * @retval HAL status
1138 HAL_StatusTypeDef HAL_PCD_EP_SetStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
1144 if ((0x80 & ep_addr) == 0x80)
1146 ep = &hpcd->IN_ep[ep_addr & 0x7F];
1150 ep = &hpcd->OUT_ep[ep_addr];
1154 ep->num = ep_addr & 0x7F;
1155 ep->is_in = ((ep_addr & 0x80) == 0x80);
1159 /* This macro sets STALL status for RX & TX*/
1160 PCD_SET_EP_TXRX_STATUS(hpcd->Instance, ep->num, USB_EP_RX_STALL, USB_EP_TX_STALL);
1166 PCD_SET_EP_TX_STATUS(hpcd->Instance, ep->num , USB_EP_TX_STALL);
1170 PCD_SET_EP_RX_STATUS(hpcd->Instance, ep->num , USB_EP_RX_STALL);
1179 * @brief Clear a STALL condition over in an endpoint
1180 * @param hpcd: PCD handle
1181 * @param ep_addr: endpoint address
1182 * @retval HAL status
1184 HAL_StatusTypeDef HAL_PCD_EP_ClrStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
1188 if ((0x80 & ep_addr) == 0x80)
1190 ep = &hpcd->IN_ep[ep_addr & 0x7F];
1194 ep = &hpcd->OUT_ep[ep_addr];
1198 ep->num = ep_addr & 0x7F;
1199 ep->is_in = ((ep_addr & 0x80) == 0x80);
1205 PCD_CLEAR_TX_DTOG(hpcd->Instance, ep->num);
1206 PCD_SET_EP_TX_STATUS(hpcd->Instance, ep->num, USB_EP_TX_VALID);
1210 PCD_CLEAR_RX_DTOG(hpcd->Instance, ep->num);
1211 PCD_SET_EP_RX_STATUS(hpcd->Instance, ep->num, USB_EP_RX_VALID);
1219 * @brief Flush an endpoint
1220 * @param hpcd: PCD handle
1221 * @param ep_addr: endpoint address
1222 * @retval HAL status
1224 HAL_StatusTypeDef HAL_PCD_EP_Flush(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
1230 * @brief HAL_PCD_ActiveRemoteWakeup : active remote wakeup signalling
1231 * @param hpcd: PCD handle
1234 HAL_StatusTypeDef HAL_PCD_ActiveRemoteWakeup(PCD_HandleTypeDef *hpcd)
1236 hpcd->Instance->CNTR |= USB_CNTR_RESUME;
1241 * @brief HAL_PCD_DeActiveRemoteWakeup : de-active remote wakeup signalling
1242 * @param hpcd: PCD handle
1245 HAL_StatusTypeDef HAL_PCD_DeActiveRemoteWakeup(PCD_HandleTypeDef *hpcd)
1247 hpcd->Instance->CNTR &= ~(USB_CNTR_RESUME);
1255 /** @defgroup PCD_Exported_Functions_Group4 Peripheral State functions
1256 * @brief Peripheral State functions
1259 ===============================================================================
1260 ##### Peripheral State functions #####
1261 ===============================================================================
1263 This subsection permit to get in run-time the status of the peripheral
1271 * @brief Return the PCD state
1272 * @param hpcd : PCD handle
1275 PCD_StateTypeDef HAL_PCD_GetState(PCD_HandleTypeDef *hpcd)
1288 #endif /* STM32F302xE || STM32F303xE || */
1289 /* STM32F302xC || STM32F303xC || */
1290 /* STM32F302x8 || */
1293 #endif /* HAL_PCD_MODULE_ENABLED */
1302 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/