]>
git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F0/stm32f0xx_hal_pcd.c
2 ******************************************************************************
3 * @file stm32f0xx_hal_pcd.c
4 * @author MCD Application Team
6 * @date 11-December-2014
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 "stm32f0xx_hal.h"
77 #ifdef HAL_PCD_MODULE_ENABLED
79 #if defined(STM32F042x6) || defined(STM32F048xx) || defined(STM32F072xB) || defined(STM32F078xx) || defined(STM32F070xB)|| defined(STM32F070x6)
81 /** @addtogroup STM32F0xx_HAL_Driver
85 /** @defgroup PCD PCD HAL module driver
86 * @brief PCD HAL module driver
90 /* Private typedef -----------------------------------------------------------*/
91 /* Private define ------------------------------------------------------------*/
93 /** @defgroup PCD_Private_Define PCD Private Define
96 #define BTABLE_ADDRESS (0x000)
101 /* Private macro -------------------------------------------------------------*/
102 /* Private variables ---------------------------------------------------------*/
103 /* Private function prototypes -----------------------------------------------*/
104 /** @defgroup PCD_Private_Functions PCD Private Functions
107 static HAL_StatusTypeDef
PCD_EP_ISR_Handler(PCD_HandleTypeDef
*hpcd
);
108 void PCD_WritePMA(USB_TypeDef
*USBx
, uint8_t *pbUsrBuf
, uint16_t wPMABufAddr
, uint16_t wNBytes
);
109 void PCD_ReadPMA(USB_TypeDef
*USBx
, uint8_t *pbUsrBuf
, uint16_t wPMABufAddr
, uint16_t wNBytes
);
113 /* Exported functions ---------------------------------------------------------*/
115 /** @defgroup PCD_Exported_Functions PCD Exported Functions
119 /** @defgroup PCD_Exported_Functions_Group1 Initialization and de-initialization functions
120 * @brief Initialization and Configuration functions
123 ===============================================================================
124 ##### Initialization and de-initialization functions #####
125 ===============================================================================
126 [..] This section provides functions allowing to:
133 * @brief Initializes the PCD according to the specified
134 * parameters in the PCD_InitTypeDef and create the associated handle.
135 * @param hpcd: PCD handle
138 HAL_StatusTypeDef
HAL_PCD_Init(PCD_HandleTypeDef
*hpcd
)
142 uint32_t wInterrupt_Mask
= 0;
144 /* Check the PCD handle allocation */
150 /* Check the parameters */
151 assert_param(IS_PCD_ALL_INSTANCE(hpcd
->Instance
));
153 hpcd
->State
= PCD_BUSY
;
155 /* Init the low level hardware : GPIO, CLOCK, NVIC... */
156 HAL_PCD_MspInit(hpcd
);
158 /* Init endpoints structures */
159 for (i
= 0; i
< hpcd
->Init
.dev_endpoints
; i
++)
161 /* Init ep structure */
162 hpcd
->IN_ep
[i
].is_in
= 1;
163 hpcd
->IN_ep
[i
].num
= i
;
164 /* Control until ep is actvated */
165 hpcd
->IN_ep
[i
].type
= PCD_EP_TYPE_CTRL
;
166 hpcd
->IN_ep
[i
].maxpacket
= 0;
167 hpcd
->IN_ep
[i
].xfer_buff
= 0;
168 hpcd
->IN_ep
[i
].xfer_len
= 0;
171 for (i
= 0; i
< hpcd
->Init
.dev_endpoints
; i
++)
173 hpcd
->OUT_ep
[i
].is_in
= 0;
174 hpcd
->OUT_ep
[i
].num
= i
;
175 /* Control until ep is activated */
176 hpcd
->OUT_ep
[i
].type
= PCD_EP_TYPE_CTRL
;
177 hpcd
->OUT_ep
[i
].maxpacket
= 0;
178 hpcd
->OUT_ep
[i
].xfer_buff
= 0;
179 hpcd
->OUT_ep
[i
].xfer_len
= 0;
184 hpcd
->Instance
->CNTR
= USB_CNTR_FRES
;
187 hpcd
->Instance
->CNTR
= 0;
189 /*Clear pending interrupts*/
190 hpcd
->Instance
->ISTR
= 0;
192 /*Set Btable Adress*/
193 hpcd
->Instance
->BTABLE
= BTABLE_ADDRESS
;
195 /*set wInterrupt_Mask global variable*/
196 wInterrupt_Mask
= USB_CNTR_CTRM
| USB_CNTR_WKUPM
| USB_CNTR_SUSPM
| USB_CNTR_ERRM \
197 | USB_CNTR_ESOFM
| USB_CNTR_RESETM
;
199 /*Set interrupt mask*/
200 hpcd
->Instance
->CNTR
= wInterrupt_Mask
;
202 hpcd
->USB_Address
= 0;
203 hpcd
->State
= PCD_READY
;
209 * @brief DeInitializes the PCD peripheral
210 * @param hpcd: PCD handle
213 HAL_StatusTypeDef
HAL_PCD_DeInit(PCD_HandleTypeDef
*hpcd
)
215 /* Check the PCD handle allocation */
221 hpcd
->State
= PCD_BUSY
;
226 /* DeInit the low level hardware */
227 HAL_PCD_MspDeInit(hpcd
);
229 hpcd
->State
= PCD_READY
;
235 * @brief Initializes the PCD MSP.
236 * @param hpcd: PCD handle
239 __weak
void HAL_PCD_MspInit(PCD_HandleTypeDef
*hpcd
)
241 /* NOTE : This function Should not be modified, when the callback is needed,
242 the HAL_PCD_MspInit could be implemented in the user file
247 * @brief DeInitializes PCD MSP.
248 * @param hpcd: PCD handle
251 __weak
void HAL_PCD_MspDeInit(PCD_HandleTypeDef
*hpcd
)
253 /* NOTE : This function Should not be modified, when the callback is needed,
254 the HAL_PCD_MspDeInit could be implemented in the user file
262 /** @defgroup PCD_Exported_Functions_Group2 IO operation functions
263 * @brief Data transfers functions
266 ===============================================================================
267 ##### IO operation functions #####
268 ===============================================================================
270 This subsection provides a set of functions allowing to manage the PCD data
278 * @brief Start The USB OTG Device.
279 * @param hpcd: PCD handle
282 HAL_StatusTypeDef
HAL_PCD_Start(PCD_HandleTypeDef
*hpcd
)
284 /* Enabling DP Pull-Down bit to Connect internal pull-up on USB DP line */
285 hpcd
->Instance
->BCDR
|= USB_BCDR_DPPU
;
291 * @brief Stop The USB OTG Device.
292 * @param hpcd: PCD handle
295 HAL_StatusTypeDef
HAL_PCD_Stop(PCD_HandleTypeDef
*hpcd
)
299 /* disable all interrupts and force USB reset */
300 hpcd
->Instance
->CNTR
= USB_CNTR_FRES
;
302 /* clear interrupt status register */
303 hpcd
->Instance
->ISTR
= 0;
305 /* switch-off device */
306 hpcd
->Instance
->CNTR
= (USB_CNTR_FRES
| USB_CNTR_PDWN
);
319 /** @addtogroup PCD_Private_Functions PCD Private Functions
323 * @brief This function handles PCD Endpoint interrupt request.
324 * @param hpcd: PCD handle
327 static HAL_StatusTypeDef
PCD_EP_ISR_Handler(PCD_HandleTypeDef
*hpcd
)
333 __IO
uint16_t wEPVal
= 0;
335 /* stay in loop while pending interrupts */
336 while (((wIstr
= hpcd
->Instance
->ISTR
) & USB_ISTR_CTR
) != 0)
338 /* extract highest priority endpoint number */
339 EPindex
= (uint8_t)(wIstr
& USB_ISTR_EP_ID
);
343 /* Decode and service control endpoint interrupt */
345 /* DIR bit = origin of the interrupt */
346 if ((wIstr
& USB_ISTR_DIR
) == 0)
350 /* DIR = 0 => IN int */
351 /* DIR = 0 implies that (EP_CTR_TX = 1) always */
352 PCD_CLEAR_TX_EP_CTR(hpcd
->Instance
, PCD_ENDP0
);
353 ep
= &hpcd
->IN_ep
[0];
355 ep
->xfer_count
= PCD_GET_EP_TX_CNT(hpcd
->Instance
, ep
->num
);
356 ep
->xfer_buff
+= ep
->xfer_count
;
359 HAL_PCD_DataInStageCallback(hpcd
, 0);
362 if((hpcd
->USB_Address
> 0)&& ( ep
->xfer_len
== 0))
364 hpcd
->Instance
->DADDR
= (hpcd
->USB_Address
| USB_DADDR_EF
);
365 hpcd
->USB_Address
= 0;
373 /* DIR = 1 & CTR_RX => SETUP or OUT int */
374 /* DIR = 1 & (CTR_TX | CTR_RX) => 2 int pending */
375 ep
= &hpcd
->OUT_ep
[0];
376 wEPVal
= PCD_GET_ENDPOINT(hpcd
->Instance
, PCD_ENDP0
);
378 if ((wEPVal
& USB_EP_SETUP
) != 0)
380 /* Get SETUP Packet*/
381 ep
->xfer_count
= PCD_GET_EP_RX_CNT(hpcd
->Instance
, ep
->num
);
382 PCD_ReadPMA(hpcd
->Instance
, (uint8_t*)hpcd
->Setup
,ep
->pmaadress
, ep
->xfer_count
);
383 /* SETUP bit kept frozen while CTR_RX = 1*/
384 PCD_CLEAR_RX_EP_CTR(hpcd
->Instance
, PCD_ENDP0
);
386 /* Process SETUP Packet*/
387 HAL_PCD_SetupStageCallback(hpcd
);
390 else if ((wEPVal
& USB_EP_CTR_RX
) != 0)
392 PCD_CLEAR_RX_EP_CTR(hpcd
->Instance
, PCD_ENDP0
);
393 /* Get Control Data OUT Packet*/
394 ep
->xfer_count
= PCD_GET_EP_RX_CNT(hpcd
->Instance
, ep
->num
);
396 if (ep
->xfer_count
!= 0)
398 PCD_ReadPMA(hpcd
->Instance
, ep
->xfer_buff
, ep
->pmaadress
, ep
->xfer_count
);
399 ep
->xfer_buff
+=ep
->xfer_count
;
402 /* Process Control Data OUT Packet*/
403 HAL_PCD_DataOutStageCallback(hpcd
, 0);
405 PCD_SET_EP_RX_CNT(hpcd
->Instance
, PCD_ENDP0
, ep
->maxpacket
);
406 PCD_SET_EP_RX_STATUS(hpcd
->Instance
, PCD_ENDP0
, USB_EP_RX_VALID
);
413 /* Decode and service non control endpoints interrupt */
415 /* process related endpoint register */
416 wEPVal
= PCD_GET_ENDPOINT(hpcd
->Instance
, EPindex
);
417 if ((wEPVal
& USB_EP_CTR_RX
) != 0)
420 PCD_CLEAR_RX_EP_CTR(hpcd
->Instance
, EPindex
);
421 ep
= &hpcd
->OUT_ep
[EPindex
];
423 /* OUT double Buffering*/
424 if (ep
->doublebuffer
== 0)
426 count
= PCD_GET_EP_RX_CNT(hpcd
->Instance
, ep
->num
);
429 PCD_ReadPMA(hpcd
->Instance
, ep
->xfer_buff
, ep
->pmaadress
, count
);
434 if (PCD_GET_ENDPOINT(hpcd
->Instance
, ep
->num
) & USB_EP_DTOG_RX
)
436 /*read from endpoint BUF0Addr buffer*/
437 count
= PCD_GET_EP_DBUF0_CNT(hpcd
->Instance
, ep
->num
);
440 PCD_ReadPMA(hpcd
->Instance
, ep
->xfer_buff
, ep
->pmaaddr0
, count
);
445 /*read from endpoint BUF1Addr buffer*/
446 count
= PCD_GET_EP_DBUF1_CNT(hpcd
->Instance
, ep
->num
);
449 PCD_ReadPMA(hpcd
->Instance
, ep
->xfer_buff
, ep
->pmaaddr1
, count
);
452 PCD_FreeUserBuffer(hpcd
->Instance
, ep
->num
, PCD_EP_DBUF_OUT
);
454 /*multi-packet on the NON control OUT endpoint*/
455 ep
->xfer_count
+=count
;
456 ep
->xfer_buff
+=count
;
458 if ((ep
->xfer_len
== 0) || (count
< ep
->maxpacket
))
461 HAL_PCD_DataOutStageCallback(hpcd
, ep
->num
);
465 HAL_PCD_EP_Receive(hpcd
, ep
->num
, ep
->xfer_buff
, ep
->xfer_len
);
468 } /* if((wEPVal & EP_CTR_RX) */
470 if ((wEPVal
& USB_EP_CTR_TX
) != 0)
472 ep
= &hpcd
->IN_ep
[EPindex
];
475 PCD_CLEAR_TX_EP_CTR(hpcd
->Instance
, EPindex
);
477 /* IN double Buffering*/
478 if (ep
->doublebuffer
== 0)
480 ep
->xfer_count
= PCD_GET_EP_TX_CNT(hpcd
->Instance
, ep
->num
);
481 if (ep
->xfer_count
!= 0)
483 PCD_WritePMA(hpcd
->Instance
, ep
->xfer_buff
, ep
->pmaadress
, ep
->xfer_count
);
488 if (PCD_GET_ENDPOINT(hpcd
->Instance
, ep
->num
) & USB_EP_DTOG_TX
)
490 /*read from endpoint BUF0Addr buffer*/
491 ep
->xfer_count
= PCD_GET_EP_DBUF0_CNT(hpcd
->Instance
, ep
->num
);
492 if (ep
->xfer_count
!= 0)
494 PCD_WritePMA(hpcd
->Instance
, ep
->xfer_buff
, ep
->pmaaddr0
, ep
->xfer_count
);
499 /*read from endpoint BUF1Addr buffer*/
500 ep
->xfer_count
= PCD_GET_EP_DBUF1_CNT(hpcd
->Instance
, ep
->num
);
501 if (ep
->xfer_count
!= 0)
503 PCD_WritePMA(hpcd
->Instance
, ep
->xfer_buff
, ep
->pmaaddr1
, ep
->xfer_count
);
506 PCD_FreeUserBuffer(hpcd
->Instance
, ep
->num
, PCD_EP_DBUF_IN
);
508 /*multi-packet on the NON control IN endpoint*/
509 ep
->xfer_count
= PCD_GET_EP_TX_CNT(hpcd
->Instance
, ep
->num
);
510 ep
->xfer_buff
+=ep
->xfer_count
;
512 /* Zero Length Packet? */
513 if (ep
->xfer_len
== 0)
516 HAL_PCD_DataInStageCallback(hpcd
, ep
->num
);
520 HAL_PCD_EP_Transmit(hpcd
, ep
->num
, ep
->xfer_buff
, ep
->xfer_len
);
531 /** @addtogroup PCD_Exported_Functions
535 /** @defgroup PCD_Exported_Functions_Group2 IO operation functions
540 * @brief This function handles PCD interrupt request.
541 * @param hpcd: PCD handle
544 void HAL_PCD_IRQHandler(PCD_HandleTypeDef
*hpcd
)
546 uint32_t wInterrupt_Mask
= 0;
548 if (__HAL_PCD_GET_FLAG (hpcd
, USB_ISTR_CTR
))
550 /* servicing of the endpoint correct transfer interrupt */
551 /* clear of the CTR flag into the sub */
552 PCD_EP_ISR_Handler(hpcd
);
555 if (__HAL_PCD_GET_FLAG (hpcd
, USB_ISTR_RESET
))
557 __HAL_PCD_CLEAR_FLAG(hpcd
, USB_ISTR_RESET
);
558 HAL_PCD_ResetCallback(hpcd
);
559 HAL_PCD_SetAddress(hpcd
, 0);
562 if (__HAL_PCD_GET_FLAG (hpcd
, USB_ISTR_PMAOVR
))
564 __HAL_PCD_CLEAR_FLAG(hpcd
, USB_ISTR_PMAOVR
);
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
))
575 hpcd
->Instance
->CNTR
&= ~(USB_CNTR_LPMODE
);
577 /*set wInterrupt_Mask global variable*/
578 wInterrupt_Mask
= USB_CNTR_CTRM
| USB_CNTR_WKUPM
| USB_CNTR_SUSPM
| USB_CNTR_ERRM \
579 | USB_CNTR_ESOFM
| USB_CNTR_RESETM
;
581 /*Set interrupt mask*/
582 hpcd
->Instance
->CNTR
= wInterrupt_Mask
;
584 HAL_PCD_ResumeCallback(hpcd
);
586 __HAL_PCD_CLEAR_FLAG(hpcd
, USB_ISTR_WKUP
);
589 if (__HAL_PCD_GET_FLAG (hpcd
, USB_ISTR_SUSP
))
591 /* clear of the ISTR bit must be done after setting of CNTR_FSUSP */
592 __HAL_PCD_CLEAR_FLAG(hpcd
, USB_ISTR_SUSP
);
594 /* Force low-power mode in the macrocell */
595 hpcd
->Instance
->CNTR
|= USB_CNTR_FSUSP
;
596 hpcd
->Instance
->CNTR
|= USB_CNTR_LPMODE
;
598 if (__HAL_PCD_GET_FLAG (hpcd
, USB_ISTR_WKUP
) == 0)
600 HAL_PCD_SuspendCallback(hpcd
);
604 if (__HAL_PCD_GET_FLAG (hpcd
, USB_ISTR_SOF
))
606 __HAL_PCD_CLEAR_FLAG(hpcd
, USB_ISTR_SOF
);
607 HAL_PCD_SOFCallback(hpcd
);
610 if (__HAL_PCD_GET_FLAG (hpcd
, USB_ISTR_ESOF
))
612 /* clear ESOF flag in ISTR */
613 __HAL_PCD_CLEAR_FLAG(hpcd
, USB_ISTR_ESOF
);
618 * @brief Data out stage callbacks
619 * @param hpcd: PCD handle
620 * @param epnum: endpoint number
623 __weak
void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef
*hpcd
, uint8_t epnum
)
625 /* NOTE : This function Should not be modified, when the callback is needed,
626 the HAL_PCD_DataOutStageCallback could be implemented in the user file
631 * @brief Data IN stage callbacks
632 * @param hpcd: PCD handle
633 * @param epnum: endpoint number
636 __weak
void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef
*hpcd
, uint8_t epnum
)
638 /* NOTE : This function Should not be modified, when the callback is needed,
639 the HAL_PCD_DataInStageCallback could be implemented in the user file
643 * @brief Setup stage callback
644 * @param hpcd: ppp handle
647 __weak
void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef
*hpcd
)
649 /* NOTE : This function Should not be modified, when the callback is needed,
650 the HAL_PCD_SetupStageCallback could be implemented in the user file
655 * @brief USB Start Of Frame callbacks
656 * @param hpcd: PCD handle
659 __weak
void HAL_PCD_SOFCallback(PCD_HandleTypeDef
*hpcd
)
661 /* NOTE : This function Should not be modified, when the callback is needed,
662 the HAL_PCD_SOFCallback could be implemented in the user file
667 * @brief USB Reset callbacks
668 * @param hpcd: PCD handle
671 __weak
void HAL_PCD_ResetCallback(PCD_HandleTypeDef
*hpcd
)
673 /* NOTE : This function Should not be modified, when the callback is needed,
674 the HAL_PCD_ResetCallback could be implemented in the user file
679 * @brief Suspend event callbacks
680 * @param hpcd: PCD handle
683 __weak
void HAL_PCD_SuspendCallback(PCD_HandleTypeDef
*hpcd
)
685 /* NOTE : This function Should not be modified, when the callback is needed,
686 the HAL_PCD_SuspendCallback could be implemented in the user file
691 * @brief Resume event callbacks
692 * @param hpcd: PCD handle
695 __weak
void HAL_PCD_ResumeCallback(PCD_HandleTypeDef
*hpcd
)
697 /* NOTE : This function Should not be modified, when the callback is needed,
698 the HAL_PCD_ResumeCallback could be implemented in the user file
703 * @brief Incomplete ISO OUT callbacks
704 * @param hpcd: PCD handle
705 * @param epnum: endpoint number
708 __weak
void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef
*hpcd
, uint8_t epnum
)
710 /* NOTE : This function Should not be modified, when the callback is needed,
711 the HAL_PCD_ISOOUTIncompleteCallback could be implemented in the user file
716 * @brief Incomplete ISO IN callbacks
717 * @param hpcd: PCD handle
718 * @param epnum: endpoint number
721 __weak
void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef
*hpcd
, uint8_t epnum
)
723 /* NOTE : This function Should not be modified, when the callback is needed,
724 the HAL_PCD_ISOINIncompleteCallback could be implemented in the user file
729 * @brief Connection event callbacks
730 * @param hpcd: PCD handle
733 __weak
void HAL_PCD_ConnectCallback(PCD_HandleTypeDef
*hpcd
)
735 /* NOTE : This function Should not be modified, when the callback is needed,
736 the HAL_PCD_ConnectCallback could be implemented in the user file
741 * @brief Disconnection event callbacks
742 * @param hpcd: ppp handle
745 __weak
void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef
*hpcd
)
747 /* NOTE : This function Should not be modified, when the callback is needed,
748 the HAL_PCD_DisconnectCallback could be implemented in the user file
755 /** @defgroup PCD_Exported_Functions_Group3 Peripheral Control functions
756 * @brief management functions
759 ===============================================================================
760 ##### Peripheral Control functions #####
761 ===============================================================================
763 This subsection provides a set of functions allowing to control the PCD data
771 * @brief Connect the USB device
772 * @param hpcd: PCD handle
775 HAL_StatusTypeDef
HAL_PCD_DevConnect(PCD_HandleTypeDef
*hpcd
)
779 /* Enabling DP Pull-Down bit to Connect internal pull-up on USB DP line */
780 hpcd
->Instance
->BCDR
|= USB_BCDR_DPPU
;
787 * @brief Disconnect the USB device
788 * @param hpcd: PCD handle
791 HAL_StatusTypeDef
HAL_PCD_DevDisconnect(PCD_HandleTypeDef
*hpcd
)
795 /* Disable DP Pull-Down bit*/
796 hpcd
->Instance
->BCDR
&= ~(USB_BCDR_DPPU
);
803 * @brief Set the USB Device address
804 * @param hpcd: PCD handle
805 * @param address: new device address
808 HAL_StatusTypeDef
HAL_PCD_SetAddress(PCD_HandleTypeDef
*hpcd
, uint8_t address
)
814 /* set device address and enable function */
815 hpcd
->Instance
->DADDR
= USB_DADDR_EF
;
817 else /* USB Address will be applied later */
819 hpcd
->USB_Address
= address
;
826 * @brief Open and configure an endpoint
827 * @param hpcd: PCD handle
828 * @param ep_addr: endpoint address
829 * @param ep_mps: endpoint max packert size
830 * @param ep_type: endpoint type
833 HAL_StatusTypeDef
HAL_PCD_EP_Open(PCD_HandleTypeDef
*hpcd
, uint8_t ep_addr
, uint16_t ep_mps
, uint8_t ep_type
)
835 HAL_StatusTypeDef ret
= HAL_OK
;
838 if ((ep_addr
& 0x80) == 0x80)
840 ep
= &hpcd
->IN_ep
[ep_addr
& 0x7F];
844 ep
= &hpcd
->OUT_ep
[ep_addr
& 0x7F];
846 ep
->num
= ep_addr
& 0x7F;
848 ep
->is_in
= (0x80 & ep_addr
) != 0;
849 ep
->maxpacket
= ep_mps
;
854 /* initialize Endpoint */
857 case PCD_EP_TYPE_CTRL
:
858 PCD_SET_EPTYPE(hpcd
->Instance
, ep
->num
, USB_EP_CONTROL
);
860 case PCD_EP_TYPE_BULK
:
861 PCD_SET_EPTYPE(hpcd
->Instance
, ep
->num
, USB_EP_BULK
);
863 case PCD_EP_TYPE_INTR
:
864 PCD_SET_EPTYPE(hpcd
->Instance
, ep
->num
, USB_EP_INTERRUPT
);
866 case PCD_EP_TYPE_ISOC
:
867 PCD_SET_EPTYPE(hpcd
->Instance
, ep
->num
, USB_EP_ISOCHRONOUS
);
871 PCD_SET_EP_ADDRESS(hpcd
->Instance
, ep
->num
, ep
->num
);
873 if (ep
->doublebuffer
== 0)
877 /*Set the endpoint Transmit buffer address */
878 PCD_SET_EP_TX_ADDRESS(hpcd
->Instance
, ep
->num
, ep
->pmaadress
);
879 PCD_CLEAR_TX_DTOG(hpcd
->Instance
, ep
->num
);
880 /* Configure NAK status for the Endpoint*/
881 PCD_SET_EP_TX_STATUS(hpcd
->Instance
, ep
->num
, USB_EP_TX_NAK
);
885 /*Set the endpoint Receive buffer address */
886 PCD_SET_EP_RX_ADDRESS(hpcd
->Instance
, ep
->num
, ep
->pmaadress
);
887 /*Set the endpoint Receive buffer counter*/
888 PCD_SET_EP_RX_CNT(hpcd
->Instance
, ep
->num
, ep
->maxpacket
);
889 PCD_CLEAR_RX_DTOG(hpcd
->Instance
, ep
->num
);
890 /* Configure VALID status for the Endpoint*/
891 PCD_SET_EP_RX_STATUS(hpcd
->Instance
, ep
->num
, USB_EP_RX_VALID
);
897 /*Set the endpoint as double buffered*/
898 PCD_SET_EP_DBUF(hpcd
->Instance
, ep
->num
);
899 /*Set buffer address for double buffered mode*/
900 PCD_SET_EP_DBUF_ADDR(hpcd
->Instance
, ep
->num
,ep
->pmaaddr0
, ep
->pmaaddr1
);
904 /* Clear the data toggle bits for the endpoint IN/OUT*/
905 PCD_CLEAR_RX_DTOG(hpcd
->Instance
, ep
->num
);
906 PCD_CLEAR_TX_DTOG(hpcd
->Instance
, ep
->num
);
908 /* Reset value of the data toggle bits for the endpoint out*/
909 PCD_TX_DTOG(hpcd
->Instance
, ep
->num
);
911 PCD_SET_EP_RX_STATUS(hpcd
->Instance
, ep
->num
, USB_EP_RX_VALID
);
912 PCD_SET_EP_TX_STATUS(hpcd
->Instance
, ep
->num
, USB_EP_TX_DIS
);
916 /* Clear the data toggle bits for the endpoint IN/OUT*/
917 PCD_CLEAR_RX_DTOG(hpcd
->Instance
, ep
->num
);
918 PCD_CLEAR_TX_DTOG(hpcd
->Instance
, ep
->num
);
919 PCD_RX_DTOG(hpcd
->Instance
, ep
->num
);
920 /* Configure DISABLE status for the Endpoint*/
921 PCD_SET_EP_TX_STATUS(hpcd
->Instance
, ep
->num
, USB_EP_TX_DIS
);
922 PCD_SET_EP_RX_STATUS(hpcd
->Instance
, ep
->num
, USB_EP_RX_DIS
);
932 * @brief Deactivate an endpoint
933 * @param hpcd: PCD handle
934 * @param ep_addr: endpoint address
937 HAL_StatusTypeDef
HAL_PCD_EP_Close(PCD_HandleTypeDef
*hpcd
, uint8_t ep_addr
)
941 if ((ep_addr
& 0x80) == 0x80)
943 ep
= &hpcd
->IN_ep
[ep_addr
& 0x7F];
947 ep
= &hpcd
->OUT_ep
[ep_addr
& 0x7F];
949 ep
->num
= ep_addr
& 0x7F;
951 ep
->is_in
= (0x80 & ep_addr
) != 0;
955 if (ep
->doublebuffer
== 0)
959 PCD_CLEAR_TX_DTOG(hpcd
->Instance
, ep
->num
);
960 /* Configure DISABLE status for the Endpoint*/
961 PCD_SET_EP_TX_STATUS(hpcd
->Instance
, ep
->num
, USB_EP_TX_DIS
);
965 PCD_CLEAR_RX_DTOG(hpcd
->Instance
, ep
->num
);
966 /* Configure DISABLE status for the Endpoint*/
967 PCD_SET_EP_RX_STATUS(hpcd
->Instance
, ep
->num
, USB_EP_RX_DIS
);
975 /* Clear the data toggle bits for the endpoint IN/OUT*/
976 PCD_CLEAR_RX_DTOG(hpcd
->Instance
, ep
->num
);
977 PCD_CLEAR_TX_DTOG(hpcd
->Instance
, ep
->num
);
979 /* Reset value of the data toggle bits for the endpoint out*/
980 PCD_TX_DTOG(hpcd
->Instance
, ep
->num
);
982 PCD_SET_EP_RX_STATUS(hpcd
->Instance
, ep
->num
, USB_EP_RX_DIS
);
983 PCD_SET_EP_TX_STATUS(hpcd
->Instance
, ep
->num
, USB_EP_TX_DIS
);
987 /* Clear the data toggle bits for the endpoint IN/OUT*/
988 PCD_CLEAR_RX_DTOG(hpcd
->Instance
, ep
->num
);
989 PCD_CLEAR_TX_DTOG(hpcd
->Instance
, ep
->num
);
990 PCD_RX_DTOG(hpcd
->Instance
, ep
->num
);
991 /* Configure DISABLE status for the Endpoint*/
992 PCD_SET_EP_TX_STATUS(hpcd
->Instance
, ep
->num
, USB_EP_TX_DIS
);
993 PCD_SET_EP_RX_STATUS(hpcd
->Instance
, ep
->num
, USB_EP_RX_DIS
);
1003 * @brief Receive an amount of data
1004 * @param hpcd: PCD handle
1005 * @param ep_addr: endpoint address
1006 * @param pBuf: pointer to the reception buffer
1007 * @param len: amount of data to be received
1008 * @retval HAL status
1010 HAL_StatusTypeDef
HAL_PCD_EP_Receive(PCD_HandleTypeDef
*hpcd
, uint8_t ep_addr
, uint8_t *pBuf
, uint32_t len
)
1015 ep
= &hpcd
->OUT_ep
[ep_addr
& 0x7F];
1017 /*setup and start the Xfer */
1018 ep
->xfer_buff
= pBuf
;
1022 ep
->num
= ep_addr
& 0x7F;
1026 /* Multi packet transfer*/
1027 if (ep
->xfer_len
> ep
->maxpacket
)
1038 /* configure and validate Rx endpoint */
1039 if (ep
->doublebuffer
== 0)
1041 /*Set RX buffer count*/
1042 PCD_SET_EP_RX_CNT(hpcd
->Instance
, ep
->num
, len
);
1046 /*Set the Double buffer counter*/
1047 PCD_SET_EP_DBUF1_CNT(hpcd
->Instance
, ep
->num
, ep
->is_in
, len
);
1050 PCD_SET_EP_RX_STATUS(hpcd
->Instance
, ep
->num
, USB_EP_RX_VALID
);
1058 * @brief Get Received Data Size
1059 * @param hpcd: PCD handle
1060 * @param ep_addr: endpoint address
1063 uint16_t HAL_PCD_EP_GetRxCount(PCD_HandleTypeDef
*hpcd
, uint8_t ep_addr
)
1065 return hpcd
->OUT_ep
[ep_addr
& 0x7F].xfer_count
;
1068 * @brief Send an amount of data
1069 * @param hpcd: PCD handle
1070 * @param ep_addr: endpoint address
1071 * @param pBuf: pointer to the transmission buffer
1072 * @param len: amount of data to be sent
1073 * @retval HAL status
1075 HAL_StatusTypeDef
HAL_PCD_EP_Transmit(PCD_HandleTypeDef
*hpcd
, uint8_t ep_addr
, uint8_t *pBuf
, uint32_t len
)
1078 uint16_t pmabuffer
= 0;
1080 ep
= &hpcd
->IN_ep
[ep_addr
& 0x7F];
1082 /*setup and start the Xfer */
1083 ep
->xfer_buff
= pBuf
;
1087 ep
->num
= ep_addr
& 0x7F;
1091 /*Multi packet transfer*/
1092 if (ep
->xfer_len
> ep
->maxpacket
)
1103 /* configure and validate Tx endpoint */
1104 if (ep
->doublebuffer
== 0)
1106 PCD_WritePMA(hpcd
->Instance
, ep
->xfer_buff
, ep
->pmaadress
, len
);
1107 PCD_SET_EP_TX_CNT(hpcd
->Instance
, ep
->num
, len
);
1111 /*Set the Double buffer counter*/
1112 PCD_SET_EP_DBUF1_CNT(hpcd
->Instance
, ep
->num
, ep
->is_in
, len
);
1114 /*Write the data to the USB endpoint*/
1115 if (PCD_GET_ENDPOINT(hpcd
->Instance
, ep
->num
)& USB_EP_DTOG_TX
)
1117 pmabuffer
= ep
->pmaaddr1
;
1121 pmabuffer
= ep
->pmaaddr0
;
1123 PCD_WritePMA(hpcd
->Instance
, ep
->xfer_buff
, pmabuffer
, len
);
1124 PCD_FreeUserBuffer(hpcd
->Instance
, ep
->num
, ep
->is_in
);
1127 PCD_SET_EP_TX_STATUS(hpcd
->Instance
, ep
->num
, USB_EP_TX_VALID
);
1135 * @brief Set a STALL condition over an endpoint
1136 * @param hpcd: PCD handle
1137 * @param ep_addr: endpoint address
1138 * @retval HAL status
1140 HAL_StatusTypeDef
HAL_PCD_EP_SetStall(PCD_HandleTypeDef
*hpcd
, uint8_t ep_addr
)
1146 if ((0x80 & ep_addr
) == 0x80)
1148 ep
= &hpcd
->IN_ep
[ep_addr
& 0x7F];
1152 ep
= &hpcd
->OUT_ep
[ep_addr
];
1156 ep
->num
= ep_addr
& 0x7F;
1157 ep
->is_in
= ((ep_addr
& 0x80) == 0x80);
1161 /* This macro sets STALL status for RX & TX*/
1162 PCD_SET_EP_TXRX_STATUS(hpcd
->Instance
, ep
->num
, USB_EP_RX_STALL
, USB_EP_TX_STALL
);
1168 PCD_SET_EP_TX_STATUS(hpcd
->Instance
, ep
->num
, USB_EP_TX_STALL
);
1172 PCD_SET_EP_RX_STATUS(hpcd
->Instance
, ep
->num
, USB_EP_RX_STALL
);
1181 * @brief Clear a STALL condition over in an endpoint
1182 * @param hpcd: PCD handle
1183 * @param ep_addr: endpoint address
1184 * @retval HAL status
1186 HAL_StatusTypeDef
HAL_PCD_EP_ClrStall(PCD_HandleTypeDef
*hpcd
, uint8_t ep_addr
)
1190 if ((0x80 & ep_addr
) == 0x80)
1192 ep
= &hpcd
->IN_ep
[ep_addr
& 0x7F];
1196 ep
= &hpcd
->OUT_ep
[ep_addr
];
1200 ep
->num
= ep_addr
& 0x7F;
1201 ep
->is_in
= ((ep_addr
& 0x80) == 0x80);
1207 PCD_CLEAR_TX_DTOG(hpcd
->Instance
, ep
->num
);
1208 PCD_SET_EP_TX_STATUS(hpcd
->Instance
, ep
->num
, USB_EP_TX_VALID
);
1212 PCD_CLEAR_RX_DTOG(hpcd
->Instance
, ep
->num
);
1213 PCD_SET_EP_RX_STATUS(hpcd
->Instance
, ep
->num
, USB_EP_RX_VALID
);
1221 * @brief Flush an endpoint
1222 * @param hpcd: PCD handle
1223 * @param ep_addr: endpoint address
1224 * @retval HAL status
1226 HAL_StatusTypeDef
HAL_PCD_EP_Flush(PCD_HandleTypeDef
*hpcd
, uint8_t ep_addr
)
1232 * @brief HAL_PCD_ActiveRemoteWakeup : active remote wakeup signalling
1233 * @param hpcd: PCD handle
1236 HAL_StatusTypeDef
HAL_PCD_ActiveRemoteWakeup(PCD_HandleTypeDef
*hpcd
)
1238 hpcd
->Instance
->CNTR
|= USB_CNTR_RESUME
;
1243 * @brief HAL_PCD_DeActiveRemoteWakeup : de-active remote wakeup signalling
1244 * @param hpcd: PCD handle
1247 HAL_StatusTypeDef
HAL_PCD_DeActiveRemoteWakeup(PCD_HandleTypeDef
*hpcd
)
1249 hpcd
->Instance
->CNTR
&= ~(USB_CNTR_RESUME
);
1260 /** @addtogroup PCD_Private_Functions
1264 * @brief Copy a buffer from user memory area to packet memory area (PMA)
1265 * @param USBx: USB peripheral instance register address.
1266 * @param pbUsrBuf: pointer to user memory area.
1267 * @param wPMABufAddr: address into PMA.
1268 * @param wNBytes: no. of bytes to be copied.
1271 void PCD_WritePMA(USB_TypeDef
*USBx
, uint8_t *pbUsrBuf
, uint16_t wPMABufAddr
, uint16_t wNBytes
)
1273 uint32_t n
= (wNBytes
+ 1) >> 1;
1275 uint16_t temp1
, temp2
;
1277 pdwVal
= (uint16_t *)(wPMABufAddr
+ (uint32_t)USBx
+ 0x400);
1279 for (i
= n
; i
!= 0; i
--)
1281 temp1
= (uint16_t) * pbUsrBuf
;
1283 temp2
= temp1
| (uint16_t) * pbUsrBuf
<< 8;
1290 * @brief Copy a buffer from user memory area to packet memory area (PMA)
1291 * @param USBx: USB peripheral instance register address.
1292 * @param pbUsrBuf = pointer to user memory area.
1293 * @param wPMABufAddr: address into PMA.
1294 * @param wNBytes: no. of bytes to be copied.
1297 void PCD_ReadPMA(USB_TypeDef
*USBx
, uint8_t *pbUsrBuf
, uint16_t wPMABufAddr
, uint16_t wNBytes
)
1299 uint32_t n
= (wNBytes
+ 1) >> 1;
1302 pdwVal
= (uint16_t *)(wPMABufAddr
+ (uint32_t)USBx
+ 0x400);
1303 for (i
= n
; i
!= 0; i
--)
1305 *(uint16_t*)pbUsrBuf
++ = *pdwVal
++;
1313 /** @addtogroup PCD_Exported_Functions
1317 /** @defgroup PCD_Exported_Functions_Group4 Peripheral State functions
1318 * @brief Peripheral State functions
1321 ===============================================================================
1322 ##### Peripheral State functions #####
1323 ===============================================================================
1325 This subsection permit to get in run-time the status of the peripheral
1333 * @brief Return the PCD state
1334 * @param hpcd : PCD handle
1337 PCD_StateTypeDef
HAL_PCD_GetState(PCD_HandleTypeDef
*hpcd
)
1356 #endif /* STM32F042x6 || STM32F072xB || STM32F078xx || STM32F070xB || STM32F070x6 */
1358 #endif /* HAL_PCD_MODULE_ENABLED */
1360 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/