MySmartUSB-MK3-Interface

git clone git://xatko.vsos.ethz.ch/MySmartUSB-MK3-Interface.git
Log | Files | Refs

dev.c (3611B)


      1 /*
      2  * MySmartUSB MK3 Interface
      3  * 
      4  * @copyright: 	Copyright (c) 2012, Dominik Schmidt
      5  * @author:		Dominik Schmidt <das1993@hotmail.com>
      6  * @version:	0.0.0 
      7  * @license: 	CC-BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0/)
      8  * 
      9 */
     10 #include <dev.h>
     11 #include <termios.h>
     12 #include <log.h>
     13 #include <cmd.h>
     14 #include <fcntl.h>
     15 #include <sys/ioctl.h>
     16 #include <stdlib.h>
     17 #include <com.h>
     18 #include <stdio.h>
     19 #include <unistd.h>
     20 #include <string.h>
     21 int closeDevice(int dev){
     22 	fsync(dev);
     23 	return close(dev);
     24 }
     25 int autoConfigBaud(int fd){
     26 	speed_t speed[3]={B500000, B115200, B19200};
     27 	unsigned int i=0;
     28 	char str[50];
     29 	do_log("autoConfigBaud","Testing preset Baud",LOG_LEVEL_DEBUG,LOG_TYPE_NORMAL);
     30 	char c;
     31 	for(i=0;i<=3; i++){
     32 		sendMagicPacket(fd,'i');
     33 		c=readMagicPacket(fd);
     34 		if(c!=0){
     35 			break;
     36 		}
     37 		
     38 		if(i==3){
     39 			do_log("autoConfigBaud","Couldn't evaluate correct baudrate", LOG_LEVEL_FATAL,LOG_TYPE_NORMAL);
     40 			return -1;
     41 		}
     42 		if(setBaudRate(fd,speed[i],BAUD_INPUT|BAUD_OUTPUT)<0){
     43 			do_log("autoConfigBaud","Couldn't set baudrate", LOG_LEVEL_FATAL,LOG_TYPE_NORMAL);
     44 			return -2;
     45 		}
     46 		sprintf(str,"Testing baud %u",speed[i]);
     47 		do_log("autoConfigBaud",str,LOG_LEVEL_DEBUG,LOG_TYPE_NORMAL);
     48 	}
     49 	do_log("autoConfigBaud","BaudRate autoconfiguration successfull",LOG_LEVEL_DEBUG,LOG_TYPE_NORMAL);
     50 	fsync(fd);
     51 	return 0;
     52 }
     53 int setBaudRate(int fd, speed_t baud, unsigned int baud_select){
     54 	return setBaudRateExt(fd, baud, baud_select, TCSANOW);
     55 }
     56 int setBaudRateExt(int fd, speed_t baud, unsigned int baud_select, int extra){
     57 	struct termios options;
     58 	char str[50];
     59 	if(tcgetattr(fd, &options)<0){
     60 		do_log("setBaudRate","Couldn't load Options",LOG_LEVEL_ERROR,LOG_TYPE_SIGNAL);
     61 		return -1;
     62 	}
     63 	if((baud_select&BAUD_INPUT)>0){
     64 		if(cfsetispeed(&options, baud)<0){
     65 			do_log("setBaudRate","Couldn't set InputSpeed",LOG_LEVEL_ERROR,LOG_TYPE_SIGNAL);
     66 			closeDevice(fd);
     67 			return -2;
     68 		}
     69 		else{
     70 			sprintf(str, "InputBaud set to %u", baud);
     71 			do_log("setBaudRate", str, LOG_LEVEL_DEBUG, LOG_TYPE_NORMAL);
     72 		}
     73 	}
     74 	if((baud_select&BAUD_OUTPUT)>0){
     75 		if(cfsetospeed(&options, baud)<0){
     76 			do_log("setBaudRate","Couldn't set OutputSpeed",LOG_LEVEL_ERROR,LOG_TYPE_SIGNAL);
     77 			closeDevice(fd);
     78 			return -3;
     79 		}
     80 		else{
     81 			sprintf(str, "OutputBaud set to %u", baud);
     82 			do_log("setBaudRate", str, LOG_LEVEL_DEBUG, LOG_TYPE_NORMAL);
     83 		}
     84 	}
     85 	if(tcsetattr(fd, extra, &options)<0){
     86 		do_log("setBaudRate","Couldn't set Options",LOG_LEVEL_ERROR,LOG_TYPE_SIGNAL);
     87 		closeDevice(fd);
     88 		return -4;
     89 	}
     90 	return 0;
     91 	
     92 }
     93 speed_t getBaudRate(int fd){
     94 	struct termios options;
     95 	if(tcgetattr(fd, &options)<0){
     96 		do_log("getBaudRate","Couldn't load Options",LOG_LEVEL_ERROR,LOG_TYPE_SIGNAL);
     97 		return -1;
     98 	}
     99 	return cfgetispeed(&options);
    100 	
    101 }
    102 int openDevice(char *dev){
    103 	int fd = open(dev, O_RDWR | O_NOCTTY );
    104 	if(fd<0){
    105 		do_log("openDevice", "Couldn't open Device", LOG_LEVEL_FATAL,LOG_TYPE_SIGNAL);
    106 		return -1;
    107 	}
    108 	//ioctl(fd, TIOCEXCL);
    109 	struct termios options;
    110 	tcgetattr(fd, &options);
    111 	
    112     options.c_cflag|=(CLOCAL | CREAD);
    113     options.c_cflag&=~CSIZE;
    114     options.c_cflag|=CS8;
    115     options.c_cflag&=~PARENB;
    116     options.c_cflag&=~CSTOPB;
    117     options.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN | ISIG);
    118     options.c_iflag &= ~(IXON | IXOFF | IXANY );
    119     options.c_oflag &= ~(OCRNL | ONLCR | ONLRET |ONOCR | OFILL | OLCUC | OPOST);
    120 
    121     options.c_cc[VTIME]=5;
    122 	options.c_cc[VMIN]=0;
    123     
    124     if(tcsetattr(fd, TCSANOW, &options)<0){
    125 		do_log("openDevice","Couldn't set Options",LOG_LEVEL_FATAL,LOG_TYPE_SIGNAL);
    126 		closeDevice(fd);
    127 		return -4;
    128 	}
    129     if(autoConfigBaud(fd)<0){
    130 		return -5;
    131 	}
    132 	return fd;
    133 }