calibration_api.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* @brief This is a simple application for testing calibration api
  2. * (c) Red Pitaya http://www.redpitaya.com
  3. *
  4. * This part of code is written in C programming language.
  5. * Please visit http://en.wikipedia.org/wiki/C_(programming_language)
  6. * for more details on the language used herein.
  7. */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <unistd.h>
  12. #include "rp_hw-calib.h"
  13. int main(int argc, char *argv[]){
  14. // Read calibrations from EEPROM to RAM
  15. int res = rp_CalibInit();
  16. printf("Init result: %d\n",res);
  17. // Backup current calibrations
  18. rp_calib_params_t current_calib = rp_GetCalibrationSettings();
  19. // Retrieves calibration coefficients
  20. double gain;
  21. int32_t offset;
  22. res = rp_CalibGetFastADCCalibValue(RP_CH_1_CALIB,RP_DC_CALIB,&gain,&offset);
  23. printf("Calibration factors for channel 1 GAIN %f OFFSET %d result = %d\n",gain,offset,res);
  24. res = rp_CalibGetFastADCCalibValue(RP_CH_2_CALIB,RP_DC_CALIB,&gain,&offset);
  25. printf("Calibration factors for channel 2 GAIN %f OFFSET %d result = %d\n",gain,offset,res);
  26. printf("\nCurrent calibration in user space\n");
  27. rp_CalibPrint(&current_calib);
  28. // Returns the default calibration for the current board version. It may be different on different boards.
  29. rp_calib_params_t def_calib = rp_GetDefaultCalibrationSettings();
  30. printf("\nDefailt calibration\n");
  31. rp_CalibPrint(&def_calib);
  32. // Write default calibration in EEPROM user space.
  33. // The previously loaded in RAM calibration will not change.
  34. rp_CalibrationWriteParams(def_calib,false);
  35. // Sets calibration parameters into RAM.
  36. rp_CalibrationSetParams(def_calib);
  37. res = rp_CalibGetFastADCCalibValue(RP_CH_1_CALIB,RP_DC_CALIB,&gain,&offset);
  38. printf("Calibration factors for channel 1 GAIN %f OFFSET %d result = %d\n",gain,offset,res);
  39. res = rp_CalibGetFastADCCalibValue(RP_CH_2_CALIB,RP_DC_CALIB,&gain,&offset);
  40. printf("Calibration factors for channel 2 GAIN %f OFFSET %d result = %d\n",gain,offset,res);
  41. // Resets calibration values in EEPROM
  42. rp_CalibrationWriteParams(current_calib,false);
  43. // Read calibrations from EEPROM to RAM
  44. res = rp_CalibInit();
  45. printf("\nRestored calibration\n");
  46. res = rp_CalibGetFastADCCalibValue(RP_CH_1_CALIB,RP_DC_CALIB,&gain,&offset);
  47. printf("Calibration factors for channel 1 GAIN %f OFFSET %d result = %d\n",gain,offset,res);
  48. res = rp_CalibGetFastADCCalibValue(RP_CH_2_CALIB,RP_DC_CALIB,&gain,&offset);
  49. printf("Calibration factors for channel 2 GAIN %f OFFSET %d result = %d\n",gain,offset,res);
  50. // Read calibration direct from eeprom
  51. uint8_t *data = NULL;
  52. uint16_t size;
  53. rp_calib_params_t eeprom_calib;
  54. rp_CalibGetEEPROM(&data,&size,false);
  55. // Convert bytes to rp_calib_params_t
  56. rp_CalibConvertEEPROM(data,size,&eeprom_calib);
  57. printf("\nCalibration read directly\n");
  58. rp_CalibPrint(&eeprom_calib);
  59. return 0;
  60. }