stm32l4xx_hal_smartcard.c 105 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_smartcard.c
  4. * @author MCD Application Team
  5. * @brief SMARTCARD HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the SMARTCARD peripheral:
  8. * + Initialization and de-initialization functions
  9. * + IO operation functions
  10. * + Peripheral Control functions
  11. * + Peripheral State and Error functions
  12. *
  13. @verbatim
  14. ==============================================================================
  15. ##### How to use this driver #####
  16. ==============================================================================
  17. [..]
  18. The SMARTCARD HAL driver can be used as follows:
  19. (#) Declare a SMARTCARD_HandleTypeDef handle structure (eg. SMARTCARD_HandleTypeDef hsmartcard).
  20. (#) Associate a USART to the SMARTCARD handle hsmartcard.
  21. (#) Initialize the SMARTCARD low level resources by implementing the HAL_SMARTCARD_MspInit() API:
  22. (++) Enable the USARTx interface clock.
  23. (++) USART pins configuration:
  24. (+++) Enable the clock for the USART GPIOs.
  25. (+++) Configure the USART pins (TX as alternate function pull-up, RX as alternate function Input).
  26. (++) NVIC configuration if you need to use interrupt process (HAL_SMARTCARD_Transmit_IT()
  27. and HAL_SMARTCARD_Receive_IT() APIs):
  28. (+++) Configure the USARTx interrupt priority.
  29. (+++) Enable the NVIC USART IRQ handle.
  30. (++) DMA Configuration if you need to use DMA process (HAL_SMARTCARD_Transmit_DMA()
  31. and HAL_SMARTCARD_Receive_DMA() APIs):
  32. (+++) Declare a DMA handle structure for the Tx/Rx channel.
  33. (+++) Enable the DMAx interface clock.
  34. (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
  35. (+++) Configure the DMA Tx/Rx channel.
  36. (+++) Associate the initialized DMA handle to the SMARTCARD DMA Tx/Rx handle.
  37. (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx channel.
  38. (#) Program the Baud Rate, Parity, Mode(Receiver/Transmitter), clock enabling/disabling and accordingly,
  39. the clock parameters (parity, phase, last bit), prescaler value, guard time and NACK on transmission
  40. error enabling or disabling in the hsmartcard handle Init structure.
  41. (#) If required, program SMARTCARD advanced features (TX/RX pins swap, TimeOut, auto-retry counter,...)
  42. in the hsmartcard handle AdvancedInit structure.
  43. (#) Initialize the SMARTCARD registers by calling the HAL_SMARTCARD_Init() API:
  44. (++) This API configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
  45. by calling the customized HAL_SMARTCARD_MspInit() API.
  46. [..]
  47. (@) The specific SMARTCARD interrupts (Transmission complete interrupt,
  48. RXNE interrupt and Error Interrupts) will be managed using the macros
  49. __HAL_SMARTCARD_ENABLE_IT() and __HAL_SMARTCARD_DISABLE_IT() inside the transmit and receive process.
  50. [..]
  51. [..] Three operation modes are available within this driver :
  52. *** Polling mode IO operation ***
  53. =================================
  54. [..]
  55. (+) Send an amount of data in blocking mode using HAL_SMARTCARD_Transmit()
  56. (+) Receive an amount of data in blocking mode using HAL_SMARTCARD_Receive()
  57. *** Interrupt mode IO operation ***
  58. ===================================
  59. [..]
  60. (+) Send an amount of data in non-blocking mode using HAL_SMARTCARD_Transmit_IT()
  61. (+) At transmission end of transfer HAL_SMARTCARD_TxCpltCallback() is executed and user can
  62. add his own code by customization of function pointer HAL_SMARTCARD_TxCpltCallback()
  63. (+) Receive an amount of data in non-blocking mode using HAL_SMARTCARD_Receive_IT()
  64. (+) At reception end of transfer HAL_SMARTCARD_RxCpltCallback() is executed and user can
  65. add his own code by customization of function pointer HAL_SMARTCARD_RxCpltCallback()
  66. (+) In case of transfer Error, HAL_SMARTCARD_ErrorCallback() function is executed and user can
  67. add his own code by customization of function pointer HAL_SMARTCARD_ErrorCallback()
  68. *** DMA mode IO operation ***
  69. ==============================
  70. [..]
  71. (+) Send an amount of data in non-blocking mode (DMA) using HAL_SMARTCARD_Transmit_DMA()
  72. (+) At transmission end of transfer HAL_SMARTCARD_TxCpltCallback() is executed and user can
  73. add his own code by customization of function pointer HAL_SMARTCARD_TxCpltCallback()
  74. (+) Receive an amount of data in non-blocking mode (DMA) using HAL_SMARTCARD_Receive_DMA()
  75. (+) At reception end of transfer HAL_SMARTCARD_RxCpltCallback() is executed and user can
  76. add his own code by customization of function pointer HAL_SMARTCARD_RxCpltCallback()
  77. (+) In case of transfer Error, HAL_SMARTCARD_ErrorCallback() function is executed and user can
  78. add his own code by customization of function pointer HAL_SMARTCARD_ErrorCallback()
  79. *** SMARTCARD HAL driver macros list ***
  80. ========================================
  81. [..]
  82. Below the list of most used macros in SMARTCARD HAL driver.
  83. (+) __HAL_SMARTCARD_GET_FLAG : Check whether or not the specified SMARTCARD flag is set
  84. (+) __HAL_SMARTCARD_CLEAR_FLAG : Clear the specified SMARTCARD pending flag
  85. (+) __HAL_SMARTCARD_ENABLE_IT: Enable the specified SMARTCARD interrupt
  86. (+) __HAL_SMARTCARD_DISABLE_IT: Disable the specified SMARTCARD interrupt
  87. (+) __HAL_SMARTCARD_GET_IT_SOURCE: Check whether or not the specified SMARTCARD interrupt is enabled
  88. [..]
  89. (@) You can refer to the SMARTCARD HAL driver header file for more useful macros
  90. @endverbatim
  91. ******************************************************************************
  92. * @attention
  93. *
  94. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  95. *
  96. * Redistribution and use in source and binary forms, with or without modification,
  97. * are permitted provided that the following conditions are met:
  98. * 1. Redistributions of source code must retain the above copyright notice,
  99. * this list of conditions and the following disclaimer.
  100. * 2. Redistributions in binary form must reproduce the above copyright notice,
  101. * this list of conditions and the following disclaimer in the documentation
  102. * and/or other materials provided with the distribution.
  103. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  104. * may be used to endorse or promote products derived from this software
  105. * without specific prior written permission.
  106. *
  107. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  108. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  109. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  110. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  111. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  112. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  113. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  114. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  115. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  116. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  117. *
  118. ******************************************************************************
  119. */
  120. /* Includes ------------------------------------------------------------------*/
  121. #include "stm32l4xx_hal.h"
  122. /** @addtogroup STM32L4xx_HAL_Driver
  123. * @{
  124. */
  125. /** @defgroup SMARTCARD SMARTCARD
  126. * @brief HAL SMARTCARD module driver
  127. * @{
  128. */
  129. #ifdef HAL_SMARTCARD_MODULE_ENABLED
  130. /* Private typedef -----------------------------------------------------------*/
  131. /* Private define ------------------------------------------------------------*/
  132. /** @defgroup SMARTCARD_Private_Constants SMARTCARD Private Constants
  133. * @{
  134. */
  135. #define SMARTCARD_TEACK_REACK_TIMEOUT 1000 /*!< SMARTCARD TX or RX enable acknowledge time-out value */
  136. #if defined(USART_CR1_FIFOEN)
  137. #define USART_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | \
  138. USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8| \
  139. USART_CR1_FIFOEN )) /*!< USART CR1 fields of parameters set by SMARTCARD_SetConfig API */
  140. #else
  141. #define USART_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | \
  142. USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8)) /*!< USART CR1 fields of parameters set by SMARTCARD_SetConfig API */
  143. #endif
  144. #define USART_CR2_CLK_FIELDS ((uint32_t)(USART_CR2_CLKEN | USART_CR2_CPOL | USART_CR2_CPHA | \
  145. USART_CR2_LBCL)) /*!< SMARTCARD clock-related USART CR2 fields of parameters */
  146. #define USART_CR2_FIELDS ((uint32_t)(USART_CR2_RTOEN | USART_CR2_CLK_FIELDS | USART_CR2_STOP)) /*!< USART CR2 fields of parameters set by SMARTCARD_SetConfig API */
  147. #if defined(USART_CR1_FIFOEN)
  148. #define USART_CR3_FIELDS ((uint32_t)(USART_CR3_ONEBIT | USART_CR3_NACK | USART_CR3_SCARCNT | \
  149. USART_CR3_TXFTCFG | USART_CR3_RXFTCFG )) /*!< USART CR3 fields of parameters set by SMARTCARD_SetConfig API */
  150. #else
  151. #define USART_CR3_FIELDS ((uint32_t)(USART_CR3_ONEBIT | USART_CR3_NACK | USART_CR3_SCARCNT)) /*!< USART CR3 fields of parameters set by SMARTCARD_SetConfig API */
  152. #endif
  153. #define USART_BRR_MIN 0x10U /*!< USART BRR minimum authorized value */
  154. #define USART_BRR_MAX 0x0000FFFFU /*!< USART BRR maximum authorized value */
  155. /**
  156. * @}
  157. */
  158. /* Private macros ------------------------------------------------------------*/
  159. /* Private variables ---------------------------------------------------------*/
  160. #if defined(USART_PRESC_PRESCALER)
  161. /** @defgroup SMARTCARD_Private_Variables SMARTCARD Private Variables
  162. * @{
  163. */
  164. static const uint16_t SMARTCARDPrescTable[12] = {1, 2, 4, 6, 8, 10, 12, 16, 32, 64, 128, 256};
  165. /**
  166. * @}
  167. */
  168. #endif
  169. /* Private function prototypes -----------------------------------------------*/
  170. /** @addtogroup SMARTCARD_Private_Functions
  171. * @{
  172. */
  173. static HAL_StatusTypeDef SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef *hsmartcard);
  174. static void SMARTCARD_AdvFeatureConfig(SMARTCARD_HandleTypeDef *hsmartcard);
  175. static HAL_StatusTypeDef SMARTCARD_CheckIdleState(SMARTCARD_HandleTypeDef *hsmartcard);
  176. static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsmartcard, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout);
  177. static void SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef *hsmartcard);
  178. static void SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef *hsmartcard);
  179. static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma);
  180. static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
  181. static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma);
  182. static void SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef *hdma);
  183. static void SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef *hdma);
  184. static void SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef *hdma);
  185. static void SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
  186. static void SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma);
  187. static void SMARTCARD_TxISR(SMARTCARD_HandleTypeDef *hsmartcard);
  188. #if defined(USART_CR1_FIFOEN)
  189. static void SMARTCARD_TxISR_FIFOEN(SMARTCARD_HandleTypeDef *hsmartcard);
  190. #endif
  191. static void SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard);
  192. static void SMARTCARD_RxISR(SMARTCARD_HandleTypeDef *hsmartcard);
  193. #if defined(USART_CR1_FIFOEN)
  194. static void SMARTCARD_RxISR_FIFOEN(SMARTCARD_HandleTypeDef *hsmartcard);
  195. #endif
  196. /**
  197. * @}
  198. */
  199. /* Exported functions --------------------------------------------------------*/
  200. /** @defgroup SMARTCARD_Exported_Functions SMARTCARD Exported Functions
  201. * @{
  202. */
  203. /** @defgroup SMARTCARD_Exported_Functions_Group1 Initialization and de-initialization functions
  204. * @brief Initialization and Configuration functions
  205. *
  206. @verbatim
  207. ==============================================================================
  208. ##### Initialization and Configuration functions #####
  209. ==============================================================================
  210. [..]
  211. This subsection provides a set of functions allowing to initialize the USARTx
  212. associated to the SmartCard.
  213. (+) These parameters can be configured:
  214. (++) Baud Rate
  215. (++) Parity: parity should be enabled, frame Length is fixed to 8 bits plus parity
  216. (++) Receiver/transmitter modes
  217. (++) Synchronous mode (and if enabled, phase, polarity and last bit parameters)
  218. (++) Prescaler value
  219. (++) Guard bit time
  220. (++) NACK enabling or disabling on transmission error
  221. (+) The following advanced features can be configured as well:
  222. (++) TX and/or RX pin level inversion
  223. (++) data logical level inversion
  224. (++) RX and TX pins swap
  225. (++) RX overrun detection disabling
  226. (++) DMA disabling on RX error
  227. (++) MSB first on communication line
  228. (++) Time out enabling (and if activated, timeout value)
  229. (++) Block length
  230. (++) Auto-retry counter
  231. [..]
  232. The HAL_SMARTCARD_Init() API follows the USART synchronous configuration procedures
  233. (details for the procedures are available in reference manual).
  234. @endverbatim
  235. The USART frame format is given in the following table:
  236. Table 1. USART frame format.
  237. +---------------------------------------------------------------+
  238. | M1M0 bits | PCE bit | USART frame |
  239. |-----------------------|---------------------------------------|
  240. | 01 | 1 | | SB | 8 bit data | PB | STB | |
  241. +---------------------------------------------------------------+
  242. * @{
  243. */
  244. /**
  245. * @brief Initialize the SMARTCARD mode according to the specified
  246. * parameters in the SMARTCARD_HandleTypeDef and initialize the associated handle.
  247. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  248. * the configuration information for the specified SMARTCARD module.
  249. * @retval HAL status
  250. */
  251. HAL_StatusTypeDef HAL_SMARTCARD_Init(SMARTCARD_HandleTypeDef *hsmartcard)
  252. {
  253. /* Check the SMARTCARD handle allocation */
  254. if(hsmartcard == NULL)
  255. {
  256. return HAL_ERROR;
  257. }
  258. /* Check the USART associated to the SMARTCARD handle */
  259. assert_param(IS_SMARTCARD_INSTANCE(hsmartcard->Instance));
  260. if(hsmartcard->gState == HAL_SMARTCARD_STATE_RESET)
  261. {
  262. /* Allocate lock resource and initialize it */
  263. hsmartcard->Lock = HAL_UNLOCKED;
  264. /* Init the low level hardware : GPIO, CLOCK */
  265. HAL_SMARTCARD_MspInit(hsmartcard);
  266. }
  267. hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY;
  268. /* Disable the Peripheral to set smartcard mode */
  269. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  270. /* In SmartCard mode, the following bits must be kept cleared:
  271. - LINEN in the USART_CR2 register,
  272. - HDSEL and IREN bits in the USART_CR3 register.*/
  273. CLEAR_BIT(hsmartcard->Instance->CR2, USART_CR2_LINEN);
  274. CLEAR_BIT(hsmartcard->Instance->CR3, (USART_CR3_HDSEL | USART_CR3_IREN));
  275. /* set the USART in SMARTCARD mode */
  276. SET_BIT(hsmartcard->Instance->CR3, USART_CR3_SCEN);
  277. /* Set the SMARTCARD Communication parameters */
  278. if (SMARTCARD_SetConfig(hsmartcard) == HAL_ERROR)
  279. {
  280. return HAL_ERROR;
  281. }
  282. /* Set the SMARTCARD transmission completion indication */
  283. SMARTCARD_TRANSMISSION_COMPLETION_SETTING(hsmartcard);
  284. if (hsmartcard->AdvancedInit.AdvFeatureInit != SMARTCARD_ADVFEATURE_NO_INIT)
  285. {
  286. SMARTCARD_AdvFeatureConfig(hsmartcard);
  287. }
  288. /* Enable the Peripheral */
  289. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  290. /* TEACK and/or REACK to check before moving hsmartcard->gState and hsmartcard->RxState to Ready */
  291. return (SMARTCARD_CheckIdleState(hsmartcard));
  292. }
  293. /**
  294. * @brief DeInitialize the SMARTCARD peripheral.
  295. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  296. * the configuration information for the specified SMARTCARD module.
  297. * @retval HAL status
  298. */
  299. HAL_StatusTypeDef HAL_SMARTCARD_DeInit(SMARTCARD_HandleTypeDef *hsmartcard)
  300. {
  301. /* Check the SMARTCARD handle allocation */
  302. if(hsmartcard == NULL)
  303. {
  304. return HAL_ERROR;
  305. }
  306. /* Check the USART/UART associated to the SMARTCARD handle */
  307. assert_param(IS_SMARTCARD_INSTANCE(hsmartcard->Instance));
  308. hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY;
  309. /* Disable the Peripheral */
  310. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  311. WRITE_REG(hsmartcard->Instance->CR1, 0x0);
  312. WRITE_REG(hsmartcard->Instance->CR2, 0x0);
  313. WRITE_REG(hsmartcard->Instance->CR3, 0x0);
  314. WRITE_REG(hsmartcard->Instance->RTOR, 0x0);
  315. WRITE_REG(hsmartcard->Instance->GTPR, 0x0);
  316. /* DeInit the low level hardware */
  317. HAL_SMARTCARD_MspDeInit(hsmartcard);
  318. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  319. hsmartcard->gState = HAL_SMARTCARD_STATE_RESET;
  320. hsmartcard->RxState = HAL_SMARTCARD_STATE_RESET;
  321. /* Process Unlock */
  322. __HAL_UNLOCK(hsmartcard);
  323. return HAL_OK;
  324. }
  325. /**
  326. * @brief Initialize the SMARTCARD MSP.
  327. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  328. * the configuration information for the specified SMARTCARD module.
  329. * @retval None
  330. */
  331. __weak void HAL_SMARTCARD_MspInit(SMARTCARD_HandleTypeDef *hsmartcard)
  332. {
  333. /* Prevent unused argument(s) compilation warning */
  334. UNUSED(hsmartcard);
  335. /* NOTE : This function should not be modified, when the callback is needed,
  336. the HAL_SMARTCARD_MspInit can be implemented in the user file
  337. */
  338. }
  339. /**
  340. * @brief DeInitialize the SMARTCARD MSP.
  341. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  342. * the configuration information for the specified SMARTCARD module.
  343. * @retval None
  344. */
  345. __weak void HAL_SMARTCARD_MspDeInit(SMARTCARD_HandleTypeDef *hsmartcard)
  346. {
  347. /* Prevent unused argument(s) compilation warning */
  348. UNUSED(hsmartcard);
  349. /* NOTE : This function should not be modified, when the callback is needed,
  350. the HAL_SMARTCARD_MspDeInit can be implemented in the user file
  351. */
  352. }
  353. /**
  354. * @}
  355. */
  356. /** @defgroup SMARTCARD_Exported_Functions_Group2 IO operation functions
  357. * @brief SMARTCARD Transmit and Receive functions
  358. *
  359. @verbatim
  360. ==============================================================================
  361. ##### IO operation functions #####
  362. ==============================================================================
  363. [..]
  364. This subsection provides a set of functions allowing to manage the SMARTCARD data transfers.
  365. [..]
  366. Smartcard is a single wire half duplex communication protocol.
  367. The Smartcard interface is designed to support asynchronous protocol Smartcards as
  368. defined in the ISO 7816-3 standard. The USART should be configured as:
  369. (+) 8 bits plus parity: where M=1 and PCE=1 in the USART_CR1 register
  370. (+) 1.5 stop bits when transmitting and receiving: where STOP=11 in the USART_CR2 register.
  371. [..]
  372. (+) There are two modes of transfer:
  373. (++) Blocking mode: The communication is performed in polling mode.
  374. The HAL status of all data processing is returned by the same function
  375. after finishing transfer.
  376. (++) Non-Blocking mode: The communication is performed using Interrupts
  377. or DMA, the relevant API's return the HAL status.
  378. The end of the data processing will be indicated through the
  379. dedicated SMARTCARD IRQ when using Interrupt mode or the DMA IRQ when
  380. using DMA mode.
  381. (++) The HAL_SMARTCARD_TxCpltCallback(), HAL_SMARTCARD_RxCpltCallback() user callbacks
  382. will be executed respectively at the end of the Transmit or Receive process
  383. The HAL_SMARTCARD_ErrorCallback() user callback will be executed when a communication
  384. error is detected.
  385. (+) Blocking mode APIs are :
  386. (++) HAL_SMARTCARD_Transmit()
  387. (++) HAL_SMARTCARD_Receive()
  388. (+) Non Blocking mode APIs with Interrupt are :
  389. (++) HAL_SMARTCARD_Transmit_IT()
  390. (++) HAL_SMARTCARD_Receive_IT()
  391. (++) HAL_SMARTCARD_IRQHandler()
  392. (+) Non Blocking mode functions with DMA are :
  393. (++) HAL_SMARTCARD_Transmit_DMA()
  394. (++) HAL_SMARTCARD_Receive_DMA()
  395. (+) A set of Transfer Complete Callbacks are provided in non Blocking mode:
  396. (++) HAL_SMARTCARD_TxCpltCallback()
  397. (++) HAL_SMARTCARD_RxCpltCallback()
  398. (++) HAL_SMARTCARD_ErrorCallback()
  399. (#) Non-Blocking mode transfers could be aborted using Abort API's :
  400. (+) HAL_SMARTCARD_Abort()
  401. (+) HAL_SMARTCARD_AbortTransmit()
  402. (+) HAL_SMARTCARD_AbortReceive()
  403. (+) HAL_SMARTCARD_Abort_IT()
  404. (+) HAL_SMARTCARD_AbortTransmit_IT()
  405. (+) HAL_SMARTCARD_AbortReceive_IT()
  406. (#) For Abort services based on interrupts (HAL_SMARTCARD_Abortxxx_IT), a set of Abort Complete Callbacks are provided:
  407. (+) HAL_SMARTCARD_AbortCpltCallback()
  408. (+) HAL_SMARTCARD_AbortTransmitCpltCallback()
  409. (+) HAL_SMARTCARD_AbortReceiveCpltCallback()
  410. (#) In Non-Blocking mode transfers, possible errors are split into 2 categories.
  411. Errors are handled as follows :
  412. (+) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is
  413. to be evaluated by user : this concerns Frame Error, Parity Error or Noise Error in Interrupt mode reception .
  414. Received character is then retrieved and stored in Rx buffer, Error code is set to allow user to identify error type,
  415. and HAL_SMARTCARD_ErrorCallback() user callback is executed. Transfer is kept ongoing on SMARTCARD side.
  416. If user wants to abort it, Abort services should be called by user.
  417. (+) Error is considered as Blocking : Transfer could not be completed properly and is aborted.
  418. This concerns Frame Error in Interrupt mode tranmission, Overrun Error in Interrupt mode reception and all errors in DMA mode.
  419. Error code is set to allow user to identify error type, and HAL_SMARTCARD_ErrorCallback() user callback is executed.
  420. @endverbatim
  421. * @{
  422. */
  423. /**
  424. * @brief Send an amount of data in blocking mode.
  425. * @note When FIFO mode is enabled, writing a data in the TDR register adds one
  426. * data to the TXFIFO. Write operations to the TDR register are performed
  427. * when TXFNF flag is set. From hardware perspective, TXFNF flag and
  428. * TXE are mapped on the same bit-field.
  429. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  430. * the configuration information for the specified SMARTCARD module.
  431. * @param pData pointer to data buffer.
  432. * @param Size amount of data to be sent.
  433. * @param Timeout Timeout duration.
  434. * @retval HAL status
  435. */
  436. HAL_StatusTypeDef HAL_SMARTCARD_Transmit(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  437. {
  438. uint32_t tickstart = 0;
  439. /* Check that a Tx process is not already ongoing */
  440. if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
  441. {
  442. if((pData == NULL) || (Size == 0))
  443. {
  444. return HAL_ERROR;
  445. }
  446. /* Process Locked */
  447. __HAL_LOCK(hsmartcard);
  448. hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY_TX;
  449. /* Init tickstart for timeout managment*/
  450. tickstart = HAL_GetTick();
  451. /* Disable the Peripheral first to update mode for TX master */
  452. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  453. /* Disable Rx, enable Tx */
  454. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
  455. SET_BIT(hsmartcard->Instance->RQR, SMARTCARD_RXDATA_FLUSH_REQUEST);
  456. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TE);
  457. /* Enable the Peripheral */
  458. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  459. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  460. hsmartcard->TxXferSize = Size;
  461. hsmartcard->TxXferCount = Size;
  462. while(hsmartcard->TxXferCount > 0)
  463. {
  464. hsmartcard->TxXferCount--;
  465. if(SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, SMARTCARD_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
  466. {
  467. return HAL_TIMEOUT;
  468. }
  469. hsmartcard->Instance->TDR = (*pData++ & (uint8_t)0xFF);
  470. }
  471. if(SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, SMARTCARD_TRANSMISSION_COMPLETION_FLAG(hsmartcard), RESET, tickstart, Timeout) != HAL_OK)
  472. {
  473. return HAL_TIMEOUT;
  474. }
  475. /* Re-enable Rx at end of transmission if initial mode is Rx/Tx */
  476. if(hsmartcard->Init.Mode == SMARTCARD_MODE_TX_RX)
  477. {
  478. /* Disable the Peripheral first to update modes */
  479. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  480. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
  481. /* Enable the Peripheral */
  482. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  483. }
  484. /* At end of Tx process, restore hsmartcard->gState to Ready */
  485. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  486. /* Process Unlocked */
  487. __HAL_UNLOCK(hsmartcard);
  488. return HAL_OK;
  489. }
  490. else
  491. {
  492. return HAL_BUSY;
  493. }
  494. }
  495. /**
  496. * @brief Receive an amount of data in blocking mode.
  497. * @note When FIFO mode is enabled, the RXFNE flag is set as long as the RXFIFO
  498. * is not empty. Read operations from the RDR register are performed when
  499. * RXFNE flag is set. From hardware perspective, RXFNE flag and
  500. * RXNE are mapped on the same bit-field.
  501. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  502. * the configuration information for the specified SMARTCARD module.
  503. * @param pData pointer to data buffer.
  504. * @param Size amount of data to be received.
  505. * @param Timeout Timeout duration.
  506. * @retval HAL status
  507. */
  508. HAL_StatusTypeDef HAL_SMARTCARD_Receive(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  509. {
  510. uint32_t tickstart = 0;
  511. /* Check that a Rx process is not already ongoing */
  512. if(hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
  513. {
  514. if((pData == NULL) || (Size == 0))
  515. {
  516. return HAL_ERROR;
  517. }
  518. /* Process Locked */
  519. __HAL_LOCK(hsmartcard);
  520. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  521. hsmartcard->RxState = HAL_SMARTCARD_STATE_BUSY_RX;
  522. /* Init tickstart for timeout managment*/
  523. tickstart = HAL_GetTick();
  524. hsmartcard->RxXferSize = Size;
  525. hsmartcard->RxXferCount = Size;
  526. /* Check the remain data to be received */
  527. while(hsmartcard->RxXferCount > 0)
  528. {
  529. hsmartcard->RxXferCount--;
  530. if(SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, SMARTCARD_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
  531. {
  532. return HAL_TIMEOUT;
  533. }
  534. *pData++ = (uint8_t)(hsmartcard->Instance->RDR & (uint8_t)0x00FF);
  535. }
  536. /* At end of Rx process, restore hsmartcard->RxState to Ready */
  537. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  538. /* Process Unlocked */
  539. __HAL_UNLOCK(hsmartcard);
  540. return HAL_OK;
  541. }
  542. else
  543. {
  544. return HAL_BUSY;
  545. }
  546. }
  547. /**
  548. * @brief Send an amount of data in interrupt mode.
  549. * @note When FIFO mode is disabled, USART interrupt is generated whenever
  550. * USART_TDR register is empty, i.e one interrupt per data to transmit.
  551. * @note When FIFO mode is enabled, USART interrupt is generated whenever
  552. * TXFIFO threshold reached. In that case the interrupt rate depends on
  553. * TXFIFO threshold configuration.
  554. * @note This function sets the hsmartcard->TxIsr function pointer according to
  555. * the FIFO mode (data transmission processing depends on FIFO mode).
  556. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  557. * the configuration information for the specified SMARTCARD module.
  558. * @param pData pointer to data buffer.
  559. * @param Size amount of data to be sent.
  560. * @retval HAL status
  561. */
  562. HAL_StatusTypeDef HAL_SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size)
  563. {
  564. /* Check that a Tx process is not already ongoing */
  565. if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
  566. {
  567. if((pData == NULL) || (Size == 0))
  568. {
  569. return HAL_ERROR;
  570. }
  571. /* Process Locked */
  572. __HAL_LOCK(hsmartcard);
  573. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  574. hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY_TX;
  575. hsmartcard->pTxBuffPtr = pData;
  576. hsmartcard->TxXferSize = Size;
  577. hsmartcard->TxXferCount = Size;
  578. hsmartcard->TxISR = NULL;
  579. /* Disable the Peripheral first to update mode for TX master */
  580. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  581. /* Disable Rx, enable Tx */
  582. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
  583. SET_BIT(hsmartcard->Instance->RQR, SMARTCARD_RXDATA_FLUSH_REQUEST);
  584. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TE);
  585. /* Enable the Peripheral */
  586. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  587. #if defined(USART_CR1_FIFOEN)
  588. /* Configure Tx interrupt processing */
  589. if (hsmartcard->FifoMode == SMARTCARD_FIFOMODE_ENABLE)
  590. {
  591. /* Set the Tx ISR function pointer */
  592. hsmartcard->TxISR = SMARTCARD_TxISR_FIFOEN;
  593. /* Process Unlocked */
  594. __HAL_UNLOCK(hsmartcard);
  595. /* Enable the SMARTCARD Error Interrupt: (Frame error) */
  596. SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  597. /* Enable the TX FIFO threshold interrupt */
  598. SET_BIT(hsmartcard->Instance->CR3, USART_CR3_TXFTIE);
  599. }
  600. else
  601. #endif
  602. {
  603. /* Set the Tx ISR function pointer */
  604. hsmartcard->TxISR = SMARTCARD_TxISR;
  605. /* Process Unlocked */
  606. __HAL_UNLOCK(hsmartcard);
  607. /* Enable the SMARTCARD Error Interrupt: (Frame error) */
  608. SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  609. /* Enable the SMARTCARD Transmit Data Register Empty Interrupt */
  610. #if defined(USART_CR1_FIFOEN)
  611. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
  612. #else
  613. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TXEIE);
  614. #endif
  615. }
  616. return HAL_OK;
  617. }
  618. else
  619. {
  620. return HAL_BUSY;
  621. }
  622. }
  623. /**
  624. * @brief Receive an amount of data in interrupt mode.
  625. * @note When FIFO mode is disabled, USART interrupt is generated whenever
  626. * USART_RDR register can be read, i.e one interrupt per data to receive.
  627. * @note When FIFO mode is enabled, USART interrupt is generated whenever
  628. * RXFIFO threshold reached. In that case the interrupt rate depends on
  629. * RXFIFO threshold configuration.
  630. * @note This function sets the hsmartcard->RxIsr function pointer according to
  631. * the FIFO mode (data reception processing depends on FIFO mode).
  632. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  633. * the configuration information for the specified SMARTCARD module.
  634. * @param pData pointer to data buffer.
  635. * @param Size amount of data to be received.
  636. * @retval HAL status
  637. */
  638. HAL_StatusTypeDef HAL_SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size)
  639. {
  640. /* Check that a Rx process is not already ongoing */
  641. if(hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
  642. {
  643. if((pData == NULL) || (Size == 0))
  644. {
  645. return HAL_ERROR;
  646. }
  647. /* Process Locked */
  648. __HAL_LOCK(hsmartcard);
  649. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  650. hsmartcard->RxState = HAL_SMARTCARD_STATE_BUSY_RX;
  651. hsmartcard->pRxBuffPtr = pData;
  652. hsmartcard->RxXferSize = Size;
  653. hsmartcard->RxXferCount = Size;
  654. /* Configure Rx interrupt processing */
  655. #if defined(USART_CR1_FIFOEN)
  656. if ((hsmartcard->FifoMode == SMARTCARD_FIFOMODE_ENABLE) && (Size >= hsmartcard->NbRxDataToProcess))
  657. {
  658. /* Set the Rx ISR function pointer */
  659. hsmartcard->RxISR = SMARTCARD_RxISR_FIFOEN;
  660. /* Process Unlocked */
  661. __HAL_UNLOCK(hsmartcard);
  662. /* Enable the SMARTCART Parity Error interrupt and RX FIFO Threshold interrupt */
  663. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
  664. SET_BIT(hsmartcard->Instance->CR3, USART_CR3_RXFTIE);
  665. }
  666. else
  667. #endif
  668. {
  669. /* Set the Rx ISR function pointer */
  670. hsmartcard->RxISR = SMARTCARD_RxISR;
  671. /* Process Unlocked */
  672. __HAL_UNLOCK(hsmartcard);
  673. /* Enable the SMARTCARD Parity Error and Data Register not empty Interrupts */
  674. #if defined(USART_CR1_FIFOEN)
  675. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE| USART_CR1_RXNEIE_RXFNEIE);
  676. #else
  677. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE| USART_CR1_RXNEIE);
  678. #endif
  679. }
  680. /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
  681. SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  682. return HAL_OK;
  683. }
  684. else
  685. {
  686. return HAL_BUSY;
  687. }
  688. }
  689. /**
  690. * @brief Send an amount of data in DMA mode.
  691. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  692. * the configuration information for the specified SMARTCARD module.
  693. * @param pData pointer to data buffer.
  694. * @param Size amount of data to be sent.
  695. * @retval HAL status
  696. */
  697. HAL_StatusTypeDef HAL_SMARTCARD_Transmit_DMA(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size)
  698. {
  699. /* Check that a Tx process is not already ongoing */
  700. if (hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
  701. {
  702. if((pData == NULL) || (Size == 0))
  703. {
  704. return HAL_ERROR;
  705. }
  706. /* Process Locked */
  707. __HAL_LOCK(hsmartcard);
  708. hsmartcard->gState = HAL_SMARTCARD_STATE_BUSY_TX;
  709. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  710. hsmartcard->pTxBuffPtr = pData;
  711. hsmartcard->TxXferSize = Size;
  712. hsmartcard->TxXferCount = Size;
  713. /* Disable the Peripheral first to update mode for TX master */
  714. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  715. /* Disable Rx, enable Tx */
  716. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
  717. SET_BIT(hsmartcard->Instance->RQR, SMARTCARD_RXDATA_FLUSH_REQUEST);
  718. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_TE);
  719. /* Enable the Peripheral */
  720. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  721. /* Set the SMARTCARD DMA transfer complete callback */
  722. hsmartcard->hdmatx->XferCpltCallback = SMARTCARD_DMATransmitCplt;
  723. /* Set the SMARTCARD error callback */
  724. hsmartcard->hdmatx->XferErrorCallback = SMARTCARD_DMAError;
  725. /* Set the DMA abort callback */
  726. hsmartcard->hdmatx->XferAbortCallback = NULL;
  727. /* Enable the SMARTCARD transmit DMA channel */
  728. HAL_DMA_Start_IT(hsmartcard->hdmatx, (uint32_t)hsmartcard->pTxBuffPtr, (uint32_t)&hsmartcard->Instance->TDR, Size);
  729. /* Clear the TC flag in the ICR register */
  730. CLEAR_BIT(hsmartcard->Instance->ICR, USART_ICR_TCCF);
  731. /* Process Unlocked */
  732. __HAL_UNLOCK(hsmartcard);
  733. /* Enable the UART Error Interrupt: (Frame error) */
  734. SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  735. /* Enable the DMA transfer for transmit request by setting the DMAT bit
  736. in the SMARTCARD associated USART CR3 register */
  737. SET_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
  738. return HAL_OK;
  739. }
  740. else
  741. {
  742. return HAL_BUSY;
  743. }
  744. }
  745. /**
  746. * @brief Receive an amount of data in DMA mode.
  747. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  748. * the configuration information for the specified SMARTCARD module.
  749. * @param pData pointer to data buffer.
  750. * @param Size amount of data to be received.
  751. * @note The SMARTCARD-associated USART parity is enabled (PCE = 1),
  752. * the received data contain the parity bit (MSB position).
  753. * @retval HAL status
  754. */
  755. HAL_StatusTypeDef HAL_SMARTCARD_Receive_DMA(SMARTCARD_HandleTypeDef *hsmartcard, uint8_t *pData, uint16_t Size)
  756. {
  757. /* Check that a Rx process is not already ongoing */
  758. if(hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
  759. {
  760. if((pData == NULL) || (Size == 0))
  761. {
  762. return HAL_ERROR;
  763. }
  764. /* Process Locked */
  765. __HAL_LOCK(hsmartcard);
  766. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  767. hsmartcard->RxState = HAL_SMARTCARD_STATE_BUSY_RX;
  768. hsmartcard->pRxBuffPtr = pData;
  769. hsmartcard->RxXferSize = Size;
  770. /* Set the SMARTCARD DMA transfer complete callback */
  771. hsmartcard->hdmarx->XferCpltCallback = SMARTCARD_DMAReceiveCplt;
  772. /* Set the SMARTCARD DMA error callback */
  773. hsmartcard->hdmarx->XferErrorCallback = SMARTCARD_DMAError;
  774. /* Set the DMA abort callback */
  775. hsmartcard->hdmarx->XferAbortCallback = NULL;
  776. /* Enable the DMA channel */
  777. HAL_DMA_Start_IT(hsmartcard->hdmarx, (uint32_t)&hsmartcard->Instance->RDR, (uint32_t)hsmartcard->pRxBuffPtr, Size);
  778. /* Process Unlocked */
  779. __HAL_UNLOCK(hsmartcard);
  780. /* Enable the UART Parity Error Interrupt */
  781. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
  782. /* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
  783. SET_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  784. /* Enable the DMA transfer for the receiver request by setting the DMAR bit
  785. in the SMARTCARD associated USART CR3 register */
  786. SET_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
  787. return HAL_OK;
  788. }
  789. else
  790. {
  791. return HAL_BUSY;
  792. }
  793. }
  794. /**
  795. * @brief Abort ongoing transfers (blocking mode).
  796. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  797. * the configuration information for the specified SMARTCARD module.
  798. * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  799. * This procedure performs following operations :
  800. * - Disable SMARTCARD Interrupts (Tx and Rx)
  801. * - Disable the DMA transfer in the peripheral register (if enabled)
  802. * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  803. * - Set handle State to READY
  804. * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  805. * @retval HAL status
  806. */
  807. HAL_StatusTypeDef HAL_SMARTCARD_Abort(SMARTCARD_HandleTypeDef *hsmartcard)
  808. {
  809. /* Disable RTOIE, EOBIE, TXEIE, TCIE, RXNE, PE, RXFT, TXFT and ERR (Frame error, noise error, overrun error) interrupts */
  810. #if defined(USART_CR1_FIFOEN)
  811. CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE | USART_CR1_RTOIE | USART_CR1_EOBIE));
  812. CLEAR_BIT(hsmartcard->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE | USART_CR3_TXFTIE));
  813. #else
  814. CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE | USART_CR1_RTOIE | USART_CR1_EOBIE));
  815. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  816. #endif
  817. /* Disable the SMARTCARD DMA Tx request if enabled */
  818. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
  819. {
  820. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
  821. /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
  822. if(hsmartcard->hdmatx != NULL)
  823. {
  824. /* Set the SMARTCARD DMA Abort callback to Null.
  825. No call back execution at end of DMA abort procedure */
  826. hsmartcard->hdmatx->XferAbortCallback = NULL;
  827. HAL_DMA_Abort(hsmartcard->hdmatx);
  828. }
  829. }
  830. /* Disable the SMARTCARD DMA Rx request if enabled */
  831. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
  832. {
  833. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
  834. /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
  835. if(hsmartcard->hdmarx != NULL)
  836. {
  837. /* Set the SMARTCARD DMA Abort callback to Null.
  838. No call back execution at end of DMA abort procedure */
  839. hsmartcard->hdmarx->XferAbortCallback = NULL;
  840. HAL_DMA_Abort(hsmartcard->hdmarx);
  841. }
  842. }
  843. /* Reset Tx and Rx transfer counters */
  844. hsmartcard->TxXferCount = 0;
  845. hsmartcard->RxXferCount = 0;
  846. /* Clear the Error flags in the ICR register */
  847. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
  848. /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
  849. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  850. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  851. /* Reset Handle ErrorCode to No Error */
  852. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  853. return HAL_OK;
  854. }
  855. /**
  856. * @brief Abort ongoing Transmit transfer (blocking mode).
  857. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  858. * the configuration information for the specified SMARTCARD module.
  859. * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
  860. * This procedure performs following operations :
  861. * - Disable SMARTCARD Interrupts (Tx)
  862. * - Disable the DMA transfer in the peripheral register (if enabled)
  863. * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  864. * - Set handle State to READY
  865. * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  866. * @retval HAL status
  867. */
  868. HAL_StatusTypeDef HAL_SMARTCARD_AbortTransmit(SMARTCARD_HandleTypeDef *hsmartcard)
  869. {
  870. #if defined(USART_CR1_FIFOEN)
  871. /* Disable TCIE, TXEIE and TXFTIE interrupts */
  872. CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE));
  873. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_TXFTIE);
  874. #else
  875. /* Disable TXEIE and TCIE interrupts */
  876. CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
  877. #endif
  878. /* Check if a receive process is ongoing or not. If not disable ERR IT */
  879. if(hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
  880. {
  881. /* Disable the SMARTCARD Error Interrupt: (Frame error) */
  882. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  883. }
  884. /* Disable the SMARTCARD DMA Tx request if enabled */
  885. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
  886. {
  887. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
  888. /* Abort the SMARTCARD DMA Tx channel : use blocking DMA Abort API (no callback) */
  889. if(hsmartcard->hdmatx != NULL)
  890. {
  891. /* Set the SMARTCARD DMA Abort callback to Null.
  892. No call back execution at end of DMA abort procedure */
  893. hsmartcard->hdmatx->XferAbortCallback = NULL;
  894. HAL_DMA_Abort(hsmartcard->hdmatx);
  895. }
  896. }
  897. /* Reset Tx transfer counter */
  898. hsmartcard->TxXferCount = 0;
  899. /* Clear the Error flags in the ICR register */
  900. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_FEF);
  901. /* Restore hsmartcard->gState to Ready */
  902. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  903. return HAL_OK;
  904. }
  905. /**
  906. * @brief Abort ongoing Receive transfer (blocking mode).
  907. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  908. * the configuration information for the specified SMARTCARD module.
  909. * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
  910. * This procedure performs following operations :
  911. * - Disable SMARTCARD Interrupts (Rx)
  912. * - Disable the DMA transfer in the peripheral register (if enabled)
  913. * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  914. * - Set handle State to READY
  915. * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  916. * @retval HAL status
  917. */
  918. HAL_StatusTypeDef HAL_SMARTCARD_AbortReceive(SMARTCARD_HandleTypeDef *hsmartcard)
  919. {
  920. #if defined(USART_CR1_FIFOEN)
  921. /* Disable RTOIE, EOBIE, RXNE, PE, RXFT, TXFT and ERR (Frame error, noise error, overrun error) interrupts */
  922. CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_RTOIE | USART_CR1_EOBIE));
  923. CLEAR_BIT(hsmartcard->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
  924. #else
  925. /* Disable RTOIE, EOBIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  926. CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_RTOIE | USART_CR1_EOBIE));
  927. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  928. #endif
  929. /* Check if a Transmit process is ongoing or not. If not disable ERR IT */
  930. if(hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
  931. {
  932. /* Disable the SMARTCARD Error Interrupt: (Frame error) */
  933. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  934. }
  935. /* Disable the SMARTCARD DMA Rx request if enabled */
  936. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
  937. {
  938. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
  939. /* Abort the SMARTCARD DMA Rx channel : use blocking DMA Abort API (no callback) */
  940. if(hsmartcard->hdmarx != NULL)
  941. {
  942. /* Set the SMARTCARD DMA Abort callback to Null.
  943. No call back execution at end of DMA abort procedure */
  944. hsmartcard->hdmarx->XferAbortCallback = NULL;
  945. HAL_DMA_Abort(hsmartcard->hdmarx);
  946. }
  947. }
  948. /* Reset Rx transfer counter */
  949. hsmartcard->RxXferCount = 0;
  950. /* Clear the Error flags in the ICR register */
  951. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
  952. /* Restore hsmartcard->RxState to Ready */
  953. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  954. return HAL_OK;
  955. }
  956. /**
  957. * @brief Abort ongoing transfers (Interrupt mode).
  958. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  959. * the configuration information for the specified SMARTCARD module.
  960. * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  961. * This procedure performs following operations :
  962. * - Disable SMARTCARD Interrupts (Tx and Rx)
  963. * - Disable the DMA transfer in the peripheral register (if enabled)
  964. * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  965. * - Set handle State to READY
  966. * - At abort completion, call user abort complete callback
  967. * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
  968. * considered as completed only when user abort complete callback is executed (not when exiting function).
  969. * @retval HAL status
  970. */
  971. HAL_StatusTypeDef HAL_SMARTCARD_Abort_IT(SMARTCARD_HandleTypeDef *hsmartcard)
  972. {
  973. uint32_t abortcplt = 1;
  974. #if defined(USART_CR1_FIFOEN)
  975. /* Disable RTOIE, EOBIE, TXEIE, TCIE, RXNE, PE, RXFT, TXFT and ERR (Frame error, noise error, overrun error) interrupts */
  976. CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE | USART_CR1_RTOIE | USART_CR1_EOBIE));
  977. CLEAR_BIT(hsmartcard->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE | USART_CR3_TXFTIE));
  978. #else
  979. /* Disable RTOIE, EOBIE, TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  980. CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE | USART_CR1_RTOIE | USART_CR1_EOBIE));
  981. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  982. #endif
  983. /* If DMA Tx and/or DMA Rx Handles are associated to SMARTCARD Handle, DMA Abort complete callbacks should be initialised
  984. before any call to DMA Abort functions */
  985. /* DMA Tx Handle is valid */
  986. if(hsmartcard->hdmatx != NULL)
  987. {
  988. /* Set DMA Abort Complete callback if SMARTCARD DMA Tx request if enabled.
  989. Otherwise, set it to NULL */
  990. if(HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
  991. {
  992. hsmartcard->hdmatx->XferAbortCallback = SMARTCARD_DMATxAbortCallback;
  993. }
  994. else
  995. {
  996. hsmartcard->hdmatx->XferAbortCallback = NULL;
  997. }
  998. }
  999. /* DMA Rx Handle is valid */
  1000. if(hsmartcard->hdmarx != NULL)
  1001. {
  1002. /* Set DMA Abort Complete callback if SMARTCARD DMA Rx request if enabled.
  1003. Otherwise, set it to NULL */
  1004. if(HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
  1005. {
  1006. hsmartcard->hdmarx->XferAbortCallback = SMARTCARD_DMARxAbortCallback;
  1007. }
  1008. else
  1009. {
  1010. hsmartcard->hdmarx->XferAbortCallback = NULL;
  1011. }
  1012. }
  1013. /* Disable the SMARTCARD DMA Tx request if enabled */
  1014. if(HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
  1015. {
  1016. /* Disable DMA Tx at UART level */
  1017. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
  1018. /* Abort the SMARTCARD DMA Tx channel : use non blocking DMA Abort API (callback) */
  1019. if(hsmartcard->hdmatx != NULL)
  1020. {
  1021. /* SMARTCARD Tx DMA Abort callback has already been initialised :
  1022. will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
  1023. /* Abort DMA TX */
  1024. if(HAL_DMA_Abort_IT(hsmartcard->hdmatx) != HAL_OK)
  1025. {
  1026. hsmartcard->hdmatx->XferAbortCallback = NULL;
  1027. }
  1028. else
  1029. {
  1030. abortcplt = 0;
  1031. }
  1032. }
  1033. }
  1034. /* Disable the SMARTCARD DMA Rx request if enabled */
  1035. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
  1036. {
  1037. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
  1038. /* Abort the SMARTCARD DMA Rx channel : use non blocking DMA Abort API (callback) */
  1039. if(hsmartcard->hdmarx != NULL)
  1040. {
  1041. /* SMARTCARD Rx DMA Abort callback has already been initialised :
  1042. will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
  1043. /* Abort DMA RX */
  1044. if(HAL_DMA_Abort_IT(hsmartcard->hdmarx) != HAL_OK)
  1045. {
  1046. hsmartcard->hdmarx->XferAbortCallback = NULL;
  1047. abortcplt = 1;
  1048. }
  1049. else
  1050. {
  1051. abortcplt = 0;
  1052. }
  1053. }
  1054. }
  1055. /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
  1056. if (abortcplt == 1)
  1057. {
  1058. /* Reset Tx and Rx transfer counters */
  1059. hsmartcard->TxXferCount = 0;
  1060. hsmartcard->RxXferCount = 0;
  1061. /* Clear ISR function pointers */
  1062. hsmartcard->RxISR = NULL;
  1063. hsmartcard->TxISR = NULL;
  1064. /* Reset errorCode */
  1065. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  1066. /* Clear the Error flags in the ICR register */
  1067. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
  1068. /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
  1069. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  1070. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  1071. /* As no DMA to be aborted, call directly user Abort complete callback */
  1072. HAL_SMARTCARD_AbortCpltCallback(hsmartcard);
  1073. }
  1074. return HAL_OK;
  1075. }
  1076. /**
  1077. * @brief Abort ongoing Transmit transfer (Interrupt mode).
  1078. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1079. * the configuration information for the specified SMARTCARD module.
  1080. * @note This procedure could be used for aborting any ongoing Tx transfer started in Interrupt or DMA mode.
  1081. * This procedure performs following operations :
  1082. * - Disable SMARTCARD Interrupts (Tx)
  1083. * - Disable the DMA transfer in the peripheral register (if enabled)
  1084. * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  1085. * - Set handle State to READY
  1086. * - At abort completion, call user abort complete callback
  1087. * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
  1088. * considered as completed only when user abort complete callback is executed (not when exiting function).
  1089. * @retval HAL status
  1090. */
  1091. HAL_StatusTypeDef HAL_SMARTCARD_AbortTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard)
  1092. {
  1093. #if defined(USART_CR1_FIFOEN)
  1094. /* Disable TCIE, TXEIE and TXFTIE interrupts */
  1095. CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE));
  1096. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_TXFTIE);
  1097. #else
  1098. /* Disable TXEIE and TCIE interrupts */
  1099. CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
  1100. #endif
  1101. /* Check if a receive process is ongoing or not. If not disable ERR IT */
  1102. if(hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
  1103. {
  1104. /* Disable the SMARTCARD Error Interrupt: (Frame error) */
  1105. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  1106. }
  1107. /* Disable the SMARTCARD DMA Tx request if enabled */
  1108. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
  1109. {
  1110. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
  1111. /* Abort the SMARTCARD DMA Tx channel : use non blocking DMA Abort API (callback) */
  1112. if(hsmartcard->hdmatx != NULL)
  1113. {
  1114. /* Set the SMARTCARD DMA Abort callback :
  1115. will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
  1116. hsmartcard->hdmatx->XferAbortCallback = SMARTCARD_DMATxOnlyAbortCallback;
  1117. /* Abort DMA TX */
  1118. if(HAL_DMA_Abort_IT(hsmartcard->hdmatx) != HAL_OK)
  1119. {
  1120. /* Call Directly hsmartcard->hdmatx->XferAbortCallback function in case of error */
  1121. hsmartcard->hdmatx->XferAbortCallback(hsmartcard->hdmatx);
  1122. }
  1123. }
  1124. else
  1125. {
  1126. /* Reset Tx transfer counter */
  1127. hsmartcard->TxXferCount = 0;
  1128. /* Clear TxISR function pointers */
  1129. hsmartcard->TxISR = NULL;
  1130. /* Restore hsmartcard->gState to Ready */
  1131. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  1132. /* As no DMA to be aborted, call directly user Abort complete callback */
  1133. HAL_SMARTCARD_AbortTransmitCpltCallback(hsmartcard);
  1134. }
  1135. }
  1136. else
  1137. {
  1138. /* Reset Tx transfer counter */
  1139. hsmartcard->TxXferCount = 0;
  1140. /* Clear TxISR function pointers */
  1141. hsmartcard->TxISR = NULL;
  1142. /* Clear the Error flags in the ICR register */
  1143. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_FEF);
  1144. /* Restore hsmartcard->gState to Ready */
  1145. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  1146. /* As no DMA to be aborted, call directly user Abort complete callback */
  1147. HAL_SMARTCARD_AbortTransmitCpltCallback(hsmartcard);
  1148. }
  1149. return HAL_OK;
  1150. }
  1151. /**
  1152. * @brief Abort ongoing Receive transfer (Interrupt mode).
  1153. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1154. * the configuration information for the specified SMARTCARD module.
  1155. * @note This procedure could be used for aborting any ongoing Rx transfer started in Interrupt or DMA mode.
  1156. * This procedure performs following operations :
  1157. * - Disable SMARTCARD Interrupts (Rx)
  1158. * - Disable the DMA transfer in the peripheral register (if enabled)
  1159. * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  1160. * - Set handle State to READY
  1161. * - At abort completion, call user abort complete callback
  1162. * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
  1163. * considered as completed only when user abort complete callback is executed (not when exiting function).
  1164. * @retval HAL status
  1165. */
  1166. HAL_StatusTypeDef HAL_SMARTCARD_AbortReceive_IT(SMARTCARD_HandleTypeDef *hsmartcard)
  1167. {
  1168. #if defined(USART_CR1_FIFOEN)
  1169. /* Disable RTOIE, EOBIE, RXNE, PE, RXFT and ERR (Frame error, noise error, overrun error) interrupts */
  1170. CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_RTOIE | USART_CR1_EOBIE));
  1171. CLEAR_BIT(hsmartcard->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
  1172. #else
  1173. /* Disable RTOIE, EOBIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1174. CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_RTOIE | USART_CR1_EOBIE));
  1175. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  1176. #endif
  1177. /* Check if a Transmit process is ongoing or not. If not disable ERR IT */
  1178. if(hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
  1179. {
  1180. /* Disable the SMARTCARD Error Interrupt: (Frame error) */
  1181. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  1182. }
  1183. /* Disable the SMARTCARD DMA Rx request if enabled */
  1184. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
  1185. {
  1186. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
  1187. /* Abort the SMARTCARD DMA Rx channel : use non blocking DMA Abort API (callback) */
  1188. if(hsmartcard->hdmarx != NULL)
  1189. {
  1190. /* Set the SMARTCARD DMA Abort callback :
  1191. will lead to call HAL_SMARTCARD_AbortCpltCallback() at end of DMA abort procedure */
  1192. hsmartcard->hdmarx->XferAbortCallback = SMARTCARD_DMARxOnlyAbortCallback;
  1193. /* Abort DMA RX */
  1194. if(HAL_DMA_Abort_IT(hsmartcard->hdmarx) != HAL_OK)
  1195. {
  1196. /* Call Directly hsmartcard->hdmarx->XferAbortCallback function in case of error */
  1197. hsmartcard->hdmarx->XferAbortCallback(hsmartcard->hdmarx);
  1198. }
  1199. }
  1200. else
  1201. {
  1202. /* Reset Rx transfer counter */
  1203. hsmartcard->RxXferCount = 0;
  1204. /* Clear RxISR function pointer */
  1205. hsmartcard->RxISR = NULL;
  1206. /* Clear the Error flags in the ICR register */
  1207. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
  1208. /* Restore hsmartcard->RxState to Ready */
  1209. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  1210. /* As no DMA to be aborted, call directly user Abort complete callback */
  1211. HAL_SMARTCARD_AbortReceiveCpltCallback(hsmartcard);
  1212. }
  1213. }
  1214. else
  1215. {
  1216. /* Reset Rx transfer counter */
  1217. hsmartcard->RxXferCount = 0;
  1218. /* Clear RxISR function pointer */
  1219. hsmartcard->RxISR = NULL;
  1220. /* Clear the Error flags in the ICR register */
  1221. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
  1222. /* Restore hsmartcard->RxState to Ready */
  1223. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  1224. /* As no DMA to be aborted, call directly user Abort complete callback */
  1225. HAL_SMARTCARD_AbortReceiveCpltCallback(hsmartcard);
  1226. }
  1227. return HAL_OK;
  1228. }
  1229. /**
  1230. * @brief Handle SMARTCARD interrupt requests.
  1231. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1232. * the configuration information for the specified SMARTCARD module.
  1233. * @retval None
  1234. */
  1235. void HAL_SMARTCARD_IRQHandler(SMARTCARD_HandleTypeDef *hsmartcard)
  1236. {
  1237. uint32_t isrflags = READ_REG(hsmartcard->Instance->ISR);
  1238. uint32_t cr1its = READ_REG(hsmartcard->Instance->CR1);
  1239. uint32_t cr3its = READ_REG(hsmartcard->Instance->CR3);
  1240. uint32_t errorflags;
  1241. /* If no error occurs */
  1242. errorflags = (isrflags & (uint32_t)(USART_ISR_PE | USART_ISR_FE | USART_ISR_ORE | USART_ISR_NE | USART_ISR_RTOF));
  1243. if (errorflags == RESET)
  1244. {
  1245. /* SMARTCARD in mode Receiver ---------------------------------------------------*/
  1246. #if defined(USART_CR1_FIFOEN)
  1247. if(((isrflags & USART_ISR_RXNE_RXFNE) != RESET)
  1248. && ( ((cr1its & USART_CR1_RXNEIE_RXFNEIE) != RESET)
  1249. || ((cr3its & USART_CR3_RXFTIE) != RESET)) )
  1250. #else
  1251. if(((isrflags & USART_ISR_RXNE) != RESET)
  1252. && ((cr1its & USART_CR1_RXNEIE) != RESET))
  1253. #endif
  1254. {
  1255. if (hsmartcard->RxISR != NULL) {hsmartcard->RxISR(hsmartcard);}
  1256. return;
  1257. }
  1258. }
  1259. /* If some errors occur */
  1260. #if defined(USART_CR1_FIFOEN)
  1261. if( (errorflags != RESET)
  1262. && ( (((cr3its & (USART_CR3_RXFTIE | USART_CR3_EIE)) != RESET)
  1263. || ((cr1its & (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE)) != RESET))) )
  1264. #else
  1265. if( (errorflags != RESET)
  1266. && ( ((cr3its & USART_CR3_EIE) != RESET)
  1267. || ((cr1its & (USART_CR1_RXNEIE | USART_CR1_PEIE)) != RESET)) )
  1268. #endif
  1269. {
  1270. /* SMARTCARD parity error interrupt occurred -------------------------------------*/
  1271. if(((isrflags & USART_ISR_PE) != RESET) && ((cr1its & USART_CR1_PEIE) != RESET))
  1272. {
  1273. __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_PEF);
  1274. hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_PE;
  1275. }
  1276. /* SMARTCARD frame error interrupt occurred --------------------------------------*/
  1277. if(((isrflags & USART_ISR_FE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
  1278. {
  1279. __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_FEF);
  1280. hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_FE;
  1281. }
  1282. /* SMARTCARD noise error interrupt occurred --------------------------------------*/
  1283. if(((isrflags & USART_ISR_NE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
  1284. {
  1285. __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_NEF);
  1286. hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_NE;
  1287. }
  1288. /* SMARTCARD Over-Run interrupt occurred -----------------------------------------*/
  1289. #if defined(USART_CR1_FIFOEN)
  1290. if( ((isrflags & USART_ISR_ORE) != RESET)
  1291. && ( ((cr1its & USART_CR1_RXNEIE_RXFNEIE) != RESET)
  1292. || ((cr3its & USART_CR3_RXFTIE) != RESET)
  1293. || ((cr3its & USART_CR3_EIE) != RESET)) )
  1294. #else
  1295. if( ((isrflags & USART_ISR_ORE) != RESET)
  1296. && ( ((cr1its & USART_CR1_RXNEIE) != RESET)
  1297. || ((cr3its & USART_CR3_EIE) != RESET)) )
  1298. #endif
  1299. {
  1300. __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_OREF);
  1301. hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_ORE;
  1302. }
  1303. /* SMARTCARD receiver timeout interrupt occurred -----------------------------------------*/
  1304. if(((isrflags & USART_ISR_RTOF) != RESET) && ((cr1its & USART_CR1_RTOIE) != RESET))
  1305. {
  1306. __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_RTOF);
  1307. hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_RTO;
  1308. }
  1309. /* Call SMARTCARD Error Call back function if need be --------------------------*/
  1310. if(hsmartcard->ErrorCode != HAL_SMARTCARD_ERROR_NONE)
  1311. {
  1312. /* SMARTCARD in mode Receiver ---------------------------------------------------*/
  1313. #if defined(USART_CR1_FIFOEN)
  1314. if(((isrflags & USART_ISR_RXNE_RXFNE) != RESET)
  1315. && ( ((cr1its & USART_CR1_RXNEIE_RXFNEIE) != RESET)
  1316. || ((cr3its & USART_CR3_RXFTIE) != RESET)) )
  1317. #else
  1318. if(((isrflags & USART_ISR_RXNE) != RESET)
  1319. && ((cr1its & USART_CR1_RXNEIE) != RESET) )
  1320. #endif
  1321. {
  1322. if (hsmartcard->RxISR != NULL) {hsmartcard->RxISR(hsmartcard);}
  1323. }
  1324. /* If Error is to be considered as blocking :
  1325. - Receiver Timeout error in Reception
  1326. - Overrun error in Reception
  1327. - any error occurs in DMA mode reception
  1328. */
  1329. if ( ((hsmartcard->ErrorCode & (HAL_SMARTCARD_ERROR_RTO | HAL_SMARTCARD_ERROR_ORE)) != RESET)
  1330. || (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR)))
  1331. {
  1332. /* Blocking error : transfer is aborted
  1333. Set the SMARTCARD state ready to be able to start again the process,
  1334. Disable Rx Interrupts, and disable Rx DMA request, if ongoing */
  1335. SMARTCARD_EndRxTransfer(hsmartcard);
  1336. /* Disable the SMARTCARD DMA Rx request if enabled */
  1337. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR))
  1338. {
  1339. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
  1340. /* Abort the SMARTCARD DMA Rx channel */
  1341. if(hsmartcard->hdmarx != NULL)
  1342. {
  1343. /* Set the SMARTCARD DMA Abort callback :
  1344. will lead to call HAL_SMARTCARD_ErrorCallback() at end of DMA abort procedure */
  1345. hsmartcard->hdmarx->XferAbortCallback = SMARTCARD_DMAAbortOnError;
  1346. /* Abort DMA RX */
  1347. if(HAL_DMA_Abort_IT(hsmartcard->hdmarx) != HAL_OK)
  1348. {
  1349. /* Call Directly hsmartcard->hdmarx->XferAbortCallback function in case of error */
  1350. hsmartcard->hdmarx->XferAbortCallback(hsmartcard->hdmarx);
  1351. }
  1352. }
  1353. else
  1354. {
  1355. /* Call user error callback */
  1356. HAL_SMARTCARD_ErrorCallback(hsmartcard);
  1357. }
  1358. }
  1359. else
  1360. {
  1361. /* Call user error callback */
  1362. HAL_SMARTCARD_ErrorCallback(hsmartcard);
  1363. }
  1364. }
  1365. /* other error type to be considered as blocking :
  1366. - Frame error in Transmission
  1367. */
  1368. else if ( (hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX)
  1369. && ((hsmartcard->ErrorCode & HAL_SMARTCARD_ERROR_FE) != RESET))
  1370. {
  1371. /* Blocking error : transfer is aborted
  1372. Set the SMARTCARD state ready to be able to start again the process,
  1373. Disable Tx Interrupts, and disable Tx DMA request, if ongoing */
  1374. SMARTCARD_EndTxTransfer(hsmartcard);
  1375. /* Disable the SMARTCARD DMA Tx request if enabled */
  1376. if (HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT))
  1377. {
  1378. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
  1379. /* Abort the SMARTCARD DMA Tx channel */
  1380. if(hsmartcard->hdmatx != NULL)
  1381. {
  1382. /* Set the SMARTCARD DMA Abort callback :
  1383. will lead to call HAL_SMARTCARD_ErrorCallback() at end of DMA abort procedure */
  1384. hsmartcard->hdmatx->XferAbortCallback = SMARTCARD_DMAAbortOnError;
  1385. /* Abort DMA TX */
  1386. if(HAL_DMA_Abort_IT(hsmartcard->hdmatx) != HAL_OK)
  1387. {
  1388. /* Call Directly hsmartcard->hdmatx->XferAbortCallback function in case of error */
  1389. hsmartcard->hdmatx->XferAbortCallback(hsmartcard->hdmatx);
  1390. }
  1391. }
  1392. else
  1393. {
  1394. /* Call user error callback */
  1395. HAL_SMARTCARD_ErrorCallback(hsmartcard);
  1396. }
  1397. }
  1398. else
  1399. {
  1400. /* Call user error callback */
  1401. HAL_SMARTCARD_ErrorCallback(hsmartcard);
  1402. }
  1403. }
  1404. else
  1405. {
  1406. /* Non Blocking error : transfer could go on.
  1407. Error is notified to user through user error callback */
  1408. HAL_SMARTCARD_ErrorCallback(hsmartcard);
  1409. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  1410. }
  1411. }
  1412. return;
  1413. } /* End if some error occurs */
  1414. /* SMARTCARD in mode Receiver, end of block interruption ------------------------*/
  1415. if(((isrflags & USART_ISR_EOBF) != RESET) && ((cr1its & USART_CR1_EOBIE) != RESET))
  1416. {
  1417. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  1418. __HAL_UNLOCK(hsmartcard);
  1419. HAL_SMARTCARD_RxCpltCallback(hsmartcard);
  1420. /* Clear EOBF interrupt after HAL_SMARTCARD_RxCpltCallback() call for the End of Block information
  1421. * to be available during HAL_SMARTCARD_RxCpltCallback() processing */
  1422. __HAL_SMARTCARD_CLEAR_IT(hsmartcard, SMARTCARD_CLEAR_EOBF);
  1423. return;
  1424. }
  1425. /* SMARTCARD in mode Transmitter ------------------------------------------------*/
  1426. #if defined(USART_CR1_FIFOEN)
  1427. if(((isrflags & USART_ISR_TXE_TXFNF) != RESET)
  1428. && ( ((cr1its & USART_CR1_TXEIE_TXFNFIE) != RESET)
  1429. || ((cr3its & USART_CR3_TXFTIE) != RESET)) )
  1430. #else
  1431. if(((isrflags & USART_ISR_TXE) != RESET)
  1432. && ((cr1its & USART_CR1_TXEIE) != RESET))
  1433. #endif
  1434. {
  1435. if (hsmartcard->TxISR != NULL) {hsmartcard->TxISR(hsmartcard);}
  1436. return;
  1437. }
  1438. /* SMARTCARD in mode Transmitter (transmission end) ------------------------*/
  1439. if( (__HAL_SMARTCARD_GET_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication) != RESET)
  1440. && (__HAL_SMARTCARD_GET_IT_SOURCE(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication) != RESET))
  1441. {
  1442. SMARTCARD_EndTransmit_IT(hsmartcard);
  1443. return;
  1444. }
  1445. #if defined(USART_CR1_FIFOEN)
  1446. /* SMARTCARD TX Fifo Empty occurred ----------------------------------------------*/
  1447. if(((isrflags & USART_ISR_TXFE) != RESET) && ((cr1its & USART_CR1_TXFEIE) != RESET))
  1448. {
  1449. HAL_SMARTCARDEx_TxFifoEmptyCallback(hsmartcard);
  1450. return;
  1451. }
  1452. /* SMARTCARD RX Fifo Full occurred ----------------------------------------------*/
  1453. if(((isrflags & USART_ISR_RXFF) != RESET) && ((cr1its & USART_CR1_RXFFIE) != RESET))
  1454. {
  1455. HAL_SMARTCARDEx_RxFifoFullCallback(hsmartcard);
  1456. return;
  1457. }
  1458. #endif
  1459. }
  1460. /**
  1461. * @brief Tx Transfer completed callback.
  1462. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1463. * the configuration information for the specified SMARTCARD module.
  1464. * @retval None
  1465. */
  1466. __weak void HAL_SMARTCARD_TxCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
  1467. {
  1468. /* Prevent unused argument(s) compilation warning */
  1469. UNUSED(hsmartcard);
  1470. /* NOTE : This function should not be modified, when the callback is needed,
  1471. the HAL_SMARTCARD_TxCpltCallback can be implemented in the user file.
  1472. */
  1473. }
  1474. /**
  1475. * @brief Rx Transfer completed callback.
  1476. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1477. * the configuration information for the specified SMARTCARD module.
  1478. * @retval None
  1479. */
  1480. __weak void HAL_SMARTCARD_RxCpltCallback(SMARTCARD_HandleTypeDef *hsmartcard)
  1481. {
  1482. /* Prevent unused argument(s) compilation warning */
  1483. UNUSED(hsmartcard);
  1484. /* NOTE : This function should not be modified, when the callback is needed,
  1485. the HAL_SMARTCARD_RxCpltCallback can be implemented in the user file.
  1486. */
  1487. }
  1488. /**
  1489. * @brief SMARTCARD error callback.
  1490. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1491. * the configuration information for the specified SMARTCARD module.
  1492. * @retval None
  1493. */
  1494. __weak void HAL_SMARTCARD_ErrorCallback(SMARTCARD_HandleTypeDef *hsmartcard)
  1495. {
  1496. /* Prevent unused argument(s) compilation warning */
  1497. UNUSED(hsmartcard);
  1498. /* NOTE : This function should not be modified, when the callback is needed,
  1499. the HAL_SMARTCARD_ErrorCallback can be implemented in the user file.
  1500. */
  1501. }
  1502. /**
  1503. * @brief SMARTCARD Abort Complete callback.
  1504. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1505. * the configuration information for the specified SMARTCARD module.
  1506. * @retval None
  1507. */
  1508. __weak void HAL_SMARTCARD_AbortCpltCallback (SMARTCARD_HandleTypeDef *hsmartcard)
  1509. {
  1510. /* Prevent unused argument(s) compilation warning */
  1511. UNUSED(hsmartcard);
  1512. /* NOTE : This function should not be modified, when the callback is needed,
  1513. the HAL_SMARTCARD_AbortCpltCallback can be implemented in the user file.
  1514. */
  1515. }
  1516. /**
  1517. * @brief SMARTCARD Abort Complete callback.
  1518. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1519. * the configuration information for the specified SMARTCARD module.
  1520. * @retval None
  1521. */
  1522. __weak void HAL_SMARTCARD_AbortTransmitCpltCallback (SMARTCARD_HandleTypeDef *hsmartcard)
  1523. {
  1524. /* Prevent unused argument(s) compilation warning */
  1525. UNUSED(hsmartcard);
  1526. /* NOTE : This function should not be modified, when the callback is needed,
  1527. the HAL_SMARTCARD_AbortTransmitCpltCallback can be implemented in the user file.
  1528. */
  1529. }
  1530. /**
  1531. * @brief SMARTCARD Abort Receive Complete callback.
  1532. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1533. * the configuration information for the specified SMARTCARD module.
  1534. * @retval None
  1535. */
  1536. __weak void HAL_SMARTCARD_AbortReceiveCpltCallback (SMARTCARD_HandleTypeDef *hsmartcard)
  1537. {
  1538. /* Prevent unused argument(s) compilation warning */
  1539. UNUSED(hsmartcard);
  1540. /* NOTE : This function should not be modified, when the callback is needed,
  1541. the HAL_SMARTCARD_AbortReceiveCpltCallback can be implemented in the user file.
  1542. */
  1543. }
  1544. /**
  1545. * @}
  1546. */
  1547. /** @defgroup SMARTCARD_Exported_Functions_Group4 Peripheral State and Errors functions
  1548. * @brief SMARTCARD State and Errors functions
  1549. *
  1550. @verbatim
  1551. ==============================================================================
  1552. ##### Peripheral State and Errors functions #####
  1553. ==============================================================================
  1554. [..]
  1555. This subsection provides a set of functions allowing to return the State of SmartCard
  1556. handle and also return Peripheral Errors occurred during communication process
  1557. (+) HAL_SMARTCARD_GetState() API can be helpful to check in run-time the state
  1558. of the SMARTCARD peripheral.
  1559. (+) HAL_SMARTCARD_GetError() checks in run-time errors that could occur during
  1560. communication.
  1561. @endverbatim
  1562. * @{
  1563. */
  1564. /**
  1565. * @brief Return the SMARTCARD handle state.
  1566. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1567. * the configuration information for the specified SMARTCARD module.
  1568. * @retval SMARTCARD handle state
  1569. */
  1570. HAL_SMARTCARD_StateTypeDef HAL_SMARTCARD_GetState(SMARTCARD_HandleTypeDef *hsmartcard)
  1571. {
  1572. /* Return SMARTCARD handle state */
  1573. uint32_t temp1= 0x00, temp2 = 0x00;
  1574. temp1 = hsmartcard->gState;
  1575. temp2 = hsmartcard->RxState;
  1576. return (HAL_SMARTCARD_StateTypeDef)(temp1 | temp2);
  1577. }
  1578. /**
  1579. * @brief Return the SMARTCARD handle error code.
  1580. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1581. * the configuration information for the specified SMARTCARD module.
  1582. * @retval SMARTCARD handle Error Code
  1583. */
  1584. uint32_t HAL_SMARTCARD_GetError(SMARTCARD_HandleTypeDef *hsmartcard)
  1585. {
  1586. return hsmartcard->ErrorCode;
  1587. }
  1588. /**
  1589. * @}
  1590. */
  1591. /**
  1592. * @}
  1593. */
  1594. /** @defgroup SMARTCARD_Private_Functions SMARTCARD Private Functions
  1595. * @{
  1596. */
  1597. /**
  1598. * @brief Configure the SMARTCARD associated USART peripheral.
  1599. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1600. * the configuration information for the specified SMARTCARD module.
  1601. * @retval HAL status
  1602. */
  1603. static HAL_StatusTypeDef SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef *hsmartcard)
  1604. {
  1605. uint32_t tmpreg = 0x00000000;
  1606. SMARTCARD_ClockSourceTypeDef clocksource = SMARTCARD_CLOCKSOURCE_UNDEFINED;
  1607. HAL_StatusTypeDef ret = HAL_OK;
  1608. /* Check the parameters */
  1609. assert_param(IS_SMARTCARD_INSTANCE(hsmartcard->Instance));
  1610. assert_param(IS_SMARTCARD_BAUDRATE(hsmartcard->Init.BaudRate));
  1611. assert_param(IS_SMARTCARD_WORD_LENGTH(hsmartcard->Init.WordLength));
  1612. assert_param(IS_SMARTCARD_STOPBITS(hsmartcard->Init.StopBits));
  1613. assert_param(IS_SMARTCARD_PARITY(hsmartcard->Init.Parity));
  1614. assert_param(IS_SMARTCARD_MODE(hsmartcard->Init.Mode));
  1615. assert_param(IS_SMARTCARD_POLARITY(hsmartcard->Init.CLKPolarity));
  1616. assert_param(IS_SMARTCARD_PHASE(hsmartcard->Init.CLKPhase));
  1617. assert_param(IS_SMARTCARD_LASTBIT(hsmartcard->Init.CLKLastBit));
  1618. assert_param(IS_SMARTCARD_ONE_BIT_SAMPLE(hsmartcard->Init.OneBitSampling));
  1619. assert_param(IS_SMARTCARD_NACK(hsmartcard->Init.NACKEnable));
  1620. assert_param(IS_SMARTCARD_TIMEOUT(hsmartcard->Init.TimeOutEnable));
  1621. assert_param(IS_SMARTCARD_AUTORETRY_COUNT(hsmartcard->Init.AutoRetryCount));
  1622. #if defined(USART_PRESC_PRESCALER)
  1623. assert_param(IS_SMARTCARD_CLOCKPRESCALER(hsmartcard->Init.ClockPrescaler));
  1624. #endif
  1625. /*-------------------------- USART CR1 Configuration -----------------------*/
  1626. /* In SmartCard mode, M and PCE are forced to 1 (8 bits + parity).
  1627. * Oversampling is forced to 16 (OVER8 = 0).
  1628. * Configure the Parity and Mode:
  1629. * set PS bit according to hsmartcard->Init.Parity value
  1630. * set TE and RE bits according to hsmartcard->Init.Mode value */
  1631. tmpreg = (uint32_t) (hsmartcard->Init.Parity | hsmartcard->Init.Mode | hsmartcard->Init.WordLength);
  1632. MODIFY_REG(hsmartcard->Instance->CR1, USART_CR1_FIELDS, tmpreg);
  1633. /*-------------------------- USART CR2 Configuration -----------------------*/
  1634. tmpreg = hsmartcard->Init.StopBits;
  1635. /* Synchronous mode is activated by default */
  1636. tmpreg |= (uint32_t) USART_CR2_CLKEN | hsmartcard->Init.CLKPolarity;
  1637. tmpreg |= (uint32_t) hsmartcard->Init.CLKPhase | hsmartcard->Init.CLKLastBit;
  1638. tmpreg |= (uint32_t) hsmartcard->Init.TimeOutEnable;
  1639. MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_FIELDS, tmpreg);
  1640. /*-------------------------- USART CR3 Configuration -----------------------*/
  1641. /* Configure
  1642. * - one-bit sampling method versus three samples' majority rule
  1643. * according to hsmartcard->Init.OneBitSampling
  1644. * - NACK transmission in case of parity error according
  1645. * to hsmartcard->Init.NACKEnable
  1646. * - autoretry counter according to hsmartcard->Init.AutoRetryCount */
  1647. tmpreg = (uint32_t) hsmartcard->Init.OneBitSampling | hsmartcard->Init.NACKEnable;
  1648. tmpreg |= ((uint32_t)hsmartcard->Init.AutoRetryCount << USART_CR3_SCARCNT_Pos);
  1649. MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_FIELDS, tmpreg);
  1650. #if defined(USART_PRESC_PRESCALER)
  1651. /*--------------------- SMARTCARD clock PRESC Configuration ----------------*/
  1652. /* Configure
  1653. * - SMARTCARD Clock Prescaler: set PRESCALER according to hsmartcard->Init.ClockPrescaler value */
  1654. MODIFY_REG(hsmartcard->Instance->PRESC, USART_PRESC_PRESCALER, hsmartcard->Init.ClockPrescaler);
  1655. #endif
  1656. /*-------------------------- USART GTPR Configuration ----------------------*/
  1657. tmpreg = (hsmartcard->Init.Prescaler | ((uint32_t)hsmartcard->Init.GuardTime << USART_GTPR_GT_Pos));
  1658. MODIFY_REG(hsmartcard->Instance->GTPR, (USART_GTPR_GT|USART_GTPR_PSC), tmpreg);
  1659. /*-------------------------- USART RTOR Configuration ----------------------*/
  1660. tmpreg = ((uint32_t)hsmartcard->Init.BlockLength << USART_RTOR_BLEN_Pos);
  1661. if (hsmartcard->Init.TimeOutEnable == SMARTCARD_TIMEOUT_ENABLE)
  1662. {
  1663. assert_param(IS_SMARTCARD_TIMEOUT_VALUE(hsmartcard->Init.TimeOutValue));
  1664. tmpreg |= (uint32_t) hsmartcard->Init.TimeOutValue;
  1665. }
  1666. MODIFY_REG(hsmartcard->Instance->RTOR, (USART_RTOR_RTO|USART_RTOR_BLEN), tmpreg);
  1667. /*-------------------------- USART BRR Configuration -----------------------*/
  1668. SMARTCARD_GETCLOCKSOURCE(hsmartcard, clocksource);
  1669. tmpreg = 0;
  1670. switch (clocksource)
  1671. {
  1672. case SMARTCARD_CLOCKSOURCE_PCLK1:
  1673. #if defined(USART_PRESC_PRESCALER)
  1674. tmpreg = (uint16_t)((HAL_RCC_GetPCLK1Freq()/SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler] + (hsmartcard->Init.BaudRate/2)) / hsmartcard->Init.BaudRate);
  1675. #else
  1676. tmpreg = (uint16_t)((HAL_RCC_GetPCLK1Freq() + (hsmartcard->Init.BaudRate/2)) / hsmartcard->Init.BaudRate);
  1677. #endif
  1678. break;
  1679. case SMARTCARD_CLOCKSOURCE_PCLK2:
  1680. #if defined(USART_PRESC_PRESCALER)
  1681. tmpreg = (uint16_t)((HAL_RCC_GetPCLK2Freq()/SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler] + (hsmartcard->Init.BaudRate/2)) / hsmartcard->Init.BaudRate);
  1682. #else
  1683. tmpreg = (uint16_t)((HAL_RCC_GetPCLK2Freq() + (hsmartcard->Init.BaudRate/2)) / hsmartcard->Init.BaudRate);
  1684. #endif
  1685. break;
  1686. case SMARTCARD_CLOCKSOURCE_HSI:
  1687. #if defined(USART_PRESC_PRESCALER)
  1688. tmpreg = (uint16_t)((HSI_VALUE/SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler] + (hsmartcard->Init.BaudRate/2)) / hsmartcard->Init.BaudRate);
  1689. #else
  1690. tmpreg = (uint16_t)((HSI_VALUE + (hsmartcard->Init.BaudRate/2)) / hsmartcard->Init.BaudRate);
  1691. #endif
  1692. break;
  1693. case SMARTCARD_CLOCKSOURCE_SYSCLK:
  1694. #if defined(USART_PRESC_PRESCALER)
  1695. tmpreg = (uint16_t)((HAL_RCC_GetSysClockFreq()/SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler] + (hsmartcard->Init.BaudRate/2)) / hsmartcard->Init.BaudRate);
  1696. #else
  1697. tmpreg = (uint16_t)((HAL_RCC_GetSysClockFreq() + (hsmartcard->Init.BaudRate/2)) / hsmartcard->Init.BaudRate);
  1698. #endif
  1699. break;
  1700. case SMARTCARD_CLOCKSOURCE_LSE:
  1701. #if defined(USART_PRESC_PRESCALER)
  1702. tmpreg = (uint16_t)((LSE_VALUE/SMARTCARDPrescTable[hsmartcard->Init.ClockPrescaler] + (hsmartcard->Init.BaudRate/2)) / hsmartcard->Init.BaudRate);
  1703. #else
  1704. tmpreg = (uint16_t)((LSE_VALUE + (hsmartcard->Init.BaudRate/2)) / hsmartcard->Init.BaudRate);
  1705. #endif
  1706. break;
  1707. case SMARTCARD_CLOCKSOURCE_UNDEFINED:
  1708. default:
  1709. ret = HAL_ERROR;
  1710. break;
  1711. }
  1712. /* USARTDIV must be greater than or equal to 0d16 */
  1713. if ((tmpreg >= USART_BRR_MIN) && (tmpreg <= USART_BRR_MAX))
  1714. {
  1715. hsmartcard->Instance->BRR = tmpreg;
  1716. }
  1717. else
  1718. {
  1719. ret = HAL_ERROR;
  1720. }
  1721. #if defined(USART_CR1_FIFOEN)
  1722. /* Initialize the number of data to process during RX/TX ISR execution */
  1723. hsmartcard->NbTxDataToProcess = 1;
  1724. hsmartcard->NbRxDataToProcess = 1;
  1725. #endif
  1726. /* Clear ISR function pointers */
  1727. hsmartcard->RxISR = NULL;
  1728. hsmartcard->TxISR = NULL;
  1729. return ret;
  1730. }
  1731. /**
  1732. * @brief Configure the SMARTCARD associated USART peripheral advanced features.
  1733. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1734. * the configuration information for the specified SMARTCARD module.
  1735. * @retval None
  1736. */
  1737. static void SMARTCARD_AdvFeatureConfig(SMARTCARD_HandleTypeDef *hsmartcard)
  1738. {
  1739. /* Check whether the set of advanced features to configure is properly set */
  1740. assert_param(IS_SMARTCARD_ADVFEATURE_INIT(hsmartcard->AdvancedInit.AdvFeatureInit));
  1741. /* if required, configure TX pin active level inversion */
  1742. if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_TXINVERT_INIT))
  1743. {
  1744. assert_param(IS_SMARTCARD_ADVFEATURE_TXINV(hsmartcard->AdvancedInit.TxPinLevelInvert));
  1745. MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_TXINV, hsmartcard->AdvancedInit.TxPinLevelInvert);
  1746. }
  1747. /* if required, configure RX pin active level inversion */
  1748. if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_RXINVERT_INIT))
  1749. {
  1750. assert_param(IS_SMARTCARD_ADVFEATURE_RXINV(hsmartcard->AdvancedInit.RxPinLevelInvert));
  1751. MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_RXINV, hsmartcard->AdvancedInit.RxPinLevelInvert);
  1752. }
  1753. /* if required, configure data inversion */
  1754. if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_DATAINVERT_INIT))
  1755. {
  1756. assert_param(IS_SMARTCARD_ADVFEATURE_DATAINV(hsmartcard->AdvancedInit.DataInvert));
  1757. MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_DATAINV, hsmartcard->AdvancedInit.DataInvert);
  1758. }
  1759. /* if required, configure RX/TX pins swap */
  1760. if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_SWAP_INIT))
  1761. {
  1762. assert_param(IS_SMARTCARD_ADVFEATURE_SWAP(hsmartcard->AdvancedInit.Swap));
  1763. MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_SWAP, hsmartcard->AdvancedInit.Swap);
  1764. }
  1765. /* if required, configure RX overrun detection disabling */
  1766. if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_RXOVERRUNDISABLE_INIT))
  1767. {
  1768. assert_param(IS_SMARTCARD_OVERRUN(hsmartcard->AdvancedInit.OverrunDisable));
  1769. MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_OVRDIS, hsmartcard->AdvancedInit.OverrunDisable);
  1770. }
  1771. /* if required, configure DMA disabling on reception error */
  1772. if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_DMADISABLEONERROR_INIT))
  1773. {
  1774. assert_param(IS_SMARTCARD_ADVFEATURE_DMAONRXERROR(hsmartcard->AdvancedInit.DMADisableonRxError));
  1775. MODIFY_REG(hsmartcard->Instance->CR3, USART_CR3_DDRE, hsmartcard->AdvancedInit.DMADisableonRxError);
  1776. }
  1777. /* if required, configure MSB first on communication line */
  1778. if (HAL_IS_BIT_SET(hsmartcard->AdvancedInit.AdvFeatureInit, SMARTCARD_ADVFEATURE_MSBFIRST_INIT))
  1779. {
  1780. assert_param(IS_SMARTCARD_ADVFEATURE_MSBFIRST(hsmartcard->AdvancedInit.MSBFirst));
  1781. MODIFY_REG(hsmartcard->Instance->CR2, USART_CR2_MSBFIRST, hsmartcard->AdvancedInit.MSBFirst);
  1782. }
  1783. }
  1784. /**
  1785. * @brief Check the SMARTCARD Idle State.
  1786. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1787. * the configuration information for the specified SMARTCARD module.
  1788. * @retval HAL status
  1789. */
  1790. static HAL_StatusTypeDef SMARTCARD_CheckIdleState(SMARTCARD_HandleTypeDef *hsmartcard)
  1791. {
  1792. uint32_t tickstart = 0;
  1793. /* Initialize the SMARTCARD ErrorCode */
  1794. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  1795. /* Init tickstart for timeout managment*/
  1796. tickstart = HAL_GetTick();
  1797. /* Check if the Transmitter is enabled */
  1798. if((hsmartcard->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
  1799. {
  1800. /* Wait until TEACK flag is set */
  1801. if(SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, USART_ISR_TEACK, RESET, tickstart, SMARTCARD_TEACK_REACK_TIMEOUT) != HAL_OK)
  1802. {
  1803. /* Timeout occurred */
  1804. return HAL_TIMEOUT;
  1805. }
  1806. }
  1807. /* Check if the Receiver is enabled */
  1808. if((hsmartcard->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE)
  1809. {
  1810. /* Wait until REACK flag is set */
  1811. if(SMARTCARD_WaitOnFlagUntilTimeout(hsmartcard, USART_ISR_REACK, RESET, tickstart, SMARTCARD_TEACK_REACK_TIMEOUT) != HAL_OK)
  1812. {
  1813. /* Timeout occurred */
  1814. return HAL_TIMEOUT;
  1815. }
  1816. }
  1817. /* Initialize the SMARTCARD states */
  1818. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  1819. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  1820. /* Process Unlocked */
  1821. __HAL_UNLOCK(hsmartcard);
  1822. return HAL_OK;
  1823. }
  1824. /**
  1825. * @brief Handle SMARTCARD Communication Timeout.
  1826. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1827. * the configuration information for the specified SMARTCARD module.
  1828. * @param Flag Specifies the SMARTCARD flag to check.
  1829. * @param Status The new Flag status (SET or RESET).
  1830. * @param Tickstart Tick start value
  1831. * @param Timeout Timeout duration.
  1832. * @retval HAL status
  1833. */
  1834. static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsmartcard, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout)
  1835. {
  1836. /* Wait until flag is set */
  1837. while((__HAL_SMARTCARD_GET_FLAG(hsmartcard, Flag) ? SET : RESET) == Status)
  1838. {
  1839. /* Check for the Timeout */
  1840. if(Timeout != HAL_MAX_DELAY)
  1841. {
  1842. if((Timeout == 0) || ((HAL_GetTick()-Tickstart) > Timeout))
  1843. {
  1844. /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
  1845. #if defined(USART_CR1_FIFOEN)
  1846. CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE));
  1847. #else
  1848. CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE));
  1849. #endif
  1850. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  1851. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  1852. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  1853. /* Process Unlocked */
  1854. __HAL_UNLOCK(hsmartcard);
  1855. return HAL_TIMEOUT;
  1856. }
  1857. }
  1858. }
  1859. return HAL_OK;
  1860. }
  1861. /**
  1862. * @brief End ongoing Tx transfer on SMARTCARD peripheral (following error detection or Transmit completion).
  1863. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1864. * the configuration information for the specified SMARTCARD module.
  1865. * @retval None
  1866. */
  1867. static void SMARTCARD_EndTxTransfer(SMARTCARD_HandleTypeDef *hsmartcard)
  1868. {
  1869. /* Disable TXEIE, TCIE and ERR (Frame error, noise error, overrun error) interrupts */
  1870. #if defined(USART_CR1_FIFOEN)
  1871. CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE));
  1872. #else
  1873. CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_TXEIE | USART_CR1_TCIE));
  1874. #endif
  1875. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  1876. /* At end of Tx process, restore hsmartcard->gState to Ready */
  1877. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  1878. }
  1879. /**
  1880. * @brief End ongoing Rx transfer on UART peripheral (following error detection or Reception completion).
  1881. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1882. * the configuration information for the specified SMARTCARD module.
  1883. * @retval None
  1884. */
  1885. static void SMARTCARD_EndRxTransfer(SMARTCARD_HandleTypeDef *hsmartcard)
  1886. {
  1887. /* Disable RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1888. #if defined(USART_CR1_FIFOEN)
  1889. CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE));
  1890. #else
  1891. CLEAR_BIT(hsmartcard->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  1892. #endif
  1893. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  1894. /* At end of Rx process, restore hsmartcard->RxState to Ready */
  1895. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  1896. }
  1897. /**
  1898. * @brief DMA SMARTCARD transmit process complete callback.
  1899. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  1900. * the configuration information for the specified DMA module.
  1901. * @retval None
  1902. */
  1903. static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma)
  1904. {
  1905. SMARTCARD_HandleTypeDef* hsmartcard = (SMARTCARD_HandleTypeDef*)(hdma->Parent);
  1906. hsmartcard->TxXferCount = 0;
  1907. /* Disable the DMA transfer for transmit request by resetting the DMAT bit
  1908. in the SMARTCARD associated USART CR3 register */
  1909. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAT);
  1910. /* Enable the SMARTCARD Transmit Complete Interrupt */
  1911. __HAL_SMARTCARD_ENABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication);
  1912. }
  1913. /**
  1914. * @brief DMA SMARTCARD receive process complete callback.
  1915. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  1916. * the configuration information for the specified DMA module.
  1917. * @retval None
  1918. */
  1919. static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
  1920. {
  1921. SMARTCARD_HandleTypeDef* hsmartcard = (SMARTCARD_HandleTypeDef*)(hdma->Parent);
  1922. hsmartcard->RxXferCount = 0;
  1923. /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
  1924. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
  1925. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  1926. /* Disable the DMA transfer for the receiver request by resetting the DMAR bit
  1927. in the SMARTCARD associated USART CR3 register */
  1928. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_DMAR);
  1929. /* At end of Rx process, restore hsmartcard->RxState to Ready */
  1930. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  1931. HAL_SMARTCARD_RxCpltCallback(hsmartcard);
  1932. }
  1933. /**
  1934. * @brief DMA SMARTCARD communication error callback.
  1935. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  1936. * the configuration information for the specified DMA module.
  1937. * @retval None
  1938. */
  1939. static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma)
  1940. {
  1941. SMARTCARD_HandleTypeDef* hsmartcard = (SMARTCARD_HandleTypeDef*)(hdma->Parent);
  1942. /* Stop SMARTCARD DMA Tx request if ongoing */
  1943. if ( (hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX)
  1944. &&(HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAT)) )
  1945. {
  1946. hsmartcard->TxXferCount = 0;
  1947. SMARTCARD_EndTxTransfer(hsmartcard);
  1948. }
  1949. /* Stop SMARTCARD DMA Rx request if ongoing */
  1950. if ( (hsmartcard->RxState == HAL_SMARTCARD_STATE_BUSY_RX)
  1951. &&(HAL_IS_BIT_SET(hsmartcard->Instance->CR3, USART_CR3_DMAR)) )
  1952. {
  1953. hsmartcard->RxXferCount = 0;
  1954. SMARTCARD_EndRxTransfer(hsmartcard);
  1955. }
  1956. hsmartcard->ErrorCode |= HAL_SMARTCARD_ERROR_DMA;
  1957. HAL_SMARTCARD_ErrorCallback(hsmartcard);
  1958. }
  1959. /**
  1960. * @brief DMA SMARTCARD communication abort callback, when initiated by HAL services on Error
  1961. * (To be called at end of DMA Abort procedure following error occurrence).
  1962. * @param hdma DMA handle.
  1963. * @retval None
  1964. */
  1965. static void SMARTCARD_DMAAbortOnError(DMA_HandleTypeDef *hdma)
  1966. {
  1967. SMARTCARD_HandleTypeDef* hsmartcard = (SMARTCARD_HandleTypeDef*)(hdma->Parent);
  1968. hsmartcard->RxXferCount = 0;
  1969. hsmartcard->TxXferCount = 0;
  1970. HAL_SMARTCARD_ErrorCallback(hsmartcard);
  1971. }
  1972. /**
  1973. * @brief DMA SMARTCARD Tx communication abort callback, when initiated by user
  1974. * (To be called at end of DMA Tx Abort procedure following user abort request).
  1975. * @note When this callback is executed, User Abort complete call back is called only if no
  1976. * Abort still ongoing for Rx DMA Handle.
  1977. * @param hdma DMA handle.
  1978. * @retval None
  1979. */
  1980. static void SMARTCARD_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
  1981. {
  1982. SMARTCARD_HandleTypeDef* hsmartcard = (SMARTCARD_HandleTypeDef* )(hdma->Parent);
  1983. hsmartcard->hdmatx->XferAbortCallback = NULL;
  1984. /* Check if an Abort process is still ongoing */
  1985. if(hsmartcard->hdmarx != NULL)
  1986. {
  1987. if(hsmartcard->hdmarx->XferAbortCallback != NULL)
  1988. {
  1989. return;
  1990. }
  1991. }
  1992. /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
  1993. hsmartcard->TxXferCount = 0;
  1994. hsmartcard->RxXferCount = 0;
  1995. /* Reset errorCode */
  1996. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  1997. /* Clear the Error flags in the ICR register */
  1998. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
  1999. /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
  2000. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  2001. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  2002. /* Call user Abort complete callback */
  2003. HAL_SMARTCARD_AbortCpltCallback(hsmartcard);
  2004. }
  2005. /**
  2006. * @brief DMA SMARTCARD Rx communication abort callback, when initiated by user
  2007. * (To be called at end of DMA Rx Abort procedure following user abort request).
  2008. * @note When this callback is executed, User Abort complete call back is called only if no
  2009. * Abort still ongoing for Tx DMA Handle.
  2010. * @param hdma DMA handle.
  2011. * @retval None
  2012. */
  2013. static void SMARTCARD_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
  2014. {
  2015. SMARTCARD_HandleTypeDef* hsmartcard = (SMARTCARD_HandleTypeDef* )(hdma->Parent);
  2016. hsmartcard->hdmarx->XferAbortCallback = NULL;
  2017. /* Check if an Abort process is still ongoing */
  2018. if(hsmartcard->hdmatx != NULL)
  2019. {
  2020. if(hsmartcard->hdmatx->XferAbortCallback != NULL)
  2021. {
  2022. return;
  2023. }
  2024. }
  2025. /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
  2026. hsmartcard->TxXferCount = 0;
  2027. hsmartcard->RxXferCount = 0;
  2028. /* Reset errorCode */
  2029. hsmartcard->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  2030. /* Clear the Error flags in the ICR register */
  2031. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
  2032. /* Restore hsmartcard->gState and hsmartcard->RxState to Ready */
  2033. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  2034. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  2035. /* Call user Abort complete callback */
  2036. HAL_SMARTCARD_AbortCpltCallback(hsmartcard);
  2037. }
  2038. /**
  2039. * @brief DMA SMARTCARD Tx communication abort callback, when initiated by user by a call to
  2040. * HAL_SMARTCARD_AbortTransmit_IT API (Abort only Tx transfer)
  2041. * (This callback is executed at end of DMA Tx Abort procedure following user abort request,
  2042. * and leads to user Tx Abort Complete callback execution).
  2043. * @param hdma DMA handle.
  2044. * @retval None
  2045. */
  2046. static void SMARTCARD_DMATxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
  2047. {
  2048. SMARTCARD_HandleTypeDef* hsmartcard = (SMARTCARD_HandleTypeDef*)(hdma->Parent);
  2049. hsmartcard->TxXferCount = 0;
  2050. /* Clear the Error flags in the ICR register */
  2051. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_FEF);
  2052. /* Restore hsmartcard->gState to Ready */
  2053. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  2054. /* Call user Abort complete callback */
  2055. HAL_SMARTCARD_AbortTransmitCpltCallback(hsmartcard);
  2056. }
  2057. /**
  2058. * @brief DMA SMARTCARD Rx communication abort callback, when initiated by user by a call to
  2059. * HAL_SMARTCARD_AbortReceive_IT API (Abort only Rx transfer)
  2060. * (This callback is executed at end of DMA Rx Abort procedure following user abort request,
  2061. * and leads to user Rx Abort Complete callback execution).
  2062. * @param hdma DMA handle.
  2063. * @retval None
  2064. */
  2065. static void SMARTCARD_DMARxOnlyAbortCallback(DMA_HandleTypeDef *hdma)
  2066. {
  2067. SMARTCARD_HandleTypeDef* hsmartcard = ( SMARTCARD_HandleTypeDef* )(hdma->Parent);
  2068. hsmartcard->RxXferCount = 0;
  2069. /* Clear the Error flags in the ICR register */
  2070. __HAL_SMARTCARD_CLEAR_FLAG(hsmartcard, SMARTCARD_CLEAR_OREF | SMARTCARD_CLEAR_NEF | SMARTCARD_CLEAR_PEF | SMARTCARD_CLEAR_FEF | SMARTCARD_CLEAR_RTOF | SMARTCARD_CLEAR_EOBF);
  2071. /* Restore hsmartcard->RxState to Ready */
  2072. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  2073. /* Call user Abort complete callback */
  2074. HAL_SMARTCARD_AbortReceiveCpltCallback(hsmartcard);
  2075. }
  2076. /**
  2077. * @brief Send an amount of data in non-blocking mode.
  2078. * @note Function called under interruption only, once
  2079. * interruptions have been enabled by HAL_SMARTCARD_Transmit_IT()
  2080. * and when the FIFO mode is disabled.
  2081. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2082. * the configuration information for the specified SMARTCARD module.
  2083. * @retval None
  2084. */
  2085. static void SMARTCARD_TxISR(SMARTCARD_HandleTypeDef *hsmartcard)
  2086. {
  2087. /* Check that a Tx process is ongoing */
  2088. if (hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX)
  2089. {
  2090. if(hsmartcard->TxXferCount == 0)
  2091. {
  2092. /* Disable the SMARTCARD Transmit Data Register Empty Interrupt */
  2093. #if defined(USART_CR1_FIFOEN)
  2094. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
  2095. #else
  2096. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_TXEIE);
  2097. #endif
  2098. /* Enable the SMARTCARD Transmit Complete Interrupt */
  2099. __HAL_SMARTCARD_ENABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication);
  2100. }
  2101. else
  2102. {
  2103. hsmartcard->Instance->TDR = (*hsmartcard->pTxBuffPtr++ & (uint8_t)0xFF);
  2104. hsmartcard->TxXferCount--;
  2105. }
  2106. }
  2107. }
  2108. #if defined(USART_CR1_FIFOEN)
  2109. /**
  2110. * @brief Send an amount of data in non-blocking mode.
  2111. * @note Function called under interruption only, once
  2112. * interruptions have been enabled by HAL_SMARTCARD_Transmit_IT()
  2113. * and when the FIFO mode is enabled.
  2114. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2115. * the configuration information for the specified SMARTCARD module.
  2116. * @retval None
  2117. */
  2118. static void SMARTCARD_TxISR_FIFOEN(SMARTCARD_HandleTypeDef *hsmartcard)
  2119. {
  2120. uint8_t nb_tx_data;
  2121. /* Check that a Tx process is ongoing */
  2122. if (hsmartcard->gState == HAL_SMARTCARD_STATE_BUSY_TX)
  2123. {
  2124. for(nb_tx_data = hsmartcard->NbTxDataToProcess ; nb_tx_data > 0 ; nb_tx_data--)
  2125. {
  2126. if(hsmartcard->TxXferCount == 0)
  2127. {
  2128. /* Disable the SMARTCARD Transmit Data Register Empty Interrupt */
  2129. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
  2130. /* Enable the SMARTCARD Transmit Complete Interrupt */
  2131. __HAL_SMARTCARD_ENABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication);
  2132. }
  2133. else if (READ_BIT(hsmartcard->Instance->ISR, USART_ISR_TXE_TXFNF) != RESET)
  2134. {
  2135. hsmartcard->Instance->TDR = (*hsmartcard->pTxBuffPtr++ & (uint8_t)0xFF);
  2136. hsmartcard->TxXferCount--;
  2137. }
  2138. }
  2139. }
  2140. }
  2141. #endif
  2142. /**
  2143. * @brief Wrap up transmission in non-blocking mode.
  2144. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2145. * the configuration information for the specified SMARTCARD module.
  2146. * @retval None
  2147. */
  2148. static void SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard)
  2149. {
  2150. /* Disable the SMARTCARD Transmit Complete Interrupt */
  2151. __HAL_SMARTCARD_DISABLE_IT(hsmartcard, hsmartcard->AdvancedInit.TxCompletionIndication);
  2152. /* Check if a receive process is ongoing or not. If not disable ERR IT */
  2153. if(hsmartcard->RxState == HAL_SMARTCARD_STATE_READY)
  2154. {
  2155. /* Disable the SMARTCARD Error Interrupt: (Frame error) */
  2156. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  2157. }
  2158. /* Re-enable Rx at end of transmission if initial mode is Rx/Tx */
  2159. if(hsmartcard->Init.Mode == SMARTCARD_MODE_TX_RX)
  2160. {
  2161. /* Disable the Peripheral first to update modes */
  2162. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  2163. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_RE);
  2164. /* Enable the Peripheral */
  2165. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_UE);
  2166. }
  2167. /* Tx process is ended, restore hsmartcard->gState to Ready */
  2168. hsmartcard->gState = HAL_SMARTCARD_STATE_READY;
  2169. /* Clear TxISR function pointer */
  2170. hsmartcard->TxISR = NULL;
  2171. HAL_SMARTCARD_TxCpltCallback(hsmartcard);
  2172. }
  2173. /**
  2174. * @brief Receive an amount of data in non-blocking mode.
  2175. * @note Function called under interruption only, once
  2176. * interruptions have been enabled by HAL_SMARTCARD_Receive_IT()
  2177. * and when the FIFO mode is disabled.
  2178. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2179. * the configuration information for the specified SMARTCARD module.
  2180. * @retval None
  2181. */
  2182. static void SMARTCARD_RxISR(SMARTCARD_HandleTypeDef *hsmartcard)
  2183. {
  2184. /* Check that a Rx process is ongoing */
  2185. if (hsmartcard->RxState == HAL_SMARTCARD_STATE_BUSY_RX)
  2186. {
  2187. *hsmartcard->pRxBuffPtr++ = (uint8_t)(hsmartcard->Instance->RDR & (uint8_t)0xFF);
  2188. if(--hsmartcard->RxXferCount == 0)
  2189. {
  2190. #if defined(USART_CR1_FIFOEN)
  2191. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
  2192. #else
  2193. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RXNEIE);
  2194. #endif
  2195. /* Check if a transmit process is ongoing or not. If not disable ERR IT */
  2196. if(hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
  2197. {
  2198. /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
  2199. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  2200. }
  2201. /* Disable the SMARTCARD Parity Error Interrupt */
  2202. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
  2203. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  2204. /* Clear RxISR function pointer */
  2205. hsmartcard->RxISR = NULL;
  2206. HAL_SMARTCARD_RxCpltCallback(hsmartcard);
  2207. }
  2208. }
  2209. else
  2210. {
  2211. /* Clear RXNE interrupt flag */
  2212. __HAL_SMARTCARD_SEND_REQ(hsmartcard, SMARTCARD_RXDATA_FLUSH_REQUEST);
  2213. }
  2214. }
  2215. #if defined(USART_CR1_FIFOEN)
  2216. /**
  2217. * @brief Receive an amount of data in non-blocking mode.
  2218. * @note Function called under interruption only, once
  2219. * interruptions have been enabled by HAL_SMARTCARD_Receive_IT()
  2220. * and when the FIFO mode is enabled.
  2221. * @param hsmartcard Pointer to a SMARTCARD_HandleTypeDef structure that contains
  2222. * the configuration information for the specified SMARTCARD module.
  2223. * @retval None
  2224. */
  2225. static void SMARTCARD_RxISR_FIFOEN(SMARTCARD_HandleTypeDef *hsmartcard)
  2226. {
  2227. uint8_t nb_rx_data;
  2228. /* Check that a Rx process is ongoing */
  2229. if (hsmartcard->RxState == HAL_SMARTCARD_STATE_BUSY_RX)
  2230. {
  2231. for(nb_rx_data = hsmartcard->NbRxDataToProcess ; nb_rx_data > 0 ; nb_rx_data--)
  2232. {
  2233. *hsmartcard->pRxBuffPtr++ = (uint8_t)(hsmartcard->Instance->RDR & (uint8_t)0xFF);
  2234. if(--hsmartcard->RxXferCount == 0)
  2235. {
  2236. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
  2237. /* Check if a transmit process is ongoing or not. If not disable ERR IT */
  2238. if(hsmartcard->gState == HAL_SMARTCARD_STATE_READY)
  2239. {
  2240. /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
  2241. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_EIE);
  2242. }
  2243. /* Disable the SMARTCARD Parity Error Interrupt */
  2244. CLEAR_BIT(hsmartcard->Instance->CR1, USART_CR1_PEIE);
  2245. hsmartcard->RxState = HAL_SMARTCARD_STATE_READY;
  2246. /* Clear RxISR function pointer */
  2247. hsmartcard->RxISR = NULL;
  2248. HAL_SMARTCARD_RxCpltCallback(hsmartcard);
  2249. }
  2250. }
  2251. /* When remaining number of bytes to receive is less than the RX FIFO
  2252. threshold, next incoming frames are processed as if FIFO mode was
  2253. disabled (i.e. one interrupt per received frame).
  2254. */
  2255. if (((hsmartcard->RxXferCount != 0U)) && (hsmartcard->RxXferCount < hsmartcard->NbRxDataToProcess))
  2256. {
  2257. /* Disable the UART RXFT interrupt*/
  2258. CLEAR_BIT(hsmartcard->Instance->CR3, USART_CR3_RXFTIE);
  2259. /* Update the RxISR function pointer */
  2260. hsmartcard->RxISR = SMARTCARD_RxISR;
  2261. /* Enable the UART Data Register Not Empty interrupt */
  2262. SET_BIT(hsmartcard->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
  2263. }
  2264. }
  2265. else
  2266. {
  2267. /* Clear RXNE interrupt flag */
  2268. __HAL_SMARTCARD_SEND_REQ(hsmartcard, SMARTCARD_RXDATA_FLUSH_REQUEST);
  2269. }
  2270. }
  2271. #endif
  2272. /**
  2273. * @}
  2274. */
  2275. #endif /* HAL_SMARTCARD_MODULE_ENABLED */
  2276. /**
  2277. * @}
  2278. */
  2279. /**
  2280. * @}
  2281. */
  2282. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/