Beispiel #1
0
void ADCreader::run()
{
  running = true;


  while (running) {
    
    // let's wait for data for max one second
    int ret = gpio_poll(sysfs_fd,1000);
    if (ret<1) {
      fprintf(stderr,"Poll error %d\n",ret);
    }
	  
    // tell the AD7705 to read the data register (16 bits)
    writeReg(fd,0x38);

    // read the data register by performing two 8 bit reads
    int value = readData(fd)-0x8000;

    *pIn = value;
    if (pIn == (&samples[MAX_SAMPLES-1])) 
      pIn = samples;
    else
      pIn++;
    
  }
  close(fd);
  gpio_fd_close(sysfs_fd);
}
Beispiel #2
0
static void post_init(struct v7 *v7) {
  sj_prompt_init(v7);
  do {
    /*
     * Now waiting until mongoose has active connections
     * and there are active gpio ISR and then exiting
     * TODO(alashkin): change this to something smart
     */
  } while ((mongoose_poll(100) || gpio_poll()) && !sj_please_quit);
  mongoose_destroy();
}
Beispiel #3
0
static int lua_gpio_poll(lua_State *L) {
    gpio_t *gpio;
    int timeout_ms;
    int ret;

    gpio = luaL_checkudata(L, 1, "periphery.GPIO");
    lua_gpio_checktype(L, 2, LUA_TNUMBER);

    timeout_ms = lua_tointeger(L, 2);

    if ((ret = gpio_poll(gpio, timeout_ms)) < 0)
        return lua_gpio_error(L, ret, gpio_errno(gpio), "Error: %s", gpio_errmsg(gpio));

    lua_pushboolean(L, ret > 0);

    return 1;
}
int main(int argc, char *argv[])
{
	int ret = 0;
	int fd;
	int sysfs_fd;

	int no_tty = !isatty( fileno(stdout) );

	fd = open(device, O_RDWR);
	if (fd < 0)
		pabort("can't open device");

	/*
	 * spi mode
	 */
	ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
	if (ret == -1)
		pabort("can't set spi mode");

	ret = ioctl(fd, SPI_IOC_RD_MODE, &mode);
	if (ret == -1)
		pabort("can't get spi mode");

	/*
	 * bits per word
	 */
	ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
	if (ret == -1)
		pabort("can't set bits per word");

	ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
	if (ret == -1)
		pabort("can't get bits per word");

	fprintf(stderr, "spi mode: %d\n", mode);
	fprintf(stderr, "bits per word: %d\n", bits);

	// enable master clock for the AD
	// divisor results in roughly 4.9MHz
	// this also inits the general purpose IO
	gz_clock_ena(GZ_CLK_5MHz,5);

	// enables sysfs entry for the GPIO pin
	gpio_export(drdy_GPIO);
	// set to input
	gpio_set_dir(drdy_GPIO,0);
	// set interrupt detection to falling edge
	gpio_set_edge(drdy_GPIO,"falling");
	// get a file descriptor for the GPIO pin
	sysfs_fd = gpio_fd_open(drdy_GPIO);

	// resets the AD7705 so that it expects a write to the communication register
        printf("sending reset\n");
	writeReset(fd);

	// tell the AD7705 that the next write will be to the clock register
	writeReg(fd,0x20);
	// write 00001100 : CLOCKDIV=1,CLK=1,expects 4.9152MHz input clock
	writeReg(fd,0x0C);
	

	//channel1
	writeReg(fd,0x11);
	// intiates a self calibration and then after that starts converting
	writeReg(fd,0x40);
	  ret = gpio_poll(sysfs_fd,1000);
	  if (ret<1) {
	    fprintf(stderr,"Poll error chennel1 set-up %d\n",ret);
	  }
	
	//channel0
	// tell the AD7705 that the next write will be the setup register
	writeReg(fd,0x10);
	// intiates a self calibration and then after that starts converting
	writeReg(fd,0x40);
		  ret = gpio_poll(sysfs_fd,1000);
	  if (ret<1) {
	    fprintf(stderr,"Poll error chennel0 set-up %d\n",ret);
	  }

	// we read data in an endless loop and display it
	// this needs to run in a thread ideally
	while (1) {

	 //channel0
	  // tell the AD7705 to read the data register (16 bits)
	  writeReg(fd,0x38);
	  	  ret = gpio_poll(sysfs_fd,1000);
	  if (ret<1) {
	    fprintf(stderr,"Poll error read data channel0 %d\n",ret);
	  }

	  // read the data register by performing two 8 bit reads
	  int value0 = readData(fd)-0x8000;
		

	   //channel1
	  // tell the AD7705 to read the data register (16 bits)
	  writeReg(fd,0x39);
	  	  ret = gpio_poll(sysfs_fd,1000);
	  if (ret<1) {
	    fprintf(stderr,"Poll error read data channel0 %d\n",ret);
	  }

	  // read the data register by performing two 8 bit reads
	  int value1 = readData(fd)-0x8000;
		fprintf(stderr,"data0 = %d data1 = %d      \r",value0,value1);

	

	}

	close(fd);
	gpio_fd_close(sysfs_fd);

	return ret;
}
void ADCreader::run()
{
        int ret = 0;
        int fd;
        int sysfs_fd;

        int no_tty = !isatty( fileno(stdout) );

        fd = open(device, O_RDWR);
        if (fd < 0)
                pabort("can't open device");

        /*
         * spi mode
         */
        ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
        if (ret == -1)
                pabort("can't set spi mode");

        ret = ioctl(fd, SPI_IOC_RD_MODE, &mode);
        if (ret == -1)
                pabort("can't get spi mode");

        /*
         * bits per word
         */
        ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
        if (ret == -1)
                pabort("can't set bits per word");

        ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
        if (ret == -1)
                pabort("can't get bits per word");

        fprintf(stderr, "spi mode: %d\n", mode);
        fprintf(stderr, "bits per word: %d\n", bits);

        // enable master clock for the AD
        // divisor results in roughly 4.9MHz
        // this also inits the general purpose IO
        gz_clock_ena(GZ_CLK_5MHz,5);

        // enables sysfs entry for the GPIO pin
        gpio_export(drdy_GPIO);
        // set to input
        gpio_set_dir(drdy_GPIO,0);
        // set interrupt detection to falling edge
        gpio_set_edge(drdy_GPIO,"falling");
        // get a file descriptor for the GPIO pin
        sysfs_fd = gpio_fd_open(drdy_GPIO);

        // resets the AD7705 so that it expects a write to the communication register
        printf("sending reset\n");
        writeReset(fd);

        // tell the AD7705 that the next write will be to the clock register
        writeReg(fd,0x20);
        // write 00001100 : CLOCKDIV=1,CLK=1,expects 4.9152MHz input clock
        writeReg(fd,0x0C);

        // tell the AD7705 that the next write will be the setup register
        writeReg(fd,0x10);

        // enable master clock for the AD
        // divisor results in roughly 4.9MHz
        // this also inits the general purpose IO
        gz_clock_ena(GZ_CLK_5MHz,5);

        // enables sysfs entry for the GPIO pin
        gpio_export(drdy_GPIO);
        // set to input
        gpio_set_dir(drdy_GPIO,0);
        // set interrupt detection to falling edge
        gpio_set_edge(drdy_GPIO,"falling");
        // get a file descriptor for the GPIO pin
        sysfs_fd = gpio_fd_open(drdy_GPIO);

        // resets the AD7705 so that it expects a write to the communication register
        printf("sending reset\n");
        writeReset(fd);

        // tell the AD7705 that the next write will be to the clock register
        writeReg(fd,0x20);
        // write 00001100 : CLOCKDIV=1,CLK=1,expects 4.9152MHz input clock
        writeReg(fd,0x0C);

        // tell the AD7705 that the next write will be the setup register
        writeReg(fd,0x10);
        // intiates a self calibration and then after that starts converting
        writeReg(fd,0x40);

        // we read data in an endless loop and display it
        // this needs to run in a thread ideally





         // let's wait for data for max one second
          ret = gpio_poll(sysfs_fd,1000);
          if (ret<1) {
            fprintf(stderr,"Poll error %d\n",ret);
          }

          // tell the AD7705 to read the data register (16 bits)
          writeReg(fd,0x38);
          // read the data register by performing two 8 bit reads
          
          //acquire and store the value of resistance (presumably at clean air)
          float init  = readData(fd);

          float vdif = ((init/32768)-1)*2.5; //translate the code into voltage to find Ain1(+)-Ain1(-)

          float Ain = vdif + 0.964; //add Ain1(-) to find the actual voltage 

          float Rair = (4300*5/Ain)-4300; // reverse engineer the voltage divider to find the resistance
          fprintf(stderr,"init = %f \t vdif= %f \t Ain=%f \t Rair = %f \n \n    ", init, vdif, Ain, Rair);



        running = 1;

        while (running) {

          // let's wait for data for max one second
          ret = gpio_poll(sysfs_fd,1000);
          if (ret<1) {
            fprintf(stderr,"Poll error %d\n",ret);
          }

          // tell the AD7705 to read the data register (16 bits)
          writeReg(fd,0x38);
          // read the data register by performing two 8 bit reads
          float value = readData(fd);

          float vdifcurrent=((value/32768)-1)*2.5;

          float Aincurrent  = vdifcurrent + 0.964;

          float Rcurrent = (4300*5/Aincurrent)-4300;
          float Rratio = Rcurrent/Rair; //divide resistance found by resistance in fresh air

	  buffer[bindex-1] = Rratio; //store value in ring buffer
	  
	  /* The following code has been used for debugging purgposes and is now commented out
	  
	  float test = buffer[bindex-1];
	  
	  fprintf(stderr,"data = %f \t vdiff=%f  \t Ain=%f  \t res ratio = %f  \r ", value, vdifcurrent, Aincurrent, test);
	  */
	  
	  bindex = bindex++; // update buffer index
	  
	  if(bindex == 20000000){
	  	bindex = 0;
	  }
	  
	  



        }

        close(fd);
        gpio_fd_close(sysfs_fd);


}