F2837xD_Adc.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. //###########################################################################
  2. //
  3. // FILE: F2837xD_Adc.c
  4. //
  5. // TITLE: F2837xD Adc Support Functions.
  6. //
  7. //###########################################################################
  8. // $TI Release: F2837xD Support Library v3.05.00.00 $
  9. // $Release Date: Tue Jun 26 03:15:23 CDT 2018 $
  10. // $Copyright:
  11. // Copyright (C) 2013-2018 Texas Instruments Incorporated - http://www.ti.com/
  12. //
  13. // Redistribution and use in source and binary forms, with or without
  14. // modification, are permitted provided that the following conditions
  15. // are met:
  16. //
  17. // Redistributions of source code must retain the above copyright
  18. // notice, this list of conditions and the following disclaimer.
  19. //
  20. // Redistributions in binary form must reproduce the above copyright
  21. // notice, this list of conditions and the following disclaimer in the
  22. // documentation and/or other materials provided with the
  23. // distribution.
  24. //
  25. // Neither the name of Texas Instruments Incorporated nor the names of
  26. // its contributors may be used to endorse or promote products derived
  27. // from this software without specific prior written permission.
  28. //
  29. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  30. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  31. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  32. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  33. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  34. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  35. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  36. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  37. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  38. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  39. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40. // $
  41. //###########################################################################
  42. //
  43. // Included Files
  44. //
  45. #include "F2837xD_device.h"
  46. #include "F2837xD_Examples.h"
  47. //
  48. // AdcSetMode - Set the resolution and signalmode for a given ADC. This will
  49. // ensure that the correct trim is loaded.
  50. //
  51. // NOTE!!! There is no EALLOW/EDIS in this function! You need to make sure you
  52. // perform the EALLOW before calling this function or else the ADC registers
  53. // will not be configured.
  54. //
  55. void AdcSetMode(Uint16 adc, Uint16 resolution, Uint16 signalmode)
  56. {
  57. Uint16 adcOffsetTrimOTPIndex; //index into OTP table of ADC offset trims
  58. Uint16 adcOffsetTrim; //temporary ADC offset trim
  59. //
  60. //re-populate INL trim
  61. //
  62. CalAdcINL(adc);
  63. if(0xFFFF != *((Uint16*)GetAdcOffsetTrimOTP))
  64. {
  65. //
  66. //offset trim function is programmed into OTP, so call it
  67. //
  68. //
  69. //calculate the index into OTP table of offset trims and call
  70. //function to return the correct offset trim
  71. //
  72. #ifndef _DUAL_HEADERS
  73. if(ADC_RESOLUTION_12BIT == resolution)
  74. #else
  75. if(ADC_BITRESOLUTION_12BIT == resolution)
  76. #endif
  77. {
  78. adcOffsetTrimOTPIndex = 4*adc + 1*signalmode;
  79. }
  80. else
  81. {
  82. adcOffsetTrimOTPIndex = 4*adc + 1*signalmode + 2;
  83. }
  84. adcOffsetTrim = (*GetAdcOffsetTrimOTP)(adcOffsetTrimOTPIndex);
  85. }
  86. else
  87. {
  88. //
  89. //offset trim function is not populated, so set offset trim to 0
  90. //
  91. adcOffsetTrim = 0;
  92. }
  93. //
  94. // Apply the resolution and signalmode to the specified ADC.
  95. // Also apply the offset trim and, if needed, linearity trim correction.
  96. //
  97. switch(adc)
  98. {
  99. case ADC_ADCA:
  100. {
  101. AdcaRegs.ADCCTL2.bit.SIGNALMODE = signalmode;
  102. AdcaRegs.ADCOFFTRIM.all = adcOffsetTrim;
  103. #ifndef _DUAL_HEADERS
  104. if(ADC_RESOLUTION_12BIT == resolution)
  105. #else
  106. if(ADC_BITRESOLUTION_12BIT == resolution)
  107. #endif
  108. {
  109. AdcaRegs.ADCCTL2.bit.RESOLUTION = 0;
  110. //
  111. //12-bit linearity trim workaround
  112. //
  113. AdcaRegs.ADCINLTRIM1 &= 0xFFFF0000;
  114. AdcaRegs.ADCINLTRIM2 &= 0xFFFF0000;
  115. AdcaRegs.ADCINLTRIM4 &= 0xFFFF0000;
  116. AdcaRegs.ADCINLTRIM5 &= 0xFFFF0000;
  117. }
  118. else
  119. {
  120. AdcaRegs.ADCCTL2.bit.RESOLUTION = 1;
  121. }
  122. break;
  123. }
  124. case ADC_ADCB:
  125. {
  126. AdcbRegs.ADCCTL2.bit.SIGNALMODE = signalmode;
  127. AdcbRegs.ADCOFFTRIM.all = adcOffsetTrim;
  128. #ifndef _DUAL_HEADERS
  129. if(ADC_RESOLUTION_12BIT == resolution)
  130. #else
  131. if(ADC_BITRESOLUTION_12BIT == resolution)
  132. #endif
  133. {
  134. AdcbRegs.ADCCTL2.bit.RESOLUTION = 0;
  135. //
  136. //12-bit linearity trim workaround
  137. //
  138. AdcbRegs.ADCINLTRIM1 &= 0xFFFF0000;
  139. AdcbRegs.ADCINLTRIM2 &= 0xFFFF0000;
  140. AdcbRegs.ADCINLTRIM4 &= 0xFFFF0000;
  141. AdcbRegs.ADCINLTRIM5 &= 0xFFFF0000;
  142. }
  143. else
  144. {
  145. AdcbRegs.ADCCTL2.bit.RESOLUTION = 1;
  146. }
  147. break;
  148. }
  149. case ADC_ADCC:
  150. {
  151. AdccRegs.ADCCTL2.bit.SIGNALMODE = signalmode;
  152. AdccRegs.ADCOFFTRIM.all = adcOffsetTrim;
  153. #ifndef _DUAL_HEADERS
  154. if(ADC_RESOLUTION_12BIT == resolution)
  155. #else
  156. if(ADC_BITRESOLUTION_12BIT == resolution)
  157. #endif
  158. {
  159. AdccRegs.ADCCTL2.bit.RESOLUTION = 0;
  160. //
  161. //12-bit linearity trim workaround
  162. //
  163. AdccRegs.ADCINLTRIM1 &= 0xFFFF0000;
  164. AdccRegs.ADCINLTRIM2 &= 0xFFFF0000;
  165. AdccRegs.ADCINLTRIM4 &= 0xFFFF0000;
  166. AdccRegs.ADCINLTRIM5 &= 0xFFFF0000;
  167. }
  168. else
  169. {
  170. AdccRegs.ADCCTL2.bit.RESOLUTION = 1;
  171. }
  172. break;
  173. }
  174. case ADC_ADCD:
  175. {
  176. AdcdRegs.ADCCTL2.bit.SIGNALMODE = signalmode;
  177. AdcdRegs.ADCOFFTRIM.all = adcOffsetTrim;
  178. #ifndef _DUAL_HEADERS
  179. if(ADC_RESOLUTION_12BIT == resolution)
  180. #else
  181. if(ADC_BITRESOLUTION_12BIT == resolution)
  182. #endif
  183. {
  184. AdcdRegs.ADCCTL2.bit.RESOLUTION = 0;
  185. //
  186. //12-bit linearity trim workaround
  187. //
  188. AdcdRegs.ADCINLTRIM1 &= 0xFFFF0000;
  189. AdcdRegs.ADCINLTRIM2 &= 0xFFFF0000;
  190. AdcdRegs.ADCINLTRIM4 &= 0xFFFF0000;
  191. AdcdRegs.ADCINLTRIM5 &= 0xFFFF0000;
  192. }
  193. else
  194. {
  195. AdcdRegs.ADCCTL2.bit.RESOLUTION = 1;
  196. }
  197. break;
  198. }
  199. }
  200. }
  201. //
  202. // CalAdcINL - Loads INL trim values from OTP into the trim registers of the
  203. // specified ADC. Use only as part of AdcSetMode function, since
  204. // linearity trim correction is needed for some modes.
  205. //
  206. void CalAdcINL(Uint16 adc)
  207. {
  208. switch(adc)
  209. {
  210. case ADC_ADCA:
  211. if(0xFFFF != *((Uint16*)CalAdcaINL))
  212. {
  213. //
  214. //trim function is programmed into OTP, so call it
  215. //
  216. (*CalAdcaINL)();
  217. }
  218. else
  219. {
  220. //
  221. //do nothing, no INL trim function populated
  222. //
  223. }
  224. break;
  225. case ADC_ADCB:
  226. if(0xFFFF != *((Uint16*)CalAdcbINL))
  227. {
  228. //
  229. //trim function is programmed into OTP, so call it
  230. //
  231. (*CalAdcbINL)();
  232. }
  233. else
  234. {
  235. //
  236. //do nothing, no INL trim function populated
  237. //
  238. }
  239. break;
  240. case ADC_ADCC:
  241. if(0xFFFF != *((Uint16*)CalAdccINL))
  242. {
  243. //
  244. //trim function is programmed into OTP, so call it
  245. //
  246. (*CalAdccINL)();
  247. }
  248. else
  249. {
  250. //
  251. //do nothing, no INL trim function populated
  252. //
  253. }
  254. break;
  255. case ADC_ADCD:
  256. if(0xFFFF != *((Uint16*)CalAdcdINL))
  257. {
  258. //
  259. //trim function is programmed into OTP, so call it
  260. //
  261. (*CalAdcdINL)();
  262. }
  263. else
  264. {
  265. //
  266. //do nothing, no INL trim function populated
  267. //
  268. }
  269. break;
  270. }
  271. }
  272. //
  273. // End of file
  274. //