int main() { configurePin(GPIO_MODE_OUTPUT,PIN_14,PORTG); configurePin(GPIO_MODE_OUTPUT,PIN_13,PORTG); //configurePin(GPIO_MODE_OUTPUT,PIN_5,PORTC); //configurePin(GPIO_MODE_OUTPUT,PIN_13,PORTB); configureIntPin(GPIO_MODE_INPUT,PIN_0,PORTA); int realSignal; int inputSignal; while(1) { realSignal = PORTA->IDR; inputSignal = filterSignal(PORTA->IDR); if( inputSignal != 1 ) { //writeOutHigh(PORTG,14); writeSet(PORTG,14); writeReset(PORTG,13); //writeOutLow(PORTG,13); } else { //writeOutHigh(PORTG,13); //writeOutLow(PORTG,14); writeSet(PORTG,13); writeReset(PORTG,14); } } /* delay(100000); writeOutHigh(PORTG,14); writeOutLow(PORTG,13); writeOutLow(PORTC,5); writeOutLow(PORTB,13); //writeOne(GPIO_PIN_14,GPIOG); //writeZero(GPIO_PIN_13,GPIOG); //writeZero(GPIO_PIN_5,GPIOC); //writeZero(GPIO_PIN_13,GPIOB); delay(100000); writeOutLow(PORTG,14); writeOutHigh(PORTG,13); writeOutHigh(PORTC,5); writeOutHigh(PORTB,13); //writeZero(GPIO_PIN_14,GPIOG); //writeOne(GPIO_PIN_13,GPIOG); //writeOne(GPIO_PIN_5,GPIOC); //writeOne(GPIO_PIN_13,GPIOB); */ }
bool L2Game_RequestUserCommand::create( unsigned int commandID ) { writeReset(); writeUChar( 0xB3 ); writeUInt( commandID ); return true; }
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; }
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); }