:

: USB

2011-04-11 11:50:25

: . - (5 USB, 12 ), . / . .

: , .

: / USB,

, purelogic.ru 8 , COM-. , . L7805, atmega8, uln2308, st232, . , , " " ...

- :

, . - ( ). , .

USB, v-usb. , . , USB-COM , . . - , , .

, , . ULN2003, 7 . , 4 .

V-USB. / , . . 8 , 8 ().

: USB - AVR USB (V-USB) libusb . :

Firmware ( )

hid-custom-rq v-usb. . , :

#defineLED_PORT_DDRDDRC
#defineLED_PORT_OUTPUTPORTC
#defineLED_PORT_INPUT PORTB
#defineLED0_BIT0
#defineLED1_BIT1
#defineLED2_BIT2

#include
#include
#include/*forsei()*/
#include/*for_delay_ms()*/

#include/*requiredbyusbdrv.h*/
#include"usbdrv.h"
#include"oddebug.h"/*Thisisalsoanexampleforusingdebugmacros*/
#include"requests.h"/*Thecustomrequestnumbersweuse*/

PROGMEMcharusbHidReportDescriptor[22]={
/*USBreportdescriptor*/
0x06, 0x00, 0xff,
//USAGE_PAGE(GenericDesktop)
0x09, 0x01,
//USAGE(VendorUsage1)
0xa1, 0x01,
//COLLECTION(Application)
0x15, 0x00,
//LOGICAL_MINIMUM(0)
0x26, 0xff, 0x00,
//LOGICAL_MAXIMUM(255)
0x75, 0x08,
//REPORT_SIZE(8)
0x95, 0x01,
//REPORT_COUNT(1)
0x09, 0x00,
//USAGE(Undefined)
0xb2, 0x02, 0x01,
//FEATURE(Data,Var,Abs,Buf)
0xc0
//END_COLLECTION
};


/*-------------------------------------------------------------------------*/

usbMsgLen_tusbFunctionSetup(uchardata[8
])
{
usbRequest_t *rq =(void *)data
;

if((
rq->bmRequestType & USBRQ_TYPE_MASK)== USBRQ_TYPE_VENDOR
){
if(
rq->bRequest == CUSTOM_RQ_SET_STATUS
){

if(
rq->wValue.bytes[0]& 1){
/*setLED*/
LED_PORT_OUTPUT |= _BV(LED0_BIT
);
}else{
/*clearLED*/
LED_PORT_OUTPUT &=~_BV(LED0_BIT
);
}
if(
rq->wValue.bytes[0]& 2){
/*setLED*/
LED_PORT_OUTPUT |= _BV(LED1_BIT
);
}else{
/*clearLED*/
LED_PORT_OUTPUT &=~_BV(LED1_BIT
);
}
if(
rq->wValue.bytes[0]& 4){
/*setLED*/
LED_PORT_OUTPUT |= _BV(LED2_BIT
);
}else{
/*clearLED*/
LED_PORT_OUTPUT &=~_BV(LED2_BIT
);
}

}elseif(
rq->bRequest == CUSTOM_RQ_GET_STATUS
){

static
uchardataBuffer[1];
/*buffermuststayvalidwhenusbFunctionSetupreturns*/
dataBuffer[0]= LED_PORT_INPUT
;
usbMsgPtr = dataBuffer;
/*tellthedriverwhichdatatoreturn*/
return 1;
/*tellthedrivertosend1byte*/

}
}
return
0;
/*defaultfornotimplementedrequests:returnnodatabacktohost*/
}

/*-------------------------------------------------------------------------*/

int__attribute__((noreturn)) main(void
)
{
uchari
;

wdt_enable(WDTO_1S
);
/*Evenifyoudon'tusethewatchdog,turnitoffhere.Onnewerdevices,
*thestatusofthewatchdog(on/off,period)isPRESERVEDOVERRESET!
*/
/*RESETstatus:allportbitsareinputswithoutpull-up.
*That'sthewayweneedD+andD-.Thereforewedon'tneedany
*additionalhardwareinitialization.
*/
odDebugInit
();
usbInit
();
usbDeviceDisconnect();
/*enforcere-enumeration,dothiswhileinterruptsaredisabled!*/
i = 0
;
while(--
i){
/*fakeUSBdisconnectfor>250ms*/
wdt_reset
();
_delay_ms(1
);
}
usbDeviceConnect
();
LED_PORT_DDR |= _BV(LED0_BIT);
/*maketheLEDbitanoutput*/
LED_PORT_DDR |= _BV(LED1_BIT);
/*maketheLEDbitanoutput*/
LED_PORT_DDR |= _BV(LED2_BIT);
/*maketheLEDbitanoutput*/
sei
();
for(;;){
/*maineventloop*/
wdt_reset
();
usbPoll
();
}
}

