Fix unused param, add PERIPH_DRIVER flag for F4, tidied up F1 and F3 in prep.
[betaflight.git] / src / main / vcp / usb_prop.c
blobbe822f155a4325ac503e8a12de398c38df65c508
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 ------------------------------------------------------------------*/
29 #include "usb_lib.h"
30 #include "usb_conf.h"
31 #include "usb_prop.h"
32 #include "usb_desc.h"
33 #include "usb_pwr.h"
34 #include "hw_config.h"
36 /* Private typedef -----------------------------------------------------------*/
37 /* Private define ------------------------------------------------------------*/
38 /* Private macro -------------------------------------------------------------*/
39 /* Private variables ---------------------------------------------------------*/
40 uint8_t Request = 0;
42 LINE_CODING linecoding = { 115200, /* baud rate*/
43 0x00, /* stop bits-1*/
44 0x00, /* parity - none*/
45 0x08 /* no. of bits 8*/
48 /* -------------------------------------------------------------------------- */
49 /* Structures initializations */
50 /* -------------------------------------------------------------------------- */
52 DEVICE Device_Table = {
53 EP_NUM, 1 };
55 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,
56 Virtual_Com_Port_GetConfigDescriptor, Virtual_Com_Port_GetStringDescriptor, 0, 0x40 /*MAX PACKET SIZE*/
59 USER_STANDARD_REQUESTS User_Standard_Requests = {
60 Virtual_Com_Port_GetConfiguration, Virtual_Com_Port_SetConfiguration,
61 Virtual_Com_Port_GetInterface,
62 Virtual_Com_Port_SetInterface,
63 Virtual_Com_Port_GetStatus,
64 Virtual_Com_Port_ClearFeature,
65 Virtual_Com_Port_SetEndPointFeature,
66 Virtual_Com_Port_SetDeviceFeature, Virtual_Com_Port_SetDeviceAddress };
68 ONE_DESCRIPTOR Device_Descriptor = { (uint8_t*)Virtual_Com_Port_DeviceDescriptor,
69 VIRTUAL_COM_PORT_SIZ_DEVICE_DESC };
71 ONE_DESCRIPTOR Config_Descriptor = { (uint8_t*)Virtual_Com_Port_ConfigDescriptor,
72 VIRTUAL_COM_PORT_SIZ_CONFIG_DESC };
74 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,
75 VIRTUAL_COM_PORT_SIZ_STRING_PRODUCT }, { (uint8_t*)Virtual_Com_Port_StringSerial, VIRTUAL_COM_PORT_SIZ_STRING_SERIAL } };
77 /* Extern variables ----------------------------------------------------------*/
78 /* Private function prototypes -----------------------------------------------*/
79 /* Extern function prototypes ------------------------------------------------*/
80 /* Private functions ---------------------------------------------------------*/
81 /*******************************************************************************
82 * Function Name : Virtual_Com_Port_init.
83 * Description : Virtual COM Port Mouse init routine.
84 * Input : None.
85 * Output : None.
86 * Return : None.
87 *******************************************************************************/
88 void Virtual_Com_Port_init(void)
90 #ifdef STM32F10X
91 /* Make absolutly sure interrupts are disabled. */
92 USB_Interrupts_Disable();
93 #endif
95 /* Update the serial number string descriptor with the data from the unique
96 ID*/
97 Get_SerialNum();
99 pInformation->Current_Configuration = 0;
101 /* Connect the device */
102 PowerOn();
104 /* Perform basic device initialization operations */
105 USB_SIL_Init();
107 bDeviceState = UNCONNECTED;
110 /*******************************************************************************
111 * Function Name : Virtual_Com_Port_Reset
112 * Description : Virtual_Com_Port Mouse reset routine
113 * Input : None.
114 * Output : None.
115 * Return : None.
116 *******************************************************************************/
117 void Virtual_Com_Port_Reset(void)
119 /* Set Virtual_Com_Port DEVICE as not configured */
120 pInformation->Current_Configuration = 0;
122 /* Current Feature initialization */
123 pInformation->Current_Feature = Virtual_Com_Port_ConfigDescriptor[7];
125 /* Set Virtual_Com_Port DEVICE with the default Interface*/
126 pInformation->Current_Interface = 0;
128 SetBTABLE(BTABLE_ADDRESS);
130 /* Initialize Endpoint 0 */
131 SetEPType(ENDP0, EP_CONTROL);
132 SetEPTxStatus(ENDP0, EP_TX_STALL);
133 SetEPRxAddr(ENDP0, ENDP0_RXADDR);
134 SetEPTxAddr(ENDP0, ENDP0_TXADDR);
135 Clear_Status_Out(ENDP0);
136 SetEPRxCount(ENDP0, Device_Property.MaxPacketSize);
137 SetEPRxValid(ENDP0);
139 /* Initialize Endpoint 1 */
140 SetEPType(ENDP1, EP_BULK);
141 SetEPTxAddr(ENDP1, ENDP1_TXADDR);
142 SetEPTxStatus(ENDP1, EP_TX_NAK);
143 SetEPRxStatus(ENDP1, EP_RX_DIS);
145 /* Initialize Endpoint 2 */
146 SetEPType(ENDP2, EP_INTERRUPT);
147 SetEPTxAddr(ENDP2, ENDP2_TXADDR);
148 SetEPRxStatus(ENDP2, EP_RX_DIS);
149 SetEPTxStatus(ENDP2, EP_TX_NAK);
151 /* Initialize Endpoint 3 */
152 SetEPType(ENDP3, EP_BULK);
153 SetEPRxAddr(ENDP3, ENDP3_RXADDR);
154 SetEPRxCount(ENDP3, VIRTUAL_COM_PORT_DATA_SIZE);
155 SetEPRxStatus(ENDP3, EP_RX_VALID);
156 SetEPTxStatus(ENDP3, EP_TX_DIS);
158 /* Set this device to response on default address */
159 SetDeviceAddress(0);
161 bDeviceState = ATTACHED;
164 /*******************************************************************************
165 * Function Name : Virtual_Com_Port_SetConfiguration.
166 * Description : Update the device state to configured.
167 * Input : None.
168 * Output : None.
169 * Return : None.
170 *******************************************************************************/
171 void Virtual_Com_Port_SetConfiguration(void)
173 DEVICE_INFO *pInfo = &Device_Info;
175 if (pInfo->Current_Configuration != 0) {
176 /* Device configured */
177 bDeviceState = CONFIGURED;
181 /*******************************************************************************
182 * Function Name : Virtual_Com_Port_SetConfiguration.
183 * Description : Update the device state to addressed.
184 * Input : None.
185 * Output : None.
186 * Return : None.
187 *******************************************************************************/
188 void Virtual_Com_Port_SetDeviceAddress(void)
190 bDeviceState = ADDRESSED;
193 /*******************************************************************************
194 * Function Name : Virtual_Com_Port_Status_In.
195 * Description : Virtual COM Port Status In Routine.
196 * Input : None.
197 * Output : None.
198 * Return : None.
199 *******************************************************************************/
200 void Virtual_Com_Port_Status_In(void)
202 if (Request == SET_LINE_CODING) {
203 Request = 0;
207 /*******************************************************************************
208 * Function Name : Virtual_Com_Port_Status_Out
209 * Description : Virtual COM Port Status OUT Routine.
210 * Input : None.
211 * Output : None.
212 * Return : None.
213 *******************************************************************************/
214 void Virtual_Com_Port_Status_Out(void)
218 /*******************************************************************************
219 * Function Name : Virtual_Com_Port_Data_Setup
220 * Description : handle the data class specific requests
221 * Input : Request Nb.
222 * Output : None.
223 * Return : USB_UNSUPPORT or USB_SUCCESS.
224 *******************************************************************************/
225 RESULT Virtual_Com_Port_Data_Setup(uint8_t RequestNo)
227 uint8_t *(*CopyRoutine)( uint16_t);
229 CopyRoutine = NULL;
231 if (RequestNo == GET_LINE_CODING) {
232 if (Type_Recipient == (CLASS_REQUEST | INTERFACE_RECIPIENT)) {
233 CopyRoutine = Virtual_Com_Port_GetLineCoding;
235 } else if (RequestNo == SET_LINE_CODING) {
236 if (Type_Recipient == (CLASS_REQUEST | INTERFACE_RECIPIENT)) {
237 CopyRoutine = Virtual_Com_Port_SetLineCoding;
239 Request = SET_LINE_CODING;
242 if (CopyRoutine == NULL) {
243 return USB_UNSUPPORT;
246 pInformation->Ctrl_Info.CopyData = CopyRoutine;
247 pInformation->Ctrl_Info.Usb_wOffset = 0;
248 (*CopyRoutine)(0);
249 return USB_SUCCESS;
252 /*******************************************************************************
253 * Function Name : Virtual_Com_Port_NoData_Setup.
254 * Description : handle the no data class specific requests.
255 * Input : Request Nb.
256 * Output : None.
257 * Return : USB_UNSUPPORT or USB_SUCCESS.
258 *******************************************************************************/
259 RESULT Virtual_Com_Port_NoData_Setup(uint8_t RequestNo)
262 if (Type_Recipient == (CLASS_REQUEST | INTERFACE_RECIPIENT)) {
263 if (RequestNo == SET_COMM_FEATURE) {
264 return USB_SUCCESS;
265 } else if (RequestNo == SET_CONTROL_LINE_STATE) {
266 return USB_SUCCESS;
270 return USB_UNSUPPORT;
273 /*******************************************************************************
274 * Function Name : Virtual_Com_Port_GetDeviceDescriptor.
275 * Description : Gets the device descriptor.
276 * Input : Length.
277 * Output : None.
278 * Return : The address of the device descriptor.
279 *******************************************************************************/
280 uint8_t *Virtual_Com_Port_GetDeviceDescriptor(uint16_t Length)
282 return Standard_GetDescriptorData(Length, &Device_Descriptor);
285 /*******************************************************************************
286 * Function Name : Virtual_Com_Port_GetConfigDescriptor.
287 * Description : get the configuration descriptor.
288 * Input : Length.
289 * Output : None.
290 * Return : The address of the configuration descriptor.
291 *******************************************************************************/
292 uint8_t *Virtual_Com_Port_GetConfigDescriptor(uint16_t Length)
294 return Standard_GetDescriptorData(Length, &Config_Descriptor);
297 /*******************************************************************************
298 * Function Name : Virtual_Com_Port_GetStringDescriptor
299 * Description : Gets the string descriptors according to the needed index
300 * Input : Length.
301 * Output : None.
302 * Return : The address of the string descriptors.
303 *******************************************************************************/
304 uint8_t *Virtual_Com_Port_GetStringDescriptor(uint16_t Length)
306 uint8_t wValue0 = pInformation->USBwValue0;
307 if (wValue0 > 4) {
308 return NULL;
309 } else {
310 return Standard_GetDescriptorData(Length, &String_Descriptor[wValue0]);
314 /*******************************************************************************
315 * Function Name : Virtual_Com_Port_Get_Interface_Setting.
316 * Description : test the interface and the alternate setting according to the
317 * supported one.
318 * Input1 : uint8_t: Interface : interface number.
319 * Input2 : uint8_t: AlternateSetting : Alternate Setting number.
320 * Output : None.
321 * Return : The address of the string descriptors.
322 *******************************************************************************/
323 RESULT Virtual_Com_Port_Get_Interface_Setting(uint8_t Interface, uint8_t AlternateSetting)
325 if (AlternateSetting > 0) {
326 return USB_UNSUPPORT;
327 } else if (Interface > 1) {
328 return USB_UNSUPPORT;
330 return USB_SUCCESS;
333 /*******************************************************************************
334 * Function Name : Virtual_Com_Port_GetLineCoding.
335 * Description : send the linecoding structure to the PC host.
336 * Input : Length.
337 * Output : None.
338 * Return : Linecoding structure base address.
339 *******************************************************************************/
340 uint8_t *Virtual_Com_Port_GetLineCoding(uint16_t Length)
342 if (Length == 0) {
343 pInformation->Ctrl_Info.Usb_wLength = sizeof(linecoding);
344 return NULL;
346 return (uint8_t *)&linecoding;
349 /*******************************************************************************
350 * Function Name : Virtual_Com_Port_SetLineCoding.
351 * Description : Set the linecoding structure fields.
352 * Input : Length.
353 * Output : None.
354 * Return : Linecoding structure base address.
355 *******************************************************************************/
356 uint8_t *Virtual_Com_Port_SetLineCoding(uint16_t Length)
358 if (Length == 0) {
359 pInformation->Ctrl_Info.Usb_wLength = sizeof(linecoding);
360 return NULL;
362 return (uint8_t *)&linecoding;
365 /*******************************************************************************
366 * Function Name : Virtual_Com_Port_GetBaudRate.
367 * Description : Get the current baudrate
368 * Input : None.
369 * Output : None.
370 * Return : baudrate in bps
371 *******************************************************************************/
372 uint32_t Virtual_Com_Port_GetBaudRate(void)
374 return linecoding.bitrate;
377 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/