#include #include #include #include #include #include #include #include "peekpoke.h" #include "ep93xx_adc.h" #define DATA_PAGE 0x12C00000 #define CALIB_LOC 2027 //location of calibration values #define NUM_SAMPLES 5 #define NUM_CHANNELS 4 /* globals */ static unsigned long adc_page, syscon_page; char *dr_page; double el,az; /*Calculate the adc value corresponding to 0V*/ //val1 is the ADC val coresponding to 0.833V //val2 is the ADC val corresponging to 2.5V int calcZeroVal(int val1, int val2) { val2 += 0x10000; return (int)(val1-(((val2-val1)/(2.5-0.833))*0.833)); } //return value of 1 indicates the board has no calibration values //return value of 0 indicates the board has calibration values int read_calibration(int buf[NUM_CHANNELS][2]) { int i,j,k = 0; unsigned short cal[NUM_CHANNELS*2]; // read 16 calibration bytes into buffer FILE *f = fopen("/etc/ADC-calibration.dat","r"); if (!f) goto empty_calibration; printf("Non-virgin board detected, evaluating stored " "calibration values\n"); printf("Stored Calibration values ["); if (fread(cal,NUM_CHANNELS*4,1,f) == 1) { for(j=0;j<2;j++) for(i=0;i 10) { printf("Calculated calibration " "values out of range...\n"); exit(-1); } } } write_calibration(cal); } else //calibration values read { for(j=0;j<2;j++) { for(i=0;i 0.25) { if(!failure) { printf("Calibration values out" "of range\n"); failure = 1; erase_cal = 1; } printf("\tChannel %d: %3.3f%%\n",i , pcnt_diff); } } } } if(erase_cal) erase_calibration(); if(failure) return 0; return 1; } void setDR(char *x,int n,int val) { if (n < 0 || n > 8) return; x[0] = (x[0] & ~(1 << n)) | (val ? (1< 8) return; x[2] = (x[2] & ~(1 << n)) | (val ? (1< 2.65)) { if(!failure) { failure = 1; //printf("EP93XX ADC out of range\n"); } //printf("\tChannel %d: %3.3fV" //"(expected 2.5V +- 150mV)\n", i, voltage); //odd channels 0.833(+-50mV) } else if(i % 2 == 1 && (voltage < 0.333 || voltage > 1.333)) { if(!failure) { failure = 1; //printf( "EP93xx ADC out of range\n"); } //printf("\tChannel %d: %3.3fV" // "(expected 0.833V +- 50mV)\n", i, voltage); } //use the datasheet values voltage = get_volts(adc_result_2[i][j], 0x9E58, 0xC350); //odd channels 2.5V(+-150mV) if(i % 2 == 1 && (voltage < 2.35 || voltage > 2.65)) { if(!failure) { failure = 1; //printf("EP93XX ADC out of range\n"); } //printf("\tChannel %d: %3.3fV" //"(expected 2.5V +- 150mV)\n", i, voltage); //even channels 0.833(+-50mV) } else if(i % 2 == 0 && (voltage < 0.333 || voltage > 1.333)) { if(!failure) { failure = 1; //printf( "EP93xx ADC out of range\n"); } ///printf("\tChannel %d: %3.3fV" "(expected 0.833V +- 50mV)\n", i, voltage); } } } calc_calibration(calibration, adc_result_1, adc_result_2); if(failure) return_val = 0; else return_val = 1; return return_val; } int main(void) { int calibration[NUM_CHANNELS][2]; int stored_calibration[NUM_CHANNELS][2]; int ret_val, state; int devmem = open("/dev/mem", O_RDWR|O_SYNC); assert(devmem != -1); dr_page = mmap(0, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED, devmem, DATA_PAGE); assert(&dr_page != MAP_FAILED); adc_page = (unsigned long)mmap(0, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED, devmem, ADC_PAGE); assert(&adc_page != MAP_FAILED); syscon_page = (unsigned long)mmap(0, getpagesize(), PROT_READ|PROT_WRITE , MAP_SHARED, devmem, SYSCON_PAGE); assert(&syscon_page != MAP_FAILED); init_ADC(adc_page, syscon_page); setDR(dr_page, 0, 1); setDR(dr_page, 1, 0); setDR(dr_page, 2, 1); setDR(dr_page, 3, 0); if(test_ADC(calibration)) { printf("ADC tested ok(data sheet values)\n"); state = read_calibration(stored_calibration); if(check_calibration(calibration, stored_calibration, state)) ret_val = 0; else ret_val = 1; } else ret_val = 1; printf("\t El.: %3.1f Az.: %3.1f \n", el, az); close(devmem); return ret_val; }