Main Page   Modules   File List   Globals  

gp3libx.c

Go to the documentation of this file.
00001 /* General C language GP3 Library by Al Williams, AWC
00002    This library supports the GP3 I/O board (http://www.awce.com/gp3.htm)
00003    
00004    As shipped this code is meant to compile under Linux or Cygwin.
00005    However, if you port the following functions at the head of this
00006    file, the code should work for nearly any standard C compiler:
00007 
00008    gp3openport - Open a com port
00009    gp3closeport - Close the com port
00010    gp3write - Write a byte to the serial port
00011    gp3read - Read a byte from the serial port
00012    gp3ready - (Optional) read handshake from GP3
00013 
00014 For more info on the other routines see the GP3 documentation
00015 http://www.awce.com/gp3.pdf
00016 
00017 */
00018 
00019 #include <stdio.h>
00020 #include <fcntl.h>
00021 
00022 // You may need one of these or the other
00023 // depending on your system
00024 //#include <termios.h>
00025 #include <asm/termios.h>
00026 
00027 
00028 #include "gp3lib.h"
00029 
00030 /* Must operate at 57600 baud */
00031 #define BAUDRATE B57600
00032 #define DEMO 1
00033 #define DEBUGOUT 0
00034 
00035 #if DEBUGOUT
00036 #define DEBUG(tag,x) printf("DEBUG: %s=%02X\n",tag,x)
00037 #else
00038 #define DEBUG(tag,x)
00039 #endif
00040 
00041 /* This is the part that is platform specific */
00042 /* This is for Linux or Cygwin */
00043 
00044 static int fd;  // file descriptor
00045 static struct termios oldtio; /* terminal info */
00046 
00047 
00048 /* Open port at 57600 baud */
00049 int gp3openport(char *port)
00050 {
00051   struct termios tio;
00052   int i;
00053   fd=open(port,O_RDWR|O_NOCTTY);
00054   if (fd<0)
00055     {
00056       return -1;
00057     }
00058   tcgetattr(fd,&oldtio); /* save old settings */
00059   memset(&tio,0,sizeof(tio));
00060   // Set up for 8N1, no processing */
00061   tio.c_cflag=BAUDRATE|CS8|CLOCAL|CREAD|CRTSCTS; 
00062   tio.c_iflag=IGNPAR;
00063   tio.c_oflag=0;
00064   tio.c_lflag=0;
00065   for (i=0;i<NCCS;i++) tio.c_cc[i]=0;
00066   tio.c_cc[VMIN]=1;
00067   tcflush(fd,TCIFLUSH);
00068   tcsetattr(fd,TCSANOW,&tio); // make settings take  
00069   return 0;
00070 }
00071 
00072 /* Close the port when done */
00073 void gp3closeport()
00074 {
00075   tcsetattr(fd,TCSANOW,&oldtio);
00076   close(fd);
00077 }
00078 
00079 /* Write a byte to the GP3 */
00080 int gp3write(int byte)
00081 {
00082   char c=(char)byte;
00083   DEBUG("write",byte);
00084   return write(fd,&c,1);
00085 }
00086 
00087 /* Read a byte from the GP3 */
00088 /* User may call this after repeat */
00089 unsigned int gp3read(void)
00090 {
00091   char c;
00092   unsigned int rv;
00093   read(fd,&c,1);
00094   rv=((unsigned int)c) & 0xFF;;  
00095   DEBUG("read",rv);
00096   return rv;
00097 }
00098 
00099 /* Check CTS (optional if you don't want to know if GP3 is busy.
00100    As written, the system will handle this automatically although
00101    your program will block until the GP3 is available. This will
00102    allow you to prevent that if you implement it.
00103    None of the portable code uses this!
00104 
00105    However, if you don't implement this, you'll need to remove
00106    the lines that call gp3ready from the example program.
00107 */
00108 int gp3ready(void)
00109 {
00110   int s;
00111   usleep(100); // wait 100uS in case GP3 is just starting a command
00112   ioctl(fd,TIOCMGET,&s);
00113   return (s&TIOCM_CTS)==TIOCM_CTS;
00114 }
00115 
00116 
00117 /* This is the part that should not change for each port */
00118 
00119 /* Private function sets argument */
00120 static void setarg(int arg)
00121 {
00122   if (arg<=255) 
00123     {
00124       gp3write(6+(arg&1));
00125       gp3write(0x80+(arg&1));
00126     }
00127   else 
00128     {
00129       int code=8 + (arg&1);
00130       gp3write(code+((arg>>8)&1));
00131       gp3write(0x80+(arg>>9));
00132       gp3write(0x80+((arg&0xFF)>>1));
00133     }
00134 }
00135 
00136 
00137 
00138 /* Function reads 16 bit result */
00139 /* User may call this after repeat */
00140 unsigned int gp3readword(void)
00141 {
00142   unsigned int rv;
00143   rv=gp3read()<<8;
00144   return rv+gp3read();
00145 }
00146 
00147 /* The remaining functions match the description in the GP3 manual */
00148 
00149 void gp3setLED(int onoff)
00150 {
00151  gp3write(0xC+(onoff&1));
00152 }
00153 
00154 unsigned int gp3a2d(int chan)
00155 {
00156   gp3write(0x30+((chan&7)<<1));
00157   return gp3readword();
00158 }
00159 
00160 void gp3high(int pin)
00161 {
00162   gp3write(0x11+(pin<<1));
00163 }
00164 
00165 void gp3low(int pin)
00166 {
00167   gp3write(0x10+(pin<<1));
00168 }
00169 
00170 void gp3toggle(int pin)
00171 {
00172   gp3write(0x20+(pin<<1));
00173 }
00174 
00175 int gp3input(int pin)
00176 {
00177   gp3write(0x21+(pin<<1));
00178   return gp3read();
00179 }
00180 
00181 void gp3settris(int tris)
00182 {
00183   gp3write(2 + (tris & 1));
00184   gp3write(0x80+(tris>>1));
00185 }
00186 
00187 unsigned int gp3gettris(void)
00188 {
00189   gp3write(0x56);
00190   return gp3read();
00191 }
00192 
00193 void gp3setpins(int byte)
00194 {
00195   gp3write(4+(byte&1));
00196   gp3write(0x80+(byte>>1));
00197 }
00198 
00199 unsigned int gp3getpins(void)
00200 {
00201   gp3write(0x57);
00202   return gp3read();
00203 }
00204 
00205 void gp3writeee(int address,int byte)
00206 {
00207   setarg(address);
00208   gp3write(0x50+(byte&1));
00209   gp3write(0x80+(byte>>1));
00210 }
00211 
00212 unsigned int gp3readee(int address)
00213 {
00214   setarg(address);
00215   gp3write(0x52);
00216   return gp3read();
00217 }
00218 
00219 void gp3pwm(int chan,int dc,int dfreq)
00220 {
00221   setarg(dfreq);
00222   gp3write((chan==-1)?0x54:(0x60+(chan<<1))+(dc&1));
00223   gp3write(0x80+(dc>>1));
00224 }
00225 
00226 unsigned int gp3count(int pin,unsigned int ms)
00227 {
00228   setarg(ms);
00229   gp3write(0x40+(pin<<1));
00230   return gp3readword();
00231 }
00232 
00233 unsigned int gp3rctime(int pin, int state)
00234 {
00235   gp3write(0xE);
00236   gp3write(0xA0+(state&1)+(pin<<1));
00237   return gp3readword();
00238 }
00239 
00240 unsigned int gp3pulsein(int pin, int state)
00241 {
00242   gp3write(0x70+(state&1)+(pin<<1));
00243   return gp3readword();
00244 }
00245 
00246 void gp3pulseout(int pin, int duration)
00247 {
00248   setarg(duration);
00249   gp3write(0x41+(pin<<1));
00250 }
00251 
00252 void gp3freq(int pin, int ms, int freq)
00253 {
00254   setarg(ms);
00255   gp3write(0xE);
00256   gp3write(0x80+(freq&1)+(pin<<1));
00257   gp3write(0x80+(freq>>8));
00258   gp3write(0x80+((freq&0xFF)>>1));
00259 }
00260 
00261 void gp3setcounter(int prescale,int ext)
00262 {
00263   int ps=0;
00264   switch (prescale)
00265     {
00266     case 1: ps=0; break;
00267     case 2: ps=2; break;
00268     case 4: ps=4; break;
00269     case 8: ps=6; break;
00270     }
00271   gp3write(0xE);
00272   gp3write(0x90+ps+(ext&1));
00273 }
00274 
00275 unsigned int gp3counter(void)
00276 {
00277   gp3write(0x5a);
00278   return gp3readword();
00279 }
00280 
00281 void gp3resetall(void)
00282 {
00283   gp3write(0x5c);
00284 }
00285 
00286 void gp3reset(void)
00287 {
00288   gp3write(0);
00289 }
00290 
00291 void gp3repeat(int count)
00292 {
00293   setarg(count-1);
00294   gp3write(0x5b);
00295 }
00296 
00297 /* increment ARG */
00298 void gp3argincr(void)
00299 {
00300   gp3write(0x59);
00301 }
00302 
00303 /* decrement ARG */
00304 void gp3argdecr(void)
00305 {
00306   gp3write(0x58);
00307 }
00308 
00309 unsigned int gp3check(void)
00310 {
00311   gp3write(1);
00312   return gp3read();  /* should be 0x33 */
00313 }
00314 
00315 
00316 
00317 
00318 /* A demo program */
00319 
00320 #if DEMO
00321 
00322 void pause()
00323 {
00324   printf("Press ENTER\n");
00325   getchar();
00326 }
00327 
00328 int main(int argc, char *argv[])
00329 {
00330   if (argc<2)
00331     {
00332       fprintf(stderr,"Usage: gp3lib port\n");
00333       return 1;
00334     }
00335   if (gp3openport(argv[1])<0)
00336     {
00337       perror(argv[1]);
00338       return 1;
00339     }
00340   printf("Connect an LED to the hardware PWM out line and output #7\n");
00341   printf("Resetting GP3...\n");
00342   gp3resetall();
00343   printf("GP3 version code=%02X\n",gp3check());
00344   printf("Turning onboard LED and out 7 on...\n");
00345   gp3setLED(1);
00346   gp3high(7);
00347   pause();
00348   printf("Turning LEDs off...\n");
00349   gp3setLED(0);
00350   gp3low(7);
00351   printf("Reading A/D: ");
00352   printf("%d\n",gp3a2d(0));
00353   printf("Setting hardware PWM to 16...\n");
00354   gp3pwm(-1,16,2000);
00355   printf("Now 50%% PWM on pin 7 for 5 seconds...\n");
00356   gp3pwm(7,128,5000);
00357   printf("gp3ready returns %d\n",gp3ready());  // should print 0
00358   gp3check(); // wait for done
00359   printf("gp3ready returns %d\n",gp3ready());  // should print 1
00360   printf("Closing port...\n");
00361   gp3closeport();
00362   return 0;
00363 
00364 }
00365 
00366 #endif

Generated on Tue Feb 17 21:37:40 2004 for GP3LIB by doxygen1.2.18