|
Reply 2# myralolx92
alaaa yeke ... ni ader program contoh dari kedai tu,and aku tak paham lansung ape mksud dia,boleh tolong explainkan kalo tahu..time kaseyyy
/* For TECHNO KIT */
// #define USB_VENDOR 0x12ed
// #define USB_PRODUCT 0x1003
/* For USB-An */
#define USB_VENDOR 0x1352
#define USB_PRODUCT 0x0110
/* Init USB */
struct usb_bus *morphy_init()
{
usb_init();
usb_find_busses();
usb_find_devices();
return( usb_get_busses() );
}
/* Find Morphy-USBIO */
struct usb_device *morphy_find(struct usb_bus *busses, struct usb_device *dev)
{
struct usb_bus *bus;
for(bus=busses; bus; bus=bus->next){
for(dev=bus->devices; dev; dev=dev->next) {
if( (dev->descriptor.idVendor==USB_VENDOR) && (dev->descriptor.idProduct==USB_PRODUCT) ){
return( dev );
}
}
}
return( NULL );
}
/* USBIO Open */
struct usb_dev_handle *morphy_open(struct usb_device *dev)
{
struct usb_dev_handle *udev = NULL;
udev=usb_open(dev);
if( (udev=usb_open(dev))==NULL ){
printf("usb_open Error.(%s)\n",usb_strerror());
exit(1);
}
if( usb_set_configuration(udev,dev->config->bConfigurationValue)<0 ){
if( usb_detach_kernel_driver_np(udev,dev->config->interface->altsetting->bInterfaceNumber)<0 ){
printf("usb_set_configuration Error.\n");
printf("usb_detach_kernel_driver_np Error.(%s)\n",usb_strerror());
}
}
if( usb_claim_interface(udev,dev->config->interface->altsetting->bInterfaceNumber)<0 ){
if( usb_detach_kernel_driver_np(udev,dev->config->interface->altsetting->bInterfaceNumber)<0 ){
printf("usb_claim_interface Error.\n");
printf("usb_detach_kernel_driver_np Error.(%s)\n",usb_strerror());
}
}
if( usb_claim_interface(udev,dev->config->interface->altsetting->bInterfaceNumber)<0 ){
printf("usb_claim_interface Error.(%s)\n",usb_strerror());
}
return(udev);
}
/* USBIO Close */
void morphy_close(struct usb_dev_handle *udev)
{
if( usb_close(udev)<0 ){
printf("usb_close Error.(%s)\n",usb_strerror());
}
}
/* USBIO out */
void uio_out(struct usb_dev_handle *udev, int PortNo, int OutData)
{
unsigned char ctl[64]; //8-> 64
ctl[0]=0x01+(PortNo&1);
ctl[1]=OutData;
ctl[2]=ctl[3]=ctl[4]=ctl[5]=ctl[6]=ctl[7]=0;
usb_control_msg(udev,0x21,0x09,0x00,0x00,ctl,64,5000); //8-> 64
}
/* USBIO in*/
int uio_in(struct usb_dev_handle *udev, int PortNo )
{
int i,ret;
unsigned char ctl[64]; //8-> 64
unsigned char indat[64]; //8-> 64
static n=0;
n++;
/* CMD:Read */
ctl[0]=0x03+(PortNo&1);
ctl[1]=ctl[2]=ctl[3]=ctl[4]=ctl[5]=ctl[6]=0;
ctl[7]=n;
ret=usb_control_msg(udev,0x21,0x09,0x00,0x00,ctl,64,5000); //8-> 64
if( ret<0 ){
printf("Read-CmdError\n");
return (-1);
}
i=0;
do {
ret=usb_interrupt_read(udev,1,indat,64,5000); //8-> 64
if( ret<0 ){
printf("Int-Read Error\n");
return (-2);
}
if (++i>100){
printf("Int-Read Error\n");
return (-3);
}
}while (ctl[7]!=indat[7]);
//ret=usb_interrupt_read(udev,1,indat,8,5000); //DEL
return (indat[1]);
}
/* fsio_io */
int fsio_io(struct usb_dev_handle *udev, unsigned char *outData, unsigned char *inData)
{
int ret;
// ret=usb_control_msg(udev,0x01,0x09,0x00,0x00,outData,64,5000);
ret = usb_bulk_write(udev, 1, outData, 64, 5000); //
// ret=usb_interrupt_read(udev,1,inData,64,1000);
if( ret < 0 ){
printf("Read-CmdError\n");
return(-1);
}
//ret=usb_interrupt_read(udev,1,inData,64,1000);
ret = usb_bulk_read(udev, 1, inData, 64, 5000);
if( ret < 0 ){
printf("Int-Read Error:%d\n", ret);
return(-2);
}
}
int fsio_ai(struct usb_dev_handle *udev, float *voltage)
{
int ret, i;
unsigned char uin[64];
unsigned char ctl[64] = {0x2A,0x01,0x00,0x00,
0x02,0x00,0x00,0x03,
0x00,0x00,0x04,0x00,
0x00,0x05,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x05};
ret = usb_bulk_write(udev, 1, ctl, 64, 5000);
if( ret < 0 ){
printf("Read-CmdError\n");
return(-1);
}
ret = usb_bulk_read(udev, 1, uin, 64, 5000);
if( ret < 0 ){
printf("Int-Read Error:%d\n", ret);
return(-2);
}
for(i = 0; i <= 5; i++){
voltage = (float)(uin[3]*256 + uin[2] ) * 5000 / 1023;
printf("%x;%x;%f\n",uin[3],uin[2],voltage);
}
return(1);
}
int fsio_rom_read(struct usb_dev_handle *udev, unsigned char *uin)
{
int ret, i;
unsigned char ctl[64] = {0xF8,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x05};
ret = usb_bulk_write(udev, 1, ctl, 64, 5000);
if( ret < 0 ){
printf("Read-CmdError\n");
return(-1);
}
ret = usb_bulk_read(udev, 1, uin, 64, 5000);
if( ret < 0 ){
printf("Int-Read Error:%d\n", ret);
return(-2);
}
printf("Channels:%x(%d)\n",uin[1],uin[1]);
printf("ort 2 Pull Up [0:Able, 1isable]:%x(%d)\n",uin[2],uin[2]);
printf("ort 1 Input Pin:%x\n",uin[5]);
printf("ort 2 Input Pin:%x\n",uin[6]);
printf("ort 3 Input Pin:%x\n",uin[7]);
printf("ort 1 Output:%x\n",uin[9]);
printf("ort 2 Output:%x\n",uin[10]);
printf("ort 3 Output:%x\n",uin[11]);
printf("ulse Timing:%x(%f) us\n", uin[13], (1158 - (float)uin[13] * 4) * (4/48));
printf("WM [0isable, 1:Able]:%x(%d)\n",uin[14],uin[14]);
return(1);
} |
|