Create release.yml
[betaflight.git] / src / main / vcp / usb_prop.c
blobc8ac671dcd8c06d97730a8e6e5438c66d37027c3
1 /**
2 ******************************************************************************
3 * @file usb_prop.c
4 * @author MCD Application Team
5 * @version V4.0.0
6 * @date 21-January-2013
7 * @brief All processing related to Virtual Com Port Demo
8 ******************************************************************************
9 * @attention
11 * <h2><center>&copy; COPYRIGHT 2013 STMicroelectronics</center></h2>
13 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
14 * You may not use this file except in compliance with the License.
15 * You may obtain a copy of the License at:
17 * http://www.st.com/software_license_agreement_liberty_v2
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS,
21 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
25 ******************************************************************************
28 /* Includes ------------------------------------------------------------------*/
30 #include "platform.h"
32 #include "usb_lib.h"
33 #include "usb_conf.h"
34 #include "usb_prop.h"
35 #include "usb_desc.h"
36 #include "usb_pwr.h"
37 #include "hw_config.h"
39 /* Private typedef -----------------------------------------------------------*/
40 /* Private define ------------------------------------------------------------*/
41 /* Private macro -------------------------------------------------------------*/
42 /* Private variables ---------------------------------------------------------*/
43 uint8_t Request = 0;
45 LINE_CODING linecoding = { 115200, /* baud rate*/
46 0x00, /* stop bits-1*/
47 0x00, /* parity - none*/
48 0x08 /* no. of bits 8*/
51 /* -------------------------------------------------------------------------- */
52 /* Structures initializations */
53 /* -------------------------------------------------------------------------- */
55 DEVICE Device_Table = {
56 EP_NUM, 1 };
58 DEVICE_PROP Device_Property = { Virtual_Com_Port_init, Virtual_Com_Port_Reset, Virtual_Com_Port_Status_In, Virtual_Com_Port_Status_Out, Virtual_Com_Port_Data_Setup, Virtual_Com_Port_NoData_Setup, Virtual_Com_Port_Get_Interface_Setting, Virtual_Com_Port_GetDeviceDescriptor,
59 Virtual_Com_Port_GetConfigDescriptor, Virtual_Com_Port_GetStringDescriptor, 0, 0x40 /*MAX PACKET SIZE*/
62 USER_STANDARD_REQUESTS User_Standard_Requests = {
63 Virtual_Com_Port_GetConfiguration, Virtual_Com_Port_SetConfiguration,
64 Virtual_Com_Port_GetInterface,
65 Virtual_Com_Port_SetInterface,
66 Virtual_Com_Port_GetStatus,
67 Virtual_Com_Port_ClearFeature,
68 Virtual_Com_Port_SetEndPointFeature,
69 Virtual_Com_Port_SetDeviceFeature, Virtual_Com_Port_SetDeviceAddress };
71 ONE_DESCRIPTOR Device_Descriptor = { (uint8_t*)Virtual_Com_Port_DeviceDescriptor,
72 VIRTUAL_COM_PORT_SIZ_DEVICE_DESC };
74 ONE_DESCRIPTOR Config_Descriptor = { (uint8_t*)Virtual_Com_Port_ConfigDescriptor,
75 VIRTUAL_COM_PORT_SIZ_CONFIG_DESC };
77 ONE_DESCRIPTOR String_Descriptor[4] = { { (uint8_t*)Virtual_Com_Port_StringLangID, VIRTUAL_COM_PORT_SIZ_STRING_LANGID }, { (uint8_t*)Virtual_Com_Port_StringVendor, VIRTUAL_COM_PORT_SIZ_STRING_VENDOR }, { (uint8_t*)Virtual_Com_Port_StringProduct,
78 VIRTUAL_COM_PORT_SIZ_STRING_PRODUCT }, { (uint8_t*)Virtual_Com_Port_StringSerial, VIRTUAL_COM_PORT_SIZ_STRING_SERIAL } };
80 /* Extern variables ----------------------------------------------------------*/
81 /* Private function prototypes -----------------------------------------------*/
82 /* Extern function prototypes ------------------------------------------------*/
83 /* Private functions ---------------------------------------------------------*/
84 /*******************************************************************************
85 * Function Name : Virtual_Com_Port_init.
86 * Description : Virtual COM Port Mouse init routine.
87 * Input : None.
88 * Output : None.
89 * Return : None.
90 *******************************************************************************/
91 void Virtual_Com_Port_init(void)
93 #ifdef STM32F10X
94 /* Make absolutly sure interrupts are disabled. */
95 USB_Interrupts_Disable();
96 #endif
98 /* Update the serial number string descriptor with the data from the unique
99 ID*/
100 Get_SerialNum();
102 pInformation->Current_Configuration = 0;
104 /* Connect the device */
105 PowerOn();
107 /* Perform basic device initialization operations */
108 USB_SIL_Init();
110 bDeviceState = UNCONNECTED;
113 /*******************************************************************************
114 * Function Name : Virtual_Com_Port_Reset
115 * Description : Virtual_Com_Port Mouse reset routine
116 * Input : None.
117 * Output : None.
118 * Return : None.
119 *******************************************************************************/
120 void Virtual_Com_Port_Reset(void)
122 /* Set Virtual_Com_Port DEVICE as not configured */
123 pInformation->Current_Configuration = 0;
125 /* Current Feature initialization */
126 pInformation->Current_Feature = Virtual_Com_Port_ConfigDescriptor[7];
128 /* Set Virtual_Com_Port DEVICE with the default Interface*/
129 pInformation->Current_Interface = 0;
131 SetBTABLE(BTABLE_ADDRESS);
133 /* Initialize Endpoint 0 */
134 SetEPType(ENDP0, EP_CONTROL);
135 SetEPTxStatus(ENDP0, EP_TX_STALL);
136 SetEPRxAddr(ENDP0, ENDP0_RXADDR);
137 SetEPTxAddr(ENDP0, ENDP0_TXADDR);
138 Clear_Status_Out(ENDP0);
139 SetEPRxCount(ENDP0, Device_Property.MaxPacketSize);
140 SetEPRxValid(ENDP0);
142 /* Initialize Endpoint 1 */
143 SetEPType(ENDP1, EP_BULK);
144 SetEPTxAddr(ENDP1, ENDP1_TXADDR);
145 SetEPTxStatus(ENDP1, EP_TX_NAK);
146 SetEPRxStatus(ENDP1, EP_RX_DIS);
148 /* Initialize Endpoint 2 */
149 SetEPType(ENDP2, EP_INTERRUPT);
150 SetEPTxAddr(ENDP2, ENDP2_TXADDR);
151 SetEPRxStatus(ENDP2, EP_RX_DIS);
152 SetEPTxStatus(ENDP2, EP_TX_NAK);
154 /* Initialize Endpoint 3 */
155 SetEPType(ENDP3, EP_BULK);
156 SetEPRxAddr(ENDP3, ENDP3_RXADDR);
157 SetEPRxCount(ENDP3, VIRTUAL_COM_PORT_DATA_SIZE);
158 SetEPRxStatus(ENDP3, EP_RX_VALID);
159 SetEPTxStatus(ENDP3, EP_TX_DIS);
161 /* Set this device to response on default address */
162 SetDeviceAddress(0);
164 bDeviceState = ATTACHED;
167 /*******************************************************************************
168 * Function Name : Virtual_Com_Port_SetConfiguration.
169 * Description : Update the device state to configured.
170 * Input : None.
171 * Output : None.
172 * Return : None.
173 *******************************************************************************/
174 void Virtual_Com_Port_SetConfiguration(void)
176 DEVICE_INFO *pInfo = &Device_Info;
178 if (pInfo->Current_Configuration != 0) {
179 /* Device configured */
180 bDeviceState = CONFIGURED;
184 /*******************************************************************************
185 * Function Name : Virtual_Com_Port_SetConfiguration.
186 * Description : Update the device state to addressed.
187 * Input : None.
188 * Output : None.
189 * Return : None.
190 *******************************************************************************/
191 void Virtual_Com_Port_SetDeviceAddress(void)
193 bDeviceState = ADDRESSED;
196 /*******************************************************************************
197 * Function Name : Virtual_Com_Port_Status_In.
198 * Description : Virtual COM Port Status In Routine.
199 * Input : None.
200 * Output : None.
201 * Return : None.
202 *******************************************************************************/
203 void Virtual_Com_Port_Status_In(void)
205 if (Request == SET_LINE_CODING) {
206 Request = 0;
210 /*******************************************************************************
211 * Function Name : Virtual_Com_Port_Status_Out
212 * Description : Virtual COM Port Status OUT Routine.
213 * Input : None.
214 * Output : None.
215 * Return : None.
216 *******************************************************************************/
217 void Virtual_Com_Port_Status_Out(void)
221 /*******************************************************************************
222 * Function Name : Virtual_Com_Port_Data_Setup
223 * Description : handle the data class specific requests
224 * Input : Request Nb.
225 * Output : None.
226 * Return : USB_UNSUPPORT or USB_SUCCESS.
227 *******************************************************************************/
228 RESULT Virtual_Com_Port_Data_Setup(uint8_t RequestNo)
230 uint8_t *(*CopyRoutine)( uint16_t);
232 CopyRoutine = NULL;
234 if (RequestNo == GET_LINE_CODING) {
235 if (Type_Recipient == (CLASS_REQUEST | INTERFACE_RECIPIENT)) {
236 CopyRoutine = Virtual_Com_Port_GetLineCoding;
238 } else if (RequestNo == SET_LINE_CODING) {
239 if (Type_Recipient == (CLASS_REQUEST | INTERFACE_RECIPIENT)) {
240 CopyRoutine = Virtual_Com_Port_SetLineCoding;
242 Request = SET_LINE_CODING;
245 if (CopyRoutine == NULL) {
246 return USB_UNSUPPORT;
249 pInformation->Ctrl_Info.CopyData = CopyRoutine;
250 pInformation->Ctrl_Info.Usb_wOffset = 0;
251 (*CopyRoutine)(0);
252 return USB_SUCCESS;
255 /*******************************************************************************
256 * Function Name : Virtual_Com_Port_NoData_Setup.
257 * Description : handle the no data class specific requests.
258 * Input : Request Nb.
259 * Output : None.
260 * Return : USB_UNSUPPORT or USB_SUCCESS.
261 *******************************************************************************/
262 RESULT Virtual_Com_Port_NoData_Setup(uint8_t RequestNo)
265 if (Type_Recipient == (CLASS_REQUEST | INTERFACE_RECIPIENT)) {
266 if (RequestNo == SET_COMM_FEATURE) {
267 return USB_SUCCESS;
268 } else if (RequestNo == SET_CONTROL_LINE_STATE) {
269 return USB_SUCCESS;
273 return USB_UNSUPPORT;
276 /*******************************************************************************
277 * Function Name : Virtual_Com_Port_GetDeviceDescriptor.
278 * Description : Gets the device descriptor.
279 * Input : Length.
280 * Output : None.
281 * Return : The address of the device descriptor.
282 *******************************************************************************/
283 uint8_t *Virtual_Com_Port_GetDeviceDescriptor(uint16_t Length)
285 return Standard_GetDescriptorData(Length, &Device_Descriptor);
288 /*******************************************************************************
289 * Function Name : Virtual_Com_Port_GetConfigDescriptor.
290 * Description : get the configuration descriptor.
291 * Input : Length.
292 * Output : None.
293 * Return : The address of the configuration descriptor.
294 *******************************************************************************/
295 uint8_t *Virtual_Com_Port_GetConfigDescriptor(uint16_t Length)
297 return Standard_GetDescriptorData(Length, &Config_Descriptor);
300 /*******************************************************************************
301 * Function Name : Virtual_Com_Port_GetStringDescriptor
302 * Description : Gets the string descriptors according to the needed index
303 * Input : Length.
304 * Output : None.
305 * Return : The address of the string descriptors.
306 *******************************************************************************/
307 uint8_t *Virtual_Com_Port_GetStringDescriptor(uint16_t Length)
309 uint8_t wValue0 = pInformation->USBwValue0;
310 if (wValue0 > 4) {
311 return NULL;
312 } else {
313 return Standard_GetDescriptorData(Length, &String_Descriptor[wValue0]);
317 /*******************************************************************************
318 * Function Name : Virtual_Com_Port_Get_Interface_Setting.
319 * Description : test the interface and the alternate setting according to the
320 * supported one.
321 * Input1 : uint8_t: Interface : interface number.
322 * Input2 : uint8_t: AlternateSetting : Alternate Setting number.
323 * Output : None.
324 * Return : The address of the string descriptors.
325 *******************************************************************************/
326 RESULT Virtual_Com_Port_Get_Interface_Setting(uint8_t Interface, uint8_t AlternateSetting)
328 if (AlternateSetting > 0) {
329 return USB_UNSUPPORT;
330 } else if (Interface > 1) {
331 return USB_UNSUPPORT;
333 return USB_SUCCESS;
336 /*******************************************************************************
337 * Function Name : Virtual_Com_Port_GetLineCoding.
338 * Description : send the linecoding structure to the PC host.
339 * Input : Length.
340 * Output : None.
341 * Return : Linecoding structure base address.
342 *******************************************************************************/
343 uint8_t *Virtual_Com_Port_GetLineCoding(uint16_t Length)
345 if (Length == 0) {
346 pInformation->Ctrl_Info.Usb_wLength = sizeof(linecoding);
347 return NULL;
349 return (uint8_t *)&linecoding;
352 /*******************************************************************************
353 * Function Name : Virtual_Com_Port_SetLineCoding.
354 * Description : Set the linecoding structure fields.
355 * Input : Length.
356 * Output : None.
357 * Return : Linecoding structure base address.
358 *******************************************************************************/
359 uint8_t *Virtual_Com_Port_SetLineCoding(uint16_t Length)
361 if (Length == 0) {
362 pInformation->Ctrl_Info.Usb_wLength = sizeof(linecoding);
363 return NULL;
365 return (uint8_t *)&linecoding;
368 /*******************************************************************************
369 * Function Name : Virtual_Com_Port_GetBaudRate.
370 * Description : Get the current baudrate
371 * Input : None.
372 * Output : None.
373 * Return : baudrate in bps
374 *******************************************************************************/
375 uint32_t Virtual_Com_Port_GetBaudRate(void)
377 return linecoding.bitrate;
380 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/