#include #include #include #include #include #include #include #include //#include "linux_arb_usb.h" #define HANDLE int char* checkerror(int error_num){ switch(error_num){ case EAGAIN: return "EAGAIN"; case EBADF: return "EBADF"; case EFAULT: return "EFAULT"; case EINTR: return "EINTR"; case EINVAL: return "EINVAL"; case EIO: return "EIO"; case EISDIR: return "EISDIR"; default: return "Unknown error"; } } long int Read_ARB_USB(HANDLE hCom,unsigned long arb_addr,int *data){ unsigned char message[6]; int num_bytes_read; int num_bytes_written; int total_bytes_read=0; int timeout; int A; message[0] = 3<<6 | ((arb_addr >> 14) & 0x3f); message[1] = 1<<6 | ((arb_addr >> 8) & 0x3f); message[2] = 2<<6 | ((arb_addr >> 2) & 0x3f); message[3] = 2<<6 | ((arb_addr << 4) & 0x30); message[4] = 0; message[5] = 0; num_bytes_written = (int)write(hCom,message,4); timeout = 10000; while((0 >= (num_bytes_read = (int)read(hCom,message + 4,1))) && (timeout-- > 0)); if(num_bytes_read == -1) printf("D0 Errno: %s\n", checkerror(errno)); if(timeout == 0) printf("D0 timeout\n"); if(num_bytes_read == 1) total_bytes_read++; timeout = 10000; while((0 >= (num_bytes_read = (int)read(hCom,message + 5,1))) && (timeout-- > 0)); if(num_bytes_read == -1) printf("D1 Errno: %s\n", checkerror(errno)); if(timeout == 0) printf("D1 timeout\n"); if(num_bytes_read == 1) total_bytes_read++; *data = ((message[4] & 0xff) << 8) | (message[5] & 0xff); if(total_bytes_read == 2){ return ((message[4] & 0xff) << 8) | (message[5] & 0xff); } else { write(hCom,"\0\0\0",2); return -(total_bytes_read+1); } } void Write_ARB_USB(HANDLE hCom,unsigned long int arb_addr, unsigned int arb_data){ unsigned char message[6]; int A; int num_bytes_written; message[0] = 3<<6 | ((arb_addr >> 14) & 0x3f); message[1] = 2<<6 | ((arb_addr >> 8) & 0x3f); message[2] = 2<<6 | ((arb_addr >> 2) & 0x3f); message[3] = 2<<6 | ((arb_addr << 4) & 0x30) | ((arb_data >> 12) & 0xf); message[4] = 2<<6 | ((arb_data >> 6) & 0x3f); message[5] = 2<<6 | ((arb_data << 0) & 0x3f); num_bytes_written = (int)write(hCom,message,6); if(num_bytes_written == -1) printf("Errno: %d\n",errno); //printf("Wrote %d\n",/*A*/num_bytes_written); return; } int open_usb_port(const char* name){ int port; struct termios termios_p; port = open(name, O_RDWR | O_NOCTTY | O_NDELAY); if(port == -1) return -1; tcgetattr(port, &termios_p); termios_p.c_cflag = B115200; termios_p.c_cflag |= CS8; termios_p.c_cflag |= CREAD; termios_p.c_cflag |= CLOCAL; termios_p.c_oflag = 0; termios_p.c_lflag = 0; termios_p.c_cc[VTIME] = 0; termios_p.c_cc[VMIN] = 1; tcsetattr(port, TCSANOW, &termios_p); tcflush(port, TCOFLUSH); tcflush(port, TCIFLUSH); return port; } void close_usb_port(HANDLE port){ close(port); }