Ram_Ip.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*==================================================================================================
  2. * Project : RTD AUTOSAR 4.4
  3. * Platform : CORTEXM
  4. * Peripheral :
  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 Ram_Ip.c
  26. * @version 1.0.0
  27. *
  28. * @brief RAM driver implementations.
  29. * @details RAM driver implementations.
  30. *
  31. * @addtogroup RAM_DRIVER Ram Ip Driver
  32. * @{
  33. */
  34. #ifdef __cplusplus
  35. extern "C"{
  36. #endif
  37. /*==================================================================================================
  38. * INCLUDE FILES
  39. * 1) system and project includes
  40. * 2) needed interfaces from external units
  41. * 3) internal and external interfaces from this unit
  42. ==================================================================================================*/
  43. #include "Ram_Ip.h"
  44. #include "OsIf.h"
  45. #if defined(S32K3XX)
  46. /* Todo remove it */
  47. #include "S32K344_MC_ME.h"
  48. #if (defined(RAM_IP_ENABLE_USER_MODE_SUPPORT) && (STD_ON == RAM_IP_ENABLE_USER_MODE_SUPPORT))
  49. #if (defined(MCAL_MC_ME_REG_PROT_AVAILABLE))
  50. #if (STD_ON == MCAL_MC_ME_REG_PROT_AVAILABLE)
  51. #define USER_MODE_REG_PROT_ENABLED (STD_ON)
  52. #include "RegLockMacros.h"
  53. #endif /* (STD_ON == MCAL_MC_ME_REG_PROT_AVAILABLE) */
  54. #endif
  55. #endif /* (STD_ON == RAM_IP_ENABLE_USER_MODE_SUPPORT) */
  56. #endif
  57. /*==================================================================================================
  58. SOURCE FILE VERSION INFORMATION
  59. ==================================================================================================*/
  60. #define RAM_IP_VENDOR_ID_C 43
  61. #define RAM_IP_AR_RELEASE_MAJOR_VERSION_C 4
  62. #define RAM_IP_AR_RELEASE_MINOR_VERSION_C 4
  63. #define RAM_IP_AR_RELEASE_REVISION_VERSION_C 0
  64. #define RAM_IP_SW_MAJOR_VERSION_C 1
  65. #define RAM_IP_SW_MINOR_VERSION_C 0
  66. #define RAM_IP_SW_PATCH_VERSION_C 0
  67. /*==================================================================================================
  68. * FILE VERSION CHECKS
  69. ==================================================================================================*/
  70. /* Check if Ram_Ip.c file and Ram_Ip.h file are of the same vendor */
  71. #if (RAM_IP_VENDOR_ID_C != RAM_IP_VENDOR_ID)
  72. #error "Ram_Ip.c and Ram_Ip.h have different vendor ids"
  73. #endif
  74. /* Check if Ram_Ip.c file and Ram_Ip.h file are of the same Autosar version */
  75. #if ((RAM_IP_AR_RELEASE_MAJOR_VERSION_C != RAM_IP_AR_RELEASE_MAJOR_VERSION) || \
  76. (RAM_IP_AR_RELEASE_MINOR_VERSION_C != RAM_IP_AR_RELEASE_MINOR_VERSION) || \
  77. (RAM_IP_AR_RELEASE_REVISION_VERSION_C != RAM_IP_AR_RELEASE_REVISION_VERSION) \
  78. )
  79. #error "AutoSar Version Numbers of Ram_Ip.c and Ram_Ip.h are different"
  80. #endif
  81. /* Check if Ram_Ip.c file and Ram_Ip.h file are of the same Software version */
  82. #if ((RAM_IP_SW_MAJOR_VERSION_C != RAM_IP_SW_MAJOR_VERSION) || \
  83. (RAM_IP_SW_MINOR_VERSION_C != RAM_IP_SW_MINOR_VERSION) || \
  84. (RAM_IP_SW_PATCH_VERSION_C != RAM_IP_SW_PATCH_VERSION) \
  85. )
  86. #error "Software Version Numbers of Ram_Ip.c and Ram_Ip.h are different"
  87. #endif
  88. #ifndef DISABLE_MCAL_INTERMODULE_ASR_CHECK
  89. /* Check if Ram_Ip.c file and OsIf.h file are of the same Autosar version */
  90. #if ((RAM_IP_AR_RELEASE_MAJOR_VERSION_C != OSIF_AR_RELEASE_MAJOR_VERSION) || \
  91. (RAM_IP_AR_RELEASE_MINOR_VERSION_C != OSIF_AR_RELEASE_MINOR_VERSION))
  92. #error "AutoSar Version Numbers of Ram_Ip.c and OsIf.h are different"
  93. #endif
  94. #endif
  95. #if defined(S32K3XX)
  96. #if (defined(RAM_IP_ENABLE_USER_MODE_SUPPORT) && (STD_ON == RAM_IP_ENABLE_USER_MODE_SUPPORT))
  97. #if (defined(MCAL_MC_ME_REG_PROT_AVAILABLE))
  98. #if (STD_ON == MCAL_MC_ME_REG_PROT_AVAILABLE)
  99. #ifndef DISABLE_MCAL_INTERMODULE_ASR_CHECK
  100. /* Check if Ram_Ip_S32K3XX.c file and RegLockMacros.h file are of the same Autosar version */
  101. #if ((RAM_IP_AR_RELEASE_MAJOR_VERSION_C != REGLOCKMACROS_AR_RELEASE_MAJOR_VERSION) || \
  102. (RAM_IP_AR_RELEASE_MINOR_VERSION_C != REGLOCKMACROS_AR_RELEASE_MINOR_VERSION))
  103. #error "AutoSar Version Numbers of Ram_Ip_S32K3XX.c and RegLockMacros.h are different"
  104. #endif
  105. #endif
  106. #endif /* (STD_ON == MCAL_MC_ME_REG_PROT_AVAILABLE) */
  107. #endif
  108. #endif /* RAM_IP_ENABLE_USER_MODE_SUPPORT */
  109. #endif
  110. /*==================================================================================================
  111. LOCAL TYPEDEFS (STRUCTURES, UNIONS, ENUMS)
  112. ==================================================================================================*/
  113. /*==================================================================================================
  114. LOCAL FUNCTION PROTOTYPES
  115. ==================================================================================================*/
  116. #define MCU_START_SEC_CODE
  117. #include "Mcu_MemMap.h"
  118. #if (RAM_IP_GET_RAM_STATE_API == STD_ON)
  119. static void RamStartTimeout(uint32 *startTimeOut,
  120. uint32 *elapsedTimeOut,
  121. uint32 *timeoutTicksOut,
  122. uint32 timeoutUs);
  123. static boolean RamTimeoutExpired(uint32 *startTimeInOut,
  124. uint32 *elapsedTimeInOut,
  125. uint32 TimeoutTicks);
  126. static void ReportRamErrorsEmptyCallback(Ram_Ip_RamReportErrorType error, uint8 u8ErrorCode);
  127. static void ReportRamErrors(Ram_Ip_RamReportErrorType error, uint8 u8ErrorCode);
  128. #endif
  129. #define MCU_STOP_SEC_CODE
  130. #include "Mcu_MemMap.h"
  131. /*==================================================================================================
  132. LOCAL MACROS
  133. ==================================================================================================*/
  134. /*==================================================================================================
  135. LOCAL CONSTANTS
  136. ==================================================================================================*/
  137. /*==================================================================================================
  138. LOCAL VARIABLES
  139. ==================================================================================================*/
  140. #if (RAM_IP_GET_RAM_STATE_API == STD_ON)
  141. #define MCU_START_SEC_VAR_INIT_UNSPECIFIED
  142. #include "Mcu_MemMap.h"
  143. /* Ram Report Error Callback */
  144. static Ram_Ip_ReportErrorsCallbackType RamReportErrorsCallback = ReportRamErrorsEmptyCallback;
  145. #define MCU_STOP_SEC_VAR_INIT_UNSPECIFIED
  146. #include "Mcu_MemMap.h"
  147. #endif /* (RAM_IP_GET_RAM_STATE_API == STD_ON) */
  148. /*==================================================================================================
  149. GLOBAL CONSTANTS
  150. ==================================================================================================*/
  151. /*==================================================================================================
  152. GLOBAL VARIABLES
  153. ==================================================================================================*/
  154. /*==================================================================================================
  155. LOCAL FUNCTIONS
  156. ==================================================================================================*/
  157. #define MCU_START_SEC_CODE
  158. #include "Mcu_MemMap.h"
  159. #if (RAM_IP_GET_RAM_STATE_API == STD_ON)
  160. /*FUNCTION**********************************************************************
  161. *
  162. * Function Name : RamStartTimeout
  163. * Description : Checks for timeout condition
  164. *
  165. *END**************************************************************************/
  166. static void RamStartTimeout(uint32 *startTimeOut,
  167. uint32 *elapsedTimeOut,
  168. uint32 *timeoutTicksOut,
  169. uint32 timeoutUs)
  170. {
  171. *startTimeOut = OsIf_GetCounter(RAM_TIMEOUT_TYPE);
  172. *elapsedTimeOut = 0U;
  173. *timeoutTicksOut = OsIf_MicrosToTicks(timeoutUs, RAM_TIMEOUT_TYPE);
  174. }
  175. /*FUNCTION**********************************************************************
  176. *
  177. * Function Name : RamTimeoutExpired
  178. * Description : Checks for timeout expiration condition
  179. *
  180. *END**************************************************************************/
  181. static boolean RamTimeoutExpired(uint32 *startTimeInOut,
  182. uint32 *elapsedTimeInOut,
  183. uint32 TimeoutTicks)
  184. {
  185. *elapsedTimeInOut += OsIf_GetElapsed(startTimeInOut, RAM_TIMEOUT_TYPE);
  186. return ((*elapsedTimeInOut >= TimeoutTicks)? TRUE : FALSE);
  187. }
  188. #endif
  189. #if (RAM_IP_GET_RAM_STATE_API == STD_ON)
  190. static void ReportRamErrorsEmptyCallback(Ram_Ip_RamReportErrorType error, uint8 u8ErrorCode)
  191. {
  192. /* No implementation */
  193. (void)error;
  194. (void)u8ErrorCode;
  195. }
  196. static void ReportRamErrors(Ram_Ip_RamReportErrorType error, uint8 u8ErrorCode)
  197. {
  198. RamReportErrorsCallback(error, u8ErrorCode);
  199. }
  200. #endif
  201. /*==================================================================================================
  202. GLOBAL FUNCTIONS
  203. ==================================================================================================*/
  204. /**
  205. * @brief This function initializes a given RAM section of the MCU on the common (main) platform.
  206. * @details Function initializes the RAM section specified by the pointer to the "Mcu_RamConfigType" parameter.
  207. * The section's base address, size, value to be written, and write-at-once-size are provided by
  208. * the configuration structure.
  209. * The function will write the value specified in the configuration structure.
  210. * After the write it will read back the RAM to verify that the requested value was
  211. * written.
  212. * Called by:
  213. * - Mcu_InitRamSection() from HLD.
  214. *
  215. * @param[in] Mcu_pRamConfigPtr Pointer to RAM section configuration structure
  216. * (member of 'Mcu_ConfigType' struct).
  217. *
  218. * @return Status signaling if the given RAM section has or has not been successfully initialized.
  219. * @retval RAM_IP_STATUS_OK The RAM section check was successful.
  220. * @retval RAM_IP_STATUS_NOT_OK The RAM section check was not successful.
  221. *
  222. * @implements Ram_Ip_InitRamSection_Activity
  223. *
  224. */
  225. Ram_Ip_StatusType Ram_Ip_InitRamSection(const Ram_Ip_RamConfigType * Ram_Ip_pRamConfigPtr)
  226. {
  227. /* Result of the operation. */
  228. Ram_Ip_StatusType RamStatus = RAM_IP_STATUS_OK;
  229. /* Variable for indexing RAM sections. */
  230. Ram_Ip_RamIndexType RamCounter;
  231. /* Limit of RamCounter. */
  232. Ram_Ip_RamSizeType RamCounterLimit;
  233. RAM_DEV_ASSERT(NULL_PTR != Ram_Ip_pRamConfigPtr);
  234. RamCounterLimit = (Ram_Ip_RamSizeType)( ((Ram_Ip_RamSizeType)(Ram_Ip_pRamConfigPtr->RamSize)) / (Ram_Ip_pRamConfigPtr->RamWriteSize) );
  235. RamStatus = RAM_IP_STATUS_OK;
  236. RamCounter = (Ram_Ip_RamIndexType)0UL;
  237. while ((RamCounter < RamCounterLimit) && (RAM_IP_STATUS_OK == RamStatus))
  238. {
  239. switch (Ram_Ip_pRamConfigPtr->RamWriteSize)
  240. {
  241. case (Ram_Ip_RamWriteSizeType)1U:
  242. ( *((uint8 (*)[1U]) ((uint32) Ram_Ip_pRamConfigPtr->pu8RamBaseAddr)) )[RamCounter] = (uint8) Ram_Ip_pRamConfigPtr->u64RamDefaultValue;
  243. break;
  244. case (Ram_Ip_RamWriteSizeType)2U:
  245. ( *((uint16 (*)[1U]) ((uint32) Ram_Ip_pRamConfigPtr->pu8RamBaseAddr)) )[RamCounter] = (uint16) Ram_Ip_pRamConfigPtr->u64RamDefaultValue;
  246. break;
  247. case (Ram_Ip_RamWriteSizeType)4U:
  248. ( *((uint32 (*)[1U]) ((uint32) Ram_Ip_pRamConfigPtr->pu8RamBaseAddr)) )[RamCounter] = (uint32) Ram_Ip_pRamConfigPtr->u64RamDefaultValue;
  249. break;
  250. case (Ram_Ip_RamWriteSizeType)8U:
  251. ( *((uint64 (*)[1U]) ((uint32) Ram_Ip_pRamConfigPtr->pu8RamBaseAddr)) )[RamCounter] = (uint64) Ram_Ip_pRamConfigPtr->u64RamDefaultValue;
  252. break;
  253. default:
  254. RamStatus = RAM_IP_STATUS_NOT_OK;
  255. break;
  256. }
  257. RamCounter++;
  258. }
  259. if ( RAM_IP_STATUS_OK == RamStatus )
  260. {
  261. RamCounter = (Ram_Ip_RamIndexType)0UL;
  262. /* Check if RAM was initialized correctly. */
  263. while ((RamCounter < RamCounterLimit) && (RAM_IP_STATUS_OK == RamStatus))
  264. {
  265. switch (Ram_Ip_pRamConfigPtr->RamWriteSize)
  266. {
  267. case (Ram_Ip_RamWriteSizeType)1U:
  268. if ( (uint8) Ram_Ip_pRamConfigPtr->u64RamDefaultValue != ( *((uint8 (*)[1U]) ((uint32)Ram_Ip_pRamConfigPtr->pu8RamBaseAddr)) )[RamCounter] )
  269. {
  270. RamStatus = RAM_IP_STATUS_NOT_OK;
  271. }
  272. break;
  273. case (Ram_Ip_RamWriteSizeType)2U:
  274. if ( (uint16) Ram_Ip_pRamConfigPtr->u64RamDefaultValue != ( *((uint16 (*)[1U]) ((uint32)Ram_Ip_pRamConfigPtr->pu8RamBaseAddr)) )[RamCounter] )
  275. {
  276. RamStatus = RAM_IP_STATUS_NOT_OK;
  277. }
  278. break;
  279. case (Ram_Ip_RamWriteSizeType)4U:
  280. if ( (uint32) Ram_Ip_pRamConfigPtr->u64RamDefaultValue != ( *((uint32 (*)[1U]) ((uint32)Ram_Ip_pRamConfigPtr->pu8RamBaseAddr)) )[RamCounter] )
  281. {
  282. RamStatus = RAM_IP_STATUS_NOT_OK;
  283. }
  284. break;
  285. case (Ram_Ip_RamWriteSizeType)8U:
  286. if ( (uint64) Ram_Ip_pRamConfigPtr->u64RamDefaultValue != ( *((uint64 (*)[1U]) ((uint32)Ram_Ip_pRamConfigPtr->pu8RamBaseAddr)) )[RamCounter] )
  287. {
  288. RamStatus = RAM_IP_STATUS_NOT_OK;
  289. }
  290. break;
  291. default:
  292. RamStatus = RAM_IP_STATUS_NOT_OK;
  293. break;
  294. }
  295. RamCounter++;
  296. }
  297. }
  298. return RamStatus;
  299. }
  300. #if (RAM_IP_GET_RAM_STATE_API == STD_ON)
  301. /**
  302. * @brief This service provides the actual status of the microcontroller Ram. (if supported)
  303. * @details
  304. *
  305. * @param[in] void
  306. *
  307. * @return State of RAM
  308. *
  309. * @implements Ram_Ip_GetRamState_Activity
  310. *
  311. */
  312. Ram_Ip_RamStateType Ram_Ip_GetRamState(void)
  313. {
  314. Ram_Ip_RamStateType eRamState = RAM_IP_RAMSTATE_INVALID;
  315. uint32 u32TempReg;
  316. uint32 StartTime;
  317. uint32 ElapsedTime;
  318. uint32 TimeoutTicks;
  319. boolean TimeoutOccurred = FALSE;
  320. #if defined(S32K3XX)
  321. /* MC_ME SetUserAccessAllowed */
  322. #if (defined(RAM_IP_ENABLE_USER_MODE_SUPPORT) && (STD_ON == RAM_IP_ENABLE_USER_MODE_SUPPORT))
  323. #if (defined(MCAL_MC_ME_REG_PROT_AVAILABLE ))
  324. #if (STD_ON == MCAL_MC_ME_REG_PROT_AVAILABLE )
  325. #if (defined(IP_MC_ME_BASE))
  326. SET_USER_ACCESS_ALLOWED(IP_MC_ME_BASE, MC_ME_PROT_MEM_U32);
  327. #endif
  328. #endif
  329. #endif /* MCAL_MC_ME_REG_PROT_AVAILABLE */
  330. #endif /* (STD_ON == RAM_IP_ENABLE_USER_MODE_SUPPORT) */
  331. /* Check clock status for STCU2 */
  332. if (((IP_MC_ME->PRTN1_COFB3_STAT & MC_ME_PRTN1_COFB3_STAT_BLOCK104_MASK) == 0U))
  333. {
  334. /* Enable clock for STCU2 device */
  335. IP_MC_ME->PRTN1_COFB3_CLKEN |= MC_ME_PRTN1_COFB3_CLKEN_REQ104(1U); /* REQ104: the clock enable control for STCU2 */
  336. IP_MC_ME->PRTN1_PCONF |= MC_ME_PRTN1_PCONF_PCE_MASK; /* PCE=1: Enable the clock to Partition #1 */
  337. IP_MC_ME->PRTN1_PUPD |= MC_ME_PRTN1_PUPD_PCUD_MASK; /* PCUD=1: Trigger the hardware process */
  338. IP_MC_ME->CTL_KEY = 0x5AF0U; /* Enter key */
  339. IP_MC_ME->CTL_KEY = 0xA50FU;
  340. /* Wait until STCU2 clock is running */
  341. RamStartTimeout(&StartTime, &ElapsedTime, &TimeoutTicks, RAM_TIMEOUT_VALUE_US);
  342. do
  343. {
  344. TimeoutOccurred = RamTimeoutExpired(&StartTime, &ElapsedTime, TimeoutTicks);
  345. }
  346. while(((IP_MC_ME->PRTN1_COFB3_STAT & MC_ME_PRTN1_COFB3_STAT_BLOCK104_MASK) == 0U) && (FALSE == TimeoutOccurred));
  347. /* timeout notification */
  348. if (TRUE == TimeoutOccurred)
  349. {
  350. /* Report timeout error */
  351. ReportRamErrors(RAM_IP_REPORT_TIMEOUT_ERROR,RAM_IP_ERR_CODE_RESERVED);
  352. }
  353. }
  354. #endif
  355. /* Stay in this loop until these registers are automatically updated following the
  356. * completion of the MBIST run. */
  357. RamStartTimeout(&StartTime, &ElapsedTime, &TimeoutTicks, RAM_TIMEOUT_VALUE_US);
  358. do
  359. {
  360. TimeoutOccurred = RamTimeoutExpired(&StartTime, &ElapsedTime, TimeoutTicks);
  361. #if defined(S32K3XX)
  362. /* Todo remove it */
  363. #if 0
  364. /* Execute WFI */
  365. u32TempReg = IP_STCU->MBESW0;
  366. #else
  367. u32TempReg = 0U;
  368. #endif
  369. #else
  370. u32TempReg = IP_STCU2->MBESW[0];
  371. #endif
  372. } while ( (STCU2_MBESW0_RAM_TEST_MASK32 != (u32TempReg & STCU2_MBESW0_RAM_TEST_MASK32)) && (!TimeoutOccurred) );
  373. if (!TimeoutOccurred)
  374. {
  375. #if defined(S32K3XX)
  376. /* Todo remove it */
  377. #if 0
  378. u32TempReg = IP_STCU->MBSSW0;
  379. #else
  380. u32TempReg = 0U;
  381. #endif
  382. #else
  383. u32TempReg = IP_STCU2->MBSSW[0];
  384. #endif
  385. if (STCU2_MBSSW0_RAM_TEST_MASK32 == (u32TempReg & STCU2_MBSSW0_RAM_TEST_MASK32))
  386. {
  387. eRamState = RAM_IP_RAMSTATE_VALID;
  388. }
  389. }
  390. else
  391. {
  392. /* report callback here */
  393. ReportRamErrors(RAM_IP_REPORT_TIMEOUT_ERROR,RAM_IP_ERR_CODE_RESERVED);
  394. }
  395. return eRamState;
  396. }
  397. #endif
  398. #if (RAM_IP_GET_RAM_STATE_API == STD_ON)
  399. /**
  400. * @brief This function installs a callback for reporting errors from Ram driver.
  401. * @details
  402. *
  403. * @param[in] reportErrorsCallback Callback to be installed.
  404. *
  405. * @return void
  406. *
  407. * @implements Ram_Ip_InstallNotificationsCallback_Activity
  408. */
  409. void Ram_Ip_InstallNotificationsCallback(Ram_Ip_ReportErrorsCallbackType reportErrorsCallback)
  410. {
  411. RAM_DEV_ASSERT(NULL_PTR != reportErrorsCallback);
  412. RamReportErrorsCallback = reportErrorsCallback;
  413. }
  414. #endif
  415. #define MCU_STOP_SEC_CODE
  416. #include "Mcu_MemMap.h"
  417. #ifdef __cplusplus
  418. }
  419. #endif
  420. /** @} */