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

📄 opensourcebdm_usb.c

📁 HCS08仿真器资料(资料详细)
💻 C
字号:
/*
    Open Source BDM - USB communication
    Copyright (C) 2005

	Change History:
	 ------------------
	 *****Prominent Notice-This software was modified from TBDML software - 12/05


    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/


#include <stdio.h>
#include "opensourcebdm_usb.h"
#include "log.h"
#include "opensourcebdm_hwdesc.h"
#include "libusb.h"

struct usb_device *opensourcebdm_devs[10]={NULL};	/* array pointing to all Open Source BDM devices found terminated by NULL pointer */
usb_dev_handle *opensourcebdm_devh=NULL;

/* provides low level USB functions which talk to opensourcebdm hardware */

/* initialisation */
void opensourcebdm_usb_init(void) {
	usb_init();				/* init LIBUSB */
}

/* find all opensourcebdm devices attached to the computer */
void opensourcebdm_usb_find_devices(void) {
	struct usb_bus *libusb_bus;
	struct usb_device *libusb_dev;
  	struct usb_version *version;
	unsigned int i=0;
	opensourcebdm_devs[0]=NULL;				/* terminate the list */
	version = usb_get_version();
	if (version==NULL) {
		/* something quite wrong... */
		return;
	}
    print("USBLIB DLL version: %i.%i.%i.%i\r\n",version->dll.major,version->dll.minor,version->dll.micro,version->dll.nano);
	if (version->driver.major==-1) {
		/* driver not running! */
		print("USB: Driver not running\r\n");
		return;
	}
	print("USBLIB Driver version: %i.%i.%i.%i\r\n",version->driver.major, version->driver.minor,version->driver.micro, version->driver.nano);
	usb_find_busses();		/* enumerate all busses */
	usb_find_devices();		/* enumerate all devices */
	for (libusb_bus = usb_get_busses(); libusb_bus; libusb_bus = libusb_bus->next) {		/* scan through all busses */
    	for (libusb_dev = libusb_bus->devices; libusb_dev; libusb_dev = libusb_dev->next) {	/* scan through all devices */
  			if ((libusb_dev->descriptor.idVendor==opensourcebdm_VID)&&(libusb_dev->descriptor.idProduct==opensourcebdm_PID)) {
				/* found a device */
				opensourcebdm_devs[i++]=libusb_dev;	/* add pointer to the device */
				opensourcebdm_devs[i]=NULL;			/* terminate the list again */
				if (i>=(sizeof(opensourcebdm_devs)/sizeof(struct usb_device*))) return;	/* too many devices! */
			}
		}
	}
}

/* returns number of attached opensourcebdm devices */
unsigned int opensourcebdm_usb_cnt(void) {
	unsigned int i=0;
	while ((i<(sizeof(opensourcebdm_devs)/sizeof(struct usb_device*)))&&(opensourcebdm_devs[i]!=NULL)) i++;	/* count the entries */
	return(i);
}

/* open connection to device enumerated by find_opensourcebdm_devices */
/* returns 0 on success and 1 on error */
unsigned char opensourcebdm_usb_open(unsigned int device_no) {
	opensourcebdm_devh = usb_open(opensourcebdm_devs[device_no]);
	if (opensourcebdm_devh==NULL) return (1);
	print("USB Device open\r\n");
	if (usb_set_configuration(opensourcebdm_devh,1)) return(1);		/* opensourcebdm has only one valid configuration */
	print("USB Configuration set\r\n");
	if (usb_claim_interface(opensourcebdm_devh,0)) return(1);		/* opensourcebdm has only 1 interface */
	print("USB Interface claimed\r\n");
	return (0);
}

/* closes connection to the currently open device */
void opensourcebdm_usb_close(void) {
	if (opensourcebdm_devh!=NULL) {
		usb_release_interface(opensourcebdm_devh,0);		/* release the interface */
		print("USB Interface released\r\n");
		usb_close(opensourcebdm_devh);						/* close the device */
		print("USB Device closed\r\n");
		opensourcebdm_devh=NULL;							/* indicate that no device is open */
	}
}

/* Message data format:
				1 byte: size of cmd+data
			    1 byte: cmd
				(size-1) bytes: data
*/

/* sends a message to the opensourcebdm device over EP0 */
/* returns 0 on success and 1 on error */
/* since the EP0 transfer is unidirectional in this case, data returned by the device must be read separately */
unsigned char opensourcebdm_usb_send_ep0(unsigned char * data) {
	unsigned char * count = data;		/* data count is the first byte of the message */
 	int i;
	if (opensourcebdm_devh==NULL) {
		print("USB EP0 send: device not open\r\n");
		return(1);
	}
	print("USB EP0 send:\r\n");
	print_dump(data,(*count)+1);
	i=usb_control_msg(opensourcebdm_devh, 0x40, *(data+1), (*(data+2))+256*(*(data+3)), (*(data+4))+256*(*(data+5)), data+6, ((*count)>5)?((*count)-5):0, TIMEOUT);
	if (i<0) return(1); else return(0);
}

/* sends a message to the opensourcebdm device over EP0 which instruct the device to exec command and return data */
/* returns 0 on success and 1 on error */
/* data count in the message is number of bytes EXPECTED/REQUIRED from the device */
/* the device will get the cmd number and 4 following data bytes */
unsigned char opensourcebdm_usb_recv_ep0(unsigned char * data) {
	unsigned char count = *data;		/* data count is the first byte of the message */
 	int i;
	if (opensourcebdm_devh==NULL) {
		print("USB EP0 receive request: device not open\r\n");
		return(1);
	}
	print("USB EP0 receive request:\r\n");
	print_dump(data,6);
	i=usb_control_msg(opensourcebdm_devh, 0xC0, *(data+1), (*(data+2))+256*(*(data+3)), (*(data+4))+256*(*(data+5)), data, count, TIMEOUT);
	print("USB EP0 receive:\r\n");
	print_dump(data,count);
	if (i<0) return(1); else return(0);
}

/* sends a message to the opensourcebdm device over EP2 */
/* returns 0 on success and 1 on error */
/* data the device wants to return need to be read out in separate recv transaction */
unsigned char opensourcebdm_usb_send_ep2(unsigned char *data) {
	unsigned char * count = data;		/* data count is the first byte of the message */
 	int i;
	if (opensourcebdm_devh==NULL) {
		print("USB EP2 send: device not open\r\n");
		return(1);
	}
	print("USB EP2 send:\r\n");
	print_dump(data,(*count)+1);
	i=usb_bulk_write(opensourcebdm_devh, 0x02, data, (*count)+1, TIMEOUT);
	if (i<0) return(1); else return(0);
}

/* receives data from EP2 */
/* returns 0 on success and 1 on error */
/* data count in the message is number of bytes EXPECTED/REQUIRED from the device */
unsigned char opensourcebdm_usb_recv_ep2(unsigned char * data) {
	unsigned char count = *data;		/* data count is the first byte of the message */
 	int i;
	if (opensourcebdm_devh==NULL) {
		print("USB EP2 receive request: device not open\r\n");
		return(1);
	}
	print("USB EP2 receive request:\r\n");
	print_dump(data,6);
	i=usb_bulk_read(opensourcebdm_devh, 0x82, data, count, TIMEOUT);
	print("USB EP2 receive (%d byte(s)):\r\n",i);
	print_dump(data,count);
	if (i<0) return(1); else return(0);
}


⌨️ 快捷键说明

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