You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

188 lines
6.9 KiB

/* Name: set-led.c
* Project: custom-class, a basic USB example
* Author: Christian Starkjohann
* Creation Date: 2008-04-10
* Tabsize: 4
* Copyright: (c) 2008 by OBJECTIVE DEVELOPMENT Software GmbH
* License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt)
* This Revision: $Id: set-led.c 692 2008-11-07 15:07:40Z cs $
*/
/*
General Description:
This is the host-side driver for the custom-class example device. It searches
the USB for the LEDControl device and sends the requests understood by this
device.
This program must be linked with libusb on Unix and libusb-win32 on Windows.
See http://libusb.sourceforge.net/ or http://libusb-win32.sourceforge.net/
respectively.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <usb.h> /* this is libusb */
#include "opendevice.h" /* common code moved to separate module */
#include <arpa/inet.h> //for htons()
#include <unistd.h>
#include "../firmware/masterchip/usbdrv/usbconfig.h" /* device's VID/PID and names */
void usage() {
printf("Usage:\tread-temp [spi|temp|dbg] [[opcode] [addr] [value]]\n\tread-temp [offset] [channel]\n");
}
int main(int argc, char **argv)
{
usb_dev_handle *handle = NULL;
const unsigned char rawVid[2] = {USB_CFG_VENDOR_ID}, rawPid[2] = {USB_CFG_DEVICE_ID};
char vendor[] = {USB_CFG_VENDOR_NAME, 0}, product[] = {USB_CFG_DEVICE_NAME, 0};
char buffer[4];
int cnt, vid, pid;
usb_init();
/* compute VID/PID from usbconfig.h so that there is a central source of information */
vid = rawVid[1] * 256 + rawVid[0];
pid = rawPid[1] * 256 + rawPid[0];
/* The following function is in opendevice.c: */
if(usbOpenDevice(&handle, vid, vendor, pid, product, NULL, NULL, NULL) != 0){
fprintf(stdout, "Could not find USB device \"%s\" with vid=0x%x pid=0x%x\n", product, vid, pid);
usage();
exit(1);
}
/* Since we use only control endpoint 0, we don't need to choose a
* configuration and interface. Reading device descriptor and setting a
* configuration and interface is done through endpoint 0 after all.
* However, newer versions of Linux require that we claim an interface
* even for endpoint 0. Enable the following code if your operating system
* needs it: */
#if 0
int retries = 1, usbConfiguration = 1, usbInterface = 0;
if(usb_set_configuration(handle, usbConfiguration) && showWarnings){
fprintf(stdout, "Warning: could not set configuration: %s\n", usb_strerror());
}
/* now try to claim the interface and detach the kernel HID driver on
* Linux and other operating systems which support the call. */
while((len = usb_claim_interface(handle, usbInterface)) != 0 && retries-- > 0){
#ifdef LIBUSB_HAS_DETACH_KERNEL_DRIVER_NP
if(usb_detach_kernel_driver_np(handle, 0) < 0 && showWarnings){
fprintf(stdout, "Warning: could not detach kernel driver: %s\n", usb_strerror());
}
#endif
}
#endif
int rxValue, rxIndex;
int value = 0, index = 0;
if(strcasecmp(argv[1], "temp") == 0) {
int i=0;
cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, 100, value, index, buffer, sizeof(buffer), 5000);
if(cnt < 0){
fprintf(stdout, "\nUSB error in iteration %d: %s\n", i, usb_strerror());
}
rxValue = ((int)buffer[1] & 0xff) | (((int)buffer[0] & 0xff) << 8);
rxIndex = ((int)buffer[3] & 0xff) | (((int)buffer[2] & 0xff) << 8);
fprintf(stdout, "CORRECT READOUT");
fprintf(stdout, "0: %3d.%02d*C \n", rxValue/100, rxValue%100);
fprintf(stdout, "1: %3d.%02d*C \n", rxIndex/100, rxIndex%100);
cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, 101, value, index, buffer, sizeof(buffer), 5000);
if(cnt < 0){
fprintf(stdout, "\nUSB error in iteration %d: %s\n", i, usb_strerror());
}
rxValue = ((int)buffer[1] & 0xff) | (((int)buffer[0] & 0xff) << 8);
rxIndex = ((int)buffer[3] & 0xff) | (((int)buffer[2] & 0xff) << 8);
fprintf(stdout, "2: %3d.%02d*C \n", rxValue/100, rxValue%100);
fprintf(stdout, "3: %3d.%02d*C \n", rxIndex/100, rxIndex%100);
} else if(strcasecmp(argv[1], "spi") == 0) {
if(argc != 5) {
usage();
return 0;
}
int opcode, addr, value;
int i=0;
opcode = atoi(argv[2]);
addr = atoi(argv[3]);
value = atoi(argv[4]);
// x86 is little-endian, avr is big-endian
value = htons(value);
cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, opcode, addr, value, buffer, sizeof(buffer), 5000);
if(cnt < 0){
fprintf(stdout, "\nUSB error in iteration %d: %s\n", i, usb_strerror());
}
rxIndex = ((int)buffer[1] & 0xff) | (((int)buffer[0] & 0xff) << 8);
fprintf(stdout, "request = 0x%04x\n", opcode);
// fprintf(stdout, "answer: opcode=0x%02x addr=0x%02x value=0x%04x (%i)\n", buffer[1],buffer[0],rxIndex,rxIndex);
fprintf(stdout, "answer: value=0x%04x (%i)\n", rxIndex,rxIndex);
// fprintf(stdout, "rxValue = 0x%04x value = 0x%04x\n", rxValue, addr);
// fprintf(stdout, "rxIndex = 0x%04x index = 0x%04x\n", rxIndex, value);
} else if(strcasecmp(argv[1], "dbg") == 0) {
if(argc != 2) {
usage();
return 0;
}
int dbg_getchar[] = {6, 0, 0};
int *rq;
while(1) {
// check whether we have new
rq = dbg_getchar;
cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, rq[0], rq[1], rq[2], buffer, sizeof(buffer), 5000);
if(cnt < 0){
fprintf(stdout, "\nUSB error in iteration ?!?: %s\n", usb_strerror());
}
rxIndex = ((int)buffer[1] & 0xff) | (((int)buffer[0] & 0xff) << 8);
if(rxIndex > 256) {
usleep(10000);
} else {
fprintf(stdout, "%c", rxIndex);
fflush(stdout);
}
}
} else if(strcasecmp(argv[1], "offset") == 0) {
if(argc != 3) {
usage();
return 0;
}
int chan;
int *rq;
chan = atoi(argv[2]);
int offset_start[] = {6,1,chan};
int offset_stop[] = {6,2,chan};
rq = offset_start;
fprintf(stdout, "starting offset measuring for channel %i", chan);
cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, rq[0], rq[1], rq[2], buffer, sizeof(buffer), 5000);
if(cnt < 0){
fprintf(stdout, "\nUSB error in iteration ?!?: %s\n", usb_strerror());
}
sleep(10);
rq = offset_stop;
fprintf(stdout, "stopping and retrieving...");
cnt = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, rq[0], rq[1], rq[2], buffer, sizeof(buffer), 5000);
if(cnt < 0){
fprintf(stdout, "\nUSB error in iteration ?!?: %s\n", usb_strerror());
}
rxIndex = ((int)buffer[1] & 0xff) | (((int)buffer[0] & 0xff) << 8);
fprintf(stdout, "measured offset %i", rxIndex);
} else {
usage();
}
usb_close(handle);
return 0;
}