2 ******************************************************************************
4 * @author MCD Application Team
6 * @date 21-January-2013
7 * @brief All processing related to Virtual Com Port Demo
8 ******************************************************************************
11 * <h2><center>© 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 ------------------------------------------------------------------*/
34 #include "hw_config.h"
36 /* Private typedef -----------------------------------------------------------*/
37 /* Private define ------------------------------------------------------------*/
38 /* Private macro -------------------------------------------------------------*/
39 /* Private variables ---------------------------------------------------------*/
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
= {
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.
87 *******************************************************************************/
88 void Virtual_Com_Port_init(void)
91 /* Update the serial number string descriptor with the data from the unique
95 pInformation
->Current_Configuration
= 0;
97 /* Connect the device */
100 /* Perform basic device initialization operations */
103 bDeviceState
= UNCONNECTED
;
106 /*******************************************************************************
107 * Function Name : Virtual_Com_Port_Reset
108 * Description : Virtual_Com_Port Mouse reset routine
112 *******************************************************************************/
113 void Virtual_Com_Port_Reset(void)
115 /* Set Virtual_Com_Port DEVICE as not configured */
116 pInformation
->Current_Configuration
= 0;
118 /* Current Feature initialization */
119 pInformation
->Current_Feature
= Virtual_Com_Port_ConfigDescriptor
[7];
121 /* Set Virtual_Com_Port DEVICE with the default Interface*/
122 pInformation
->Current_Interface
= 0;
124 SetBTABLE(BTABLE_ADDRESS
);
126 /* Initialize Endpoint 0 */
127 SetEPType(ENDP0
, EP_CONTROL
);
128 SetEPTxStatus(ENDP0
, EP_TX_STALL
);
129 SetEPRxAddr(ENDP0
, ENDP0_RXADDR
);
130 SetEPTxAddr(ENDP0
, ENDP0_TXADDR
);
131 Clear_Status_Out(ENDP0
);
132 SetEPRxCount(ENDP0
, Device_Property
.MaxPacketSize
);
135 /* Initialize Endpoint 1 */
136 SetEPType(ENDP1
, EP_BULK
);
137 SetEPTxAddr(ENDP1
, ENDP1_TXADDR
);
138 SetEPTxStatus(ENDP1
, EP_TX_NAK
);
139 SetEPRxStatus(ENDP1
, EP_RX_DIS
);
141 /* Initialize Endpoint 2 */
142 SetEPType(ENDP2
, EP_INTERRUPT
);
143 SetEPTxAddr(ENDP2
, ENDP2_TXADDR
);
144 SetEPRxStatus(ENDP2
, EP_RX_DIS
);
145 SetEPTxStatus(ENDP2
, EP_TX_NAK
);
147 /* Initialize Endpoint 3 */
148 SetEPType(ENDP3
, EP_BULK
);
149 SetEPRxAddr(ENDP3
, ENDP3_RXADDR
);
150 SetEPRxCount(ENDP3
, VIRTUAL_COM_PORT_DATA_SIZE
);
151 SetEPRxStatus(ENDP3
, EP_RX_VALID
);
152 SetEPTxStatus(ENDP3
, EP_TX_DIS
);
154 /* Set this device to response on default address */
157 bDeviceState
= ATTACHED
;
160 /*******************************************************************************
161 * Function Name : Virtual_Com_Port_SetConfiguration.
162 * Description : Update the device state to configured.
166 *******************************************************************************/
167 void Virtual_Com_Port_SetConfiguration(void)
169 DEVICE_INFO
*pInfo
= &Device_Info
;
171 if (pInfo
->Current_Configuration
!= 0) {
172 /* Device configured */
173 bDeviceState
= CONFIGURED
;
177 /*******************************************************************************
178 * Function Name : Virtual_Com_Port_SetConfiguration.
179 * Description : Update the device state to addressed.
183 *******************************************************************************/
184 void Virtual_Com_Port_SetDeviceAddress(void)
186 bDeviceState
= ADDRESSED
;
189 /*******************************************************************************
190 * Function Name : Virtual_Com_Port_Status_In.
191 * Description : Virtual COM Port Status In Routine.
195 *******************************************************************************/
196 void Virtual_Com_Port_Status_In(void)
198 if (Request
== SET_LINE_CODING
) {
203 /*******************************************************************************
204 * Function Name : Virtual_Com_Port_Status_Out
205 * Description : Virtual COM Port Status OUT Routine.
209 *******************************************************************************/
210 void Virtual_Com_Port_Status_Out(void)
214 /*******************************************************************************
215 * Function Name : Virtual_Com_Port_Data_Setup
216 * Description : handle the data class specific requests
217 * Input : Request Nb.
219 * Return : USB_UNSUPPORT or USB_SUCCESS.
220 *******************************************************************************/
221 RESULT
Virtual_Com_Port_Data_Setup(uint8_t RequestNo
)
223 uint8_t *(*CopyRoutine
)( uint16_t);
227 if (RequestNo
== GET_LINE_CODING
) {
228 if (Type_Recipient
== (CLASS_REQUEST
| INTERFACE_RECIPIENT
)) {
229 CopyRoutine
= Virtual_Com_Port_GetLineCoding
;
231 } else if (RequestNo
== SET_LINE_CODING
) {
232 if (Type_Recipient
== (CLASS_REQUEST
| INTERFACE_RECIPIENT
)) {
233 CopyRoutine
= Virtual_Com_Port_SetLineCoding
;
235 Request
= SET_LINE_CODING
;
238 if (CopyRoutine
== NULL
) {
239 return USB_UNSUPPORT
;
242 pInformation
->Ctrl_Info
.CopyData
= CopyRoutine
;
243 pInformation
->Ctrl_Info
.Usb_wOffset
= 0;
248 /*******************************************************************************
249 * Function Name : Virtual_Com_Port_NoData_Setup.
250 * Description : handle the no data class specific requests.
251 * Input : Request Nb.
253 * Return : USB_UNSUPPORT or USB_SUCCESS.
254 *******************************************************************************/
255 RESULT
Virtual_Com_Port_NoData_Setup(uint8_t RequestNo
)
258 if (Type_Recipient
== (CLASS_REQUEST
| INTERFACE_RECIPIENT
)) {
259 if (RequestNo
== SET_COMM_FEATURE
) {
261 } else if (RequestNo
== SET_CONTROL_LINE_STATE
) {
266 return USB_UNSUPPORT
;
269 /*******************************************************************************
270 * Function Name : Virtual_Com_Port_GetDeviceDescriptor.
271 * Description : Gets the device descriptor.
274 * Return : The address of the device descriptor.
275 *******************************************************************************/
276 uint8_t *Virtual_Com_Port_GetDeviceDescriptor(uint16_t Length
)
278 return Standard_GetDescriptorData(Length
, &Device_Descriptor
);
281 /*******************************************************************************
282 * Function Name : Virtual_Com_Port_GetConfigDescriptor.
283 * Description : get the configuration descriptor.
286 * Return : The address of the configuration descriptor.
287 *******************************************************************************/
288 uint8_t *Virtual_Com_Port_GetConfigDescriptor(uint16_t Length
)
290 return Standard_GetDescriptorData(Length
, &Config_Descriptor
);
293 /*******************************************************************************
294 * Function Name : Virtual_Com_Port_GetStringDescriptor
295 * Description : Gets the string descriptors according to the needed index
298 * Return : The address of the string descriptors.
299 *******************************************************************************/
300 uint8_t *Virtual_Com_Port_GetStringDescriptor(uint16_t Length
)
302 uint8_t wValue0
= pInformation
->USBwValue0
;
306 return Standard_GetDescriptorData(Length
, &String_Descriptor
[wValue0
]);
310 /*******************************************************************************
311 * Function Name : Virtual_Com_Port_Get_Interface_Setting.
312 * Description : test the interface and the alternate setting according to the
314 * Input1 : uint8_t: Interface : interface number.
315 * Input2 : uint8_t: AlternateSetting : Alternate Setting number.
317 * Return : The address of the string descriptors.
318 *******************************************************************************/
319 RESULT
Virtual_Com_Port_Get_Interface_Setting(uint8_t Interface
, uint8_t AlternateSetting
)
321 if (AlternateSetting
> 0) {
322 return USB_UNSUPPORT
;
323 } else if (Interface
> 1) {
324 return USB_UNSUPPORT
;
329 /*******************************************************************************
330 * Function Name : Virtual_Com_Port_GetLineCoding.
331 * Description : send the linecoding structure to the PC host.
334 * Return : Linecoding structure base address.
335 *******************************************************************************/
336 uint8_t *Virtual_Com_Port_GetLineCoding(uint16_t Length
)
339 pInformation
->Ctrl_Info
.Usb_wLength
= sizeof(linecoding
);
342 return (uint8_t *)&linecoding
;
345 /*******************************************************************************
346 * Function Name : Virtual_Com_Port_SetLineCoding.
347 * Description : Set the linecoding structure fields.
350 * Return : Linecoding structure base address.
351 *******************************************************************************/
352 uint8_t *Virtual_Com_Port_SetLineCoding(uint16_t Length
)
355 pInformation
->Ctrl_Info
.Usb_wLength
= sizeof(linecoding
);
358 return (uint8_t *)&linecoding
;
361 /*******************************************************************************
362 * Function Name : Virtual_Com_Port_GetBaudRate.
363 * Description : Get the current baudrate
366 * Return : baudrate in bps
367 *******************************************************************************/
368 uint32_t Virtual_Com_Port_GetBaudRate(void)
370 return linecoding
.bitrate
;
373 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/