Adc_Ip_HwAccess.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*==================================================================================================
  2. * Project : RTD AUTOSAR 4.4
  3. * Platform : CORTEXM
  4. * Peripheral : ADC
  5. * Dependencies : none
  6. *
  7. * Autosar Version : 4.4.0
  8. * Autosar Revision : ASR_REL_4_4_REV_0000
  9. * Autosar Conf.Variant :
  10. * SW Version : 1.0.0
  11. * Build Version : S32K1_RTD_1_0_0_HF01_D2109_ASR_REL_4_4_REV_0000_20210907
  12. *
  13. * (c) Copyright 2020-2021 NXP Semiconductors
  14. * All Rights Reserved.
  15. *
  16. * NXP Confidential. This software is owned or controlled by NXP and may only be
  17. * used strictly in accordance with the applicable license terms. By expressly
  18. * accepting such terms or by downloading, installing, activating and/or otherwise
  19. * using the software, you are agreeing that you have read, and that you agree to
  20. * comply with and are bound by, such license terms. If you do not agree to be
  21. * bound by the applicable license terms, then you may not retain, install,
  22. * activate or otherwise use the software.
  23. ==================================================================================================*/
  24. #ifndef ADC_IP_HW_ACCESS_H
  25. #define ADC_IP_HW_ACCESS_H
  26. /**
  27. * @file
  28. *
  29. * @internal
  30. * @addtogroup adc_ip Adc IPL
  31. * @{
  32. */
  33. #include "StandardTypes.h"
  34. /*******************************************************************************
  35. * Source file version information
  36. ******************************************************************************/
  37. #define ADC_IP_VENDOR_ID_HWACCESS_H 43
  38. #define ADC_IP_AR_RELEASE_MAJOR_VERSION_HWACCESS_H 4
  39. #define ADC_IP_AR_RELEASE_MINOR_VERSION_HWACCESS_H 4
  40. #define ADC_IP_AR_RELEASE_REVISION_VERSION_HWACCESS_H 0
  41. #define ADC_IP_SW_MAJOR_VERSION_HWACCESS_H 1
  42. #define ADC_IP_SW_MINOR_VERSION_HWACCESS_H 0
  43. #define ADC_IP_SW_PATCH_VERSION_HWACCESS_H 0
  44. /*******************************************************************************
  45. * File version checks
  46. ******************************************************************************/
  47. #ifndef DISABLE_MCAL_INTERMODULE_ASR_CHECK
  48. /* Check if Adc_Ip_HwAccess.h file and StandardTypes.h file are of the same Autosar version */
  49. #if ((ADC_IP_AR_RELEASE_MAJOR_VERSION_HWACCESS_H != STD_AR_RELEASE_MAJOR_VERSION) || \
  50. (ADC_IP_AR_RELEASE_MINOR_VERSION_HWACCESS_H != STD_AR_RELEASE_MINOR_VERSION) \
  51. )
  52. #error "AutoSar Version Numbers of Adc_Ip_HwAccess.h and StandardTypes.h are different"
  53. #endif
  54. #endif /* DISABLE_MCAL_INTERMODULE_ASR_CHECK */
  55. #if defined (__cplusplus)
  56. extern "C" {
  57. #endif
  58. /*******************************************************************************
  59. * Code
  60. ******************************************************************************/
  61. #define ADC_START_SEC_CODE
  62. #include "Adc_MemMap.h"
  63. static inline void Adc_HwAcc_SetSC2Reg(ADC_Type * const Base, const uint32 ClearMask, const uint32 Value)
  64. {
  65. /* Clear and set SC2 register based on ClearMask and Value */
  66. uint32 Sc2Reg = Base->SC2;
  67. Sc2Reg &= ~(ClearMask);
  68. Sc2Reg |= Value;
  69. Base->SC2 = Sc2Reg;
  70. }
  71. static inline void Adc_HwAcc_SetClock(ADC_Type * const Base, const Adc_Ip_ClockSelType ClockDivide, const Adc_Ip_ClkSourceType InputClock)
  72. {
  73. /* Update ClockDivide and InputClock values found in CFG1 register */
  74. uint32 Cfg1Reg = Base->CFG1;
  75. Cfg1Reg &= ~(ADC_CFG1_ADIV_MASK | ADC_CFG1_ADICLK_MASK);
  76. Cfg1Reg |= ADC_CFG1_ADIV(ClockDivide);
  77. Cfg1Reg |= ADC_CFG1_ADICLK(InputClock);
  78. Base->CFG1 = Cfg1Reg;
  79. }
  80. static inline Adc_Ip_ClockSelType Adc_HwAcc_GetClockDivide(const uint32 Reg)
  81. {
  82. /*
  83. * Retrieve ClockDivide value found in CFG1 register
  84. * Note: Reg should be the value of CFG1 register
  85. */
  86. Adc_Ip_ClockSelType ReturnValue = ADC_IP_CLK_FULL_BUS;
  87. switch ((Reg & ADC_CFG1_ADIV_MASK) >> ADC_CFG1_ADIV_SHIFT)
  88. {
  89. case 1u:
  90. ReturnValue = ADC_IP_CLK_HALF_BUS;
  91. break;
  92. case 2u:
  93. ReturnValue = ADC_IP_CLK_QUARTER_BUS;
  94. break;
  95. case 3u:
  96. ReturnValue = ADC_IP_CLK_EIGHTH_BUS;
  97. break;
  98. default:
  99. ; /* no-op */
  100. break;
  101. }
  102. return ReturnValue;
  103. }
  104. static inline Adc_Ip_ClkSourceType Adc_HwAcc_GetInputClock(const uint32 Reg)
  105. {
  106. /*
  107. * Retrieve InputClock value found in CFG1 register
  108. * Note: Reg should be the value of CFG1 register
  109. */
  110. Adc_Ip_ClkSourceType ReturnValue = ADC_IP_CLK_ALT_1;
  111. switch ((Reg & ADC_CFG1_ADICLK_MASK) >> ADC_CFG1_ADICLK_SHIFT)
  112. {
  113. case 1u:
  114. ReturnValue = ADC_IP_CLK_ALT_2;
  115. break;
  116. case 2u:
  117. ReturnValue = ADC_IP_CLK_ALT_3;
  118. break;
  119. case 3u:
  120. ReturnValue = ADC_IP_CLK_ALT_4;
  121. break;
  122. default:
  123. ; /* no-op */
  124. break;
  125. }
  126. return ReturnValue;
  127. }
  128. static inline void Adc_HwAcc_SetSampleTime(ADC_Type * const Base, const uint8 SampleTime)
  129. {
  130. /* Clip sample time to minimum value */
  131. uint8 ClippedSampleTime = (uint8)((SampleTime > 0U) ? SampleTime : 1U);
  132. /* Update SampleTime values found in SC3 register */
  133. uint32 Cfg2Reg = Base->CFG2;
  134. Cfg2Reg &= ~(ADC_CFG2_SMPLTS_MASK);
  135. Cfg2Reg |= ADC_CFG2_SMPLTS(ClippedSampleTime);
  136. Base->CFG2 = Cfg2Reg;
  137. }
  138. static inline void Adc_HwAcc_SetAveraging(ADC_Type * const Base, const boolean AvgEn, const Adc_Ip_AvgSelectType AvgSel)
  139. {
  140. /* Update AvgEn and AvgSel values found in SC3 register */
  141. uint32 Sc3Reg = Base->SC3;
  142. Sc3Reg &= ~(ADC_SC3_AVGE_MASK | ADC_SC3_AVGS_MASK);
  143. Sc3Reg |= ADC_SC3_AVGE(AvgEn ? 1u : 0u);
  144. Sc3Reg |= ADC_SC3_AVGS(AvgSel);
  145. Base->SC3 = Sc3Reg;
  146. }
  147. static inline Adc_Ip_AvgSelectType Adc_HwAcc_GetAverageSelect(const uint32 Reg)
  148. {
  149. /*
  150. * Retrieve AvgSelect value found in SC3 register
  151. * Note: Reg should be the value of SC3 register
  152. */
  153. Adc_Ip_AvgSelectType ReturnValue = ADC_IP_AVG_4_CONV;
  154. switch ((Reg & ADC_SC3_AVGS_MASK) >> ADC_SC3_AVGS_SHIFT)
  155. {
  156. case 1u:
  157. ReturnValue = ADC_IP_AVG_8_CONV;
  158. break;
  159. case 2u:
  160. ReturnValue = ADC_IP_AVG_16_CONV;
  161. break;
  162. case 3u:
  163. ReturnValue = ADC_IP_AVG_32_CONV;
  164. break;
  165. default:
  166. ; /* no-op */
  167. break;
  168. }
  169. return ReturnValue;
  170. }
  171. static inline void Adc_HwAcc_SetTriggerMode(ADC_Type * const Base, const Adc_Ip_TrigType TriggerMode)
  172. {
  173. /* Update TriggerMode value found in SC2 register */
  174. uint32 Sc2Reg = Base->SC2;
  175. Sc2Reg &= ~(ADC_SC2_ADTRG_MASK);
  176. Sc2Reg |= ADC_SC2_ADTRG(TriggerMode);
  177. Base->SC2 = Sc2Reg;
  178. }
  179. static inline Adc_Ip_TrigType Adc_HwAcc_GetTriggerMode(const uint32 Reg)
  180. {
  181. /* Retrieve TriggerMode value found in SC2 register */
  182. Adc_Ip_TrigType ReturnValue = ADC_IP_TRIGGER_SOFTWARE;
  183. if (((Reg & ADC_SC2_ADTRG_MASK) >> ADC_SC2_ADTRG_SHIFT) == 1u)
  184. {
  185. ReturnValue = ADC_IP_TRIGGER_HARDWARE;
  186. }
  187. return ReturnValue;
  188. }
  189. static inline void Adc_HwAcc_SetChannel(ADC_Type * const Base, const uint8 ChnIdx, const Adc_Ip_InputChannelType InputChannel, const boolean InterruptEnable)
  190. {
  191. /* Configure channel by writing all SC1n register fields */
  192. uint32 Sc1Reg = SC1(Base, ChnIdx);
  193. Sc1Reg &= ~(ADC_SC1_ADCH_MASK | ADC_SC1_AIEN_MASK);
  194. Sc1Reg |= ADC_SC1_ADCH(InputChannel);
  195. Sc1Reg |= ADC_SC1_AIEN(InterruptEnable ? 1u : 0u);
  196. SC1(Base, ChnIdx) = Sc1Reg;
  197. }
  198. static inline void Adc_HwAcc_SetUserGainAndOffset(ADC_Type * const Base, const uint16 UsrGain, const uint16 UsrOffset)
  199. {
  200. /*
  201. * Currently, user gain and user offset values are set at config time.
  202. * If user gain is to be changed at runtime (e.g. after a calibration was
  203. * already executed) then the G register should also be updated.
  204. * To calculate the new value of this register, it is necessary execute
  205. * the following algorithm:
  206. * 1. Sum <- UsrGain + Clp0 + Clp1 + Clp2 + Clp3 + ClpS
  207. * 2. RegVal <- Sum & 0xF800U
  208. * 3. if RegVal != 0x0000U then RegVal <- 0xFFFFU
  209. * 4. Base->G <- RegVal
  210. */
  211. Base->USR_OFS = ADC_USR_OFS_USR_OFS(UsrOffset);
  212. Base->UG = ADC_UG_UG(UsrGain);
  213. }
  214. static inline boolean Adc_HwAcc_GetAIEN(const uint32 Reg)
  215. {
  216. /* Retrive AIEN flag from given SC1 register */
  217. return (((Reg & ADC_SC1_AIEN_MASK) >> ADC_SC1_AIEN_SHIFT) != 0u) ? TRUE : FALSE;
  218. }
  219. static inline boolean Adc_HwAcc_GetCOCO(const uint32 Reg)
  220. {
  221. /* Retrive COCO flag from given SC1 register */
  222. return (((Reg & ADC_SC1_COCO_MASK) >> ADC_SC1_COCO_SHIFT) != 0u) ? TRUE : FALSE;
  223. }
  224. static inline uint16 Adc_HwAcc_GetData(const ADC_Type * const Base, const uint8 ChnIdx)
  225. {
  226. /* Retrieve the conversion result of a given channel */
  227. uint16 Result = (uint16) R(Base, ChnIdx);
  228. Result = (uint16) ((Result & ADC_R_D_MASK) >> ADC_R_D_SHIFT);
  229. return Result;
  230. }
  231. #define ADC_STOP_SEC_CODE
  232. #include "Adc_MemMap.h"
  233. #if defined (__cplusplus)
  234. }
  235. #endif
  236. /** @} */
  237. #endif /* ADC_IP_HW_ACCESS_H */