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 /* Make absolutly sure interrupts are disabled. */
92 USB_Interrupts_Disable();
95 /* Update the serial number string descriptor with the data from the unique
99 pInformation
->Current_Configuration
= 0;
101 /* Connect the device */
104 /* Perform basic device initialization operations */
107 bDeviceState
= UNCONNECTED
;
110 /*******************************************************************************
111 * Function Name : Virtual_Com_Port_Reset
112 * Description : Virtual_Com_Port Mouse reset routine
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
);
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 */
161 bDeviceState
= ATTACHED
;
164 /*******************************************************************************
165 * Function Name : Virtual_Com_Port_SetConfiguration.
166 * Description : Update the device state to configured.
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.
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.
199 *******************************************************************************/
200 void Virtual_Com_Port_Status_In(void)
202 if (Request
== SET_LINE_CODING
) {
207 /*******************************************************************************
208 * Function Name : Virtual_Com_Port_Status_Out
209 * Description : Virtual COM Port Status OUT Routine.
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.
223 * Return : USB_UNSUPPORT or USB_SUCCESS.
224 *******************************************************************************/
225 RESULT
Virtual_Com_Port_Data_Setup(uint8_t RequestNo
)
227 uint8_t *(*CopyRoutine
)( uint16_t);
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;
252 /*******************************************************************************
253 * Function Name : Virtual_Com_Port_NoData_Setup.
254 * Description : handle the no data class specific requests.
255 * Input : Request Nb.
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
) {
265 } else if (RequestNo
== SET_CONTROL_LINE_STATE
) {
270 return USB_UNSUPPORT
;
273 /*******************************************************************************
274 * Function Name : Virtual_Com_Port_GetDeviceDescriptor.
275 * Description : Gets the device descriptor.
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.
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
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
;
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
318 * Input1 : uint8_t: Interface : interface number.
319 * Input2 : uint8_t: AlternateSetting : Alternate Setting number.
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
;
333 /*******************************************************************************
334 * Function Name : Virtual_Com_Port_GetLineCoding.
335 * Description : send the linecoding structure to the PC host.
338 * Return : Linecoding structure base address.
339 *******************************************************************************/
340 uint8_t *Virtual_Com_Port_GetLineCoding(uint16_t Length
)
343 pInformation
->Ctrl_Info
.Usb_wLength
= sizeof(linecoding
);
346 return (uint8_t *)&linecoding
;
349 /*******************************************************************************
350 * Function Name : Virtual_Com_Port_SetLineCoding.
351 * Description : Set the linecoding structure fields.
354 * Return : Linecoding structure base address.
355 *******************************************************************************/
356 uint8_t *Virtual_Com_Port_SetLineCoding(uint16_t Length
)
359 pInformation
->Ctrl_Info
.Usb_wLength
= sizeof(linecoding
);
362 return (uint8_t *)&linecoding
;
365 /*******************************************************************************
366 * Function Name : Virtual_Com_Port_GetBaudRate.
367 * Description : Get the current baudrate
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****/