source: asp3_wo_tecs/trunk/arch/arm_m_gcc/stm32f4xx_stm32cube/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pcd.c@ 303

Last change on this file since 303 was 303, checked in by ertl-honda, 7 years ago

nucleo_f401re依存部の追加

File size: 32.9 KB
Line 
1/**
2 ******************************************************************************
3 * @file stm32f4xx_hal_pcd.c
4 * @author MCD Application Team
5 * @version V1.4.1
6 * @date 09-October-2015
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
14 *
15 @verbatim
16 ==============================================================================
17 ##### How to use this driver #####
18 ==============================================================================
19 [..]
20 The PCD HAL driver can be used as follows:
21
22 (#) Declare a PCD_HandleTypeDef handle structure, for example:
23 PCD_HandleTypeDef hpcd;
24
25 (#) Fill parameters of Init structure in HCD handle
26
27 (#) Call HAL_PCD_Init() API to initialize the PCD peripheral (Core, Device core, ...)
28
29 (#) Initialize the PCD low level resources through the HAL_PCD_MspInit() API:
30 (##) Enable the PCD/USB Low Level interface clock using
31 (+++) __HAL_RCC_USB_OTG_FS_CLK_ENABLE();
32 (+++) __HAL_RCC_USB_OTG_HS_CLK_ENABLE(); (For High Speed Mode)
33
34 (##) Initialize the related GPIO clocks
35 (##) Configure PCD pin-out
36 (##) Configure PCD NVIC interrupt
37
38 (#)Associate the Upper USB device stack to the HAL PCD Driver:
39 (##) hpcd.pData = pdev;
40
41 (#)Enable PCD transmission and reception:
42 (##) HAL_PCD_Start();
43
44 @endverbatim
45 ******************************************************************************
46 * @attention
47 *
48 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
49 *
50 * Redistribution and use in source and binary forms, with or without modification,
51 * are permitted provided that the following conditions are met:
52 * 1. Redistributions of source code must retain the above copyright notice,
53 * this list of conditions and the following disclaimer.
54 * 2. Redistributions in binary form must reproduce the above copyright notice,
55 * this list of conditions and the following disclaimer in the documentation
56 * and/or other materials provided with the distribution.
57 * 3. Neither the name of STMicroelectronics nor the names of its contributors
58 * may be used to endorse or promote products derived from this software
59 * without specific prior written permission.
60 *
61 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
62 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
63 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
64 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
65 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
66 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
67 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
68 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
69 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
70 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
71 *
72 ******************************************************************************
73 */
74
75/* Includes ------------------------------------------------------------------*/
76#include "stm32f4xx_hal.h"
77
78/** @addtogroup STM32F4xx_HAL_Driver
79 * @{
80 */
81
82/** @defgroup PCD PCD
83 * @brief PCD HAL module driver
84 * @{
85 */
86
87#ifdef HAL_PCD_MODULE_ENABLED
88#if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx) || \
89 defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) || \
90 defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F411xE) || defined(STM32F446xx) || \
91 defined(STM32F469xx) || defined(STM32F479xx)
92/* Private types -------------------------------------------------------------*/
93/* Private variables ---------------------------------------------------------*/
94/* Private constants ---------------------------------------------------------*/
95/* Private macros ------------------------------------------------------------*/
96/** @defgroup PCD_Private_Macros PCD Private Macros
97 * @{
98 */
99#define PCD_MIN(a, b) (((a) < (b)) ? (a) : (b))
100#define PCD_MAX(a, b) (((a) > (b)) ? (a) : (b))
101/**
102 * @}
103 */
104
105/* Private functions prototypes ----------------------------------------------*/
106/** @defgroup PCD_Private_Functions PCD Private Functions
107 * @{
108 */
109static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t epnum);
110/**
111 * @}
112 */
113
114/* Exported functions --------------------------------------------------------*/
115/** @defgroup PCD_Exported_Functions PCD Exported Functions
116 * @{
117 */
118
119/** @defgroup PCD_Exported_Functions_Group1 Initialization and de-initialization functions
120 * @brief Initialization and Configuration functions
121 *
122@verbatim
123 ===============================================================================
124 ##### Initialization and de-initialization functions #####
125 ===============================================================================
126 [..] This section provides functions allowing to:
127
128@endverbatim
129 * @{
130 */
131
132/**
133 * @brief Initializes the PCD according to the specified
134 * parameters in the PCD_InitTypeDef and initialize the associated handle.
135 * @param hpcd: PCD handle
136 * @retval HAL status
137 */
138HAL_StatusTypeDef HAL_PCD_Init(PCD_HandleTypeDef *hpcd)
139{
140 uint32_t i = 0;
141
142 /* Check the PCD handle allocation */
143 if(hpcd == NULL)
144 {
145 return HAL_ERROR;
146 }
147
148 /* Check the parameters */
149 assert_param(IS_PCD_ALL_INSTANCE(hpcd->Instance));
150
151 hpcd->State = HAL_PCD_STATE_BUSY;
152
153 /* Init the low level hardware : GPIO, CLOCK, NVIC... */
154 HAL_PCD_MspInit(hpcd);
155
156 /* Disable the Interrupts */
157 __HAL_PCD_DISABLE(hpcd);
158
159 /*Init the Core (common init.) */
160 USB_CoreInit(hpcd->Instance, hpcd->Init);
161
162 /* Force Device Mode*/
163 USB_SetCurrentMode(hpcd->Instance , USB_OTG_DEVICE_MODE);
164
165 /* Init endpoints structures */
166 for (i = 0; i < 15 ; i++)
167 {
168 /* Init ep structure */
169 hpcd->IN_ep[i].is_in = 1;
170 hpcd->IN_ep[i].num = i;
171 hpcd->IN_ep[i].tx_fifo_num = i;
172 /* Control until ep is activated */
173 hpcd->IN_ep[i].type = EP_TYPE_CTRL;
174 hpcd->IN_ep[i].maxpacket = 0;
175 hpcd->IN_ep[i].xfer_buff = 0;
176 hpcd->IN_ep[i].xfer_len = 0;
177 }
178
179 for (i = 0; i < 15 ; i++)
180 {
181 hpcd->OUT_ep[i].is_in = 0;
182 hpcd->OUT_ep[i].num = i;
183 hpcd->IN_ep[i].tx_fifo_num = i;
184 /* Control until ep is activated */
185 hpcd->OUT_ep[i].type = EP_TYPE_CTRL;
186 hpcd->OUT_ep[i].maxpacket = 0;
187 hpcd->OUT_ep[i].xfer_buff = 0;
188 hpcd->OUT_ep[i].xfer_len = 0;
189
190 hpcd->Instance->DIEPTXF[i] = 0;
191 }
192
193 /* Init Device */
194 USB_DevInit(hpcd->Instance, hpcd->Init);
195
196 hpcd->State= HAL_PCD_STATE_READY;
197
198#ifdef USB_OTG_GLPMCFG_LPMEN
199 /* Activate LPM */
200 if (hpcd->Init.lpm_enable == 1)
201 {
202 HAL_PCDEx_ActivateLPM(hpcd);
203 }
204#endif /* USB_OTG_GLPMCFG_LPMEN */
205
206#ifdef USB_OTG_GCCFG_BCDEN
207 /* Activate Battery charging */
208 if (hpcd->Init.battery_charging_enable ==1)
209 {
210 HAL_PCDEx_ActivateBCD(hpcd);
211 }
212#endif /* USB_OTG_GCCFG_BCDEN */
213
214 USB_DevDisconnect (hpcd->Instance);
215 return HAL_OK;
216}
217
218/**
219 * @brief DeInitializes the PCD peripheral.
220 * @param hpcd: PCD handle
221 * @retval HAL status
222 */
223HAL_StatusTypeDef HAL_PCD_DeInit(PCD_HandleTypeDef *hpcd)
224{
225 /* Check the PCD handle allocation */
226 if(hpcd == NULL)
227 {
228 return HAL_ERROR;
229 }
230
231 hpcd->State = HAL_PCD_STATE_BUSY;
232
233 /* Stop Device */
234 HAL_PCD_Stop(hpcd);
235
236 /* DeInit the low level hardware */
237 HAL_PCD_MspDeInit(hpcd);
238
239 hpcd->State = HAL_PCD_STATE_RESET;
240
241 return HAL_OK;
242}
243
244/**
245 * @brief Initializes the PCD MSP.
246 * @param hpcd: PCD handle
247 * @retval None
248 */
249__weak void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)
250{
251 /* NOTE : This function Should not be modified, when the callback is needed,
252 the HAL_PCD_MspInit could be implemented in the user file
253 */
254}
255
256/**
257 * @brief DeInitializes PCD MSP.
258 * @param hpcd: PCD handle
259 * @retval None
260 */
261__weak void HAL_PCD_MspDeInit(PCD_HandleTypeDef *hpcd)
262{
263 /* NOTE : This function Should not be modified, when the callback is needed,
264 the HAL_PCD_MspDeInit could be implemented in the user file
265 */
266}
267
268/**
269 * @}
270 */
271
272/** @defgroup PCD_Exported_Functions_Group2 Input and Output operation functions
273 * @brief Data transfers functions
274 *
275@verbatim
276 ===============================================================================
277 ##### IO operation functions #####
278 ===============================================================================
279 [..]
280 This subsection provides a set of functions allowing to manage the PCD data
281 transfers.
282
283@endverbatim
284 * @{
285 */
286
287/**
288 * @brief Start The USB OTG Device.
289 * @param hpcd: PCD handle
290 * @retval HAL status
291 */
292HAL_StatusTypeDef HAL_PCD_Start(PCD_HandleTypeDef *hpcd)
293{
294 __HAL_LOCK(hpcd);
295 USB_DevConnect (hpcd->Instance);
296 __HAL_PCD_ENABLE(hpcd);
297 __HAL_UNLOCK(hpcd);
298 return HAL_OK;
299}
300
301/**
302 * @brief Stop The USB OTG Device.
303 * @param hpcd: PCD handle
304 * @retval HAL status
305 */
306HAL_StatusTypeDef HAL_PCD_Stop(PCD_HandleTypeDef *hpcd)
307{
308 __HAL_LOCK(hpcd);
309 __HAL_PCD_DISABLE(hpcd);
310 USB_StopDevice(hpcd->Instance);
311 USB_DevDisconnect(hpcd->Instance);
312 __HAL_UNLOCK(hpcd);
313 return HAL_OK;
314}
315
316/**
317 * @brief Handles PCD interrupt request.
318 * @param hpcd: PCD handle
319 * @retval HAL status
320 */
321void HAL_PCD_IRQHandler(PCD_HandleTypeDef *hpcd)
322{
323 USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
324 uint32_t i = 0, ep_intr = 0, epint = 0, epnum = 0;
325 uint32_t fifoemptymsk = 0, temp = 0;
326 USB_OTG_EPTypeDef *ep;
327
328 /* ensure that we are in device mode */
329 if (USB_GetMode(hpcd->Instance) == USB_OTG_MODE_DEVICE)
330 {
331 /* avoid spurious interrupt */
332 if(__HAL_PCD_IS_INVALID_INTERRUPT(hpcd))
333 {
334 return;
335 }
336
337 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_MMIS))
338 {
339 /* incorrect mode, acknowledge the interrupt */
340 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_MMIS);
341 }
342
343 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_OEPINT))
344 {
345 epnum = 0;
346
347 /* Read in the device interrupt bits */
348 ep_intr = USB_ReadDevAllOutEpInterrupt(hpcd->Instance);
349
350 while ( ep_intr )
351 {
352 if (ep_intr & 0x1)
353 {
354 epint = USB_ReadDevOutEPInterrupt(hpcd->Instance, epnum);
355
356 if(( epint & USB_OTG_DOEPINT_XFRC) == USB_OTG_DOEPINT_XFRC)
357 {
358 CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_XFRC);
359
360 if(hpcd->Init.dma_enable == 1)
361 {
362 hpcd->OUT_ep[epnum].xfer_count = hpcd->OUT_ep[epnum].maxpacket- (USBx_OUTEP(epnum)->DOEPTSIZ & USB_OTG_DOEPTSIZ_XFRSIZ);
363 hpcd->OUT_ep[epnum].xfer_buff += hpcd->OUT_ep[epnum].maxpacket;
364 }
365
366 HAL_PCD_DataOutStageCallback(hpcd, epnum);
367 if(hpcd->Init.dma_enable == 1)
368 {
369 if((epnum == 0) && (hpcd->OUT_ep[epnum].xfer_len == 0))
370 {
371 /* this is ZLP, so prepare EP0 for next setup */
372 USB_EP0_OutStart(hpcd->Instance, 1, (uint8_t *)hpcd->Setup);
373 }
374 }
375 }
376
377 if(( epint & USB_OTG_DOEPINT_STUP) == USB_OTG_DOEPINT_STUP)
378 {
379 /* Inform the upper layer that a setup packet is available */
380 HAL_PCD_SetupStageCallback(hpcd);
381 CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_STUP);
382 }
383
384 if(( epint & USB_OTG_DOEPINT_OTEPDIS) == USB_OTG_DOEPINT_OTEPDIS)
385 {
386 CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_OTEPDIS);
387 }
388 }
389 epnum++;
390 ep_intr >>= 1;
391 }
392 }
393
394 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_IEPINT))
395 {
396 /* Read in the device interrupt bits */
397 ep_intr = USB_ReadDevAllInEpInterrupt(hpcd->Instance);
398
399 epnum = 0;
400
401 while ( ep_intr )
402 {
403 if (ep_intr & 0x1) /* In ITR */
404 {
405 epint = USB_ReadDevInEPInterrupt(hpcd->Instance, epnum);
406
407 if(( epint & USB_OTG_DIEPINT_XFRC) == USB_OTG_DIEPINT_XFRC)
408 {
409 fifoemptymsk = 0x1 << epnum;
410 USBx_DEVICE->DIEPEMPMSK &= ~fifoemptymsk;
411
412 CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_XFRC);
413
414 if (hpcd->Init.dma_enable == 1)
415 {
416 hpcd->IN_ep[epnum].xfer_buff += hpcd->IN_ep[epnum].maxpacket;
417 }
418
419 HAL_PCD_DataInStageCallback(hpcd, epnum);
420
421 if (hpcd->Init.dma_enable == 1)
422 {
423 /* this is ZLP, so prepare EP0 for next setup */
424 if((epnum == 0) && (hpcd->IN_ep[epnum].xfer_len == 0))
425 {
426 /* prepare to rx more setup packets */
427 USB_EP0_OutStart(hpcd->Instance, 1, (uint8_t *)hpcd->Setup);
428 }
429 }
430 }
431 if(( epint & USB_OTG_DIEPINT_TOC) == USB_OTG_DIEPINT_TOC)
432 {
433 CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_TOC);
434 }
435 if(( epint & USB_OTG_DIEPINT_ITTXFE) == USB_OTG_DIEPINT_ITTXFE)
436 {
437 CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_ITTXFE);
438 }
439 if(( epint & USB_OTG_DIEPINT_INEPNE) == USB_OTG_DIEPINT_INEPNE)
440 {
441 CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_INEPNE);
442 }
443 if(( epint & USB_OTG_DIEPINT_EPDISD) == USB_OTG_DIEPINT_EPDISD)
444 {
445 CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_EPDISD);
446 }
447 if(( epint & USB_OTG_DIEPINT_TXFE) == USB_OTG_DIEPINT_TXFE)
448 {
449 PCD_WriteEmptyTxFifo(hpcd , epnum);
450 }
451 }
452 epnum++;
453 ep_intr >>= 1;
454 }
455 }
456
457 /* Handle Resume Interrupt */
458 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_WKUINT))
459 {
460 /* Clear the Remote Wake-up Signaling */
461 USBx_DEVICE->DCTL &= ~USB_OTG_DCTL_RWUSIG;
462
463#ifdef USB_OTG_GLPMCFG_LPMEN
464 if(hpcd->LPM_State == LPM_L1)
465 {
466 hpcd->LPM_State = LPM_L0;
467 HAL_PCDEx_LPM_Callback(hpcd, PCD_LPM_L0_ACTIVE);
468 }
469 else
470#endif /* USB_OTG_GLPMCFG_LPMEN */
471 {
472 HAL_PCD_ResumeCallback(hpcd);
473 }
474
475 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_WKUINT);
476 }
477
478 /* Handle Suspend Interrupt */
479 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_USBSUSP))
480 {
481 if((USBx_DEVICE->DSTS & USB_OTG_DSTS_SUSPSTS) == USB_OTG_DSTS_SUSPSTS)
482 {
483
484 HAL_PCD_SuspendCallback(hpcd);
485 }
486 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_USBSUSP);
487 }
488
489#ifdef USB_OTG_GLPMCFG_LPMEN
490 /* Handle LPM Interrupt */
491 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_LPMINT))
492 {
493 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_LPMINT);
494 if( hpcd->LPM_State == LPM_L0)
495 {
496 hpcd->LPM_State = LPM_L1;
497 hpcd->BESL = (hpcd->Instance->GLPMCFG & USB_OTG_GLPMCFG_BESL) >>2 ;
498 HAL_PCDEx_LPM_Callback(hpcd, PCD_LPM_L1_ACTIVE);
499 }
500 else
501 {
502 HAL_PCD_SuspendCallback(hpcd);
503 }
504 }
505#endif /* USB_OTG_GLPMCFG_LPMEN */
506
507 /* Handle Reset Interrupt */
508 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_USBRST))
509 {
510 USBx_DEVICE->DCTL &= ~USB_OTG_DCTL_RWUSIG;
511 USB_FlushTxFifo(hpcd->Instance , 0 );
512
513 for (i = 0; i < hpcd->Init.dev_endpoints ; i++)
514 {
515 USBx_INEP(i)->DIEPINT = 0xFF;
516 USBx_OUTEP(i)->DOEPINT = 0xFF;
517 }
518 USBx_DEVICE->DAINT = 0xFFFFFFFF;
519 USBx_DEVICE->DAINTMSK |= 0x10001;
520
521 if(hpcd->Init.use_dedicated_ep1)
522 {
523 USBx_DEVICE->DOUTEP1MSK |= (USB_OTG_DOEPMSK_STUPM | USB_OTG_DOEPMSK_XFRCM | USB_OTG_DOEPMSK_EPDM);
524 USBx_DEVICE->DINEP1MSK |= (USB_OTG_DIEPMSK_TOM | USB_OTG_DIEPMSK_XFRCM | USB_OTG_DIEPMSK_EPDM);
525 }
526 else
527 {
528 USBx_DEVICE->DOEPMSK |= (USB_OTG_DOEPMSK_STUPM | USB_OTG_DOEPMSK_XFRCM | USB_OTG_DOEPMSK_EPDM);
529 USBx_DEVICE->DIEPMSK |= (USB_OTG_DIEPMSK_TOM | USB_OTG_DIEPMSK_XFRCM | USB_OTG_DIEPMSK_EPDM);
530 }
531
532 /* Set Default Address to 0 */
533 USBx_DEVICE->DCFG &= ~USB_OTG_DCFG_DAD;
534
535 /* setup EP0 to receive SETUP packets */
536 USB_EP0_OutStart(hpcd->Instance, hpcd->Init.dma_enable, (uint8_t *)hpcd->Setup);
537
538 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_USBRST);
539 }
540
541 /* Handle Enumeration done Interrupt */
542 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_ENUMDNE))
543 {
544 USB_ActivateSetup(hpcd->Instance);
545 hpcd->Instance->GUSBCFG &= ~USB_OTG_GUSBCFG_TRDT;
546
547 if ( USB_GetDevSpeed(hpcd->Instance) == USB_OTG_SPEED_HIGH)
548 {
549 hpcd->Init.speed = USB_OTG_SPEED_HIGH;
550 hpcd->Init.ep0_mps = USB_OTG_HS_MAX_PACKET_SIZE ;
551 hpcd->Instance->GUSBCFG |= (uint32_t)((USBD_HS_TRDT_VALUE << 10) & USB_OTG_GUSBCFG_TRDT);
552 }
553 else
554 {
555 hpcd->Init.speed = USB_OTG_SPEED_FULL;
556 hpcd->Init.ep0_mps = USB_OTG_FS_MAX_PACKET_SIZE ;
557 hpcd->Instance->GUSBCFG |= (uint32_t)((USBD_FS_TRDT_VALUE << 10) & USB_OTG_GUSBCFG_TRDT);
558 }
559
560 HAL_PCD_ResetCallback(hpcd);
561
562 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_ENUMDNE);
563 }
564
565 /* Handle RxQLevel Interrupt */
566 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_RXFLVL))
567 {
568 USB_MASK_INTERRUPT(hpcd->Instance, USB_OTG_GINTSTS_RXFLVL);
569
570 temp = USBx->GRXSTSP;
571
572 ep = &hpcd->OUT_ep[temp & USB_OTG_GRXSTSP_EPNUM];
573
574 if(((temp & USB_OTG_GRXSTSP_PKTSTS) >> 17) == STS_DATA_UPDT)
575 {
576 if((temp & USB_OTG_GRXSTSP_BCNT) != 0)
577 {
578 USB_ReadPacket(USBx, ep->xfer_buff, (temp & USB_OTG_GRXSTSP_BCNT) >> 4);
579 ep->xfer_buff += (temp & USB_OTG_GRXSTSP_BCNT) >> 4;
580 ep->xfer_count += (temp & USB_OTG_GRXSTSP_BCNT) >> 4;
581 }
582 }
583 else if (((temp & USB_OTG_GRXSTSP_PKTSTS) >> 17) == STS_SETUP_UPDT)
584 {
585 USB_ReadPacket(USBx, (uint8_t *)hpcd->Setup, 8);
586 ep->xfer_count += (temp & USB_OTG_GRXSTSP_BCNT) >> 4;
587 }
588 USB_UNMASK_INTERRUPT(hpcd->Instance, USB_OTG_GINTSTS_RXFLVL);
589 }
590
591 /* Handle SOF Interrupt */
592 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_SOF))
593 {
594 HAL_PCD_SOFCallback(hpcd);
595 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_SOF);
596 }
597
598 /* Handle Incomplete ISO IN Interrupt */
599 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_IISOIXFR))
600 {
601 HAL_PCD_ISOINIncompleteCallback(hpcd, epnum);
602 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_IISOIXFR);
603 }
604
605 /* Handle Incomplete ISO OUT Interrupt */
606 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT))
607 {
608 HAL_PCD_ISOOUTIncompleteCallback(hpcd, epnum);
609 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT);
610 }
611
612 /* Handle Connection event Interrupt */
613 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_SRQINT))
614 {
615 HAL_PCD_ConnectCallback(hpcd);
616 __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_SRQINT);
617 }
618
619 /* Handle Disconnection event Interrupt */
620 if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_OTGINT))
621 {
622 temp = hpcd->Instance->GOTGINT;
623
624 if((temp & USB_OTG_GOTGINT_SEDET) == USB_OTG_GOTGINT_SEDET)
625 {
626 HAL_PCD_DisconnectCallback(hpcd);
627 }
628 hpcd->Instance->GOTGINT |= temp;
629 }
630 }
631}
632
633/**
634 * @brief Data OUT stage callback.
635 * @param hpcd: PCD handle
636 * @param epnum: endpoint number
637 * @retval None
638 */
639 __weak void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
640{
641 /* NOTE : This function Should not be modified, when the callback is needed,
642 the HAL_PCD_DataOutStageCallback could be implemented in the user file
643 */
644}
645
646/**
647 * @brief Data IN stage callback.
648 * @param hpcd: PCD handle
649 * @param epnum: endpoint number
650 * @retval None
651 */
652 __weak void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
653{
654 /* NOTE : This function Should not be modified, when the callback is needed,
655 the HAL_PCD_DataInStageCallback could be implemented in the user file
656 */
657}
658/**
659 * @brief Setup stage callback.
660 * @param hpcd: PCD handle
661 * @retval None
662 */
663 __weak void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd)
664{
665 /* NOTE : This function Should not be modified, when the callback is needed,
666 the HAL_PCD_SetupStageCallback could be implemented in the user file
667 */
668}
669
670/**
671 * @brief USB Start Of Frame callback.
672 * @param hpcd: PCD handle
673 * @retval None
674 */
675 __weak void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
676{
677 /* NOTE : This function Should not be modified, when the callback is needed,
678 the HAL_PCD_SOFCallback could be implemented in the user file
679 */
680}
681
682/**
683 * @brief USB Reset callback.
684 * @param hpcd: PCD handle
685 * @retval None
686 */
687 __weak void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd)
688{
689 /* NOTE : This function Should not be modified, when the callback is needed,
690 the HAL_PCD_ResetCallback could be implemented in the user file
691 */
692}
693
694/**
695 * @brief Suspend event callback.
696 * @param hpcd: PCD handle
697 * @retval None
698 */
699 __weak void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
700{
701 /* NOTE : This function Should not be modified, when the callback is needed,
702 the HAL_PCD_SuspendCallback could be implemented in the user file
703 */
704}
705
706/**
707 * @brief Resume event callback.
708 * @param hpcd: PCD handle
709 * @retval None
710 */
711 __weak void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd)
712{
713 /* NOTE : This function Should not be modified, when the callback is needed,
714 the HAL_PCD_ResumeCallback could be implemented in the user file
715 */
716}
717
718/**
719 * @brief Incomplete ISO OUT callback.
720 * @param hpcd: PCD handle
721 * @param epnum: endpoint number
722 * @retval None
723 */
724 __weak void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
725{
726 /* NOTE : This function Should not be modified, when the callback is needed,
727 the HAL_PCD_ISOOUTIncompleteCallback could be implemented in the user file
728 */
729}
730
731/**
732 * @brief Incomplete ISO IN callback.
733 * @param hpcd: PCD handle
734 * @param epnum: endpoint number
735 * @retval None
736 */
737 __weak void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
738{
739 /* NOTE : This function Should not be modified, when the callback is needed,
740 the HAL_PCD_ISOINIncompleteCallback could be implemented in the user file
741 */
742}
743
744/**
745 * @brief Connection event callback.
746 * @param hpcd: PCD handle
747 * @retval None
748 */
749 __weak void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)
750{
751 /* NOTE : This function Should not be modified, when the callback is needed,
752 the HAL_PCD_ConnectCallback could be implemented in the user file
753 */
754}
755
756/**
757 * @brief Disconnection event callback.
758 * @param hpcd: PCD handle
759 * @retval None
760 */
761 __weak void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
762{
763 /* NOTE : This function Should not be modified, when the callback is needed,
764 the HAL_PCD_DisconnectCallback could be implemented in the user file
765 */
766}
767
768/**
769 * @}
770 */
771
772/** @defgroup PCD_Exported_Functions_Group3 Peripheral Control functions
773 * @brief management functions
774 *
775@verbatim
776 ===============================================================================
777 ##### Peripheral Control functions #####
778 ===============================================================================
779 [..]
780 This subsection provides a set of functions allowing to control the PCD data
781 transfers.
782
783@endverbatim
784 * @{
785 */
786
787/**
788 * @brief Connect the USB device.
789 * @param hpcd: PCD handle
790 * @retval HAL status
791 */
792HAL_StatusTypeDef HAL_PCD_DevConnect(PCD_HandleTypeDef *hpcd)
793{
794 __HAL_LOCK(hpcd);
795 USB_DevConnect(hpcd->Instance);
796 __HAL_UNLOCK(hpcd);
797 return HAL_OK;
798}
799
800/**
801 * @brief Disconnect the USB device.
802 * @param hpcd: PCD handle
803 * @retval HAL status
804 */
805HAL_StatusTypeDef HAL_PCD_DevDisconnect(PCD_HandleTypeDef *hpcd)
806{
807 __HAL_LOCK(hpcd);
808 USB_DevDisconnect(hpcd->Instance);
809 __HAL_UNLOCK(hpcd);
810 return HAL_OK;
811}
812
813/**
814 * @brief Set the USB Device address.
815 * @param hpcd: PCD handle
816 * @param address: new device address
817 * @retval HAL status
818 */
819HAL_StatusTypeDef HAL_PCD_SetAddress(PCD_HandleTypeDef *hpcd, uint8_t address)
820{
821 __HAL_LOCK(hpcd);
822 USB_SetDevAddress(hpcd->Instance, address);
823 __HAL_UNLOCK(hpcd);
824 return HAL_OK;
825}
826/**
827 * @brief Open and configure an endpoint.
828 * @param hpcd: PCD handle
829 * @param ep_addr: endpoint address
830 * @param ep_mps: endpoint max packet size
831 * @param ep_type: endpoint type
832 * @retval HAL status
833 */
834HAL_StatusTypeDef HAL_PCD_EP_Open(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint16_t ep_mps, uint8_t ep_type)
835{
836 HAL_StatusTypeDef ret = HAL_OK;
837 USB_OTG_EPTypeDef *ep;
838
839 if ((ep_addr & 0x80) == 0x80)
840 {
841 ep = &hpcd->IN_ep[ep_addr & 0x7F];
842 }
843 else
844 {
845 ep = &hpcd->OUT_ep[ep_addr & 0x7F];
846 }
847 ep->num = ep_addr & 0x7F;
848
849 ep->is_in = (0x80 & ep_addr) != 0;
850 ep->maxpacket = ep_mps;
851 ep->type = ep_type;
852 if (ep->is_in)
853 {
854 /* Assign a Tx FIFO */
855 ep->tx_fifo_num = ep->num;
856 }
857 /* Set initial data PID. */
858 if (ep_type == EP_TYPE_BULK )
859 {
860 ep->data_pid_start = 0;
861 }
862
863 __HAL_LOCK(hpcd);
864 USB_ActivateEndpoint(hpcd->Instance , ep);
865 __HAL_UNLOCK(hpcd);
866 return ret;
867}
868
869
870/**
871 * @brief Deactivate an endpoint.
872 * @param hpcd: PCD handle
873 * @param ep_addr: endpoint address
874 * @retval HAL status
875 */
876HAL_StatusTypeDef HAL_PCD_EP_Close(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
877{
878 USB_OTG_EPTypeDef *ep;
879
880 if ((ep_addr & 0x80) == 0x80)
881 {
882 ep = &hpcd->IN_ep[ep_addr & 0x7F];
883 }
884 else
885 {
886 ep = &hpcd->OUT_ep[ep_addr & 0x7F];
887 }
888 ep->num = ep_addr & 0x7F;
889
890 ep->is_in = (0x80 & ep_addr) != 0;
891
892 __HAL_LOCK(hpcd);
893 USB_DeactivateEndpoint(hpcd->Instance , ep);
894 __HAL_UNLOCK(hpcd);
895 return HAL_OK;
896}
897
898
899/**
900 * @brief Receive an amount of data.
901 * @param hpcd: PCD handle
902 * @param ep_addr: endpoint address
903 * @param pBuf: pointer to the reception buffer
904 * @param len: amount of data to be received
905 * @retval HAL status
906 */
907HAL_StatusTypeDef HAL_PCD_EP_Receive(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len)
908{
909 USB_OTG_EPTypeDef *ep;
910
911 ep = &hpcd->OUT_ep[ep_addr & 0x7F];
912
913 /*setup and start the Xfer */
914 ep->xfer_buff = pBuf;
915 ep->xfer_len = len;
916 ep->xfer_count = 0;
917 ep->is_in = 0;
918 ep->num = ep_addr & 0x7F;
919
920 if (hpcd->Init.dma_enable == 1)
921 {
922 ep->dma_addr = (uint32_t)pBuf;
923 }
924
925 __HAL_LOCK(hpcd);
926
927 if ((ep_addr & 0x7F) == 0 )
928 {
929 USB_EP0StartXfer(hpcd->Instance , ep, hpcd->Init.dma_enable);
930 }
931 else
932 {
933 USB_EPStartXfer(hpcd->Instance , ep, hpcd->Init.dma_enable);
934 }
935 __HAL_UNLOCK(hpcd);
936
937 return HAL_OK;
938}
939
940/**
941 * @brief Get Received Data Size.
942 * @param hpcd: PCD handle
943 * @param ep_addr: endpoint address
944 * @retval Data Size
945 */
946uint16_t HAL_PCD_EP_GetRxCount(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
947{
948 return hpcd->OUT_ep[ep_addr & 0x7F].xfer_count;
949}
950/**
951 * @brief Send an amount of data.
952 * @param hpcd: PCD handle
953 * @param ep_addr: endpoint address
954 * @param pBuf: pointer to the transmission buffer
955 * @param len: amount of data to be sent
956 * @retval HAL status
957 */
958HAL_StatusTypeDef HAL_PCD_EP_Transmit(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len)
959{
960 USB_OTG_EPTypeDef *ep;
961
962 ep = &hpcd->IN_ep[ep_addr & 0x7F];
963
964 /*setup and start the Xfer */
965 ep->xfer_buff = pBuf;
966 ep->xfer_len = len;
967 ep->xfer_count = 0;
968 ep->is_in = 1;
969 ep->num = ep_addr & 0x7F;
970
971 if (hpcd->Init.dma_enable == 1)
972 {
973 ep->dma_addr = (uint32_t)pBuf;
974 }
975
976 __HAL_LOCK(hpcd);
977
978 if ((ep_addr & 0x7F) == 0 )
979 {
980 USB_EP0StartXfer(hpcd->Instance , ep, hpcd->Init.dma_enable);
981 }
982 else
983 {
984 USB_EPStartXfer(hpcd->Instance , ep, hpcd->Init.dma_enable);
985 }
986
987 __HAL_UNLOCK(hpcd);
988
989 return HAL_OK;
990}
991
992/**
993 * @brief Set a STALL condition over an endpoint.
994 * @param hpcd: PCD handle
995 * @param ep_addr: endpoint address
996 * @retval HAL status
997 */
998HAL_StatusTypeDef HAL_PCD_EP_SetStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
999{
1000 USB_OTG_EPTypeDef *ep;
1001
1002 if ((0x80 & ep_addr) == 0x80)
1003 {
1004 ep = &hpcd->IN_ep[ep_addr & 0x7F];
1005 }
1006 else
1007 {
1008 ep = &hpcd->OUT_ep[ep_addr];
1009 }
1010
1011 ep->is_stall = 1;
1012 ep->num = ep_addr & 0x7F;
1013 ep->is_in = ((ep_addr & 0x80) == 0x80);
1014
1015
1016 __HAL_LOCK(hpcd);
1017 USB_EPSetStall(hpcd->Instance , ep);
1018 if((ep_addr & 0x7F) == 0)
1019 {
1020 USB_EP0_OutStart(hpcd->Instance, hpcd->Init.dma_enable, (uint8_t *)hpcd->Setup);
1021 }
1022 __HAL_UNLOCK(hpcd);
1023
1024 return HAL_OK;
1025}
1026
1027/**
1028 * @brief Clear a STALL condition over in an endpoint.
1029 * @param hpcd: PCD handle
1030 * @param ep_addr: endpoint address
1031 * @retval HAL status
1032 */
1033HAL_StatusTypeDef HAL_PCD_EP_ClrStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
1034{
1035 USB_OTG_EPTypeDef *ep;
1036
1037 if ((0x80 & ep_addr) == 0x80)
1038 {
1039 ep = &hpcd->IN_ep[ep_addr & 0x7F];
1040 }
1041 else
1042 {
1043 ep = &hpcd->OUT_ep[ep_addr];
1044 }
1045
1046 ep->is_stall = 0;
1047 ep->num = ep_addr & 0x7F;
1048 ep->is_in = ((ep_addr & 0x80) == 0x80);
1049
1050 __HAL_LOCK(hpcd);
1051 USB_EPClearStall(hpcd->Instance , ep);
1052 __HAL_UNLOCK(hpcd);
1053
1054 return HAL_OK;
1055}
1056
1057/**
1058 * @brief Flush an endpoint.
1059 * @param hpcd: PCD handle
1060 * @param ep_addr: endpoint address
1061 * @retval HAL status
1062 */
1063HAL_StatusTypeDef HAL_PCD_EP_Flush(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
1064{
1065 __HAL_LOCK(hpcd);
1066
1067 if ((ep_addr & 0x80) == 0x80)
1068 {
1069 USB_FlushTxFifo(hpcd->Instance, ep_addr & 0x7F);
1070 }
1071 else
1072 {
1073 USB_FlushRxFifo(hpcd->Instance);
1074 }
1075
1076 __HAL_UNLOCK(hpcd);
1077
1078 return HAL_OK;
1079}
1080
1081/**
1082 * @brief Activate remote wakeup signalling.
1083 * @param hpcd: PCD handle
1084 * @retval HAL status
1085 */
1086HAL_StatusTypeDef HAL_PCD_ActivateRemoteWakeup(PCD_HandleTypeDef *hpcd)
1087{
1088 USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
1089
1090 if((USBx_DEVICE->DSTS & USB_OTG_DSTS_SUSPSTS) == USB_OTG_DSTS_SUSPSTS)
1091 {
1092 /* Activate Remote wakeup signaling */
1093 USBx_DEVICE->DCTL |= USB_OTG_DCTL_RWUSIG;
1094 }
1095 return HAL_OK;
1096}
1097
1098/**
1099 * @brief De-activate remote wakeup signalling.
1100 * @param hpcd: PCD handle
1101 * @retval HAL status
1102 */
1103HAL_StatusTypeDef HAL_PCD_DeActivateRemoteWakeup(PCD_HandleTypeDef *hpcd)
1104{
1105 USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
1106
1107 /* De-activate Remote wakeup signaling */
1108 USBx_DEVICE->DCTL &= ~(USB_OTG_DCTL_RWUSIG);
1109 return HAL_OK;
1110}
1111/**
1112 * @}
1113 */
1114
1115/** @defgroup PCD_Exported_Functions_Group4 Peripheral State functions
1116 * @brief Peripheral State functions
1117 *
1118@verbatim
1119 ===============================================================================
1120 ##### Peripheral State functions #####
1121 ===============================================================================
1122 [..]
1123 This subsection permits to get in run-time the status of the peripheral
1124 and the data flow.
1125
1126@endverbatim
1127 * @{
1128 */
1129
1130/**
1131 * @brief Return the PCD handle state.
1132 * @param hpcd: PCD handle
1133 * @retval HAL state
1134 */
1135PCD_StateTypeDef HAL_PCD_GetState(PCD_HandleTypeDef *hpcd)
1136{
1137 return hpcd->State;
1138}
1139/**
1140 * @}
1141 */
1142
1143/**
1144 * @}
1145 */
1146
1147/* Private functions ---------------------------------------------------------*/
1148/** @addtogroup PCD_Private_Functions
1149 * @{
1150 */
1151
1152/**
1153 * @brief Check FIFO for the next packet to be loaded.
1154 * @param hpcd: PCD handle
1155 * @param epnum : endpoint number
1156 * @retval HAL status
1157 */
1158static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t epnum)
1159{
1160 USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
1161 USB_OTG_EPTypeDef *ep;
1162 int32_t len = 0;
1163 uint32_t len32b;
1164 uint32_t fifoemptymsk = 0;
1165
1166 ep = &hpcd->IN_ep[epnum];
1167 len = ep->xfer_len - ep->xfer_count;
1168
1169 if (len > ep->maxpacket)
1170 {
1171 len = ep->maxpacket;
1172 }
1173
1174
1175 len32b = (len + 3) / 4;
1176
1177 while ( (USBx_INEP(epnum)->DTXFSTS & USB_OTG_DTXFSTS_INEPTFSAV) > len32b &&
1178 ep->xfer_count < ep->xfer_len &&
1179 ep->xfer_len != 0)
1180 {
1181 /* Write the FIFO */
1182 len = ep->xfer_len - ep->xfer_count;
1183
1184 if (len > ep->maxpacket)
1185 {
1186 len = ep->maxpacket;
1187 }
1188 len32b = (len + 3) / 4;
1189
1190 USB_WritePacket(USBx, ep->xfer_buff, epnum, len, hpcd->Init.dma_enable);
1191
1192 ep->xfer_buff += len;
1193 ep->xfer_count += len;
1194 }
1195
1196 if(len <= 0)
1197 {
1198 fifoemptymsk = 0x1 << epnum;
1199 USBx_DEVICE->DIEPEMPMSK &= ~fifoemptymsk;
1200
1201 }
1202
1203 return HAL_OK;
1204}
1205
1206/**
1207 * @}
1208 */
1209#endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx ||
1210 STM32F401xC || STM32F401xE || STM32F411xE || STM32F446xx || STM32F469xx || STM32F479xx */
1211#endif /* HAL_PCD_MODULE_ENABLED */
1212/**
1213 * @}
1214 */
1215
1216/**
1217 * @}
1218 */
1219
1220/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Note: See TracBrowser for help on using the repository browser.