Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals  

adxl_util.c

Go to the documentation of this file.
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     // If no port passed, try environment or default
00058     if(COMPORT_FILE == NULL)
00059     {
00060         // If set, use environment variable
00061         portname = getenv("ADXL_PORT");
00062         if(portname == NULL)
00063         {
00064             // Fallback to hardwired serial port zero
00065             portname = (char *) com1;
00066         }
00067     }
00068     else // try users' port
00069         portname = (char *) COMPORT_FILE;
00070         
00071     // Open and program the serial port
00072     flog_usr(FLOG_NOTICE, 0, me, "Opening ADXL202 on port '%s'", portname);
00073 
00074     /* Open the comport, R/W access required */
00075     comm_fd = open(portname, O_RDWR);
00076         
00077     /* Exit if could not open the comport */
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     // Clear the struct
00086     memset(&term, 0x00, sizeof(term));
00087 
00088     tcgetattr(comm_fd, &term);
00089 
00090     // Set local device, 8 bits, read enabled, 38400 baud
00091     term.c_cflag = baud_rate | CS8 | CLOCAL | CREAD;
00092     term.c_iflag = IGNPAR;// No parity
00093     term.c_oflag = 0;     // Clear these flags
00094     term.c_lflag = 0;
00095     term.c_cc[VMIN] = 1;  // Block until 1 char arrives
00096     term.c_cc[VTIME] = 0; // Inter-character timeout not used
00097 
00098     tcflush(comm_fd, TCIFLUSH);
00099     tcsetattr(comm_fd, TCSANOW, &term);
00100 
00101     // Delay to allow the board to start up
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     // Zero the result buffer
00134     memset(result, 0x00, sizeof(adxl_reading_t));
00135 
00136     // Reads are triggered by sending the 'G' character, no timed mode
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     // Read 4 bytes of data back - 16 bits for each axis
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     // Decode 16-bit signed ints
00155     result->raw[0] = ((read_buf[0] * 256) + read_buf[1]);
00156     result->raw[1] = ((read_buf[2] * 256) + read_buf[3]);
00157 
00158     // Calibration is from the data sheet, convert to +- 2.0
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 

Generated on Fri May 2 15:13:15 2003 for ADXL202-NEESgrid driver by doxygen1.3