stm32f1xx_hal_smartcard.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_hal_smartcard.c
  4. * @author MCD Application Team
  5. * @version V1.0.4
  6. * @date 29-April-2016
  7. * @brief SMARTCARD HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of the SMARTCARD peripheral:
  10. * + Initialization and de-initialization functions
  11. * + IO operation functions
  12. * + Peripheral State and Errors functions
  13. * + Peripheral Control functions
  14. *
  15. @verbatim
  16. ==============================================================================
  17. ##### How to use this driver #####
  18. ==============================================================================
  19. [..]
  20. The SMARTCARD HAL driver can be used as follows:
  21. (#) Declare a SMARTCARD_HandleTypeDef handle structure.
  22. (#) Initialize the SMARTCARD low level resources by implementing the HAL_SMARTCARD_MspInit() API:
  23. (##) Enable the interface clock of the USARTx associated to the SMARTCARD.
  24. (##) SMARTCARD pins configuration:
  25. (+++) Enable the clock for the SMARTCARD GPIOs.
  26. (+++) Configure the USART pins (TX as alternate function pull-up, RX as alternate function Input).
  27. (##) NVIC configuration if you need to use interrupt process (HAL_SMARTCARD_Transmit_IT()
  28. and HAL_SMARTCARD_Receive_IT() APIs):
  29. (+++) Configure the USARTx interrupt priority.
  30. (+++) Enable the NVIC USART IRQ handle.
  31. (##) DMA Configuration if you need to use DMA process (HAL_SMARTCARD_Transmit_DMA()
  32. and HAL_SMARTCARD_Receive_DMA() APIs):
  33. (+++) Declare a DMA handle structure for the Tx/Rx channel.
  34. (+++) Enable the DMAx interface clock.
  35. (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
  36. (+++) Configure the DMA Tx/Rx channel.
  37. (+++) Associate the initilalized DMA handle to the SMARTCARD DMA Tx/Rx handle.
  38. (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx channel.
  39. (+++) Configure the USARTx interrupt priority and enable the NVIC USART IRQ handle
  40. (used for last byte sending completion detection in DMA non circular mode)
  41. (#) Program the Baud Rate, Word Length , Stop Bit, Parity, Hardware
  42. flow control and Mode(Receiver/Transmitter) in the SMARTCARD Init 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 customed HAL_SMARTCARD_MspInit(&hsc) API.
  46. -@@- The specific SMARTCARD interrupts (Transmission complete interrupt,
  47. RXNE interrupt and Error Interrupts) will be managed using the macros
  48. __HAL_SMARTCARD_ENABLE_IT() and __HAL_SMARTCARD_DISABLE_IT() inside the transmit and receive process.
  49. (#) Three operation modes are available within this driver :
  50. *** Polling mode IO operation ***
  51. =================================
  52. [..]
  53. (+) Send an amount of data in blocking mode using HAL_SMARTCARD_Transmit()
  54. (+) Receive an amount of data in blocking mode using HAL_SMARTCARD_Receive()
  55. *** Interrupt mode IO operation ***
  56. ===================================
  57. [..]
  58. (+) Send an amount of data in non blocking mode using HAL_SMARTCARD_Transmit_IT()
  59. (+) At transmission end of transfer HAL_SMARTCARD_TxCpltCallback is executed and user can
  60. add his own code by customization of function pointer HAL_SMARTCARD_TxCpltCallback
  61. (+) Receive an amount of data in non blocking mode using HAL_SMARTCARD_Receive_IT()
  62. (+) At reception end of transfer HAL_SMARTCARD_RxCpltCallback is executed and user can
  63. add his own code by customization of function pointer HAL_SMARTCARD_RxCpltCallback
  64. (+) In case of transfer Error, HAL_SMARTCARD_ErrorCallback() function is executed and user can
  65. add his own code by customization of function pointer HAL_SMARTCARD_ErrorCallback
  66. *** DMA mode IO operation ***
  67. ==============================
  68. [..]
  69. (+) Send an amount of data in non blocking mode (DMA) using HAL_SMARTCARD_Transmit_DMA()
  70. (+) At transmission end of transfer HAL_SMARTCARD_TxCpltCallback is executed and user can
  71. add his own code by customization of function pointer HAL_SMARTCARD_TxCpltCallback
  72. (+) Receive an amount of data in non blocking mode (DMA) using HAL_SMARTCARD_Receive_DMA()
  73. (+) At reception end of transfer HAL_SMARTCARD_RxCpltCallback is executed and user can
  74. add his own code by customization of function pointer HAL_SMARTCARD_RxCpltCallback
  75. (+) In case of transfer Error, HAL_SMARTCARD_ErrorCallback() function is executed and user can
  76. add his own code by customization of function pointer HAL_SMARTCARD_ErrorCallback
  77. *** SMARTCARD HAL driver macros list ***
  78. ========================================
  79. [..]
  80. Below the list of most used macros in SMARTCARD HAL driver.
  81. (+) __HAL_SMARTCARD_ENABLE: Enable the SMARTCARD peripheral
  82. (+) __HAL_SMARTCARD_DISABLE: Disable the SMARTCARD peripheral
  83. (+) __HAL_SMARTCARD_GET_FLAG : Check whether the specified SMARTCARD flag is set or not
  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 the specified SMARTCARD interrupt has occurred or not
  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) 2016 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 "stm32f1xx_hal.h"
  122. /** @addtogroup STM32F1xx_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. /* Private macros --------------------------------------------------------*/
  133. /* Private variables ---------------------------------------------------------*/
  134. /* Private function prototypes -----------------------------------------------*/
  135. /** @addtogroup SMARTCARD_Private_Functions SMARTCARD Private Functions
  136. * @{
  137. */
  138. static HAL_StatusTypeDef SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc);
  139. static HAL_StatusTypeDef SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard);
  140. static HAL_StatusTypeDef SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc);
  141. static void SMARTCARD_SetConfig (SMARTCARD_HandleTypeDef *hsc);
  142. static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma);
  143. static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
  144. static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma);
  145. static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsc, uint32_t Flag, FlagStatus Status, uint32_t Timeout);
  146. /**
  147. * @}
  148. */
  149. /* Exported functions ---------------------------------------------------------*/
  150. /** @defgroup SMARTCARD_Exported_Functions SMARTCARD Exported Functions
  151. * @{
  152. */
  153. /** @defgroup SMARTCARD_Exported_Functions_Group1 Initialization and de-initialization functions
  154. * @brief Initialization and Configuration functions
  155. *
  156. @verbatim
  157. ==============================================================================
  158. ##### Initialization and Configuration functions #####
  159. ==============================================================================
  160. [..]
  161. This subsection provides a set of functions allowing to initialize the USART
  162. in Smartcard mode.
  163. [..]
  164. The Smartcard interface is designed to support asynchronous protocol Smartcards as
  165. defined in the ISO 7816-3 standard.
  166. [..]
  167. The USART can provide a clock to the smartcard through the SCLK output.
  168. In smartcard mode, SCLK is not associated to the communication but is simply derived
  169. from the internal peripheral input clock through a 5-bit prescaler.
  170. [..]
  171. (+) For the Smartcard mode only these parameters can be configured:
  172. (++) Baud Rate
  173. (++) Word Length => Should be 9 bits (8 bits + parity)
  174. (++) Stop Bit
  175. (++) Parity: => Should be enabled
  176. (++) USART polarity
  177. (++) USART phase
  178. (++) USART LastBit
  179. (++) Receiver/transmitter modes
  180. (++) Prescaler
  181. (++) GuardTime
  182. (++) NACKState: The Smartcard NACK state
  183. (+) Recommended SmartCard interface configuration to get the Answer to Reset from the Card:
  184. (++) Word Length = 9 Bits
  185. (++) 1.5 Stop Bit
  186. (++) Even parity
  187. (++) BaudRate = 12096 baud
  188. (++) Tx and Rx enabled
  189. [..]
  190. Please refer to the ISO 7816-3 specification for more details.
  191. (@) It is also possible to choose 0.5 stop bit for receiving but it is recommended
  192. to use 1.5 stop bits for both transmitting and receiving to avoid switching
  193. between the two configurations.
  194. [..]
  195. The HAL_SMARTCARD_Init() function follows the USART SmartCard configuration
  196. procedure (details for the procedure are available in reference manuals
  197. (RM0008 for STM32F10Xxx MCUs and RM0041 for STM32F100xx MCUs)).
  198. @endverbatim
  199. * @{
  200. */
  201. /*
  202. Additionnal remark on the smartcard frame:
  203. +-------------------------------------------------------------+
  204. | M bit | PCE bit | SMARTCARD frame |
  205. |---------------------|---------------------------------------|
  206. | 1 | 1 | | SB | 8 bit data | PB | STB | |
  207. +-------------------------------------------------------------+
  208. */
  209. /**
  210. * @brief Initializes the SmartCard mode according to the specified
  211. * parameters in the SMARTCARD_HandleTypeDef and create the associated handle.
  212. * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  213. * the configuration information for the specified SMARTCARD module.
  214. * @retval HAL status
  215. */
  216. HAL_StatusTypeDef HAL_SMARTCARD_Init(SMARTCARD_HandleTypeDef *hsc)
  217. {
  218. /* Check the SMARTCARD handle allocation */
  219. if(hsc == NULL)
  220. {
  221. return HAL_ERROR;
  222. }
  223. /* Check Wordlength, Parity and Stop bits parameters */
  224. if ( (!(IS_SMARTCARD_WORD_LENGTH(hsc->Init.WordLength)))
  225. ||(!(IS_SMARTCARD_STOPBITS(hsc->Init.StopBits)))
  226. ||(!(IS_SMARTCARD_PARITY(hsc->Init.Parity))) )
  227. {
  228. return HAL_ERROR;
  229. }
  230. /* Check the parameters */
  231. assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance));
  232. assert_param(IS_SMARTCARD_NACK_STATE(hsc->Init.NACKState));
  233. assert_param(IS_SMARTCARD_PRESCALER(hsc->Init.Prescaler));
  234. if(hsc->State == HAL_SMARTCARD_STATE_RESET)
  235. {
  236. /* Allocate lock resource and initialize it */
  237. hsc->Lock = HAL_UNLOCKED;
  238. /* Init the low level hardware */
  239. HAL_SMARTCARD_MspInit(hsc);
  240. }
  241. hsc->State = HAL_SMARTCARD_STATE_BUSY;
  242. /* Disable the Peripheral */
  243. __HAL_SMARTCARD_DISABLE(hsc);
  244. /* Set the Prescaler */
  245. MODIFY_REG(hsc->Instance->GTPR, USART_GTPR_PSC, hsc->Init.Prescaler);
  246. /* Set the Guard Time */
  247. MODIFY_REG(hsc->Instance->GTPR, USART_GTPR_GT, ((hsc->Init.GuardTime)<<8));
  248. /* Set the Smartcard Communication parameters */
  249. SMARTCARD_SetConfig(hsc);
  250. /* In SmartCard mode, the following bits must be kept cleared:
  251. - LINEN bit in the USART_CR2 register
  252. - HDSEL and IREN bits in the USART_CR3 register.*/
  253. CLEAR_BIT(hsc->Instance->CR2, USART_CR2_LINEN);
  254. CLEAR_BIT(hsc->Instance->CR3, (USART_CR3_IREN | USART_CR3_HDSEL));
  255. /* Enable the Peripharal */
  256. __HAL_SMARTCARD_ENABLE(hsc);
  257. /* Configure the Smartcard NACK state */
  258. MODIFY_REG(hsc->Instance->CR3, USART_CR3_NACK, hsc->Init.NACKState);
  259. /* Enable the SC mode by setting the SCEN bit in the CR3 register */
  260. SET_BIT(hsc->Instance->CR3, USART_CR3_SCEN);
  261. /* Initialize the SMARTCARD state*/
  262. hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  263. hsc->State= HAL_SMARTCARD_STATE_READY;
  264. return HAL_OK;
  265. }
  266. /**
  267. * @brief DeInitializes the SMARTCARD peripheral
  268. * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  269. * the configuration information for the specified SMARTCARD module.
  270. * @retval HAL status
  271. */
  272. HAL_StatusTypeDef HAL_SMARTCARD_DeInit(SMARTCARD_HandleTypeDef *hsc)
  273. {
  274. /* Check the SMARTCARD handle allocation */
  275. if(hsc == NULL)
  276. {
  277. return HAL_ERROR;
  278. }
  279. /* Check the parameters */
  280. assert_param(IS_SMARTCARD_INSTANCE(hsc->Instance));
  281. hsc->State = HAL_SMARTCARD_STATE_BUSY;
  282. /* Disable the Peripheral */
  283. __HAL_SMARTCARD_DISABLE(hsc);
  284. hsc->Instance->CR1 = 0x0;
  285. hsc->Instance->CR2 = 0x0;
  286. hsc->Instance->CR3 = 0x0;
  287. hsc->Instance->BRR = 0x0;
  288. hsc->Instance->GTPR = 0x0;
  289. /* DeInit the low level hardware */
  290. HAL_SMARTCARD_MspDeInit(hsc);
  291. hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  292. hsc->State = HAL_SMARTCARD_STATE_RESET;
  293. /* Release Lock */
  294. __HAL_UNLOCK(hsc);
  295. return HAL_OK;
  296. }
  297. /**
  298. * @brief SMARTCARD MSP Init.
  299. * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  300. * the configuration information for the specified SMARTCARD module.
  301. * @retval None
  302. */
  303. __weak void HAL_SMARTCARD_MspInit(SMARTCARD_HandleTypeDef *hsc)
  304. {
  305. /* Prevent unused argument(s) compilation warning */
  306. UNUSED(hsc);
  307. /* NOTE: This function should not be modified, when the callback is needed,
  308. the HAL_SMARTCARD_MspInit can be implemented in the user file
  309. */
  310. }
  311. /**
  312. * @brief SMARTCARD MSP DeInit.
  313. * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  314. * the configuration information for the specified SMARTCARD module.
  315. * @retval None
  316. */
  317. __weak void HAL_SMARTCARD_MspDeInit(SMARTCARD_HandleTypeDef *hsc)
  318. {
  319. /* Prevent unused argument(s) compilation warning */
  320. UNUSED(hsc);
  321. /* NOTE: This function should not be modified, when the callback is needed,
  322. the HAL_SMARTCARD_MspDeInit can be implemented in the user file
  323. */
  324. }
  325. /**
  326. * @}
  327. */
  328. /** @defgroup SMARTCARD_Exported_Functions_Group2 IO operation functions
  329. * @brief SMARTCARD Transmit and Receive functions
  330. *
  331. @verbatim
  332. ==============================================================================
  333. ##### IO operation functions #####
  334. ==============================================================================
  335. [..]
  336. This subsection provides a set of functions allowing to manage the SMARTCARD data transfers.
  337. [..]
  338. (#) Smartcard is a single wire half duplex communication protocol.
  339. The Smartcard interface is designed to support asynchronous protocol Smartcards as
  340. defined in the ISO 7816-3 standard.
  341. (#) The USART should be configured as:
  342. (++) 8 bits plus parity: where M=1 and PCE=1 in the USART_CR1 register
  343. (++) 1.5 stop bits when transmitting and receiving: where STOP=11 in the USART_CR2 register.
  344. (#) There are two modes of transfer:
  345. (++) Blocking mode: The communication is performed in polling mode.
  346. The HAL status of all data processing is returned by the same function
  347. after finishing transfer.
  348. (++) No-Blocking mode: The communication is performed using Interrupts
  349. or DMA, the relevant API's return the HAL status.
  350. The end of the data processing will be indicated through the
  351. dedicated SMARTCARD IRQ when using Interrupt mode or the DMA IRQ when
  352. using DMA mode.
  353. The HAL_SMARTCARD_TxCpltCallback(), HAL_SMARTCARD_RxCpltCallback() user callbacks
  354. will be executed respectively at the end of the Transmit or Receive process
  355. The HAL_SMARTCARD_ErrorCallback() user callback will be executed when a communication
  356. error is detected.
  357. (#) Blocking mode APIs are :
  358. (++) HAL_SMARTCARD_Transmit()
  359. (++) HAL_SMARTCARD_Receive()
  360. (#) Non Blocking mode APIs with Interrupt are :
  361. (++) HAL_SMARTCARD_Transmit_IT()
  362. (++) HAL_SMARTCARD_Receive_IT()
  363. (++) HAL_SMARTCARD_IRQHandler()
  364. (#) Non Blocking mode functions with DMA are :
  365. (++) HAL_SMARTCARD_Transmit_DMA()
  366. (++) HAL_SMARTCARD_Receive_DMA()
  367. (#) A set of Transfer Complete Callbacks are provided in non Blocking mode:
  368. (++) HAL_SMARTCARD_TxCpltCallback()
  369. (++) HAL_SMARTCARD_RxCpltCallback()
  370. (++) HAL_SMARTCARD_ErrorCallback()
  371. @endverbatim
  372. * @{
  373. */
  374. /**
  375. * @brief Sends an amount of data in blocking mode.
  376. * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  377. * the configuration information for the specified SMARTCARD module.
  378. * @param pData: Pointer to data buffer
  379. * @param Size: Amount of data to be sent
  380. * @param Timeout: Specify timeout value
  381. * @retval HAL status
  382. */
  383. HAL_StatusTypeDef HAL_SMARTCARD_Transmit(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  384. {
  385. uint32_t tmp_state = 0;
  386. tmp_state = hsc->State;
  387. if((tmp_state == HAL_SMARTCARD_STATE_READY) || (tmp_state == HAL_SMARTCARD_STATE_BUSY_RX))
  388. {
  389. if((pData == NULL) || (Size == 0))
  390. {
  391. return HAL_ERROR;
  392. }
  393. /* Process Locked */
  394. __HAL_LOCK(hsc);
  395. hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  396. /* Check if a non-blocking receive process is ongoing or not */
  397. if(hsc->State == HAL_SMARTCARD_STATE_BUSY_RX)
  398. {
  399. hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
  400. }
  401. else
  402. {
  403. hsc->State = HAL_SMARTCARD_STATE_BUSY_TX;
  404. }
  405. hsc->TxXferSize = Size;
  406. hsc->TxXferCount = Size;
  407. while(hsc->TxXferCount > 0)
  408. {
  409. if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_TXE, RESET, Timeout) != HAL_OK)
  410. {
  411. return HAL_TIMEOUT;
  412. }
  413. WRITE_REG(hsc->Instance->DR, (*pData++ & (uint8_t)0xFF));
  414. hsc->TxXferCount--;
  415. }
  416. if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_TC, RESET, Timeout) != HAL_OK)
  417. {
  418. return HAL_TIMEOUT;
  419. }
  420. /* Check if a non-blocking receive process is ongoing or not */
  421. if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX_RX)
  422. {
  423. hsc->State = HAL_SMARTCARD_STATE_BUSY_RX;
  424. }
  425. else
  426. {
  427. hsc->State = HAL_SMARTCARD_STATE_READY;
  428. }
  429. /* Process Unlocked */
  430. __HAL_UNLOCK(hsc);
  431. return HAL_OK;
  432. }
  433. else
  434. {
  435. return HAL_BUSY;
  436. }
  437. }
  438. /**
  439. * @brief Receive an amount of data in blocking mode.
  440. * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  441. * the configuration information for the specified SMARTCARD module.
  442. * @param pData: Pointer to data buffer
  443. * @param Size: Amount of data to be received
  444. * @param Timeout: Specify timeout value
  445. * @retval HAL status
  446. */
  447. HAL_StatusTypeDef HAL_SMARTCARD_Receive(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  448. {
  449. uint32_t tmp_state = 0;
  450. tmp_state = hsc->State;
  451. if((tmp_state == HAL_SMARTCARD_STATE_READY) || (tmp_state == HAL_SMARTCARD_STATE_BUSY_TX))
  452. {
  453. if((pData == NULL) || (Size == 0))
  454. {
  455. return HAL_ERROR;
  456. }
  457. /* Process Locked */
  458. __HAL_LOCK(hsc);
  459. hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  460. /* Check if a non-blocking transmit process is ongoing or not */
  461. if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX)
  462. {
  463. hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
  464. }
  465. else
  466. {
  467. hsc->State = HAL_SMARTCARD_STATE_BUSY_RX;
  468. }
  469. hsc->RxXferSize = Size;
  470. hsc->RxXferCount = Size;
  471. /* Check the remain data to be received */
  472. while(hsc->RxXferCount > 0)
  473. {
  474. if(SMARTCARD_WaitOnFlagUntilTimeout(hsc, SMARTCARD_FLAG_RXNE, RESET, Timeout) != HAL_OK)
  475. {
  476. return HAL_TIMEOUT;
  477. }
  478. *pData++ = (uint8_t)(hsc->Instance->DR & (uint8_t)0x00FF);
  479. hsc->RxXferCount--;
  480. }
  481. /* Check if a non-blocking transmit process is ongoing or not */
  482. if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX_RX)
  483. {
  484. hsc->State = HAL_SMARTCARD_STATE_BUSY_TX;
  485. }
  486. else
  487. {
  488. hsc->State = HAL_SMARTCARD_STATE_READY;
  489. }
  490. /* Process Unlocked */
  491. __HAL_UNLOCK(hsc);
  492. return HAL_OK;
  493. }
  494. else
  495. {
  496. return HAL_BUSY;
  497. }
  498. }
  499. /**
  500. * @brief Sends an amount of data in non-blocking mode.
  501. * @param hsc: 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 sent
  505. * @retval HAL status
  506. */
  507. HAL_StatusTypeDef HAL_SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
  508. {
  509. uint32_t tmp_state = 0;
  510. tmp_state = hsc->State;
  511. if((tmp_state == HAL_SMARTCARD_STATE_READY) || (tmp_state == HAL_SMARTCARD_STATE_BUSY_RX))
  512. {
  513. if((pData == NULL) || (Size == 0))
  514. {
  515. return HAL_ERROR;
  516. }
  517. /* Process Locked */
  518. __HAL_LOCK(hsc);
  519. hsc->pTxBuffPtr = pData;
  520. hsc->TxXferSize = Size;
  521. hsc->TxXferCount = Size;
  522. hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  523. /* Check if a non-blocking receive process is ongoing or not */
  524. if(hsc->State == HAL_SMARTCARD_STATE_BUSY_RX)
  525. {
  526. hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
  527. }
  528. else
  529. {
  530. hsc->State = HAL_SMARTCARD_STATE_BUSY_TX;
  531. }
  532. /* Process Unlocked */
  533. __HAL_UNLOCK(hsc);
  534. /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
  535. __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_ERR);
  536. /* Enable the SMARTCARD Transmit data register empty Interrupt */
  537. __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_TXE);
  538. return HAL_OK;
  539. }
  540. else
  541. {
  542. return HAL_BUSY;
  543. }
  544. }
  545. /**
  546. * @brief Receives an amount of data in non-blocking mode.
  547. * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  548. * the configuration information for the specified SMARTCARD module.
  549. * @param pData: Pointer to data buffer
  550. * @param Size: Amount of data to be received
  551. * @retval HAL status
  552. */
  553. HAL_StatusTypeDef HAL_SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
  554. {
  555. uint32_t tmp_state = 0;
  556. tmp_state = hsc->State;
  557. if((tmp_state == HAL_SMARTCARD_STATE_READY) || (tmp_state == HAL_SMARTCARD_STATE_BUSY_TX))
  558. {
  559. if((pData == NULL) || (Size == 0))
  560. {
  561. return HAL_ERROR;
  562. }
  563. /* Process Locked */
  564. __HAL_LOCK(hsc);
  565. hsc->pRxBuffPtr = pData;
  566. hsc->RxXferSize = Size;
  567. hsc->RxXferCount = Size;
  568. hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  569. /* Check if a non-blocking transmit process is ongoing or not */
  570. if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX)
  571. {
  572. hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
  573. }
  574. else
  575. {
  576. hsc->State = HAL_SMARTCARD_STATE_BUSY_RX;
  577. }
  578. /* Process Unlocked */
  579. __HAL_UNLOCK(hsc);
  580. /* Enable the SMARTCARD Data Register not empty Interrupt */
  581. __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_RXNE);
  582. /* Enable the SMARTCARD Parity Error Interrupt */
  583. __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_PE);
  584. /* Enable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
  585. __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_ERR);
  586. return HAL_OK;
  587. }
  588. else
  589. {
  590. return HAL_BUSY;
  591. }
  592. }
  593. /**
  594. * @brief Sends an amount of data in non-blocking mode.
  595. * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  596. * the configuration information for the specified SMARTCARD module.
  597. * @param pData: Pointer to data buffer
  598. * @param Size: Amount of data to be sent
  599. * @retval HAL status
  600. */
  601. HAL_StatusTypeDef HAL_SMARTCARD_Transmit_DMA(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
  602. {
  603. uint32_t *tmp = 0;
  604. uint32_t tmp_state = 0;
  605. tmp_state = hsc->State;
  606. if((tmp_state == HAL_SMARTCARD_STATE_READY) || (tmp_state == HAL_SMARTCARD_STATE_BUSY_RX))
  607. {
  608. if((pData == NULL) || (Size == 0))
  609. {
  610. return HAL_ERROR;
  611. }
  612. /* Process Locked */
  613. __HAL_LOCK(hsc);
  614. hsc->pTxBuffPtr = pData;
  615. hsc->TxXferSize = Size;
  616. hsc->TxXferCount = Size;
  617. hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  618. /* Check if a non-blocking receive process is ongoing or not */
  619. if(hsc->State == HAL_SMARTCARD_STATE_BUSY_RX)
  620. {
  621. hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
  622. }
  623. else
  624. {
  625. hsc->State = HAL_SMARTCARD_STATE_BUSY_TX;
  626. }
  627. /* Set the SMARTCARD DMA transfer complete callback */
  628. hsc->hdmatx->XferCpltCallback = SMARTCARD_DMATransmitCplt;
  629. /* Set the DMA error callback */
  630. hsc->hdmatx->XferErrorCallback = SMARTCARD_DMAError;
  631. /* Enable the SMARTCARD transmit DMA channel */
  632. tmp = (uint32_t*)&pData;
  633. HAL_DMA_Start_IT(hsc->hdmatx, *(uint32_t*)tmp, (uint32_t)&hsc->Instance->DR, Size);
  634. /* Clear the TC flag in the SR register by writing 0 to it */
  635. __HAL_SMARTCARD_CLEAR_FLAG(hsc, SMARTCARD_FLAG_TC);
  636. /* Enable the DMA transfer for transmit request by setting the DMAT bit
  637. in the SMARTCARD CR3 register */
  638. SET_BIT(hsc->Instance->CR3,USART_CR3_DMAT);
  639. /* Process Unlocked */
  640. __HAL_UNLOCK(hsc);
  641. return HAL_OK;
  642. }
  643. else
  644. {
  645. return HAL_BUSY;
  646. }
  647. }
  648. /**
  649. * @brief Receive an amount of data in non-blocking mode.
  650. * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  651. * the configuration information for the specified SMARTCARD module.
  652. * @param pData: Pointer to data buffer
  653. * @param Size: Amount of data to be received
  654. * @note When the SMARTCARD parity is enabled (PCE = 1) the data received contain the parity bit.
  655. * @retval HAL status
  656. */
  657. HAL_StatusTypeDef HAL_SMARTCARD_Receive_DMA(SMARTCARD_HandleTypeDef *hsc, uint8_t *pData, uint16_t Size)
  658. {
  659. uint32_t *tmp = 0;
  660. uint32_t tmp_state = 0;
  661. tmp_state = hsc->State;
  662. if((tmp_state == HAL_SMARTCARD_STATE_READY) || (tmp_state == HAL_SMARTCARD_STATE_BUSY_TX))
  663. {
  664. if((pData == NULL) || (Size == 0))
  665. {
  666. return HAL_ERROR;
  667. }
  668. /* Process Locked */
  669. __HAL_LOCK(hsc);
  670. hsc->pRxBuffPtr = pData;
  671. hsc->RxXferSize = Size;
  672. hsc->ErrorCode = HAL_SMARTCARD_ERROR_NONE;
  673. /* Check if a non-blocking transmit process is ongoing or not */
  674. if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX)
  675. {
  676. hsc->State = HAL_SMARTCARD_STATE_BUSY_TX_RX;
  677. }
  678. else
  679. {
  680. hsc->State = HAL_SMARTCARD_STATE_BUSY_RX;
  681. }
  682. /* Set the SMARTCARD DMA transfer complete callback */
  683. hsc->hdmarx->XferCpltCallback = SMARTCARD_DMAReceiveCplt;
  684. /* Set the DMA error callback */
  685. hsc->hdmarx->XferErrorCallback = SMARTCARD_DMAError;
  686. /* Enable the DMA channel */
  687. tmp = (uint32_t*)&pData;
  688. HAL_DMA_Start_IT(hsc->hdmarx, (uint32_t)&hsc->Instance->DR, *(uint32_t*)tmp, Size);
  689. /* Enable the DMA transfer for the receiver request by setting the DMAR bit
  690. in the SMARTCARD CR3 register */
  691. SET_BIT(hsc->Instance->CR3,USART_CR3_DMAR);
  692. /* Process Unlocked */
  693. __HAL_UNLOCK(hsc);
  694. return HAL_OK;
  695. }
  696. else
  697. {
  698. return HAL_BUSY;
  699. }
  700. }
  701. /**
  702. * @brief This function handles SMARTCARD interrupt request.
  703. * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  704. * the configuration information for the specified SMARTCARD module.
  705. * @retval None
  706. */
  707. void HAL_SMARTCARD_IRQHandler(SMARTCARD_HandleTypeDef *hsc)
  708. {
  709. uint32_t tmp_flag = 0, tmp_it_source = 0;
  710. tmp_flag = __HAL_SMARTCARD_GET_FLAG(hsc, SMARTCARD_FLAG_PE);
  711. tmp_it_source = __HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_PE);
  712. /* SMARTCARD parity error interrupt occurred -----------------------------------*/
  713. if((tmp_flag != RESET) && (tmp_it_source != RESET))
  714. {
  715. hsc->ErrorCode |= HAL_SMARTCARD_ERROR_PE;
  716. }
  717. tmp_flag = __HAL_SMARTCARD_GET_FLAG(hsc, SMARTCARD_FLAG_FE);
  718. tmp_it_source = __HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_ERR);
  719. /* SMARTCARD frame error interrupt occurred ------------------------------------*/
  720. if((tmp_flag != RESET) && (tmp_it_source != RESET))
  721. {
  722. hsc->ErrorCode |= HAL_SMARTCARD_ERROR_FE;
  723. }
  724. tmp_flag = __HAL_SMARTCARD_GET_FLAG(hsc, SMARTCARD_FLAG_NE);
  725. /* SMARTCARD noise error interrupt occurred ------------------------------------*/
  726. if((tmp_flag != RESET) && (tmp_it_source != RESET))
  727. {
  728. hsc->ErrorCode |= HAL_SMARTCARD_ERROR_NE;
  729. }
  730. tmp_flag = __HAL_SMARTCARD_GET_FLAG(hsc, SMARTCARD_FLAG_ORE);
  731. /* SMARTCARD Over-Run interrupt occurred ---------------------------------------*/
  732. if((tmp_flag != RESET) && (tmp_it_source != RESET))
  733. {
  734. hsc->ErrorCode |= HAL_SMARTCARD_ERROR_ORE;
  735. }
  736. tmp_flag = __HAL_SMARTCARD_GET_FLAG(hsc, SMARTCARD_FLAG_RXNE);
  737. tmp_it_source = __HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_RXNE);
  738. /* SMARTCARD in mode Receiver --------------------------------------------------*/
  739. if((tmp_flag != RESET) && (tmp_it_source != RESET))
  740. {
  741. SMARTCARD_Receive_IT(hsc);
  742. }
  743. tmp_flag = __HAL_SMARTCARD_GET_FLAG(hsc, SMARTCARD_FLAG_TXE);
  744. tmp_it_source = __HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_TXE);
  745. /* SMARTCARD in mode Transmitter -----------------------------------------------*/
  746. if((tmp_flag != RESET) && (tmp_it_source != RESET))
  747. {
  748. SMARTCARD_Transmit_IT(hsc);
  749. }
  750. tmp_flag = __HAL_SMARTCARD_GET_FLAG(hsc, SMARTCARD_FLAG_TC);
  751. tmp_it_source = __HAL_SMARTCARD_GET_IT_SOURCE(hsc, SMARTCARD_IT_TC);
  752. /* SMARTCARD in mode Transmitter (transmission end) ------------------------*/
  753. if((tmp_flag != RESET) && (tmp_it_source != RESET))
  754. {
  755. SMARTCARD_EndTransmit_IT(hsc);
  756. }
  757. /* Call the Error call Back in case of Errors */
  758. if(hsc->ErrorCode != HAL_SMARTCARD_ERROR_NONE)
  759. {
  760. /* Clear all the error flag at once */
  761. __HAL_SMARTCARD_CLEAR_PEFLAG(hsc);
  762. /* Set the SMARTCARD state ready to be able to start again the process */
  763. hsc->State= HAL_SMARTCARD_STATE_READY;
  764. HAL_SMARTCARD_ErrorCallback(hsc);
  765. }
  766. }
  767. /**
  768. * @brief Tx Transfer completed callback.
  769. * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  770. * the configuration information for the specified SMARTCARD module.
  771. * @retval None
  772. */
  773. __weak void HAL_SMARTCARD_TxCpltCallback(SMARTCARD_HandleTypeDef *hsc)
  774. {
  775. /* Prevent unused argument(s) compilation warning */
  776. UNUSED(hsc);
  777. /* NOTE: This function should not be modified, when the callback is needed,
  778. the HAL_SMARTCARD_TxCpltCallback can be implemented in the user file
  779. */
  780. }
  781. /**
  782. * @brief Rx Transfer completed callback.
  783. * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  784. * the configuration information for the specified SMARTCARD module.
  785. * @retval None
  786. */
  787. __weak void HAL_SMARTCARD_RxCpltCallback(SMARTCARD_HandleTypeDef *hsc)
  788. {
  789. /* Prevent unused argument(s) compilation warning */
  790. UNUSED(hsc);
  791. /* NOTE: This function should not be modified, when the callback is needed,
  792. the HAL_SMARTCARD_RxCpltCallback can be implemented in the user file
  793. */
  794. }
  795. /**
  796. * @brief SMARTCARD error callback.
  797. * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  798. * the configuration information for the specified SMARTCARD module.
  799. * @retval None
  800. */
  801. __weak void HAL_SMARTCARD_ErrorCallback(SMARTCARD_HandleTypeDef *hsc)
  802. {
  803. /* Prevent unused argument(s) compilation warning */
  804. UNUSED(hsc);
  805. /* NOTE: This function should not be modified, when the callback is needed,
  806. the HAL_SMARTCARD_ErrorCallback can be implemented in the user file
  807. */
  808. }
  809. /**
  810. * @}
  811. */
  812. /** @defgroup SMARTCARD_Exported_Functions_Group3 Peripheral State and Errors functions
  813. * @brief SMARTCARD State and Errors functions
  814. *
  815. @verbatim
  816. ==============================================================================
  817. ##### Peripheral State and Errors functions #####
  818. ==============================================================================
  819. [..]
  820. This subsection provides a set of functions allowing to return the State of SmartCard
  821. communication process and also return Peripheral Errors occurred during communication process
  822. (+) HAL_SMARTCARD_GetState() API can be helpful to check in run-time the state
  823. of the SMARTCARD peripheral.
  824. (+) HAL_SMARTCARD_GetError() check in run-time errors that could be occurred during
  825. communication.
  826. @endverbatim
  827. * @{
  828. */
  829. /**
  830. * @brief Returns the SMARTCARD state.
  831. * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  832. * the configuration information for the specified SMARTCARD module.
  833. * @retval HAL state
  834. */
  835. HAL_SMARTCARD_StateTypeDef HAL_SMARTCARD_GetState(SMARTCARD_HandleTypeDef *hsc)
  836. {
  837. return hsc->State;
  838. }
  839. /**
  840. * @brief Return the SMARTCARD error code
  841. * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  842. * the configuration information for the specified SMARTCARD module.
  843. * @retval SMARTCARD Error Code
  844. */
  845. uint32_t HAL_SMARTCARD_GetError(SMARTCARD_HandleTypeDef *hsc)
  846. {
  847. return hsc->ErrorCode;
  848. }
  849. /**
  850. * @}
  851. */
  852. /**
  853. * @}
  854. */
  855. /** @defgroup SMARTCARD_Private_Functions SMARTCARD Private Functions
  856. * @brief SMARTCARD Private functions
  857. * @{
  858. */
  859. /**
  860. * @brief DMA SMARTCARD transmit process complete callback.
  861. * @param hdma: Pointer to a DMA_HandleTypeDef structure that contains
  862. * the configuration information for the specified DMA module.
  863. * @retval None
  864. */
  865. static void SMARTCARD_DMATransmitCplt(DMA_HandleTypeDef *hdma)
  866. {
  867. SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  868. hsc->TxXferCount = 0;
  869. /* Disable the DMA transfer for transmit request by setting the DMAT bit
  870. in the SMARTCARD CR3 register */
  871. CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAT);
  872. /* Enable the SMARTCARD Transmit Complete Interrupt */
  873. __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_TC);
  874. }
  875. /**
  876. * @brief DMA SMARTCARD receive process complete callback.
  877. * @param hdma: Pointer to a DMA_HandleTypeDef structure that contains
  878. * the configuration information for the specified DMA module.
  879. * @retval None
  880. */
  881. static void SMARTCARD_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
  882. {
  883. SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  884. hsc->RxXferCount = 0;
  885. /* Disable the DMA transfer for the receiver request by setting the DMAR bit
  886. in the USART CR3 register */
  887. CLEAR_BIT(hsc->Instance->CR3, USART_CR3_DMAR);
  888. /* Check if a non-blocking transmit process is ongoing or not */
  889. if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX_RX)
  890. {
  891. hsc->State = HAL_SMARTCARD_STATE_BUSY_TX;
  892. }
  893. else
  894. {
  895. hsc->State = HAL_SMARTCARD_STATE_READY;
  896. }
  897. HAL_SMARTCARD_RxCpltCallback(hsc);
  898. }
  899. /**
  900. * @brief DMA SMARTCARD communication error callback.
  901. * @param hdma: Pointer to a DMA_HandleTypeDef structure that contains
  902. * the configuration information for the specified DMA module.
  903. * @retval None
  904. */
  905. static void SMARTCARD_DMAError(DMA_HandleTypeDef *hdma)
  906. {
  907. SMARTCARD_HandleTypeDef* hsc = ( SMARTCARD_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  908. hsc->RxXferCount = 0;
  909. hsc->TxXferCount = 0;
  910. hsc->ErrorCode = HAL_SMARTCARD_ERROR_DMA;
  911. hsc->State= HAL_SMARTCARD_STATE_READY;
  912. HAL_SMARTCARD_ErrorCallback(hsc);
  913. }
  914. /**
  915. * @brief This function handles SMARTCARD Communication Timeout.
  916. * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  917. * the configuration information for the specified SMARTCARD module.
  918. * @param Flag: specifies the SMARTCARD flag to check.
  919. * @param Status: The new Flag status (SET or RESET).
  920. * @param Timeout: Timeout duration
  921. * @retval HAL status
  922. */
  923. static HAL_StatusTypeDef SMARTCARD_WaitOnFlagUntilTimeout(SMARTCARD_HandleTypeDef *hsc, uint32_t Flag, FlagStatus Status, uint32_t Timeout)
  924. {
  925. uint32_t tickstart = 0;
  926. /* Get tick */
  927. tickstart = HAL_GetTick();
  928. /* Wait until flag is set */
  929. if(Status == RESET)
  930. {
  931. while(__HAL_SMARTCARD_GET_FLAG(hsc, Flag) == RESET)
  932. {
  933. /* Check for the Timeout */
  934. if(Timeout != HAL_MAX_DELAY)
  935. {
  936. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  937. {
  938. /* Disable TXE and RXNE interrupts for the interrupt process */
  939. __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_TXE);
  940. __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_RXNE);
  941. hsc->State= HAL_SMARTCARD_STATE_READY;
  942. /* Process Unlocked */
  943. __HAL_UNLOCK(hsc);
  944. return HAL_TIMEOUT;
  945. }
  946. }
  947. }
  948. }
  949. else
  950. {
  951. while(__HAL_SMARTCARD_GET_FLAG(hsc, Flag) != RESET)
  952. {
  953. /* Check for the Timeout */
  954. if(Timeout != HAL_MAX_DELAY)
  955. {
  956. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  957. {
  958. /* Disable TXE and RXNE interrupts for the interrupt process */
  959. __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_TXE);
  960. __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_RXNE);
  961. hsc->State= HAL_SMARTCARD_STATE_READY;
  962. /* Process Unlocked */
  963. __HAL_UNLOCK(hsc);
  964. return HAL_TIMEOUT;
  965. }
  966. }
  967. }
  968. }
  969. return HAL_OK;
  970. }
  971. /**
  972. * @brief Send an amount of data in non-blocking mode.
  973. * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  974. * the configuration information for the specified SMARTCARD module.
  975. * Function called under interruption only, once
  976. * interruptions have been enabled by HAL_SMARTCARD_Transmit_IT()
  977. * @retval HAL status
  978. */
  979. static HAL_StatusTypeDef SMARTCARD_Transmit_IT(SMARTCARD_HandleTypeDef *hsc)
  980. {
  981. uint32_t tmp_state = 0;
  982. tmp_state = hsc->State;
  983. if((tmp_state == HAL_SMARTCARD_STATE_BUSY_TX) || (tmp_state == HAL_SMARTCARD_STATE_BUSY_TX_RX))
  984. {
  985. WRITE_REG(hsc->Instance->DR, (*hsc->pTxBuffPtr++ & (uint8_t)0xFF));
  986. if(--hsc->TxXferCount == 0)
  987. {
  988. /* Disable the SMARTCARD Transmit Data Register Empty Interrupt */
  989. __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_TXE);
  990. /* Enable the SMARTCARD Transmit Complete Interrupt */
  991. __HAL_SMARTCARD_ENABLE_IT(hsc, SMARTCARD_IT_TC);
  992. }
  993. return HAL_OK;
  994. }
  995. else
  996. {
  997. return HAL_BUSY;
  998. }
  999. }
  1000. /**
  1001. * @brief Wraps up transmission in non blocking mode.
  1002. * @param hsmartcard: pointer to a SMARTCARD_HandleTypeDef structure that contains
  1003. * the configuration information for the specified SMARTCARD module.
  1004. * @retval HAL status
  1005. */
  1006. static HAL_StatusTypeDef SMARTCARD_EndTransmit_IT(SMARTCARD_HandleTypeDef *hsmartcard)
  1007. {
  1008. /* Disable the SMARTCARD Transmit Complete Interrupt */
  1009. __HAL_SMARTCARD_DISABLE_IT(hsmartcard, SMARTCARD_IT_TC);
  1010. /* Check if a receive process is ongoing or not */
  1011. if(hsmartcard->State == HAL_SMARTCARD_STATE_BUSY_TX_RX)
  1012. {
  1013. hsmartcard->State = HAL_SMARTCARD_STATE_BUSY_RX;
  1014. }
  1015. else
  1016. {
  1017. /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
  1018. __HAL_SMARTCARD_DISABLE_IT(hsmartcard, SMARTCARD_IT_ERR);
  1019. hsmartcard->State = HAL_SMARTCARD_STATE_READY;
  1020. }
  1021. HAL_SMARTCARD_TxCpltCallback(hsmartcard);
  1022. return HAL_OK;
  1023. }
  1024. /**
  1025. * @brief Receive an amount of data in non-blocking mode.
  1026. * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1027. * the configuration information for the specified SMARTCARD module.
  1028. * @retval HAL status
  1029. */
  1030. static HAL_StatusTypeDef SMARTCARD_Receive_IT(SMARTCARD_HandleTypeDef *hsc)
  1031. {
  1032. uint32_t tmp_state = 0;
  1033. tmp_state = hsc->State;
  1034. if((tmp_state == HAL_SMARTCARD_STATE_BUSY_RX) || (tmp_state == HAL_SMARTCARD_STATE_BUSY_TX_RX))
  1035. {
  1036. *hsc->pRxBuffPtr++ = (uint8_t)(hsc->Instance->DR & (uint8_t)0xFF);
  1037. if(--hsc->RxXferCount == 0)
  1038. {
  1039. __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_RXNE);
  1040. /* Disable the SMARTCARD Parity Error Interrupt */
  1041. __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_PE);
  1042. /* Disable the SMARTCARD Error Interrupt: (Frame error, noise error, overrun error) */
  1043. __HAL_SMARTCARD_DISABLE_IT(hsc, SMARTCARD_IT_ERR);
  1044. /* Check if a non-blocking transmit process is ongoing or not */
  1045. if(hsc->State == HAL_SMARTCARD_STATE_BUSY_TX_RX)
  1046. {
  1047. hsc->State = HAL_SMARTCARD_STATE_BUSY_TX;
  1048. }
  1049. else
  1050. {
  1051. hsc->State = HAL_SMARTCARD_STATE_READY;
  1052. }
  1053. HAL_SMARTCARD_RxCpltCallback(hsc);
  1054. return HAL_OK;
  1055. }
  1056. return HAL_OK;
  1057. }
  1058. else
  1059. {
  1060. return HAL_BUSY;
  1061. }
  1062. }
  1063. /**
  1064. * @brief Configures the SMARTCARD peripheral.
  1065. * @param hsc: Pointer to a SMARTCARD_HandleTypeDef structure that contains
  1066. * the configuration information for the specified SMARTCARD module.
  1067. * @retval None
  1068. */
  1069. static void SMARTCARD_SetConfig(SMARTCARD_HandleTypeDef *hsc)
  1070. {
  1071. /* Check the parameters */
  1072. assert_param(IS_SMARTCARD_POLARITY(hsc->Init.CLKPolarity));
  1073. assert_param(IS_SMARTCARD_PHASE(hsc->Init.CLKPhase));
  1074. assert_param(IS_SMARTCARD_LASTBIT(hsc->Init.CLKLastBit));
  1075. assert_param(IS_SMARTCARD_BAUDRATE(hsc->Init.BaudRate));
  1076. assert_param(IS_SMARTCARD_WORD_LENGTH(hsc->Init.WordLength));
  1077. assert_param(IS_SMARTCARD_STOPBITS(hsc->Init.StopBits));
  1078. assert_param(IS_SMARTCARD_PARITY(hsc->Init.Parity));
  1079. assert_param(IS_SMARTCARD_MODE(hsc->Init.Mode));
  1080. assert_param(IS_SMARTCARD_NACK_STATE(hsc->Init.NACKState));
  1081. /* The LBCL, CPOL and CPHA bits have to be selected when both the transmitter and the
  1082. receiver are disabled (TE=RE=0) to ensure that the clock pulses function correctly. */
  1083. CLEAR_BIT(hsc->Instance->CR1, (uint32_t)(USART_CR1_TE | USART_CR1_RE));
  1084. /*------ SMARTCARD-associated USART registers setting : CR2 Configuration ------*/
  1085. /* Clear CLKEN, CPOL, CPHA and LBCL bits */
  1086. /* Configure the SMARTCARD Clock, CPOL, CPHA and LastBit -----------------------*/
  1087. /* Set CPOL bit according to hsc->Init.CLKPolarity value */
  1088. /* Set CPHA bit according to hsc->Init.CLKPhase value */
  1089. /* Set LBCL bit according to hsc->Init.CLKLastBit value */
  1090. MODIFY_REG(hsc->Instance->CR2,
  1091. ((uint32_t)(USART_CR2_CPHA | USART_CR2_CPOL | USART_CR2_CLKEN | USART_CR2_LBCL)),
  1092. ((uint32_t)(USART_CR2_CLKEN | hsc->Init.CLKPolarity | hsc->Init.CLKPhase| hsc->Init.CLKLastBit)) );
  1093. /* Set Stop Bits: Set STOP[13:12] bits according to hsc->Init.StopBits value */
  1094. MODIFY_REG(hsc->Instance->CR2, USART_CR2_STOP,(uint32_t)(hsc->Init.StopBits));
  1095. /*------ SMARTCARD-associated USART registers setting : CR1 Configuration ------*/
  1096. /* Clear M, PCE, PS, TE and RE bits */
  1097. /* Configure the SMARTCARD Word Length, Parity and mode:
  1098. Set the M according to hsc->Init.WordLength value (forced to 1 as 9B data frame should be selected)
  1099. Set PCE and PS bits according to hsc->Init.Parity value (PCE bit forced to 1 as parity control should always be enabled)
  1100. Set TE and RE bits according to hsc->Init.Mode value */
  1101. MODIFY_REG(hsc->Instance->CR1,
  1102. ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | USART_CR1_TE | USART_CR1_RE)),
  1103. ((uint32_t)(USART_CR1_M | USART_CR1_PCE | hsc->Init.Parity | hsc->Init.Mode)) );
  1104. /*------ SMARTCARD-associated USART registers setting : CR3 Configuration ------*/
  1105. /* Clear CTSE and RTSE bits */
  1106. CLEAR_BIT(hsc->Instance->CR3, (USART_CR3_RTSE | USART_CR3_CTSE));
  1107. /*------ SMARTCARD-associated USART registers setting : BRR Configuration ------*/
  1108. if(hsc->Instance == USART1)
  1109. {
  1110. hsc->Instance->BRR = SMARTCARD_BRR(HAL_RCC_GetPCLK2Freq(), hsc->Init.BaudRate);
  1111. }
  1112. else
  1113. {
  1114. hsc->Instance->BRR = SMARTCARD_BRR(HAL_RCC_GetPCLK1Freq(), hsc->Init.BaudRate);
  1115. }
  1116. }
  1117. /**
  1118. * @}
  1119. */
  1120. #endif /* HAL_SMARTCARD_MODULE_ENABLED */
  1121. /**
  1122. * @}
  1123. */
  1124. /**
  1125. * @}
  1126. */
  1127. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/