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 ------------------------------------------------------------------*/
37 #include "hw_config.h"
39 /* Private typedef -----------------------------------------------------------*/
40 /* Private define ------------------------------------------------------------*/
41 /* Private macro -------------------------------------------------------------*/
42 /* Private variables ---------------------------------------------------------*/
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
= {
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.
90 *******************************************************************************/
91 void Virtual_Com_Port_init(void)
94 /* Make absolutly sure interrupts are disabled. */
95 USB_Interrupts_Disable();
98 /* Update the serial number string descriptor with the data from the unique
102 pInformation
->Current_Configuration
= 0;
104 /* Connect the device */
107 /* Perform basic device initialization operations */
110 bDeviceState
= UNCONNECTED
;
113 /*******************************************************************************
114 * Function Name : Virtual_Com_Port_Reset
115 * Description : Virtual_Com_Port Mouse reset routine
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
);
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 */
164 bDeviceState
= ATTACHED
;
167 /*******************************************************************************
168 * Function Name : Virtual_Com_Port_SetConfiguration.
169 * Description : Update the device state to configured.
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.
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.
202 *******************************************************************************/
203 void Virtual_Com_Port_Status_In(void)
205 if (Request
== SET_LINE_CODING
) {
210 /*******************************************************************************
211 * Function Name : Virtual_Com_Port_Status_Out
212 * Description : Virtual COM Port Status OUT Routine.
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.
226 * Return : USB_UNSUPPORT or USB_SUCCESS.
227 *******************************************************************************/
228 RESULT
Virtual_Com_Port_Data_Setup(uint8_t RequestNo
)
230 uint8_t *(*CopyRoutine
)( uint16_t);
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;
255 /*******************************************************************************
256 * Function Name : Virtual_Com_Port_NoData_Setup.
257 * Description : handle the no data class specific requests.
258 * Input : Request Nb.
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
) {
268 } else if (RequestNo
== SET_CONTROL_LINE_STATE
) {
273 return USB_UNSUPPORT
;
276 /*******************************************************************************
277 * Function Name : Virtual_Com_Port_GetDeviceDescriptor.
278 * Description : Gets the device descriptor.
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.
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
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
;
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
321 * Input1 : uint8_t: Interface : interface number.
322 * Input2 : uint8_t: AlternateSetting : Alternate Setting number.
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
;
336 /*******************************************************************************
337 * Function Name : Virtual_Com_Port_GetLineCoding.
338 * Description : send the linecoding structure to the PC host.
341 * Return : Linecoding structure base address.
342 *******************************************************************************/
343 uint8_t *Virtual_Com_Port_GetLineCoding(uint16_t Length
)
346 pInformation
->Ctrl_Info
.Usb_wLength
= sizeof(linecoding
);
349 return (uint8_t *)&linecoding
;
352 /*******************************************************************************
353 * Function Name : Virtual_Com_Port_SetLineCoding.
354 * Description : Set the linecoding structure fields.
357 * Return : Linecoding structure base address.
358 *******************************************************************************/
359 uint8_t *Virtual_Com_Port_SetLineCoding(uint16_t Length
)
362 pInformation
->Ctrl_Info
.Usb_wLength
= sizeof(linecoding
);
365 return (uint8_t *)&linecoding
;
368 /*******************************************************************************
369 * Function Name : Virtual_Com_Port_GetBaudRate.
370 * Description : Get the current baudrate
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****/