|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区
您需要 登录 才可以下载或查看,没有账号?立即注册
×
源代码来自cypress的开发包 cyusbs232 RDK.
/*
## Cypress USB-Serial Windows Example source file (gpio.cpp)
## ===========================
##
## Copyright Cypress Semiconductor Corporation, 2013-2014,
## All Rights Reserved
## UNPUBLISHED, LICENSED SOFTWARE.
##
## CONFIDENTIAL AND PROPRIETARY INFORMATION
## WHICH IS THE PROPERTY OF CYPRESS.
##
## Use of this file is governed
## by the license agreement included in the file
##
## <install>/license/license.txt
##
## where <install> is the Cypress software
## installation root directory path.
##
## ===========================
*/
// gpio.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include <dbt.h>
#include <conio.h>
#include "..\..\..\library\inc\CyUSBSerial.h"
/** ****************
Data Definitions
*** **************** */
// Define VID & PID
// These numbers depends on individual products
#define VID 0x04B4
#define PID 0x0003 //4
//Variable to store cyHandle of the selected device
CY_HANDLE cyHandle;
//Varible to capture return values from USB Serial API
CY_RETURN_STATUS cyReturnStatus;
//CY_DEVICE_INFO provides additional details of each device such as product Name, serial number etc..
CY_DEVICE_INFO cyDeviceInfo, cyDeviceInfoList[16];
//Structure to store VID & PID defined in CyUSBSerial.h
CY_VID_PID cyVidPid;
//Variables used by application
UINT8 cyNumDevices;
unsigned char deviceID[16];
/** ****************
Functions
*** **************** */
/*
Function Name: int GPIOTasks(int deviceNumber)
Purpose: Demonstates how to communicate with USB-Serial device's GPIOs
Arguments:
deviceNumber - The device number identified during the enumeration process
Retrun Code: returns falure code of USB-Serial API, -1 for local failures.
*/
int GPIOTasks(int deviceNumber)
{
CY_DATA_BUFFER cyDatabuffer;
unsigned char gpioNumber, value;
unsigned char buffer[4096];
CY_RETURN_STATUS rStatus;
int interfaceNum = 0, count = 1000;
unsigned char bytesPending = 0;
cyDatabuffer.buffer = buffer;
//Obtain handle for a USB-Serial device
printf("\nOpening USB-Serial device with device number %d...\n",deviceNumber);
rStatus = CyOpen (deviceNumber, interfaceNum, &cyHandle);
if (rStatus){
printf ("Failed to open USB-Serial device\n");
return -1;
}
// Verify that the GPIO is configured using Cypress USB-Serial Configuration Utility
gpioNumber = 2;
printf("Output 0 on GPIO-2 configured as OUTPUT using Cypress USB-Serial Configuration Utility...\n");
rStatus = CySetGpioValue (cyHandle, gpioNumber, 0);
if (rStatus){
printf ("Set GPIO failed.\n");
return -1;
}
printf("Output 1 on GPIO-2 configured as OUTPUT using Cypress USB-Serial Configuration Utility...\n");
rStatus = CySetGpioValue (cyHandle, gpioNumber, 1);
if (rStatus){
printf ("Set GPIO failed.\n");
return -1;
}
gpioNumber = 1;
printf("Read from on GPIO-2 configured as INPUT using Cypress USB-Serial Configuration Utility...\n");
rStatus = CyGetGpioValue (cyHandle, gpioNumber, &value);
if (rStatus){
printf ("Get GPIO value failed\n");
return -1;
}
printf ("GPIO-1 Value is %d \n", value);
// Close the cyHandle to USB-Serial device
return CyClose (cyHandle);
}
/*
Function Name: int FindDeviceAtSCB0()
Purpose: Demonstates how to enumerate and find device from the list of devices.
- Demonstrates how to use deviceBlock member in DEVICE_INFO structure
Arguments: None
Retrun Code: returns -1, if no device is present or deviceIndex of the device.
*/
int FindDeviceAtSCB0()
{
CY_VID_PID cyVidPid;
cyVidPid.vid = VID; //Defined as macro
cyVidPid.pid = PID; //Defined as macro
//Array size of cyDeviceInfoList is 16
cyReturnStatus = CyGetDeviceInfoVidPid (cyVidPid, deviceID, (PCY_DEVICE_INFO)&cyDeviceInfoList, &cyNumDevices, 16);
int deviceIndexAtSCB0 = -1;
for (int index = 0; index < cyNumDevices; index++){
printf ("\nNumber of interfaces: %d\n \
Vid: 0x%X \n\
Pid: 0x%X \n\
Serial name is: %s\n\
Manufacturer name: %s\n\
Product Name: %s\n\
SCB Number: 0x%X \n\
Device Type: %d \n\
Device Class: %d\n\n\n",
cyDeviceInfoList[index].numInterfaces,
cyDeviceInfoList[index].vidPid.vid,
cyDeviceInfoList[index].vidPid.pid,
cyDeviceInfoList[index].serialNum,
cyDeviceInfoList[index].manufacturerName,
cyDeviceInfoList[index].productName,
cyDeviceInfoList[index].deviceBlock,
cyDeviceInfoList[index].deviceType[0],
cyDeviceInfoList[index].deviceClass[0]);
// Find the device at device index at SCB0
if (cyDeviceInfoList[index].deviceBlock == SerialBlock_MFG) //SerialBlock_SCB0
{
deviceIndexAtSCB0 = index;
}
}
return deviceIndexAtSCB0;
}
/** ********************************
Application main() function
*** ******************************** */
int _tmain(int argc, _TCHAR* argv[])
{
//NOTE: SCB Index is not needed for accessing GPIO. But as this function call ensures
//access to right SCB, this example uses this code.
int deviceIndexAtSCB0 = FindDeviceAtSCB0();
//Open the device at index deviceIndexAtSCB0
if (deviceIndexAtSCB0 >= 0)
{
//Assuming that device at "index" is USB-Serial device
//Device Open, Close, Configuration, Data operations are handled in the function GPIOTasks
int status = GPIOTasks(deviceIndexAtSCB0);
if (status == CY_SUCCESS)
{
printf("\nGPIOTasks returned success.\n");
}
else
{
printf("\nGPIOTasks returned failure code.\n");
}
} //cyNumDevices > 0 && cyReturnStatus == CY_SUCCESS
return 0;
} |
|