DSP2803x_OscComp.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //###########################################################################
  2. //
  3. // FILE: DSP2803x_OscComp.c
  4. //
  5. // TITLE: DSP2803x oscillator compensation functions
  6. //
  7. // This file contains the functions which adjust the oscillator trim and compensate
  8. // for frequency drift based on the current temperature sensor reading.
  9. //
  10. // This program makes use of variables stored in OTP during factory
  11. // test. These OTP locations on pre-TMS devices may not be populated.
  12. // Ensure that the following memory locations in TI OTP are populated
  13. // (not 0xFFFF) before use:
  14. //
  15. // 0x3D7E90 to 0x3D7EA4
  16. //
  17. // Note that these functions pull data from the OTP by calling functions which
  18. // reside in OTP. Therefore the OTP wait-states (controlled by
  19. // FlashRegs.FOTPWAIT.bit.OTPWAIT) will significantly affect the time required
  20. // to execute these functions.
  21. //
  22. //###########################################################################
  23. // $TI Release: F2803x C/C++ Header Files and Peripheral Examples V130 $
  24. // $Release Date: May 8, 2015 $
  25. // $Copyright: Copyright (C) 2009-2015 Texas Instruments Incorporated -
  26. // http://www.ti.com/ ALL RIGHTS RESERVED $
  27. //###########################################################################
  28. #include "DSP2803x_Device.h" // DSP2803x Headerfile Include File
  29. #include "DSP2803x_Examples.h" // DSP2803x Examples Include File
  30. // Useful definitions
  31. #define FP_SCALE 32768 // Scale factor for Q15 fixed point numbers (2^15)
  32. #define FP_ROUND FP_SCALE/2 // Quantity added to Q15 numbers before converting
  33. // to integer to round the number
  34. //Amount to add to Q16.15 fixed point number to shift from a fine trim range of
  35. //(-31 to 31) to (1 to 63). This guarantees that the trim is positive and can
  36. //therefore be efficiently rounded
  37. #define OSC_POSTRIM 32
  38. #define OSC_POSTRIM_OFF FP_SCALE*OSC_POSTRIM
  39. //The following functions return reference values stored in OTP.
  40. //Slope used to compensate oscillator 1 (fine trim steps / ADC code). Stored
  41. //in fixed point Q15 format.
  42. #define getOsc1FineTrimSlope() (*(int16 (*)(void))0x3D7E90)()
  43. //Oscillator 1 fine trim at high temp
  44. #define getOsc1FineTrimOffset() (*(int16 (*)(void))0x3D7E93)()
  45. //Oscillator 1 coarse trim
  46. #define getOsc1CoarseTrim() (*(int16 (*)(void))0x3D7E96)()
  47. //Slope used to compensate oscillator 2 (fine trim steps / ADC code). Stored
  48. //in fixed point Q15 format.
  49. #define getOsc2FineTrimSlope() (*(int16 (*)(void))0x3D7E99)()
  50. //Oscillator 2 fine trim at high temp
  51. #define getOsc2FineTrimOffset() (*(int16 (*)(void))0x3D7E9C)()
  52. //Oscillator 2 coarse trim
  53. #define getOsc2CoarseTrim() (*(int16 (*)(void))0x3D7E9F)()
  54. //ADC reading of temperature sensor at reference temperature for compensation
  55. #define getRefTempOffset() (*(int16 (*)(void))0x3D7EA2)()
  56. //Define function for later use
  57. Uint16 GetOscTrimValue(int Coarse, int Fine);
  58. // This function uses the temperature sensor sample reading to perform internal oscillator 1 compensation with
  59. // reference values stored in OTP.
  60. void Osc1Comp (int16 sensorSample)
  61. {
  62. int16 compOscFineTrim;
  63. EALLOW;
  64. compOscFineTrim = ((sensorSample - getRefTempOffset())*(int32)getOsc1FineTrimSlope()
  65. + OSC_POSTRIM_OFF + FP_ROUND )/FP_SCALE + getOsc1FineTrimOffset() - OSC_POSTRIM;
  66. if(compOscFineTrim > 31){
  67. compOscFineTrim = 31;
  68. }
  69. else if(compOscFineTrim < -31){
  70. compOscFineTrim = -31;
  71. }
  72. SysCtrlRegs.INTOSC1TRIM.all = GetOscTrimValue(getOsc1CoarseTrim(), compOscFineTrim);
  73. EDIS;
  74. }
  75. // This function uses the temperature sensor sample reading to perform internal oscillator 2 compensation with
  76. // reference values stored in OTP.
  77. void Osc2Comp (int16 sensorSample)
  78. {
  79. int16 compOscFineTrim;
  80. EALLOW;
  81. compOscFineTrim = ((sensorSample - getRefTempOffset())*(int32)getOsc2FineTrimSlope()
  82. + OSC_POSTRIM_OFF + FP_ROUND )/FP_SCALE + getOsc2FineTrimOffset() - OSC_POSTRIM;
  83. if(compOscFineTrim > 31){
  84. compOscFineTrim = 31;
  85. }
  86. else if(compOscFineTrim < -31){
  87. compOscFineTrim = -31;
  88. }
  89. SysCtrlRegs.INTOSC2TRIM.all = GetOscTrimValue(getOsc2CoarseTrim(), compOscFineTrim);
  90. EDIS;
  91. }
  92. //This function packs the coarse and fine trim into
  93. //the format of the oscillator trim register
  94. Uint16 GetOscTrimValue(int Coarse, int Fine)
  95. {
  96. Uint16 regValue = 0;
  97. if(Fine < 0)
  98. {
  99. regValue = ((-Fine) | 0x20) << 9;
  100. }
  101. else
  102. {
  103. regValue = Fine << 9;
  104. }
  105. if(Coarse < 0)
  106. {
  107. regValue |= ((-Coarse) | 0x80);
  108. }
  109. else
  110. {
  111. regValue |= Coarse;
  112. }
  113. return regValue;
  114. }