, , , , ...: ( ) .

,

, . , , (0...7), '0' . d{1,3} . , V-USB, C :)

#include
#include
#include
#include/*thisislibusb*/
#include"opendevice.h"/*commoncodemovedtoseparatemodule*/

#include"../firmware/requests.h"/*customrequestnumbers*/
#include"../firmware/usbconfig.h"/*device'sVID/PIDandnames*/

static voidusage(char *name
)
{
fprintf(stderr, "usage: "
);
fprintf(stderr, "%snumber....set relay (use 1, 2, 4 and combinations for relays) ", name
);
fprintf(stderr, "%sstatus...askcurrentstatusofinputs ", name);
}

intmain(intargc, char **argv
)
{
usb_dev_handle *handle = NULL
;
const
unsignedcharrawVid[2]={USB_CFG_VENDOR_ID}, rawPid[2]={USB_CFG_DEVICE_ID
};
charvendor[]={USB_CFG_VENDOR_NAME, 0}, product[]={USB_CFG_DEVICE_NAME, 0
};
charbuffer[4
];
intcnt, vid, pid, isOn
;

usb_init
();
if(
argc < 2){
/*weneedatleastoneargument*/
usage(argv[0
]);
exit(
1
);
}
/*computeVID/PIDfromusbconfig.hsothatthereisacentralsourceofinformation*/
vid = rawVid[1]* 256 + rawVid[0
];
pid = rawPid[1]* 256 + rawPid[0
];
/*Thefollowingfunctionisinopendevice.c:*/
if(usbOpenDevice(&handle, vid, vendor, pid, product, NULL, NULL, NULL)!= 0
){
fprintf(stderr, "CouldnotfindUSBdevice"%s"withvid=0x%xpid=0x%x ", product, vid, pid
);
exit(
1
);
}

if(strcasecmp(argv[1], "status")== 0
){
cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, CUSTOM_RQ_GET_STATUS, 0, 0, buffer, sizeof(buffer), 5000
);
if(
cnt < 1
){
if(
cnt < 0
){
fprintf(stderr, "USBerror:%s ", usb_strerror
());
}else{
fprintf(stderr, "only%dbytesreceived. ", cnt
);
}
}else{
printf("RELAYis%d ", buffer[0
]);
}
}else{
cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, CUSTOM_RQ_SET_STATUS, argv[1][0]- '0', 0, buffer, 0, 5000
);
if(
cnt < 0
){
fprintf(stderr, "USBerror:%s ", usb_strerror
());
}
}
usb_close(handle
);
return
0
;
}

, c#, windows form

LibUsb C#. LibUsbDotNet, :http://sourceforge.net/projects/libusbdotnet/ . , , , :

//

usingLibUsbDotNet
;
usingLibUsbDotNet.Info
;
usingLibUsbDotNet.Main
;

//()

publicstatic UsbDeviceMyUsbDevice
;
publicstatic
UsbDeviceFinderMyUsbFinder =new UsbDeviceFinder(0x16c0, 0x05df);

. 1, ( , ):

private voidbutton16_Click(objectsender, EventArgse)
{
MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder
);
if(
MyUsbDevice == null)thrownew Exception("DeviceNotFound."
);


UsbSetupPacketpacket =new UsbSetupPacket((byte)(UsbCtrlFlags.RequestType_Vendor | UsbCtrlFlags.Recipient_Device | UsbCtrlFlags.Direction_Out), 1,(short)1, 0, 0
);
intcountIn
;
byte[] data =new byte[1
];
MyUsbDevice.ControlTransfer(refpacket, data, 0, outcountIn
);
}

:

private voidbutton17_Click(objectsender, EventArgse)
{
MyUsbDevice = UsbDevice.OpenUsbDevice(MyUsbFinder
);
if(
MyUsbDevice == null)thrownew Exception("DeviceNotFound."
);

UsbSetupPacketpacket =new UsbSetupPacket((byte)(UsbCtrlFlags.RequestType_Vendor | UsbCtrlFlags.Recipient_Device | UsbCtrlFlags.Direction_In), 2,(short)0,(short)0,(short)0
);
intcountIn
;
byte[] data =new byte[1
];
if(
MyUsbDevice.ControlTransfer(refpacket, data, 1, outcountIn)&&(countIn == 1
))
{
MessageBox.Show("readok,value=" + data[0].ToString
());
}
}

libusb/bin/inf-wizard.exe . , USB .

:http://oleg.milantiev.com/files/v-usb-atmega8-relay.zip

 

: +7 (928) 425-32-10
e-mail: