/* * ms5607.c * * Copyright (C) 2012 Computex Co.,Ltd. * * Sample to test MS5607 using CM-3G CHECK BOARD */ #include #include #include #include #include #include #include #define I2C_DEVICE "/dev/i2c-3" #define DEV_ADDRESS 0x76 #define CMD_RESET 0x1E // ADC reset command #define CMD_ADC_READ 0x00 // ADC read command #define CMD_ADC_CONV 0x40 // ADC conversion command #define CMD_ADC_D1 0x00 // ADC D1 conversion #define CMD_ADC_D2 0x10 // ADC D2 conversion #define CMD_ADC_256 0x00 // ADC OSR=256 #define CMD_ADC_512 0x02 // ADC OSR=512 #define CMD_ADC_1024 0x04 // ADC OSR=1024 #define CMD_ADC_2048 0x06 // ADC OSR=2048 #define CMD_ADC_4096 0x08 // ADC OSR=4096 #define CMD_PROM_RD 0xA0 // Prom read command int file; void usage(char *app_name) { printf("%s - Displays Temperature and Pressure values using MS5607 sensor\n", app_name); printf("Usage: %s -d [device path]\n", app_name); printf("\t\t-d - i2c device path (Default: "I2C_DEVICE")\n"); printf("Eg: %s\n", app_name); exit(0); } int i2c_send(unsigned char cmd) { unsigned char buf[1]; buf[0] = cmd; if (write(file, buf, 1) != 1) { printf("Failed to write to i2c bus. Check if MS5607 is connected\n"); return 1; } return 0; } int i2c_recv(unsigned char *buf, int count) { if (read(file, buf, 2) != 2) { printf("Failed to read from to i2c bus. Check if MS5607 is connected\n"); return 1; } return 0; } /******************************************************* Performs ADC conversion ********************************************************/ int read_adc(char cmd, unsigned int *val) { unsigned char buf[3]; /* send conversion command */ if(i2c_send(CMD_ADC_CONV | cmd)){ return 1; } /* wait for conversion */ switch (cmd & 0x0f) { case CMD_ADC_256: usleep(900000); break; case CMD_ADC_512: usleep(3000); break; case CMD_ADC_1024: usleep(4000); break; case CMD_ADC_2048: usleep(6000); break; case CMD_ADC_4096: usleep(10000); break; } /* send read comand and read */ if(i2c_send(CMD_ADC_READ)){ return 1; } if(i2c_recv(buf, 3)){ return 1; } *val = (buf[0]<<16) | (buf[1]<<8) | buf[2]; return 0; } /******************************************************* Reads calibration coefficients ********************************************************/ int get_coeff(unsigned int coeff_num, double *val) { unsigned char buf[2]; /* send PROM READ command and read */ if(i2c_send(CMD_PROM_RD + coeff_num * 2)) return 1; if(i2c_recv(buf, 2)){ return 1; } *val = (buf[0]<<8) | buf[1]; return 0; } /**************************************************************** * Main ****************************************************************/ int main(int argc, char* argv[]) { unsigned char buf[10]; int d; int i; char shortoptions[] = "d:h"; char input_device[16]; float hres, hres2, lres; double coeff[8]; unsigned int D1, D2; double dT, T, P, OFF, SENS; input_device[0] = 0; for (;;) { d = getopt_long(argc, argv, shortoptions, (void *)NULL, &index); if (-1 == d) { break; } switch (d) { case 'd': strcpy(input_device, optarg); break; default: usage(argv[0]); } } if(input_device[0] == 0) strcpy(input_device, I2C_DEVICE); file = open(input_device, O_RDWR); if(file < 0) { printf("Error: "I2C_DEVICE " open failed\n"); exit(1); } if(ioctl(file, I2C_SLAVE, DEV_ADDRESS) < 0) { printf("Error: %s setting device address to 0x%xfailed\n", input_device, DEV_ADDRESS); goto _out; } /* send reset sequence */ if (i2c_send(CMD_RESET)){ goto _out; } /* wait for the reset sequence timing */ usleep(3000); /* read coefficients */ for(i=1; i<7; i++) if(get_coeff(i, &coeff[i])) goto _out; printf("Temperature Pressure\n"); while(1) { if(read_adc(CMD_ADC_D1 | CMD_ADC_4096, &D1) || read_adc(CMD_ADC_D2 | CMD_ADC_4096, &D2)) goto _out; dT = D2 - coeff[5] * 256; OFF = coeff[2] * 131072 + ((coeff[4] * dT) / 64); SENS = coeff[1] * 65536 + (coeff[3] * dT) / 128; T = (2000 + (dT * coeff[6]) / 8388608) / 100; P = (((D1 * SENS) / 2097152 - OFF) / 32768) /100; printf("%0.3f℃ %0.3f mbar\n", T, P); sleep(1); } _out: close(file); return 0; }