Lpuart_Uart_Ip.c 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174
  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 Lpuart_Uart_Ip.c
  26. * @defgroup lpuart_uart_ip Lpuart UART IPL
  27. * @addtogroup lpuart_uart_ip Lpuart 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 "Lpuart_Uart_Ip.h"
  40. #include "Lpuart_Uart_Ip_HwAccess.h"
  41. #if (LPUART_UART_IP_HAS_DMA_ENABLED == STD_ON)
  42. #include "Dma_Ip.h"
  43. #endif
  44. #ifdef LPUART_UART_IP_DEV_ERROR_DETECT
  45. #if (LPUART_UART_IP_DEV_ERROR_DETECT == STD_ON)
  46. #include "Devassert.h"
  47. #endif /* (STD_ON == LPUART_UART_IP_DEV_ERROR_DETECT) */
  48. #endif /* idfef LPUART_UART_IP_DEV_ERROR_DETECT */
  49. /*==================================================================================================
  50. * SOURCE FILE VERSION INFORMATION
  51. ==================================================================================================*/
  52. #define LPUART_UART_IP_VENDOR_ID_C 43
  53. #define LPUART_UART_IP_AR_RELEASE_MAJOR_VERSION_C 4
  54. #define LPUART_UART_IP_AR_RELEASE_MINOR_VERSION_C 4
  55. #define LPUART_UART_IP_AR_RELEASE_REVISION_VERSION_C 0
  56. #define LPUART_UART_IP_SW_MAJOR_VERSION_C 1
  57. #define LPUART_UART_IP_SW_MINOR_VERSION_C 0
  58. #define LPUART_UART_IP_SW_PATCH_VERSION_C 0
  59. /*==================================================================================================
  60. * FILE VERSION CHECKS
  61. ==================================================================================================*/
  62. /* Checks against Lpuart_Uart_Ip.h */
  63. #if (LPUART_UART_IP_VENDOR_ID_C != LPUART_UART_IP_VENDOR_ID)
  64. #error "Lpuart_Uart_Ip.c and Lpuart_Uart_Ip.h have different vendor ids"
  65. #endif
  66. #if ((LPUART_UART_IP_AR_RELEASE_MAJOR_VERSION_C != LPUART_UART_IP_AR_RELEASE_MAJOR_VERSION) || \
  67. (LPUART_UART_IP_AR_RELEASE_MINOR_VERSION_C != LPUART_UART_IP_AR_RELEASE_MINOR_VERSION) || \
  68. (LPUART_UART_IP_AR_RELEASE_REVISION_VERSION_C != LPUART_UART_IP_AR_RELEASE_REVISION_VERSION))
  69. #error "AUTOSAR Version Numbers of Lpuart_Uart_Ip.c and Lpuart_Uart_Ip.h are different"
  70. #endif
  71. #if ((LPUART_UART_IP_SW_MAJOR_VERSION_C != LPUART_UART_IP_SW_MAJOR_VERSION) || \
  72. (LPUART_UART_IP_SW_MINOR_VERSION_C != LPUART_UART_IP_SW_MINOR_VERSION) || \
  73. (LPUART_UART_IP_SW_PATCH_VERSION_C != LPUART_UART_IP_SW_PATCH_VERSION))
  74. #error "Software Version Numbers of Lpuart_Uart_Ip.c and Lpuart_Uart_Ip.h are different"
  75. #endif
  76. /* Checks against Lpuart_Uart_Ip_HwAccess.h */
  77. #if (LPUART_UART_IP_VENDOR_ID_C != LPUART_UART_IP_HWACCESS_VENDOR_ID)
  78. #error "Lpuart_Uart_Ip.c and Lpuart_Uart_Ip_HwAccess.h have different vendor ids"
  79. #endif
  80. #if ((LPUART_UART_IP_AR_RELEASE_MAJOR_VERSION_C != LPUART_UART_IP_HWACCESS_AR_RELEASE_MAJOR_VERSION) || \
  81. (LPUART_UART_IP_AR_RELEASE_MINOR_VERSION_C != LPUART_UART_IP_HWACCESS_AR_RELEASE_MINOR_VERSION) || \
  82. (LPUART_UART_IP_AR_RELEASE_REVISION_VERSION_C != LPUART_UART_IP_HWACCESS_AR_RELEASE_REVISION_VERSION))
  83. #error "AUTOSAR Version Numbers of Lpuart_Uart_Ip.c and Lpuart_Uart_Ip_HwAccess.h are different"
  84. #endif
  85. #if ((LPUART_UART_IP_SW_MAJOR_VERSION_C != LPUART_UART_IP_HWACCESS_SW_MAJOR_VERSION) || \
  86. (LPUART_UART_IP_SW_MINOR_VERSION_C != LPUART_UART_IP_HWACCESS_SW_MINOR_VERSION) || \
  87. (LPUART_UART_IP_SW_PATCH_VERSION_C != LPUART_UART_IP_HWACCESS_SW_PATCH_VERSION))
  88. #error "Software Version Numbers of Lpuart_Uart_Ip.c and Lpuart_Uart_Ip_HwAccess.h are different"
  89. #endif
  90. #ifndef DISABLE_MCAL_INTERMODULE_ASR_CHECK
  91. /* Checks against Dma_Ip.h */
  92. #if (LPUART_UART_IP_HAS_DMA_ENABLED == STD_ON)
  93. #if ((LPUART_UART_IP_AR_RELEASE_MAJOR_VERSION_C != DMA_IP_AR_RELEASE_MAJOR_VERSION_H) || \
  94. (LPUART_UART_IP_AR_RELEASE_MINOR_VERSION_C != DMA_IP_AR_RELEASE_MINOR_VERSION_H))
  95. #error "AutoSar Version Numbers of Lpuart_Uart_Ip.c and Dma_Ip.h are different"
  96. #endif
  97. #endif
  98. /* Checks against Devassert.h */
  99. #ifdef LPUART_UART_IP_DEV_ERROR_DETECT
  100. #if (STD_ON == LPUART_UART_IP_DEV_ERROR_DETECT)
  101. /* Check if Lpuart_Uart_Ip.c file and Devassert.h file are of the same Autosar version */
  102. #if ((LPUART_UART_IP_AR_RELEASE_MAJOR_VERSION_C != DEVASSERT_AR_RELEASE_MAJOR_VERSION) || \
  103. (LPUART_UART_IP_AR_RELEASE_MINOR_VERSION_C != DEVASSERT_AR_RELEASE_MINOR_VERSION))
  104. #error "AutoSar Version Numbers of Lpuart_Uart_Ip.c and Devassert.h are different"
  105. #endif
  106. #endif /* (STD_ON == LPUART_UART_IP_DEV_ERROR_DETECT) */
  107. #endif /* #ifdef LPUART_UART_IP_DEV_ERROR_DETECT */
  108. #endif
  109. /*==================================================================================================
  110. * LOCAL TYPEDEFS (STRUCTURES, UNIONS, ENUMS)
  111. ==================================================================================================*/
  112. /*==================================================================================================
  113. * LOCAL MACROS
  114. ==================================================================================================*/
  115. #ifdef LPUART_UART_IP_DEV_ERROR_DETECT
  116. #if (LPUART_UART_IP_DEV_ERROR_DETECT == STD_ON)
  117. #define LPUART_UART_IP_DEV_ASSERT(x) DevAssert(x)
  118. #else
  119. #define LPUART_UART_IP_DEV_ASSERT(x) (void)(x)
  120. #endif
  121. #endif
  122. /* @brief Address of the least significant byte or word in a 32-bit register (depends on endianness) */
  123. #define LPUART_UART_IP_LSBW_ADDR(reg) ((uint32)(&(reg)))
  124. /*==================================================================================================
  125. * LOCAL CONSTANTS
  126. ==================================================================================================*/
  127. #if (LPUART_UART_IP_HAS_DMA_ENABLED == STD_ON)
  128. #define LPUART_UART_IP_DMA_CONFIG_LIST_DIMENSION (10U)
  129. #define LPUART_UART_IP_DMA_LEAST_CONFIG_LIST_DIMENSION (2U)
  130. #endif
  131. /*==================================================================================================
  132. * LOCAL VARIABLES
  133. ==================================================================================================*/
  134. #define UART_START_SEC_VAR_CLEARED_UNSPECIFIED_NO_CACHEABLE
  135. #include "Uart_MemMap.h"
  136. /** @brief Array of UART driver runtime state structures */
  137. Lpuart_Uart_Ip_StateStructureType Lpuart_Uart_Ip_apStateStructure[LPUART_UART_IP_NUMBER_OF_INSTANCES];
  138. #define UART_STOP_SEC_VAR_CLEARED_UNSPECIFIED_NO_CACHEABLE
  139. #include "Uart_MemMap.h"
  140. #define UART_START_SEC_VAR_CLEARED_UNSPECIFIED
  141. #include "Uart_MemMap.h"
  142. /* Pointer to lpuart runtime state structure */
  143. Lpuart_Uart_Ip_StateStructureType * Lpuart_Uart_Ip_apStateStructuresArray[LPUART_UART_IP_NUMBER_OF_INSTANCES];
  144. /** @brief User config structure. */
  145. const Lpuart_Uart_Ip_UserConfigType * Lpuart_Uart_Ip_apUserConfig[LPUART_UART_IP_NUMBER_OF_INSTANCES];
  146. #define UART_STOP_SEC_VAR_CLEARED_UNSPECIFIED
  147. #include "Uart_MemMap.h"
  148. #define UART_START_SEC_CONST_UNSPECIFIED
  149. #include "Uart_MemMap.h"
  150. /* Table of base addresses for lpuart instances. */
  151. static LPUART_Type * const Lpuart_Uart_Ip_apBases[LPUART_UART_IP_NUMBER_OF_INSTANCES] = IP_LPUART_BASE_PTRS;
  152. #define UART_STOP_SEC_CONST_UNSPECIFIED
  153. #include "Uart_MemMap.h"
  154. /*==================================================================================================
  155. * GLOBAL CONSTANTS
  156. ==================================================================================================*/
  157. /*==================================================================================================
  158. * GLOBAL VARIABLES
  159. ==================================================================================================*/
  160. /*==================================================================================================
  161. * LOCAL FUNCTION PROTOTYPES
  162. ==================================================================================================*/
  163. /*==================================================================================================
  164. * LOCAL FUNCTIONS
  165. ==================================================================================================*/
  166. #define UART_START_SEC_CODE
  167. #include "Uart_MemMap.h"
  168. static Lpuart_Uart_Ip_StatusType Lpuart_Uart_Ip_StartSendDataUsingInt(const uint8 Instance,
  169. const uint8 * TxBuff,
  170. const uint32 TxSize);
  171. static void Lpuart_Uart_Ip_CompleteSendDataUsingInt(const uint8 Instance);
  172. static Lpuart_Uart_Ip_StatusType Lpuart_Uart_Ip_StartReceiveDataUsingInt(const uint8 Instance,
  173. uint8 * RxBuff,
  174. const uint32 RxSize);
  175. static void Lpuart_Uart_Ip_CompleteReceiveDataUsingInt(const uint8 Instance);
  176. #if (LPUART_UART_IP_HAS_DMA_ENABLED == STD_ON)
  177. static Lpuart_Uart_Ip_StatusType Lpuart_Uart_Ip_StartSendDataUsingDma(const uint8 Instance,
  178. const uint8 * TxBuff,
  179. const uint32 TxSize);
  180. static Lpuart_Uart_Ip_StatusType Lpuart_Uart_Ip_StartReceiveDataUsingDma(const uint8 Instance,
  181. uint8 * RxBuff,
  182. const uint32 RxSize);
  183. static void Lpuart_Uart_Ip_SetupIntDmaMode(const uint8 Instance, boolean Enable);
  184. #endif
  185. static void Lpuart_Uart_Ip_PutData(const uint8 Instance);
  186. static void Lpuart_Uart_Ip_GetData(const uint8 Instance);
  187. static void Lpuart_Uart_Ip_RxIrqHandler(const uint8 Instance);
  188. static void Lpuart_Uart_Ip_TxEmptyIrqHandler(const uint8 Instance);
  189. static void Lpuart_Uart_Ip_TxCompleteIrqHandler(const uint8 Instance);
  190. static void Lpuart_Uart_Ip_ErrIrqHandler(const uint8 Instance);
  191. static void Lpuart_Uart_Ip_StartGetData(const uint8 Instance, uint32 * StartTime, uint32 * ElapsedTicks, uint32 TimeoutTicks);
  192. /*==================================================================================================
  193. * GLOBAL FUNCTIONS
  194. ==================================================================================================*/
  195. /*FUNCTION**********************************************************************
  196. *
  197. * Function Name : Lpuart_Uart_Ip_Init
  198. * Description : This function initializes a LPUART instance for operation.
  199. * This function will initialize the run-time state structure to keep track of
  200. * the on-going transfers, ungate the clock to the LPUART module, initialize the
  201. * module to user defined settings and default settings, configure the IRQ state
  202. * structure and enable the module-level interrupt to the core, and enable the
  203. * LPUART module transmitter and receiver.
  204. *
  205. * Implements : Lpuart_Uart_Ip_Init_Activity
  206. *END**************************************************************************/
  207. /* implements Lpuart_Uart_Ip_Init_Activity*/
  208. void Lpuart_Uart_Ip_Init(const uint8 Instance, const Lpuart_Uart_Ip_UserConfigType * UserConfig)
  209. {
  210. LPUART_UART_IP_DEV_ASSERT(Instance < LPUART_UART_IP_NUMBER_OF_INSTANCES);
  211. LPUART_UART_IP_DEV_ASSERT(UserConfig != NULL_PTR);
  212. /* Check if current instance is already initialized. */
  213. LPUART_UART_IP_DEV_ASSERT(NULL_PTR == Lpuart_Uart_Ip_apStateStructuresArray[Instance]);
  214. /* Check if Baudrate parameters are valid value */
  215. LPUART_UART_IP_DEV_ASSERT(UserConfig->BaudOverSamplingRatio <= 0x20U);
  216. LPUART_UART_IP_DEV_ASSERT((UserConfig->BaudRateDivisor <= 0x1FFFU) && \
  217. (UserConfig->BaudRateDivisor >= 1U));
  218. LPUART_Type * Base = Lpuart_Uart_Ip_apBases[Instance];
  219. Lpuart_Uart_Ip_StateStructureType *UartStatePtr;
  220. uint32 Index;
  221. uint8 *ClearStructPtr;
  222. Lpuart_Uart_Ip_apStateStructuresArray[Instance] = UserConfig->StateStruct;
  223. UartStatePtr = Lpuart_Uart_Ip_apStateStructuresArray[Instance];
  224. Lpuart_Uart_Ip_apUserConfig[Instance] = UserConfig;
  225. ClearStructPtr = (uint8 *)UartStatePtr;
  226. #if (LPUART_UART_IP_HAS_DMA_ENABLED == STD_ON)
  227. /* In DMA mode, only 7-bits and 8-bits chars are supported */
  228. LPUART_UART_IP_DEV_ASSERT((UserConfig->TransferType != LPUART_UART_IP_USING_DMA) || \
  229. ((LPUART_UART_IP_7_BITS_PER_CHAR == UserConfig->BitCountPerChar) || \
  230. (LPUART_UART_IP_8_BITS_PER_CHAR == UserConfig->BitCountPerChar)));
  231. #endif /* (LPUART_UART_IP_HAS_DMA_ENABLED == STD_ON) */
  232. /* For 10 bits per char, parity bit cannot be enabled */
  233. LPUART_UART_IP_DEV_ASSERT((UserConfig->BitCountPerChar != LPUART_UART_IP_10_BITS_PER_CHAR) || \
  234. (LPUART_UART_IP_PARITY_DISABLED == UserConfig->ParityMode));
  235. /* Clear the state struct for this instance. */
  236. for (Index = 0; Index < sizeof(Lpuart_Uart_Ip_StateStructureType); Index++)
  237. {
  238. ClearStructPtr[Index] = 0;
  239. }
  240. /* Initialize the LPUART instance */
  241. Lpuart_Uart_Ip_Hw_Init(Base);
  242. /* Check if Osr is between 4x and 7x oversampling.
  243. * If so, then "BOTHEDGE" sampling must be turned on */
  244. if (UserConfig->BaudOverSamplingRatio < 8U)
  245. {
  246. Lpuart_Uart_Ip_EnableBothEdgeSamplingCmd(Base);
  247. }
  248. /* Program the Osr value (bit value is one less than actual value) */
  249. Lpuart_Uart_Ip_SetOversamplingRatio(Base, (uint32)UserConfig->BaudOverSamplingRatio -1U);
  250. /* Write the Sbr value to the BAUD registers */
  251. Lpuart_Uart_Ip_SetBaudRateDivisor(Base, UserConfig->BaudRateDivisor);
  252. if (UserConfig->ParityMode != LPUART_UART_IP_PARITY_DISABLED)
  253. {
  254. Lpuart_Uart_Ip_SetBitCountPerChar(Base, UserConfig->BitCountPerChar, TRUE);
  255. }
  256. else
  257. {
  258. Lpuart_Uart_Ip_SetBitCountPerChar(Base, UserConfig->BitCountPerChar, FALSE);
  259. }
  260. Lpuart_Uart_Ip_SetParityMode(Base, UserConfig->ParityMode);
  261. Lpuart_Uart_Ip_SetStopBitCount(Base, UserConfig->StopBitsCount);
  262. /* Initialize last driver operation status */
  263. UartStatePtr->TransmitStatus = LPUART_UART_IP_STATUS_SUCCESS;
  264. UartStatePtr->ReceiveStatus = LPUART_UART_IP_STATUS_SUCCESS;
  265. /* Set the initial baudrate from user's structure */
  266. UartStatePtr->BaudRate = UserConfig->BaudRate;
  267. }
  268. /*FUNCTION**********************************************************************
  269. *
  270. * Function Name : Lpuart_Uart_Ip_Deinit
  271. * Description : This function shuts down the UART by disabling interrupts and
  272. * transmitter/receiver.
  273. *
  274. * Implements : Lpuart_Uart_Ip_Deinit_Activity
  275. *END**************************************************************************/
  276. /* implements Lpuart_Uart_Ip_Deinit_Activity*/
  277. void Lpuart_Uart_Ip_Deinit(const uint8 Instance)
  278. {
  279. LPUART_UART_IP_DEV_ASSERT(Instance < LPUART_UART_IP_NUMBER_OF_INSTANCES);
  280. uint32 StartTime;
  281. uint32 TimeoutTicks;
  282. uint32 ElapsedTicks = 0;
  283. LPUART_Type * Base = Lpuart_Uart_Ip_apBases[Instance];
  284. /* Check if current instance is already de-initialized or is gated.*/
  285. LPUART_UART_IP_DEV_ASSERT(Lpuart_Uart_Ip_apStateStructuresArray[Instance] != NULL_PTR);
  286. Lpuart_Uart_Ip_StartTimeout(&StartTime, &TimeoutTicks, LPUART_UART_IP_TIMEOUT_VALUE_US, LPUART_UART_IP_TIMEOUT_TYPE);
  287. /* Wait until the data is completely shifted out of shift register */
  288. while (!Lpuart_Uart_Ip_GetStatusFlag(Base, LPUART_UART_IP_TX_COMPLETE) && \
  289. !Lpuart_Uart_Ip_CheckTimeout(&StartTime, &ElapsedTicks, TimeoutTicks, LPUART_UART_IP_TIMEOUT_TYPE))
  290. {}
  291. /* Disble Tx data register empty and transmission complete interrupt */
  292. Lpuart_Uart_Ip_SetIntMode(Base, LPUART_UART_IP_INT_TX_DATA_REG_EMPTY, FALSE);
  293. Lpuart_Uart_Ip_SetIntMode(Base, LPUART_UART_IP_INT_TX_COMPLETE, FALSE);
  294. /* Disble Rx data register full */
  295. Lpuart_Uart_Ip_SetIntMode(Base, LPUART_UART_IP_INT_RX_DATA_REG_FULL, FALSE);
  296. /* Disable error interrupts */
  297. Lpuart_Uart_Ip_SetIntMode(Base, LPUART_UART_IP_INT_RX_OVERRUN, FALSE);
  298. Lpuart_Uart_Ip_SetIntMode(Base, LPUART_UART_IP_INT_PARITY_ERR_FLAG, FALSE);
  299. Lpuart_Uart_Ip_SetIntMode(Base, LPUART_UART_IP_INT_NOISE_ERR_FLAG, FALSE);
  300. Lpuart_Uart_Ip_SetIntMode(Base, LPUART_UART_IP_INT_FRAME_ERR_FLAG, FALSE);
  301. /* Clear our saved pointer to the state structure */
  302. Lpuart_Uart_Ip_apStateStructuresArray[Instance] = NULL_PTR;
  303. }
  304. /*FUNCTION**********************************************************************
  305. *
  306. * Function Name : Lpuart_Uart_Ip_SyncSend
  307. * Description : Send out multiple bytes of data using polling method.
  308. *
  309. * Implements : Lpuart_Uart_Ip_SyncSend_Activity
  310. *END**************************************************************************/
  311. /* implements Lpuart_Uart_Ip_SyncSend_Activity*/
  312. Lpuart_Uart_Ip_StatusType Lpuart_Uart_Ip_SyncSend(const uint8 Instance,
  313. const uint8 *TxBuff,
  314. const uint32 TxSize,
  315. const uint32 Timeout)
  316. {
  317. /* Check the validity of the parameters */
  318. LPUART_UART_IP_DEV_ASSERT(Instance < LPUART_UART_IP_NUMBER_OF_INSTANCES);
  319. LPUART_UART_IP_DEV_ASSERT(TxBuff != NULL_PTR);
  320. LPUART_UART_IP_DEV_ASSERT(TxSize > 0U);
  321. LPUART_Type * Base;
  322. Lpuart_Uart_Ip_StateStructureType * UartState;
  323. uint32 StartTime;
  324. uint32 TimeoutTicks;
  325. uint32 ElapsedTicks = 0;
  326. boolean IsReturn = FALSE;
  327. Lpuart_Uart_Ip_StatusType RetVal = LPUART_UART_IP_STATUS_SUCCESS;
  328. Base = Lpuart_Uart_Ip_apBases[Instance];
  329. UartState = (Lpuart_Uart_Ip_StateStructureType *)Lpuart_Uart_Ip_apStateStructuresArray[Instance];
  330. LPUART_UART_IP_DEV_ASSERT(UartState != NULL_PTR);
  331. SchM_Enter_Uart_UART_EXCLUSIVE_AREA_00();
  332. /* Check driver is not busy transmitting data from a previous asynchronous call */
  333. if (UartState->IsTxBusy)
  334. {
  335. SchM_Exit_Uart_UART_EXCLUSIVE_AREA_00();
  336. RetVal = LPUART_UART_IP_STATUS_BUSY;
  337. IsReturn = TRUE;
  338. }
  339. if (!IsReturn)
  340. {
  341. UartState->IsTxBusy = TRUE;
  342. SchM_Exit_Uart_UART_EXCLUSIVE_AREA_00();
  343. UartState->TxBuff = TxBuff;
  344. UartState->TxSize = TxSize;
  345. UartState->TransmitStatus = LPUART_UART_IP_STATUS_BUSY;
  346. /* Disble Tx data register empty and transmission complete interrupt */
  347. Lpuart_Uart_Ip_SetIntMode(Base, LPUART_UART_IP_INT_TX_DATA_REG_EMPTY, FALSE);
  348. Lpuart_Uart_Ip_SetIntMode(Base, LPUART_UART_IP_INT_TX_COMPLETE, FALSE);
  349. /* Enable the LPUART transmitter */
  350. Lpuart_Uart_Ip_SetTransmitterCmd(Base, TRUE);
  351. Lpuart_Uart_Ip_StartTimeout(&StartTime, &TimeoutTicks, Timeout, LPUART_UART_IP_TIMEOUT_TYPE);
  352. while ((UartState->TxSize > 0U) && \
  353. !Lpuart_Uart_Ip_CheckTimeout(&StartTime, &ElapsedTicks, TimeoutTicks, LPUART_UART_IP_TIMEOUT_TYPE))
  354. {
  355. Lpuart_Uart_Ip_PutData(Instance);
  356. while (!Lpuart_Uart_Ip_GetStatusFlag(Base, LPUART_UART_IP_TX_DATA_REG_EMPTY) && \
  357. !Lpuart_Uart_Ip_CheckTimeout(&StartTime, &ElapsedTicks, TimeoutTicks, LPUART_UART_IP_TIMEOUT_TYPE))
  358. {}
  359. }
  360. /* Disable the LPUART transmitter */
  361. Lpuart_Uart_Ip_SetTransmitterCmd(Base, FALSE);
  362. /* Check if Timeout occur */
  363. if (Lpuart_Uart_Ip_CheckTimeout(&StartTime, &ElapsedTicks, TimeoutTicks, LPUART_UART_IP_TIMEOUT_TYPE))
  364. {
  365. UartState->TransmitStatus = LPUART_UART_IP_STATUS_TIMEOUT;
  366. }else /* The transmit process is complete */
  367. {
  368. UartState->TransmitStatus = LPUART_UART_IP_STATUS_SUCCESS;
  369. }
  370. UartState->IsTxBusy = FALSE;
  371. RetVal = UartState->TransmitStatus;
  372. }
  373. return RetVal;
  374. }
  375. /*FUNCTION**********************************************************************
  376. *
  377. * Function Name : Lpuart_Uart_Ip_AsyncSend
  378. * Description : This function sends data out through the LPUART module using
  379. * non-blocking method. The function will return immediately after calling this
  380. * function.
  381. *
  382. * Implements : Lpuart_Uart_Ip_AsyncSend_Activity
  383. *END**************************************************************************/
  384. /* implements Lpuart_Uart_Ip_AsyncSend_Activity*/
  385. Lpuart_Uart_Ip_StatusType Lpuart_Uart_Ip_AsyncSend(const uint8 Instance,
  386. const uint8 * TxBuff,
  387. const uint32 TxSize)
  388. {
  389. /* Check the validity of the parameters */
  390. LPUART_UART_IP_DEV_ASSERT(Instance < LPUART_UART_IP_NUMBER_OF_INSTANCES);
  391. LPUART_UART_IP_DEV_ASSERT(TxBuff != NULL_PTR);
  392. LPUART_UART_IP_DEV_ASSERT(TxSize > 0U);
  393. const Lpuart_Uart_Ip_UserConfigType *UartUserCfg;
  394. Lpuart_Uart_Ip_StateStructureType * UartState;
  395. Lpuart_Uart_Ip_StatusType RetVal = LPUART_UART_IP_STATUS_SUCCESS;
  396. UartUserCfg = Lpuart_Uart_Ip_apUserConfig[Instance];
  397. UartState = (Lpuart_Uart_Ip_StateStructureType *)Lpuart_Uart_Ip_apStateStructuresArray[Instance];
  398. boolean IsReturn = FALSE;
  399. LPUART_UART_IP_DEV_ASSERT(UartState != NULL_PTR);
  400. SchM_Enter_Uart_UART_EXCLUSIVE_AREA_01();
  401. /* Check it's not busy transmitting data from a previous function call */
  402. if (UartState->IsTxBusy)
  403. {
  404. SchM_Exit_Uart_UART_EXCLUSIVE_AREA_01();
  405. RetVal = LPUART_UART_IP_STATUS_BUSY;
  406. IsReturn = TRUE;
  407. }
  408. if (!IsReturn)
  409. {
  410. UartState->IsTxBusy = TRUE;
  411. SchM_Exit_Uart_UART_EXCLUSIVE_AREA_01();
  412. LPUART_UART_IP_DEV_ASSERT(UartUserCfg != NULL_PTR);
  413. LPUART_UART_IP_DEV_ASSERT((LPUART_UART_IP_USING_INTERRUPTS == UartUserCfg->TransferType) ||
  414. (LPUART_UART_IP_USING_DMA == UartUserCfg->TransferType));
  415. if (LPUART_UART_IP_USING_INTERRUPTS == UartUserCfg->TransferType)
  416. {
  417. /* Start the transmission process using interrupts */
  418. RetVal = Lpuart_Uart_Ip_StartSendDataUsingInt(Instance, TxBuff, TxSize);
  419. }
  420. #if (LPUART_UART_IP_HAS_DMA_ENABLED == STD_ON)
  421. else
  422. {
  423. /* Start the transmission process using DMA */
  424. RetVal = Lpuart_Uart_Ip_StartSendDataUsingDma(Instance, TxBuff, TxSize);
  425. }
  426. #endif
  427. }
  428. return RetVal;
  429. }
  430. /*FUNCTION**********************************************************************
  431. *
  432. * Function Name : Lpuart_Uart_Ip_GetTransmitStatus
  433. * Description : This function returns whether the previous LPUART transmit has
  434. * finished. When performing non-blocking transmit, the user can call this
  435. * function to ascertain the state of the current transmission:
  436. * in progress (or busy) or complete (success). In addition, if the transmission
  437. * is still in progress, the user can obtain the number of words that have been
  438. * currently transferred.
  439. *
  440. * Implements : Lpuart_Uart_Ip_GetTransmitStatus_Activity
  441. *END**************************************************************************/
  442. /* implements Lpuart_Uart_Ip_GetTransmitStatus_Activity*/
  443. Lpuart_Uart_Ip_StatusType Lpuart_Uart_Ip_GetTransmitStatus(const uint8 Instance, uint32 * BytesRemaining)
  444. {
  445. LPUART_UART_IP_DEV_ASSERT(Instance < LPUART_UART_IP_NUMBER_OF_INSTANCES);
  446. const Lpuart_Uart_Ip_StateStructureType * UartState;
  447. const Lpuart_Uart_Ip_UserConfigType *UartUserCfg;
  448. #if (LPUART_UART_IP_HAS_DMA_ENABLED == STD_ON)
  449. const Dma_Ip_LogicChannelInfoParamType DmaLogicChnParam = DMA_IP_CH_GET_CURRENT_ITER_COUNT;
  450. #endif
  451. UartState = (const Lpuart_Uart_Ip_StateStructureType *)Lpuart_Uart_Ip_apStateStructuresArray[Instance];
  452. UartUserCfg = Lpuart_Uart_Ip_apUserConfig[Instance];
  453. LPUART_UART_IP_DEV_ASSERT(UartState != NULL_PTR);
  454. LPUART_UART_IP_DEV_ASSERT(UartUserCfg != NULL_PTR);
  455. if (BytesRemaining != NULL_PTR)
  456. {
  457. if (UartState->IsTxBusy)
  458. {
  459. /* Fill in the bytes not transferred yet. */
  460. if (LPUART_UART_IP_USING_INTERRUPTS == UartUserCfg->TransferType)
  461. {
  462. /* In interrupt-based communication, the remaining bytes are retrieved
  463. * from the state structure
  464. */
  465. *BytesRemaining = UartState->TxSize;;
  466. }
  467. #if (LPUART_UART_IP_HAS_DMA_ENABLED == STD_ON)
  468. else
  469. {
  470. /* In DMA-based communication, the remaining bytes are retrieved
  471. * from the current DMA major loop count
  472. */
  473. (void)Dma_Ip_GetLogicChannelParam(UartUserCfg->TxDMAChannel, DmaLogicChnParam, BytesRemaining);
  474. }
  475. #endif
  476. }
  477. else
  478. {
  479. *BytesRemaining = 0;
  480. }
  481. }
  482. return UartState->TransmitStatus;
  483. }
  484. /*FUNCTION**********************************************************************
  485. *
  486. * Function Name : Lpuart_Uart_Ip_AbortSendingData
  487. * Description : This function terminates an non-blocking LPUART transmission
  488. * early. During a non-blocking LPUART transmission, the user has the option to
  489. * terminate the transmission early if the transmission is still in progress.
  490. *
  491. * Implements : Lpuart_Uart_Ip_AbortSendingData_Activity
  492. *END**************************************************************************/
  493. /* implements Lpuart_Uart_Ip_AbortSendingData_Activity*/
  494. Lpuart_Uart_Ip_StatusType Lpuart_Uart_Ip_AbortSendingData(const uint8 Instance)
  495. {
  496. LPUART_UART_IP_DEV_ASSERT(Instance < LPUART_UART_IP_NUMBER_OF_INSTANCES);
  497. Lpuart_Uart_Ip_StateStructureType * UartState;
  498. const Lpuart_Uart_Ip_UserConfigType *UartUserCfg;
  499. LPUART_Type * Base = Lpuart_Uart_Ip_apBases[Instance];
  500. boolean IsReturn = FALSE;
  501. Lpuart_Uart_Ip_StatusType RetVal = LPUART_UART_IP_STATUS_SUCCESS;
  502. UartState = (Lpuart_Uart_Ip_StateStructureType *)Lpuart_Uart_Ip_apStateStructuresArray[Instance];
  503. UartUserCfg = Lpuart_Uart_Ip_apUserConfig[Instance];
  504. LPUART_UART_IP_DEV_ASSERT(UartState != NULL_PTR);
  505. LPUART_UART_IP_DEV_ASSERT(UartUserCfg != NULL_PTR);
  506. /* Check if a transfer is running. */
  507. if (!UartState->IsTxBusy)
  508. {
  509. RetVal = LPUART_UART_IP_STATUS_SUCCESS;
  510. IsReturn = TRUE;
  511. }
  512. if (!IsReturn)
  513. {
  514. /* Update the tx status */
  515. UartState->TransmitStatus = LPUART_UART_IP_STATUS_ABORTED;
  516. /* Stop the running transfer. */
  517. if (LPUART_UART_IP_USING_INTERRUPTS == UartUserCfg->TransferType)
  518. {
  519. Lpuart_Uart_Ip_CompleteSendDataUsingInt(Instance);
  520. }
  521. #if (LPUART_UART_IP_HAS_DMA_ENABLED == STD_ON)
  522. else
  523. {
  524. /* Release the DMA channel */
  525. (void)Dma_Ip_SetLogicChannelCommand(UartUserCfg->TxDMAChannel, DMA_IP_CH_CLEAR_HARDWARE_REQUEST);
  526. Lpuart_Uart_Ip_CompleteSendUsingDma(Instance);
  527. }
  528. #endif
  529. /* Flush the Tx Buffer */
  530. Lpuart_Uart_Ip_FlushTxBuffer(Base);
  531. RetVal = LPUART_UART_IP_STATUS_SUCCESS;
  532. }
  533. return RetVal;
  534. }
  535. /*FUNCTION**********************************************************************
  536. *
  537. * Function Name : Lpuart_Uart_Ip_SyncReceive
  538. * Description : Receive multiple bytes of data using polling method.
  539. *
  540. * Implements : Lpuart_Uart_Ip_SyncReceive_Activity
  541. *END**************************************************************************/
  542. /* implements Lpuart_Uart_Ip_SyncReceive_Activity*/
  543. Lpuart_Uart_Ip_StatusType Lpuart_Uart_Ip_SyncReceive(const uint8 Instance,
  544. uint8 *RxBuff,
  545. const uint32 RxSize,
  546. const uint32 Timeout)
  547. {
  548. /* Check the validity of the parameters */
  549. LPUART_UART_IP_DEV_ASSERT(Instance < LPUART_UART_IP_NUMBER_OF_INSTANCES);
  550. LPUART_UART_IP_DEV_ASSERT(RxBuff != NULL_PTR);
  551. LPUART_UART_IP_DEV_ASSERT(RxSize > 0U);
  552. LPUART_Type * Base = Lpuart_Uart_Ip_apBases[Instance];
  553. Lpuart_Uart_Ip_StateStructureType * UartState;
  554. uint32 StartTime;
  555. uint32 TimeoutTicks;
  556. uint32 ElapsedTicks = 0;
  557. boolean IsReturn = FALSE;
  558. Lpuart_Uart_Ip_StatusType RetVal = LPUART_UART_IP_STATUS_SUCCESS;
  559. UartState = (Lpuart_Uart_Ip_StateStructureType *)Lpuart_Uart_Ip_apStateStructuresArray[Instance];
  560. LPUART_UART_IP_DEV_ASSERT(UartState != NULL_PTR);
  561. SchM_Enter_Uart_UART_EXCLUSIVE_AREA_02();
  562. /* Check driver is not busy receiving data from a previous asynchronous call */
  563. if (UartState->IsRxBusy)
  564. {
  565. SchM_Exit_Uart_UART_EXCLUSIVE_AREA_02();
  566. RetVal = LPUART_UART_IP_STATUS_BUSY;
  567. IsReturn = TRUE;
  568. }
  569. if (!IsReturn)
  570. {
  571. UartState->IsRxBusy = TRUE;
  572. SchM_Exit_Uart_UART_EXCLUSIVE_AREA_02();
  573. UartState->RxBuff = RxBuff;
  574. UartState->RxSize = RxSize;
  575. UartState->ReceiveStatus = LPUART_UART_IP_STATUS_BUSY;
  576. /* Disble Rx data register full */
  577. Lpuart_Uart_Ip_SetIntMode(Base, LPUART_UART_IP_INT_RX_DATA_REG_FULL, FALSE);
  578. /* Enable the LPUART receiver */
  579. Lpuart_Uart_Ip_SetReceiverCmd((LPUART_Type *)Base, TRUE);
  580. Lpuart_Uart_Ip_StartTimeout(&StartTime, &TimeoutTicks, Timeout, LPUART_UART_IP_TIMEOUT_TYPE);
  581. Lpuart_Uart_Ip_StartGetData(Instance, &StartTime, &ElapsedTicks, TimeoutTicks);
  582. /* Check if Timeout occur */
  583. if (Lpuart_Uart_Ip_CheckTimeout(&StartTime, &ElapsedTicks, TimeoutTicks, LPUART_UART_IP_TIMEOUT_TYPE))
  584. {
  585. UartState->ReceiveStatus = LPUART_UART_IP_STATUS_TIMEOUT;
  586. }
  587. /* Check other success receiving case*/
  588. if (LPUART_UART_IP_STATUS_BUSY == UartState->ReceiveStatus)
  589. {
  590. UartState->ReceiveStatus = LPUART_UART_IP_STATUS_SUCCESS;
  591. }
  592. if ((0U == UartState->RxSize) && (LPUART_UART_IP_STATUS_RX_OVERRUN == UartState->ReceiveStatus))
  593. {
  594. UartState->ReceiveStatus = LPUART_UART_IP_STATUS_SUCCESS;
  595. }
  596. /* Disable the LPUART receiver */
  597. Lpuart_Uart_Ip_SetReceiverCmd((LPUART_Type *)Base, FALSE);
  598. UartState->IsRxBusy = FALSE;
  599. /* Read dummy to clear RDRF flag */
  600. (void)Lpuart_Uart_Ip_Getchar(Base);
  601. RetVal = UartState->ReceiveStatus;
  602. }
  603. return RetVal;
  604. }
  605. /*FUNCTION**********************************************************************
  606. *
  607. * Function Name : Lpuart_Uart_Ip_StartGetData
  608. * Description : Start Getting Data in SyncReceive mode
  609. *
  610. *END**************************************************************************/
  611. static void Lpuart_Uart_Ip_StartGetData(const uint8 Instance, uint32 * StartTime, uint32 * ElapsedTicks, uint32 TimeoutTicks)
  612. {
  613. Lpuart_Uart_Ip_StateStructureType * UartState;
  614. UartState = (Lpuart_Uart_Ip_StateStructureType *)Lpuart_Uart_Ip_apStateStructuresArray[Instance];
  615. LPUART_Type * Base = Lpuart_Uart_Ip_apBases[Instance];
  616. uint8 StatusIndex = 0U;
  617. uint32 Mask = 0U;
  618. const Lpuart_Uart_Ip_StatusFlagType Lpuart_Uart_Ip_StatusFlagTypeArray[4U] = {LPUART_UART_IP_RX_OVERRUN,
  619. LPUART_UART_IP_FRAME_ERR,
  620. LPUART_UART_IP_NOISE_DETECT,
  621. LPUART_UART_IP_PARITY_ERR};
  622. const Lpuart_Uart_Ip_StatusType Lpuart_Uart_Ip_ReceiveStatusArray[4U] = {LPUART_UART_IP_STATUS_RX_OVERRUN,
  623. LPUART_UART_IP_STATUS_FRAMING_ERROR,
  624. LPUART_UART_IP_STATUS_NOISE_ERROR,
  625. LPUART_UART_IP_STATUS_PARITY_ERROR};
  626. const uint32 Lpuart_Uart_Ip_StatusRegMask[4U] = {LPUART_STAT_OR_MASK,
  627. LPUART_STAT_FE_MASK,
  628. LPUART_STAT_NF_MASK,
  629. LPUART_STAT_PF_MASK};
  630. while ((UartState->RxSize > 0U) && \
  631. !Lpuart_Uart_Ip_CheckTimeout(StartTime, ElapsedTicks, TimeoutTicks, LPUART_UART_IP_TIMEOUT_TYPE))
  632. {
  633. /* Wait until data reception flag is set or timeout occurs if there is an error during reception */
  634. while (!Lpuart_Uart_Ip_GetStatusFlag((const LPUART_Type*)Base, LPUART_UART_IP_DATA_REG_FULL) && \
  635. !Lpuart_Uart_Ip_CheckTimeout(StartTime, ElapsedTicks, TimeoutTicks, LPUART_UART_IP_TIMEOUT_TYPE))
  636. {}
  637. /* Check for errors on received data */
  638. for(StatusIndex = 0U; StatusIndex <= 3U; StatusIndex++)
  639. {
  640. if(Lpuart_Uart_Ip_GetStatusFlag((const LPUART_Type*)Base, Lpuart_Uart_Ip_StatusFlagTypeArray[StatusIndex]))
  641. {
  642. UartState->ReceiveStatus = Lpuart_Uart_Ip_ReceiveStatusArray[StatusIndex];
  643. Mask |= Lpuart_Uart_Ip_StatusRegMask[StatusIndex];
  644. }
  645. }
  646. if(Mask > 0U)
  647. {
  648. /* Disable the LPUART receiver */
  649. Lpuart_Uart_Ip_SetReceiverCmd((LPUART_Type *)Base, FALSE);
  650. /* Clear the flag */
  651. Lpuart_Uart_Ip_ClearStatusFlagWithMask(Base, Mask);
  652. break;
  653. }
  654. else
  655. {
  656. /* Get received data */
  657. Lpuart_Uart_Ip_GetData(Instance);
  658. }
  659. }
  660. }
  661. /*FUNCTION**********************************************************************
  662. *
  663. * Function Name : Lpuart_Uart_Ip_ReceiveData
  664. * Description : This function receives data from LPUART module using
  665. * non-blocking method. This function returns immediately after initiating the
  666. * receive function. The application has to get the receive status to see when
  667. * the receive is complete. In other words, after calling non-blocking get
  668. * function, the application must get the receive status to check if receive
  669. * is completed or not.
  670. *
  671. * Implements : Lpuart_Uart_Ip_ReceiveData_Activity
  672. *END**************************************************************************/
  673. /* implements Lpuart_Uart_Ip_AsyncReceive_Activity*/
  674. Lpuart_Uart_Ip_StatusType Lpuart_Uart_Ip_AsyncReceive(const uint8 Instance,
  675. uint8 * RxBuff,
  676. const uint32 RxSize)
  677. {
  678. LPUART_UART_IP_DEV_ASSERT(Instance < LPUART_UART_IP_NUMBER_OF_INSTANCES);
  679. LPUART_UART_IP_DEV_ASSERT(RxBuff != NULL_PTR);
  680. LPUART_UART_IP_DEV_ASSERT(RxSize > 0U);
  681. const Lpuart_Uart_Ip_UserConfigType *UartUserCfg;
  682. Lpuart_Uart_Ip_StateStructureType * UartState;
  683. Lpuart_Uart_Ip_StatusType RetVal = LPUART_UART_IP_STATUS_SUCCESS;
  684. UartUserCfg = Lpuart_Uart_Ip_apUserConfig[Instance];
  685. UartState = (Lpuart_Uart_Ip_StateStructureType *)Lpuart_Uart_Ip_apStateStructuresArray[Instance];
  686. boolean IsReturn = FALSE;
  687. LPUART_UART_IP_DEV_ASSERT(UartState != NULL_PTR);
  688. SchM_Enter_Uart_UART_EXCLUSIVE_AREA_03();
  689. /* Check it's not busy receiving data from a previous function call */
  690. if (UartState->IsRxBusy)
  691. {
  692. SchM_Exit_Uart_UART_EXCLUSIVE_AREA_03();
  693. RetVal = LPUART_UART_IP_STATUS_BUSY;
  694. IsReturn = TRUE;
  695. }
  696. if (!IsReturn)
  697. {
  698. UartState->IsRxBusy = TRUE;
  699. SchM_Exit_Uart_UART_EXCLUSIVE_AREA_03();
  700. LPUART_UART_IP_DEV_ASSERT(UartUserCfg != NULL_PTR);
  701. LPUART_UART_IP_DEV_ASSERT((LPUART_UART_IP_USING_INTERRUPTS == UartUserCfg->TransferType) ||
  702. (LPUART_UART_IP_USING_DMA == UartUserCfg->TransferType));
  703. if (LPUART_UART_IP_USING_INTERRUPTS == UartUserCfg->TransferType)
  704. {
  705. /* Start the reception process using interrupts */
  706. RetVal = Lpuart_Uart_Ip_StartReceiveDataUsingInt(Instance, RxBuff, RxSize);
  707. }
  708. #if (LPUART_UART_IP_HAS_DMA_ENABLED == STD_ON)
  709. else
  710. {
  711. /* Start the reception process using DMA */
  712. RetVal = Lpuart_Uart_Ip_StartReceiveDataUsingDma(Instance, RxBuff, RxSize);
  713. }
  714. #endif
  715. }
  716. return RetVal;
  717. }
  718. /*FUNCTION**********************************************************************
  719. *
  720. * Function Name : Lpuart_Uart_Ip_GetReceiveStatus
  721. * Description : This function returns whether the previous LPUART receive is
  722. * complete. When performing a non-blocking receive, the user can call this
  723. * function to ascertain the state of the current receive progress: in progress
  724. * or complete. In addition, if the receive is still in progress, the user can
  725. * obtain the number of words that have been currently received.
  726. *
  727. * Implements : Lpuart_Uart_Ip_GetReceiveStatus_Activity
  728. *END**************************************************************************/
  729. /* implements Lpuart_Uart_Ip_GetReceiveStatus_Activity*/
  730. Lpuart_Uart_Ip_StatusType Lpuart_Uart_Ip_GetReceiveStatus(const uint8 Instance, uint32 * BytesRemaining)
  731. {
  732. LPUART_UART_IP_DEV_ASSERT(Instance < LPUART_UART_IP_NUMBER_OF_INSTANCES);
  733. const Lpuart_Uart_Ip_StateStructureType * UartState;
  734. const Lpuart_Uart_Ip_UserConfigType *UartUserCfg;
  735. #if (LPUART_UART_IP_HAS_DMA_ENABLED == STD_ON)
  736. const Dma_Ip_LogicChannelInfoParamType DmaLogicChnParam = DMA_IP_CH_GET_CURRENT_ITER_COUNT;
  737. #endif
  738. UartState = (const Lpuart_Uart_Ip_StateStructureType *)Lpuart_Uart_Ip_apStateStructuresArray[Instance];
  739. UartUserCfg = Lpuart_Uart_Ip_apUserConfig[Instance];
  740. LPUART_UART_IP_DEV_ASSERT(UartState != NULL_PTR);
  741. LPUART_UART_IP_DEV_ASSERT(UartUserCfg != NULL_PTR);
  742. if (BytesRemaining != NULL_PTR)
  743. {
  744. if (UartState->IsRxBusy)
  745. {
  746. /* Fill in the bytes transferred. */
  747. if (LPUART_UART_IP_USING_INTERRUPTS == UartUserCfg->TransferType)
  748. {
  749. /* In interrupt-based communication, the remaining bytes are retrieved
  750. * from the state structure
  751. */
  752. *BytesRemaining = UartState->RxSize;
  753. }
  754. #if (LPUART_UART_IP_HAS_DMA_ENABLED == STD_ON)
  755. else
  756. {
  757. /* In DMA-based communication, the remaining bytes are retrieved
  758. * from the current DMA major loop count
  759. */
  760. (void)Dma_Ip_GetLogicChannelParam(UartUserCfg->RxDMAChannel, DmaLogicChnParam, BytesRemaining);
  761. }
  762. #endif
  763. }
  764. else
  765. {
  766. *BytesRemaining = 0;
  767. }
  768. }
  769. return UartState->ReceiveStatus;
  770. }
  771. /*FUNCTION**********************************************************************
  772. *
  773. * Function Name : Lpuart_Uart_Ip_AbortReceivingData
  774. * Description : Terminates a non-blocking receive early.
  775. *
  776. * Implements : Lpuart_Uart_Ip_AbortReceivingData_Activity
  777. *END**************************************************************************/
  778. /* implements Lpuart_Uart_Ip_AbortReceivingData_Activity*/
  779. Lpuart_Uart_Ip_StatusType Lpuart_Uart_Ip_AbortReceivingData(const uint8 Instance)
  780. {
  781. LPUART_UART_IP_DEV_ASSERT(Instance < LPUART_UART_IP_NUMBER_OF_INSTANCES);
  782. Lpuart_Uart_Ip_StateStructureType * UartState;
  783. const Lpuart_Uart_Ip_UserConfigType *UartUserCfg;
  784. LPUART_Type * Base = Lpuart_Uart_Ip_apBases[Instance];
  785. UartState = (Lpuart_Uart_Ip_StateStructureType *)Lpuart_Uart_Ip_apStateStructuresArray[Instance];
  786. UartUserCfg = Lpuart_Uart_Ip_apUserConfig[Instance];
  787. LPUART_UART_IP_DEV_ASSERT(UartState != NULL_PTR);
  788. LPUART_UART_IP_DEV_ASSERT(UartUserCfg != NULL_PTR);
  789. boolean IsReturn = FALSE;
  790. Lpuart_Uart_Ip_StatusType RetVal = LPUART_UART_IP_STATUS_ERROR;
  791. /* Check if a transfer is running. */
  792. if (!UartState->IsRxBusy)
  793. {
  794. RetVal = LPUART_UART_IP_STATUS_SUCCESS;
  795. IsReturn = TRUE;
  796. }
  797. if (!IsReturn)
  798. {
  799. /* Update the rx status */
  800. UartState->ReceiveStatus = LPUART_UART_IP_STATUS_ABORTED;
  801. /* Stop the running transfer. */
  802. if (LPUART_UART_IP_USING_INTERRUPTS == UartUserCfg->TransferType)
  803. {
  804. Lpuart_Uart_Ip_CompleteReceiveDataUsingInt(Instance);
  805. }
  806. #if (LPUART_UART_IP_HAS_DMA_ENABLED == STD_ON)
  807. else
  808. {
  809. /* Release the DMA channel */
  810. (void)Dma_Ip_SetLogicChannelCommand(UartUserCfg->RxDMAChannel, DMA_IP_CH_CLEAR_HARDWARE_REQUEST);
  811. Lpuart_Uart_Ip_CompleteReceiveUsingDma(Instance);
  812. }
  813. #endif
  814. /* Clear all the error flags */
  815. Lpuart_Uart_Ip_ClearErrorFlags(Base);
  816. /* Flush the Rx Buffer */
  817. Lpuart_Uart_Ip_FlushRxBuffer(Base);
  818. RetVal = LPUART_UART_IP_STATUS_SUCCESS;
  819. }
  820. return RetVal;
  821. }
  822. /*FUNCTION**********************************************************************
  823. *
  824. * Function Name : Lpuart_Uart_Ip_SetBaudRate
  825. * Description : Configures the LPUART baud rate.
  826. * In some LPUART instances the user must disable the transmitter/receiver
  827. * before calling this function.
  828. * Generally, this may be applied to all LPUARTs to ensure safe operation.
  829. *
  830. * Implements : Lpuart_Uart_Ip_SetBaudRate_Activity
  831. *END**************************************************************************/
  832. /* implements Lpuart_Uart_Ip_SetBaudRate_Activity*/
  833. Lpuart_Uart_Ip_StatusType Lpuart_Uart_Ip_SetBaudRate(const uint8 Instance,
  834. const Lpuart_Uart_Ip_BaudrateType DesiredBaudrate,
  835. const uint32 ClockFrequency)
  836. {
  837. LPUART_UART_IP_DEV_ASSERT(Instance < LPUART_UART_IP_NUMBER_OF_INSTANCES);
  838. uint16 Sbr;
  839. uint16 Idx;
  840. uint16 SbrTemp;
  841. uint32 Osr;
  842. uint32 TempDiff;
  843. uint32 CalculatedBaud;
  844. uint32 BaudDiff;
  845. uint32 MaxOsr;
  846. uint32 ExpectedBaud = (uint32)DesiredBaudrate;
  847. LPUART_Type * Base = Lpuart_Uart_Ip_apBases[Instance];
  848. Lpuart_Uart_Ip_StateStructureType * UartState;
  849. UartState = (Lpuart_Uart_Ip_StateStructureType *)Lpuart_Uart_Ip_apStateStructuresArray[Instance];
  850. Lpuart_Uart_Ip_StatusType Status = LPUART_UART_IP_STATUS_ERROR;
  851. LPUART_UART_IP_DEV_ASSERT(UartState != NULL_PTR);
  852. if ((TRUE == UartState->IsTxBusy) || (TRUE == UartState->IsRxBusy))
  853. {
  854. Status = LPUART_UART_IP_STATUS_BUSY;
  855. }
  856. else
  857. {
  858. /* Check if current instance is clock gated off. */
  859. LPUART_UART_IP_DEV_ASSERT(ClockFrequency > 0U);
  860. /* Check if the desired baud rate can be configured with the current protocol clock. */
  861. LPUART_UART_IP_DEV_ASSERT(ClockFrequency >= (ExpectedBaud * 5U));
  862. /* This lpuart instantiation uses a slightly different baud rate calculation
  863. * The idea is to use the best OSR (over-sampling rate) possible
  864. * Note, Osr is typically hard-set to 16 in other lpuart instantiations
  865. * First calculate the baud rate using the minimum OSR possible (4) */
  866. Osr = 4;
  867. Sbr = (uint16)(ClockFrequency / (ExpectedBaud * Osr));
  868. CalculatedBaud = (ClockFrequency / ((uint32)Osr * (uint32)Sbr));
  869. if (CalculatedBaud > ExpectedBaud)
  870. {
  871. BaudDiff = CalculatedBaud - ExpectedBaud;
  872. }
  873. else
  874. {
  875. BaudDiff = ExpectedBaud - CalculatedBaud;
  876. }
  877. /* find maximum Osr */
  878. MaxOsr = ClockFrequency / ExpectedBaud;
  879. if (MaxOsr > 32U)
  880. {
  881. MaxOsr = 32U;
  882. }
  883. /* loop to find the best Osr value possible, one that generates minimum BaudDiff
  884. * iterate through the rest of the supported values of Osr */
  885. if (MaxOsr >= 5U)
  886. {
  887. for (Idx = 5U; Idx <= MaxOsr; Idx++)
  888. {
  889. /* calculate the temporary Sbr value */
  890. SbrTemp = (uint16)(ClockFrequency / (ExpectedBaud * Idx));
  891. /* calculate the baud rate based on the temporary Osr and Sbr values */
  892. CalculatedBaud = (ClockFrequency / ((uint32)Idx * (uint32)SbrTemp));
  893. if (CalculatedBaud > ExpectedBaud)
  894. {
  895. TempDiff = CalculatedBaud - ExpectedBaud;
  896. }
  897. else
  898. {
  899. TempDiff = ExpectedBaud - CalculatedBaud;
  900. }
  901. if (TempDiff <= BaudDiff)
  902. {
  903. BaudDiff = TempDiff;
  904. Osr = Idx; /* update and store the best Osr value calculated */
  905. Sbr = SbrTemp; /* update store the best Sbr value calculated */
  906. }
  907. }
  908. }
  909. /* Check if Osr is between 4x and 7x oversampling.
  910. * If so, then "BOTHEDGE" sampling must be turned on */
  911. if (Osr < 8U)
  912. {
  913. Lpuart_Uart_Ip_EnableBothEdgeSamplingCmd(Base);
  914. }
  915. /* program the Osr value (bit value is one less than actual value) */
  916. Lpuart_Uart_Ip_SetOversamplingRatio(Base, (Osr - 1U));
  917. /* write the Sbr value to the BAUD registers */
  918. Lpuart_Uart_Ip_SetBaudRateDivisor(Base, Sbr);
  919. UartState->BaudRate = CalculatedBaud;
  920. Status = LPUART_UART_IP_STATUS_SUCCESS;
  921. }
  922. return Status;
  923. }
  924. /*FUNCTION**********************************************************************
  925. *
  926. * Function Name : Lpuart_Uart_Ip_GetBaudRate
  927. * Description : Returns the LPUART configured baud rate.
  928. *
  929. * Implements : Lpuart_Uart_Ip_GetBaudRate_Activity
  930. *END**************************************************************************/
  931. /* implements Lpuart_Uart_Ip_GetBaudRate_Activity*/
  932. void Lpuart_Uart_Ip_GetBaudRate(const uint8 Instance, uint32 * ConfiguredBaudRate)
  933. {
  934. LPUART_UART_IP_DEV_ASSERT(Instance < LPUART_UART_IP_NUMBER_OF_INSTANCES);
  935. LPUART_UART_IP_DEV_ASSERT(ConfiguredBaudRate != NULL_PTR);
  936. const Lpuart_Uart_Ip_StateStructureType * UartState;
  937. UartState = (const Lpuart_Uart_Ip_StateStructureType *)Lpuart_Uart_Ip_apStateStructuresArray[Instance];
  938. *ConfiguredBaudRate = UartState->BaudRate;
  939. }
  940. /*FUNCTION**********************************************************************
  941. *
  942. * Function Name : Lpuart_Uart_Ip_SetTxBuffer
  943. * Description : Sets the driver internal reference to the tx buffer.
  944. * Can be called from the tx callback to provide a different
  945. * buffer for continuous transmission.
  946. *
  947. * Implements : Lpuart_Uart_Ip_SetTxBuffer_Activity
  948. *END**************************************************************************/
  949. /* implements Lpuart_Uart_Ip_SetTxBuffer_Activity*/
  950. Lpuart_Uart_Ip_StatusType Lpuart_Uart_Ip_SetTxBuffer(const uint8 Instance,
  951. const uint8 * TxBuff,
  952. const uint32 TxSize)
  953. {
  954. LPUART_UART_IP_DEV_ASSERT(Instance < LPUART_UART_IP_NUMBER_OF_INSTANCES);
  955. LPUART_UART_IP_DEV_ASSERT(TxBuff != NULL_PTR);
  956. LPUART_UART_IP_DEV_ASSERT(TxSize > 0U);
  957. Lpuart_Uart_Ip_StateStructureType * UartState;
  958. UartState = (Lpuart_Uart_Ip_StateStructureType *)Lpuart_Uart_Ip_apStateStructuresArray[Instance];
  959. UartState->TxBuff = TxBuff;
  960. UartState->TxSize = TxSize;
  961. return LPUART_UART_IP_STATUS_SUCCESS;
  962. }
  963. /*FUNCTION**********************************************************************
  964. *
  965. * Function Name : Lpuart_Uart_Ip_SetRxBuffer
  966. * Description : Sets the driver internal reference to the rx buffer.
  967. * Can be called from the rx callback to provide a different
  968. * buffer for continuous reception.
  969. *
  970. * Implements : Lpuart_Uart_Ip_SetRxBuffer_Activity
  971. *END**************************************************************************/
  972. /* implements Lpuart_Uart_Ip_SetRxBuffer_Activity*/
  973. Lpuart_Uart_Ip_StatusType Lpuart_Uart_Ip_SetRxBuffer(const uint8 Instance,
  974. uint8 * RxBuff,
  975. const uint32 RxSize)
  976. {
  977. LPUART_UART_IP_DEV_ASSERT(Instance < LPUART_UART_IP_NUMBER_OF_INSTANCES);
  978. LPUART_UART_IP_DEV_ASSERT(RxBuff != NULL_PTR);
  979. LPUART_UART_IP_DEV_ASSERT(RxSize > 0U);
  980. Lpuart_Uart_Ip_StateStructureType * UartState;
  981. UartState = (Lpuart_Uart_Ip_StateStructureType *)Lpuart_Uart_Ip_apStateStructuresArray[Instance];
  982. UartState->RxBuff = RxBuff;
  983. UartState->RxSize = RxSize;
  984. return LPUART_UART_IP_STATUS_SUCCESS;
  985. }
  986. //zhengchao
  987. static void Lpuart_Uart_Ip_RxIdleIrqHandler(uint32 Instance)
  988. {
  989. Lpuart_Uart_Ip_StateStructureType * UartState;
  990. const Lpuart_Uart_Ip_UserConfigType *UartUserCfg;
  991. LPUART_Type * Base;
  992. Base = Lpuart_Uart_Ip_apBases[Instance];
  993. UartState = (Lpuart_Uart_Ip_StateStructureType *)Lpuart_Uart_Ip_apStateStructuresArray[Instance];
  994. UartUserCfg = (Lpuart_Uart_Ip_UserConfigType*) Lpuart_Uart_Ip_apUserConfig[Instance];
  995. // /* Disable receive idle interrupt. */
  996. // Lpuart_Uart_Ip_SetIntMode(Base, (uint32)LPUART_CTRL_ILIE_SHIFT, FALSE);
  997. /* Invoke callback if there is one */
  998. /* Clear idle flag*/
  999. Lpuart_Uart_Ip_ClearStatusFlag(Base, LPUART_UART_IP_IDLE);
  1000. if (UartUserCfg->Callback != NULL_PTR)
  1001. {
  1002. UartUserCfg->Callback(Instance, LPUART_UART_IP_EVENT_RECV_IDLE,UartUserCfg->CallbackParam);
  1003. }
  1004. UartState->ReceiveStatus = LPUART_UART_IP_STATUS_SUCCESS;
  1005. Lpuart_Uart_Ip_CompleteReceiveUsingDma(Instance);
  1006. }
  1007. /*FUNCTION**********************************************************************
  1008. *
  1009. * Function Name : Lpuart_Uart_Ip_IrqHandler
  1010. * Description : Interrupt handler for LPUART.
  1011. * This handler uses the buffers stored in the Lpuart_Uart_Ip_StateStructureType structs to transfer
  1012. * data. This is not a public API as it is called by IRQ whenever an interrupt
  1013. * occurs.
  1014. *
  1015. *END**************************************************************************/
  1016. /* implements Lpuart_Uart_Ip_IrqHandler_Activity*/
  1017. void Lpuart_Uart_Ip_IrqHandler(const uint8 Instance)
  1018. {
  1019. LPUART_UART_IP_DEV_ASSERT(Instance < LPUART_UART_IP_NUMBER_OF_INSTANCES);
  1020. LPUART_Type * Base;
  1021. const Lpuart_Uart_Ip_StateStructureType * UartState;
  1022. Base = Lpuart_Uart_Ip_apBases[Instance];
  1023. UartState = (const Lpuart_Uart_Ip_StateStructureType *)Lpuart_Uart_Ip_apStateStructuresArray[Instance];
  1024. boolean IsReturn = FALSE;
  1025. /* Case of spurious interrupt when driver is not at all initialized or it is not in transmit/receive process*/
  1026. if (NULL_PTR == UartState)
  1027. {
  1028. /* Read dummy to clear RDRF flag if it is set*/
  1029. (void)Lpuart_Uart_Ip_Getchar(Base);
  1030. /* Clear all the error flags */
  1031. Lpuart_Uart_Ip_ClearErrorFlags(Base);
  1032. /* Transmit flags can not be cleared, return to exit */
  1033. }
  1034. else
  1035. {
  1036. Lpuart_Uart_Ip_ErrIrqHandler(Instance);
  1037. /* Handle receive data full interrupt */
  1038. if (Lpuart_Uart_Ip_GetStatusFlag(Base, LPUART_UART_IP_DATA_REG_FULL))
  1039. {
  1040. if (Lpuart_Uart_Ip_GetIntMode(Base, LPUART_UART_IP_INT_RX_DATA_REG_FULL))
  1041. {
  1042. Lpuart_Uart_Ip_RxIrqHandler(Instance);
  1043. }
  1044. /* Case of spurious interrupt when the interupt enable flag is not set and respective interrupt status flag is set */
  1045. else
  1046. {
  1047. /* Read dummy to clear RDRF flag */
  1048. (void)Lpuart_Uart_Ip_Getchar(Base);
  1049. }
  1050. IsReturn = TRUE;
  1051. }
  1052. /* Handle transmitter data register empty interrupt */
  1053. if (Lpuart_Uart_Ip_GetStatusFlag(Base, LPUART_UART_IP_TX_DATA_REG_EMPTY) && (FALSE == IsReturn))
  1054. {
  1055. if (Lpuart_Uart_Ip_GetIntMode(Base, LPUART_UART_IP_INT_TX_DATA_REG_EMPTY))
  1056. {
  1057. Lpuart_Uart_Ip_TxEmptyIrqHandler(Instance);
  1058. IsReturn = TRUE;
  1059. }
  1060. /* Case of spurious interrupt when the interupt enable flag is not set and respective interrupt status flag is set */
  1061. else
  1062. {
  1063. /* Do nothing, because TDRE can not clear without affecting to normal operation*/
  1064. }
  1065. }
  1066. /* Handle transmission complete interrupt */
  1067. if (Lpuart_Uart_Ip_GetStatusFlag(Base, LPUART_UART_IP_TX_COMPLETE) && (FALSE == IsReturn))
  1068. {
  1069. if (Lpuart_Uart_Ip_GetIntMode(Base, LPUART_UART_IP_INT_TX_COMPLETE))
  1070. {
  1071. Lpuart_Uart_Ip_TxCompleteIrqHandler(Instance);
  1072. }
  1073. /* Case of spurious interrupt when the interupt enable flag is not set and respective interrupt status flag is set */
  1074. else
  1075. {
  1076. /* Do nothing, because TC can not clear without affecting to normal operation*/
  1077. }
  1078. }
  1079. /*Handle receiving data idle interrupt*///zhengchao
  1080. if (Lpuart_Uart_Ip_GetStatusFlag(Base, LPUART_UART_IP_IDLE) && (FALSE == IsReturn))
  1081. {
  1082. if (Lpuart_Uart_Ip_GetIntMode(Base, LPUART_UART_IP_IDLE))
  1083. {
  1084. Lpuart_Uart_Ip_RxIdleIrqHandler(Instance);
  1085. }
  1086. else
  1087. {
  1088. /* Do nothing*/
  1089. }
  1090. }
  1091. }
  1092. }
  1093. /*FUNCTION**********************************************************************
  1094. *
  1095. * Function Name : Lpuart_Uart_Ip_RxIrqHandler
  1096. * Description : Rx Interrupt handler for LPUART.
  1097. * This function treats the rx full interrupt.
  1098. *
  1099. *END**************************************************************************/
  1100. static void Lpuart_Uart_Ip_RxIrqHandler(const uint8 Instance)
  1101. {
  1102. const Lpuart_Uart_Ip_StateStructureType * UartState;
  1103. const Lpuart_Uart_Ip_UserConfigType *UartUserCfg;
  1104. UartState = (Lpuart_Uart_Ip_StateStructureType *)Lpuart_Uart_Ip_apStateStructuresArray[Instance];
  1105. UartUserCfg = Lpuart_Uart_Ip_apUserConfig[Instance];
  1106. /* Get data and put in receive buffer */
  1107. Lpuart_Uart_Ip_GetData(Instance);
  1108. /* Check if this was the last byte in the current buffer */
  1109. if (0U == UartState->RxSize)
  1110. {
  1111. /* Invoke callback if there is one (callback may reset the rx buffer for continuous reception) */
  1112. if (UartUserCfg->Callback != NULL_PTR)
  1113. {
  1114. UartUserCfg->Callback(Instance, LPUART_UART_IP_EVENT_RX_FULL, UartUserCfg->CallbackParam);
  1115. }
  1116. }
  1117. /* Finish reception if this was the last byte received */
  1118. if (0U == UartState->RxSize)
  1119. {
  1120. /* Complete transfer (disable rx logic) */
  1121. Lpuart_Uart_Ip_CompleteReceiveDataUsingInt(Instance);
  1122. /* Invoke callback if there is one */
  1123. if (UartUserCfg->Callback != NULL_PTR)
  1124. {
  1125. UartUserCfg->Callback(Instance, LPUART_UART_IP_EVENT_END_TRANSFER, UartUserCfg->CallbackParam);
  1126. }
  1127. }
  1128. }
  1129. /*FUNCTION**********************************************************************
  1130. *
  1131. * Function Name : Lpuart_Uart_Ip_TxEmptyIrqHandler
  1132. * Description : Tx Empty Interrupt handler for LPUART.
  1133. * This function treats the tx empty interrupt.
  1134. *
  1135. *END**************************************************************************/
  1136. static void Lpuart_Uart_Ip_TxEmptyIrqHandler(const uint8 Instance)
  1137. {
  1138. const Lpuart_Uart_Ip_StateStructureType * UartState;
  1139. const Lpuart_Uart_Ip_UserConfigType *UartUserCfg;
  1140. LPUART_Type * Base;
  1141. Base = Lpuart_Uart_Ip_apBases[Instance];
  1142. UartState = (Lpuart_Uart_Ip_StateStructureType *)Lpuart_Uart_Ip_apStateStructuresArray[Instance];
  1143. UartUserCfg = Lpuart_Uart_Ip_apUserConfig[Instance];
  1144. /* Check if there are any more bytes to send */
  1145. if (UartState->TxSize > 0U)
  1146. {
  1147. /* Transmit the data */
  1148. Lpuart_Uart_Ip_PutData(Instance);
  1149. /* Check if this was the last byte in the current buffer */
  1150. if ((0U == UartState->TxSize) && (UartUserCfg->Callback != NULL_PTR))
  1151. {
  1152. /* Invoke callback if there is one (callback may reset the tx buffer for continuous transmission)*/
  1153. UartUserCfg->Callback(Instance, LPUART_UART_IP_EVENT_TX_EMPTY, UartUserCfg->CallbackParam);
  1154. }
  1155. /* If there's no new data, disable tx empty interrupt and enable transmission complete interrupt */
  1156. if (0U == UartState->TxSize)
  1157. {
  1158. Lpuart_Uart_Ip_SetIntMode(Base, LPUART_UART_IP_INT_TX_DATA_REG_EMPTY, FALSE);
  1159. Lpuart_Uart_Ip_SetIntMode(Base, LPUART_UART_IP_INT_TX_COMPLETE, TRUE);
  1160. }
  1161. }
  1162. }
  1163. /*FUNCTION**********************************************************************
  1164. *
  1165. * Function Name : Lpuart_Uart_Ip_TxCompleteIrqHandler
  1166. * Description : Tx Complete Interrupt handler for LPUART.
  1167. * This function treats the tx complete interrupt.
  1168. *
  1169. *END**************************************************************************/
  1170. static void Lpuart_Uart_Ip_TxCompleteIrqHandler(const uint8 Instance)
  1171. {
  1172. const Lpuart_Uart_Ip_StateStructureType * UartState;
  1173. const Lpuart_Uart_Ip_UserConfigType *UartUserCfg;
  1174. UartState = (Lpuart_Uart_Ip_StateStructureType *)Lpuart_Uart_Ip_apStateStructuresArray[Instance];
  1175. UartUserCfg = Lpuart_Uart_Ip_apUserConfig[Instance];
  1176. if (0U == UartState->TxSize)
  1177. {
  1178. if (LPUART_UART_IP_USING_INTERRUPTS == UartUserCfg->TransferType)
  1179. {
  1180. /* Complete the interrupt based transfer */
  1181. Lpuart_Uart_Ip_CompleteSendDataUsingInt(Instance);
  1182. }
  1183. /* Invoke callback if there is one */
  1184. if (UartUserCfg->Callback != NULL_PTR)
  1185. {
  1186. UartUserCfg->Callback(Instance, LPUART_UART_IP_EVENT_END_TRANSFER, UartUserCfg->CallbackParam);
  1187. }
  1188. }
  1189. }
  1190. /*FUNCTION**********************************************************************
  1191. *
  1192. * Function Name : Lpuart_Uart_Ip_ErrIrqHandler
  1193. * Description : Error Interrupt handler for LPUART.
  1194. * This function treats the error interrupts.
  1195. *
  1196. *END**************************************************************************/
  1197. static void Lpuart_Uart_Ip_ErrIrqHandler(const uint8 Instance)
  1198. {
  1199. Lpuart_Uart_Ip_StateStructureType * UartState;
  1200. const Lpuart_Uart_Ip_UserConfigType *UartUserCfg;
  1201. LPUART_Type * Base;
  1202. boolean IsError = FALSE;
  1203. boolean IsReturn = FALSE;
  1204. Base = Lpuart_Uart_Ip_apBases[Instance];
  1205. UartState = (Lpuart_Uart_Ip_StateStructureType *)Lpuart_Uart_Ip_apStateStructuresArray[Instance];
  1206. UartUserCfg = Lpuart_Uart_Ip_apUserConfig[Instance];
  1207. /* Handle receive overrun interrupt */
  1208. if (Lpuart_Uart_Ip_GetStatusFlag(Base, LPUART_UART_IP_RX_OVERRUN))
  1209. {
  1210. /* Clear the flag */
  1211. Lpuart_Uart_Ip_ClearStatusFlag(Base, LPUART_UART_IP_RX_OVERRUN);
  1212. /* Case of spurious interrupt when interrupt enable bit is not set*/
  1213. if (!Lpuart_Uart_Ip_GetIntMode(Base, LPUART_UART_IP_INT_RX_OVERRUN))
  1214. {
  1215. IsReturn = TRUE;
  1216. }
  1217. else
  1218. {
  1219. /* Update the status */
  1220. IsError = TRUE;
  1221. UartState->ReceiveStatus = LPUART_UART_IP_STATUS_RX_OVERRUN;
  1222. }
  1223. }
  1224. /* Handle framing error interrupt */
  1225. if (Lpuart_Uart_Ip_GetStatusFlag(Base, LPUART_UART_IP_FRAME_ERR) && (FALSE == IsReturn))
  1226. {
  1227. /* Clear the flag */
  1228. Lpuart_Uart_Ip_ClearStatusFlag(Base, LPUART_UART_IP_FRAME_ERR);
  1229. /* Case of spurious interrupt when interrupt enable bit is not set*/
  1230. if (!Lpuart_Uart_Ip_GetIntMode(Base, LPUART_UART_IP_INT_FRAME_ERR_FLAG))
  1231. {
  1232. IsReturn = TRUE;
  1233. }
  1234. else
  1235. {
  1236. /* Update the status */
  1237. IsError = TRUE;
  1238. UartState->ReceiveStatus = LPUART_UART_IP_STATUS_FRAMING_ERROR;
  1239. }
  1240. }
  1241. /* Handle parity error interrupt */
  1242. if (Lpuart_Uart_Ip_GetStatusFlag(Base, LPUART_UART_IP_PARITY_ERR) && (FALSE == IsReturn))
  1243. {
  1244. /* Clear the flag */
  1245. Lpuart_Uart_Ip_ClearStatusFlag(Base, LPUART_UART_IP_PARITY_ERR);
  1246. /* Case of spurious interrupt when interrupt enable bit is not set*/
  1247. if (!Lpuart_Uart_Ip_GetIntMode(Base, LPUART_UART_IP_INT_PARITY_ERR_FLAG))
  1248. {
  1249. IsReturn = TRUE;
  1250. }
  1251. else
  1252. {
  1253. /* Update the status */
  1254. IsError = TRUE;
  1255. UartState->ReceiveStatus = LPUART_UART_IP_STATUS_PARITY_ERROR;
  1256. }
  1257. }
  1258. /* Handle noise error interrupt */
  1259. if (Lpuart_Uart_Ip_GetStatusFlag(Base, LPUART_UART_IP_NOISE_DETECT) && (FALSE == IsReturn))
  1260. {
  1261. /* Clear the flag */
  1262. Lpuart_Uart_Ip_ClearStatusFlag(Base, LPUART_UART_IP_NOISE_DETECT);
  1263. /* Case of spurious interrupt when interrupt enable bit is not set*/
  1264. if (!Lpuart_Uart_Ip_GetIntMode(Base, LPUART_UART_IP_INT_NOISE_ERR_FLAG))
  1265. {
  1266. IsReturn = TRUE;
  1267. }
  1268. else
  1269. {
  1270. /* Update the internal status */
  1271. IsError = TRUE;
  1272. UartState->ReceiveStatus = LPUART_UART_IP_STATUS_NOISE_ERROR;
  1273. }
  1274. }
  1275. if (FALSE == IsReturn)
  1276. {
  1277. if (TRUE == IsError)
  1278. {
  1279. if (LPUART_UART_IP_USING_INTERRUPTS == UartUserCfg->TransferType)
  1280. {
  1281. /* Complete the transfer (disable rx logic) */
  1282. Lpuart_Uart_Ip_CompleteReceiveDataUsingInt(Instance);
  1283. }
  1284. #if (LPUART_UART_IP_HAS_DMA_ENABLED == STD_ON)
  1285. else
  1286. {
  1287. /* Complete the transfer (stop DMA channel) */
  1288. (void)Dma_Ip_SetLogicChannelCommand(UartUserCfg->RxDMAChannel, DMA_IP_CH_CLEAR_HARDWARE_REQUEST);
  1289. Lpuart_Uart_Ip_CompleteReceiveUsingDma(Instance);
  1290. }
  1291. #endif
  1292. /* Invoke callback if there is one */
  1293. if (UartUserCfg->Callback != NULL_PTR)
  1294. {
  1295. UartUserCfg->Callback(Instance, LPUART_UART_IP_EVENT_ERROR, UartUserCfg->CallbackParam);
  1296. }
  1297. }
  1298. }
  1299. }
  1300. /*FUNCTION**********************************************************************
  1301. *
  1302. * Function Name : Lpuart_Uart_Ip_StartSendDataUsingInt
  1303. * Description : Initiate (start) a transmit by beginning the process of
  1304. * sending data and enabling the interrupt.
  1305. * This is not a public API as it is called from other driver functions.
  1306. *
  1307. *END**************************************************************************/
  1308. static Lpuart_Uart_Ip_StatusType Lpuart_Uart_Ip_StartSendDataUsingInt(const uint8 Instance,
  1309. const uint8 * TxBuff,
  1310. const uint32 TxSize)
  1311. {
  1312. LPUART_Type * Base;
  1313. Lpuart_Uart_Ip_StateStructureType * UartState;
  1314. Base = Lpuart_Uart_Ip_apBases[Instance];
  1315. UartState = (Lpuart_Uart_Ip_StateStructureType *)Lpuart_Uart_Ip_apStateStructuresArray[Instance];
  1316. /* initialize the module driver state structure */
  1317. UartState->TxBuff = TxBuff;
  1318. UartState->TxSize = TxSize;
  1319. UartState->TransmitStatus = LPUART_UART_IP_STATUS_BUSY;
  1320. /* Enable the LPUART transmitter */
  1321. Lpuart_Uart_Ip_SetTransmitterCmd(Base, TRUE);
  1322. /* Enable tx empty interrupt */
  1323. Lpuart_Uart_Ip_SetIntMode(Base, LPUART_UART_IP_INT_TX_DATA_REG_EMPTY, TRUE);
  1324. return LPUART_UART_IP_STATUS_SUCCESS;
  1325. }
  1326. #if (LPUART_UART_IP_HAS_DMA_ENABLED == STD_ON)
  1327. /*FUNCTION**********************************************************************
  1328. *
  1329. * Function Name : Lpuart_Uart_Ip_StartSendDataUsingDma
  1330. * Description : Initiate (start) a transmit by beginning the process of
  1331. * sending data using DMA transfers.
  1332. * This is not a public API as it is called from other driver functions.
  1333. *
  1334. *END**************************************************************************/
  1335. static Lpuart_Uart_Ip_StatusType Lpuart_Uart_Ip_StartSendDataUsingDma(const uint8 Instance,
  1336. const uint8 * TxBuff,
  1337. const uint32 TxSize)
  1338. {
  1339. Lpuart_Uart_Ip_StateStructureType * UartState;
  1340. const Lpuart_Uart_Ip_UserConfigType *UartUserCfg;
  1341. LPUART_Type * Base;
  1342. Dma_Ip_LogicChannelTransferListType DmaTransferList[LPUART_UART_IP_DMA_CONFIG_LIST_DIMENSION];
  1343. Dma_Ip_ReturnType DmaReturnStatus;
  1344. Base = Lpuart_Uart_Ip_apBases[Instance];
  1345. UartState = (Lpuart_Uart_Ip_StateStructureType *)Lpuart_Uart_Ip_apStateStructuresArray[Instance];
  1346. UartUserCfg = Lpuart_Uart_Ip_apUserConfig[Instance];
  1347. /* Set up parameters for Dma_Ip_LogicChannelTransferListType */
  1348. DmaTransferList[0].Param = DMA_IP_CH_SET_SOURCE_ADDRESS;
  1349. DmaTransferList[0].Value = (uint32)TxBuff;
  1350. DmaTransferList[1].Param = DMA_IP_CH_SET_DESTINATION_ADDRESS;
  1351. DmaTransferList[1].Value = LPUART_UART_IP_LSBW_ADDR(Base->DATA);
  1352. DmaTransferList[2].Param = DMA_IP_CH_SET_SOURCE_SIGNED_OFFSET;
  1353. DmaTransferList[2].Value = 1;
  1354. DmaTransferList[3].Param = DMA_IP_CH_SET_DESTINATION_SIGNED_OFFSET;
  1355. DmaTransferList[3].Value = 0;
  1356. DmaTransferList[4].Param = DMA_IP_CH_SET_MAJORLOOP_COUNT;
  1357. DmaTransferList[4].Value = TxSize;
  1358. DmaTransferList[5].Param = DMA_IP_CH_SET_MINORLOOP_SIZE;
  1359. DmaTransferList[5].Value = 1;
  1360. DmaTransferList[6].Param = DMA_IP_CH_SET_DESTINATION_TRANSFER_SIZE;
  1361. DmaTransferList[6].Value = DMA_IP_TRANSFER_SIZE_1_BYTE;
  1362. DmaTransferList[7].Param = DMA_IP_CH_SET_SOURCE_TRANSFER_SIZE;
  1363. DmaTransferList[7].Value = DMA_IP_TRANSFER_SIZE_1_BYTE;
  1364. DmaTransferList[8].Param = DMA_IP_CH_SET_CONTROL_EN_MAJOR_INTERRUPT;
  1365. DmaTransferList[8].Value = 1;
  1366. DmaTransferList[9].Param = DMA_IP_CH_SET_CONTROL_DIS_AUTO_REQUEST;
  1367. DmaTransferList[9].Value = 1;
  1368. /* Update state structure */
  1369. UartState->TxBuff = TxBuff;
  1370. UartState->TxSize = 0U;
  1371. UartState->TransmitStatus = LPUART_UART_IP_STATUS_BUSY;
  1372. /* Configure the transfer control descriptor for the DMA channel */
  1373. DmaReturnStatus = Dma_Ip_SetLogicChannelTransferList(UartUserCfg->TxDMAChannel, DmaTransferList, LPUART_UART_IP_DMA_CONFIG_LIST_DIMENSION);
  1374. LPUART_UART_IP_DEV_ASSERT(DMA_IP_STATUS_SUCCESS == DmaReturnStatus);
  1375. /* Start the DMA channel */
  1376. DmaReturnStatus = Dma_Ip_SetLogicChannelCommand(UartUserCfg->TxDMAChannel, DMA_IP_CH_SET_HARDWARE_REQUEST);
  1377. LPUART_UART_IP_DEV_ASSERT(DMA_IP_STATUS_SUCCESS == DmaReturnStatus);
  1378. /* Enable the LPUART transmitter */
  1379. Lpuart_Uart_Ip_SetTransmitterCmd(Base, TRUE);
  1380. /* Enable tx DMA requests for the current instance */
  1381. Lpuart_Uart_Ip_SetTxDmaCmd(Base, TRUE);
  1382. return LPUART_UART_IP_STATUS_SUCCESS;
  1383. }
  1384. #endif
  1385. /*FUNCTION**********************************************************************
  1386. *
  1387. * Function Name : Lpuart_Uart_Ip_CompleteSendDataUsingInt
  1388. * Description : Finish up a transmit by completing the process of sending
  1389. * data and disabling the interrupt.
  1390. * This is not a public API as it is called from other driver functions.
  1391. *
  1392. *END**************************************************************************/
  1393. static void Lpuart_Uart_Ip_CompleteSendDataUsingInt(const uint8 Instance)
  1394. {
  1395. Lpuart_Uart_Ip_StateStructureType * UartState;
  1396. LPUART_Type * Base;
  1397. uint32 StartTime;
  1398. uint32 TimeoutTicks;
  1399. uint32 ElapsedTicks = 0;
  1400. Base = Lpuart_Uart_Ip_apBases[Instance];
  1401. UartState = (Lpuart_Uart_Ip_StateStructureType *)Lpuart_Uart_Ip_apStateStructuresArray[Instance];
  1402. if (LPUART_UART_IP_STATUS_BUSY != UartState->TransmitStatus)
  1403. {
  1404. /* If the transfer is aborted or timed out, disable tx empty interrupt */
  1405. Lpuart_Uart_Ip_SetIntMode(Base, LPUART_UART_IP_INT_TX_DATA_REG_EMPTY, FALSE);
  1406. }
  1407. /* Disable transmission complete interrupt */
  1408. Lpuart_Uart_Ip_SetIntMode(Base, LPUART_UART_IP_INT_TX_COMPLETE, FALSE);
  1409. Lpuart_Uart_Ip_StartTimeout(&StartTime, &TimeoutTicks, LPUART_UART_IP_TIMEOUT_VALUE_US, LPUART_UART_IP_TIMEOUT_TYPE);
  1410. /* Wait until the data is completely shifted out of shift register */
  1411. while (!Lpuart_Uart_Ip_GetStatusFlag(Base, LPUART_UART_IP_TX_COMPLETE) && \
  1412. !Lpuart_Uart_Ip_CheckTimeout(&StartTime, &ElapsedTicks, TimeoutTicks, LPUART_UART_IP_TIMEOUT_TYPE))
  1413. {}
  1414. if (Lpuart_Uart_Ip_CheckTimeout(&StartTime, &ElapsedTicks, TimeoutTicks, LPUART_UART_IP_TIMEOUT_TYPE))
  1415. {
  1416. /* In case timeout occur */
  1417. UartState->TransmitStatus = LPUART_UART_IP_STATUS_ERROR;
  1418. }
  1419. /* Disable transmitter */
  1420. Lpuart_Uart_Ip_SetTransmitterCmd(Base, FALSE);
  1421. if (LPUART_UART_IP_STATUS_BUSY == UartState->TransmitStatus)
  1422. {
  1423. /* If the transfer is completed, update the transmit status */
  1424. UartState->TransmitStatus = LPUART_UART_IP_STATUS_SUCCESS;
  1425. }
  1426. /* Update the internal busy flag */
  1427. UartState->IsTxBusy = FALSE;
  1428. }
  1429. #if (LPUART_UART_IP_HAS_DMA_ENABLED == STD_ON)
  1430. /*FUNCTION**********************************************************************
  1431. *
  1432. * Function Name : Lpuart_Uart_Ip_CompleteSendUsingDma
  1433. * Description : Finish up a transmit by completing the process of sending
  1434. * data and disabling the DMA requests. This is a callback for DMA major loop
  1435. * completion, so it must match the DMA callback signature.
  1436. *
  1437. *END**************************************************************************/
  1438. void Lpuart_Uart_Ip_CompleteSendUsingDma(uint8 Instance)
  1439. {
  1440. Lpuart_Uart_Ip_StateStructureType * UartState;
  1441. const Lpuart_Uart_Ip_UserConfigType *UartUserCfg;
  1442. LPUART_Type * Base;
  1443. Dma_Ip_LogicChannelTransferListType DmaTransferList[LPUART_UART_IP_DMA_CONFIG_LIST_DIMENSION];
  1444. Dma_Ip_ReturnType DmaReturnStatus;
  1445. Dma_Ip_LogicChannelStatusType DmaStatus;
  1446. uint32 StartTime;
  1447. uint32 TimeoutTicks;
  1448. uint32 ElapsedTicks = 0;
  1449. Base = Lpuart_Uart_Ip_apBases[Instance];
  1450. UartState = (Lpuart_Uart_Ip_StateStructureType *)Lpuart_Uart_Ip_apStateStructuresArray[Instance];
  1451. UartUserCfg = Lpuart_Uart_Ip_apUserConfig[Instance];
  1452. /* Get Dma Ip Logic Channel Status */
  1453. (void)Dma_Ip_GetLogicChannelStatus(UartUserCfg->TxDMAChannel, &DmaStatus);
  1454. if (DMA_IP_CH_ERROR_STATE == DmaStatus.ChStateValue)
  1455. {
  1456. /* Reset the Dma Channel Error status. */
  1457. DmaReturnStatus = Dma_Ip_SetLogicChannelCommand(UartUserCfg->TxDMAChannel, DMA_IP_CH_CLEAR_ERROR);
  1458. LPUART_UART_IP_DEV_ASSERT(DMA_IP_STATUS_SUCCESS == DmaReturnStatus);
  1459. /* Update transmit status */
  1460. UartState->TransmitStatus = LPUART_UART_IP_STATUS_ERROR;
  1461. /* Invoke callback if there is one */
  1462. if (UartUserCfg->Callback != NULL_PTR)
  1463. {
  1464. UartUserCfg->Callback(Instance, LPUART_UART_IP_EVENT_ERROR, UartUserCfg->CallbackParam);
  1465. }
  1466. }
  1467. /* Invoke the callback when the buffer is finished;
  1468. * Application can provide another buffer inside the callback by calling Lpuart_Uart_Ip_SetTxBuffer */
  1469. if (LPUART_UART_IP_STATUS_BUSY == UartState->TransmitStatus)
  1470. {
  1471. /* Invoke callback if there is one (callback may reset the tx buffer for continuous transmission)*/
  1472. if (UartUserCfg->Callback != NULL_PTR)
  1473. {
  1474. UartUserCfg->Callback(Instance, LPUART_UART_IP_EVENT_TX_EMPTY, UartUserCfg->CallbackParam);
  1475. }
  1476. }
  1477. /* If the callback has updated the tx buffer, update the DMA descriptor to continue the transfer;
  1478. * otherwise, stop the current transfer.
  1479. */
  1480. if ((UartState->TxSize > 0U) && (UartState->TransmitStatus != LPUART_UART_IP_STATUS_ERROR))
  1481. {
  1482. /* Set up parameters for Dma_Ip_LogicChannelTransferListType */
  1483. DmaTransferList[0].Param = DMA_IP_CH_SET_SOURCE_ADDRESS;
  1484. DmaTransferList[0].Value = (uint32)(UartState->TxBuff);
  1485. /* Update tx size and major loop count parameters for Dma_Ip_LogicChannelTransferListType */
  1486. DmaTransferList[1].Param = DMA_IP_CH_SET_MAJORLOOP_COUNT;
  1487. DmaTransferList[1].Value = UartState->TxSize;
  1488. /* Re-configure the transfer control descriptor for the DMA channel */
  1489. DmaReturnStatus = Dma_Ip_SetLogicChannelTransferList(UartUserCfg->TxDMAChannel, DmaTransferList, LPUART_UART_IP_DMA_LEAST_CONFIG_LIST_DIMENSION);
  1490. LPUART_UART_IP_DEV_ASSERT(DMA_IP_STATUS_SUCCESS == DmaReturnStatus);
  1491. /* Now that this tx is set up, clear remaining bytes count */
  1492. UartState->TxSize = 0U;
  1493. /* Re-start the channel */
  1494. DmaReturnStatus = Dma_Ip_SetLogicChannelCommand(UartUserCfg->TxDMAChannel, DMA_IP_CH_SET_HARDWARE_REQUEST);
  1495. LPUART_UART_IP_DEV_ASSERT(DMA_IP_STATUS_SUCCESS == DmaReturnStatus);
  1496. }
  1497. else
  1498. {
  1499. /* Disable tx DMA requests for the current instance */
  1500. Lpuart_Uart_Ip_SetTxDmaCmd(Base, FALSE);
  1501. /* Wait until the last transmission complete */
  1502. Lpuart_Uart_Ip_StartTimeout(&StartTime, &TimeoutTicks, LPUART_UART_IP_TIMEOUT_VALUE_US, LPUART_UART_IP_TIMEOUT_TYPE);
  1503. while (!Lpuart_Uart_Ip_GetStatusFlag(Base, LPUART_UART_IP_TX_COMPLETE) && \
  1504. !Lpuart_Uart_Ip_CheckTimeout(&StartTime, &ElapsedTicks, TimeoutTicks, LPUART_UART_IP_TIMEOUT_TYPE))
  1505. {}
  1506. if (Lpuart_Uart_Ip_CheckTimeout(&StartTime, &ElapsedTicks, TimeoutTicks, LPUART_UART_IP_TIMEOUT_TYPE))
  1507. {
  1508. /* In case timeout occur */
  1509. UartState->TransmitStatus = LPUART_UART_IP_STATUS_ERROR;
  1510. }
  1511. /* Disable the transmitter */
  1512. Lpuart_Uart_Ip_SetTransmitterCmd(Base, FALSE);
  1513. if (LPUART_UART_IP_STATUS_BUSY == UartState->TransmitStatus)
  1514. {
  1515. /* If the transfer is completed, update the transmit status */
  1516. UartState->TransmitStatus = LPUART_UART_IP_STATUS_SUCCESS;
  1517. /* Invoke callback if there is one */
  1518. if (UartUserCfg->Callback != NULL_PTR)
  1519. {
  1520. UartUserCfg->Callback(Instance, LPUART_UART_IP_EVENT_END_TRANSFER, UartUserCfg->CallbackParam);
  1521. }
  1522. }
  1523. /* Update the busy flag */
  1524. UartState->IsTxBusy = FALSE;
  1525. }
  1526. }
  1527. #endif
  1528. /*FUNCTION**********************************************************************
  1529. *
  1530. * Function Name : Lpuart_Uart_Ip_StartReceiveDataUsingInt
  1531. * Description : Initiate (start) a receive by beginning the process of
  1532. * receiving data and enabling the interrupt.
  1533. * This is not a public API as it is called from other driver functions.
  1534. *
  1535. *END**************************************************************************/
  1536. static Lpuart_Uart_Ip_StatusType Lpuart_Uart_Ip_StartReceiveDataUsingInt(const uint8 Instance,
  1537. uint8 * RxBuff,
  1538. const uint32 RxSize)
  1539. {
  1540. LPUART_Type * Base;
  1541. Lpuart_Uart_Ip_StateStructureType * UartState;
  1542. Base = Lpuart_Uart_Ip_apBases[Instance];
  1543. UartState = (Lpuart_Uart_Ip_StateStructureType *)Lpuart_Uart_Ip_apStateStructuresArray[Instance];
  1544. /* Initialize the module driver state struct to indicate transfer in progress
  1545. * and with the buffer and byte count data. */
  1546. UartState->IsRxBusy = TRUE;
  1547. UartState->RxBuff = RxBuff;
  1548. UartState->RxSize = RxSize;
  1549. UartState->ReceiveStatus = LPUART_UART_IP_STATUS_BUSY;
  1550. /* Clear all the error flags */
  1551. Lpuart_Uart_Ip_ClearErrorFlags(Base);
  1552. /* Flush the Tx Buffer */
  1553. Lpuart_Uart_Ip_FlushRxBuffer(Base);
  1554. /* Enable the receiver */
  1555. Lpuart_Uart_Ip_SetReceiverCmd(Base, TRUE);
  1556. /* Enable error interrupts */
  1557. Lpuart_Uart_Ip_SetIntMode(Base, LPUART_UART_IP_INT_RX_OVERRUN, TRUE);
  1558. Lpuart_Uart_Ip_SetIntMode(Base, LPUART_UART_IP_INT_PARITY_ERR_FLAG, TRUE);
  1559. Lpuart_Uart_Ip_SetIntMode(Base, LPUART_UART_IP_INT_NOISE_ERR_FLAG, TRUE);
  1560. Lpuart_Uart_Ip_SetIntMode(Base, LPUART_UART_IP_INT_FRAME_ERR_FLAG, TRUE);
  1561. /* Enable receive data full interrupt */
  1562. Lpuart_Uart_Ip_SetIntMode(Base, LPUART_UART_IP_INT_RX_DATA_REG_FULL, TRUE);
  1563. return LPUART_UART_IP_STATUS_SUCCESS;
  1564. }
  1565. #if (STD_ON == LPUART_UART_IP_HAS_DMA_ENABLED)
  1566. /*FUNCTION**********************************************************************
  1567. *
  1568. * Function Name : Lpuart_Uart_Ip_StartReceiveDataUsingDma
  1569. * Description : Initiate (start) a receive by beginning the process of
  1570. * receiving data using DMA transfers.
  1571. * This is not a public API as it is called from other driver functions.
  1572. *
  1573. *END**************************************************************************/
  1574. static Lpuart_Uart_Ip_StatusType Lpuart_Uart_Ip_StartReceiveDataUsingDma(const uint8 Instance,
  1575. uint8 * RxBuff,
  1576. const uint32 RxSize)
  1577. {
  1578. Lpuart_Uart_Ip_StateStructureType * UartState;
  1579. const Lpuart_Uart_Ip_UserConfigType *UartUserCfg;
  1580. LPUART_Type * Base;
  1581. Dma_Ip_LogicChannelTransferListType DmaTransferList[LPUART_UART_IP_DMA_CONFIG_LIST_DIMENSION];
  1582. Dma_Ip_ReturnType DmaReturnStatus;
  1583. Base = Lpuart_Uart_Ip_apBases[Instance];
  1584. UartState = (Lpuart_Uart_Ip_StateStructureType *)Lpuart_Uart_Ip_apStateStructuresArray[Instance];
  1585. UartUserCfg = Lpuart_Uart_Ip_apUserConfig[Instance];
  1586. /* Clear all the error flags */
  1587. Lpuart_Uart_Ip_ClearErrorFlags(Base);
  1588. /* Flush the Tx Buffer */
  1589. Lpuart_Uart_Ip_FlushRxBuffer(Base);
  1590. /* Set up parameters for Dma_Ip_LogicChannelTransferListType */
  1591. DmaTransferList[0].Param = DMA_IP_CH_SET_SOURCE_ADDRESS;
  1592. DmaTransferList[0].Value = LPUART_UART_IP_LSBW_ADDR(Base->DATA);
  1593. DmaTransferList[1].Param = DMA_IP_CH_SET_DESTINATION_ADDRESS;
  1594. DmaTransferList[1].Value = (uint32)RxBuff;
  1595. DmaTransferList[2].Param = DMA_IP_CH_SET_SOURCE_SIGNED_OFFSET;
  1596. DmaTransferList[2].Value = 0;
  1597. DmaTransferList[3].Param = DMA_IP_CH_SET_DESTINATION_SIGNED_OFFSET;
  1598. DmaTransferList[3].Value = 1;
  1599. DmaTransferList[4].Param = DMA_IP_CH_SET_MAJORLOOP_COUNT;
  1600. DmaTransferList[4].Value = RxSize;
  1601. DmaTransferList[5].Param = DMA_IP_CH_SET_MINORLOOP_SIZE;
  1602. DmaTransferList[5].Value = 1;
  1603. DmaTransferList[6].Param = DMA_IP_CH_SET_DESTINATION_TRANSFER_SIZE;
  1604. DmaTransferList[6].Value = DMA_IP_TRANSFER_SIZE_1_BYTE;
  1605. DmaTransferList[7].Param = DMA_IP_CH_SET_SOURCE_TRANSFER_SIZE;
  1606. DmaTransferList[7].Value = DMA_IP_TRANSFER_SIZE_1_BYTE;
  1607. DmaTransferList[8].Param = DMA_IP_CH_SET_CONTROL_EN_MAJOR_INTERRUPT;
  1608. DmaTransferList[8].Value = 1;
  1609. DmaTransferList[9].Param = DMA_IP_CH_SET_CONTROL_DIS_AUTO_REQUEST;
  1610. DmaTransferList[9].Value = 1;
  1611. /* Update the state structure */
  1612. UartState->RxBuff = RxBuff;
  1613. UartState->RxSize = 0U;
  1614. UartState->IsRxBusy = TRUE;
  1615. UartState->ReceiveStatus = LPUART_UART_IP_STATUS_BUSY;
  1616. /* Configure the transfer control descriptor for the DMA channel */
  1617. DmaReturnStatus = Dma_Ip_SetLogicChannelTransferList(UartUserCfg->RxDMAChannel, DmaTransferList, LPUART_UART_IP_DMA_CONFIG_LIST_DIMENSION);
  1618. LPUART_UART_IP_DEV_ASSERT(DMA_IP_STATUS_SUCCESS == DmaReturnStatus);
  1619. /* Start the DMA channel */
  1620. DmaReturnStatus = Dma_Ip_SetLogicChannelCommand(UartUserCfg->RxDMAChannel, DMA_IP_CH_SET_HARDWARE_REQUEST);
  1621. LPUART_UART_IP_DEV_ASSERT(DMA_IP_STATUS_SUCCESS == DmaReturnStatus);
  1622. /* Enable the receiver */
  1623. Lpuart_Uart_Ip_SetReceiverCmd(Base, TRUE);
  1624. /* Enable error interrupts and enable rx DMA requests for the current instance */
  1625. Lpuart_Uart_Ip_SetupIntDmaMode(Instance, TRUE);
  1626. return LPUART_UART_IP_STATUS_SUCCESS;
  1627. }
  1628. #endif
  1629. /*FUNCTION**********************************************************************
  1630. *
  1631. * Function Name : Lpuart_Uart_Ip_CompleteReceiveDataUsingInt
  1632. * Description : Finish up a receive by completing the process of receiving data
  1633. * and disabling the interrupt.
  1634. * This is not a public API as it is called from other driver functions.
  1635. *
  1636. *END**************************************************************************/
  1637. static void Lpuart_Uart_Ip_CompleteReceiveDataUsingInt(const uint8 Instance)
  1638. {
  1639. LPUART_Type * Base;
  1640. Lpuart_Uart_Ip_StateStructureType * UartState;
  1641. uint32 StartTime;
  1642. uint32 TimeoutTicks;
  1643. uint32 ElapsedTicks = 0;
  1644. Base = Lpuart_Uart_Ip_apBases[Instance];
  1645. UartState = (Lpuart_Uart_Ip_StateStructureType *)Lpuart_Uart_Ip_apStateStructuresArray[Instance];
  1646. /* Disable all error interrupts */
  1647. Lpuart_Uart_Ip_SetIntMode(Base, LPUART_UART_IP_INT_RX_OVERRUN, FALSE);
  1648. Lpuart_Uart_Ip_SetIntMode(Base, LPUART_UART_IP_INT_PARITY_ERR_FLAG, FALSE);
  1649. Lpuart_Uart_Ip_SetIntMode(Base, LPUART_UART_IP_INT_NOISE_ERR_FLAG, FALSE);
  1650. Lpuart_Uart_Ip_SetIntMode(Base, LPUART_UART_IP_INT_FRAME_ERR_FLAG, FALSE);
  1651. /* Disable receive data full and rx overrun interrupt. */
  1652. Lpuart_Uart_Ip_SetIntMode(Base, LPUART_UART_IP_INT_RX_DATA_REG_FULL, FALSE);
  1653. /* In Abort case, the transmission need to stop instantly */
  1654. if (UartState->ReceiveStatus == LPUART_UART_IP_STATUS_ABORTED)
  1655. {
  1656. Lpuart_Uart_Ip_StartTimeout(&StartTime, &TimeoutTicks, LPUART_UART_IP_TIMEOUT_VALUE_US/10, LPUART_UART_IP_TIMEOUT_TYPE);
  1657. /* Wait until the data is completely received */
  1658. while (!Lpuart_Uart_Ip_GetStatusFlag(Base, LPUART_UART_IP_DATA_REG_FULL) && \
  1659. !Lpuart_Uart_Ip_CheckTimeout(&StartTime, &ElapsedTicks, TimeoutTicks, LPUART_UART_IP_TIMEOUT_TYPE))
  1660. {}
  1661. if (Lpuart_Uart_Ip_CheckTimeout(&StartTime, &ElapsedTicks, TimeoutTicks, LPUART_UART_IP_TIMEOUT_TYPE))
  1662. {
  1663. /* In case timeout occur */
  1664. UartState->ReceiveStatus = LPUART_UART_IP_STATUS_ERROR;
  1665. }
  1666. }
  1667. /* Disable receiver */
  1668. Lpuart_Uart_Ip_SetReceiverCmd(Base, FALSE);
  1669. /* Read dummy to clear RDRF flag */
  1670. (void)Lpuart_Uart_Ip_Getchar(Base);
  1671. /* Update the information of the module driver state */
  1672. UartState->IsRxBusy = FALSE;
  1673. if (LPUART_UART_IP_STATUS_BUSY == UartState->ReceiveStatus)
  1674. {
  1675. UartState->ReceiveStatus = LPUART_UART_IP_STATUS_SUCCESS;
  1676. }
  1677. }
  1678. #if (STD_ON == LPUART_UART_IP_HAS_DMA_ENABLED)
  1679. /*FUNCTION**********************************************************************
  1680. *
  1681. * Function Name : Lpuart_Uart_Ip_CompleteReceiveUsingDma
  1682. * Description : Finish up a receive by completing the process of receiving data
  1683. * and disabling the DMA requests. This is a callback for DMA major loop
  1684. * completion, so it must match the DMA callback signature.
  1685. *
  1686. *END**************************************************************************/
  1687. void Lpuart_Uart_Ip_CompleteReceiveUsingDma(uint8 Instance)
  1688. {
  1689. Lpuart_Uart_Ip_StateStructureType * UartState;
  1690. const Lpuart_Uart_Ip_UserConfigType *UartUserCfg;
  1691. LPUART_Type * Base;
  1692. Dma_Ip_LogicChannelTransferListType DmaTransferList[LPUART_UART_IP_DMA_CONFIG_LIST_DIMENSION];
  1693. Dma_Ip_ReturnType DmaReturnStatus;
  1694. Dma_Ip_LogicChannelStatusType DmaStatus;
  1695. uint32 StartTime;
  1696. uint32 TimeoutTicks;
  1697. uint32 ElapsedTicks = 0;
  1698. Base = Lpuart_Uart_Ip_apBases[Instance];
  1699. UartState = (Lpuart_Uart_Ip_StateStructureType *)Lpuart_Uart_Ip_apStateStructuresArray[Instance];
  1700. UartUserCfg = Lpuart_Uart_Ip_apUserConfig[Instance];
  1701. /* Get Dma Ip Logic Channel Status */
  1702. (void)Dma_Ip_GetLogicChannelStatus(UartUserCfg->RxDMAChannel, &DmaStatus);
  1703. if (DMA_IP_CH_ERROR_STATE == DmaStatus.ChStateValue)
  1704. {
  1705. /* Reset the Dma Channel Error status. */
  1706. DmaReturnStatus = Dma_Ip_SetLogicChannelCommand(UartUserCfg->RxDMAChannel, DMA_IP_CH_CLEAR_ERROR);
  1707. LPUART_UART_IP_DEV_ASSERT(DMA_IP_STATUS_SUCCESS == DmaReturnStatus);
  1708. /* Update transmit status */
  1709. UartState->ReceiveStatus = LPUART_UART_IP_STATUS_ERROR;
  1710. /* Invoke callback if there is one */
  1711. if (UartUserCfg->Callback != NULL_PTR)
  1712. {
  1713. UartUserCfg->Callback(Instance, LPUART_UART_IP_EVENT_ERROR, UartUserCfg->CallbackParam);
  1714. }
  1715. }
  1716. /* Invoke the callback when the buffer is finished */
  1717. if (LPUART_UART_IP_STATUS_BUSY == UartState->ReceiveStatus)
  1718. {
  1719. /* Application can provide another buffer inside the callback by calling Lpuart_Uart_Ip_SetRxBuffer */
  1720. if (UartUserCfg->Callback != NULL_PTR)
  1721. {
  1722. UartUserCfg->Callback(Instance, LPUART_UART_IP_EVENT_RX_FULL, UartUserCfg->CallbackParam);
  1723. }
  1724. }
  1725. /* If the callback has updated the rx buffer, update the DMA descriptor to continue the transfer;
  1726. * otherwise, stop the current transfer.
  1727. */
  1728. if ((UartState->RxSize > 0U) && (UartState->ReceiveStatus != LPUART_UART_IP_STATUS_ERROR))
  1729. {
  1730. /* Set up parameters for Dma_Ip_LogicChannelTransferListType */
  1731. DmaTransferList[0].Param = DMA_IP_CH_SET_DESTINATION_ADDRESS;
  1732. DmaTransferList[0].Value = (uint32)(UartState->RxBuff);
  1733. DmaTransferList[1].Param = DMA_IP_CH_SET_MAJORLOOP_COUNT;
  1734. DmaTransferList[1].Value = UartState->RxSize;
  1735. /* Re-configure the transfer control descriptor for the DMA channel */
  1736. DmaReturnStatus = Dma_Ip_SetLogicChannelTransferList(UartUserCfg->RxDMAChannel, DmaTransferList, LPUART_UART_IP_DMA_LEAST_CONFIG_LIST_DIMENSION);
  1737. LPUART_UART_IP_DEV_ASSERT(DMA_IP_STATUS_SUCCESS == DmaReturnStatus);
  1738. /* Now that this rx is set up, clear remaining bytes count */
  1739. UartState->RxSize = 0U;
  1740. /* Re-start the channel */
  1741. DmaReturnStatus = Dma_Ip_SetLogicChannelCommand(UartUserCfg->RxDMAChannel, DMA_IP_CH_SET_HARDWARE_REQUEST);
  1742. LPUART_UART_IP_DEV_ASSERT(DMA_IP_STATUS_SUCCESS == DmaReturnStatus);
  1743. }
  1744. else
  1745. {
  1746. /* Disable error interrupts and disable rx DMA requests for the current instance*/
  1747. Lpuart_Uart_Ip_SetupIntDmaMode(Instance, FALSE);
  1748. /* In Abort case, the transmission need to stop instantly */
  1749. if (UartState->ReceiveStatus == LPUART_UART_IP_STATUS_ABORTED)
  1750. {
  1751. /* Wait until the last transmission complete */
  1752. Lpuart_Uart_Ip_StartTimeout(&StartTime, &TimeoutTicks, LPUART_UART_IP_TIMEOUT_VALUE_US/10, LPUART_UART_IP_TIMEOUT_TYPE);
  1753. while (!Lpuart_Uart_Ip_GetStatusFlag(Base, LPUART_UART_IP_DATA_REG_FULL) && \
  1754. !Lpuart_Uart_Ip_CheckTimeout(&StartTime, &ElapsedTicks, TimeoutTicks, LPUART_UART_IP_TIMEOUT_TYPE))
  1755. {}
  1756. if (Lpuart_Uart_Ip_CheckTimeout(&StartTime, &ElapsedTicks, TimeoutTicks, LPUART_UART_IP_TIMEOUT_TYPE))
  1757. {
  1758. /* In case timeout occur */
  1759. UartState->ReceiveStatus = LPUART_UART_IP_STATUS_ERROR;
  1760. }
  1761. }
  1762. /* Disable receiver */
  1763. Lpuart_Uart_Ip_SetReceiverCmd(Base, FALSE);
  1764. /* Read dummy to clear RDRF flag */
  1765. (void)Lpuart_Uart_Ip_Getchar(Base);
  1766. /* Update the information of the module driver state */
  1767. UartState->IsRxBusy = FALSE;
  1768. /* If the current reception hasn't been aborted, update the status and call the callback */
  1769. if (LPUART_UART_IP_STATUS_BUSY == UartState->ReceiveStatus)
  1770. {
  1771. UartState->ReceiveStatus = LPUART_UART_IP_STATUS_SUCCESS;
  1772. /* Call the callback to notify application that the transfer is complete */
  1773. if (UartUserCfg->Callback != NULL_PTR)
  1774. {
  1775. UartUserCfg->Callback(Instance, LPUART_UART_IP_EVENT_END_TRANSFER, UartUserCfg->CallbackParam);
  1776. }
  1777. }
  1778. }
  1779. }
  1780. /*FUNCTION**********************************************************************
  1781. *
  1782. * Function Name : Lpuart_Uart_Ip_SetupIntDmaMode
  1783. * Description : Setup error interrupts and Setup rx DMA requests for
  1784. * the current instance
  1785. * This is not a public API as it is called from other driver functions.
  1786. *
  1787. *END**************************************************************************/
  1788. static void Lpuart_Uart_Ip_SetupIntDmaMode(const uint8 Instance, boolean Enable)
  1789. {
  1790. LPUART_Type * Base;
  1791. Base = Lpuart_Uart_Ip_apBases[Instance];
  1792. /* Setup error interrupts */
  1793. Lpuart_Uart_Ip_SetIntMode(Base, LPUART_UART_IP_INT_RX_OVERRUN, 0);
  1794. Lpuart_Uart_Ip_SetIntMode(Base, LPUART_UART_IP_INT_PARITY_ERR_FLAG, Enable);
  1795. Lpuart_Uart_Ip_SetIntMode(Base, LPUART_UART_IP_INT_NOISE_ERR_FLAG, Enable);
  1796. Lpuart_Uart_Ip_SetIntMode(Base, LPUART_UART_IP_INT_FRAME_ERR_FLAG, Enable);
  1797. /* Setup rx DMA requests for the current instance */
  1798. Lpuart_Uart_Ip_SetRxDmaCmd(Base, Enable);
  1799. }
  1800. #endif
  1801. /*FUNCTION**********************************************************************
  1802. *
  1803. * Function Name : Lpuart_Uart_Ip_PutData
  1804. * Description : Write data to the buffer register, according to configured
  1805. * word length.
  1806. * This is not a public API as it is called from other driver functions.
  1807. *
  1808. *END**************************************************************************/
  1809. static void Lpuart_Uart_Ip_PutData(const uint8 Instance)
  1810. {
  1811. Lpuart_Uart_Ip_StateStructureType * UartState;
  1812. const Lpuart_Uart_Ip_UserConfigType *UartUserCfg;
  1813. LPUART_Type * Base;
  1814. uint16 Data16b;
  1815. uint8 Data8b;
  1816. Base = Lpuart_Uart_Ip_apBases[Instance];
  1817. UartState = (Lpuart_Uart_Ip_StateStructureType *)Lpuart_Uart_Ip_apStateStructuresArray[Instance];
  1818. UartUserCfg = Lpuart_Uart_Ip_apUserConfig[Instance];
  1819. if ((LPUART_UART_IP_7_BITS_PER_CHAR == UartUserCfg->BitCountPerChar) || (LPUART_UART_IP_8_BITS_PER_CHAR == UartUserCfg->BitCountPerChar))
  1820. {
  1821. Data8b = *(UartState->TxBuff);
  1822. /* Update the state structure */
  1823. ++UartState->TxBuff;
  1824. --UartState->TxSize;
  1825. /* Transmit the data */
  1826. Lpuart_Uart_Ip_Putchar(Base, Data8b);
  1827. }
  1828. else
  1829. {
  1830. Data16b = (uint16)(*UartState->TxBuff);
  1831. /* Update the state structure */
  1832. if (1U == UartState->TxSize)
  1833. {
  1834. ++UartState->TxBuff;
  1835. --UartState->TxSize;
  1836. }
  1837. else
  1838. {
  1839. ++UartState->TxBuff;
  1840. Data16b |= (uint16)(((uint16)(*UartState->TxBuff)) << 8U);
  1841. ++UartState->TxBuff;
  1842. UartState->TxSize -= 2U;
  1843. }
  1844. /* Transmit the data */
  1845. if (LPUART_UART_IP_9_BITS_PER_CHAR == UartUserCfg->BitCountPerChar)
  1846. {
  1847. Lpuart_Uart_Ip_Putchar9(Base, Data16b);
  1848. }
  1849. else
  1850. {
  1851. Lpuart_Uart_Ip_Putchar10(Base, Data16b);
  1852. }
  1853. }
  1854. }
  1855. /*FUNCTION**********************************************************************
  1856. *
  1857. * Function Name : Lpuart_Uart_Ip_GetData
  1858. * Description : Read data from the buffer register, according to configured
  1859. * word length.
  1860. * This is not a public API as it is called from other driver functions.
  1861. *
  1862. *END**************************************************************************/
  1863. static void Lpuart_Uart_Ip_GetData(const uint8 Instance)
  1864. {
  1865. Lpuart_Uart_Ip_StateStructureType * UartState;
  1866. const Lpuart_Uart_Ip_UserConfigType *UartUserCfg;
  1867. const LPUART_Type * Base;
  1868. uint16 Data16b;
  1869. Base = (const LPUART_Type *)Lpuart_Uart_Ip_apBases[Instance];
  1870. UartState = (Lpuart_Uart_Ip_StateStructureType *)Lpuart_Uart_Ip_apStateStructuresArray[Instance];
  1871. UartUserCfg = Lpuart_Uart_Ip_apUserConfig[Instance];
  1872. if ((LPUART_UART_IP_7_BITS_PER_CHAR == UartUserCfg->BitCountPerChar) || (LPUART_UART_IP_8_BITS_PER_CHAR == UartUserCfg->BitCountPerChar))
  1873. {
  1874. /* Receive the data */
  1875. *(UartState->RxBuff) = Lpuart_Uart_Ip_Getchar(Base);
  1876. if (LPUART_UART_IP_7_BITS_PER_CHAR == UartUserCfg->BitCountPerChar)
  1877. {
  1878. *(UartState->RxBuff) &= 0x7FU;
  1879. }
  1880. /* Update the state structure */
  1881. ++UartState->RxBuff;
  1882. --UartState->RxSize;
  1883. }
  1884. else
  1885. {
  1886. if (LPUART_UART_IP_9_BITS_PER_CHAR == UartUserCfg->BitCountPerChar)
  1887. {
  1888. Data16b = Lpuart_Uart_Ip_Getchar9(Base);
  1889. }
  1890. else
  1891. {
  1892. Data16b = Lpuart_Uart_Ip_Getchar10(Base);
  1893. }
  1894. /* Get the data and update state structure */
  1895. if (1U == UartState->RxSize)
  1896. {
  1897. *(UartState->RxBuff) = (uint8)Data16b;
  1898. ++UartState->RxBuff;
  1899. --UartState->RxSize;
  1900. }
  1901. else
  1902. {
  1903. /* Write the least significant bits to the receive buffer */
  1904. *(UartState->RxBuff) = (uint8)(Data16b & 0xFFU);
  1905. ++UartState->RxBuff;
  1906. /* Write the ninth bit to the subsequent byte in the rx buffer */
  1907. *UartState->RxBuff = (uint8)(Data16b >> 8U);
  1908. ++UartState->RxBuff;
  1909. UartState->RxSize -= 2U;
  1910. }
  1911. }
  1912. }
  1913. #define UART_STOP_SEC_CODE
  1914. #include "Uart_MemMap.h"
  1915. #ifdef __cplusplus
  1916. }
  1917. /** @} */
  1918. #endif