int main(int argc, char* argv[]) { char aChar; gz_clock_ena(GZ_CLK_5MHz, 0x00f); // Turn on a slow clock printf("\nPress any key to stop test."); scanf("%c", &aChar); gz_clock_dis(); return 0; }
ADCreader::ADCreader() { int ret = 0; // set up ringbuffer samples = new int[MAX_SAMPLES]; // pointer for incoming data pIn = samples; // pointer for outgoing data pOut = samples; // SPI constants static const char *device = "/dev/spidev0.0"; mode = SPI_CPHA | SPI_CPOL; bits = 8; speed = 50000; delay = 10; drdy_GPIO = 22; // open SPI device 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"); /* * max speed hz */ ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed); if (ret == -1) pabort("can't set max speed hz"); ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed); if (ret == -1) pabort("can't get max speed hz"); fprintf(stderr, "spi mode: %d\n", mode); fprintf(stderr, "bits per word: %d\n", bits); fprintf(stderr, "max speed: %d Hz (%d KHz)\n", speed, speed/1000); // 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 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); }
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; }
int main(int argc, char* argv[]) { initscr(); // initialize ncurses display nodelay(stdscr, 1); // don't wait for key presses noecho(); // don't echo key presses gz_spi_set_width(2); // Pass blocks of 2 bytes on SPI gz_clock_ena(GZ_CLK_5MHz, 0x02); // 2.5 MHz erase(); outputs_off(); printw("Modulating PWMs.\n"); printw("Press 'n' for next test, any other key to stop.\n"); int key = 0; while(1) { exercise_pwms(); key = getch(); if (key != -1) { break; } } if (key == 'n') { printw("Toggling all outputs.\n"); printw("Press 'n' for next test, any other key to stop.\n"); while(1) { exercise_outputs(0xff, 0x00); key = getch(); if (key != -1) { break; } } } if (key == 'n') { erase(); printw("Toggling alternate outputs.\n"); printw("Press 'n' for next test, any other key to stop.\n"); while(1) { exercise_outputs(0xaa, 0x55); key = getch(); if (key != -1) { break; } } } if (key == 'n') { erase(); printw("Walking outputs.\n"); printw("Press 'n' for next test, any other key to stop.\n"); unsigned char current = 0xfe; while(1) { exercise_outputs(current, (current << 1) | 0x01); current = (current << 2) | 0x03; if (current == 0xff) { current = 0xfe; } key = getch(); if (key != -1) { break; } } } if (key == 'n') { erase(); curs_set(0); // Hide the cursor printw("Reading inputs.\n"); printw("Press any key to stop.\n"); while(1) { display_inputs(); key = getch(); if (key != -1) { break; } } move(getcury(stdscr) + 1 ,0); curs_set(1); refresh(); } gz_spi_close(); // close SPI channel erase(); reset_shell_mode(); // turn off ncurses return 0; }
static void writeReset(int fd) { int ret; uint8_t tx1[5] = {0xff,0xff,0xff,0xff,0xff}; uint8_t rx1[5] = {0}; struct spi_ioc_transfer tr = { .tx_buf = (unsigned long)tx1, .rx_buf = (unsigned long)rx1, .len = ARRAY_SIZE(tx1), .delay_usecs = delay, .speed_hz = speed, .bits_per_word = bits, }; ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr); if (ret < 1) pabort("can't send spi message"); } static void writeReg(int fd, uint8_t v) { int ret; uint8_t tx1[1]; tx1[0] = v; uint8_t rx1[1] = {0}; struct spi_ioc_transfer tr = { .tx_buf = (unsigned long)tx1, .rx_buf = (unsigned long)rx1, .len = ARRAY_SIZE(tx1), .delay_usecs = delay, .speed_hz = speed, .bits_per_word = bits, }; ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr); if (ret < 1) pabort("can't send spi message"); } static uint8_t readReg(int fd) { int ret; uint8_t tx1[1]; tx1[0] = 0; uint8_t rx1[1] = {0}; struct spi_ioc_transfer tr = { .tx_buf = (unsigned long)tx1, .rx_buf = (unsigned long)rx1, .len = ARRAY_SIZE(tx1), .delay_usecs = delay, .speed_hz = speed, .bits_per_word = 8, }; ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr); if (ret < 1) pabort("can't send spi message"); return rx1[0]; } static int readData(int fd) { int ret; uint8_t tx1[2] = {0,0}; uint8_t rx1[2] = {0,0}; struct spi_ioc_transfer tr = { .tx_buf = (unsigned long)tx1, .rx_buf = (unsigned long)rx1, .len = ARRAY_SIZE(tx1), .delay_usecs = delay, .speed_hz = speed, .bits_per_word = 8, }; ret = ioctl(fd, SPI_IOC_MESSAGE(1), &tr); if (ret < 1) pabort("can't send spi message"); return (rx1[0]<<8)|(rx1[1]); } void setADC() { int ret = 0; 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"); /* * max speed hz */ ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed); if (ret == -1) pabort("can't set max speed hz"); ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &speed); if (ret == -1) pabort("can't get max speed hz"); fprintf(stderr, "spi mode: %d\n", mode); fprintf(stderr, "bits per word: %d\n", bits); fprintf(stderr, "max speed: %d Hz (%d KHz)\n", speed, speed/1000); // enable master clock for the AD // divisor results in roughly 4.9MHz gz_clock_ena(GZ_CLK_5MHz,5); // resets the AD7705 so that it expects a write to the communication register 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); }
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); }