]>
git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32L1/stm32l1xx_hal_pcd.c
2 ******************************************************************************
3 * @file stm32l1xx_hal_pcd.c
4 * @author MCD Application Team
6 * @date 5-September-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 "stm32l1xx_hal.h"
77 /** @addtogroup STM32L1xx_HAL_Driver
82 * @brief PCD HAL module driver
86 #ifdef HAL_PCD_MODULE_ENABLED
88 /* Private typedef -----------------------------------------------------------*/
89 /* Private define ------------------------------------------------------------*/
90 /** @defgroup PCD_Private_Constants PCD Private Constants
93 #define BTABLE_ADDRESS (0x000)
98 /* Private macro -------------------------------------------------------------*/
99 /* Private variables ---------------------------------------------------------*/
100 /* Private function prototypes -----------------------------------------------*/
101 /** @defgroup PCD_Private_Functions PCD Private Functions
104 static HAL_StatusTypeDef
PCD_EP_ISR_Handler(PCD_HandleTypeDef
*hpcd
);
105 static void PCD_WritePMA(USB_TypeDef
*USBx
, uint8_t *pbUsrBuf
, uint16_t wPMABufAddr
, uint16_t wNBytes
);
106 static void PCD_ReadPMA(USB_TypeDef
*USBx
, uint8_t *pbUsrBuf
, uint16_t wPMABufAddr
, uint16_t wNBytes
);
113 /** @defgroup PCD_Exported_Functions PCD Exported Functions
117 /** @defgroup PCD_Exported_Functions_Group1 Initialization and de-initialization functions
118 * @brief Initialization and Configuration functions
121 ===============================================================================
122 ##### Initialization and de-initialization functions #####
123 ===============================================================================
124 [..] This section provides functions allowing to:
131 * @brief Initializes the PCD according to the specified
132 * parameters in the PCD_InitTypeDef and create the associated handle.
133 * @param hpcd: PCD handle
136 HAL_StatusTypeDef
HAL_PCD_Init(PCD_HandleTypeDef
*hpcd
)
140 uint32_t wInterrupt_Mask
= 0;
142 /* Check the PCD handle allocation */
148 /* Check the parameters */
149 assert_param(IS_PCD_ALL_INSTANCE(hpcd
->Instance
));
151 hpcd
->State
= PCD_BUSY
;
153 /* Init the low level hardware : GPIO, CLOCK, NVIC... */
154 HAL_PCD_MspInit(hpcd
);
156 /* Init endpoints structures */
157 for (i
= 0; i
< hpcd
->Init
.dev_endpoints
; i
++)
159 /* Init ep structure */
160 hpcd
->IN_ep
[i
].is_in
= 1;
161 hpcd
->IN_ep
[i
].num
= i
;
162 /* Control until ep is actvated */
163 hpcd
->IN_ep
[i
].type
= PCD_EP_TYPE_CTRL
;
164 hpcd
->IN_ep
[i
].maxpacket
= 0;
165 hpcd
->IN_ep
[i
].xfer_buff
= 0;
166 hpcd
->IN_ep
[i
].xfer_len
= 0;
169 for (i
= 0; i
< hpcd
->Init
.dev_endpoints
; i
++)
171 hpcd
->OUT_ep
[i
].is_in
= 0;
172 hpcd
->OUT_ep
[i
].num
= i
;
173 /* Control until ep is activated */
174 hpcd
->OUT_ep
[i
].type
= PCD_EP_TYPE_CTRL
;
175 hpcd
->OUT_ep
[i
].maxpacket
= 0;
176 hpcd
->OUT_ep
[i
].xfer_buff
= 0;
177 hpcd
->OUT_ep
[i
].xfer_len
= 0;
182 hpcd
->Instance
->CNTR
= USB_CNTR_FRES
;
185 hpcd
->Instance
->CNTR
= 0;
187 /*Clear pending interrupts*/
188 hpcd
->Instance
->ISTR
= 0;
190 /*Set Btable Adress*/
191 hpcd
->Instance
->BTABLE
= BTABLE_ADDRESS
;
193 /*set wInterrupt_Mask global variable*/
194 wInterrupt_Mask
= USB_CNTR_CTRM
| USB_CNTR_WKUPM
| USB_CNTR_SUSPM
| USB_CNTR_ERRM \
195 | USB_CNTR_ESOFM
| USB_CNTR_RESETM
;
197 /*Set interrupt mask*/
198 hpcd
->Instance
->CNTR
= wInterrupt_Mask
;
200 hpcd
->USB_Address
= 0;
201 hpcd
->State
= PCD_READY
;
207 * @brief DeInitializes the PCD peripheral
208 * @param hpcd: PCD handle
211 HAL_StatusTypeDef
HAL_PCD_DeInit(PCD_HandleTypeDef
*hpcd
)
213 /* Check the PCD handle allocation */
219 hpcd
->State
= PCD_BUSY
;
224 /* DeInit the low level hardware */
225 HAL_PCD_MspDeInit(hpcd
);
227 hpcd
->State
= PCD_READY
;
233 * @brief Initializes the PCD MSP.
234 * @param hpcd: PCD handle
237 __weak
void HAL_PCD_MspInit(PCD_HandleTypeDef
*hpcd
)
239 /* NOTE : This function Should not be modified, when the callback is needed,
240 the HAL_PCD_MspInit could be implenetd in the user file
245 * @brief DeInitializes PCD MSP.
246 * @param hpcd: PCD handle
249 __weak
void HAL_PCD_MspDeInit(PCD_HandleTypeDef
*hpcd
)
251 /* NOTE : This function Should not be modified, when the callback is needed,
252 the HAL_PCD_MspDeInit could be implenetd in the user file
260 /** @defgroup PCD_Exported_Functions_Group2 IO operation functions
261 * @brief Data transfers functions
264 ===============================================================================
265 ##### IO operation functions #####
266 ===============================================================================
268 This subsection provides a set of functions allowing to manage the PCD data
276 * @brief Start The USB OTG Device.
277 * @param hpcd: PCD handle
280 HAL_StatusTypeDef
HAL_PCD_Start(PCD_HandleTypeDef
*hpcd
)
282 HAL_PCDEx_SetConnectionState (hpcd
, 1);
287 * @brief Stop The USB OTG Device.
288 * @param hpcd: PCD handle
291 HAL_StatusTypeDef
HAL_PCD_Stop(PCD_HandleTypeDef
*hpcd
)
295 /* disable all interrupts and force USB reset */
296 hpcd
->Instance
->CNTR
= USB_CNTR_FRES
;
298 /* clear interrupt status register */
299 hpcd
->Instance
->ISTR
= 0;
301 /* switch-off device */
302 hpcd
->Instance
->CNTR
= (USB_CNTR_FRES
| USB_CNTR_PDWN
);
309 * @brief This function handles PCD interrupt request.
310 * @param hpcd: PCD handle
313 void HAL_PCD_IRQHandler(PCD_HandleTypeDef
*hpcd
)
315 uint32_t wInterrupt_Mask
= 0;
317 if (__HAL_PCD_GET_FLAG (hpcd
, USB_ISTR_CTR
))
319 /* servicing of the endpoint correct transfer interrupt */
320 /* clear of the CTR flag into the sub */
321 PCD_EP_ISR_Handler(hpcd
);
324 if (__HAL_PCD_GET_FLAG (hpcd
, USB_ISTR_RESET
))
326 __HAL_PCD_CLEAR_FLAG(hpcd
, USB_ISTR_RESET
);
327 HAL_PCD_ResetCallback(hpcd
);
328 HAL_PCD_SetAddress(hpcd
, 0);
331 if (__HAL_PCD_GET_FLAG (hpcd
, USB_ISTR_PMAOVRM
))
333 __HAL_PCD_CLEAR_FLAG(hpcd
, USB_ISTR_PMAOVRM
);
335 if (__HAL_PCD_GET_FLAG (hpcd
, USB_ISTR_ERR
))
337 __HAL_PCD_CLEAR_FLAG(hpcd
, USB_ISTR_ERR
);
340 if (__HAL_PCD_GET_FLAG (hpcd
, USB_ISTR_WKUP
))
342 hpcd
->Instance
->CNTR
&= ~(USB_CNTR_LP_MODE
);
344 /*set wInterrupt_Mask global variable*/
345 wInterrupt_Mask
= USB_CNTR_CTRM
| USB_CNTR_WKUPM
| USB_CNTR_SUSPM
| USB_CNTR_ERRM \
346 | USB_CNTR_ESOFM
| USB_CNTR_RESETM
;
348 /*Set interrupt mask*/
349 hpcd
->Instance
->CNTR
= wInterrupt_Mask
;
351 HAL_PCD_ResumeCallback(hpcd
);
353 __HAL_PCD_CLEAR_FLAG(hpcd
, USB_ISTR_WKUP
);
356 if (__HAL_PCD_GET_FLAG (hpcd
, USB_ISTR_SUSP
))
358 /* clear of the ISTR bit must be done after setting of CNTR_FSUSP */
359 __HAL_PCD_CLEAR_FLAG(hpcd
, USB_ISTR_SUSP
);
361 /* Force low-power mode in the macrocell */
362 hpcd
->Instance
->CNTR
|= USB_CNTR_FSUSP
;
363 hpcd
->Instance
->CNTR
|= USB_CNTR_LP_MODE
;
364 if (__HAL_PCD_GET_FLAG (hpcd
, USB_ISTR_WKUP
) == 0)
366 HAL_PCD_SuspendCallback(hpcd
);
370 if (__HAL_PCD_GET_FLAG (hpcd
, USB_ISTR_SOF
))
372 __HAL_PCD_CLEAR_FLAG(hpcd
, USB_ISTR_SOF
);
373 HAL_PCD_SOFCallback(hpcd
);
376 if (__HAL_PCD_GET_FLAG (hpcd
, USB_ISTR_ESOF
))
378 /* clear ESOF flag in ISTR */
379 __HAL_PCD_CLEAR_FLAG(hpcd
, USB_ISTR_ESOF
);
384 * @brief Data out stage callbacks
385 * @param hpcd: PCD handle
386 * @param epnum: endpoint number
389 __weak
void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef
*hpcd
, uint8_t epnum
)
391 /* NOTE : This function Should not be modified, when the callback is needed,
392 the HAL_PCD_DataOutStageCallback could be implenetd in the user file
397 * @brief Data IN stage callbacks
398 * @param hpcd: PCD handle
399 * @param epnum: endpoint number
402 __weak
void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef
*hpcd
, uint8_t epnum
)
404 /* NOTE : This function Should not be modified, when the callback is needed,
405 the HAL_PCD_DataInStageCallback could be implenetd in the user file
409 * @brief Setup stage callback
410 * @param hpcd: ppp handle
413 __weak
void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef
*hpcd
)
415 /* NOTE : This function Should not be modified, when the callback is needed,
416 the HAL_PCD_SetupStageCallback could be implenetd in the user file
421 * @brief USB Start Of Frame callbacks
422 * @param hpcd: PCD handle
425 __weak
void HAL_PCD_SOFCallback(PCD_HandleTypeDef
*hpcd
)
427 /* NOTE : This function Should not be modified, when the callback is needed,
428 the HAL_PCD_SOFCallback could be implenetd in the user file
433 * @brief USB Reset callbacks
434 * @param hpcd: PCD handle
437 __weak
void HAL_PCD_ResetCallback(PCD_HandleTypeDef
*hpcd
)
439 /* NOTE : This function Should not be modified, when the callback is needed,
440 the HAL_PCD_ResetCallback could be implenetd in the user file
446 * @brief Suspend event callbacks
447 * @param hpcd: PCD handle
450 __weak
void HAL_PCD_SuspendCallback(PCD_HandleTypeDef
*hpcd
)
452 /* NOTE : This function Should not be modified, when the callback is needed,
453 the HAL_PCD_SuspendCallback could be implenetd in the user file
458 * @brief Resume event callbacks
459 * @param hpcd: PCD handle
462 __weak
void HAL_PCD_ResumeCallback(PCD_HandleTypeDef
*hpcd
)
464 /* NOTE : This function Should not be modified, when the callback is needed,
465 the HAL_PCD_ResumeCallback could be implenetd in the user file
470 * @brief Incomplete ISO OUT callbacks
471 * @param hpcd: PCD handle
472 * @param epnum: endpoint number
475 __weak
void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef
*hpcd
, uint8_t epnum
)
477 /* NOTE : This function Should not be modified, when the callback is needed,
478 the HAL_PCD_ISOOUTIncompleteCallback could be implenetd in the user file
483 * @brief Incomplete ISO IN callbacks
484 * @param hpcd: PCD handle
485 * @param epnum: endpoint number
488 __weak
void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef
*hpcd
, uint8_t epnum
)
490 /* NOTE : This function Should not be modified, when the callback is needed,
491 the HAL_PCD_ISOINIncompleteCallback could be implenetd in the user file
496 * @brief Connection event callbacks
497 * @param hpcd: PCD handle
500 __weak
void HAL_PCD_ConnectCallback(PCD_HandleTypeDef
*hpcd
)
502 /* NOTE : This function Should not be modified, when the callback is needed,
503 the HAL_PCD_ConnectCallback could be implenetd in the user file
508 * @brief Disconnection event callbacks
509 * @param hpcd: ppp handle
512 __weak
void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef
*hpcd
)
514 /* NOTE : This function Should not be modified, when the callback is needed,
515 the HAL_PCD_DisconnectCallback could be implenetd in the user file
523 /** @defgroup PCD_Exported_Functions_Group3 Peripheral Control functions
524 * @brief management functions
527 ===============================================================================
528 ##### Peripheral Control functions #####
529 ===============================================================================
531 This subsection provides a set of functions allowing to control the PCD data
539 * @brief Connect the USB device
540 * @param hpcd: PCD handle
543 HAL_StatusTypeDef
HAL_PCD_DevConnect(PCD_HandleTypeDef
*hpcd
)
547 /* Enabling DP Pull-Down bit to Connect internal pull-up on USB DP line */
548 HAL_PCDEx_SetConnectionState (hpcd
, 1);
555 * @brief Disconnect the USB device
556 * @param hpcd: PCD handle
559 HAL_StatusTypeDef
HAL_PCD_DevDisconnect(PCD_HandleTypeDef
*hpcd
)
563 /* Disable DP Pull-Down bit*/
564 HAL_PCDEx_SetConnectionState (hpcd
, 0);
571 * @brief Set the USB Device address
572 * @param hpcd: PCD handle
573 * @param address: new device address
576 HAL_StatusTypeDef
HAL_PCD_SetAddress(PCD_HandleTypeDef
*hpcd
, uint8_t address
)
582 /* set device address and enable function */
583 hpcd
->Instance
->DADDR
= USB_DADDR_EF
;
585 else /* USB Address will be applied later */
587 hpcd
->USB_Address
= address
;
594 * @brief Open and configure an endpoint
595 * @param hpcd: PCD handle
596 * @param ep_addr: endpoint address
597 * @param ep_mps: endpoint max packert size
598 * @param ep_type: endpoint type
601 HAL_StatusTypeDef
HAL_PCD_EP_Open(PCD_HandleTypeDef
*hpcd
, uint8_t ep_addr
, uint16_t ep_mps
, uint8_t ep_type
)
603 HAL_StatusTypeDef ret
= HAL_OK
;
606 if ((ep_addr
& 0x80) == 0x80)
608 ep
= &hpcd
->IN_ep
[ep_addr
& 0x7F];
612 ep
= &hpcd
->OUT_ep
[ep_addr
& 0x7F];
614 ep
->num
= ep_addr
& 0x7F;
616 ep
->is_in
= (0x80 & ep_addr
) != 0;
617 ep
->maxpacket
= ep_mps
;
622 /* initialize Endpoint */
625 case PCD_EP_TYPE_CTRL
:
626 PCD_SET_EPTYPE(hpcd
->Instance
, ep
->num
, USB_EP_CONTROL
);
628 case PCD_EP_TYPE_BULK
:
629 PCD_SET_EPTYPE(hpcd
->Instance
, ep
->num
, USB_EP_BULK
);
631 case PCD_EP_TYPE_INTR
:
632 PCD_SET_EPTYPE(hpcd
->Instance
, ep
->num
, USB_EP_INTERRUPT
);
634 case PCD_EP_TYPE_ISOC
:
635 PCD_SET_EPTYPE(hpcd
->Instance
, ep
->num
, USB_EP_ISOCHRONOUS
);
641 PCD_SET_EP_ADDRESS(hpcd
->Instance
, ep
->num
, ep
->num
);
643 if (ep
->doublebuffer
== 0)
647 /*Set the endpoint Transmit buffer address */
648 PCD_SET_EP_TX_ADDRESS(hpcd
->Instance
, ep
->num
, ep
->pmaadress
);
649 PCD_CLEAR_TX_DTOG(hpcd
->Instance
, ep
->num
);
650 /* Configure NAK status for the Endpoint*/
651 PCD_SET_EP_TX_STATUS(hpcd
->Instance
, ep
->num
, USB_EP_TX_NAK
);
655 /*Set the endpoint Receive buffer address */
656 PCD_SET_EP_RX_ADDRESS(hpcd
->Instance
, ep
->num
, ep
->pmaadress
);
657 /*Set the endpoint Receive buffer counter*/
658 PCD_SET_EP_RX_CNT(hpcd
->Instance
, ep
->num
, ep
->maxpacket
);
659 PCD_CLEAR_RX_DTOG(hpcd
->Instance
, ep
->num
);
660 /* Configure VALID status for the Endpoint*/
661 PCD_SET_EP_RX_STATUS(hpcd
->Instance
, ep
->num
, USB_EP_RX_VALID
);
667 /*Set the endpoint as double buffered*/
668 PCD_SET_EP_DBUF(hpcd
->Instance
, ep
->num
);
669 /*Set buffer address for double buffered mode*/
670 PCD_SET_EP_DBUF_ADDR(hpcd
->Instance
, ep
->num
,ep
->pmaaddr0
, ep
->pmaaddr1
);
674 /* Clear the data toggle bits for the endpoint IN/OUT*/
675 PCD_CLEAR_RX_DTOG(hpcd
->Instance
, ep
->num
);
676 PCD_CLEAR_TX_DTOG(hpcd
->Instance
, ep
->num
);
678 /* Reset value of the data toggle bits for the endpoint out*/
679 PCD_TX_DTOG(hpcd
->Instance
, ep
->num
);
681 PCD_SET_EP_RX_STATUS(hpcd
->Instance
, ep
->num
, USB_EP_RX_VALID
);
682 PCD_SET_EP_TX_STATUS(hpcd
->Instance
, ep
->num
, USB_EP_TX_DIS
);
686 /* Clear the data toggle bits for the endpoint IN/OUT*/
687 PCD_CLEAR_RX_DTOG(hpcd
->Instance
, ep
->num
);
688 PCD_CLEAR_TX_DTOG(hpcd
->Instance
, ep
->num
);
689 PCD_RX_DTOG(hpcd
->Instance
, ep
->num
);
690 /* Configure DISABLE status for the Endpoint*/
691 PCD_SET_EP_TX_STATUS(hpcd
->Instance
, ep
->num
, USB_EP_TX_DIS
);
692 PCD_SET_EP_RX_STATUS(hpcd
->Instance
, ep
->num
, USB_EP_RX_DIS
);
702 * @brief Deactivate an endpoint
703 * @param hpcd: PCD handle
704 * @param ep_addr: endpoint address
707 HAL_StatusTypeDef
HAL_PCD_EP_Close(PCD_HandleTypeDef
*hpcd
, uint8_t ep_addr
)
711 if ((ep_addr
& 0x80) == 0x80)
713 ep
= &hpcd
->IN_ep
[ep_addr
& 0x7F];
717 ep
= &hpcd
->OUT_ep
[ep_addr
& 0x7F];
719 ep
->num
= ep_addr
& 0x7F;
721 ep
->is_in
= (0x80 & ep_addr
) != 0;
725 if (ep
->doublebuffer
== 0)
729 PCD_CLEAR_TX_DTOG(hpcd
->Instance
, ep
->num
);
730 /* Configure DISABLE status for the Endpoint*/
731 PCD_SET_EP_TX_STATUS(hpcd
->Instance
, ep
->num
, USB_EP_TX_DIS
);
735 PCD_CLEAR_RX_DTOG(hpcd
->Instance
, ep
->num
);
736 /* Configure DISABLE status for the Endpoint*/
737 PCD_SET_EP_RX_STATUS(hpcd
->Instance
, ep
->num
, USB_EP_RX_DIS
);
745 /* Clear the data toggle bits for the endpoint IN/OUT*/
746 PCD_CLEAR_RX_DTOG(hpcd
->Instance
, ep
->num
);
747 PCD_CLEAR_TX_DTOG(hpcd
->Instance
, ep
->num
);
749 /* Reset value of the data toggle bits for the endpoint out*/
750 PCD_TX_DTOG(hpcd
->Instance
, ep
->num
);
752 PCD_SET_EP_RX_STATUS(hpcd
->Instance
, ep
->num
, USB_EP_RX_DIS
);
753 PCD_SET_EP_TX_STATUS(hpcd
->Instance
, ep
->num
, USB_EP_TX_DIS
);
757 /* Clear the data toggle bits for the endpoint IN/OUT*/
758 PCD_CLEAR_RX_DTOG(hpcd
->Instance
, ep
->num
);
759 PCD_CLEAR_TX_DTOG(hpcd
->Instance
, ep
->num
);
760 PCD_RX_DTOG(hpcd
->Instance
, ep
->num
);
761 /* Configure DISABLE status for the Endpoint*/
762 PCD_SET_EP_TX_STATUS(hpcd
->Instance
, ep
->num
, USB_EP_TX_DIS
);
763 PCD_SET_EP_RX_STATUS(hpcd
->Instance
, ep
->num
, USB_EP_RX_DIS
);
773 * @brief Receive an amount of data
774 * @param hpcd: PCD handle
775 * @param ep_addr: endpoint address
776 * @param pBuf: pointer to the reception buffer
777 * @param len: amount of data to be received
780 HAL_StatusTypeDef
HAL_PCD_EP_Receive(PCD_HandleTypeDef
*hpcd
, uint8_t ep_addr
, uint8_t *pBuf
, uint32_t len
)
785 ep
= &hpcd
->OUT_ep
[ep_addr
& 0x7F];
787 /*setup and start the Xfer */
788 ep
->xfer_buff
= pBuf
;
792 ep
->num
= ep_addr
& 0x7F;
796 /* Multi packet transfer*/
797 if (ep
->xfer_len
> ep
->maxpacket
)
808 /* configure and validate Rx endpoint */
809 if (ep
->doublebuffer
== 0)
811 /*Set RX buffer count*/
812 PCD_SET_EP_RX_CNT(hpcd
->Instance
, ep
->num
, len
);
816 /*Set the Double buffer counter*/
817 PCD_SET_EP_DBUF1_CNT(hpcd
->Instance
, ep
->num
, ep
->is_in
, len
);
820 PCD_SET_EP_RX_STATUS(hpcd
->Instance
, ep
->num
, USB_EP_RX_VALID
);
828 * @brief Get Received Data Size
829 * @param hpcd: PCD handle
830 * @param ep_addr: endpoint address
833 uint16_t HAL_PCD_EP_GetRxCount(PCD_HandleTypeDef
*hpcd
, uint8_t ep_addr
)
835 return hpcd
->OUT_ep
[ep_addr
& 0x7F].xfer_count
;
838 * @brief Send an amount of data
839 * @param hpcd: PCD handle
840 * @param ep_addr: endpoint address
841 * @param pBuf: pointer to the transmission buffer
842 * @param len: amount of data to be sent
845 HAL_StatusTypeDef
HAL_PCD_EP_Transmit(PCD_HandleTypeDef
*hpcd
, uint8_t ep_addr
, uint8_t *pBuf
, uint32_t len
)
848 uint16_t pmabuffer
= 0;
850 ep
= &hpcd
->IN_ep
[ep_addr
& 0x7F];
852 /*setup and start the Xfer */
853 ep
->xfer_buff
= pBuf
;
857 ep
->num
= ep_addr
& 0x7F;
861 /*Multi packet transfer*/
862 if (ep
->xfer_len
> ep
->maxpacket
)
873 /* configure and validate Tx endpoint */
874 if (ep
->doublebuffer
== 0)
876 PCD_WritePMA(hpcd
->Instance
, ep
->xfer_buff
, ep
->pmaadress
, len
);
877 PCD_SET_EP_TX_CNT(hpcd
->Instance
, ep
->num
, len
);
881 /*Set the Double buffer counter*/
882 PCD_SET_EP_DBUF1_CNT(hpcd
->Instance
, ep
->num
, ep
->is_in
, len
);
884 /*Write the data to the USB endpoint*/
885 if (PCD_GET_ENDPOINT(hpcd
->Instance
, ep
->num
)& USB_EP_DTOG_TX
)
887 pmabuffer
= ep
->pmaaddr1
;
891 pmabuffer
= ep
->pmaaddr0
;
893 PCD_WritePMA(hpcd
->Instance
, ep
->xfer_buff
, pmabuffer
, len
);
894 PCD_FreeUserBuffer(hpcd
->Instance
, ep
->num
, ep
->is_in
);
897 PCD_SET_EP_TX_STATUS(hpcd
->Instance
, ep
->num
, USB_EP_TX_VALID
);
905 * @brief Set a STALL condition over an endpoint
906 * @param hpcd: PCD handle
907 * @param ep_addr: endpoint address
910 HAL_StatusTypeDef
HAL_PCD_EP_SetStall(PCD_HandleTypeDef
*hpcd
, uint8_t ep_addr
)
916 if ((0x80 & ep_addr
) == 0x80)
918 ep
= &hpcd
->IN_ep
[ep_addr
& 0x7F];
922 ep
= &hpcd
->OUT_ep
[ep_addr
];
926 ep
->num
= ep_addr
& 0x7F;
927 ep
->is_in
= ((ep_addr
& 0x80) == 0x80);
931 /* This macro sets STALL status for RX & TX*/
932 PCD_SET_EP_TXRX_STATUS(hpcd
->Instance
, ep
->num
, USB_EP_RX_STALL
, USB_EP_TX_STALL
);
938 PCD_SET_EP_TX_STATUS(hpcd
->Instance
, ep
->num
, USB_EP_TX_STALL
);
942 PCD_SET_EP_RX_STATUS(hpcd
->Instance
, ep
->num
, USB_EP_RX_STALL
);
951 * @brief Clear a STALL condition over in an endpoint
952 * @param hpcd: PCD handle
953 * @param ep_addr: endpoint address
956 HAL_StatusTypeDef
HAL_PCD_EP_ClrStall(PCD_HandleTypeDef
*hpcd
, uint8_t ep_addr
)
960 if ((0x80 & ep_addr
) == 0x80)
962 ep
= &hpcd
->IN_ep
[ep_addr
& 0x7F];
966 ep
= &hpcd
->OUT_ep
[ep_addr
];
970 ep
->num
= ep_addr
& 0x7F;
971 ep
->is_in
= ((ep_addr
& 0x80) == 0x80);
977 PCD_CLEAR_TX_DTOG(hpcd
->Instance
, ep
->num
);
978 PCD_SET_EP_TX_STATUS(hpcd
->Instance
, ep
->num
, USB_EP_TX_VALID
);
982 PCD_CLEAR_RX_DTOG(hpcd
->Instance
, ep
->num
);
983 PCD_SET_EP_RX_STATUS(hpcd
->Instance
, ep
->num
, USB_EP_RX_VALID
);
991 * @brief Flush an endpoint
992 * @param hpcd: PCD handle
993 * @param ep_addr: endpoint address
996 HAL_StatusTypeDef
HAL_PCD_EP_Flush(PCD_HandleTypeDef
*hpcd
, uint8_t ep_addr
)
1002 * @brief HAL_PCD_ActiveRemoteWakeup : active remote wakeup signalling
1003 * @param hpcd: PCD handle
1006 HAL_StatusTypeDef
HAL_PCD_ActiveRemoteWakeup(PCD_HandleTypeDef
*hpcd
)
1008 hpcd
->Instance
->CNTR
|= USB_CNTR_RESUME
;
1013 * @brief HAL_PCD_DeActiveRemoteWakeup : de-active remote wakeup signalling
1014 * @param hpcd: PCD handle
1017 HAL_StatusTypeDef
HAL_PCD_DeActiveRemoteWakeup(PCD_HandleTypeDef
*hpcd
)
1019 hpcd
->Instance
->CNTR
&= ~(USB_CNTR_RESUME
);
1027 /** @defgroup PCD_Exported_Functions_Group4 Peripheral State functions
1028 * @brief Peripheral State functions
1031 ===============================================================================
1032 ##### Peripheral State functions #####
1033 ===============================================================================
1035 This subsection permit to get in run-time the status of the peripheral
1044 * @brief Return the PCD state
1045 * @param hpcd : PCD handle
1048 PCD_StateTypeDef
HAL_PCD_GetState(PCD_HandleTypeDef
*hpcd
)
1055 * @brief Software Device Connection
1056 * @param hpcd: PCD handle
1057 * @param state: Device state
1060 __weak
void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef
*hpcd
, uint8_t state
)
1062 /* NOTE : This function Should not be modified, when the callback is needed,
1063 the HAL_PCDEx_SetConnectionState could be implenetd in the user file
1075 /** @addtogroup PCD_Private_Functions
1081 * @brief This function handles PCD Endpoint interrupt request.
1082 * @param hpcd: PCD handle
1083 * @retval HAL status
1085 static HAL_StatusTypeDef
PCD_EP_ISR_Handler(PCD_HandleTypeDef
*hpcd
)
1090 __IO
uint16_t wIstr
;
1091 __IO
uint16_t wEPVal
= 0;
1093 /* stay in loop while pending interrupts */
1094 while (((wIstr
= hpcd
->Instance
->ISTR
) & USB_ISTR_CTR
) != 0)
1096 /* extract highest priority endpoint number */
1097 EPindex
= (uint8_t)(wIstr
& USB_ISTR_EP_ID
);
1101 /* Decode and service control endpoint interrupt */
1103 /* DIR bit = origin of the interrupt */
1104 if ((wIstr
& USB_ISTR_DIR
) == 0)
1108 /* DIR = 0 => IN int */
1109 /* DIR = 0 implies that (EP_CTR_TX = 1) always */
1110 PCD_CLEAR_TX_EP_CTR(hpcd
->Instance
, PCD_ENDP0
);
1111 ep
= &hpcd
->IN_ep
[0];
1113 ep
->xfer_count
= PCD_GET_EP_TX_CNT(hpcd
->Instance
, ep
->num
);
1114 ep
->xfer_buff
+= ep
->xfer_count
;
1117 HAL_PCD_DataInStageCallback(hpcd
, 0);
1120 if((hpcd
->USB_Address
> 0)&& ( ep
->xfer_len
== 0))
1122 hpcd
->Instance
->DADDR
= (hpcd
->USB_Address
| USB_DADDR_EF
);
1123 hpcd
->USB_Address
= 0;
1131 /* DIR = 1 & CTR_RX => SETUP or OUT int */
1132 /* DIR = 1 & (CTR_TX | CTR_RX) => 2 int pending */
1133 ep
= &hpcd
->OUT_ep
[0];
1134 wEPVal
= PCD_GET_ENDPOINT(hpcd
->Instance
, PCD_ENDP0
);
1136 if ((wEPVal
& USB_EP_SETUP
) != 0)
1138 /* Get SETUP Packet*/
1139 ep
->xfer_count
= PCD_GET_EP_RX_CNT(hpcd
->Instance
, ep
->num
);
1140 PCD_ReadPMA(hpcd
->Instance
, (uint8_t*)hpcd
->Setup
,ep
->pmaadress
, ep
->xfer_count
);
1141 /* SETUP bit kept frozen while CTR_RX = 1*/
1142 PCD_CLEAR_RX_EP_CTR(hpcd
->Instance
, PCD_ENDP0
);
1144 /* Process SETUP Packet*/
1145 HAL_PCD_SetupStageCallback(hpcd
);
1148 else if ((wEPVal
& USB_EP_CTR_RX
) != 0)
1150 PCD_CLEAR_RX_EP_CTR(hpcd
->Instance
, PCD_ENDP0
);
1151 /* Get Control Data OUT Packet*/
1152 ep
->xfer_count
= PCD_GET_EP_RX_CNT(hpcd
->Instance
, ep
->num
);
1154 if (ep
->xfer_count
!= 0)
1156 PCD_ReadPMA(hpcd
->Instance
, ep
->xfer_buff
, ep
->pmaadress
, ep
->xfer_count
);
1157 ep
->xfer_buff
+=ep
->xfer_count
;
1160 /* Process Control Data OUT Packet*/
1161 HAL_PCD_DataOutStageCallback(hpcd
, 0);
1163 PCD_SET_EP_RX_CNT(hpcd
->Instance
, PCD_ENDP0
, ep
->maxpacket
);
1164 PCD_SET_EP_RX_STATUS(hpcd
->Instance
, PCD_ENDP0
, USB_EP_RX_VALID
);
1171 /* Decode and service non control endpoints interrupt */
1173 /* process related endpoint register */
1174 wEPVal
= PCD_GET_ENDPOINT(hpcd
->Instance
, EPindex
);
1175 if ((wEPVal
& USB_EP_CTR_RX
) != 0)
1177 /* clear int flag */
1178 PCD_CLEAR_RX_EP_CTR(hpcd
->Instance
, EPindex
);
1179 ep
= &hpcd
->OUT_ep
[EPindex
];
1181 /* OUT double Buffering*/
1182 if (ep
->doublebuffer
== 0)
1184 count
= PCD_GET_EP_RX_CNT(hpcd
->Instance
, ep
->num
);
1187 PCD_ReadPMA(hpcd
->Instance
, ep
->xfer_buff
, ep
->pmaadress
, count
);
1192 if (PCD_GET_ENDPOINT(hpcd
->Instance
, ep
->num
) & USB_EP_DTOG_RX
)
1194 /*read from endpoint BUF0Addr buffer*/
1195 count
= PCD_GET_EP_DBUF0_CNT(hpcd
->Instance
, ep
->num
);
1198 PCD_ReadPMA(hpcd
->Instance
, ep
->xfer_buff
, ep
->pmaaddr0
, count
);
1203 /*read from endpoint BUF1Addr buffer*/
1204 count
= PCD_GET_EP_DBUF1_CNT(hpcd
->Instance
, ep
->num
);
1207 PCD_ReadPMA(hpcd
->Instance
, ep
->xfer_buff
, ep
->pmaaddr1
, count
);
1210 PCD_FreeUserBuffer(hpcd
->Instance
, ep
->num
, PCD_EP_DBUF_OUT
);
1212 /*multi-packet on the NON control OUT endpoint*/
1213 ep
->xfer_count
+=count
;
1214 ep
->xfer_buff
+=count
;
1216 if ((ep
->xfer_len
== 0) || (count
< ep
->maxpacket
))
1219 HAL_PCD_DataOutStageCallback(hpcd
, ep
->num
);
1223 HAL_PCD_EP_Receive(hpcd
, ep
->num
, ep
->xfer_buff
, ep
->xfer_len
);
1226 } /* if((wEPVal & EP_CTR_RX) */
1228 if ((wEPVal
& USB_EP_CTR_TX
) != 0)
1230 ep
= &hpcd
->IN_ep
[EPindex
];
1232 /* clear int flag */
1233 PCD_CLEAR_TX_EP_CTR(hpcd
->Instance
, EPindex
);
1235 /* IN double Buffering*/
1236 if (ep
->doublebuffer
== 0)
1238 ep
->xfer_count
= PCD_GET_EP_TX_CNT(hpcd
->Instance
, ep
->num
);
1239 if (ep
->xfer_count
!= 0)
1241 PCD_WritePMA(hpcd
->Instance
, ep
->xfer_buff
, ep
->pmaadress
, ep
->xfer_count
);
1246 if (PCD_GET_ENDPOINT(hpcd
->Instance
, ep
->num
) & USB_EP_DTOG_TX
)
1248 /*read from endpoint BUF0Addr buffer*/
1249 ep
->xfer_count
= PCD_GET_EP_DBUF0_CNT(hpcd
->Instance
, ep
->num
);
1250 if (ep
->xfer_count
!= 0)
1252 PCD_WritePMA(hpcd
->Instance
, ep
->xfer_buff
, ep
->pmaaddr0
, ep
->xfer_count
);
1257 /*read from endpoint BUF1Addr buffer*/
1258 ep
->xfer_count
= PCD_GET_EP_DBUF1_CNT(hpcd
->Instance
, ep
->num
);
1259 if (ep
->xfer_count
!= 0)
1261 PCD_WritePMA(hpcd
->Instance
, ep
->xfer_buff
, ep
->pmaaddr1
, ep
->xfer_count
);
1264 PCD_FreeUserBuffer(hpcd
->Instance
, ep
->num
, PCD_EP_DBUF_IN
);
1266 /*multi-packet on the NON control IN endpoint*/
1267 ep
->xfer_count
= PCD_GET_EP_TX_CNT(hpcd
->Instance
, ep
->num
);
1268 ep
->xfer_buff
+=ep
->xfer_count
;
1270 /* Zero Length Packet? */
1271 if (ep
->xfer_len
== 0)
1274 HAL_PCD_DataInStageCallback(hpcd
, ep
->num
);
1278 HAL_PCD_EP_Transmit(hpcd
, ep
->num
, ep
->xfer_buff
, ep
->xfer_len
);
1287 * @brief Copy a buffer from user memory area to packet memory area (PMA)
1288 * @param USBx = pointer to USB register.
1289 * @param pbUsrBuf: pointer to user memory area.
1290 * @param wPMABufAddr: address into PMA.
1291 * @param wNBytes: no. of bytes to be copied.
1294 static void PCD_WritePMA(USB_TypeDef
*USBx
, uint8_t *pbUsrBuf
, uint16_t wPMABufAddr
, uint16_t wNBytes
)
1296 uint32_t n
= (wNBytes
+ 1) >> 1; /* n = (wNBytes + 1) / 2 */
1297 uint32_t i
, temp1
, temp2
;
1299 pdwVal
= (uint16_t *)(wPMABufAddr
* 2 + (uint32_t)USBx
+ 0x400);
1300 for (i
= n
; i
!= 0; i
--)
1302 temp1
= (uint16_t) * pbUsrBuf
;
1304 temp2
= temp1
| (uint16_t) * pbUsrBuf
<< 8;
1312 * @brief Copy a buffer from user memory area to packet memory area (PMA)
1313 * @param USBx = pointer to USB register.
1314 * @param pbUsrBuf = pointer to user memory area.
1315 * @param wPMABufAddr: address into PMA.
1316 * @param wNBytes: no. of bytes to be copied.
1319 static void PCD_ReadPMA(USB_TypeDef
*USBx
, uint8_t *pbUsrBuf
, uint16_t wPMABufAddr
, uint16_t wNBytes
)
1321 uint32_t n
= (wNBytes
+ 1) >> 1;/* /2*/
1324 pdwVal
= (uint32_t *)(wPMABufAddr
* 2 + (uint32_t)USBx
+ 0x400);
1325 for (i
= n
; i
!= 0; i
--)
1327 *(uint16_t*)pbUsrBuf
++ = *pdwVal
++;
1336 #endif /* HAL_PCD_MODULE_ENABLED */
1345 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/