1 /**
2 ******************************************************************************
3 * @file : usb_device.c
4 * @version : v2.0_Cube
5 * @brief : This file implements the USB Device
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
52 #include "usb_device.h"
53 #include "usbd_core.h"
54 #include "usbd_desc.h"
55 #include "usbd_cdc.h"
56 #include "usbd_cdc_if.h"
57
58 /* USER CODE BEGIN Includes */
59
60 /* USER CODE END Includes */
61
62 /* USER CODE BEGIN PV */
63 /* Private variables ---------------------------------------------------------*/
64
65 /* USER CODE END PV */
66
67 /* USER CODE BEGIN PFP */
68 /* Private function prototypes -----------------------------------------------*/
69
70 /* USER CODE END PFP */
71
72 /* Return USBD_OK if the Battery Charging Detection mode (BCD) is used, else USBD_FAIL. */
73 extern USBD_StatusTypeDef USBD_LL_BatteryCharging(USBD_HandleTypeDef *pdev);
74
75 /* USB Device Core handle declaration. */
76 USBD_HandleTypeDef hUsbDeviceFS;
77
78 /*
79 * -- Insert your variables declaration here --
80 */
81 /* USER CODE BEGIN 0 */
82
83 /* USER CODE END 0 */
84
85 /*
86 * -- Insert your external function declaration here --
87 */
88 /* USER CODE BEGIN 1 */
MX_USB_DEVICE_DeInit(void)89 void MX_USB_DEVICE_DeInit(void)
90 {
91 USBD_DeInit(&hUsbDeviceFS);
92 }
93
94 /* USER CODE END 1 */
95
96 /**
97 * Init USB device Library, add supported class and start the library
98 * @retval None
99 */
MX_USB_DEVICE_Init(void)100 void MX_USB_DEVICE_Init(void)
101 {
102 /* USER CODE BEGIN USB_DEVICE_Init_PreTreatment */
103
104 /* USER CODE END USB_DEVICE_Init_PreTreatment */
105
106 /* Init Device Library, add supported class and start the library. */
107 USBD_Init(&hUsbDeviceFS, &FS_Desc, DEVICE_FS);
108 USBD_RegisterClass(&hUsbDeviceFS, &USBD_CDC);
109 USBD_CDC_RegisterInterface(&hUsbDeviceFS, &USBD_Interface_fops_FS);
110 /* Verify if the Battery Charging Detection mode (BCD) is used : */
111 /* If yes, the USB device is started in the HAL_PCDEx_BCD_Callback */
112 /* upon reception of PCD_BCD_DISCOVERY_COMPLETED message. */
113 /* If no, the USB device is started now. */
114 if (USBD_LL_BatteryCharging(&hUsbDeviceFS) != USBD_OK) {
115 USBD_Start(&hUsbDeviceFS);
116 }
117 /* USER CODE BEGIN USB_DEVICE_Init_PostTreatment */
118
119 /* USER CODE END USB_DEVICE_Init_PostTreatment */
120 }
121
122 /**
123 * @brief Send BCD message to user layer
124 * @param hpcd: PCD handle
125 * @param msg: LPM message
126 * @retval None
127 */
HAL_PCDEx_BCD_Callback(PCD_HandleTypeDef * hpcd,PCD_BCD_MsgTypeDef msg)128 void HAL_PCDEx_BCD_Callback(PCD_HandleTypeDef *hpcd, PCD_BCD_MsgTypeDef msg)
129 {
130 USBD_HandleTypeDef usbdHandle = hUsbDeviceFS;
131
132 /* USER CODE BEGIN 7 */
133 if (hpcd->battery_charging_active == ENABLE)
134 {
135 switch(msg)
136 {
137 case PCD_BCD_CONTACT_DETECTION:
138
139 break;
140
141 case PCD_BCD_STD_DOWNSTREAM_PORT:
142
143 break;
144
145 case PCD_BCD_CHARGING_DOWNSTREAM_PORT:
146
147 break;
148
149 case PCD_BCD_DEDICATED_CHARGING_PORT:
150
151 break;
152
153 case PCD_BCD_DISCOVERY_COMPLETED:
154 USBD_Start(&usbdHandle);
155 break;
156
157 case PCD_BCD_ERROR:
158 default:
159 break;
160 }
161 }
162 /* USER CODE END 7 */
163 }
164
165 /**
166 * @}
167 */
168
169 /**
170 * @}
171 */
172
173 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
174