LPit_Gpt_Ip.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. /*==================================================================================================
  2. * Project : RTD AUTOSAR 4.4
  3. * Platform : CORTEXM
  4. * Peripheral : Ftm_Srtc_Lptmr_LPit
  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_Gpt_Ip.c
  26. *
  27. * @addtogroup lpit_ip LPit IPL
  28. *
  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_Gpt_Ip.h"
  41. #ifdef LPIT_GPT_IP_ENABLE_USER_MODE_SUPPORT
  42. #define USER_MODE_REG_PROT_ENABLED LPIT_GPT_IP_ENABLE_USER_MODE_SUPPORT
  43. #include "RegLockMacros.h"
  44. #endif
  45. /*==================================================================================================
  46. * SOURCE FILE VERSION INFORMATION
  47. ==================================================================================================*/
  48. #define LPIT_GPT_IP_VENDOR_ID_C 43
  49. #define LPIT_GPT_IP_AR_RELEASE_MAJOR_VERSION_C 4
  50. #define LPIT_GPT_IP_AR_RELEASE_MINOR_VERSION_C 4
  51. #define LPIT_GPT_IP_AR_RELEASE_REVISION_VERSION_C 0
  52. #define LPIT_GPT_IP_SW_MAJOR_VERSION_C 1
  53. #define LPIT_GPT_IP_SW_MINOR_VERSION_C 0
  54. #define LPIT_GPT_IP_SW_PATCH_VERSION_C 0
  55. /*==================================================================================================
  56. * FILE VERSION CHECKS
  57. ==================================================================================================*/
  58. #if (LPIT_GPT_IP_VENDOR_ID != LPIT_GPT_IP_VENDOR_ID_C)
  59. #error "LPit_Ip.h and LPit_Ip.c have different vendor ids"
  60. #endif
  61. /* Check if header file and Gpt header file are of the same Autosar version */
  62. #if ((LPIT_GPT_IP_AR_RELEASE_MAJOR_VERSION != LPIT_GPT_IP_AR_RELEASE_MAJOR_VERSION_C) || \
  63. (LPIT_GPT_IP_AR_RELEASE_MINOR_VERSION != LPIT_GPT_IP_AR_RELEASE_MINOR_VERSION_C) || \
  64. (LPIT_GPT_IP_AR_RELEASE_REVISION_VERSION != LPIT_GPT_IP_AR_RELEASE_REVISION_VERSION_C) \
  65. )
  66. #error "AutoSar Version Numbers of LPit_Gpt_Ip.h and LPit_Gpt_Ip.c are different"
  67. #endif
  68. /* Check if source file and GPT header file are of the same Software version */
  69. #if ((LPIT_GPT_IP_SW_MAJOR_VERSION != LPIT_GPT_IP_SW_MAJOR_VERSION_C) || \
  70. (LPIT_GPT_IP_SW_MINOR_VERSION != LPIT_GPT_IP_SW_MINOR_VERSION_C) || \
  71. (LPIT_GPT_IP_SW_PATCH_VERSION != LPIT_GPT_IP_SW_PATCH_VERSION_C) \
  72. )
  73. #error "Software Version Numbers of LPit_Gpt_Ip.h and LPit_Gpt_Ip.c are different"
  74. #endif
  75. #ifdef LPIT_GPT_IP_ENABLE_USER_MODE_SUPPORT
  76. #ifndef DISABLE_MCAL_INTERMODULE_ASR_CHECK
  77. #if ((REGLOCKMACROS_AR_RELEASE_MAJOR_VERSION != LPIT_GPT_IP_AR_RELEASE_MAJOR_VERSION_C) || \
  78. (REGLOCKMACROS_AR_RELEASE_MINOR_VERSION != LPIT_GPT_IP_AR_RELEASE_MINOR_VERSION_C))
  79. #error "AutoSar Version Numbers of RegLockMacros.h and LPit_Gpt_Ip.c are different"
  80. #endif
  81. #endif
  82. #endif
  83. /*==================================================================================================
  84. * LOCAL TYPEDEFS (STRUCTURES, UNIONS, ENUMS)
  85. ==================================================================================================*/
  86. /*=================================================================================================
  87. * LOCAL MACROS
  88. =================================================================================================*/
  89. /*==================================================================================================
  90. * LOCAL CONSTANTS
  91. ==================================================================================================*/
  92. /*==================================================================================================
  93. * LOCAL VARIABLES
  94. ==================================================================================================*/
  95. #if (LPIT_GPT_IP_USED == STD_ON)
  96. #if ((defined LPIT_0_ISR_USED)||(defined LPIT_0_CH_0_ISR_USED)||(defined LPIT_0_CH_1_ISR_USED)||(defined LPIT_0_CH_2_ISR_USED)||(defined LPIT_0_CH_3_ISR_USED))
  97. static Lpit_Gpt_Ip_State Lpit_Gpt_Ip_u32ChState[LPIT_INSTANCE_COUNT][LPIT_TMR_COUNT] = {
  98. {
  99. {
  100. (boolean)FALSE,
  101. NULL_PTR,
  102. 0U
  103. }
  104. }
  105. };
  106. #endif
  107. /*==================================================================================================
  108. * GLOBAL CONSTANTS
  109. ==================================================================================================*/
  110. /*==================================================================================================
  111. * GLOBAL VARIABLES
  112. ==================================================================================================*/
  113. #define GPT_START_SEC_CONST_UNSPECIFIED
  114. #include "Gpt_MemMap.h"
  115. /** @brief Table of base addresses for PIT instances. */
  116. LPIT_Type * const LPitGptBase[LPIT_INSTANCE_COUNT] = IP_LPIT_BASE_PTRS;
  117. #define GPT_STOP_SEC_CONST_UNSPECIFIED
  118. #include "Gpt_MemMap.h"
  119. #if ((defined LPIT_0_ISR_USED)||(defined LPIT_0_CH_0_ISR_USED)||(defined LPIT_0_CH_1_ISR_USED)||(defined LPIT_0_CH_2_ISR_USED)||(defined LPIT_0_CH_3_ISR_USED))
  120. static void Lpit_Gpt_Ip_ProcessCommonInterrupt(uint8 instance, uint8 channel);
  121. #endif
  122. #if (defined (LPIT_GPT_IP_MODULE_SINGLE_INTERRUPT) && (STD_ON == LPIT_GPT_IP_MODULE_SINGLE_INTERRUPT))
  123. #ifdef LPIT_0_ISR_USED
  124. ISR(LPIT_0_ISR);
  125. #endif
  126. #else
  127. #ifdef LPIT_0_CH_0_ISR_USED
  128. ISR(LPIT_0_CH_0_ISR);
  129. #endif
  130. #ifdef LPIT_0_CH_1_ISR_USED
  131. ISR(LPIT_0_CH_1_ISR);
  132. #endif
  133. #ifdef LPIT_0_CH_2_ISR_USED
  134. ISR(LPIT_0_CH_2_ISR);
  135. #endif
  136. #ifdef LPIT_0_CH_3_ISR_USED
  137. ISR(LPIT_0_CH_3_ISR);
  138. #endif
  139. #endif /*LPIT_GPT_IP_MODULE_SINGLE_INTERRUPT == STD_ON*/
  140. static inline void Lpit_Gpt_Ip_EnableMdlClk(uint8 instance, boolean enable);
  141. static inline void Lpit_Gpt_Ip_TimerEnable(uint8 instance, uint8 channel, boolean enable);
  142. #if (LPIT_GPT_IP_ENABLE_EXT_TRIGGERS == STD_ON)
  143. static inline void Lpit_Gpt_Ip_SetTrigger(uint8 instance, uint8 channel, uint32 value);
  144. #endif
  145. static inline void Lpit_Gpt_Ip_SetMode(uint8 instance, uint8 channel, uint32 mode);
  146. static inline void Lpit_Gpt_Ip_SetCmpValue(uint8 instance, uint8 channel, uint32 value);
  147. static inline void Lpit_Gpt_Ip_SetDebugMode(uint8 instance, boolean stopRun);
  148. static inline void Lpit_Gpt_Ip_SetDozeMode(uint8 instance, boolean running);
  149. static inline void Lpit_Gpt_Ip_InterruptTimerChannels(uint8 instance, uint8 channel, boolean enable);
  150. static inline void Lpit_Gpt_Ip_ClearInterruptFlagTimerChannels(uint8 instance, uint8 channel);
  151. #if (LPIT_GPT_IP_CHAIN_MODE == STD_ON)
  152. static inline void Lpit_Gpt_Ip_SetChainMode(uint8 instance, uint8 channel, boolean enable);
  153. #endif
  154. /*==================================================================================================
  155. * LOCAL FUNCTION PROTOTYPES
  156. ==================================================================================================*/
  157. /**
  158. * @brief
  159. * Function Name : Lpit_Gpt_Ip_EnableMdlClk
  160. * Description : Enable hardware module clock (Module Clock Enable)
  161. *
  162. * @param[in] instance LPit hardware instance number
  163. * @param[in] enable (TRUE/FALSE)
  164. *
  165. * @return void
  166. *
  167. */
  168. static inline void Lpit_Gpt_Ip_EnableMdlClk(uint8 instance, boolean enable)
  169. {
  170. SchM_Enter_Gpt_GPT_EXCLUSIVE_AREA_50();
  171. if (TRUE == enable)
  172. {
  173. LPitGptBase[instance]->MCR |= LPIT_MCR_M_CEN_MASK;
  174. }
  175. else
  176. {
  177. LPitGptBase[instance]->MCR &= (~(LPIT_MCR_M_CEN_MASK));
  178. }
  179. SchM_Exit_Gpt_GPT_EXCLUSIVE_AREA_50();
  180. }
  181. /**
  182. * @brief
  183. * Function Name : Lpit_Gpt_Ip_TimerEnable
  184. * Description : Enable Timer Enable (T_EN)
  185. *
  186. * @param[in] instance LPit hardware instance number
  187. * @param[in] channel LPit hardware channel number
  188. * @param[in] enable (TRUE/FALSE)
  189. *
  190. * @return void
  191. *
  192. */
  193. static inline void Lpit_Gpt_Ip_TimerEnable(uint8 instance, uint8 channel, boolean enable)
  194. {
  195. SchM_Enter_Gpt_GPT_EXCLUSIVE_AREA_51();
  196. if (TRUE == enable)
  197. {
  198. LPitGptBase[instance]->TMR[channel].TCTRL |= LPIT_TMR_TCTRL_T_EN_MASK;
  199. }
  200. else
  201. {
  202. LPitGptBase[instance]->TMR[channel].TCTRL &= (~(LPIT_TMR_TCTRL_T_EN_MASK));
  203. }
  204. SchM_Exit_Gpt_GPT_EXCLUSIVE_AREA_51();
  205. }
  206. #if (LPIT_GPT_IP_ENABLE_EXT_TRIGGERS == STD_ON)
  207. /**
  208. * @brief
  209. * Function Name : Lpit_Gpt_Ip_SetTrigger
  210. * Description : Set trigger (TCTRL)
  211. *
  212. * @param[in] instance LPit hardware instance number
  213. * @param[in] channel
  214. * @param[in] value
  215. *
  216. * @return void
  217. *
  218. */
  219. static inline void Lpit_Gpt_Ip_SetTrigger(uint8 instance, uint8 channel, uint32 value)
  220. {
  221. LPitGptBase[instance]-> TMR[channel].TCTRL = value;
  222. }
  223. #endif
  224. /**
  225. * @brief
  226. * Function Name : Lpit_Gpt_Ip_SetMode
  227. * Description : Configures the channel timer's mode of operation. The MODE bits control how the timer decrements
  228. * @param[in] : Set mode
  229. * - 00b - 32-bit Periodic Counter
  230. * - 01b - Dual 16-bit Periodic Counter
  231. * - 10b - 32-bit Trigger Accumulator
  232. * - 11b - 32-bit Trigger Input Capture
  233. * @param[in] instance LPit hardware instance number
  234. *
  235. * @return void
  236. *
  237. */
  238. static inline void Lpit_Gpt_Ip_SetMode(uint8 instance, uint8 channel, uint32 mode)
  239. {
  240. SchM_Enter_Gpt_GPT_EXCLUSIVE_AREA_52();
  241. LPitGptBase[instance]->TMR[channel].TCTRL = (LPitGptBase[instance]->TMR[channel].TCTRL & ~LPIT_TMR_TCTRL_MODE_MASK) | LPIT_TMR_TCTRL_MODE(mode);
  242. SchM_Exit_Gpt_GPT_EXCLUSIVE_AREA_52();
  243. }
  244. /**
  245. * @brief
  246. * Function Name : Lpit_Gpt_Ip_SetCmpValue
  247. * Description : Set the SetCmpValue
  248. *
  249. * @param[in] instance LPit hardware instance number
  250. * @param[in] value
  251. *
  252. * @return void
  253. *
  254. */
  255. static inline void Lpit_Gpt_Ip_SetCmpValue(uint8 instance, uint8 channel, uint32 value)
  256. {
  257. LPitGptBase[instance]->TMR[channel].TVAL = (uint32)(((uint32)value << LPIT_TMR_TVAL_TMR_VAL_SHIFT) & LPIT_TMR_TVAL_TMR_VAL_MASK);
  258. }
  259. /**
  260. * @brief Lpit_Ip_SetDebugMode
  261. * @details This register enables or disables the LPIT timer clocks and controls the timers
  262. * when the LPIT enters the Debug mode. (DBG_EN)
  263. *
  264. * Stops the timer channels when the device enters Debug mode
  265. * - 0b - Stop timer channels in Debug mode
  266. * - 1b - Allow timer channels to continue to run in Debug mode
  267. *
  268. * @param[in] instance LPIT hw instance number
  269. * @param[in] stopRun (TRUE/FALSE)
  270. *
  271. *
  272. * @return void
  273. * @pre The driver needs to be initialized.Enable/disable debug mode.
  274. */
  275. static inline void Lpit_Gpt_Ip_SetDebugMode(uint8 instance, boolean stopRun)
  276. {
  277. SchM_Enter_Gpt_GPT_EXCLUSIVE_AREA_53();
  278. if (TRUE == stopRun)
  279. {
  280. LPitGptBase[instance]->MCR |= LPIT_MCR_DBG_EN_MASK;
  281. }
  282. else
  283. {
  284. LPitGptBase[instance]->MCR &= ~LPIT_MCR_DBG_EN_MASK;
  285. }
  286. SchM_Exit_Gpt_GPT_EXCLUSIVE_AREA_53();
  287. }
  288. /**
  289. * @brief Lpit_Ip_SetDozeMode
  290. * @details This register enables or disables Doze mode
  291. * when the LPIT enters the Doze mode. (DOZE_EN)
  292. *
  293. * Stops the timer channels when the device enters DOZE mode
  294. * - 0b - Stop timer channels in DOZE mode
  295. * - 1b - Allow timer channels to continue to run in DOZE mode
  296. *
  297. * @param[in] instance LPIT hw instance number
  298. * @param[in] running
  299. *
  300. * @return void
  301. * @pre The driver needs to be initialized.Enable/disable Doze mode.
  302. */
  303. static inline void Lpit_Gpt_Ip_SetDozeMode(uint8 instance, boolean running)
  304. {
  305. SchM_Enter_Gpt_GPT_EXCLUSIVE_AREA_54();
  306. if (TRUE == running)
  307. {
  308. LPitGptBase[instance]->MCR |= LPIT_MCR_DOZE_EN_MASK;
  309. }
  310. else
  311. {
  312. LPitGptBase[instance]->MCR &= ~LPIT_MCR_DOZE_EN_MASK;
  313. }
  314. SchM_Exit_Gpt_GPT_EXCLUSIVE_AREA_54();
  315. }
  316. /**
  317. * @brief Lpit_Gpt_Ip_InterruptTimerChannels
  318. *
  319. * @details This function allows enabling/disabling interrupt generation for timer channels simultaneously.
  320. *
  321. * @param[in] base LPIT peripheral base address
  322. * @param[in] channel LPIT channel
  323. * @param[in] enable The interrupt enabling/disabling channel that decides which channels will
  324. * be disabled interrupt.
  325. *
  326. * @return void
  327. * @pre The driver needs to be initialized.Enable/disable Doze mode.
  328. */
  329. static inline void Lpit_Gpt_Ip_InterruptTimerChannels(uint8 instance, uint8 channel, boolean enable)
  330. {
  331. SchM_Enter_Gpt_GPT_EXCLUSIVE_AREA_55();
  332. if (TRUE == enable)
  333. {
  334. LPitGptBase[instance]->MIER |= (uint32)(LPIT_MIER_TIE_GPT_MASK << (uint32)channel);
  335. }
  336. else
  337. {
  338. LPitGptBase[instance]->MIER &= ~(uint32)(LPIT_MIER_TIE_GPT_MASK << (uint32)channel);
  339. }
  340. SchM_Exit_Gpt_GPT_EXCLUSIVE_AREA_55();
  341. }
  342. /**
  343. * @brief Lpit_Gpt_Ip_ClearInterruptFlagTimerChannels
  344. *
  345. * @details This function clears current interrupt flag of timer channels.
  346. *
  347. * @param[in] instance LPIT peripheral base address
  348. * @param[in] channel The interrupt flag clearing mask that decides which channels will
  349. * be cleared interrupt flag.
  350. *
  351. * - To clear a channel timer interrupt flag, write logic 1 to it
  352. */
  353. static inline void Lpit_Gpt_Ip_ClearInterruptFlagTimerChannels(uint8 instance, uint8 channel)
  354. {
  355. /* Write 1 to clear the interrupt flag. */
  356. LPitGptBase[instance]->MSR = ((uint32)(LPIT_MSR_TIF_GPT_MASK << (uint32)channel));
  357. }
  358. #if (LPIT_GPT_IP_CHAIN_MODE == STD_ON)
  359. /**
  360. * @brief Lpit_Gpt_Ip_SetChainMode
  361. * @details Support chain mode (CHAIN)
  362. * This register is intended for Timer Control
  363. *
  364. * Chain Channel
  365. * When enabled, the timer channel will decrement when timer channel N-1 trigger asserts. Timer channel 0
  366. * cannot be chained.
  367. * - 0b - Channel Chaining is disabled. The channel timer runs independently.
  368. * - 1b - Channel Chaining is enabled. The timer decrements on the previous channel's timeout.
  369. *
  370. * @param[in] instance LPIT hw instance number
  371. * @param[in] channel LPIT hw instance number
  372. * @param[in] enable TRUE/FALSE
  373. *
  374. * @return void
  375. * @pre The driver needs to be initialized.Enable/disable chain mode.
  376. */
  377. static inline void Lpit_Gpt_Ip_SetChainMode(uint8 instance, uint8 channel, boolean enable)
  378. {
  379. SchM_Enter_Gpt_GPT_EXCLUSIVE_AREA_56();
  380. if ((TRUE == enable) && (channel > 0U))
  381. {
  382. LPitGptBase[instance]->TMR[channel].TCTRL |= LPIT_TMR_TCTRL_CHAIN_MASK;
  383. }
  384. else
  385. {
  386. LPitGptBase[instance]->TMR[channel].TCTRL &= ~LPIT_TMR_TCTRL_CHAIN_MASK;
  387. }
  388. SchM_Exit_Gpt_GPT_EXCLUSIVE_AREA_56();
  389. }
  390. #endif
  391. /**
  392. * @brief Lpit_Gpt_Ip_GetInterruptFlagTimerChannels
  393. * @details Support LPIT interrupt flags
  394. * This register is intended for Timer Interrupt Flag
  395. *
  396. * @param[in] instance LPIT hw instance number
  397. * @param[in] channel LPIT hw channel number
  398. * @return returnValue
  399. * @pre The driver needs to be initialized.
  400. */
  401. uint32 Lpit_Gpt_Ip_GetInterruptFlagTimerChannels(uint8 instance, uint8 channel)
  402. {
  403. volatile uint32 returnValue = 0U;
  404. returnValue = ((LPitGptBase[instance]->MSR & ((uint32)(LPIT_MSR_TIF_GPT_MASK) << (uint32)channel)) >> (uint32)channel);
  405. return returnValue;
  406. }
  407. /**
  408. * @brief Lpit_Gpt_Ip_GetTimerInterruptEnable
  409. * @details Support LPIT Timer Interrupt Enable
  410. * This register is intended for Timer Interrupt Enable
  411. *
  412. * @param[in] instance LPIT hw instance number
  413. * @param[in] channel LPIT hw channel number
  414. * @return returnValue
  415. * @pre The driver needs to be initialized.
  416. */
  417. uint32 Lpit_Gpt_Ip_GetTimerInterruptEnable(uint8 instance, uint8 channel)
  418. {
  419. uint32 returnValue = 0U;
  420. returnValue = ((LPitGptBase[instance]->MIER & ((uint32)(LPIT_MIER_TIE_GPT_MASK) << (uint32)channel)) >> (uint32)channel);
  421. return returnValue;
  422. }
  423. /**
  424. * @brief Lpit_Gpt_Ip_GetCurrentTimervalue - Gets the current timer channel counting value.
  425. *
  426. * @details This function returns the real-time timer channel counting value, the value in
  427. * a range from 0 to timer channel period.
  428. * Need to make sure the running time does not exceed the timer channel period.
  429. *
  430. * @param[in] instance LPIT peripheral base address
  431. * @param[in] channel Timer channel number
  432. * @return Current timer channel counting value
  433. */
  434. uint32 Lpit_Gpt_Ip_GetCurrentTimervalue(uint8 instance, uint8 channel)
  435. {
  436. return (LPitGptBase[instance]->TMR[channel].CVAL);
  437. }
  438. /**
  439. * @brief Lpit_Gpt_Ip_GetTimerValue- Gets the timer channel period in count unit.
  440. *
  441. * @details This function returns current period of timer channel given as argument.
  442. *
  443. * @param[in] instance LPIT peripheral base address
  444. * @param[in] channel Timer channel number
  445. * @return Timer channel period in count unit
  446. */
  447. uint32 Lpit_Gpt_Ip_GetTimerValue(uint8 instance, uint8 channel)
  448. {
  449. return (LPitGptBase[instance]->TMR[channel].TVAL);
  450. }
  451. /*==================================================================================================
  452. * GLOBAL FUNCTIONS
  453. ==================================================================================================*/
  454. #define GPT_START_SEC_CODE
  455. #include "Gpt_MemMap.h"
  456. /**
  457. * @brief Function Name : Lpit_Gpt_Ip_Init
  458. * @details Driver initialization function for LPit instance.
  459. *
  460. * - enables the LPIT module
  461. * - configures the freeze mode (enabled or disabled)
  462. * - configures the DOZE mode (enabled or disabled)
  463. *
  464. * @param[in] instance LPIT hw instance number
  465. * @param[in] config Pointer to a selected configuration structure
  466. * @return void
  467. * @pre The data structure including the configuration set required for initializing the driver
  468. * @implements Lpit_Gpt_Ip_Init_Activity
  469. */
  470. void Lpit_Gpt_Ip_Init(uint8 instance, const Lpit_Gpt_Ip_InstanceConfigType *config)
  471. {
  472. #if LPIT_GPT_IP_DEV_ERROR_DETECT == STD_ON
  473. DevAssert(LPIT_INSTANCE_COUNT > instance);
  474. DevAssert(NULL_PTR != config);
  475. #endif
  476. /* Enable hardware module clock. This shall be be executed before any other setup is made */
  477. Lpit_Gpt_Ip_EnableMdlClk(instance, TRUE);
  478. /* Sets LPIT operation in Debug mode*/
  479. Lpit_Gpt_Ip_SetDebugMode(instance, config->stopRunInDebug);
  480. /* Enable LPIT in DOZE modes */
  481. Lpit_Gpt_Ip_SetDozeMode(instance, config->runInDozeMode);
  482. }
  483. /*================================================================================================*/
  484. /**
  485. * @brief Function Name : Lpit_Gpt_Ip_InitChannel
  486. * @details Initializes the LPIT channels. This functions is called for each LPIT hw channel.
  487. *
  488. * - disables the IRQ correpsonding to the LPIT channel
  489. * - clears the (pending) interrupt flag corresponding to LPit channel
  490. * - disables the LPIT timer channel
  491. * - clears the Load Value register correponding to the LPit channel.
  492. *
  493. *
  494. * @param[in] instance LPIT hw instance number
  495. * @param[in] configChannel Pointer to a selected configuration structure.
  496. * @return void
  497. * @pre The data structure including the configuration set required for initializing the driver.
  498. * @implements Lpit_Gpt_Ip_InitChannel_Activity
  499. */
  500. void Lpit_Gpt_Ip_InitChannel(uint8 instance, const Lpit_Gpt_Ip_ChannelConfigType *configChannel)
  501. {
  502. #if LPIT_GPT_IP_DEV_ERROR_DETECT == STD_ON
  503. DevAssert(LPIT_INSTANCE_COUNT > instance);
  504. DevAssert(NULL_PTR != configChannel);
  505. DevAssert(LPIT_TMR_COUNT > configChannel->hwChannel);
  506. #endif
  507. /* Enable hardware module clock. This shall be be executed before any other setup is made */
  508. Lpit_Gpt_Ip_EnableMdlClk(instance, TRUE);
  509. /* Stop Channel to Configure Channel */
  510. Lpit_Gpt_Ip_TimerEnable(instance, configChannel->hwChannel, FALSE);
  511. /* Config LPIT Mode as periodic counters */
  512. Lpit_Gpt_Ip_SetMode(instance, configChannel->hwChannel, (uint32)0U);
  513. /* Set Compare Value to 0 */
  514. Lpit_Gpt_Ip_SetCmpValue(instance, configChannel->hwChannel, 0xFFFFFFFFU);
  515. #if (LPIT_GPT_IP_ENABLE_EXT_TRIGGERS == STD_ON)
  516. /* Configures Trigger */
  517. Lpit_Gpt_Ip_SetTrigger(instance, configChannel->hwChannel,configChannel->triggerConfig);
  518. #endif
  519. /* Disable interrupts*/
  520. Lpit_Gpt_Ip_InterruptTimerChannels(instance, (uint8)(configChannel->hwChannel), FALSE);
  521. /* Clear pending interrupts */
  522. Lpit_Gpt_Ip_ClearInterruptFlagTimerChannels(instance, (uint8)(configChannel->hwChannel));
  523. #if ((defined LPIT_0_ISR_USED)||(defined LPIT_0_CH_0_ISR_USED)||(defined LPIT_0_CH_1_ISR_USED)||(defined LPIT_0_CH_2_ISR_USED)||(defined LPIT_0_CH_3_ISR_USED))
  524. Lpit_Gpt_Ip_u32ChState[instance][configChannel->hwChannel].chInit = TRUE;
  525. Lpit_Gpt_Ip_u32ChState[instance][configChannel->hwChannel].callback = configChannel->callback;
  526. Lpit_Gpt_Ip_u32ChState[instance][configChannel->hwChannel].callbackParam = configChannel->callbackParam;
  527. #endif
  528. }
  529. /*================================================================================================*/
  530. /**
  531. * @brief Function Name: Lpit_Gpt_Ip_Deinit
  532. * @details De-Initializes the LPIT instances. This functions is called and
  533. *
  534. *
  535. * @param[in] instance LPit hw instance
  536. * @return void
  537. * @pre The data structure including the configuration set required for initializing the GPT driver.
  538. *
  539. * @implements Lpit_Gpt_Ip_Deinit_Activity
  540. */
  541. void Lpit_Gpt_Ip_Deinit(uint8 instance)
  542. {
  543. #if LPIT_GPT_IP_DEV_ERROR_DETECT == STD_ON
  544. DevAssert(LPIT_INSTANCE_COUNT > instance);
  545. #endif
  546. uint8 channelIndex;
  547. /* Enable hardware module clock. This shall be be executed before any other setup is made */
  548. Lpit_Gpt_Ip_EnableMdlClk(instance, TRUE);
  549. /*Reset all channels to default*/
  550. for (channelIndex = 0; channelIndex < LPIT_TMR_COUNT; channelIndex++)
  551. {
  552. /* Reset Status and Control Register */
  553. Lpit_Gpt_Ip_TimerEnable(instance, channelIndex, FALSE);
  554. /* Config LPIT Mode as periodic counters */
  555. Lpit_Gpt_Ip_SetMode(instance, channelIndex, (uint32)0U);
  556. /* Disable channel interrupts*/
  557. Lpit_Gpt_Ip_InterruptTimerChannels(instance, channelIndex, FALSE);
  558. /* Clear Interrupt Flag */
  559. Lpit_Gpt_Ip_ClearInterruptFlagTimerChannels(instance, channelIndex);
  560. }
  561. /* Sets LPIT operation in Debug mode*/
  562. Lpit_Gpt_Ip_SetDebugMode(instance, FALSE);
  563. /* Enable LPIT in DOZE modes */
  564. Lpit_Gpt_Ip_SetDozeMode(instance, FALSE);
  565. /* Disable hardware module clock */
  566. Lpit_Gpt_Ip_EnableMdlClk(instance, FALSE);
  567. }
  568. /*================================================================================================*/
  569. /**
  570. * @brief Function Name : Lpit_Gpt_Ip_StartTimer
  571. * @details This function is called for starting the LPit timer channel.
  572. * - sets the timeout value into the LPIT timer channel register
  573. * - enables the LPIT channel
  574. *
  575. * @param[in] instance LPit hw instance
  576. * @param[in] channel LPit hw channel
  577. * @param[in] countValue channel timeout value
  578. * @return void
  579. * @pre The driver needs to be initialized. This function is called for starting the Pit timer channel.
  580. * @implements Lpit_Gpt_Ip_StartTimer_Activity
  581. */
  582. void Lpit_Gpt_Ip_StartTimer(uint8 instance, uint8 channel, uint32 countValue)
  583. {
  584. #if LPIT_GPT_IP_DEV_ERROR_DETECT == STD_ON
  585. DevAssert(LPIT_INSTANCE_COUNT > instance);
  586. DevAssert(LPIT_TMR_COUNT > channel);
  587. DevAssert(LPIT_MAX_VALUE > countValue);
  588. #endif
  589. /* Lpit timer disable */
  590. Lpit_Gpt_Ip_TimerEnable(instance, channel, FALSE);
  591. /* Sets the timer channel period in count unit */
  592. Lpit_Gpt_Ip_SetCmpValue(instance, channel, countValue);
  593. /* Lpit timer enable */
  594. Lpit_Gpt_Ip_TimerEnable(instance, channel, TRUE);
  595. }
  596. /*================================================================================================*/
  597. /**
  598. * @brief Function Name : Lpit_Gpt_Ip_StopTimer
  599. * @details This function is called for stopping the LPit counter. This function disables the LPIT channel
  600. *
  601. *
  602. * @param[in] instance LPit hw instance
  603. * @param[in] channel LPit hw channel
  604. * @return void
  605. * @pre The driver needs to be initialized. Lpit_Gpt_Ip_StartTimer must be call before.
  606. * @implements Lpit_Gpt_Ip_StopTimer_Activity
  607. */
  608. void Lpit_Gpt_Ip_StopTimer(uint8 instance, uint8 channel)
  609. {
  610. #if LPIT_GPT_IP_DEV_ERROR_DETECT == STD_ON
  611. DevAssert(LPIT_INSTANCE_COUNT > instance);
  612. DevAssert(LPIT_TMR_COUNT > channel);
  613. #endif
  614. /* Lpit timer disable */
  615. Lpit_Gpt_Ip_TimerEnable(instance, channel, FALSE);
  616. /* Clear interrupt flag */
  617. Lpit_Gpt_Ip_ClearInterruptFlagTimerChannels(instance, channel);
  618. }
  619. /*================================================================================================*/
  620. /**
  621. * @brief Function Name : Lpit_Gpt_Ip_EnableChInterrupt
  622. * @details This function allows enabling interrupt generation of timer channel
  623. * when timeout occurs
  624. *
  625. * @param[in] instance LPit hw instance
  626. * @param[in] channel LPit hw channel
  627. * @return void
  628. * @pre The driver needs to be initialized.
  629. * @implements Lpit_Gpt_Ip_EnableChInterrupt_Activity
  630. */
  631. void Lpit_Gpt_Ip_EnableChInterrupt(uint8 instance, uint8 channel)
  632. {
  633. #if LPIT_GPT_IP_DEV_ERROR_DETECT == STD_ON
  634. DevAssert(LPIT_INSTANCE_COUNT > instance);
  635. DevAssert(LPIT_TMR_COUNT > channel);
  636. #endif
  637. /* Clear interrupt flag */
  638. Lpit_Gpt_Ip_ClearInterruptFlagTimerChannels(instance, channel);
  639. /* Enable interrupt */
  640. Lpit_Gpt_Ip_InterruptTimerChannels(instance, channel, TRUE);
  641. }
  642. /*================================================================================================*/
  643. /**
  644. * @brief Function Name : Lpit_Gpt_Ip_DisableChInterrupt
  645. * @details This function allows disabling interrupt of a timer channel
  646. *
  647. * @param[in] instance LPit hw instance
  648. * @param[in] channel LPit hw channel
  649. * @return void
  650. * @pre The driver needs to be initialized.
  651. * @implements Lpit_Gpt_Ip_DisableChInterrupt_Activity
  652. */
  653. void Lpit_Gpt_Ip_DisableChInterrupt(uint8 instance, uint8 channel)
  654. {
  655. #if LPIT_GPT_IP_DEV_ERROR_DETECT == STD_ON
  656. DevAssert(LPIT_INSTANCE_COUNT > instance);
  657. DevAssert(LPIT_TMR_COUNT > channel);
  658. #endif
  659. /* Clear Lpit interrupt flag */
  660. Lpit_Gpt_Ip_ClearInterruptFlagTimerChannels(instance, channel);
  661. /* Disable Lpit interrupt */
  662. Lpit_Gpt_Ip_InterruptTimerChannels(instance, channel, FALSE);
  663. }
  664. /*================================================================================================*/
  665. #if (LPIT_GPT_IP_CHAIN_MODE == STD_ON)
  666. /**
  667. * @brief Function Name : Lpit_Gpt_Ip_ChainMode.
  668. * @details This function:
  669. * - Chain/Unchain LPit channels.
  670. * @param[in] instance LPit hw channel ID
  671. * @param[in] channel channel timeout value
  672. * @param[in] enable enable/disable chain mode
  673. * @return returnValue
  674. * @pre The driver needs to be initialized. LPIT_GPT_IP_CHAIN_MODE == STD_ON
  675. * @implements Lpit_Gpt_Ip_ChainMode_Activity
  676. */
  677. Lpit_Gpt_Ip_StatusType Lpit_Gpt_Ip_ChainMode(uint8 instance, uint8 channel, boolean enable)
  678. {
  679. #if LPIT_GPT_IP_DEV_ERROR_DETECT == STD_ON
  680. DevAssert(LPIT_INSTANCE_COUNT > instance);
  681. DevAssert(LPIT_TMR_COUNT > channel);
  682. #endif
  683. Lpit_Gpt_Ip_StatusType returnValue = LPIT_GPT_IP_ERROR;
  684. /* Can not enable chain mode for LPIT_0_CH_0 */
  685. if((uint8) 0x00 < channel)
  686. {
  687. /* Enable Chain Mode*/
  688. Lpit_Gpt_Ip_SetChainMode(instance, channel, enable);
  689. returnValue = LPIT_GPT_IP_SUCCESS;
  690. }
  691. return returnValue;
  692. }
  693. #endif /*(LPIT_GPT_IP_CHAIN_MODE== STD_ON)*/
  694. /*================================================================================================*/
  695. #if ((defined LPIT_0_ISR_USED)||(defined LPIT_0_CH_0_ISR_USED)||(defined LPIT_0_CH_1_ISR_USED)||(defined LPIT_0_CH_2_ISR_USED)||(defined LPIT_0_CH_3_ISR_USED))
  696. /**
  697. * @brief Driver routine to process all the interrupts of LPIT.
  698. * @details Support function used by interrupt service routines to implement LPIT specific operations
  699. * and call the upper layer handler to implement non-hardware specific operations.
  700. *
  701. * @param[in] instance LPIT hw instance number
  702. * @param[in] channel LPIT hw channel number
  703. * @return void
  704. * @pre Enable interrupt service routines
  705. * @implements Lpit_Gpt_Ip_ProcessCommonInterrupt_Activity
  706. */
  707. static void Lpit_Gpt_Ip_ProcessCommonInterrupt(uint8 instance, uint8 channel)
  708. {
  709. uint32 flag;
  710. uint32 interruptEnabled;
  711. /*Checks for spurious interrupts*/
  712. flag = (uint32)(Lpit_Gpt_Ip_GetInterruptFlagTimerChannels(instance, channel));
  713. interruptEnabled = (uint32)(Lpit_Gpt_Ip_GetTimerInterruptEnable(instance, channel));
  714. if ((1U == flag) && (1U == interruptEnabled))
  715. {
  716. /* Call GPT upper layer handler */
  717. if((TRUE == Lpit_Gpt_Ip_u32ChState[instance][channel].chInit) && \
  718. (NULL_PTR != Lpit_Gpt_Ip_u32ChState[instance][channel].callback))
  719. {
  720. Lpit_Gpt_Ip_u32ChState[instance][channel].callback(Lpit_Gpt_Ip_u32ChState[instance][channel].callbackParam);
  721. }
  722. }
  723. /*Clear interrupt flag*/
  724. Lpit_Gpt_Ip_ClearInterruptFlagTimerChannels(instance, channel);
  725. }
  726. #endif
  727. /*================================================================================================*/
  728. #if (defined (LPIT_GPT_IP_MODULE_SINGLE_INTERRUPT) && (STD_ON == LPIT_GPT_IP_MODULE_SINGLE_INTERRUPT))
  729. #ifdef LPIT_0_ISR_USED
  730. /**
  731. * @brief Interrupt handler for LPIT_0_CH_0 channels.
  732. * @details Interrupt Service Routine corresponding to LPIT_0_CH_0 hw module.
  733. * @param[in] none
  734. * @return void
  735. * @isr
  736. * @pre The driver needs to be initialized
  737. */
  738. ISR(LPIT_0_ISR)
  739. {
  740. uint8 channel = 0U;
  741. for (channel = 0U; channel < LPIT_TMR_COUNT; channel++)
  742. {
  743. Lpit_Gpt_Ip_ProcessCommonInterrupt(0U, channel);
  744. }
  745. EXIT_INTERRUPT();
  746. }
  747. #endif
  748. #else
  749. #ifdef LPIT_0_CH_0_ISR_USED
  750. /**
  751. * @brief Interrupt handler for LPIT_0_CH_0 channels.
  752. * @details Interrupt Service Routine corresponding to LPIT_0_CH_0 hw module.
  753. * @param[in] none
  754. * @return void
  755. * @isr
  756. * @pre The driver needs to be initialized
  757. */
  758. ISR(LPIT_0_CH_0_ISR)
  759. {
  760. Lpit_Gpt_Ip_ProcessCommonInterrupt(0U, 0U);
  761. EXIT_INTERRUPT();
  762. }
  763. #endif
  764. /*================================================================================================*/
  765. #ifdef LPIT_0_CH_1_ISR_USED
  766. /**
  767. * @brief Interrupt handler for LPIT_0_CH_1 channels.
  768. * @details Interrupt Service Routine corresponding to LPIT_0_CH_1 hw module.
  769. * @param[in] none
  770. * @return void
  771. * @isr
  772. * @pre The driver needs to be initialized
  773. */
  774. ISR(LPIT_0_CH_1_ISR)
  775. {
  776. Lpit_Gpt_Ip_ProcessCommonInterrupt(0U, 1U);
  777. EXIT_INTERRUPT();
  778. }
  779. #endif
  780. /*================================================================================================*/
  781. #ifdef LPIT_0_CH_2_ISR_USED
  782. /**
  783. * @brief Interrupt handler for LPIT_0_CH_0 channels.
  784. * @details Interrupt Service Routine corresponding to LPIT_0_CH_0 hw module.
  785. * @param[in] none
  786. * @return void
  787. * @isr
  788. * @pre The driver needs to be initialized
  789. */
  790. ISR(LPIT_0_CH_2_ISR)
  791. {
  792. Lpit_Gpt_Ip_ProcessCommonInterrupt(0U, 2U);
  793. EXIT_INTERRUPT();
  794. }
  795. #endif
  796. /*================================================================================================*/
  797. #ifdef LPIT_0_CH_3_ISR_USED
  798. /**
  799. * @brief Interrupt handler for LPIT_0_CH_0 channels.
  800. * @details Interrupt Service Routine corresponding to LPIT_0_CH_0 hw module.
  801. * @param[in] none
  802. * @return void
  803. * @isr
  804. * @pre The driver needs to be initialized
  805. */
  806. ISR(LPIT_0_CH_3_ISR)
  807. {
  808. Lpit_Gpt_Ip_ProcessCommonInterrupt(0U, 3U);
  809. EXIT_INTERRUPT();
  810. }
  811. #endif
  812. #endif /*LPIT_GPT_IP_MODULE_SINGLE_INTERRUPT*/
  813. /*================================================================================================*/
  814. #define GPT_STOP_SEC_CODE
  815. #include "Gpt_MemMap.h"
  816. #endif /* LPIT_GPT_IP_USED == STD_ON */
  817. #ifdef __cplusplus
  818. }
  819. #endif /* LPIT_GPT_IP_C */
  820. /** @} */