⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 USB外接口程序,ARM实现USB端口控制
💻 C
字号:
/****************************************Copyright (c)**************************************************
**                               Guangzou ZLG-MCU Development Co.,LTD.
**                                      graduate school
**                                 http://www.zlgmcu.com
**
**--------------File Info-------------------------------------------------------------------------------
** File name:			main.c
** Last modified Date:  2004-09-16
** Last Version:		1.0
** Descriptions:		The main() function example template
**
**------------------------------------------------------------------------------------------------------
** Created by:			Chenmingji
** Created date:		2004-09-16
** Version:				1.0
** Descriptions:		The original version
**
**------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
**
********************************************************************************************************/
#include "config.h"
#include "hardwareinit.h"

#define CMD_INIT  0x00
#define CMD_READ  0x01
#define CMD_WRITE 0x02

#define HC_COMMAND_ADDRESS     0x82000002
//#include "isp1161hal.h"
#define HC_DATA_ADDRESS        0x82000000
void outcommand( uint16  data)
{

*(volatile uint16 *)HC_COMMAND_ADDRESS=data;

}
void outdata( uint16  data)
{
*(volatile uint16*)HC_DATA_ADDRESS=data;
}		
uint16 indata(void)
{
return *((volatile uint16*)HC_COMMAND_ADDRESS);
}						

#define		Com16_HcATLBufferPort			0x41
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <setupapi.h>
#define DIRECTINPUT_VERSION 0x0800
#include <Dinput.h>
extern "C"
{
        #include <hidsdi.h>
}
#include <tchar.h>
#pragma comment(lib, "Dinput8.lib")
#pragma comment(lib, "Dinput.lib")
#pragma comment(lib, "Setupapi.lib") 
#pragma comment(lib, "hid.lib")
GUID myGuid;
LPDIRECTINPUT8 m_lpDI;         // DI8接口指针 

LPDIENUMDEVICESCALLBACK DIEnumDevicesCallback(const DIDEVICEINSTANCE* lpddi, VOID* pvRef)
{ 
  *(GUID*) pvRef = lpddi->guidInstance; 


  return DIENUM_STOP;   
} 

