00001
00008 #include <sys/types.h>
00009 #include <stdlib.h>
00010 #include <stdio.h>
00011 #include <math.h>
00012 #include <unistd.h>
00013 #include <stdint.h>
00014 #include <string.h>
00015 #include <signal.h>
00016 #include <time.h>
00017
00018 #include <sys/termios.h>
00019 #include <fcntl.h>
00020
00021 #include "nsds_util.h"
00022 #include "flog.h"
00023 #include "adxl_util.h"
00024
00026 bool control_break;
00027
00028
00047 int adxl_open(const char *COMPORT_FILE)
00048 {
00049 char me[] = "adxl_open";
00050 struct termios term;
00051 int comm_fd = -1;
00052 const int baud_rate = B38400;
00053 char *portname = NULL;
00054 const char com1[] = "/dev/ttyS1";
00055
00056
00057
00058 if(COMPORT_FILE == NULL)
00059 {
00060
00061 portname = getenv("ADXL_PORT");
00062 if(portname == NULL)
00063 {
00064
00065 portname = (char *) com1;
00066 }
00067 }
00068 else
00069 portname = (char *) COMPORT_FILE;
00070
00071
00072 flog_usr(FLOG_NOTICE, 0, me, "Opening ADXL202 on port '%s'", portname);
00073
00074
00075 comm_fd = open(portname, O_RDWR);
00076
00077
00078 if(comm_fd <= 0)
00079 {
00080 flog_usr(FLOG_ERROR, FL_ERR_SYSTEM, me,
00081 "Could not open serial port '%s'\n", portname);
00082 return(comm_fd);
00083 }
00084
00085
00086 memset(&term, 0x00, sizeof(term));
00087
00088 tcgetattr(comm_fd, &term);
00089
00090
00091 term.c_cflag = baud_rate | CS8 | CLOCAL | CREAD;
00092 term.c_iflag = IGNPAR;
00093 term.c_oflag = 0;
00094 term.c_lflag = 0;
00095 term.c_cc[VMIN] = 1;
00096 term.c_cc[VTIME] = 0;
00097
00098 tcflush(comm_fd, TCIFLUSH);
00099 tcsetattr(comm_fd, TCSANOW, &term);
00100
00101
00102 sleep(2);
00103
00104 flog_usr(FLOG_DEBUG, 0, me, "Serial initialization completed OK");
00105
00106 return(comm_fd);
00107 }
00108
00109
00118 int adxl_read(const int COMM_FD, adxl_reading_t *result)
00119 {
00120 char me[] = "adxl_read";
00121 int itmp;
00122 char read_buf[2 * ADXL_READ_LEN] = "";
00123 const char start_char[] = "G";
00124
00125
00126 if((COMM_FD <= 0) || (result == NULL))
00127 {
00128 flog_usr(FLOG_ERROR, FL_ERR_NOCANDO, me,
00129 "Incorrect parameter!");
00130 return(-1);
00131 }
00132
00133
00134 memset(result, 0x00, sizeof(adxl_reading_t));
00135
00136
00137 itmp = write(COMM_FD, start_char, 1);
00138 if(itmp <= 0)
00139 {
00140 flog_usr(FLOG_ERROR, FL_ERR_SYSTEM, me,
00141 "Error %d on serial write", itmp);
00142 return(itmp);
00143 }
00144
00145
00146 itmp = read(COMM_FD, read_buf, ADXL_READ_LEN);
00147 if(itmp < ADXL_READ_LEN)
00148 {
00149 flog_usr(FLOG_ERROR, FL_ERR_SYSTEM, me,
00150 "Error %d on serial read", itmp);
00151 return(1);
00152 }
00153
00154
00155 result->raw[0] = ((read_buf[0] * 256) + read_buf[1]);
00156 result->raw[1] = ((read_buf[2] * 256) + read_buf[3]);
00157
00158
00159 result->G[0] = (result->raw[0] / 100.0);
00160 result->G[1] = (result->raw[1] / 100.0);
00161
00162 return(0);
00163 }
00164
00165
00182 int adxl_close(const int COMM_FD)
00183 {
00184 if(COMM_FD > 0)
00185 {
00186 return(close(COMM_FD));
00187 }
00188 else
00189 return(-1);
00190 }
00191
00192
00193
00202 void adxl_sighandler(int signal)
00203 {
00204 char me[] = "adxl_sighandler";
00205
00206 flog_usr(FLOG_NOTICE, 0, me, "Got signal %d, exiting", signal);
00207 control_break = true;
00208
00209 return;
00210 }
00211