1 /**
2 ******************************************************************************
3 * @file : usbd_conf.c
4 * @version : v2.0_Cube
5 * @brief : This file implements the board support package for the USB device library
6 ******************************************************************************
7 * This notice applies to any and all portions of this file
8 * that are not between comment pairs USER CODE BEGIN and
9 * USER CODE END. Other portions of this file, whether
10 * inserted by the user or by software development tools
11 * are owned by their respective copyright owners.
12 *
13 * Copyright (c) 2018 STMicroelectronics International N.V.
14 * All rights reserved.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted, provided that the following conditions are met:
18 *
19 * 1. Redistribution of source code must retain the above copyright notice,
20 * this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright notice,
22 * this list of conditions and the following disclaimer in the documentation
23 * and/or other materials provided with the distribution.
24 * 3. Neither the name of STMicroelectronics nor the names of other
25 * contributors to this software may be used to endorse or promote products
26 * derived from this software without specific written permission.
27 * 4. This software, including modifications and/or derivative works of this
28 * software, must execute solely and exclusively on microcontroller or
29 * microprocessor devices manufactured by or for STMicroelectronics.
30 * 5. Redistribution and use of this software other than as permitted under
31 * this license is void and will automatically terminate your rights under
32 * this license.
33 *
34 * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
35 * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
36 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
37 * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
38 * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
39 * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
40 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
42 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
43 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
44 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
45 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46 *
47 ******************************************************************************
48 */
49
50 /* Includes ------------------------------------------------------------------*/
51 #include "stm32l4xx.h"
52 #include "stm32l4xx_hal.h"
53 #include "usbd_def.h"
54 #include "usbd_core.h"
55 #include "usbd_cdc.h"
56
57 /* USER CODE BEGIN Includes */
58
59 /* USER CODE END Includes */
60
61 /* Private typedef -----------------------------------------------------------*/
62 /* Private define ------------------------------------------------------------*/
63 /* Private macro -------------------------------------------------------------*/
64
65 /* USER CODE BEGIN PV */
66 /* Private variables ---------------------------------------------------------*/
67
68 /* USER CODE END PV */
69
70 PCD_HandleTypeDef hpcd_USB_OTG_FS;
71 void _Error_Handler(char * file, int line);
72
73 /* USER CODE BEGIN 0 */
74
75 /* USER CODE END 0 */
76
77 /* Exported function prototypes ----------------------------------------------*/
78 extern USBD_StatusTypeDef USBD_LL_BatteryCharging(USBD_HandleTypeDef *pdev);
79
80 /* USER CODE BEGIN PFP */
81 /* Private function prototypes -----------------------------------------------*/
82
83 /* USER CODE END PFP */
84
85 /* Private functions ---------------------------------------------------------*/
86
87 /* USER CODE BEGIN 1 */
88 static void SystemClockConfig_Resume(void);
89
90 /* USER CODE END 1 */
91
92 void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state);
93 extern void SystemClock_Config(void);
94
95 /*******************************************************************************
96 LL Driver Callbacks (PCD -> USB Device Library)
97 *******************************************************************************/
98 /* MSP Init */
99
HAL_PCD_MspInit(PCD_HandleTypeDef * pcdHandle)100 void HAL_PCD_MspInit(PCD_HandleTypeDef* pcdHandle)
101 {
102 GPIO_InitTypeDef GPIO_InitStruct;
103 if(pcdHandle->Instance==USB_OTG_FS)
104 {
105 /* USER CODE BEGIN USB_OTG_FS_MspInit 0 */
106
107 /* USER CODE END USB_OTG_FS_MspInit 0 */
108
109 /**USB_OTG_FS GPIO Configuration
110 PA11 ------> USB_OTG_FS_DM
111 PA12 ------> USB_OTG_FS_DP
112 */
113 GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12;
114 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
115 GPIO_InitStruct.Pull = GPIO_NOPULL;
116 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
117 GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS;
118 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
119
120 /* Peripheral clock enable */
121 __HAL_RCC_USB_OTG_FS_CLK_ENABLE();
122
123 /* Enable VDDUSB */
124 if(__HAL_RCC_PWR_IS_CLK_DISABLED())
125 {
126 __HAL_RCC_PWR_CLK_ENABLE();
127 HAL_PWREx_EnableVddUSB();
128 __HAL_RCC_PWR_CLK_DISABLE();
129 }
130 else
131 {
132 HAL_PWREx_EnableVddUSB();
133 }
134
135 /* Peripheral interrupt init */
136 HAL_NVIC_SetPriority(OTG_FS_IRQn, 0, 0);
137 HAL_NVIC_EnableIRQ(OTG_FS_IRQn);
138 /* USER CODE BEGIN USB_OTG_FS_MspInit 1 */
139
140 /* USER CODE END USB_OTG_FS_MspInit 1 */
141 }
142 }
143
HAL_PCD_MspDeInit(PCD_HandleTypeDef * pcdHandle)144 void HAL_PCD_MspDeInit(PCD_HandleTypeDef* pcdHandle)
145 {
146 if(pcdHandle->Instance==USB_OTG_FS)
147 {
148 /* USER CODE BEGIN USB_OTG_FS_MspDeInit 0 */
149
150 /* USER CODE END USB_OTG_FS_MspDeInit 0 */
151 /* Peripheral clock disable */
152 __HAL_RCC_USB_OTG_FS_CLK_DISABLE();
153
154 /**USB_OTG_FS GPIO Configuration
155 PA11 ------> USB_OTG_FS_DM
156 PA12 ------> USB_OTG_FS_DP
157 */
158 HAL_GPIO_DeInit(GPIOA, GPIO_PIN_11|GPIO_PIN_12);
159
160 /* Disable VDDUSB */
161 if(__HAL_RCC_PWR_IS_CLK_DISABLED())
162 {
163 __HAL_RCC_PWR_CLK_ENABLE();
164 HAL_PWREx_DisableVddUSB();
165 __HAL_RCC_PWR_CLK_DISABLE();
166 }
167 else
168 {
169 HAL_PWREx_DisableVddUSB();
170 }
171
172 /* Peripheral interrupt Deinit*/
173 HAL_NVIC_DisableIRQ(OTG_FS_IRQn);
174
175 /* USER CODE BEGIN USB_OTG_FS_MspDeInit 1 */
176
177 /* USER CODE END USB_OTG_FS_MspDeInit 1 */
178 }
179 }
180
181 /**
182 * @brief Setup stage callback
183 * @param hpcd: PCD handle
184 * @retval None
185 */
HAL_PCD_SetupStageCallback(PCD_HandleTypeDef * hpcd)186 void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd)
187 {
188 USBD_LL_SetupStage((USBD_HandleTypeDef*)hpcd->pData, (uint8_t *)hpcd->Setup);
189 }
190
191 /**
192 * @brief Data Out stage callback.
193 * @param hpcd: PCD handle
194 * @param epnum: Endpoint number
195 * @retval None
196 */
HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef * hpcd,uint8_t epnum)197 void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
198 {
199 USBD_LL_DataOutStage((USBD_HandleTypeDef*)hpcd->pData, epnum, hpcd->OUT_ep[epnum].xfer_buff);
200 }
201
202 /**
203 * @brief Data In stage callback.
204 * @param hpcd: PCD handle
205 * @param epnum: Endpoint number
206 * @retval None
207 */
HAL_PCD_DataInStageCallback(PCD_HandleTypeDef * hpcd,uint8_t epnum)208 void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
209 {
210 USBD_LL_DataInStage((USBD_HandleTypeDef*)hpcd->pData, epnum, hpcd->IN_ep[epnum].xfer_buff);
211 }
212
213 /**
214 * @brief SOF callback.
215 * @param hpcd: PCD handle
216 * @retval None
217 */
HAL_PCD_SOFCallback(PCD_HandleTypeDef * hpcd)218 void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
219 {
220 USBD_LL_SOF((USBD_HandleTypeDef*)hpcd->pData);
221 }
222
223 /**
224 * @brief Reset callback.
225 * @param hpcd: PCD handle
226 * @retval None
227 */
HAL_PCD_ResetCallback(PCD_HandleTypeDef * hpcd)228 void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd)
229 {
230 USBD_SpeedTypeDef speed = USBD_SPEED_FULL;
231
232 /* Set USB current speed. */
233 switch (hpcd->Init.speed)
234 {
235 case PCD_SPEED_FULL:
236 speed = USBD_SPEED_FULL;
237 break;
238
239 default:
240 speed = USBD_SPEED_FULL;
241 break;
242 }
243 USBD_LL_SetSpeed((USBD_HandleTypeDef*)hpcd->pData, speed);
244
245 /* Reset Device. */
246 USBD_LL_Reset((USBD_HandleTypeDef*)hpcd->pData);
247 }
248
249 /**
250 * @brief Suspend callback.
251 * When Low power mode is enabled the debug cannot be used (IAR, Keil doesn't support it)
252 * @param hpcd: PCD handle
253 * @retval None
254 */
HAL_PCD_SuspendCallback(PCD_HandleTypeDef * hpcd)255 void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
256 {
257 __HAL_PCD_GATE_PHYCLOCK(hpcd);
258 /* Inform USB library that core enters in suspend Mode. */
259 USBD_LL_Suspend((USBD_HandleTypeDef*)hpcd->pData);
260 /* Enter in STOP mode. */
261 /* USER CODE BEGIN 2 */
262 if (hpcd->Init.low_power_enable)
263 {
264 /* Set SLEEPDEEP bit and SleepOnExit of Cortex System Control Register. */
265 SCB->SCR |= (uint32_t)((uint32_t)(SCB_SCR_SLEEPDEEP_Msk | SCB_SCR_SLEEPONEXIT_Msk));
266 }
267 /* USER CODE END 2 */
268 }
269
270 /**
271 * @brief Resume callback.
272 * When Low power mode is enabled the debug cannot be used (IAR, Keil doesn't support it)
273 * @param hpcd: PCD handle
274 * @retval None
275 */
HAL_PCD_ResumeCallback(PCD_HandleTypeDef * hpcd)276 void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd)
277 {
278 __HAL_PCD_UNGATE_PHYCLOCK(hpcd);
279
280 /* USER CODE BEGIN 3 */
281 if (hpcd->Init.low_power_enable)
282 {
283 /* Reset SLEEPDEEP bit of Cortex System Control Register. */
284 SCB->SCR &= (uint32_t)~((uint32_t)(SCB_SCR_SLEEPDEEP_Msk | SCB_SCR_SLEEPONEXIT_Msk));
285 SystemClockConfig_Resume();
286 }
287 /* USER CODE END 3 */
288 USBD_LL_Resume((USBD_HandleTypeDef*)hpcd->pData);
289 }
290
291 /**
292 * @brief ISOOUTIncomplete callback.
293 * @param hpcd: PCD handle
294 * @param epnum: Endpoint number
295 * @retval None
296 */
HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef * hpcd,uint8_t epnum)297 void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
298 {
299 USBD_LL_IsoOUTIncomplete((USBD_HandleTypeDef*)hpcd->pData, epnum);
300 }
301
302 /**
303 * @brief ISOINIncomplete callback.
304 * @param hpcd: PCD handle
305 * @param epnum: Endpoint number
306 * @retval None
307 */
HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef * hpcd,uint8_t epnum)308 void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
309 {
310 USBD_LL_IsoINIncomplete((USBD_HandleTypeDef*)hpcd->pData, epnum);
311 }
312
313 /**
314 * @brief Connect callback.
315 * @param hpcd: PCD handle
316 * @retval None
317 */
HAL_PCD_ConnectCallback(PCD_HandleTypeDef * hpcd)318 void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)
319 {
320 USBD_LL_DevConnected((USBD_HandleTypeDef*)hpcd->pData);
321 }
322
323 /**
324 * @brief Disconnect callback.
325 * @param hpcd: PCD handle
326 * @retval None
327 */
HAL_PCD_DisconnectCallback(PCD_HandleTypeDef * hpcd)328 void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
329 {
330 USBD_LL_DevDisconnected((USBD_HandleTypeDef*)hpcd->pData);
331 }
332
333 /*******************************************************************************
334 LL Driver Interface (USB Device Library --> PCD)
335 *******************************************************************************/
336
337 /**
338 * @brief Initializes the low level portion of the device driver.
339 * @param pdev: Device handle
340 * @retval USBD status
341 */
USBD_LL_Init(USBD_HandleTypeDef * pdev)342 USBD_StatusTypeDef USBD_LL_Init(USBD_HandleTypeDef *pdev)
343 {
344 /* Init USB Ip. */
345 if (pdev->id == DEVICE_FS) {
346 /* Enable USB power on Pwrctrl CR2 register. */
347 /* Link the driver to the stack. */
348 hpcd_USB_OTG_FS.pData = pdev;
349 pdev->pData = &hpcd_USB_OTG_FS;
350
351 hpcd_USB_OTG_FS.Instance = USB_OTG_FS;
352 hpcd_USB_OTG_FS.Init.dev_endpoints = 6;
353 hpcd_USB_OTG_FS.Init.speed = PCD_SPEED_FULL;
354 hpcd_USB_OTG_FS.Init.ep0_mps = DEP0CTL_MPS_64;
355 hpcd_USB_OTG_FS.Init.phy_itface = PCD_PHY_EMBEDDED;
356 hpcd_USB_OTG_FS.Init.Sof_enable = DISABLE;
357 hpcd_USB_OTG_FS.Init.low_power_enable = DISABLE;
358 hpcd_USB_OTG_FS.Init.lpm_enable = DISABLE;
359 hpcd_USB_OTG_FS.Init.battery_charging_enable = DISABLE;
360 hpcd_USB_OTG_FS.Init.use_dedicated_ep1 = DISABLE;
361 hpcd_USB_OTG_FS.Init.vbus_sensing_enable = DISABLE;
362 if (HAL_PCD_Init(&hpcd_USB_OTG_FS) != HAL_OK)
363 {
364 _Error_Handler(__FILE__, __LINE__);
365 }
366
367 HAL_PCDEx_SetRxFiFo(&hpcd_USB_OTG_FS, 0x80);
368 HAL_PCDEx_SetTxFiFo(&hpcd_USB_OTG_FS, 0, 0x40);
369 HAL_PCDEx_SetTxFiFo(&hpcd_USB_OTG_FS, 1, 0x80);
370 }
371 return USBD_OK;
372 }
373
374 /**
375 * @brief De-Initializes the low level portion of the device driver.
376 * @param pdev: Device handle
377 * @retval USBD status
378 */
USBD_LL_DeInit(USBD_HandleTypeDef * pdev)379 USBD_StatusTypeDef USBD_LL_DeInit(USBD_HandleTypeDef *pdev)
380 {
381 HAL_StatusTypeDef hal_status = HAL_OK;
382 USBD_StatusTypeDef usb_status = USBD_OK;
383
384 hal_status = HAL_PCD_DeInit(pdev->pData);
385
386 switch (hal_status) {
387 case HAL_OK :
388 usb_status = USBD_OK;
389 break;
390 case HAL_ERROR :
391 usb_status = USBD_FAIL;
392 break;
393 case HAL_BUSY :
394 usb_status = USBD_BUSY;
395 break;
396 case HAL_TIMEOUT :
397 usb_status = USBD_FAIL;
398 break;
399 default :
400 usb_status = USBD_FAIL;
401 break;
402 }
403 return usb_status;
404 }
405
406 /**
407 * @brief Starts the low level portion of the device driver.
408 * @param pdev: Device handle
409 * @retval USBD status
410 */
USBD_LL_Start(USBD_HandleTypeDef * pdev)411 USBD_StatusTypeDef USBD_LL_Start(USBD_HandleTypeDef *pdev)
412 {
413 HAL_StatusTypeDef hal_status = HAL_OK;
414 USBD_StatusTypeDef usb_status = USBD_OK;
415
416 hal_status = HAL_PCD_Start(pdev->pData);
417
418 switch (hal_status) {
419 case HAL_OK :
420 usb_status = USBD_OK;
421 break;
422 case HAL_ERROR :
423 usb_status = USBD_FAIL;
424 break;
425 case HAL_BUSY :
426 usb_status = USBD_BUSY;
427 break;
428 case HAL_TIMEOUT :
429 usb_status = USBD_FAIL;
430 break;
431 default :
432 usb_status = USBD_FAIL;
433 break;
434 }
435 return usb_status;
436 }
437
438 /**
439 * @brief Stops the low level portion of the device driver.
440 * @param pdev: Device handle
441 * @retval USBD status
442 */
USBD_LL_Stop(USBD_HandleTypeDef * pdev)443 USBD_StatusTypeDef USBD_LL_Stop(USBD_HandleTypeDef *pdev)
444 {
445 HAL_StatusTypeDef hal_status = HAL_OK;
446 USBD_StatusTypeDef usb_status = USBD_OK;
447
448 hal_status = HAL_PCD_Stop(pdev->pData);
449
450 switch (hal_status) {
451 case HAL_OK :
452 usb_status = USBD_OK;
453 break;
454 case HAL_ERROR :
455 usb_status = USBD_FAIL;
456 break;
457 case HAL_BUSY :
458 usb_status = USBD_BUSY;
459 break;
460 case HAL_TIMEOUT :
461 usb_status = USBD_FAIL;
462 break;
463 default :
464 usb_status = USBD_FAIL;
465 break;
466 }
467 return usb_status;
468 }
469
470 /**
471 * @brief Opens an endpoint of the low level driver.
472 * @param pdev: Device handle
473 * @param ep_addr: Endpoint number
474 * @param ep_type: Endpoint type
475 * @param ep_mps: Endpoint max packet size
476 * @retval USBD status
477 */
USBD_LL_OpenEP(USBD_HandleTypeDef * pdev,uint8_t ep_addr,uint8_t ep_type,uint16_t ep_mps)478 USBD_StatusTypeDef USBD_LL_OpenEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr, uint8_t ep_type, uint16_t ep_mps)
479 {
480 HAL_StatusTypeDef hal_status = HAL_OK;
481 USBD_StatusTypeDef usb_status = USBD_OK;
482
483 hal_status = HAL_PCD_EP_Open(pdev->pData, ep_addr, ep_mps, ep_type);
484
485 switch (hal_status) {
486 case HAL_OK :
487 usb_status = USBD_OK;
488 break;
489 case HAL_ERROR :
490 usb_status = USBD_FAIL;
491 break;
492 case HAL_BUSY :
493 usb_status = USBD_BUSY;
494 break;
495 case HAL_TIMEOUT :
496 usb_status = USBD_FAIL;
497 break;
498 default :
499 usb_status = USBD_FAIL;
500 break;
501 }
502 return usb_status;
503 }
504
505 /**
506 * @brief Closes an endpoint of the low level driver.
507 * @param pdev: Device handle
508 * @param ep_addr: Endpoint number
509 * @retval USBD status
510 */
USBD_LL_CloseEP(USBD_HandleTypeDef * pdev,uint8_t ep_addr)511 USBD_StatusTypeDef USBD_LL_CloseEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
512 {
513 HAL_StatusTypeDef hal_status = HAL_OK;
514 USBD_StatusTypeDef usb_status = USBD_OK;
515
516 hal_status = HAL_PCD_EP_Close(pdev->pData, ep_addr);
517
518 switch (hal_status) {
519 case HAL_OK :
520 usb_status = USBD_OK;
521 break;
522 case HAL_ERROR :
523 usb_status = USBD_FAIL;
524 break;
525 case HAL_BUSY :
526 usb_status = USBD_BUSY;
527 break;
528 case HAL_TIMEOUT :
529 usb_status = USBD_FAIL;
530 break;
531 default :
532 usb_status = USBD_FAIL;
533 break;
534 }
535 return usb_status;
536 }
537
538 /**
539 * @brief Flushes an endpoint of the Low Level Driver.
540 * @param pdev: Device handle
541 * @param ep_addr: Endpoint number
542 * @retval USBD status
543 */
USBD_LL_FlushEP(USBD_HandleTypeDef * pdev,uint8_t ep_addr)544 USBD_StatusTypeDef USBD_LL_FlushEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
545 {
546 HAL_StatusTypeDef hal_status = HAL_OK;
547 USBD_StatusTypeDef usb_status = USBD_OK;
548
549 hal_status = HAL_PCD_EP_Flush(pdev->pData, ep_addr);
550
551 switch (hal_status) {
552 case HAL_OK :
553 usb_status = USBD_OK;
554 break;
555 case HAL_ERROR :
556 usb_status = USBD_FAIL;
557 break;
558 case HAL_BUSY :
559 usb_status = USBD_BUSY;
560 break;
561 case HAL_TIMEOUT :
562 usb_status = USBD_FAIL;
563 break;
564 default :
565 usb_status = USBD_FAIL;
566 break;
567 }
568 return usb_status;
569 }
570
571 /**
572 * @brief Sets a Stall condition on an endpoint of the Low Level Driver.
573 * @param pdev: Device handle
574 * @param ep_addr: Endpoint number
575 * @retval USBD status
576 */
USBD_LL_StallEP(USBD_HandleTypeDef * pdev,uint8_t ep_addr)577 USBD_StatusTypeDef USBD_LL_StallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
578 {
579 HAL_StatusTypeDef hal_status = HAL_OK;
580 USBD_StatusTypeDef usb_status = USBD_OK;
581
582 hal_status = HAL_PCD_EP_SetStall(pdev->pData, ep_addr);
583
584 switch (hal_status) {
585 case HAL_OK :
586 usb_status = USBD_OK;
587 break;
588 case HAL_ERROR :
589 usb_status = USBD_FAIL;
590 break;
591 case HAL_BUSY :
592 usb_status = USBD_BUSY;
593 break;
594 case HAL_TIMEOUT :
595 usb_status = USBD_FAIL;
596 break;
597 default :
598 usb_status = USBD_FAIL;
599 break;
600 }
601 return usb_status;
602 }
603
604 /**
605 * @brief Clears a Stall condition on an endpoint of the Low Level Driver.
606 * @param pdev: Device handle
607 * @param ep_addr: Endpoint number
608 * @retval USBD status
609 */
USBD_LL_ClearStallEP(USBD_HandleTypeDef * pdev,uint8_t ep_addr)610 USBD_StatusTypeDef USBD_LL_ClearStallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
611 {
612 HAL_StatusTypeDef hal_status = HAL_OK;
613 USBD_StatusTypeDef usb_status = USBD_OK;
614
615 hal_status = HAL_PCD_EP_ClrStall(pdev->pData, ep_addr);
616
617 switch (hal_status) {
618 case HAL_OK :
619 usb_status = USBD_OK;
620 break;
621 case HAL_ERROR :
622 usb_status = USBD_FAIL;
623 break;
624 case HAL_BUSY :
625 usb_status = USBD_BUSY;
626 break;
627 case HAL_TIMEOUT :
628 usb_status = USBD_FAIL;
629 break;
630 default :
631 usb_status = USBD_FAIL;
632 break;
633 }
634 return usb_status;
635 }
636
637 /**
638 * @brief Returns Stall condition.
639 * @param pdev: Device handle
640 * @param ep_addr: Endpoint number
641 * @retval Stall (1: Yes, 0: No)
642 */
USBD_LL_IsStallEP(USBD_HandleTypeDef * pdev,uint8_t ep_addr)643 uint8_t USBD_LL_IsStallEP(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
644 {
645 PCD_HandleTypeDef *hpcd = (PCD_HandleTypeDef*) pdev->pData;
646
647 if((ep_addr & 0x80) == 0x80)
648 {
649 return hpcd->IN_ep[ep_addr & 0x7F].is_stall;
650 }
651 else
652 {
653 return hpcd->OUT_ep[ep_addr & 0x7F].is_stall;
654 }
655 }
656
657 /**
658 * @brief Assigns a USB address to the device.
659 * @param pdev: Device handle
660 * @param dev_addr: Device address
661 * @retval USBD status
662 */
USBD_LL_SetUSBAddress(USBD_HandleTypeDef * pdev,uint8_t dev_addr)663 USBD_StatusTypeDef USBD_LL_SetUSBAddress(USBD_HandleTypeDef *pdev, uint8_t dev_addr)
664 {
665 HAL_StatusTypeDef hal_status = HAL_OK;
666 USBD_StatusTypeDef usb_status = USBD_OK;
667
668 hal_status = HAL_PCD_SetAddress(pdev->pData, dev_addr);
669
670 switch (hal_status) {
671 case HAL_OK :
672 usb_status = USBD_OK;
673 break;
674 case HAL_ERROR :
675 usb_status = USBD_FAIL;
676 break;
677 case HAL_BUSY :
678 usb_status = USBD_BUSY;
679 break;
680 case HAL_TIMEOUT :
681 usb_status = USBD_FAIL;
682 break;
683 default :
684 usb_status = USBD_FAIL;
685 break;
686 }
687 return usb_status;
688 }
689
690 /**
691 * @brief Transmits data over an endpoint.
692 * @param pdev: Device handle
693 * @param ep_addr: Endpoint number
694 * @param pbuf: Pointer to data to be sent
695 * @param size: Data size
696 * @retval USBD status
697 */
USBD_LL_Transmit(USBD_HandleTypeDef * pdev,uint8_t ep_addr,uint8_t * pbuf,uint16_t size)698 USBD_StatusTypeDef USBD_LL_Transmit(USBD_HandleTypeDef *pdev, uint8_t ep_addr, uint8_t *pbuf, uint16_t size)
699 {
700 HAL_StatusTypeDef hal_status = HAL_OK;
701 USBD_StatusTypeDef usb_status = USBD_OK;
702
703 hal_status = HAL_PCD_EP_Transmit(pdev->pData, ep_addr, pbuf, size);
704
705 switch (hal_status) {
706 case HAL_OK :
707 usb_status = USBD_OK;
708 break;
709 case HAL_ERROR :
710 usb_status = USBD_FAIL;
711 break;
712 case HAL_BUSY :
713 usb_status = USBD_BUSY;
714 break;
715 case HAL_TIMEOUT :
716 usb_status = USBD_FAIL;
717 break;
718 default :
719 usb_status = USBD_FAIL;
720 break;
721 }
722 return usb_status;
723 }
724
725 /**
726 * @brief Prepares an endpoint for reception.
727 * @param pdev: Device handle
728 * @param ep_addr: Endpoint number
729 * @param pbuf: Pointer to data to be received
730 * @param size: Data size
731 * @retval USBD status
732 */
USBD_LL_PrepareReceive(USBD_HandleTypeDef * pdev,uint8_t ep_addr,uint8_t * pbuf,uint16_t size)733 USBD_StatusTypeDef USBD_LL_PrepareReceive(USBD_HandleTypeDef *pdev, uint8_t ep_addr, uint8_t *pbuf, uint16_t size)
734 {
735 HAL_StatusTypeDef hal_status = HAL_OK;
736 USBD_StatusTypeDef usb_status = USBD_OK;
737
738 hal_status = HAL_PCD_EP_Receive(pdev->pData, ep_addr, pbuf, size);
739
740 switch (hal_status) {
741 case HAL_OK :
742 usb_status = USBD_OK;
743 break;
744 case HAL_ERROR :
745 usb_status = USBD_FAIL;
746 break;
747 case HAL_BUSY :
748 usb_status = USBD_BUSY;
749 break;
750 case HAL_TIMEOUT :
751 usb_status = USBD_FAIL;
752 break;
753 default :
754 usb_status = USBD_FAIL;
755 break;
756 }
757 return usb_status;
758 }
759
760 /**
761 * @brief Returns the last transfered packet size.
762 * @param pdev: Device handle
763 * @param ep_addr: Endpoint number
764 * @retval Recived Data Size
765 */
USBD_LL_GetRxDataSize(USBD_HandleTypeDef * pdev,uint8_t ep_addr)766 uint32_t USBD_LL_GetRxDataSize(USBD_HandleTypeDef *pdev, uint8_t ep_addr)
767 {
768 return HAL_PCD_EP_GetRxCount((PCD_HandleTypeDef*) pdev->pData, ep_addr);
769 }
770
771 #if (USBD_LPM_ENABLED == 1)
772 /**
773 * @brief Send LPM message to user layer
774 * @param hpcd: PCD handle
775 * @param msg: LPM message
776 * @retval None
777 */
HAL_PCDEx_LPM_Callback(PCD_HandleTypeDef * hpcd,PCD_LPM_MsgTypeDef msg)778 void HAL_PCDEx_LPM_Callback(PCD_HandleTypeDef *hpcd, PCD_LPM_MsgTypeDef msg)
779 {
780 switch (msg)
781 {
782 case PCD_LPM_L0_ACTIVE:
783 if (hpcd->Init.low_power_enable)
784 {
785 SystemClock_Config();
786
787 /* Reset SLEEPDEEP bit of Cortex System Control Register. */
788 SCB->SCR &= (uint32_t)~((uint32_t)(SCB_SCR_SLEEPDEEP_Msk | SCB_SCR_SLEEPONEXIT_Msk));
789 }
790 __HAL_PCD_UNGATE_PHYCLOCK(hpcd);
791 USBD_LL_Resume(hpcd->pData);
792 break;
793
794 case PCD_LPM_L1_ACTIVE:
795 __HAL_PCD_GATE_PHYCLOCK(hpcd);
796 USBD_LL_Suspend(hpcd->pData);
797
798 /* Enter in STOP mode. */
799 if (hpcd->Init.low_power_enable)
800 {
801 /* Set SLEEPDEEP bit and SleepOnExit of Cortex System Control Register. */
802 SCB->SCR |= (uint32_t)((uint32_t)(SCB_SCR_SLEEPDEEP_Msk | SCB_SCR_SLEEPONEXIT_Msk));
803 }
804 break;
805 }
806 }
807 #endif /* (USBD_LPM_ENABLED == 1) */
808
809 /**
810 * @brief Delays routine for the USB Device Library.
811 * @param Delay: Delay in ms
812 * @retval None
813 */
USBD_LL_Delay(uint32_t Delay)814 void USBD_LL_Delay(uint32_t Delay)
815 {
816 HAL_Delay(Delay);
817 }
818
819 /**
820 * @brief Static single allocation.
821 * @param size: Size of allocated memory
822 * @retval None
823 */
USBD_static_malloc(uint32_t size)824 void *USBD_static_malloc(uint32_t size)
825 {
826 static uint32_t mem[(sizeof(USBD_CDC_HandleTypeDef)/4)+1];/* On 32-bit boundary */
827 return mem;
828 }
829
830 /**
831 * @brief Dummy memory free
832 * @param p: Pointer to allocated memory address
833 * @retval None
834 */
USBD_static_free(void * p)835 void USBD_static_free(void *p)
836 {
837
838 }
839
840 /* USER CODE BEGIN 5 */
841 /**
842 * @brief Configures system clock after wake-up from USB resume callBack:
843 * enable HSI, PLL and select PLL as system clock source.
844 * @retval None
845 */
SystemClockConfig_Resume(void)846 static void SystemClockConfig_Resume(void)
847 {
848 SystemClock_Config();
849 }
850 /* USER CODE END 5 */
851
852 /**
853 * @brief Software device connection
854 * @param hpcd: PCD handle
855 * @param state: Connection state (0: disconnected / 1: connected)
856 * @retval None
857 */
HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef * hpcd,uint8_t state)858 void HAL_PCDEx_SetConnectionState(PCD_HandleTypeDef *hpcd, uint8_t state)
859 {
860 /* USER CODE BEGIN 6 */
861 if (state == 1)
862 {
863 /* Configure Low connection state. */
864
865 }
866 else
867 {
868 /* Configure High connection state. */
869
870 }
871 /* USER CODE END 6 */
872 }
873
874 /**
875 * @brief Verify if the Battery Charging Detection mode (BCD) is used :
876 * return USBD_OK if true
877 * else return USBD_FAIL if false
878 * @param pdev: Device handle
879 * @retval USBD status
880 */
USBD_LL_BatteryCharging(USBD_HandleTypeDef * pdev)881 USBD_StatusTypeDef USBD_LL_BatteryCharging(USBD_HandleTypeDef *pdev)
882 {
883 PCD_HandleTypeDef *hpcd = (PCD_HandleTypeDef*)pdev->pData;
884 if (hpcd->Init.battery_charging_enable == ENABLE)
885 {
886 return USBD_OK;
887 }
888 else
889 {
890 return USBD_FAIL;
891 }
892 }
893
894 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
895