Flexio_Uart_Ip.c 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938
  1. /*==================================================================================================
  2. * Project : RTD AUTOSAR 4.4
  3. * Platform : CORTEXM
  4. * Peripheral : FLEXIO
  5. * Dependencies :
  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 Flexio_Uart_Ip.c
  26. * @defgroup flexio_uart_ip Flexio UART IPL
  27. * @addtogroup flexio_uart_ip Flexio UART IPL
  28. * @{
  29. */
  30. #ifdef __cplusplus
  31. extern "C"{
  32. #endif
  33. /*==================================================================================================
  34. * INCLUDE FILES
  35. * 1) system and project includes
  36. * 2) needed interfaces from external units
  37. * 3) internal and external interfaces from this unit
  38. ==================================================================================================*/
  39. #include "Flexio_Uart_Ip_HwAccess.h"
  40. #include "Flexio_Uart_Ip_Irq.h"
  41. #include "Flexio_Uart_Ip.h"
  42. #include "SchM_Uart.h"
  43. #ifdef FLEXIO_UART_IP_DEV_ERROR_DETECT
  44. #if (FLEXIO_UART_IP_DEV_ERROR_DETECT == STD_ON)
  45. #include "Devassert.h"
  46. #endif
  47. #endif
  48. #if (FLEXIO_UART_IP_HAS_DMA_ENABLED == STD_ON)
  49. #include "Dma_Ip.h"
  50. #endif
  51. /*==================================================================================================
  52. * SOURCE FILE VERSION INFORMATION
  53. ==================================================================================================*/
  54. #define FLEXIO_UART_IP_VENDOR_ID_C 43
  55. #define FLEXIO_UART_IP_AR_RELEASE_MAJOR_VERSION_C 4
  56. #define FLEXIO_UART_IP_AR_RELEASE_MINOR_VERSION_C 4
  57. #define FLEXIO_UART_IP_AR_RELEASE_REVISION_VERSION_C 0
  58. #define FLEXIO_UART_IP_SW_MAJOR_VERSION_C 1
  59. #define FLEXIO_UART_IP_SW_MINOR_VERSION_C 0
  60. #define FLEXIO_UART_IP_SW_PATCH_VERSION_C 0
  61. /*==================================================================================================
  62. * FILE VERSION CHECKS
  63. ==================================================================================================*/
  64. /* Checks against Flexio_Uart_Ip_Irq.h */
  65. #if (FLEXIO_UART_IP_VENDOR_ID_C != FLEXIO_UART_IP_IRQ_VENDOR_ID)
  66. #error "Flexio_Uart_Ip_Irq.c and Flexio_Uart_Ip_Irq.h have different vendor ids"
  67. #endif
  68. #if ((FLEXIO_UART_IP_AR_RELEASE_MAJOR_VERSION_C != FLEXIO_UART_IP_IRQ_AR_RELEASE_MAJOR_VERSION) || \
  69. (FLEXIO_UART_IP_AR_RELEASE_MINOR_VERSION_C != FLEXIO_UART_IP_IRQ_AR_RELEASE_MINOR_VERSION) || \
  70. (FLEXIO_UART_IP_AR_RELEASE_REVISION_VERSION_C != FLEXIO_UART_IP_IRQ_AR_RELEASE_REVISION_VERSION))
  71. #error "AUTOSAR Version Numbers of Flexio_Uart_Ip.c and Flexio_Uart_Ip_Irq.h are different"
  72. #endif
  73. #if ((FLEXIO_UART_IP_SW_MAJOR_VERSION_C != FLEXIO_UART_IP_IRQ_SW_MAJOR_VERSION) || \
  74. (FLEXIO_UART_IP_SW_MINOR_VERSION_C != FLEXIO_UART_IP_IRQ_SW_MINOR_VERSION) || \
  75. (FLEXIO_UART_IP_SW_PATCH_VERSION_C != FLEXIO_UART_IP_IRQ_SW_PATCH_VERSION))
  76. #error "Software Version Numbers of Flexio_Uart_Ip.c and Flexio_Uart_Ip_Irq.h are different"
  77. #endif
  78. /* Checks against Flexio_Uart_Ip.h */
  79. #if (FLEXIO_UART_IP_VENDOR_ID_C != FLEXIO_UART_IP_VENDOR_ID)
  80. #error "Flexio_Uart_Ip.c and Flexio_Uart_Ip.h have different vendor ids"
  81. #endif
  82. #if ((FLEXIO_UART_IP_AR_RELEASE_MAJOR_VERSION_C != FLEXIO_UART_IP_AR_RELEASE_MAJOR_VERSION) || \
  83. (FLEXIO_UART_IP_AR_RELEASE_MINOR_VERSION_C != FLEXIO_UART_IP_AR_RELEASE_MINOR_VERSION) || \
  84. (FLEXIO_UART_IP_AR_RELEASE_REVISION_VERSION_C != FLEXIO_UART_IP_AR_RELEASE_REVISION_VERSION))
  85. #error "AUTOSAR Version Numbers of Flexio_Uart_Ip.c and Flexio_Uart_Ip.h are different"
  86. #endif
  87. #if ((FLEXIO_UART_IP_SW_MAJOR_VERSION_C != FLEXIO_UART_IP_SW_MAJOR_VERSION) || \
  88. (FLEXIO_UART_IP_SW_MINOR_VERSION_C != FLEXIO_UART_IP_SW_MINOR_VERSION) || \
  89. (FLEXIO_UART_IP_SW_PATCH_VERSION_C != FLEXIO_UART_IP_SW_PATCH_VERSION))
  90. #error "Software Version Numbers of Flexio_Uart_Ip.c and Flexio_Uart_Ip.h are different"
  91. #endif
  92. /* Checks against Flexio_Uart_Ip_HwAccess.h */
  93. #if (FLEXIO_UART_IP_VENDOR_ID_C != FLEXIO_UART_IP_HWACCESS_VENDOR_ID)
  94. #error "Flexio_Uart_Ip.c and Flexio_Uart_Ip_Types.h have different vendor ids"
  95. #endif
  96. #if ((FLEXIO_UART_IP_AR_RELEASE_MAJOR_VERSION_C != FLEXIO_UART_IP_HWACCESS_AR_RELEASE_MAJOR_VERSION) || \
  97. (FLEXIO_UART_IP_AR_RELEASE_MINOR_VERSION_C != FLEXIO_UART_IP_HWACCESS_AR_RELEASE_MINOR_VERSION) || \
  98. (FLEXIO_UART_IP_AR_RELEASE_REVISION_VERSION_C != FLEXIO_UART_IP_HWACCESS_AR_RELEASE_REVISION_VERSION))
  99. #error "AUTOSAR Version Numbers of Flexio_Uart_Ip.c and Flexio_Uart_Ip_Types.h are different"
  100. #endif
  101. #if ((FLEXIO_UART_IP_SW_MAJOR_VERSION_C != FLEXIO_UART_IP_HWACCESS_SW_MAJOR_VERSION) || \
  102. (FLEXIO_UART_IP_SW_MINOR_VERSION_C != FLEXIO_UART_IP_HWACCESS_SW_MINOR_VERSION) || \
  103. (FLEXIO_UART_IP_SW_PATCH_VERSION_C != FLEXIO_UART_IP_HWACCESS_SW_PATCH_VERSION))
  104. #error "Software Version Numbers of Flexio_Uart_Ip.c and Flexio_Uart_Ip_Types.h are different"
  105. #endif
  106. #ifndef DISABLE_MCAL_INTERMODULE_ASR_CHECK
  107. /* Check if current file and SchM_Uart.h header file are of the same Autosar version */
  108. #if ((FLEXIO_UART_IP_AR_RELEASE_MAJOR_VERSION_C != SCHM_UART_AR_RELEASE_MAJOR_VERSION) || \
  109. (FLEXIO_UART_IP_AR_RELEASE_MINOR_VERSION_C != SCHM_UART_AR_RELEASE_MAJOR_VERSION))
  110. #error "Flexio_Uart_Ip.c and SchM_Uart.h are different"
  111. #endif
  112. /* Checks against Dma_Ip.h */
  113. #if (FLEXIO_UART_IP_HAS_DMA_ENABLED == STD_ON)
  114. #if ((FLEXIO_UART_IP_AR_RELEASE_MAJOR_VERSION_C != DMA_IP_AR_RELEASE_MAJOR_VERSION_H) || \
  115. (FLEXIO_UART_IP_AR_RELEASE_MINOR_VERSION_C != DMA_IP_AR_RELEASE_MINOR_VERSION_H))
  116. #error "AutoSar Version Numbers of Flexio_Uart_Ip.c and Dma_Ip.h are different"
  117. #endif
  118. #endif
  119. #ifdef FLEXIO_UART_IP_DEV_ERROR_DETECT
  120. #if (STD_ON == FLEXIO_UART_IP_DEV_ERROR_DETECT)
  121. /* Check if Flexio_Uart_Ip.c file and Devassert.h file are of the same Autosar version */
  122. #if ((FLEXIO_UART_IP_AR_RELEASE_MAJOR_VERSION_C != DEVASSERT_AR_RELEASE_MAJOR_VERSION) || \
  123. (FLEXIO_UART_IP_AR_RELEASE_MINOR_VERSION_C != DEVASSERT_AR_RELEASE_MINOR_VERSION))
  124. #error "AutoSar Version Numbers of Flexio_Uart_Ip.c and Devassert.h are different"
  125. #endif
  126. #endif /* (STD_ON == FLEXIO_UART_IP_DEV_ERROR_DETECT) */
  127. #endif /* #ifdef FLEXIO_UART_IP_DEV_ERROR_DETECT */
  128. #endif
  129. /*==================================================================================================
  130. * LOCAL TYPEDEFS (STRUCTURES, UNIONS, ENUMS)
  131. ==================================================================================================*/
  132. /*==================================================================================================
  133. * LOCAL MACROS
  134. ==================================================================================================*/
  135. #ifdef FLEXIO_UART_IP_DEV_ERROR_DETECT
  136. #if (FLEXIO_UART_IP_DEV_ERROR_DETECT == STD_ON)
  137. #define FLEXIO_UART_IP_DEV_ASSERT(x) DevAssert(x)
  138. #else
  139. #define FLEXIO_UART_IP_DEV_ASSERT(x) (void)(x)
  140. #endif
  141. #endif
  142. /* Shifters/Timers used for UART simulation The parameter x represents the
  143. ResourceIndex value for the current driver Channel */
  144. #define TX_SHIFTER(x) (x)
  145. #define RX_SHIFTER(x) (x)
  146. #define TX_TIMER(x) (x)
  147. #define RX_TIMER(x) (x)
  148. /* @brief Address of the least significant byte or word in a 32-bit register (depends on endianness) */
  149. #define FLEXIO_UART_IP_LSBW_ADDR(reg) ((uint32)(&(reg)))
  150. #if (FLEXIO_UART_IP_HAS_DMA_ENABLED == STD_ON)
  151. #define FLEXIO_UART_DMA_CONFIG_LIST_DIMENSION (10U)
  152. #define FLEXIO_UART_DMA_LEAST_CONFIG_LIST_DIMENSION (2U)
  153. #endif
  154. /*==================================================================================================
  155. * LOCAL CONSTANTS
  156. ==================================================================================================*/
  157. /*==================================================================================================
  158. LOCAL VARIABLES
  159. ==================================================================================================*/
  160. #define UART_START_SEC_VAR_CLEARED_UNSPECIFIED_NO_CACHEABLE
  161. #include "Uart_MemMap.h"
  162. /** @brief Pointer to flexio runtime state structure */
  163. Flexio_Uart_Ip_StateStructureType Flexio_Uart_Ip_apStateStructure[FLEXIO_UART_IP_NUMBER_OF_SHIFTER_AND_TIMER];
  164. #define UART_STOP_SEC_VAR_CLEARED_UNSPECIFIED_NO_CACHEABLE
  165. #include "Uart_MemMap.h"
  166. #ifdef FLEXIO_UART_IP_IS_USING
  167. #if (FLEXIO_UART_IP_IS_USING == STD_ON)
  168. #define UART_START_SEC_CONST_UNSPECIFIED
  169. /* @violates @ref Uart_c_REF_1 This violation is not fixed since the inclusion of Uart_MemMap.h is as per AUTOSAR requirement*/
  170. #include "Uart_MemMap.h"
  171. /** @brief Table of base addresses for FLEXIO instances. */
  172. static FLEXIO_Type * const Flexio_Uart_Ip_apBases[FLEXIO_INSTANCE_COUNT] = IP_FLEXIO_BASE_PTRS;
  173. #define UART_STOP_SEC_CONST_UNSPECIFIED
  174. /* @violates @ref Uart_c_REF_1 This violation is not fixed since the inclusion of Uart_MemMap.h is as per AUTOSAR requirement*/
  175. #include "Uart_MemMap.h"
  176. #endif
  177. #endif /* #ifdef FLEXIO_UART_IP_IS_USING */
  178. /*==================================================================================================
  179. * GLOBAL CONSTANTS
  180. ==================================================================================================*/
  181. #define UART_START_SEC_VAR_CLEARED_UNSPECIFIED
  182. #include "Uart_MemMap.h"
  183. /** @brief User config structure. */
  184. const Flexio_Uart_Ip_UserConfigType * Flexio_Uart_Ip_apUserConfig[FLEXIO_UART_IP_NUMBER_OF_SHIFTER_AND_TIMER];
  185. #ifdef FLEXIO_UART_IP_IS_USING
  186. #if (FLEXIO_UART_IP_IS_USING == STD_ON)
  187. /** @brief Array of UART driver runtime state structures */
  188. static Flexio_Uart_Ip_StateStructureType * Flexio_Uart_Ip_apStateStructuresArray[FLEXIO_UART_IP_NUMBER_OF_SHIFTER_AND_TIMER];
  189. #endif /* (FLEXIO_UART_IP_IS_USING == STD_ON) */
  190. #endif /* #ifdef FLEXIO_UART_IP_IS_USING */
  191. #define UART_STOP_SEC_VAR_CLEARED_UNSPECIFIED
  192. #include "Uart_MemMap.h"
  193. /*==================================================================================================
  194. * GLOBAL VARIABLES
  195. ==================================================================================================*/
  196. /*==================================================================================================
  197. * LOCAL FUNCTION PROTOTYPES
  198. ==================================================================================================*/
  199. /*==================================================================================================
  200. * LOCAL FUNCTIONS
  201. ==================================================================================================*/
  202. #ifdef FLEXIO_UART_IP_IS_USING
  203. #if (FLEXIO_UART_IP_IS_USING == STD_ON)
  204. #define UART_START_SEC_CODE
  205. #include "Uart_MemMap.h"
  206. static void Flexio_Uart_Ip_ConfigureTx(const uint8 Channel,
  207. const Flexio_Uart_Ip_BitCountPerCharType BitCount,
  208. const uint8 Divider,
  209. const uint8 DataPin,
  210. const Flexio_Uart_Ip_TimerDecrementType TimerDec);
  211. static void Flexio_Uart_Ip_ConfigureRx(const uint8 Channel,
  212. const Flexio_Uart_Ip_BitCountPerCharType BitCount,
  213. const uint8 Divider,
  214. const uint8 DataPin,
  215. const Flexio_Uart_Ip_TimerDecrementType TimerDec);
  216. static void Flexio_Uart_Ip_StopTransfer(const uint8 Channel);
  217. static void Flexio_Uart_Ip_CheckTxOperation(const uint8 Channel,
  218. const uint8 ShifterMaskFlag,
  219. const uint8 TimerMaskFlag);
  220. static void Flexio_Uart_Ip_ReadData(const uint8 Channel);
  221. static void Flexio_Uart_Ip_CheckRxOperation(const uint8 Channel,
  222. const uint8 ShifterMaskFlag,
  223. const uint8 ShifterErrMaskFlag);
  224. static void Flexio_Uart_Ip_EnableReceiver(const uint8 Channel);
  225. static void Flexio_Uart_Ip_EndTransfer(const uint8 Channel);
  226. static void Flexio_Uart_Ip_WriteData(const uint8 Channel);
  227. #if (FLEXIO_UART_IP_HAS_DMA_ENABLED == STD_ON)
  228. static Flexio_Uart_Ip_StatusType Flexio_Uart_Ip_StartSendDataUsingDma(uint8 Channel,
  229. const uint8 * TxData,
  230. uint32 TxSize);
  231. static Flexio_Uart_Ip_StatusType Flexio_Uart_Ip_StartReceiveDataUsingDma(uint8 Channel,
  232. uint8 * RxBuff,
  233. uint32 RxSize);
  234. #endif /*(FLEXIO_UART_IP_HAS_DMA_ENABLED == STD_ON)*/
  235. static void Flexio_Uart_Ip_CheckCompleteTransferData(const uint8 Channel);
  236. static void Flexio_Uart_Ip_SetupReceiveData(const uint8 Channel);
  237. static void Flexio_Uart_Ip_CheckShifterErrorStatus(const uint8 Channel);
  238. static void Flexio_Uart_Ip_SetShifterInterrupt(const uint8 Channel, boolean Enable);
  239. /*==================================================================================================
  240. * GLOBAL FUNCTIONS
  241. ==================================================================================================*/
  242. /*FUNCTION**********************************************************************
  243. *
  244. * Function Name : Flexio_Uart_Ip_OperationHandler
  245. * Description : Implementation of FLEXIO interrupt handler named in start-up code
  246. *
  247. *END**************************************************************************/
  248. /* implements Flexio_Uart_Ip_IrqHandlerHandler_Activity*/
  249. void Flexio_Uart_Ip_IrqHandler(uint8 Channel, uint8 ShifterMaskFlag, uint8 ShifterErrorMaskFlag, uint8 TimerMaskFlag)
  250. {
  251. const Flexio_Uart_Ip_UserConfigType *UartUserCfg;
  252. FLEXIO_Type *Base;
  253. UartUserCfg = Flexio_Uart_Ip_apUserConfig[Channel];
  254. Base = Flexio_Uart_Ip_apBases[FLEXIO_HW_INSTANCE];
  255. /* If the driver is not initialized, the ISR shall only clear interrupt status flag and return immediately. */
  256. if(NULL_PTR == UartUserCfg)
  257. {
  258. /* Clear timer status */
  259. Flexio_Mcl_Ip_ClearTimerStatus(Base, Channel);
  260. /* Clear Shifter status */
  261. Flexio_Mcl_Ip_ClearShifterStatus(Base, Channel);
  262. /*Clear Error Status flag*/
  263. Flexio_Mcl_Ip_ClearShifterErrorStatus(Base, Channel);
  264. }
  265. else
  266. {
  267. /* There is an Event for the current instance - call the isr */
  268. if (FLEXIO_UART_IP_DIRECTION_TX == UartUserCfg->Direction)
  269. {
  270. Flexio_Uart_Ip_CheckTxOperation(Channel,
  271. ShifterMaskFlag,
  272. TimerMaskFlag);
  273. }
  274. else
  275. {
  276. Flexio_Uart_Ip_CheckRxOperation(Channel,
  277. ShifterMaskFlag,
  278. ShifterErrorMaskFlag);
  279. }
  280. }
  281. }
  282. /*FUNCTION**********************************************************************
  283. *
  284. * Function Name : Flexio_Uart_Ip_ConfigureTx
  285. * Description : configures the FLEXIO module for UART
  286. *
  287. *END**************************************************************************/
  288. static void Flexio_Uart_Ip_ConfigureTx(const uint8 Channel,
  289. const Flexio_Uart_Ip_BitCountPerCharType BitCount,
  290. const uint8 Divider,
  291. const uint8 DataPin,
  292. const Flexio_Uart_Ip_TimerDecrementType TimerDec)
  293. {
  294. FLEXIO_Type *Base;
  295. uint16 Bits;
  296. Base = Flexio_Uart_Ip_apBases[FLEXIO_HW_INSTANCE];
  297. Bits = (uint16)BitCount;
  298. /* Configure tx shifter */
  299. Flexio_Uart_Ip_SetShifterConfig(Base,
  300. TX_SHIFTER(Channel),
  301. FLEXIO_SHIFTER_START_BIT_0,
  302. FLEXIO_SHIFTER_STOP_BIT_1,
  303. FLEXIO_SHIFTER_SOURCE_PIN);
  304. Flexio_Uart_Ip_SetShifterControl(Base,
  305. TX_SHIFTER(Channel),
  306. FLEXIO_SHIFTER_MODE_TRANSMIT,
  307. TX_TIMER(Channel),
  308. FLEXIO_TIMER_POLARITY_POSEDGE);
  309. Flexio_Uart_Ip_SetPinShifterControl(Base,
  310. TX_SHIFTER(Channel),
  311. DataPin, /* Output on tx pin */
  312. FLEXIO_PIN_POLARITY_HIGH,
  313. FLEXIO_PIN_CONFIG_OUTPUT);
  314. /* (Divider = InputClock / (BaudRate * 32U or 512U depend on baudrate) - 1; */
  315. Flexio_Uart_Ip_SetTimerCompare(Base, TX_TIMER(Channel), (uint16)((((uint16)(Bits << 1U) - 1U) << 8U) + Divider));
  316. /* Configure tx timer */
  317. Flexio_Uart_Ip_SetTimerConfig(Base,
  318. TX_TIMER(Channel),
  319. FLEXIO_TIMER_RESET_NEVER,
  320. TimerDec, /* Decrement on FlexIO clock */
  321. FLEXIO_TIMER_INITOUT_ONE);
  322. Flexio_Uart_Ip_SetTimerStartStopBitConfig(Base,
  323. TX_TIMER(Channel),
  324. FLEXIO_TIMER_START_BIT_ENABLED,
  325. FLEXIO_TIMER_STOP_BIT_TIM_DIS);
  326. Flexio_Uart_Ip_SetTimerCondition(Base,
  327. TX_TIMER(Channel),
  328. FLEXIO_TIMER_ENABLE_TRG_HIGH, /* Enable when Tx data is available */
  329. FLEXIO_TIMER_DISABLE_TIM_CMP);
  330. Flexio_Uart_Ip_SetTimerControl(Base,
  331. TX_TIMER(Channel),
  332. FLEXIO_TIMER_MODE_DISABLED);
  333. Flexio_Uart_Ip_SetTimerTrigger(Base,
  334. TX_TIMER(Channel),
  335. (uint8)((uint8)(TX_SHIFTER(Channel) << 2U) + 1U), /* Trigger on tx shifter status flag */
  336. FLEXIO_TRIGGER_POLARITY_LOW,
  337. FLEXIO_TRIGGER_SOURCE_INTERNAL);
  338. Flexio_Uart_Ip_SetPinTimerControl(Base,
  339. TX_TIMER(Channel),
  340. 0U, /* Pin unused */
  341. FLEXIO_PIN_POLARITY_HIGH,
  342. FLEXIO_PIN_CONFIG_DISABLED);
  343. }
  344. /*FUNCTION**********************************************************************
  345. *
  346. * Function Name : Flexio_Uart_Ip_ConfigureRx
  347. * Description : configures the FLEXIO module for UART
  348. *
  349. *END**************************************************************************/
  350. static void Flexio_Uart_Ip_ConfigureRx(const uint8 Channel,
  351. const Flexio_Uart_Ip_BitCountPerCharType BitCount,
  352. const uint8 Divider,
  353. const uint8 DataPin,
  354. const Flexio_Uart_Ip_TimerDecrementType TimerDec)
  355. {
  356. FLEXIO_Type *Base;
  357. uint16 Bits;
  358. Base = Flexio_Uart_Ip_apBases[FLEXIO_HW_INSTANCE];
  359. Bits = (uint16)BitCount;
  360. /* Configure rx shifter */
  361. Flexio_Uart_Ip_SetShifterConfig(Base,
  362. RX_SHIFTER(Channel),
  363. FLEXIO_SHIFTER_START_BIT_0,
  364. FLEXIO_SHIFTER_STOP_BIT_1,
  365. FLEXIO_SHIFTER_SOURCE_PIN);
  366. Flexio_Uart_Ip_SetShifterControl(Base,
  367. RX_SHIFTER(Channel),
  368. FLEXIO_SHIFTER_MODE_DISABLED,
  369. RX_TIMER(Channel),
  370. FLEXIO_TIMER_POLARITY_NEGEDGE);
  371. Flexio_Uart_Ip_SetPinShifterControl(Base,
  372. RX_SHIFTER(Channel),
  373. DataPin, /* Input from rx pin */
  374. FLEXIO_PIN_POLARITY_HIGH,
  375. FLEXIO_PIN_CONFIG_DISABLED);
  376. /* (Divider = InputClock / (BaudRate * 32U or 512U depend on baudrate) - 1; */
  377. Flexio_Uart_Ip_SetTimerCompare(Base, RX_TIMER(Channel), (uint16)((((uint16)(Bits << 1U) - 1U) << 8U) + Divider));
  378. /* Configure rx timer */
  379. Flexio_Uart_Ip_SetTimerConfig(Base,
  380. RX_TIMER(Channel),
  381. FLEXIO_TIMER_RESET_PIN_RISING,
  382. TimerDec, /* Decrement on FlexIO clock */
  383. FLEXIO_TIMER_INITOUT_ONE_RESET);
  384. Flexio_Uart_Ip_SetTimerStartStopBitConfig(Base,
  385. TX_TIMER(Channel),
  386. FLEXIO_TIMER_START_BIT_ENABLED,
  387. FLEXIO_TIMER_STOP_BIT_TIM_DIS);
  388. Flexio_Uart_Ip_SetTimerCondition(Base,
  389. TX_TIMER(Channel),
  390. FLEXIO_TIMER_ENABLE_PIN_POSEDGE, /* Enable when data is available */
  391. FLEXIO_TIMER_DISABLE_TIM_CMP);
  392. Flexio_Uart_Ip_SetTimerControl(Base,
  393. RX_TIMER(Channel),
  394. FLEXIO_TIMER_MODE_DISABLED);
  395. Flexio_Uart_Ip_SetTimerTrigger(Base,
  396. TX_TIMER(Channel),
  397. 0U, /* Trigger unused */
  398. FLEXIO_TRIGGER_POLARITY_HIGH,
  399. FLEXIO_TRIGGER_SOURCE_EXTERNAL);
  400. Flexio_Uart_Ip_SetPinTimerControl(Base,
  401. TX_TIMER(Channel),
  402. DataPin, /* Input from rx pin */
  403. FLEXIO_PIN_POLARITY_LOW,
  404. FLEXIO_PIN_CONFIG_DISABLED);
  405. }
  406. /*FUNCTION**********************************************************************
  407. *
  408. * Function Name : Flexio_Uart_Ip_EndTransfer
  409. * Description : End the current transfer
  410. *
  411. *END**************************************************************************/
  412. static void Flexio_Uart_Ip_EndTransfer(const uint8 Channel)
  413. {
  414. FLEXIO_Type *Base;
  415. Flexio_Uart_Ip_StateStructureType * UartState;
  416. const Flexio_Uart_Ip_UserConfigType *UartUserCfg;
  417. UartState = (Flexio_Uart_Ip_StateStructureType *)Flexio_Uart_Ip_apStateStructuresArray[Channel];
  418. UartUserCfg = Flexio_Uart_Ip_apUserConfig[Channel];
  419. Base = Flexio_Uart_Ip_apBases[FLEXIO_HW_INSTANCE];
  420. /* Disable transfer engine */
  421. switch (UartUserCfg->DriverType)
  422. {
  423. case FLEXIO_UART_IP_DRIVER_TYPE_INTERRUPTS:
  424. /* Disable interrupts for Rx / Tx shifter */
  425. Flexio_Uart_Ip_SetShifterInterrupt(Channel, FALSE);
  426. /* Disable timer interrupt */
  427. Flexio_Mcl_Ip_SetTimerInterrupt(Base, (uint8)(1U << TX_TIMER(Channel)), FALSE);
  428. break;
  429. case FLEXIO_UART_IP_DRIVER_TYPE_DMA:
  430. /* For Tx we need to disable timer interrupt */
  431. Flexio_Mcl_Ip_SetTimerInterrupt(Base, (uint8)(1U << TX_TIMER(Channel)), FALSE);
  432. /* Disable the FlexIO DMA request */
  433. Flexio_Mcl_Ip_SetShifterDMARequest(Base, (uint8)(1U << TX_SHIFTER(Channel)), FALSE);
  434. break;
  435. default:
  436. /* Impossible type - do nothing */
  437. break;
  438. }
  439. UartState->RemainingBytes = 0U;
  440. UartState->DriverIdle = FALSE;
  441. }
  442. /*FUNCTION**********************************************************************
  443. *
  444. * Function Name : Flexio_Uart_Ip_StopTransfer
  445. * Description : Forcefully stops the current transfer
  446. *
  447. *END**************************************************************************/
  448. static void Flexio_Uart_Ip_StopTransfer(const uint8 Channel)
  449. {
  450. FLEXIO_Type *Base;
  451. const Flexio_Uart_Ip_UserConfigType *UartUserCfg;
  452. UartUserCfg = Flexio_Uart_Ip_apUserConfig[Channel];
  453. Base = Flexio_Uart_Ip_apBases[FLEXIO_HW_INSTANCE];
  454. const Flexio_Uart_Ip_StateStructureType * UartState;
  455. uint32 StartTime;
  456. uint32 TimeoutTicks;
  457. uint32 ElapsedTicks = 0;
  458. UartState = (Flexio_Uart_Ip_StateStructureType *)Flexio_Uart_Ip_apStateStructuresArray[Channel];
  459. /* End the transfer */
  460. Flexio_Uart_Ip_EndTransfer(Channel);
  461. if (FLEXIO_UART_IP_STATUS_ABORTED == UartState->Status)
  462. {
  463. Flexio_Uart_Ip_StartTimeout(&StartTime, &TimeoutTicks, FLEXIO_UART_IP_TIMEOUT_VALUE_US, FLEXIO_UART_IP_TIMEOUT_TYPE);
  464. while (!Flexio_Mcl_Ip_GetTimerStatus(Base, TX_TIMER(Channel)) && \
  465. !Flexio_Uart_Ip_CheckTimeout(&StartTime, &ElapsedTicks, TimeoutTicks, FLEXIO_UART_IP_TIMEOUT_TYPE))
  466. {}
  467. }
  468. /* Disable and re-enable timers and shifters to reset them */
  469. Flexio_Uart_Ip_SetTimerMode(Base, TX_TIMER(Channel), FLEXIO_TIMER_MODE_DISABLED);
  470. Flexio_Uart_Ip_SetShifterMode(Base, TX_SHIFTER(Channel), FLEXIO_SHIFTER_MODE_DISABLED);
  471. /* Clear any leftover error flags */
  472. Flexio_Mcl_Ip_ClearShifterErrorStatus(Base, TX_SHIFTER(Channel));
  473. /* Re-enable shifter for Tx, to ensure correct idle state */
  474. if (FLEXIO_UART_IP_DIRECTION_TX == UartUserCfg->Direction)
  475. {
  476. /* In tx mode restore start bit in case it was changed for end of transmission detection */
  477. Flexio_Uart_Ip_SetShifterStartBit(Base, TX_SHIFTER(Channel), FLEXIO_SHIFTER_START_BIT_0);
  478. Flexio_Uart_Ip_SetShifterMode(Base, TX_SHIFTER(Channel), FLEXIO_SHIFTER_MODE_TRANSMIT);
  479. }
  480. }
  481. /*FUNCTION**********************************************************************
  482. *
  483. * Function Name : Flexio_Uart_Ip_WriteData
  484. * Description : Writes data to be transmitted by the module
  485. *
  486. *END**************************************************************************/
  487. static void Flexio_Uart_Ip_WriteData(const uint8 Channel)
  488. {
  489. FLEXIO_Type *Base;
  490. const Flexio_Uart_Ip_UserConfigType *UartUserCfg;
  491. Flexio_Uart_Ip_StateStructureType * UartState;
  492. uint32 Data32b = 0UL;
  493. UartUserCfg = Flexio_Uart_Ip_apUserConfig[Channel];
  494. UartState = (Flexio_Uart_Ip_StateStructureType *)Flexio_Uart_Ip_apStateStructuresArray[Channel];
  495. FLEXIO_UART_IP_DEV_ASSERT(UartState->TxData != NULL_PTR);
  496. Base = Flexio_Uart_Ip_apBases[FLEXIO_HW_INSTANCE];
  497. /* Read data from user buffer and update tx buffer pointer and remaining bytes count */
  498. if (FLEXIO_UART_IP_8_BITS_PER_CHAR == UartUserCfg->BitCount)
  499. {
  500. Data32b = (uint32)(*UartState->TxData);
  501. UartState->TxData++;
  502. UartState->RemainingBytes -= 1U;
  503. }
  504. /* Writes data in the specified shifter buffer.*/
  505. Flexio_Uart_Ip_WriteShifterBuffer(Base, TX_SHIFTER(Channel), Data32b);
  506. }
  507. /*FUNCTION**********************************************************************
  508. *
  509. * Function Name : Flexio_Uart_Ip_CheckTxOperation
  510. * Description : Check status of the UART transmission. This function can be
  511. * called either in an interrupt routine or directly in polling
  512. * mode to advance the transfer.
  513. *
  514. *END**************************************************************************/
  515. static void Flexio_Uart_Ip_CheckTxOperation(const uint8 Channel,
  516. const uint8 ShifterMaskFlag,
  517. const uint8 TimerMaskFlag)
  518. {
  519. FLEXIO_Type *Base;
  520. Flexio_Uart_Ip_StateStructureType * UartState;
  521. const Flexio_Uart_Ip_UserConfigType *UartUserCfg;
  522. UartState = (Flexio_Uart_Ip_StateStructureType *)Flexio_Uart_Ip_apStateStructuresArray[Channel];
  523. UartUserCfg = Flexio_Uart_Ip_apUserConfig[Channel];
  524. Base = Flexio_Uart_Ip_apBases[FLEXIO_HW_INSTANCE];
  525. uint32 ResourceMask = (1UL << Channel);
  526. boolean IsReturn = FALSE;
  527. /* No need to check for Tx underflow since timer is controlled by the shifter status flag */
  528. /* Check for transfer end */
  529. if (0U == UartState->RemainingBytes)
  530. {
  531. if (((uint32)TimerMaskFlag & ResourceMask) != 0U)
  532. {
  533. /*In case the Interrupt is spurious and the ISR shall only clear the interrupt status flag and return immediately*/
  534. if (!Flexio_Uart_Ip_GetTimerInterrupt(Base,TX_TIMER(Channel)))
  535. {
  536. /* Clear timer status */
  537. Flexio_Mcl_Ip_ClearTimerStatus(Base, TX_TIMER(Channel));
  538. IsReturn = TRUE;
  539. }
  540. if(!IsReturn)
  541. {
  542. UartState->TxFlush--;
  543. if (0U == UartState->TxFlush)
  544. {
  545. /* Clear Shifter status for transmit mode*/
  546. Flexio_Mcl_Ip_ClearShifterStatus(Base, TX_SHIFTER(Channel));
  547. /* Done flushing the Tx buffer, end transfer */
  548. /* Record success if there was no error */
  549. if (FLEXIO_UART_IP_STATUS_BUSY == UartState->Status)
  550. {
  551. UartState->Status = FLEXIO_UART_IP_STATUS_SUCCESS;
  552. }
  553. Flexio_Uart_Ip_StopTransfer(Channel);
  554. /* Call callback to announce the end transfer Event to the user */
  555. if (UartUserCfg->Callback != NULL_PTR)
  556. {
  557. UartUserCfg->Callback(Channel, FLEXIO_UART_IP_EVENT_END_TRANSFER, UartUserCfg->CallbackParam);
  558. }
  559. }
  560. else if (Flexio_Mcl_Ip_GetShifterStatus(Base, TX_SHIFTER(Channel)))
  561. {
  562. /* TxFlush == 1, but last byte was already transferred from buffer to shifter. There is a
  563. danger that the transmission is over and we end up never reporting the end Event.
  564. To avoid this, send one extra dummy byte */
  565. /* Set start bit to 1 and send an 0xFF byte, this way the line will appear idle */
  566. Flexio_Uart_Ip_SetShifterStartBit(Base, TX_SHIFTER(Channel), FLEXIO_SHIFTER_START_BIT_1);
  567. /* Writes data in the specified shifter buffer.*/
  568. Flexio_Uart_Ip_WriteShifterBuffer(Base, TX_SHIFTER(Channel), 0xFFFFFFFFUL);
  569. }
  570. else
  571. {
  572. /* TxFlush == 1, and last byte was not yet transferred from buffer to shifter.
  573. No need to do anything, just wait for the next timer Event. */
  574. }
  575. }
  576. }
  577. }
  578. /* Check if transmitter needs more data */
  579. else if ((((uint32)ShifterMaskFlag & ResourceMask) != 0U) && (UartState->RemainingBytes > 0U))
  580. {
  581. /*In case the Interrupt is spurious and the ISR shall only clear the interrupt status flag and return immediately*/
  582. if (!Flexio_Uart_Ip_GetShifterInterrupt(Base, TX_SHIFTER(Channel)))
  583. {
  584. /* Clear Shifter status for transmit mode*/
  585. Flexio_Mcl_Ip_ClearShifterStatus(Base, TX_SHIFTER(Channel));
  586. }
  587. else
  588. {
  589. Flexio_Uart_Ip_WriteData(Channel);
  590. Flexio_Uart_Ip_CheckCompleteTransferData(Channel);
  591. }
  592. }
  593. else
  594. {
  595. /* No relevant Events - nothing to do */
  596. }
  597. }
  598. /*FUNCTION**********************************************************************
  599. *
  600. * Function Name : Flexio_Uart_Ip_CheckCompleteTransferData
  601. * Description : Setup User Callback, Clear Timer Status, Setup Shifter, Timer
  602. * Interrupt when all remaning bytes are transmited.
  603. *
  604. *END**************************************************************************/
  605. static void Flexio_Uart_Ip_CheckCompleteTransferData(const uint8 Channel)
  606. {
  607. FLEXIO_Type *Base;
  608. const Flexio_Uart_Ip_StateStructureType * UartState;
  609. const Flexio_Uart_Ip_UserConfigType *UartUserCfg;
  610. UartState = (Flexio_Uart_Ip_StateStructureType *)Flexio_Uart_Ip_apStateStructuresArray[Channel];
  611. UartUserCfg = Flexio_Uart_Ip_apUserConfig[Channel];
  612. Base = Flexio_Uart_Ip_apBases[FLEXIO_HW_INSTANCE];
  613. if (0U == UartState->RemainingBytes)
  614. {
  615. /* Out of data, call callback to allow user to provide a new buffer */
  616. if (UartUserCfg->Callback != NULL_PTR)
  617. {
  618. UartUserCfg->Callback(Channel, FLEXIO_UART_IP_EVENT_TX_EMPTY, UartUserCfg->CallbackParam);
  619. }
  620. }
  621. if (0U == UartState->RemainingBytes)
  622. {
  623. /* No more data, transmission will stop after the last bytes are sent.
  624. The timer Event will indicate when the send is complete */
  625. /* Clear any previous timer events */
  626. Flexio_Mcl_Ip_ClearTimerStatus(Base, TX_TIMER(Channel));
  627. if (FLEXIO_UART_IP_DRIVER_TYPE_INTERRUPTS == UartUserCfg->DriverType)
  628. {
  629. /* Transmission completed; disable interrupt */
  630. Flexio_Mcl_Ip_SetShifterInterrupt(Base, (uint8)(1U << TX_SHIFTER(Channel)), FALSE);
  631. /* Enable timer interrupt to ensure that transfer is completed */
  632. Flexio_Mcl_Ip_SetTimerInterrupt(Base, (uint8)(1U << TX_TIMER(Channel)), TRUE);
  633. }
  634. }
  635. }
  636. /*FUNCTION**********************************************************************
  637. *
  638. * Function Name : Flexio_Uart_Ip_ReadData
  639. * Description : reads data received by the module
  640. *
  641. *END**************************************************************************/
  642. static void Flexio_Uart_Ip_ReadData(const uint8 Channel)
  643. {
  644. const FLEXIO_Type *Base;
  645. uint32 Data32b;
  646. Flexio_Uart_Ip_StateStructureType * UartState;
  647. const Flexio_Uart_Ip_UserConfigType *UartUserCfg;
  648. UartState = (Flexio_Uart_Ip_StateStructureType *)Flexio_Uart_Ip_apStateStructuresArray[Channel];
  649. UartUserCfg = Flexio_Uart_Ip_apUserConfig[Channel];
  650. FLEXIO_UART_IP_DEV_ASSERT(UartState->RemainingBytes > 0U);
  651. FLEXIO_UART_IP_DEV_ASSERT(UartState->RxData != NULL_PTR);
  652. Base = Flexio_Uart_Ip_apBases[FLEXIO_HW_INSTANCE];
  653. /* Read data from shifter buffer */
  654. Data32b = Flexio_Uart_Ip_ReadShifterBuffer(Base, RX_SHIFTER(Channel));
  655. Data32b >>= 32U - (uint32)(UartUserCfg->BitCount);
  656. if (FLEXIO_UART_IP_8_BITS_PER_CHAR == UartUserCfg->BitCount)
  657. {
  658. *UartState->RxData = (uint8)Data32b;
  659. /* Update rx buffer pointer and remaining bytes count */
  660. UartState->RxData ++;
  661. UartState->RemainingBytes -= 1U;
  662. }
  663. }
  664. /*FUNCTION**********************************************************************
  665. *
  666. * Function Name : Flexio_Uart_Ip_CheckRxOperation
  667. * Description : Check status of the UART reception. This function can be
  668. * called either in an interrupt routine or directly in polling
  669. * mode to advance the transfer.
  670. *
  671. *END**************************************************************************/
  672. static void Flexio_Uart_Ip_CheckRxOperation(const uint8 Channel,
  673. const uint8 ShifterMaskFlag,
  674. const uint8 ShifterErrMaskFlag)
  675. {
  676. FLEXIO_Type * Base;
  677. Flexio_Uart_Ip_StateStructureType * UartState;
  678. const Flexio_Uart_Ip_UserConfigType *UartUserCfg;
  679. UartState = (Flexio_Uart_Ip_StateStructureType *)Flexio_Uart_Ip_apStateStructuresArray[Channel];
  680. UartUserCfg = Flexio_Uart_Ip_apUserConfig[Channel];
  681. Base = Flexio_Uart_Ip_apBases[FLEXIO_HW_INSTANCE];
  682. uint32 ResourceMask = (1UL << Channel);
  683. boolean IsReturn = FALSE;
  684. /* Check for errors */
  685. if (((uint32)ShifterErrMaskFlag & ResourceMask) != 0U)
  686. {
  687. /*In case the Interrupt is spurious and the ISR shall only clear the interrupt status flag and return immediately*/
  688. if (!Flexio_Uart_Ip_GetShifterErrorInterrupt(Base, RX_SHIFTER(Channel)))
  689. {
  690. /*Clear Error Status flag*/
  691. Flexio_Mcl_Ip_ClearShifterErrorStatus(Base, RX_SHIFTER(Channel));
  692. IsReturn = TRUE;
  693. }
  694. else
  695. {
  696. UartState->Status = FLEXIO_UART_IP_STATUS_RX_OVERRUN;
  697. UartState->RemainingBytes = 0U;
  698. /* Invoke callback if there is one */
  699. if (UartUserCfg->Callback != NULL_PTR)
  700. {
  701. UartUserCfg->Callback(Channel, FLEXIO_UART_IP_EVENT_ERROR, UartUserCfg->CallbackParam);
  702. }
  703. /* Continue processing Events */
  704. }
  705. }
  706. /* Check if data was received */
  707. else if ((((uint32)ShifterMaskFlag & ResourceMask) != 0U))
  708. {
  709. /*In case the Interrupt is spurious and the ISR shall only clear the interrupt status flag and return immediately*/
  710. if (!Flexio_Uart_Ip_GetShifterInterrupt(Base, RX_SHIFTER(Channel)))
  711. {
  712. /*Clear Shifter Status flag*/
  713. Flexio_Mcl_Ip_ClearShifterStatus(Base, RX_SHIFTER(Channel));
  714. IsReturn = TRUE;;
  715. }
  716. else
  717. {
  718. Flexio_Uart_Ip_ReadData(Channel);
  719. if (0U == UartState->RemainingBytes)
  720. {
  721. /* Out of data, call callback to allow user to provide a new buffer */
  722. if (UartUserCfg->Callback != NULL_PTR)
  723. {
  724. UartUserCfg->Callback(Channel, FLEXIO_UART_IP_EVENT_RX_FULL, UartUserCfg->CallbackParam);
  725. }
  726. }
  727. }
  728. }
  729. else
  730. {
  731. /* No Events - nothing to do */
  732. }
  733. /* Check if transfer is over */
  734. if ((0U == UartState->RemainingBytes) && (!IsReturn))
  735. {
  736. /* Record success if there was no error */
  737. if (FLEXIO_UART_IP_STATUS_BUSY == UartState->Status)
  738. {
  739. UartState->Status = FLEXIO_UART_IP_STATUS_SUCCESS;
  740. }
  741. /* Discard any leftover rx data */
  742. Flexio_Mcl_Ip_ClearShifterStatus(Base, RX_SHIFTER(Channel));
  743. /* End transfer */
  744. Flexio_Uart_Ip_StopTransfer(Channel);
  745. /* Call callback to announce the Event to the user */
  746. if (UartUserCfg->Callback != NULL_PTR)
  747. {
  748. UartUserCfg->Callback(Channel, FLEXIO_UART_IP_EVENT_END_TRANSFER, UartUserCfg->CallbackParam);
  749. }
  750. }
  751. }
  752. /*FUNCTION**********************************************************************
  753. *
  754. * Function Name : Flexio_Uart_Ip_CheckShifterErrorStatus
  755. * Description : Checking Shifter Error Status and clear it if an error occured
  756. *
  757. *END**************************************************************************/
  758. static void Flexio_Uart_Ip_CheckShifterErrorStatus(const uint8 Channel)
  759. {
  760. FLEXIO_Type * Base;
  761. Flexio_Uart_Ip_StateStructureType * UartState;
  762. Base = Flexio_Uart_Ip_apBases[FLEXIO_HW_INSTANCE];
  763. UartState = (Flexio_Uart_Ip_StateStructureType *)Flexio_Uart_Ip_apStateStructuresArray[Channel];
  764. if (Flexio_Mcl_Ip_GetShifterErrorStatus(Base, RX_SHIFTER(Channel)))
  765. {
  766. UartState->Status = FLEXIO_UART_IP_STATUS_ERROR;
  767. Flexio_Mcl_Ip_ClearShifterErrorStatus(Base, RX_SHIFTER(Channel));
  768. }
  769. }
  770. /*FUNCTION**********************************************************************
  771. *
  772. * Function Name : Flexio_Uart_Ip_CheckShifterErrorStatus
  773. * Description : Disable interrupts for shifter
  774. *
  775. *END**************************************************************************/
  776. static void Flexio_Uart_Ip_SetShifterInterrupt(const uint8 Channel, boolean Enable)
  777. {
  778. FLEXIO_Type * Base;
  779. Base = Flexio_Uart_Ip_apBases[FLEXIO_HW_INSTANCE];
  780. Flexio_Mcl_Ip_SetShifterInterrupt(Base, (uint8)(1U << Channel), Enable);
  781. Flexio_Mcl_Ip_SetShifterErrorInterrupt(Base, (uint8)(1U << Channel), Enable);
  782. }
  783. /*FUNCTION**********************************************************************
  784. *
  785. * Function Name : Flexio_Uart_Ip_SetupReceiveData
  786. * Description : Disable interrupts for Rx Shifter and enable timer
  787. * and shifter to receiver data.
  788. *
  789. *END**************************************************************************/
  790. static void Flexio_Uart_Ip_SetupReceiveData(const uint8 Channel)
  791. {
  792. /* Disable interrupts for Rx shifter */
  793. Flexio_Uart_Ip_SetShifterInterrupt(Channel, FALSE);
  794. /* Enable timers and shifters for receive data */
  795. Flexio_Uart_Ip_EnableReceiver(Channel);
  796. }
  797. /*FUNCTION**********************************************************************
  798. *
  799. * Function Name : Flexio_Uart_Ip_EnableReceiver
  800. * Description : Enables timers and shifters to start receive data.
  801. *
  802. *END**************************************************************************/
  803. static void Flexio_Uart_Ip_EnableReceiver(const uint8 Channel)
  804. {
  805. FLEXIO_Type * Base;
  806. Base = Flexio_Uart_Ip_apBases[FLEXIO_HW_INSTANCE];
  807. /* Enable timers and shifters and discard any leftover rx data*/
  808. Flexio_Mcl_Ip_ClearShifterStatus(Base, RX_SHIFTER(Channel));
  809. Flexio_Uart_Ip_SetShifterMode(Base, RX_SHIFTER(Channel), FLEXIO_SHIFTER_MODE_RECEIVE);
  810. Flexio_Uart_Ip_SetTimerMode(Base, RX_TIMER(Channel), FLEXIO_TIMER_MODE_8BIT_BAUD);
  811. }
  812. #if (FLEXIO_UART_IP_HAS_DMA_ENABLED == STD_ON)
  813. /*FUNCTION**********************************************************************
  814. *
  815. * Function Name : Flexio_Uart_Ip_StartSendDataUsingDma
  816. * Description : Initiate (start) a transmit by beginning the process of
  817. * sending data using DMA transfers.
  818. * This is not a public API as it is called from other driver functions.
  819. *
  820. *END**************************************************************************/
  821. static Flexio_Uart_Ip_StatusType Flexio_Uart_Ip_StartSendDataUsingDma(uint8 Channel,
  822. const uint8 * TxData,
  823. uint32 TxSize)
  824. {
  825. Flexio_Uart_Ip_StateStructureType * UartState;
  826. const Flexio_Uart_Ip_UserConfigType *UserConfig;
  827. FLEXIO_Type * Base;
  828. Dma_Ip_LogicChannelTransferListType DmaTransferList[FLEXIO_UART_DMA_CONFIG_LIST_DIMENSION];
  829. Dma_Ip_ReturnType DmaReturnStatus;
  830. UartState = (Flexio_Uart_Ip_StateStructureType *)Flexio_Uart_Ip_apStateStructuresArray[Channel];
  831. UserConfig = Flexio_Uart_Ip_apUserConfig[Channel];
  832. Base = Flexio_Uart_Ip_apBases[FLEXIO_HW_INSTANCE];
  833. /* Set up parameters for Dma_Ip_LogicChannelTransferListType */
  834. DmaTransferList[0].Param = DMA_IP_CH_SET_SOURCE_ADDRESS;
  835. DmaTransferList[0].Value = (uint32)TxData;
  836. DmaTransferList[1].Param = DMA_IP_CH_SET_DESTINATION_ADDRESS;
  837. DmaTransferList[1].Value = FLEXIO_UART_IP_LSBW_ADDR(Base->SHIFTBUF[Channel]);
  838. DmaTransferList[2].Param = DMA_IP_CH_SET_SOURCE_SIGNED_OFFSET;
  839. DmaTransferList[2].Value = 1;
  840. DmaTransferList[3].Param = DMA_IP_CH_SET_DESTINATION_SIGNED_OFFSET;
  841. DmaTransferList[3].Value = 0;
  842. DmaTransferList[4].Param = DMA_IP_CH_SET_MAJORLOOP_COUNT;
  843. DmaTransferList[4].Value = TxSize;
  844. DmaTransferList[5].Param = DMA_IP_CH_SET_MINORLOOP_SIZE;
  845. DmaTransferList[5].Value = 1;
  846. DmaTransferList[6].Param = DMA_IP_CH_SET_DESTINATION_TRANSFER_SIZE;
  847. DmaTransferList[6].Value = DMA_IP_TRANSFER_SIZE_1_BYTE;
  848. DmaTransferList[7].Param = DMA_IP_CH_SET_SOURCE_TRANSFER_SIZE;
  849. DmaTransferList[7].Value = DMA_IP_TRANSFER_SIZE_1_BYTE;
  850. DmaTransferList[8].Param = DMA_IP_CH_SET_CONTROL_EN_MAJOR_INTERRUPT;
  851. DmaTransferList[8].Value = 1;
  852. DmaTransferList[9].Param = DMA_IP_CH_SET_CONTROL_DIS_AUTO_REQUEST;
  853. DmaTransferList[9].Value = 1;
  854. /* Update state structure */
  855. UartState->TxData = TxData;
  856. UartState->RemainingBytes = 0U;
  857. UartState->Status = FLEXIO_UART_IP_STATUS_BUSY;
  858. /* Configure the transfer control descriptor for the DMA channel */
  859. DmaReturnStatus = Dma_Ip_SetLogicChannelTransferList(UserConfig->DmaChannel, DmaTransferList, FLEXIO_UART_DMA_CONFIG_LIST_DIMENSION);
  860. FLEXIO_UART_IP_DEV_ASSERT(DMA_IP_STATUS_SUCCESS == DmaReturnStatus);
  861. /* Start the DMA channel */
  862. DmaReturnStatus = Dma_Ip_SetLogicChannelCommand(UserConfig->DmaChannel, DMA_IP_CH_SET_HARDWARE_REQUEST);
  863. FLEXIO_UART_IP_DEV_ASSERT(DMA_IP_STATUS_SUCCESS == DmaReturnStatus);
  864. /* Enable FlexIO DMA requests */
  865. Flexio_Mcl_Ip_SetShifterDMARequest(Base, (uint8)(1U << TX_SHIFTER(Channel)), TRUE);
  866. return FLEXIO_UART_IP_STATUS_SUCCESS;
  867. }
  868. /*FUNCTION**********************************************************************
  869. *
  870. * Function Name : Flexio_Uart_Ip_StartReceiveDataUsingDma
  871. * Description : Initiate (start) a transmit by beginning the process of
  872. * receiving data using DMA transfers.
  873. * This is not a public API as it is called from other driver functions.
  874. *
  875. *END**************************************************************************/
  876. static Flexio_Uart_Ip_StatusType Flexio_Uart_Ip_StartReceiveDataUsingDma(uint8 Channel,
  877. uint8 * RxBuff,
  878. uint32 RxSize)
  879. {
  880. Flexio_Uart_Ip_StateStructureType * UartState;
  881. const Flexio_Uart_Ip_UserConfigType *UserConfig;
  882. FLEXIO_Type * Base;
  883. Dma_Ip_LogicChannelTransferListType DmaTransferList[FLEXIO_UART_DMA_CONFIG_LIST_DIMENSION];
  884. Dma_Ip_ReturnType DmaReturnStatus;
  885. UartState = (Flexio_Uart_Ip_StateStructureType *)Flexio_Uart_Ip_apStateStructuresArray[Channel];
  886. UserConfig = Flexio_Uart_Ip_apUserConfig[Channel];
  887. Base = Flexio_Uart_Ip_apBases[FLEXIO_HW_INSTANCE];
  888. /* Set up parameters for Dma_Ip_LogicChannelTransferListType */
  889. DmaTransferList[0].Param = DMA_IP_CH_SET_SOURCE_ADDRESS;
  890. DmaTransferList[0].Value = FLEXIO_UART_IP_LSBW_ADDR(Base->SHIFTBUF[Channel]) + 3U;
  891. DmaTransferList[1].Param = DMA_IP_CH_SET_DESTINATION_ADDRESS;
  892. DmaTransferList[1].Value = (uint32)RxBuff;
  893. DmaTransferList[2].Param = DMA_IP_CH_SET_SOURCE_SIGNED_OFFSET;
  894. DmaTransferList[2].Value = 0;
  895. DmaTransferList[3].Param = DMA_IP_CH_SET_DESTINATION_SIGNED_OFFSET;
  896. DmaTransferList[3].Value = 1;
  897. DmaTransferList[4].Param = DMA_IP_CH_SET_MAJORLOOP_COUNT;
  898. DmaTransferList[4].Value = RxSize;
  899. DmaTransferList[5].Param = DMA_IP_CH_SET_MINORLOOP_SIZE;
  900. DmaTransferList[5].Value = 1;
  901. DmaTransferList[6].Param = DMA_IP_CH_SET_DESTINATION_TRANSFER_SIZE;
  902. DmaTransferList[6].Value = DMA_IP_TRANSFER_SIZE_1_BYTE;
  903. DmaTransferList[7].Param = DMA_IP_CH_SET_SOURCE_TRANSFER_SIZE;
  904. DmaTransferList[7].Value = DMA_IP_TRANSFER_SIZE_1_BYTE;
  905. DmaTransferList[8].Param = DMA_IP_CH_SET_CONTROL_EN_MAJOR_INTERRUPT;
  906. DmaTransferList[8].Value = 1;
  907. DmaTransferList[9].Param = DMA_IP_CH_SET_CONTROL_DIS_AUTO_REQUEST;
  908. DmaTransferList[9].Value = 1;
  909. /* Update state structure */
  910. UartState->RxData = RxBuff;
  911. UartState->RemainingBytes = 0U;
  912. UartState->Status = FLEXIO_UART_IP_STATUS_BUSY;
  913. /* Configure the transfer control descriptor for the DMA channel */
  914. DmaReturnStatus = Dma_Ip_SetLogicChannelTransferList(UserConfig->DmaChannel, DmaTransferList, FLEXIO_UART_DMA_CONFIG_LIST_DIMENSION);
  915. FLEXIO_UART_IP_DEV_ASSERT(DMA_IP_STATUS_SUCCESS == DmaReturnStatus);
  916. /* Start the DMA channel */
  917. DmaReturnStatus = Dma_Ip_SetLogicChannelCommand(UserConfig->DmaChannel, DMA_IP_CH_SET_HARDWARE_REQUEST);
  918. FLEXIO_UART_IP_DEV_ASSERT(DMA_IP_STATUS_SUCCESS == DmaReturnStatus);
  919. /* Enable FlexIO DMA requests */
  920. Flexio_Mcl_Ip_SetShifterDMARequest(Base, (uint8)(1U << RX_SHIFTER(Channel)), TRUE);
  921. return FLEXIO_UART_IP_STATUS_SUCCESS;
  922. }
  923. #endif
  924. /*==================================================================================================
  925. * GLOBAL FUNCTIONS
  926. ==================================================================================================*/
  927. /*FUNCTION**********************************************************************
  928. *
  929. * Function Name : Flexio_Uart_Ip_Init
  930. * Description : Initialize the FLEXIO_UART driver
  931. * Implements : Flexio_Uart_Ip_Init_Activity
  932. *
  933. *END**************************************************************************/
  934. /* implements Flexio_Uart_Ip_Init_Activity*/
  935. void Flexio_Uart_Ip_Init(const uint8 Channel,
  936. const Flexio_Uart_Ip_UserConfigType * UserConfig)
  937. {
  938. Flexio_Uart_Ip_StateStructureType *UartStatePtr;
  939. UartStatePtr = UserConfig->StateStruct;
  940. FLEXIO_UART_IP_DEV_ASSERT(NULL_PTR == Flexio_Uart_Ip_apStateStructuresArray[Channel]);
  941. FLEXIO_UART_IP_DEV_ASSERT(Channel < FLEXIO_UART_IP_NUMBER_OF_SHIFTER_AND_TIMER);
  942. FLEXIO_UART_IP_DEV_ASSERT(Channel == UserConfig->Channel);
  943. Flexio_Uart_Ip_apStateStructuresArray[Channel] = UserConfig->StateStruct;
  944. Flexio_Uart_Ip_apUserConfig[Channel] = UserConfig;
  945. /* Initialize driver-specific context structure */
  946. UartStatePtr->RxData = NULL_PTR;
  947. UartStatePtr->TxData = NULL_PTR;
  948. UartStatePtr->RemainingBytes = 0U;
  949. UartStatePtr->BaudRate = UserConfig->BaudRate;
  950. UartStatePtr->Status = FLEXIO_UART_IP_STATUS_SUCCESS;
  951. UartStatePtr->DriverIdle = FALSE;
  952. if (FLEXIO_UART_IP_DIRECTION_TX == UserConfig->Direction)
  953. {
  954. /* Configure device for UART Tx mode */
  955. Flexio_Uart_Ip_ConfigureTx(Channel,
  956. UserConfig->BitCount,
  957. UserConfig->Divider,
  958. UserConfig->DataPin,
  959. UserConfig->TimerDec);
  960. }
  961. else
  962. {
  963. /* Configure device for UART Rx mode */
  964. Flexio_Uart_Ip_ConfigureRx(Channel,
  965. UserConfig->BitCount,
  966. UserConfig->Divider,
  967. UserConfig->DataPin,
  968. UserConfig->TimerDec);
  969. }
  970. }
  971. /*FUNCTION**********************************************************************
  972. *
  973. * Function Name : Flexio_Uart_Ip_GetBaudRate
  974. * Description : Returns the FLEXIO configured baud rate.
  975. *
  976. * Implements : Flexio_Uart_Ip_GetBaudRate_Activity
  977. *END**************************************************************************/
  978. /* implements Flexio_Uart_Ip_GetBaudRate_Activity*/
  979. void Flexio_Uart_Ip_GetBaudRate(const uint8 Channel, uint32 * BaudRate)
  980. {
  981. FLEXIO_UART_IP_DEV_ASSERT(Channel < FLEXIO_UART_IP_NUMBER_OF_SHIFTER_AND_TIMER);
  982. FLEXIO_UART_IP_DEV_ASSERT(BaudRate != NULL_PTR);
  983. const Flexio_Uart_Ip_StateStructureType * UartState;
  984. UartState = (Flexio_Uart_Ip_StateStructureType *)Flexio_Uart_Ip_apStateStructuresArray[Channel];
  985. *BaudRate = UartState->BaudRate;
  986. }
  987. /*FUNCTION**********************************************************************
  988. *
  989. * Function Name : Flexio_Uart_Ip_Deinit
  990. * Description : De-initialize the FLEXIO_UART driver
  991. * Implements : Flexio_Uart_Ip_Deinit_Activity
  992. *
  993. *END**************************************************************************/
  994. /* implements Flexio_Uart_Ip_Deinit_Activity*/
  995. void Flexio_Uart_Ip_Deinit(const uint8 Channel)
  996. {
  997. FLEXIO_UART_IP_DEV_ASSERT(Channel < FLEXIO_UART_IP_NUMBER_OF_SHIFTER_AND_TIMER);
  998. uint32 StartTime;
  999. uint32 TimeoutTicks;
  1000. uint32 ElapsedTicks = 0;
  1001. FLEXIO_Type *Base;
  1002. Base = Flexio_Uart_Ip_apBases[FLEXIO_HW_INSTANCE];
  1003. FLEXIO_UART_IP_DEV_ASSERT(Flexio_Uart_Ip_apStateStructuresArray[Channel] != NULL_PTR);
  1004. Flexio_Uart_Ip_StartTimeout(&StartTime, &TimeoutTicks, FLEXIO_UART_IP_TIMEOUT_VALUE_US, FLEXIO_UART_IP_TIMEOUT_TYPE);
  1005. /* Wait until the data is completely shifted out of shift register */
  1006. while ((FLEXIO_UART_IP_STATUS_BUSY == Flexio_Uart_Ip_GetStatus(Channel, NULL_PTR)) && \
  1007. !Flexio_Uart_Ip_CheckTimeout(&StartTime, &ElapsedTicks, TimeoutTicks, FLEXIO_UART_IP_TIMEOUT_TYPE))
  1008. {}
  1009. /* Clear our saved pointer to the state structure */
  1010. Flexio_Uart_Ip_apStateStructuresArray[Channel] = NULL_PTR;
  1011. /* Disable interrupts for Tx or Rx shifter */
  1012. Flexio_Uart_Ip_SetShifterInterrupt(Channel, FALSE);
  1013. /* Reset FlexIO module */
  1014. Flexio_Mcl_Ip_SetSoftwareReset(Base, TRUE);
  1015. /* Clear reset bit */
  1016. Flexio_Mcl_Ip_SetSoftwareReset(Base, FALSE);
  1017. }
  1018. /*FUNCTION**********************************************************************
  1019. *
  1020. * Function Name : Flexio_Uart_Ip_AsyncSend
  1021. * Description : Perform a Async UART transmission
  1022. * Implements : Flexio_Uart_Ip_AsyncSendData_Activity
  1023. *
  1024. *END**************************************************************************/
  1025. /* implements Flexio_Uart_Ip_AsyncSend_Activity*/
  1026. Flexio_Uart_Ip_StatusType Flexio_Uart_Ip_AsyncSend(const uint8 Channel,
  1027. const uint8 * TxBuff,
  1028. const uint32 TxSize)
  1029. {
  1030. FLEXIO_UART_IP_DEV_ASSERT(Channel < FLEXIO_UART_IP_NUMBER_OF_SHIFTER_AND_TIMER);
  1031. FLEXIO_Type *Base;
  1032. Flexio_Uart_Ip_StateStructureType * UartState;
  1033. const Flexio_Uart_Ip_UserConfigType *UartUserCfg;
  1034. UartState = (Flexio_Uart_Ip_StateStructureType *)Flexio_Uart_Ip_apStateStructuresArray[Channel];
  1035. UartUserCfg = Flexio_Uart_Ip_apUserConfig[Channel];
  1036. Flexio_Uart_Ip_StatusType RetStatus = FLEXIO_UART_IP_STATUS_ERROR;
  1037. FLEXIO_UART_IP_DEV_ASSERT(UartState != NULL_PTR);
  1038. FLEXIO_UART_IP_DEV_ASSERT(TxBuff != NULL_PTR);
  1039. FLEXIO_UART_IP_DEV_ASSERT(TxSize > 0U);
  1040. FLEXIO_UART_IP_DEV_ASSERT(FLEXIO_UART_IP_DIRECTION_TX == UartUserCfg->Direction);
  1041. /* If 2 bytes per word are needed, then the size must be even */
  1042. FLEXIO_UART_IP_DEV_ASSERT((UartUserCfg->BitCount <= FLEXIO_UART_IP_8_BITS_PER_CHAR) || ((TxSize & 1U) == 0U));
  1043. /* Check if driver is busy */
  1044. SchM_Enter_Uart_UART_EXCLUSIVE_AREA_04();
  1045. if (UartState->DriverIdle)
  1046. {
  1047. SchM_Exit_Uart_UART_EXCLUSIVE_AREA_04();
  1048. RetStatus = FLEXIO_UART_IP_STATUS_BUSY;
  1049. }
  1050. else
  1051. {
  1052. UartState->DriverIdle = TRUE;
  1053. SchM_Exit_Uart_UART_EXCLUSIVE_AREA_04();
  1054. Base = Flexio_Uart_Ip_apBases[FLEXIO_HW_INSTANCE];
  1055. UartState->TxData = TxBuff;
  1056. UartState->RemainingBytes = TxSize;
  1057. UartState->Status = FLEXIO_UART_IP_STATUS_BUSY;
  1058. /* Number of bytes to flush after the last byte is copied in the tx shifter buffer */
  1059. UartState->TxFlush = (uint8)((1U == TxSize) ? 1U : 2U);
  1060. /* Enable timers for sending data */
  1061. Flexio_Uart_Ip_SetTimerMode(Base, TX_TIMER(Channel), FLEXIO_TIMER_MODE_8BIT_BAUD);
  1062. /* Enable transfer engine */
  1063. switch (UartUserCfg->DriverType)
  1064. {
  1065. case FLEXIO_UART_IP_DRIVER_TYPE_INTERRUPTS:
  1066. /* Enable interrupts for Tx shifter */
  1067. Flexio_Uart_Ip_SetShifterInterrupt(Channel, TRUE);
  1068. RetStatus = FLEXIO_UART_IP_STATUS_SUCCESS;
  1069. break;
  1070. #if (FLEXIO_UART_IP_HAS_DMA_ENABLED == STD_ON)
  1071. case FLEXIO_UART_IP_DRIVER_TYPE_DMA:
  1072. RetStatus = Flexio_Uart_Ip_StartSendDataUsingDma(Channel, TxBuff, TxSize);
  1073. break;
  1074. #endif
  1075. default:
  1076. /* Impossible type - do nothing */
  1077. break;
  1078. }
  1079. }
  1080. return RetStatus;
  1081. }
  1082. /*FUNCTION**********************************************************************
  1083. *
  1084. * Function Name : Flexio_Uart_Ip_GetStatus
  1085. * Description : Get the status of the current non-blocking UART transaction
  1086. * Implements : Flexio_Uart_Ip_GetStatus_Activity
  1087. *
  1088. *END**************************************************************************/
  1089. Flexio_Uart_Ip_StatusType Flexio_Uart_Ip_GetStatus(const uint8 Channel, uint32 *BytesRemaining)
  1090. {
  1091. FLEXIO_UART_IP_DEV_ASSERT(Channel < FLEXIO_UART_IP_NUMBER_OF_SHIFTER_AND_TIMER);
  1092. const Flexio_Uart_Ip_StateStructureType * UartState;
  1093. const Flexio_Uart_Ip_UserConfigType *UartUserCfg;
  1094. UartState = (Flexio_Uart_Ip_StateStructureType *)Flexio_Uart_Ip_apStateStructuresArray[Channel];
  1095. UartUserCfg = Flexio_Uart_Ip_apUserConfig[Channel];
  1096. #if (FLEXIO_UART_IP_HAS_DMA_ENABLED == STD_ON)
  1097. const Dma_Ip_LogicChannelInfoParamType DmaLogicChnParam = DMA_IP_CH_GET_CURRENT_ITER_COUNT;
  1098. #endif
  1099. FLEXIO_UART_IP_DEV_ASSERT(UartState != NULL_PTR);
  1100. if (BytesRemaining != NULL_PTR)
  1101. {
  1102. if (UartState->DriverIdle)
  1103. {
  1104. /* Fill in the bytes not transferred yet. */
  1105. if (FLEXIO_UART_IP_DRIVER_TYPE_INTERRUPTS == UartUserCfg->DriverType)
  1106. {
  1107. *BytesRemaining = UartState->RemainingBytes;
  1108. }
  1109. #if (FLEXIO_UART_IP_HAS_DMA_ENABLED == STD_ON)
  1110. else
  1111. {
  1112. /* In DMA-based communication, the remaining bytes are retrieved
  1113. * from the current DMA major loop count
  1114. */
  1115. (void)Dma_Ip_GetLogicChannelParam(UartUserCfg->DmaChannel, DmaLogicChnParam, BytesRemaining);
  1116. }
  1117. #endif
  1118. }
  1119. else
  1120. {
  1121. *BytesRemaining = 0;
  1122. }
  1123. }
  1124. return UartState->Status;
  1125. }
  1126. /*FUNCTION**********************************************************************
  1127. *
  1128. * Function Name : Flexio_Uart_Ip_SyncSend
  1129. * Description : Perform a non-blocking UART transmission
  1130. * Implements : Flexio_Uart_Ip_SyncSendData_Activity
  1131. *
  1132. *END**************************************************************************/
  1133. /* implements Flexio_Uart_Ip_SyncSend_Activity*/
  1134. Flexio_Uart_Ip_StatusType Flexio_Uart_Ip_SyncSend(const uint8 Channel,
  1135. const uint8 * TxBuff,
  1136. const uint32 TxSize,
  1137. const uint32 Timeout)
  1138. {
  1139. FLEXIO_UART_IP_DEV_ASSERT(Channel < FLEXIO_UART_IP_NUMBER_OF_SHIFTER_AND_TIMER);
  1140. FLEXIO_UART_IP_DEV_ASSERT(TxBuff != NULL_PTR);
  1141. FLEXIO_UART_IP_DEV_ASSERT(TxSize > 0U);
  1142. FLEXIO_Type *Base;
  1143. uint32 StartTime;
  1144. uint32 TimeoutTicks;
  1145. uint32 ElapsedTicks = 0;
  1146. Flexio_Uart_Ip_StateStructureType * UartState;
  1147. const Flexio_Uart_Ip_UserConfigType *UartUserCfg;
  1148. Flexio_Uart_Ip_StatusType RetStatus = FLEXIO_UART_IP_STATUS_ERROR;
  1149. UartState = (Flexio_Uart_Ip_StateStructureType *)Flexio_Uart_Ip_apStateStructuresArray[Channel];
  1150. UartUserCfg = Flexio_Uart_Ip_apUserConfig[Channel];
  1151. Base = Flexio_Uart_Ip_apBases[FLEXIO_HW_INSTANCE];
  1152. FLEXIO_UART_IP_DEV_ASSERT(UartState != NULL_PTR);
  1153. FLEXIO_UART_IP_DEV_ASSERT(FLEXIO_UART_IP_DIRECTION_TX == UartUserCfg->Direction);
  1154. /* If 2 bytes per word are needed, then the size must be even */
  1155. FLEXIO_UART_IP_DEV_ASSERT((UartUserCfg->BitCount <= FLEXIO_UART_IP_8_BITS_PER_CHAR) || ((TxSize & 1U) == 0U));
  1156. /* Check if driver is busy */
  1157. SchM_Enter_Uart_UART_EXCLUSIVE_AREA_05();
  1158. if (UartState->DriverIdle)
  1159. {
  1160. SchM_Exit_Uart_UART_EXCLUSIVE_AREA_05();
  1161. RetStatus = FLEXIO_UART_IP_STATUS_BUSY;
  1162. }
  1163. else
  1164. {
  1165. UartState->DriverIdle = TRUE;
  1166. SchM_Exit_Uart_UART_EXCLUSIVE_AREA_05();
  1167. UartState->TxData = TxBuff;
  1168. UartState->RemainingBytes = TxSize;
  1169. UartState->Status = FLEXIO_UART_IP_STATUS_BUSY;
  1170. /* Disable interrupts for Tx shifter */
  1171. Flexio_Uart_Ip_SetShifterInterrupt(Channel, FALSE);
  1172. /* Number of bytes to flush after the last byte is copied in the tx shifter buffer */
  1173. UartState->TxFlush = (uint8)((1U == TxSize) ? 1U : 2U);
  1174. /* Enable timers and shifters */
  1175. Flexio_Uart_Ip_SetTimerMode(Base, TX_TIMER(Channel), FLEXIO_TIMER_MODE_8BIT_BAUD);
  1176. Flexio_Uart_Ip_StartTimeout(&StartTime, &TimeoutTicks, Timeout, FLEXIO_UART_IP_TIMEOUT_TYPE);
  1177. while ((UartState->RemainingBytes > 0U) && \
  1178. !Flexio_Uart_Ip_CheckTimeout(&StartTime, &ElapsedTicks, TimeoutTicks, FLEXIO_UART_IP_TIMEOUT_TYPE))
  1179. {
  1180. Flexio_Uart_Ip_WriteData(Channel);
  1181. while (!Flexio_Mcl_Ip_GetShifterStatus(Base, Channel) && \
  1182. !Flexio_Uart_Ip_CheckTimeout(&StartTime, &ElapsedTicks, TimeoutTicks, FLEXIO_UART_IP_TIMEOUT_TYPE))
  1183. {}
  1184. }
  1185. /* Check if Timeout occur */
  1186. if (Flexio_Uart_Ip_CheckTimeout(&StartTime, &ElapsedTicks, TimeoutTicks, FLEXIO_UART_IP_TIMEOUT_TYPE))
  1187. {
  1188. UartState->Status = FLEXIO_UART_IP_STATUS_TIMEOUT;
  1189. }
  1190. /* Check other success receiving case*/
  1191. if (FLEXIO_UART_IP_STATUS_BUSY == UartState->Status)
  1192. {
  1193. UartState->Status = FLEXIO_UART_IP_STATUS_SUCCESS;
  1194. }
  1195. /* Clear any previous timer events */
  1196. Flexio_Mcl_Ip_ClearTimerStatus(Base, TX_TIMER(Channel));
  1197. UartState->DriverIdle = FALSE;
  1198. RetStatus = UartState->Status;
  1199. }
  1200. return RetStatus;
  1201. }
  1202. /*FUNCTION**********************************************************************
  1203. *
  1204. * Function Name : Flexio_Uart_Ip_AsyncReceive
  1205. * Description : Perform a non-blocking UART reception
  1206. * Implements : Flexio_Uart_Ip_AsyncReceive_Activity
  1207. *
  1208. *END**************************************************************************/
  1209. /* implements Flexio_Uart_Ip_AsyncReceive_Activity*/
  1210. Flexio_Uart_Ip_StatusType Flexio_Uart_Ip_AsyncReceive(const uint8 Channel,
  1211. uint8 * RxBuff,
  1212. const uint32 RxSize)
  1213. {
  1214. FLEXIO_UART_IP_DEV_ASSERT(Channel < FLEXIO_UART_IP_NUMBER_OF_SHIFTER_AND_TIMER);
  1215. Flexio_Uart_Ip_StateStructureType * UartState;
  1216. const Flexio_Uart_Ip_UserConfigType *UartUserCfg;
  1217. UartState = (Flexio_Uart_Ip_StateStructureType *)Flexio_Uart_Ip_apStateStructuresArray[Channel];
  1218. UartUserCfg = Flexio_Uart_Ip_apUserConfig[Channel];
  1219. Flexio_Uart_Ip_StatusType RetStatus = FLEXIO_UART_IP_STATUS_ERROR;
  1220. FLEXIO_UART_IP_DEV_ASSERT(UartState != NULL_PTR);
  1221. FLEXIO_UART_IP_DEV_ASSERT(RxBuff != NULL_PTR);
  1222. FLEXIO_UART_IP_DEV_ASSERT(RxSize > 0U);
  1223. FLEXIO_UART_IP_DEV_ASSERT(FLEXIO_UART_IP_DIRECTION_RX == UartUserCfg->Direction);
  1224. /* If 2 bytes per word are needed, then the size must be even */
  1225. FLEXIO_UART_IP_DEV_ASSERT((UartUserCfg->BitCount <= FLEXIO_UART_IP_8_BITS_PER_CHAR) || ((RxSize & 1U) == 0U));
  1226. /* Check if driver is busy */
  1227. SchM_Enter_Uart_UART_EXCLUSIVE_AREA_06();
  1228. if (UartState->DriverIdle)
  1229. {
  1230. SchM_Exit_Uart_UART_EXCLUSIVE_AREA_06();
  1231. RetStatus = FLEXIO_UART_IP_STATUS_BUSY;
  1232. }
  1233. else
  1234. {
  1235. UartState->DriverIdle = TRUE;
  1236. SchM_Exit_Uart_UART_EXCLUSIVE_AREA_06();
  1237. UartState->RxData = RxBuff;
  1238. UartState->RemainingBytes = RxSize;
  1239. UartState->Status = FLEXIO_UART_IP_STATUS_BUSY;
  1240. /* Enable timers and shifters for receive data */
  1241. Flexio_Uart_Ip_EnableReceiver(Channel);
  1242. /* Enable transfer engine */
  1243. switch (UartUserCfg->DriverType)
  1244. {
  1245. case FLEXIO_UART_IP_DRIVER_TYPE_INTERRUPTS:
  1246. /* Enable interrupts for Rx shifter */
  1247. Flexio_Uart_Ip_SetShifterInterrupt(Channel, TRUE);
  1248. RetStatus = FLEXIO_UART_IP_STATUS_SUCCESS;
  1249. break;
  1250. #if (FLEXIO_UART_IP_HAS_DMA_ENABLED == STD_ON)
  1251. case FLEXIO_UART_IP_DRIVER_TYPE_DMA:
  1252. RetStatus = Flexio_Uart_Ip_StartReceiveDataUsingDma(Channel, RxBuff, RxSize);
  1253. break;
  1254. #endif
  1255. default:
  1256. /* Impossible type - do nothing */
  1257. break;
  1258. }
  1259. }
  1260. return RetStatus;
  1261. }
  1262. /*FUNCTION**********************************************************************
  1263. *
  1264. * Function Name : Flexio_Uart_Ip_SyncReceive
  1265. * Description : Perform a non-blocking UART reception
  1266. * Implements : Flexio_Uart_Ip_SyncReceive_Activity
  1267. *
  1268. *END**************************************************************************/
  1269. /* implements Flexio_Uart_Ip_SyncReceive_Activity*/
  1270. Flexio_Uart_Ip_StatusType Flexio_Uart_Ip_SyncReceive(const uint8 Channel,
  1271. uint8 * RxBuff,
  1272. const uint32 RxSize,
  1273. const uint32 Timeout)
  1274. {
  1275. FLEXIO_UART_IP_DEV_ASSERT(Channel < FLEXIO_UART_IP_NUMBER_OF_SHIFTER_AND_TIMER);
  1276. FLEXIO_UART_IP_DEV_ASSERT(RxBuff != NULL_PTR);
  1277. FLEXIO_UART_IP_DEV_ASSERT(RxSize > 0U);
  1278. Flexio_Uart_Ip_StateStructureType * UartState;
  1279. const Flexio_Uart_Ip_UserConfigType *UartUserCfg;
  1280. uint32 StartTime;
  1281. uint32 TimeoutTicks;
  1282. uint32 ElapsedTicks = 0;
  1283. FLEXIO_Type *Base;
  1284. Base = Flexio_Uart_Ip_apBases[FLEXIO_HW_INSTANCE];
  1285. Flexio_Uart_Ip_StatusType RetStatus = FLEXIO_UART_IP_STATUS_ERROR;
  1286. UartState = (Flexio_Uart_Ip_StateStructureType *)Flexio_Uart_Ip_apStateStructuresArray[Channel];
  1287. UartUserCfg = Flexio_Uart_Ip_apUserConfig[Channel];
  1288. FLEXIO_UART_IP_DEV_ASSERT(UartState != NULL_PTR);
  1289. FLEXIO_UART_IP_DEV_ASSERT(FLEXIO_UART_IP_DIRECTION_RX == UartUserCfg->Direction);
  1290. /* If 2 bytes per word are needed, then the size must be even */
  1291. FLEXIO_UART_IP_DEV_ASSERT((UartUserCfg->BitCount <= FLEXIO_UART_IP_8_BITS_PER_CHAR) || ((RxSize & 1U) == 0U));
  1292. /* Check if driver is busy */
  1293. SchM_Enter_Uart_UART_EXCLUSIVE_AREA_07();
  1294. if (UartState->DriverIdle)
  1295. {
  1296. SchM_Exit_Uart_UART_EXCLUSIVE_AREA_07();
  1297. RetStatus = FLEXIO_UART_IP_STATUS_BUSY;
  1298. }
  1299. else
  1300. {
  1301. UartState->DriverIdle = TRUE;
  1302. SchM_Exit_Uart_UART_EXCLUSIVE_AREA_07();
  1303. UartState->RxData = RxBuff;
  1304. UartState->RemainingBytes = RxSize;
  1305. UartState->Status = FLEXIO_UART_IP_STATUS_BUSY;
  1306. Flexio_Uart_Ip_SetupReceiveData(Channel);
  1307. Flexio_Uart_Ip_StartTimeout(&StartTime, &TimeoutTicks, Timeout, FLEXIO_UART_IP_TIMEOUT_TYPE);
  1308. while ((UartState->RemainingBytes > 0U) && \
  1309. !Flexio_Uart_Ip_CheckTimeout(&StartTime, &ElapsedTicks, TimeoutTicks, FLEXIO_UART_IP_TIMEOUT_TYPE))
  1310. {
  1311. while (!Flexio_Mcl_Ip_GetShifterStatus(Base, Channel) && \
  1312. !Flexio_Uart_Ip_CheckTimeout(&StartTime, &ElapsedTicks, TimeoutTicks, FLEXIO_UART_IP_TIMEOUT_TYPE))
  1313. {}
  1314. /* For SMOD=Receive, indicates Shifter was ready to store new data into SHIFTBUF before the previous data
  1315. was read from SHIFTBUF (SHIFTBUF Overrun), or indicates that the received start or stop bit does not
  1316. match the expected value. */
  1317. Flexio_Uart_Ip_CheckShifterErrorStatus(Channel);
  1318. if (UartState->Status == FLEXIO_UART_IP_STATUS_ERROR)
  1319. {
  1320. break;
  1321. }
  1322. /* Read only data from SHIFBUF when shifter status flag is set */
  1323. Flexio_Uart_Ip_ReadData(Channel);
  1324. }
  1325. /* Check other success receiving case*/
  1326. if (FLEXIO_UART_IP_STATUS_BUSY == UartState->Status)
  1327. {
  1328. /* Check if Timeout occur */
  1329. if (Flexio_Uart_Ip_CheckTimeout(&StartTime, &ElapsedTicks, TimeoutTicks, FLEXIO_UART_IP_TIMEOUT_TYPE))
  1330. {
  1331. UartState->Status = FLEXIO_UART_IP_STATUS_TIMEOUT;
  1332. }
  1333. else
  1334. {
  1335. UartState->Status = FLEXIO_UART_IP_STATUS_SUCCESS;
  1336. }
  1337. }
  1338. /* Clear any previous timer events */
  1339. Flexio_Mcl_Ip_ClearTimerStatus(Base, RX_TIMER(Channel));
  1340. UartState->DriverIdle = FALSE;
  1341. RetStatus = UartState->Status;
  1342. }
  1343. return RetStatus;
  1344. }
  1345. #if (FLEXIO_UART_IP_HAS_DMA_ENABLED == STD_ON)
  1346. /*FUNCTION**********************************************************************
  1347. *
  1348. * Function Name : Flexio_Uart_Ip_CompleteSendUsingDma
  1349. * Description : Finish up a transmit by completing the process of sending
  1350. * data and disabling the DMA requests. This is a callback for DMA major loop
  1351. * completion, so it must match the DMA callback signature.
  1352. *
  1353. *END**************************************************************************/
  1354. void Flexio_Uart_Ip_CompleteSendUsingDma(uint8 Channel)
  1355. {
  1356. FLEXIO_UART_IP_DEV_ASSERT(Channel < FLEXIO_UART_IP_NUMBER_OF_SHIFTER_AND_TIMER);
  1357. Flexio_Uart_Ip_StateStructureType * UartState;
  1358. const Flexio_Uart_Ip_UserConfigType *UartUserCfg;
  1359. Dma_Ip_LogicChannelTransferListType DmaTransferList[FLEXIO_UART_DMA_CONFIG_LIST_DIMENSION];
  1360. Dma_Ip_ReturnType DmaReturnStatus;
  1361. Dma_Ip_LogicChannelStatusType DmaStatus;
  1362. FLEXIO_Type * Base;
  1363. uint32 StartTime;
  1364. uint32 TimeoutTicks;
  1365. uint32 ElapsedTicks = 0;
  1366. UartState = (Flexio_Uart_Ip_StateStructureType *)Flexio_Uart_Ip_apStateStructuresArray[Channel];
  1367. UartUserCfg = Flexio_Uart_Ip_apUserConfig[Channel];
  1368. Base = Flexio_Uart_Ip_apBases[FLEXIO_HW_INSTANCE];
  1369. /* Get Dma Ip Logic Channel Status */
  1370. (void)Dma_Ip_GetLogicChannelStatus(UartUserCfg->DmaChannel, &DmaStatus);
  1371. if (DMA_IP_CH_ERROR_STATE == DmaStatus.ChStateValue)
  1372. {
  1373. /* Reset the Dma Channel Error status. */
  1374. DmaReturnStatus = Dma_Ip_SetLogicChannelCommand(UartUserCfg->DmaChannel, DMA_IP_CH_CLEAR_ERROR);
  1375. FLEXIO_UART_IP_DEV_ASSERT(DMA_IP_STATUS_SUCCESS == DmaReturnStatus);
  1376. /* Update transmit status */
  1377. UartState->Status = FLEXIO_UART_IP_STATUS_ERROR;
  1378. /* Invoke callback if there is one */
  1379. if (UartUserCfg->Callback != NULL_PTR)
  1380. {
  1381. UartUserCfg->Callback(Channel, FLEXIO_UART_IP_EVENT_ERROR, UartUserCfg->CallbackParam);
  1382. }
  1383. }
  1384. /* Invoke the callback when the buffer is finished;
  1385. * Application can provide another buffer inside the callback by calling Flexio_Uart_Ip_SetTxBuffer */
  1386. if (FLEXIO_UART_IP_STATUS_BUSY == UartState->Status)
  1387. {
  1388. /* Invoke callback if there is one (callback may reset the tx buffer for continuous transmission)*/
  1389. if (UartUserCfg->Callback != NULL_PTR)
  1390. {
  1391. UartUserCfg->Callback(Channel, FLEXIO_UART_IP_EVENT_TX_EMPTY, UartUserCfg->CallbackParam);
  1392. }
  1393. }
  1394. /* If the callback has updated the tx buffer, update the DMA descriptor to continue the transfer;
  1395. * otherwise, stop the current transfer.
  1396. */
  1397. if ((UartState->RemainingBytes > 0U) && (UartState->Status != FLEXIO_UART_IP_STATUS_ERROR))
  1398. {
  1399. /* Set up parameters for Dma_Ip_LogicChannelTransferListType */
  1400. DmaTransferList[0].Param = DMA_IP_CH_SET_SOURCE_ADDRESS;
  1401. DmaTransferList[0].Value = (uint32)(UartState->TxData);
  1402. /* Update tx size and major loop count parameters for Dma_Ip_LogicChannelTransferListType */
  1403. DmaTransferList[1].Param = DMA_IP_CH_SET_MAJORLOOP_COUNT;
  1404. DmaTransferList[1].Value = UartState->RemainingBytes;
  1405. /* Re-configure the transfer control descriptor for the DMA channel */
  1406. DmaReturnStatus = Dma_Ip_SetLogicChannelTransferList(UartUserCfg->DmaChannel, DmaTransferList, FLEXIO_UART_DMA_LEAST_CONFIG_LIST_DIMENSION);
  1407. FLEXIO_UART_IP_DEV_ASSERT(DMA_IP_STATUS_SUCCESS == DmaReturnStatus);
  1408. /* Now that this tx is set up, clear remaining bytes count */
  1409. UartState->RemainingBytes = 0U;
  1410. /* Re-start the channel */
  1411. DmaReturnStatus = Dma_Ip_SetLogicChannelCommand(UartUserCfg->DmaChannel, DMA_IP_CH_SET_HARDWARE_REQUEST);
  1412. FLEXIO_UART_IP_DEV_ASSERT(DMA_IP_STATUS_SUCCESS == DmaReturnStatus);
  1413. }
  1414. else
  1415. {
  1416. /* Disable tx DMA requests for the current instance */
  1417. Flexio_Mcl_Ip_SetShifterDMARequest(Base, (uint8)(1U << TX_TIMER(Channel)), FALSE);
  1418. /* For Tx we need to disable timer interrupt */
  1419. Flexio_Mcl_Ip_SetTimerInterrupt(Base, (uint8)(1U << TX_TIMER(Channel)), FALSE);
  1420. Flexio_Uart_Ip_StartTimeout(&StartTime, &TimeoutTicks, FLEXIO_UART_IP_TIMEOUT_VALUE_US, FLEXIO_UART_IP_TIMEOUT_TYPE);
  1421. /* Wait until the data is completely shifted out of shift register */
  1422. while (!Flexio_Mcl_Ip_GetTimerStatus(Base, TX_TIMER(Channel)) && \
  1423. !Flexio_Uart_Ip_CheckTimeout(&StartTime, &ElapsedTicks, TimeoutTicks, FLEXIO_UART_IP_TIMEOUT_TYPE))
  1424. {}
  1425. if (Flexio_Uart_Ip_CheckTimeout(&StartTime, &ElapsedTicks, TimeoutTicks, FLEXIO_UART_IP_TIMEOUT_TYPE))
  1426. {
  1427. /* In case timeout occur */
  1428. UartState->Status = FLEXIO_UART_IP_STATUS_ERROR;
  1429. }
  1430. /* Update the busy flag */
  1431. UartState->DriverIdle = FALSE;
  1432. /* If the current reception hasn't been aborted, update the status */
  1433. if (FLEXIO_UART_IP_STATUS_BUSY == UartState->Status)
  1434. {
  1435. UartState->Status = FLEXIO_UART_IP_STATUS_SUCCESS;
  1436. /* Invoke callback if there is one */
  1437. if (UartUserCfg->Callback != NULL_PTR)
  1438. {
  1439. UartUserCfg->Callback(Channel, FLEXIO_UART_IP_EVENT_END_TRANSFER, UartUserCfg->CallbackParam);
  1440. }
  1441. }
  1442. }
  1443. }
  1444. /*FUNCTION**********************************************************************
  1445. *
  1446. * Function Name : Flexio_Uart_Ip_CompleteReceiveUsingDma
  1447. * Description : Finish up a transmit by completing the process of sending
  1448. * data and disabling the DMA requests. This is a callback for DMA major loop
  1449. * completion, so it must match the DMA callback signature.
  1450. *
  1451. *END**************************************************************************/
  1452. void Flexio_Uart_Ip_CompleteReceiveUsingDma(uint8 Channel)
  1453. {
  1454. FLEXIO_UART_IP_DEV_ASSERT(Channel < FLEXIO_UART_IP_NUMBER_OF_SHIFTER_AND_TIMER);
  1455. Flexio_Uart_Ip_StateStructureType * UartState;
  1456. const Flexio_Uart_Ip_UserConfigType * UartUserCfg;
  1457. Dma_Ip_LogicChannelTransferListType DmaTransferList[FLEXIO_UART_DMA_CONFIG_LIST_DIMENSION];
  1458. Dma_Ip_ReturnType DmaReturnStatus;
  1459. Dma_Ip_LogicChannelStatusType DmaStatus;
  1460. FLEXIO_Type * Base;
  1461. uint32 StartTime;
  1462. uint32 TimeoutTicks;
  1463. uint32 ElapsedTicks = 0;
  1464. UartState = (Flexio_Uart_Ip_StateStructureType *)Flexio_Uart_Ip_apStateStructuresArray[Channel];
  1465. UartUserCfg = Flexio_Uart_Ip_apUserConfig[Channel];
  1466. Base = Flexio_Uart_Ip_apBases[FLEXIO_HW_INSTANCE];
  1467. /* Get Dma Ip Logic Channel Status */
  1468. (void)Dma_Ip_GetLogicChannelStatus(UartUserCfg->DmaChannel, &DmaStatus);
  1469. if (DMA_IP_CH_ERROR_STATE == DmaStatus.ChStateValue)
  1470. {
  1471. /* Reset the Dma Channel Error status. */
  1472. DmaReturnStatus = Dma_Ip_SetLogicChannelCommand(UartUserCfg->DmaChannel, DMA_IP_CH_CLEAR_ERROR);
  1473. FLEXIO_UART_IP_DEV_ASSERT(DMA_IP_STATUS_SUCCESS == DmaReturnStatus);
  1474. /* Update transmit status */
  1475. UartState->Status = FLEXIO_UART_IP_STATUS_ERROR;
  1476. /* Invoke callback if there is one */
  1477. if (UartUserCfg->Callback != NULL_PTR)
  1478. {
  1479. UartUserCfg->Callback(Channel, FLEXIO_UART_IP_EVENT_ERROR, UartUserCfg->CallbackParam);
  1480. }
  1481. }
  1482. /* Invoke the callback when the buffer is finished;
  1483. * Application can provide another buffer inside the callback by calling Flexio_Uart_Ip_SetTxBuffer */
  1484. if (FLEXIO_UART_IP_STATUS_BUSY == UartState->Status)
  1485. {
  1486. /* Invoke callback if there is one (callback may reset the tx buffer for continuous transmission)*/
  1487. if (UartUserCfg->Callback != NULL_PTR)
  1488. {
  1489. UartUserCfg->Callback(Channel, FLEXIO_UART_IP_EVENT_RX_FULL, UartUserCfg->CallbackParam);
  1490. }
  1491. }
  1492. /* If the callback has updated the tx buffer, update the DMA descriptor to continue the transfer;
  1493. * otherwise, stop the current transfer.
  1494. */
  1495. if ((UartState->RemainingBytes > 0U) && (UartState->Status != FLEXIO_UART_IP_STATUS_ERROR))
  1496. {
  1497. /* Set up parameters for Dma_Ip_LogicChannelTransferListType */
  1498. DmaTransferList[0].Param = DMA_IP_CH_SET_DESTINATION_ADDRESS;
  1499. DmaTransferList[0].Value = (uint32)(UartState->RxData);
  1500. /* Update tx size and major loop count parameters for Dma_Ip_LogicChannelTransferListType */
  1501. DmaTransferList[1].Param = DMA_IP_CH_SET_MAJORLOOP_COUNT;
  1502. DmaTransferList[1].Value = UartState->RemainingBytes;
  1503. /* Re-configure the transfer control descriptor for the DMA channel */
  1504. DmaReturnStatus = Dma_Ip_SetLogicChannelTransferList(UartUserCfg->DmaChannel, DmaTransferList, FLEXIO_UART_DMA_LEAST_CONFIG_LIST_DIMENSION);
  1505. FLEXIO_UART_IP_DEV_ASSERT(DMA_IP_STATUS_SUCCESS == DmaReturnStatus);
  1506. /* Now that this rx is set up, clear remaining bytes count */
  1507. UartState->RemainingBytes = 0U;
  1508. /* Re-start the channel */
  1509. DmaReturnStatus = Dma_Ip_SetLogicChannelCommand(UartUserCfg->DmaChannel, DMA_IP_CH_SET_HARDWARE_REQUEST);
  1510. FLEXIO_UART_IP_DEV_ASSERT(DMA_IP_STATUS_SUCCESS == DmaReturnStatus);
  1511. }
  1512. else
  1513. {
  1514. /* Disable interrupts for Rx / Tx shifter */
  1515. Flexio_Uart_Ip_SetShifterInterrupt(Channel, FALSE);
  1516. /* Disable timer interrupt */
  1517. Flexio_Mcl_Ip_SetTimerInterrupt(Base, (uint8)(1U << TX_TIMER(Channel)), FALSE);
  1518. /* Disable rx DMA requests for the current instance */
  1519. Flexio_Mcl_Ip_SetShifterDMARequest(Base, (uint8)(1U << RX_TIMER(Channel)), FALSE);
  1520. /* For Rx we need to disable timer interrupt */
  1521. Flexio_Mcl_Ip_SetTimerInterrupt(Base, (uint8)(1U << RX_TIMER(Channel)), FALSE);
  1522. Flexio_Uart_Ip_StartTimeout(&StartTime, &TimeoutTicks, FLEXIO_UART_IP_TIMEOUT_VALUE_US, FLEXIO_UART_IP_TIMEOUT_TYPE);
  1523. /* Wait until the data is completely shifted out of shift register */
  1524. while (!Flexio_Mcl_Ip_GetTimerStatus(Base, TX_TIMER(Channel)) && \
  1525. !Flexio_Uart_Ip_CheckTimeout(&StartTime, &ElapsedTicks, TimeoutTicks, FLEXIO_UART_IP_TIMEOUT_TYPE))
  1526. {}
  1527. if (Flexio_Uart_Ip_CheckTimeout(&StartTime, &ElapsedTicks, TimeoutTicks, FLEXIO_UART_IP_TIMEOUT_TYPE))
  1528. {
  1529. /* In case timeout occur */
  1530. UartState->Status = FLEXIO_UART_IP_STATUS_ERROR;
  1531. }
  1532. /* Update the busy flag */
  1533. UartState->DriverIdle = FALSE;
  1534. /* If the current reception hasn't been aborted, update the status */
  1535. if (FLEXIO_UART_IP_STATUS_BUSY == UartState->Status)
  1536. {
  1537. UartState->Status = FLEXIO_UART_IP_STATUS_SUCCESS;
  1538. /* Call the callback to notify application that the transfer is complete */
  1539. if (UartUserCfg->Callback != NULL_PTR)
  1540. {
  1541. UartUserCfg->Callback(Channel, FLEXIO_UART_IP_EVENT_END_TRANSFER, UartUserCfg->CallbackParam);
  1542. }
  1543. }
  1544. }
  1545. }
  1546. #endif
  1547. /*FUNCTION**********************************************************************
  1548. *
  1549. * Function Name : Flexio_Uart_Ip_AbortTransferData
  1550. * Description : This function terminates an non-blocking FLEXIO transmission
  1551. * early. During a non-blocking FLEXIO transmission, the user has the option to
  1552. * terminate the transmission early if the transmission is still in progress.
  1553. *
  1554. * Implements : Flexio_Uart_Ip_AbortTransferData_Activity
  1555. *END**************************************************************************/
  1556. Flexio_Uart_Ip_StatusType Flexio_Uart_Ip_AbortTransferData(const uint8 Channel)
  1557. {
  1558. FLEXIO_UART_IP_DEV_ASSERT(Channel < FLEXIO_UART_IP_NUMBER_OF_SHIFTER_AND_TIMER);
  1559. Flexio_Uart_Ip_StateStructureType * UartState;
  1560. const Flexio_Uart_Ip_UserConfigType *UartUserCfg;
  1561. Flexio_Uart_Ip_StatusType RetStatus = FLEXIO_UART_IP_STATUS_ERROR;
  1562. UartState = (Flexio_Uart_Ip_StateStructureType *)Flexio_Uart_Ip_apStateStructuresArray[Channel];
  1563. UartUserCfg = Flexio_Uart_Ip_apUserConfig[Channel];
  1564. FLEXIO_UART_IP_DEV_ASSERT(UartState != NULL_PTR);
  1565. FLEXIO_UART_IP_DEV_ASSERT(UartUserCfg != NULL_PTR);
  1566. /* Check if a transfer is running. */
  1567. if (!UartState->DriverIdle)
  1568. {
  1569. RetStatus = FLEXIO_UART_IP_STATUS_SUCCESS;
  1570. }
  1571. else
  1572. {
  1573. /* Update the tx status */
  1574. UartState->Status = FLEXIO_UART_IP_STATUS_ABORTED;
  1575. /* Stop the running transfer. */
  1576. if (FLEXIO_UART_IP_DRIVER_TYPE_INTERRUPTS == UartUserCfg->DriverType)
  1577. {
  1578. Flexio_Uart_Ip_StopTransfer(Channel);
  1579. }
  1580. #if (FLEXIO_UART_IP_HAS_DMA_ENABLED == STD_ON)
  1581. else
  1582. {
  1583. /* Release the DMA channel */
  1584. (void)Dma_Ip_SetLogicChannelCommand(UartUserCfg->DmaChannel, DMA_IP_CH_CLEAR_HARDWARE_REQUEST);
  1585. if (FLEXIO_UART_IP_DIRECTION_TX == UartUserCfg->Direction)
  1586. {
  1587. Flexio_Uart_Ip_CompleteSendUsingDma(Channel);
  1588. }
  1589. else
  1590. {
  1591. Flexio_Uart_Ip_CompleteReceiveUsingDma(Channel);
  1592. }
  1593. }
  1594. #endif
  1595. RetStatus = FLEXIO_UART_IP_STATUS_SUCCESS;
  1596. }
  1597. return RetStatus;
  1598. }
  1599. /*FUNCTION**********************************************************************
  1600. *
  1601. * Function Name : Flexio_Uart_Ip_SetBaudRate
  1602. * Description : Configures the FLEXIO baud rate.
  1603. * In some FLEXIO instances the user must disable the transmitter/receiver
  1604. * before calling this function.
  1605. * Generally, this may be applied to all FLEXIOs to ensure safe operation.
  1606. *
  1607. * Implements : Flexio_Uart_Ip_SetBaudRate_Activity
  1608. *END**************************************************************************/
  1609. Flexio_Uart_Ip_StatusType Flexio_Uart_Ip_SetBaudRate(const uint8 Channel,
  1610. const Flexio_Uart_Ip_BaudrateType DesiredBaudrate,
  1611. const uint32 ClockFrequency)
  1612. {
  1613. FLEXIO_UART_IP_DEV_ASSERT(Channel < FLEXIO_UART_IP_NUMBER_OF_SHIFTER_AND_TIMER);
  1614. FLEXIO_UART_IP_DEV_ASSERT(ClockFrequency > 0U);
  1615. Flexio_Uart_Ip_StateStructureType * UartState;
  1616. const Flexio_Uart_Ip_UserConfigType *UartUserCfg;
  1617. Flexio_Uart_Ip_TimerDecrementType TimerDecrement;
  1618. Flexio_Uart_Ip_StatusType Status;
  1619. FLEXIO_Type * Base;
  1620. sint32 Divider;
  1621. UartState = (Flexio_Uart_Ip_StateStructureType *)Flexio_Uart_Ip_apStateStructuresArray[Channel];
  1622. UartUserCfg = Flexio_Uart_Ip_apUserConfig[Channel];
  1623. Base = Flexio_Uart_Ip_apBases[FLEXIO_HW_INSTANCE];
  1624. uint16 BitCountValue = (uint16)UartUserCfg->BitCount;
  1625. FLEXIO_UART_IP_DEV_ASSERT(UartState != NULL_PTR);
  1626. /* Check if there is an ongoing transfer */
  1627. if (TRUE == UartState->DriverIdle)
  1628. {
  1629. Status = FLEXIO_UART_IP_STATUS_BUSY;
  1630. }
  1631. else
  1632. {
  1633. /* In case driver does not support 16/256 prescale timer decreasement modes */
  1634. if (FLEXIO_TIMER_DECREMENT_FXIO_CLK_SHIFT_TMR == UartUserCfg->TimerDec)
  1635. {
  1636. TimerDecrement = FLEXIO_TIMER_DECREMENT_FXIO_CLK_SHIFT_TMR;
  1637. Divider = ((sint32)ClockFrequency / ((sint32)DesiredBaudrate * 2)) - 1;
  1638. FLEXIO_UART_IP_DEV_ASSERT((Divider <= 255) && (Divider >= 0));
  1639. UartState->BaudRate = (uint32)(ClockFrequency / (2U * ((uint32)Divider + 1U)));
  1640. }
  1641. else
  1642. {
  1643. Divider = ((sint32)ClockFrequency / ((sint32)DesiredBaudrate * 16 * 2)) - 1;
  1644. if ((Divider <= 255) && (Divider >= 0))
  1645. {
  1646. TimerDecrement = FLEXIO_TIMER_DECREMENT_FXIO_CLK_DIV_16;
  1647. UartState->BaudRate = (uint32)(ClockFrequency / (16U * 2U * ((uint32)Divider + 1U)));
  1648. }
  1649. else
  1650. {
  1651. TimerDecrement = FLEXIO_TIMER_DECREMENT_FXIO_CLK_DIV_256;
  1652. Divider = ((sint32)ClockFrequency / ((sint32)DesiredBaudrate * 256 * 2)) - 1;
  1653. FLEXIO_UART_IP_DEV_ASSERT((Divider <= 255) && (Divider >= 0));
  1654. UartState->BaudRate = (uint32)(ClockFrequency / (256U * 2U * ((uint32)Divider + 1U)));
  1655. }
  1656. }
  1657. Flexio_Uart_Ip_SetTimerCompare(Base, TX_TIMER(Channel), (uint16)((((uint16)(BitCountValue << 1U) - 1U) << 8U) + (uint8)Divider));
  1658. if (FLEXIO_UART_IP_DIRECTION_TX == UartUserCfg->Direction)
  1659. {
  1660. /* Configure tx timer */
  1661. Flexio_Uart_Ip_SetTimerConfig(Base,
  1662. TX_TIMER(Channel),
  1663. FLEXIO_TIMER_RESET_NEVER,
  1664. TimerDecrement, /* Decrement on FlexIO clock */
  1665. FLEXIO_TIMER_INITOUT_ONE);
  1666. Flexio_Uart_Ip_SetTimerStartStopBitConfig(Base,
  1667. TX_TIMER(Channel),
  1668. FLEXIO_TIMER_START_BIT_ENABLED,
  1669. FLEXIO_TIMER_STOP_BIT_TIM_DIS);
  1670. Flexio_Uart_Ip_SetTimerCondition(Base,
  1671. TX_TIMER(Channel),
  1672. FLEXIO_TIMER_ENABLE_TRG_HIGH, /* Enable when Tx data is available */
  1673. FLEXIO_TIMER_DISABLE_TIM_CMP);
  1674. }
  1675. else
  1676. {
  1677. /* Configure rx timer */
  1678. Flexio_Uart_Ip_SetTimerConfig(Base,
  1679. RX_TIMER(Channel),
  1680. FLEXIO_TIMER_RESET_PIN_RISING,
  1681. TimerDecrement, /* Decrement on FlexIO clock */
  1682. FLEXIO_TIMER_INITOUT_ONE_RESET);
  1683. Flexio_Uart_Ip_SetTimerStartStopBitConfig(Base,
  1684. TX_TIMER(Channel),
  1685. FLEXIO_TIMER_START_BIT_ENABLED,
  1686. FLEXIO_TIMER_STOP_BIT_TIM_DIS);
  1687. Flexio_Uart_Ip_SetTimerCondition(Base,
  1688. TX_TIMER(Channel),
  1689. FLEXIO_TIMER_ENABLE_PIN_POSEDGE, /* Enable when data is available */
  1690. FLEXIO_TIMER_DISABLE_TIM_CMP);
  1691. }
  1692. Status = FLEXIO_UART_IP_STATUS_SUCCESS;
  1693. }
  1694. return Status;
  1695. }
  1696. /*FUNCTION**********************************************************************
  1697. *
  1698. * Function Name : Flexio_Uart_Ip_SetTxBuffer
  1699. * Description : Sets the driver internal reference to the tx buffer.
  1700. * Can be called from the tx callback to provide a different
  1701. * buffer for continuous transmission.
  1702. *
  1703. * Implements : Flexio_Uart_Ip_SetTxBuffer_Activity
  1704. *END**************************************************************************/
  1705. Flexio_Uart_Ip_StatusType Flexio_Uart_Ip_SetTxBuffer(const uint8 Channel,
  1706. const uint8 * TxData,
  1707. const uint32 TxSize)
  1708. {
  1709. FLEXIO_UART_IP_DEV_ASSERT(TxData != NULL_PTR);
  1710. FLEXIO_UART_IP_DEV_ASSERT(TxSize > 0U);
  1711. Flexio_Uart_Ip_StateStructureType * UartState;
  1712. UartState = (Flexio_Uart_Ip_StateStructureType *)Flexio_Uart_Ip_apStateStructuresArray[Channel];
  1713. UartState->TxData = TxData;
  1714. UartState->RemainingBytes = TxSize;
  1715. return FLEXIO_UART_IP_STATUS_SUCCESS;
  1716. }
  1717. /*FUNCTION**********************************************************************
  1718. *
  1719. * Function Name : Flexio_Uart_Ip_SetRxBuffer
  1720. * Description : Sets the driver internal reference to the rx buffer.
  1721. * Can be called from the rx callback to provide a different
  1722. * buffer for continuous reception.
  1723. *
  1724. * Implements : Flexio_Uart_Ip_SetRxBuffer_Activity
  1725. *END**************************************************************************/
  1726. Flexio_Uart_Ip_StatusType Flexio_Uart_Ip_SetRxBuffer(const uint8 Channel,
  1727. uint8 * RxData,
  1728. const uint32 RxSize)
  1729. {
  1730. FLEXIO_UART_IP_DEV_ASSERT(RxData != NULL_PTR);
  1731. FLEXIO_UART_IP_DEV_ASSERT(RxSize > 0U);
  1732. Flexio_Uart_Ip_StateStructureType * UartState;
  1733. UartState = (Flexio_Uart_Ip_StateStructureType *)Flexio_Uart_Ip_apStateStructuresArray[Channel];
  1734. UartState->RxData = RxData;
  1735. UartState->RemainingBytes = RxSize;
  1736. return FLEXIO_UART_IP_STATUS_SUCCESS;
  1737. }
  1738. #define UART_STOP_SEC_CODE
  1739. #include "Uart_MemMap.h"
  1740. #endif /* (FLEXIO_UART_IP_IS_USING == STD_ON) */
  1741. #endif /* #ifdef FLEXIO_UART_IP_IS_USING */
  1742. #ifdef __cplusplus
  1743. }
  1744. /** @} */
  1745. #endif