我们从2011年坚守至今,只想做存粹的技术论坛。  由于网站在外面,点击附件后要很长世间才弹出下载,请耐心等待,勿重复点击不要用Edge和IE浏览器下载,否则提示不安全下载不了

 找回密码
 立即注册
搜索
查看: 2276|回复: 1

cypress USB-to-uart芯片, 上位机控制 gpio 的代码

[复制链接]

该用户从未签到

6

主题

108

回帖

33

积分

二级逆天

积分
33

社区居民终身成就奖

QQ
发表于 2015-6-28 13:52:57 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区

您需要 登录 才可以下载或查看,没有账号?立即注册

×
源代码来自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;
    }
回复

使用道具 举报

该用户从未签到

3

主题

3952

回帖

4862

积分

二级逆天

积分
4862

社区居民忠实会员最爱沙发终身成就奖优秀斑竹奖

QQ
发表于 2015-6-28 14:23:31 | 显示全部楼层
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

公告:服务器刚移机,
大家请不要下载东西。
会下载失败


Copyright ©2011-2024 NTpcb.com All Right Reserved.  Powered by Discuz! (NTpcb)

本站信息均由会员发表,不代表NTpcb立场,如侵犯了您的权利请发帖投诉

( 闽ICP备2024076463号-1 ) 论坛技术支持QQ群171867948 ,论坛问题,充值问题请联系QQ1308068381

平平安安
TOP
快速回复 返回顶部 返回列表