int main (void)
{  
   static uint16 DataReg,DataReg1;
   Sys_Hc_Ini();
   
     GUID HidGuid;

        // 查找本系统中HID类的GUID标识
        HidD_GetHidGuid(&HidGuid);
        printf("系统中HID类的GUID标识为:%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x\n",
               HidGuid.Data1,HidGuid.Data2 ,HidGuid.Data3 ,
               HidGuid.Data4[0],HidGuid.Data4[1],HidGuid.Data4[2],
               HidGuid.Data4[3],HidGuid.Data4[4],HidGuid.Data4[5],
               HidGuid.Data4[6],HidGuid.Data4[7]);
 
        // 准备查找符合HID规范的USB设备
        HDEVINFO hDevInfo = SetupDiGetClassDevs(
                                &HidGuid,
                                NULL,
                                NULL,
                                DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
 
        if (hDevInfo == INVALID_HANDLE_VALUE)
        {
                printf("符合HID规范的USB设备发生错误 \n");
                
                return 0;
        }

        printf("正在查找可用的USB设备。。。。 \n");

        DWORD MemberIndex = 0;
        SP_DEVICE_INTERFACE_DATA DeviceInterfaceData;
        BOOL bSuccess = FALSE;

        DeviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
        do
        {
                bSuccess = SetupDiEnumDeviceInterfaces(
                                        hDevInfo,
                                        NULL,
                                        &HidGuid,
                                        MemberIndex,
                                        &DeviceInterfaceData);
                if ((!bSuccess) && (GetLastError() == ERROR_NO_MORE_ITEMS))
                {
                        if(MemberIndex == 0)
                        {
                                printf("抱歉,未找到可用的USB设备! \n");
                        }
                        else
                        {
                                printf("没有更多的可用的USB设备! \n");
                        }
                        SetupDiDestroyDeviceInfoList(hDevInfo);
                        return 0;
                }
          
                printf("找到了一个USB设备: \n");
                // 若找到了一个USB设备,则获取该设备的细节信息
                PSP_DEVICE_INTERFACE_DETAIL_DATA pDeviceInterfaceDetailData;
                DWORD Length = 0;
          
                SetupDiGetDeviceInterfaceDetail(
                                hDevInfo,
                                &DeviceInterfaceData,
                                NULL,
                                0,
                                &Length,
                                NULL);
                pDeviceInterfaceDetailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(Length);
                pDeviceInterfaceDetailData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);  //MUST BE!!!

                if (!SetupDiGetDeviceInterfaceDetail(
                                        hDevInfo,
                                        &DeviceInterfaceData,
                                        pDeviceInterfaceDetailData,
                                        Length,
                                        NULL,
                                        NULL))
                {
                        printf("查找路径设备时出错! \n");
                }
                else
                {
                        printf("设备路径:%s ", pDeviceInterfaceDetailData->DevicePath);
                }
                //打开设备句柄
                HANDLE hDeviceHandle = CreateFile(pDeviceInterfaceDetailData->DevicePath,
                                GENERIC_READ | GENERIC_WRITE,
                                FILE_SHARE_READ | FILE_SHARE_WRITE,
                                NULL,
                                OPEN_EXISTING,
                                0,
                                NULL);
                if (hDeviceHandle == INVALID_HANDLE_VALUE)
                {
                        printf("打开设备路径出错! \n");
                        // printf("%d", GetLastError());
                }
                else
                {
                        HIDD_ATTRIBUTES Attributes;
                        HidD_GetAttributes(hDeviceHandle,&Attributes);

   /**************************************************/
   /*
                        long result;
   int ReadBuffer[10];
   int flag;
   do
   {
    ReadBuffer[9] = 0;
    ReadBuffer[8] = 0;
    ReadBuffer[7] = 0;
    ReadBuffer[6] = 0;
    ReadBuffer[5] = 0;
    ReadBuffer[4] = 0;
    ReadBuffer[3] = 0;
    ReadBuffer[2] = 0;
    ReadBuffer[1] = 0;
    ReadBuffer[0] = 0;
   result = HidD_GetFeature(
     hDeviceHandle,
     (PVOID)ReadBuffer,
     0x09);
  // for (int i = 0; i < ReadBuffer[1]; i++)
   {
    //printf("%d", ReadBuffer[i]);
   } 
   }

   //scanf("%d", &flag);
   while(1);
   */
   HRESULT hr; 
   HINSTANCE hInstance = GetModuleHandle(NULL) ;//获取实例句柄;

   //建立DirectInput接口 
     if(NULL == m_lpDI) 
     {
         hr = DirectInput8Create(hInstance,
            DIRECTINPUT_VERSION, 
            IID_IDirectInput8, 
            (void**)&m_lpDI, //接口取值 
            NULL); 

     hr = m_lpDI->EnumDevices(DI8DEVCLASS_GAMECTRL, 
                              (LPDIENUMDEVICESCALLBACK)DIEnumDevicesCallback,     //回调函数 
                                     &myGuid,           //赋值GUID 
                                            DIEDFL_ATTACHEDONLY);   //扫描安装好的和连接好的设备 

     }
   /**************************************************/
                        //将有关该设备的标识显示出来
                        printf("供应商ID :0X%04X ",Attributes.VendorID);
                        printf("产品ID :0X%04X ",Attributes.ProductID);
                        printf("产品版本号:0X%04X ",Attributes.VersionNumber);
           
                        WCHAR mString[256];
                        TCHAR Buffer[256];

                        HidD_GetManufacturerString(hDeviceHandle,mString,sizeof(mString));
                        if (wcstombs((char *)Buffer,mString,256) == -1)  // fail
                        {
                                Buffer[0] = NULL;
                        }
                        printf("生产商: %s ",Buffer);
   
                        HidD_GetProductString(hDeviceHandle,mString,sizeof(mString));
                           if (wcstombs((char *)Buffer,mString,256) == -1)
                           {
                                Buffer[0] = NULL;
                           }
                           printf("产品名称: %s ",Buffer);

                           // 通信:
                           PHIDP_PREPARSED_DATA pHidpPreparsedData;
                           HIDP_CAPS hidPCaps;
                           if (!HidD_GetPreparsedData(hDeviceHandle, &pHidpPreparsedData))
                           {
                                printf("获取 HID PREPARED DATA 失败! \n");
                                return 0;
                           }
                           NTSTATUS status = HidP_GetCaps(pHidpPreparsedData,&hidPCaps);
                           if (status == HIDP_STATUS_SUCCESS)
                           {
                            printf("CAP信息如下: \n");
                            printf("     InputReportByteLength %d ", hidPCaps.InputReportByteLength);
                            printf("     OutputReportByteLength %d ", hidPCaps.OutputReportByteLength);
                           }
                        
                        DWORD nReadBytes = 0;
                        BYTE *pInputReport = new BYTE[hidPCaps.InputReportByteLength];
                        memset(pInputReport,0,hidPCaps.InputReportByteLength);
          
                        do
                        { 
                                ReadFile(hDeviceHandle,
                                         pInputReport,
                                         hidPCaps.InputReportByteLength,
                                         &nReadBytes,
                                         NULL);
          
                                if (hidPCaps.InputReportByteLength == nReadBytes)
                                {
                                        for (unsigned int i = 0; i< nReadBytes - 1;i++)
                                        {
                                                printf("%02x-",pInputReport[i]);
                                        }
                                        printf("%02x ",pInputReport[nReadBytes-1]);
                                }
                               // if (pInputReport[nReadBytes-2] == 0x20)  //break the loop when pressing a specific key
                                {
                               //         printf(" \n");
                                        break;
                                }
                                Sleep(10);
                        }while (hidPCaps.InputReportByteLength == nReadBytes);

                        // 释放句柄资源
                        CloseHandle(hDeviceHandle);
                }
    
                MemberIndex++;
        }while(bSuccess);
        SetupDiDestroyDeviceInfoList(hDevInfo);

        return 0;
     
    while(1)
    {
        outcommand(0x00);
        DataReg1 = 1; 
        outcommand(0x02);
      outdata(0x01);
         DataReg1 = 1; 
         outcommand(0x01); 
        DataReg = indata();
        DataReg1 = 1;    
    }
  
  
}
/*********************************************************************************************************
**                            End Of File
********************************************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -