Lpit_Icu_Ip.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*==================================================================================================
  2. * Project : RTD AUTOSAR 4.4
  3. * Platform : CORTEXM
  4. * Peripheral : Ftm Lpit Lptmr Port_Ci LpCmp
  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. /**
  25. * @file Lpit_Icu_Ip.c
  26. * @details This source file contains the code for all driver functions which are using LPIT
  27. * module.
  28. * @addtogroup lpit_icu_ip LPIT IPL
  29. * @{
  30. */
  31. #ifdef __cplusplus
  32. extern "C"{
  33. #endif
  34. /*==================================================================================================
  35. * INCLUDE FILES
  36. * 1) system and project includes
  37. * 2) needed interfaces from external units
  38. * 3) internal and external interfaces from this unit
  39. ==================================================================================================*/
  40. #include "Lpit_Icu_Ip.h"
  41. #if(LPIT_ICU_IP_DEV_ERROR_DETECT == STD_ON)
  42. #include "Devassert.h"
  43. #endif
  44. /*==================================================================================================
  45. * SOURCE FILE VERSION INFORMATION
  46. ==================================================================================================*/
  47. #define LPIT_ICU_IP_VENDOR_ID_C 43
  48. #define LPIT_ICU_IP_AR_RELEASE_MAJOR_VERSION_C 4
  49. #define LPIT_ICU_IP_AR_RELEASE_MINOR_VERSION_C 4
  50. #define LPIT_ICU_IP_AR_RELEASE_REVISION_VERSION_C 0
  51. #define LPIT_ICU_IP_SW_MAJOR_VERSION_C 1
  52. #define LPIT_ICU_IP_SW_MINOR_VERSION_C 0
  53. #define LPIT_ICU_IP_SW_PATCH_VERSION_C 0
  54. /*==================================================================================================
  55. * FILE VERSION CHECKS
  56. ==================================================================================================*/
  57. /* Check if source file and Lpit_Icu_Ip.h header file are of the same vendor */
  58. #if (LPIT_ICU_IP_VENDOR_ID_C != LPIT_ICU_IP_VENDOR_ID)
  59. #error "Lpit_Icu_Ip.c and Lpit_Icu_Ip.h have different vendor IDs"
  60. #endif
  61. /* Check if source file and Lpit_Icu_Ip.h header file are of the same AUTOSAR version */
  62. #if ((LPIT_ICU_IP_AR_RELEASE_MAJOR_VERSION_C != LPIT_ICU_IP_AR_RELEASE_MAJOR_VERSION) || \
  63. (LPIT_ICU_IP_AR_RELEASE_MINOR_VERSION_C != LPIT_ICU_IP_AR_RELEASE_MINOR_VERSION) || \
  64. (LPIT_ICU_IP_AR_RELEASE_REVISION_VERSION_C != LPIT_ICU_IP_AR_RELEASE_REVISION_VERSION))
  65. #error "AutoSar Version Numbers of Lpit_Icu_Ip.c and Lpit_Icu_Ip.h are different"
  66. #endif
  67. /* Check if source file and Lpit_Icu_Ip.h header file are of the same software version */
  68. #if ((LPIT_ICU_IP_SW_MAJOR_VERSION_C != LPIT_ICU_IP_SW_MAJOR_VERSION) || \
  69. (LPIT_ICU_IP_SW_MINOR_VERSION_C != LPIT_ICU_IP_SW_MINOR_VERSION) || \
  70. (LPIT_ICU_IP_SW_PATCH_VERSION_C != LPIT_ICU_IP_SW_PATCH_VERSION))
  71. #error "Software Version Numbers of Lpit_Icu_Ip.c and Lpit_Icu_Ip.h are different"
  72. #endif
  73. #ifndef DISABLE_MCAL_INTERMODULE_ASR_CHECK
  74. #if(LPIT_ICU_IP_DEV_ERROR_DETECT == STD_ON)
  75. /* Check if this header file and Devassert.h file are of the same Autosar version */
  76. #if ((LPIT_ICU_IP_AR_RELEASE_MAJOR_VERSION_C != DEVASSERT_AR_RELEASE_MAJOR_VERSION) || \
  77. (LPIT_ICU_IP_AR_RELEASE_MINOR_VERSION_C != DEVASSERT_AR_RELEASE_MINOR_VERSION))
  78. #error "AutoSar Version Numbers of Lpit_Icu_Ip.c and Devassert.h are different"
  79. #endif
  80. #endif
  81. #endif
  82. /*==================================================================================================
  83. * LOCAL CONSTANTS
  84. ==================================================================================================*/
  85. #define ICU_START_SEC_CONST_UNSPECIFIED
  86. #include "Icu_MemMap.h"
  87. /* Table of base addresses for LPIT instances. */
  88. LPIT_Type* const lpitBase[] = IP_LPIT_BASE_PTRS;
  89. #define ICU_STOP_SEC_CONST_UNSPECIFIED
  90. #include "Icu_MemMap.h"
  91. /*==================================================================================================
  92. * GLOBAL VARIABLES
  93. ==================================================================================================*/
  94. #define ICU_START_SEC_VAR_CLEARED_UNSPECIFIED_NO_CACHEABLE
  95. #include "Icu_MemMap.h"
  96. #if (STD_ON == LPIT_ICU_TIMESTAMP_API)
  97. Lpit_Icu_Ip_TimestampType timestampState[LPIT_INSTANCE_COUNT][LPIT_TMR_COUNT];
  98. #endif
  99. Lpit_Icu_Ip_ChannelsStateType channelsState[LPIT_INSTANCE_COUNT][LPIT_TMR_COUNT];
  100. #define ICU_STOP_SEC_VAR_CLEARED_UNSPECIFIED_NO_CACHEABLE
  101. #include "Icu_MemMap.h"
  102. /*==================================================================================================
  103. * GLOBAL FUNCTIONS
  104. ==================================================================================================*/
  105. #define ICU_START_SEC_CODE
  106. #include "Icu_MemMap.h"
  107. /** @implements Lpit_Icu_Ip_Init_Activity */
  108. Lpit_Icu_Ip_StatusType Lpit_Icu_Ip_Init (uint8 instance, const Lpit_Icu_Ip_ConfigType * userConfig)
  109. {
  110. const Lpit_Icu_Ip_ChannelConfigType* lpitChConfig;
  111. uint8 counter;
  112. /* Enable hardware module clock. This shall be be executed before any other setup is made. */
  113. lpitBase[instance]->MCR |= LPIT_MCR_M_CEN_MASK;
  114. for (counter = 0U; counter < userConfig->numberOfChannels; counter++)
  115. {
  116. lpitChConfig = &(*userConfig->pChannelsConfig)[counter];
  117. /* Enables the LPIT timer channel. */
  118. lpitBase[instance]->TMR[lpitChConfig->hwChannel].TCTRL |= LPIT_TMR_TCTRL_T_EN_MASK;
  119. /* Disable channel interrupts. */
  120. lpitBase[instance]->MIER &= ~(uint32)(LPIT_MIER_TIE_START_MASK << (uint32)lpitChConfig->hwChannel);
  121. /* Clear pending interrupts. */
  122. lpitBase[instance]->MSR |= (uint32)(LPIT_MSR_TIF_START_MASK << (uint32)lpitChConfig->hwChannel);
  123. /* Enable LPIT in DOZE mode. */
  124. lpitBase[instance]->MCR |= LPIT_MCR_DOZE_EN_MASK;
  125. /* Enable/Disable Debug(freeze) mode. */
  126. /* if ((uint8)1U == u8freezeEnable)
  127. {
  128. REG_BIT_SET32(LPIT_MCR_ADDR32(instance), LPIT_MCR_FRZ_MASK_U32);
  129. }
  130. else
  131. {
  132. REG_BIT_CLEAR32(LPIT_MCR_ADDR32(instance), LPIT_MCR_FRZ_MASK_U32);
  133. } */
  134. lpitBase[instance]->MCR |= (uint32)((uint32)(userConfig->debugState?1U:0U) << LPIT_MCR_DBG_EN_SHIFT);
  135. /* Select source of trigger as internal/external trigger. */
  136. /* if ((uint8)1U == u8TriggerSource)
  137. {
  138. Trigger source selected is the internal trigger
  139. REG_BIT_SET32(LPIT_TCTRL_ADDR32((instance),(currentCh)),(uint32)LPIT_TCTRL_TRG_SRC_MASK_U32);
  140. }
  141. else
  142. {
  143. Trigger source selected is the external trigger
  144. REG_BIT_CLEAR32(LPIT_TCTRL_ADDR32((instance),(currentCh)),(uint32)LPIT_TCTRL_TRG_SRC_MASK_U32);
  145. } */
  146. lpitBase[instance]->TMR[lpitChConfig->hwChannel].TCTRL |= (uint32)((uint32)lpitChConfig->triggerSource << LPIT_TMR_TCTRL_TRG_SRC_SHIFT);
  147. /* Selects the trigger to use for starting and/or reloading the LPIT timer. */
  148. lpitBase[instance]->TMR[lpitChConfig->hwChannel].TCTRL |= (uint32)((uint32)lpitChConfig->triggerSelect << LPIT_TMR_TCTRL_TRG_SEL_SHIFT);
  149. /* Enter Trigger Input Capture Mode */
  150. lpitBase[instance]->TMR[lpitChConfig->hwChannel].TCTRL |= LPIT_TMR_TCTRL_MODE_MASK;
  151. channelsState[instance][lpitChConfig->hwChannel].initState = TRUE;
  152. channelsState[instance][lpitChConfig->hwChannel].callback = lpitChConfig->callback;
  153. channelsState[instance][lpitChConfig->hwChannel].callbackParams = lpitChConfig->callbackParams;
  154. channelsState[instance][lpitChConfig->hwChannel].lpitChannelNotify = lpitChConfig->lpitChannelNotify;
  155. channelsState[instance][lpitChConfig->hwChannel].logicChStateCallback = lpitChConfig->logicChStateCallback;
  156. channelsState[instance][lpitChConfig->hwChannel].notificationEnable = lpitChConfig->initNotificationEnable;
  157. #if (STD_ON == LPIT_ICU_TIMESTAMP_API)
  158. /* Save state for the timestamp APIs. */
  159. timestampState[instance][lpitChConfig->hwChannel].timestampBuffer = lpitChConfig->timestampBuffer;
  160. #endif
  161. }
  162. return LPIT_IP_STATUS_SUCCESS;
  163. }
  164. #if (LPIT_ICU_DEINIT_API == STD_ON)
  165. /** @implements Lpit_Icu_Ip_Deinit_Activity */
  166. Lpit_Icu_Ip_StatusType Lpit_Icu_Ip_Deinit(uint8 instance)
  167. {
  168. uint8 currentCh;
  169. /* Enable hardware module clock. This shall be be executed before any other setup is made */
  170. lpitBase[instance]->MCR |= LPIT_MCR_M_CEN_MASK;
  171. for (currentCh=0U; currentCh < LPIT_TMR_COUNT; currentCh++)
  172. {
  173. /* Disable the LPIT timer channel. */
  174. lpitBase[instance]->TMR[currentCh].TCTRL &= ~LPIT_TMR_TCTRL_T_EN_MASK;
  175. /* Disable channel interrupts. */
  176. lpitBase[instance]->MIER &= ~(uint32)(LPIT_MIER_TIE_START_MASK << (uint32)currentCh);
  177. /* Clear interrupt flag. */
  178. lpitBase[instance]->MSR |= (uint32)((uint32)LPIT_MSR_TIF0_MASK << (uint32)currentCh);
  179. /* Clear debug mode. */
  180. lpitBase[instance]->MCR &= ~LPIT_MCR_DBG_EN_MASK;
  181. /* Disable DOZE mode. */
  182. lpitBase[instance]->MCR &= ~LPIT_MCR_DOZE_EN_MASK;
  183. /* Update state variable. */
  184. channelsState[instance][currentCh].initState = FALSE;
  185. channelsState[instance][currentCh].notificationEnable = FALSE;
  186. }
  187. /* Disable hardware module clock */
  188. lpitBase[instance]->MCR &= ~LPIT_MCR_M_CEN_MASK;
  189. return LPIT_IP_STATUS_SUCCESS;
  190. }
  191. #endif /* LPIT_ICU_DEINIT_API */
  192. /** @implements Lpit_Icu_Ip_EnableInterrupt_Activity */
  193. void Lpit_Icu_Ip_EnableInterrupt(uint8 instance, uint8 hwChannel)
  194. {
  195. /* Clear timer interrupt flag if it is enabled. */
  196. lpitBase[instance]->MSR |= (uint32)(LPIT_MSR_TIF_START_MASK << (uint32)hwChannel);
  197. /* Enable channel interrupts. */
  198. lpitBase[instance]->MIER |= (uint32)(LPIT_MIER_TIE_START_MASK << (uint32)hwChannel);
  199. }
  200. /** @implements Lpit_Icu_Ip_DisableInterrupt_Activity */
  201. void Lpit_Icu_Ip_DisableInterrupt(uint8 instance, uint8 hwChannel)
  202. {
  203. /* Disable channel interrupts. */
  204. lpitBase[instance]->MIER &= ~(uint32)(LPIT_MIER_TIE_START_MASK << (uint32)hwChannel);
  205. }
  206. #if (LPIT_ICU_EDGE_DETECT_API == STD_ON)
  207. /** @implements Lpit_Icu_Ip_EnableEdgeDetection_Activity */
  208. void Lpit_Icu_Ip_EnableEdgeDetection(uint8 instance, uint8 hwChannel)
  209. {
  210. /* Enable interrupt for channel. */
  211. Lpit_Icu_Ip_EnableInterrupt(instance, hwChannel);
  212. /* Set Edge Detect mode for the Lpit channel in the configuration array */
  213. channelsState[instance][hwChannel].measurementMode = LPIT_ICU_MODE_SIGNAL_EDGE_DETECT;
  214. }
  215. #endif /* LPIT_ICU_EDGE_DETECT_API */
  216. #if ((LPIT_ICU_EDGE_DETECT_API == STD_ON) || (LPIT_ICU_TIMESTAMP_API == STD_ON))
  217. /** @implements Lpit_Icu_Ip_DisableDetectionMode_Activity */
  218. void Lpit_Icu_Ip_DisableDetectionMode(uint8 instance, uint8 hwChannel)
  219. {
  220. /* Disable interrupt for channel. */
  221. Lpit_Icu_Ip_DisableInterrupt(instance, hwChannel);
  222. /* Clean measurement mode for the LPIT channel in the configuration array. */
  223. channelsState[instance][hwChannel].measurementMode = LPIT_ICU_MODE_NO_MEASUREMENT;
  224. }
  225. #endif /* (LPIT_ICU_EDGE_DETECT_API == STD_ON) || (LPIT_ICU_TIMESTAMP_API == STD_ON) */
  226. #if (STD_ON == LPIT_ICU_GET_INPUT_STATE_API)
  227. /** @implements Lpit_Icu_Ip_GetInputState_Activity */
  228. boolean Lpit_Icu_Ip_GetInputState(uint8 instance, uint8 hwChannel)
  229. {
  230. boolean result = FALSE;
  231. uint32 statusRegister = lpitBase[instance]->MSR;
  232. uint32 intEnRegister = lpitBase[instance]->MIER;
  233. /* Interrupt not enabled, flag bit was set. */
  234. if (0x0U == ((uint32)(LPIT_MIER_TIE_START_MASK << hwChannel) & intEnRegister))
  235. {
  236. if (0x0U != ((uint32)(LPIT_MSR_TIF_START_MASK << hwChannel) & statusRegister))
  237. {
  238. result = TRUE;
  239. /* Clear interrupt flag. */
  240. lpitBase[instance]->MSR |= (uint32)(LPIT_MSR_TIF_START_MASK << hwChannel);
  241. }
  242. }
  243. return result;
  244. }
  245. #endif /* STD_ON == LPIT_ICU_GET_INPUT_STATE_API */
  246. #if (STD_ON == LPIT_ICU_TIMESTAMP_API)
  247. /** @implements Lpit_Icu_Ip_StartTimestamp_Activity */
  248. void Lpit_Icu_Ip_StartTimestamp(uint8 instance,
  249. uint8 hwChannel,
  250. uint16 *bufferPtr,
  251. uint16 bufferSize,
  252. uint16 notifyInterval)
  253. {
  254. #if (STD_ON == LPIT_ICU_IP_DEV_ERROR_DETECT)
  255. DevAssert(LPIT_INSTANCE_COUNT > instance);
  256. DevAssert(LPIT_TMR_COUNT > hwChannel);
  257. DevAssert(NULL_PTR != lpitBase[instance]);
  258. #endif
  259. /* Configure the Timestamp mode for the LPIT hwChannel in the state configuration array. */
  260. channelsState[instance][hwChannel].measurementMode = LPIT_ICU_MODE_TIMESTAMP;
  261. /* Initiate timestamp state for current channel. */
  262. timestampState[instance][hwChannel].bufferPtr = bufferPtr;
  263. timestampState[instance][hwChannel].bufferSize = bufferSize;
  264. timestampState[instance][hwChannel].notifyInterval = notifyInterval;
  265. timestampState[instance][hwChannel].notifyCount = (uint16)0U;
  266. timestampState[instance][hwChannel].bufferIndex = (uint16)0U;
  267. /* Clear interrupt flag. */
  268. lpitBase[instance]->MSR |= (uint32)(LPIT_MSR_TIF_START_MASK << (uint32)hwChannel);
  269. /* Enable interrupt for channel. */
  270. Lpit_Icu_Ip_EnableInterrupt(instance, hwChannel);
  271. }
  272. /**
  273. * @brief Get timestamp index for timestamp mode.
  274. */
  275. uint16 Lpit_Icu_Ip_GetTimestampIndex(uint8 instance, uint8 channel)
  276. {
  277. #if (STD_ON == LPIT_ICU_IP_DEV_ERROR_DETECT)
  278. DevAssert(LPIT_INSTANCE_COUNT > instance);
  279. DevAssert(LPIT_TMR_COUNT > channel);
  280. #endif
  281. uint16 timestampIndex = 0U;
  282. if (NULL_PTR != timestampState[instance][channel].bufferPtr)
  283. {
  284. timestampIndex = timestampState[instance][channel].bufferIndex;
  285. }
  286. return timestampIndex;
  287. }
  288. #endif /* STD_ON == LPIT_ICU_TIMESTAMP_API */
  289. /**
  290. * @brief Driver function Enable Notification for timestamp.
  291. */
  292. void Lpit_Icu_Ip_EnableNotification(uint8 instance, uint8 hwChannel)
  293. {
  294. channelsState[instance][hwChannel].notificationEnable = TRUE;
  295. }
  296. /**
  297. * @brief Driver function Disable Notification for timestamp.
  298. */
  299. void Lpit_Icu_Ip_DisableNotification(uint8 instance, uint8 hwChannel)
  300. {
  301. channelsState[instance][hwChannel].notificationEnable = FALSE;
  302. }
  303. #define ICU_STOP_SEC_CODE
  304. #include "Icu_MemMap.h"
  305. #ifdef __cplusplus
  306. }
  307. #endif
  308. /** @} */