Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line | 
|---|---|---|---|
| 360 | f9daq | 1 | /** | 
        
| 2 |  * $Id: calib.c 881 2013-12-16 05:37:34Z rp_jmenart $ | 
        ||
| 3 |  * | 
        ||
| 4 |  * @brief Red Pitaya Oscilloscope Calibration Module. | 
        ||
| 5 |  * | 
        ||
| 6 |  * @Author Jure Menart <juremenart@gmail.com> | 
        ||
| 7 |  *         | 
        ||
| 8 |  * (c) Red Pitaya  http://www.redpitaya.com | 
        ||
| 9 |  * | 
        ||
| 10 |  * This part of code is written in C programming language. | 
        ||
| 11 |  * Please visit http://en.wikipedia.org/wiki/C_(programming_language) | 
        ||
| 12 |  * for more details on the language used herein. | 
        ||
| 13 |  */ | 
        ||
| 14 | |||
| 15 | #include <stdio.h> | 
        ||
| 16 | #include <stdlib.h> | 
        ||
| 17 | #include <errno.h> | 
        ||
| 18 | #include <string.h> | 
        ||
| 19 | |||
| 20 | #include "calib.h" | 
        ||
| 21 | |||
| 22 | const char eeprom_device[]="/sys/bus/i2c/devices/0-0050/eeprom";  | 
        ||
| 23 | const int eeprom_calib_off=0x0008;  | 
        ||
| 24 | |||
| 25 | |||
| 26 | /*----------------------------------------------------------------------------*/ | 
        ||
| 27 | /** | 
        ||
| 28 |  * @brief Read calibration parameters from EEPROM device. | 
        ||
| 29 |  * | 
        ||
| 30 |  * Function reads calibration parameters from EEPROM device and stores them to the | 
        ||
| 31 |  * specified buffer. Communication to the EEPROM device is taken place through | 
        ||
| 32 |  * appropriate system driver accessed through the file system device | 
        ||
| 33 |  * /sys/bus/i2c/devices/0-0050/eeprom. | 
        ||
| 34 |  * | 
        ||
| 35 |  * @param[out]   calib_params  Pointer to destination buffer. | 
        ||
| 36 |  * @retval       0 Success | 
        ||
| 37 |  * @retval      -1 Failure, error message is put on stderr device | 
        ||
| 38 |  * | 
        ||
| 39 |  */ | 
        ||
| 40 | int rp_read_calib_params(rp_calib_params_t *calib_params)  | 
        ||
| 41 | { | 
        ||
| 42 | FILE *fp;  | 
        ||
| 43 | size_t size;  | 
        ||
| 44 | |||
| 45 |     /* sanity check */ | 
        ||
| 46 | if(calib_params == NULL) {  | 
        ||
| 47 | fprintf(stderr, "rp_read_calib_params(): input structure "  | 
        ||
| 48 | "not initialized\n");  | 
        ||
| 49 | return -1;  | 
        ||
| 50 |     } | 
        ||
| 51 | |||
| 52 |     /* open eeprom device */ | 
        ||
| 53 | fp=fopen(eeprom_device, "r");  | 
        ||
| 54 | if(fp == NULL) {  | 
        ||
| 55 | fprintf(stderr, "rp_read_calib_params(): Can not open EEPROM device: "  | 
        ||
| 56 | " %s\n", strerror(errno));  | 
        ||
| 57 | return -1;  | 
        ||
| 58 |     } | 
        ||
| 59 | |||
| 60 |     /* ...and seek to the appropriate storage offset */ | 
        ||
| 61 | if(fseek(fp, eeprom_calib_off, SEEK_SET) < 0) {  | 
        ||
| 62 | fclose(fp);  | 
        ||
| 63 | fprintf(stderr, "rp_read_calib_params(): fseek() failed: %s\n",  | 
        ||
| 64 | strerror(errno));  | 
        ||
| 65 | return -1;  | 
        ||
| 66 |     } | 
        ||
| 67 | |||
| 68 |     /* read data from eeprom component and store it to the specified buffer */ | 
        ||
| 69 | size=fread(calib_params, sizeof(char), sizeof(rp_calib_params_t), fp);  | 
        ||
| 70 | if(size != sizeof(rp_calib_params_t)) {  | 
        ||
| 71 | fclose(fp);  | 
        ||
| 72 | fprintf(stderr, "rp_read_calib_params(): fread() failed, "  | 
        ||
| 73 | "returned bytes: %d (should be :%d)\n", size,  | 
        ||
| 74 | sizeof(rp_calib_params_t));  | 
        ||
| 75 | return -1;  | 
        ||
| 76 |     } | 
        ||
| 77 | fclose(fp);  | 
        ||
| 78 | |||
| 79 | return 0;  | 
        ||
| 80 | } | 
        ||
| 81 | |||
| 82 | |||
| 83 | /*----------------------------------------------------------------------------*/ | 
        ||
| 84 | /** | 
        ||
| 85 |  * Initialize calibration parameters to default values. | 
        ||
| 86 |  * | 
        ||
| 87 |  * @param[out] calib_params  Pointer to target buffer to be initialized. | 
        ||
| 88 |  * @retval 0 Success, could never fail. | 
        ||
| 89 |  */ | 
        ||
| 90 | |||
| 91 | int rp_default_calib_params(rp_calib_params_t *calib_params)  | 
        ||
| 92 | { | 
        ||
| 93 | calib_params->fe_ch1_fs_g_hi = 28101971; /* 0.6543 [V] */  | 
        ||
| 94 | calib_params->fe_ch2_fs_g_hi = 28101971; /* 0.6543 [V] */  | 
        ||
| 95 | calib_params->fe_ch1_fs_g_lo = 625682246; /* 14.56 [V] */  | 
        ||
| 96 | calib_params->fe_ch2_fs_g_lo = 625682246; /* 14.56 [V] */  | 
        ||
| 97 | calib_params->fe_ch1_dc_offs = 585;  | 
        ||
| 98 | calib_params->fe_ch2_dc_offs = 585;  | 
        ||
| 99 | calib_params->be_ch1_fs = 42949673; /* 1 [V] */  | 
        ||
| 100 | calib_params->be_ch2_fs = 42949673; /* 1 [V] */  | 
        ||
| 101 | calib_params->be_ch1_dc_offs = 0x3eac;  | 
        ||
| 102 | calib_params->be_ch2_dc_offs = 0x3eac;  | 
        ||
| 103 | |||
| 104 | return 0;  | 
        ||
| 105 | } |