1、完善下位机程序
1)修改Chap_9.c文件中的control_handler函数,内容如下:
-
-
-
-
- void control_handler(void)
- {
- INT8U type, req;
- INT16U wValue;
-
-
- type = ControlData.DeviceRequest.bmRequestType & USB_REQUEST_TYPE_MASK;
-
- req = ControlData.DeviceRequest.bRequest & USB_REQUEST_MASK;
-
- wValue = ControlData.DeviceRequest.wValue;
-
- if (type == USB_STANDARD_REQUEST)
- (*StandardDeviceRequest[req])();
- else if (type == USB_VENDOR_REQUEST)
- (*VendorDeviceRequest[req])(wValue);
-
-
- else
- stall_ep0();
- }
2)在Chap_9.c文件中增加以下函数:
- sbit K1 = P3^5;
- sbit K2 = P3^6;
-
-
-
-
-
-
- void control_led(INT16U wValue)
- {
- P0 = wValue % 256;
-
- single_transmit(0, 0);
- }
-
-
-
-
-
-
- void get_key_state(INT16U wValue)
- {
- INT8U ucKeyState[1],i;
-
- ucKeyState[0] = 0x00;
- K1 = 1;
- K2 = 1;
- for(i=0;i<100;i++);
-
- if(~K1)
- {
- ucKeyState[0] |= 0x01;
- }
-
- if(~K2)
- {
- ucKeyState[0] |= 0x02;
- }
-
- single_transmit(ucKeyState,1);
- }
-
-
- code void (*VendorDeviceRequest[]) (INT16U wValue) =
- {
- control_led,
- get_key_state
- };
3)向Chap_9.h中增加以下内容
- extern code void (*VendorDeviceRequest[])(INT16U wValue);
- extern void control_led(INT16U wValue);
- extern void get_key_state(INT16U wValue);
下载修改好后的源代码
2、修改Windows驱动程序
1)完善IOCTL_LED_Handler函数,内容如下
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- NTSTATUS Easy_USB_51_ProgramerDevice::IOCTL_LED_Handler(KIrp I)
- {
- NTSTATUS status = STATUS_SUCCESS;
-
- t << "Entering Easy_USB_51_ProgramerDevice::IOCTL_LED_Handler, " << I << EOL;
-
-
-
- if(I.IoctlOutputBufferSize() || !I.IoctlBuffer())
- {
- status = STATUS_INVALID_PARAMETER;
- return status;
- }
-
-
-
-
-
- PURB pUrb = m_Lower.BuildVendorRequest(NULL,
- 0,
- 0,
- CMD_CTL_LED,
- (UCHAR)(*(PUCHAR)I.IoctlBuffer()),
- FALSE,
- TRUE,
- NULL,
- 0
- );
-
- if(pUrb)
- {
-
- status = m_Lower.SubmitUrb(pUrb, NULL, NULL, 1000L);
- }
- else
- {
- status = STATUS_PENDING;
- }
-
- delete pUrb;
-
-
- if(NT_SUCCESS(status))
- {
- I.Information() = pUrb->UrbControlVendorClassRequest.TransferBufferLength;
- }
- else
- {
- I.Information() = 0;
- }
-
- I.Status() = status;
-
- return status;
- }
2)完善IOCTL_GET_KEY_Handler函数,内容如下
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- NTSTATUS Easy_USB_51_ProgramerDevice::IOCTL_GET_KEY_Handler(KIrp I)
- {
- NTSTATUS status = STATUS_SUCCESS;
-
- t << "Entering Easy_USB_51_ProgramerDevice::IOCTL_GET_KEY_Handler, " << I << EOL;
-
-
-
-
-
-
-
- PURB pUrb = m_Lower.BuildVendorRequest((PUCHAR)I.IoctlBuffer(),
- 1,
- 0,
- CMD_GET_KEY_STATUS,
- 0,
- TRUE,
- TRUE,
- NULL,
- 0
- );
-
- if(pUrb)
- {
-
- status = m_Lower.SubmitUrb(pUrb, NULL, NULL, 1000L);
- }
- else
- {
- status = STATUS_PENDING;
- }
-
- delete pUrb;
-
-
-
- if(NT_SUCCESS(status))
- {
- I.Information() = pUrb->UrbControlVendorClassRequest.TransferBufferLength;
- }
- else
- {
- I.Information() = 0;
- }
-
- I.Status() = status;
-
- return status;
- }
3)在Easy_USB_51_ProgramerDevice.h中加入如下宏定义:
- #define CMD_CTL_LED 0x00
- #define CMD_GET_KEY_STATUS 0x01
3、修改测试程序
1)完善Test_IOCTL_LED函数,内容如下:
-
-
-
-
-
-
-
-
-
- void Test_IOCTL_LED(void)
- {
-
-
-
-
-
- CHAR bufInput[IOCTL_INBUF_SIZE];
- CHAR bufOutput[IOCTL_OUTBUF_SIZE];
- ULONG nOutput;
-
- bufInput[0] = 0x03;
-
-
- printf("Issuing Ioctl to device - ");
- if (!DeviceIoControl(hDevice,
- IOCTL_LED,
- bufInput,
- IOCTL_INBUF_SIZE,
- NULL,
- 0,
- NULL,
- NULL)
- )
- {
- printf("ERROR: DeviceIoControl returns %0x.", GetLastError());
- Exit(1);
- }
- }
2)完善Test_IOCTL_GET_KEY函数,内容如下
-
-
-
-
-
-
-
-
-
- void Test_IOCTL_GET_KEY(void)
- {
-
-
-
-
-
- CHAR bufInput[IOCTL_INBUF_SIZE];
- CHAR bufOutput[IOCTL_OUTBUF_SIZE];
- ULONG nOutput;
-
-
-
- printf("Issuing Ioctl to device - ");
- if (!DeviceIoControl(hDevice,
- IOCTL_GET_KEY,
- NULL,
- 0,
- bufOutput,
- IOCTL_OUTBUF_SIZE,
- &nOutput,
- NULL)
- )
- {
- printf("ERROR: DeviceIoControl returns %0x.\n", GetLastError());
- Exit(1);
- }
- else if(nOutput == 0)
- {
- printf("ERROR: DeviceIoControl complete, but Device not respond");
- }
- else
- {
- printf("Device responsed data:0x%.2x\n",bufOutput[0]);
-
- if(bufOutput[0] & 0x01)
- {
- printf("K1 Pressed\n");
- }
- else
- {
- printf("K1 Unpressed\n");
- }
-
- if(bufOutput[0] & 0x02)
- {
- printf("K2 Pressed\n");
- }
- else
- {
- printf("K2 Unpressed\n");
- }
- }
- }
3)编译驱动程序和测试程序,重新安装驱动程序
下载编译好的驱动
下载编译好后的测试程序
下载驱动和测试程序源代码(需要放到C:\Easy_USB_51_Programer目录)
4)在命令行窗口中运行测试程序,键入:TestApp i 0,代表发送请求代码为0x00的厂商请求,这个请求是控制LED的,运行结果如下:
运行testapp i 0 后发现扩展板上的D0和D1被点亮了
(呵呵,我已经投PCB了,所以没有用手工制作的那个原始东东来展示)
这是用Bus Hound抓到的数据
5)在命令行窗口中运行测试程序,键入:TestApp i 1,代表发送请求代码为0x01的厂商请求,这个请求是读取扩展板EXT-BOARD-A上按键状的,运行结果如下:
下图是用BusHound抓到的数据