Dio_Ipw.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /*==================================================================================================
  2. * Project : RTD AUTOSAR 4.4
  3. * Platform : CORTEXM
  4. * Peripheral : GPIO
  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. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. /**
  28. * @file Dio_Ipw.c
  29. *
  30. * @internal
  31. * @addtogroup DIO_IPW
  32. * @{
  33. */
  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 "Dio_Cfg.h"
  41. #include "Dio_Ipw.h"
  42. #include "Gpio_Dio_Ip_Cfg.h"
  43. /*=================================================================================================
  44. SOURCE FILE VERSION INFORMATION
  45. =================================================================================================*/
  46. #define DIO_VENDOR_ID_IPW_C 43
  47. #define DIO_AR_RELEASE_MAJOR_VERSION_IPW_C 4
  48. #define DIO_AR_RELEASE_MINOR_VERSION_IPW_C 4
  49. #define DIO_AR_RELEASE_REVISION_VERSION_IPW_C 0
  50. #define DIO_SW_MAJOR_VERSION_IPW_C 1
  51. #define DIO_SW_MINOR_VERSION_IPW_C 0
  52. #define DIO_SW_PATCH_VERSION_IPW_C 0
  53. /*=================================================================================================
  54. * FILE VERSION CHECKS
  55. =================================================================================================*/
  56. /* Check if Dio_Ipw source file and Dio_Ipw header file are of the same vendor */
  57. #if (DIO_VENDOR_ID_IPW_C != DIO_VENDOR_ID_IPW_H)
  58. #error "Dio_Ipw.c and Dio_Ipw.h have different vendor ids"
  59. #endif
  60. /* Check if Dio_Ipw source file and Dio_Ipw header file are of the same Autosar version */
  61. #if ((DIO_AR_RELEASE_MAJOR_VERSION_IPW_C != DIO_AR_RELEASE_MAJOR_VERSION_IPW_H) || \
  62. (DIO_AR_RELEASE_MINOR_VERSION_IPW_C != DIO_AR_RELEASE_MINOR_VERSION_IPW_H) || \
  63. (DIO_AR_RELEASE_REVISION_VERSION_IPW_C != DIO_AR_RELEASE_REVISION_VERSION_IPW_H) \
  64. )
  65. #error "AutoSar Version Numbers of Dio_Ipw.c and Dio_Ipw.h are different"
  66. #endif
  67. /* Check if Dio_Ipw source file and Dio_Ipw header file are of the same Software version */
  68. #if ((DIO_SW_MAJOR_VERSION_IPW_C != DIO_SW_MAJOR_VERSION_IPW_H) || \
  69. (DIO_SW_MINOR_VERSION_IPW_C != DIO_SW_MINOR_VERSION_IPW_H) || \
  70. (DIO_SW_PATCH_VERSION_IPW_C != DIO_SW_PATCH_VERSION_IPW_H) \
  71. )
  72. #error "Software Version Numbers of Dio_Ipw.c and Dio_Ipw.h are different"
  73. #endif
  74. /* Check if Dio_Ipw source file and Dio_Cfg header file are of the same vendor */
  75. #if (DIO_VENDOR_ID_IPW_C != DIO_VENDOR_ID_CFG_H)
  76. #error "Dio_Ipw.c and Dio_Cfg.h have different vendor ids"
  77. #endif
  78. /* Check if Dio_Ipw source file and Dio_Cfg header file are of the same Autosar version */
  79. #if ((DIO_AR_RELEASE_MAJOR_VERSION_IPW_C != DIO_AR_RELEASE_MAJOR_VERSION_CFG_H) || \
  80. (DIO_AR_RELEASE_MINOR_VERSION_IPW_C != DIO_AR_RELEASE_MINOR_VERSION_CFG_H) || \
  81. (DIO_AR_RELEASE_REVISION_VERSION_IPW_C != DIO_AR_RELEASE_REVISION_VERSION_CFG_H) \
  82. )
  83. #error "AutoSar Version Numbers of Dio_Ipw.c and Dio_Cfg.h are different"
  84. #endif
  85. /* Check if Dio_Ipw source file and Dio_Cfg header file are of the same Software version */
  86. #if ((DIO_SW_MAJOR_VERSION_IPW_C != DIO_SW_MAJOR_VERSION_CFG_H) || \
  87. (DIO_SW_MINOR_VERSION_IPW_C != DIO_SW_MINOR_VERSION_CFG_H) || \
  88. (DIO_SW_PATCH_VERSION_IPW_C != DIO_SW_PATCH_VERSION_CFG_H) \
  89. )
  90. #error "Software Version Numbers of Dio_Ipw.c and Dio_Cfg.h are different"
  91. #endif
  92. /* Check if Dio_Ipw source file and Gpio_Dio_Ip_Cfg header file are of the same vendor */
  93. #if (DIO_VENDOR_ID_IPW_C != GPIO_DIO_IP_VENDOR_ID_CFG_H)
  94. #error "Dio_Ipw.c and Gpio_Dio_Ip_Cfg.h have different vendor ids"
  95. #endif
  96. /* Check if Dio_Ipw source file and Gpio_Dio_Ip_Cfg header file are of the same Autosar version */
  97. #if ((DIO_AR_RELEASE_MAJOR_VERSION_IPW_C != GPIO_DIO_IP_AR_RELEASE_MAJOR_VERSION_CFG_H) || \
  98. (DIO_AR_RELEASE_MINOR_VERSION_IPW_C != GPIO_DIO_IP_AR_RELEASE_MINOR_VERSION_CFG_H) || \
  99. (DIO_AR_RELEASE_REVISION_VERSION_IPW_C != GPIO_DIO_IP_AR_RELEASE_REVISION_VERSION_CFG_H) \
  100. )
  101. #error "AutoSar Version Numbers of Dio_Ipw.c and Gpio_Dio_Ip_Cfg.h are different"
  102. #endif
  103. /* Check if Dio_Ipw source file and Gpio_Dio_Ip_Cfg header file are of the same Software version */
  104. #if ((DIO_SW_MAJOR_VERSION_IPW_C != GPIO_DIO_IP_SW_MAJOR_VERSION_CFG_H) || \
  105. (DIO_SW_MINOR_VERSION_IPW_C != GPIO_DIO_IP_SW_MINOR_VERSION_CFG_H) || \
  106. (DIO_SW_PATCH_VERSION_IPW_C != GPIO_DIO_IP_SW_PATCH_VERSION_CFG_H) \
  107. )
  108. #error "Software Version Numbers of Dio_Ipw.c and Gpio_Dio_Ip_Cfg.h are different"
  109. #endif
  110. /*=================================================================================================
  111. * LOCAL TYPEDEFS (STRUCTURES, UNIONS, ENUMS)
  112. =================================================================================================*/
  113. /*=================================================================================================
  114. * LOCAL MACROS
  115. =================================================================================================*/
  116. #if (STD_ON == DIO_REVERSEPORTBITS)
  117. #define GPIO_DIO_REVERSE_BITS_SHIFT ((Dio_PortLevelType)1U)
  118. #define GPIO_DIO_REVERSE_BITS_MASK ((Dio_PortLevelType)0x1U)
  119. #define GPIO_DIO_REVERSE_BITS_NUMBER ((Dio_PortLevelType)((Dio_PortLevelType)((Dio_PortLevelType)(sizeof(Dio_PortLevelType)) << 0x3U) - 1U))
  120. #endif /* (STD_ON == DIO_REVERSEPORTBITS) */
  121. /*=================================================================================================
  122. * LOCAL CONSTANTS
  123. =================================================================================================*/
  124. /*=================================================================================================
  125. * LOCAL VARIABLES
  126. =================================================================================================*/
  127. /*=================================================================================================
  128. * GLOBAL CONSTANTS
  129. =================================================================================================*/
  130. /*=================================================================================================
  131. * GLOBAL VARIABLES
  132. =================================================================================================*/
  133. /*=================================================================================================
  134. * LOCAL FUNCTION PROTOTYPES
  135. =================================================================================================*/
  136. #define DIO_START_SEC_CODE
  137. #include "Dio_MemMap.h"
  138. #if (STD_ON == DIO_REVERSEPORTBITS)
  139. static Dio_PortLevelType Dio_Ipw_ReverseBits
  140. (
  141. Dio_PortLevelType Level
  142. );
  143. #endif /* (STD_ON == DIO_REVERSEPORTBITS) */
  144. /*=================================================================================================
  145. * LOCAL FUNCTIONS
  146. =================================================================================================*/
  147. #if (STD_ON == DIO_REVERSEPORTBITS)
  148. /**
  149. * @brief Reverses bits.
  150. *
  151. * @param[in] Level Bits to be reversed.
  152. *
  153. * @return The reversed bits.
  154. *
  155. * @pre This function can be used only if @p DIO_REVERSEPORTBITS is
  156. * enabled.
  157. */
  158. static Dio_PortLevelType Dio_Ipw_ReverseBits
  159. (
  160. Dio_PortLevelType Level
  161. )
  162. {
  163. /* RevLevel will be reversed bits of Level */
  164. Dio_PortLevelType TempLevel = Level;
  165. Dio_PortLevelType RevLevel = TempLevel;
  166. Dio_PortLevelType NumShifts = GPIO_DIO_REVERSE_BITS_NUMBER; /* number of shifts needed due to trailing 0s */
  167. for (TempLevel = (Dio_PortLevelType)(TempLevel >> GPIO_DIO_REVERSE_BITS_SHIFT); (Dio_PortLevelType)STD_LOW != TempLevel; TempLevel = (Dio_PortLevelType)(TempLevel >> GPIO_DIO_REVERSE_BITS_SHIFT))
  168. {
  169. RevLevel = (Dio_PortLevelType)(RevLevel << GPIO_DIO_REVERSE_BITS_SHIFT);
  170. RevLevel = (Dio_PortLevelType)(RevLevel | ((Dio_PortLevelType)(TempLevel & GPIO_DIO_REVERSE_BITS_MASK)));
  171. NumShifts--;
  172. }
  173. RevLevel = (Dio_PortLevelType)((uint32)RevLevel << NumShifts); /* Remaining shifts when Level's highest bits are zero */
  174. return RevLevel;
  175. }
  176. #endif /* (STD_ON == DIO_REVERSEPORTBITS) */
  177. /*=================================================================================================
  178. * GLOBAL FUNCTIONS
  179. =================================================================================================*/
  180. /*===============================================================================================*/
  181. /**
  182. * @brief Returns the value of the specified DIO channel.
  183. * @details This function returns the value of the specified DIO channel.
  184. *
  185. * @param[in] ChannelId Specifies the required channel id.
  186. *
  187. * @return Returns the level of the corresponding pin as
  188. * @p STD_HIGH or @p STD_LOW.
  189. * @retval STD_HIGH The logical level of the corresponding pin is 1.
  190. * @retval STD_LOW The logical level of the corresponding Pin is 0.
  191. */
  192. Dio_LevelType Dio_Ipw_ReadChannel
  193. (
  194. Dio_ChannelType ChannelId
  195. )
  196. {
  197. Dio_LevelType ChannelLevel = (Dio_LevelType) STD_LOW;
  198. uint32 u32GpioInstance;
  199. uint32 u32PinIndex;
  200. u32GpioInstance = GPIO_DIO_IP_PORT_U32(ChannelId);
  201. u32PinIndex = GPIO_DIO_IP_CHANNEL_U32(ChannelId);
  202. const GPIO_Type * GpioBase = (GPIO_Type *)GpioBaseAdresses[u32GpioInstance];
  203. ChannelLevel = Gpio_Dio_Ip_ReadPin(GpioBase, u32PinIndex);
  204. return ChannelLevel;
  205. }
  206. /*===============================================================================================*/
  207. /**
  208. * @brief Sets the level of a channel.
  209. * @details If the specified channel is configured as an output channel,
  210. * this function shall set the specified level on the
  211. * specified channel. If the specified channel is configured
  212. * as an input channel, this function shall have no influence
  213. * on the physical output and on the result of the next read
  214. * service.
  215. *
  216. * @param[in] ChannelId Specifies the required channel id.
  217. * @param[in] Level Specifies the channel desired level.
  218. */
  219. void Dio_Ipw_WriteChannel
  220. (
  221. Dio_ChannelType ChannelId,
  222. Dio_LevelType Level
  223. )
  224. {
  225. uint32 u32GpioInstance;
  226. uint32 u32PinIndex;
  227. u32GpioInstance = GPIO_DIO_IP_PORT_U32(ChannelId);
  228. u32PinIndex = GPIO_DIO_IP_CHANNEL_U32(ChannelId);
  229. GPIO_Type * GpioBase = (GPIO_Type *)GpioBaseAdresses[u32GpioInstance];
  230. Gpio_Dio_Ip_WritePin(GpioBase, u32PinIndex, Level);
  231. }
  232. #if (STD_ON == DIO_FLIP_CHANNEL_API)
  233. /*===============================================================================================*/
  234. /**
  235. * @brief Inverts the level of a channel.
  236. * @details If the specified channel is configured as an output channel,
  237. * this function shall invert the level of the specified
  238. * channel. If the specified channel is configured as an
  239. * input channel, this function shall have no influence
  240. * on the physical output and on the result of the next
  241. * read service.
  242. *
  243. * @param[in] ChannelId Specifies the required channel id.
  244. *
  245. * @return Returns the level of the corresponding pin as
  246. * @p STD_HIGH or @p STD_LOW.
  247. * @retval STD_HIGH The logical level of the corresponding pin is 1.
  248. * @retval STD_LOW The logical level of the corresponding Pin is 0.
  249. *
  250. * @pre This function can be used only if @p DIO_FLIP_CHANNEL_API
  251. * has been enabled.
  252. */
  253. Dio_LevelType Dio_Ipw_FlipChannel
  254. (
  255. Dio_ChannelType ChannelId
  256. )
  257. {
  258. Dio_LevelType ChannelLevel = (Dio_LevelType)STD_LOW;
  259. uint32 u32GpioInstance;
  260. uint32 u32PinIndex;
  261. uint32 u32PortOutPutLevel;
  262. u32GpioInstance = GPIO_DIO_IP_PORT_U32(ChannelId);
  263. u32PinIndex = GPIO_DIO_IP_CHANNEL_U32(ChannelId);
  264. GPIO_Type * GpioBase = (GPIO_Type *)GpioBaseAdresses[u32GpioInstance];
  265. Gpio_Dio_Ip_TogglePins(GpioBase, ((uint32)1U << u32PinIndex));
  266. u32PortOutPutLevel = Gpio_Dio_Ip_GetPinsOutput(GpioBase);
  267. ChannelLevel = (Dio_LevelType)((u32PortOutPutLevel & ((uint32)1U << u32PinIndex)) >> u32PinIndex);
  268. return ChannelLevel;
  269. }
  270. #endif /* (STD_ON == DIO_FLIP_CHANNEL_API) */
  271. /*===============================================================================================*/
  272. /**
  273. * @brief Returns the level of all channels of specified port.
  274. * @details This function will return the level of all channels
  275. * belonging to the specified port.
  276. *
  277. * @param[in] PortId Specifies the required port id.
  278. *
  279. * @return Levels of all channels of specified port.
  280. */
  281. Dio_PortLevelType Dio_Ipw_ReadPort
  282. (
  283. Dio_PortType PortId
  284. )
  285. {
  286. Dio_PortLevelType PortLevel = (Dio_PortLevelType)STD_LOW;
  287. const GPIO_Type * GpioBase = (GPIO_Type *)GpioBaseAdresses[PortId];
  288. PortLevel = (Dio_PortLevelType)(Gpio_Dio_Ip_ReadPins(GpioBase));
  289. #if (STD_ON == DIO_READZERO_UNDEFINEDPORTS)
  290. PortLevel =(Dio_PortLevelType)(PortLevel & Dio_aAvailablePinsForRead[PortId]);
  291. #endif
  292. #if (STD_ON == DIO_REVERSEPORTBITS)
  293. PortLevel = (Dio_PortLevelType)(Dio_Ipw_ReverseBits(PortLevel));
  294. #endif
  295. return PortLevel;
  296. }
  297. /*===============================================================================================*/
  298. /**
  299. * @brief Sets the value of a port.
  300. * @details This function will set the specified value on the specified
  301. * port.
  302. *
  303. * @param[in] PortId Specifies the required port id.
  304. * @param[in] Level Specifies the required levels for the port pins.
  305. */
  306. void Dio_Ipw_WritePort
  307. (
  308. Dio_PortType PortId,
  309. Dio_PortLevelType Level
  310. )
  311. {
  312. Dio_PortLevelType CrtLevel = Level;
  313. GPIO_Type * GpioBase = (GPIO_Type *)GpioBaseAdresses[PortId];
  314. #if (STD_ON == DIO_REVERSEPORTBITS)
  315. CrtLevel = (Dio_PortLevelType)(Dio_Ipw_ReverseBits(CrtLevel));
  316. #endif /* STD_ON == DIO_REVERSEPORTBITS */
  317. Gpio_Dio_Ip_WritePins(GpioBase, CrtLevel);
  318. }
  319. /*===============================================================================================*/
  320. /**
  321. * @brief This service reads a subset of the adjoining bits of a port.
  322. * @details This function will read a subset of adjoining bits of a
  323. * port (channel group).
  324. *
  325. * @param[in] pChannelGroupIdPtr Pointer to the channel group.
  326. * @return The channel group levels.
  327. */
  328. Dio_PortLevelType Dio_Ipw_ReadChannelGroup
  329. (
  330. const Dio_ChannelGroupType * pChannelGroupIdPtr
  331. )
  332. {
  333. Dio_PortLevelType PortLevel = (Dio_PortLevelType)STD_LOW;
  334. Dio_PortLevelType pinsValue;
  335. const GPIO_Type * GpioBase = (GPIO_Type *)GpioBaseAdresses[pChannelGroupIdPtr->port];
  336. pinsValue = (Dio_PortLevelType)(Gpio_Dio_Ip_ReadPins(GpioBase));
  337. #if (STD_ON == DIO_REVERSEPORTBITS)
  338. PortLevel = (Dio_PortLevelType)((pinsValue & (pChannelGroupIdPtr->mask)) << (pChannelGroupIdPtr->u8offset));
  339. PortLevel = (Dio_PortLevelType)(Dio_Ipw_ReverseBits(PortLevel));
  340. #else
  341. PortLevel = (Dio_PortLevelType)((pinsValue & (pChannelGroupIdPtr->mask)) >> (pChannelGroupIdPtr->u8offset));
  342. #endif
  343. return PortLevel;
  344. }
  345. /*===============================================================================================*/
  346. /**
  347. * @brief Sets a subset of the adjoining bits of a port to the
  348. * specified levels.
  349. * @details This function will set a subset of adjoining bits of a port
  350. * (channel group) to the specified levels without changing
  351. * the remaining channels of the port and channels that
  352. * are configured as input.
  353. * This function will do the masking of the channels and will
  354. * do the shifting so that the values written by the function
  355. * are aligned to the LSB.
  356. *
  357. * @param[in] pChannelGroupIdPtr Pointer to the channel group.
  358. * @param[in] Level Desired levels for the channel group.
  359. */
  360. void Dio_Ipw_WriteChannelGroup
  361. (
  362. const Dio_ChannelGroupType * pChannelGroupIdPtr,
  363. Dio_PortLevelType Level
  364. )
  365. {
  366. Dio_PortLevelType ValueSet;
  367. Dio_PortLevelType ValueClear;
  368. GPIO_Type * GpioBase = (GPIO_Type *)GpioBaseAdresses[pChannelGroupIdPtr->port];
  369. #if (STD_ON == DIO_REVERSEPORTBITS)
  370. ValueSet = (((Dio_PortLevelType)(Dio_Ipw_ReverseBits(Level)) >> (pChannelGroupIdPtr->u8offset)) & (((Dio_PortLevelType)(pChannelGroupIdPtr->mask) )));
  371. #else
  372. ValueSet = (((Dio_PortLevelType)(Level) << (pChannelGroupIdPtr->u8offset)) & (((Dio_PortLevelType)(pChannelGroupIdPtr->mask) )));
  373. #endif
  374. Gpio_Dio_Ip_SetPins(GpioBase, ValueSet);
  375. ValueClear = (~ValueSet) & pChannelGroupIdPtr->mask;
  376. Gpio_Dio_Ip_ClearPins(GpioBase, ValueClear);
  377. }
  378. #if ((STD_ON == DIO_MASKEDWRITEPORT_API) || defined(__DOXYGEN__))
  379. /*===============================================================================================*/
  380. /**
  381. * @brief DIO Mask write port using mask.
  382. * @details Writes a DIO port with masked value.
  383. *
  384. * @param[in] PortId Specifies the required port id.
  385. * @param[in] Level Specifies the required levels for the port pins.
  386. * @param[in] Mask Specifies the Mask value of the port.
  387. *
  388. * @pre This function can be used only if @p DIO_MASKEDWRITEPORT_API
  389. * has been enabled.
  390. */
  391. void Dio_Ipw_MaskedWritePort
  392. (
  393. Dio_PortType PortId,
  394. Dio_PortLevelType Level,
  395. Dio_PortLevelType Mask
  396. )
  397. {
  398. Dio_PortLevelType Value;
  399. GPIO_Type * GpioBase = (GPIO_Type *)GpioBaseAdresses[PortId];
  400. Value = ((uint32)Mask) & ((uint32)(Level));
  401. Gpio_Dio_Ip_SetPins(GpioBase, Value);
  402. Value = (~Level) & Mask;
  403. Gpio_Dio_Ip_ClearPins(GpioBase, Value);
  404. }
  405. #endif /* (STD_ON == DIO_MASKEDWRITEPORT_API) || defined(__DOXYGEN__) */
  406. #define DIO_STOP_SEC_CODE
  407. #include "Dio_MemMap.h"
  408. #ifdef __cplusplus
  409. }
  410. #endif
  411. /** @} */