stm32f7xx_hal_usart.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796
  1. /**
  2. ******************************************************************************
  3. * @file stm32f7xx_hal_usart.c
  4. * @author MCD Application Team
  5. * @version V1.0.1
  6. * @date 25-June-2015
  7. * @brief USART HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of the Universal Synchronous/Asynchronous Receiver Transmitter
  10. * Peripheral (USART).
  11. * + Initialization and de-initialization functions
  12. * + IO operation functions
  13. * + Peripheral Control functions
  14. *
  15. @verbatim
  16. ===============================================================================
  17. ##### How to use this driver #####
  18. ===============================================================================
  19. [..]
  20. The USART HAL driver can be used as follows:
  21. (#) Declare a USART_HandleTypeDef handle structure.
  22. (#) Initialize the USART low level resources by implement the HAL_USART_MspInit ()API:
  23. (##) Enable the USARTx interface clock.
  24. (##) USART pins configuration:
  25. (+++) Enable the clock for the USART GPIOs.
  26. (+++) Configure these USART pins as alternate function pull-up.
  27. (##) NVIC configuration if you need to use interrupt process (HAL_USART_Transmit_IT(),
  28. HAL_USART_Receive_IT() and HAL_USART_TransmitReceive_IT() APIs):
  29. (+++) Configure the USARTx interrupt priority.
  30. (+++) Enable the NVIC USART IRQ handle.
  31. (+++) The specific USART interrupts (Transmission complete interrupt,
  32. RXNE interrupt and Error Interrupts) will be managed using the macros
  33. __HAL_USART_ENABLE_IT() and __HAL_USART_DISABLE_IT() inside the transmit and receive process.
  34. (##) DMA Configuration if you need to use DMA process (HAL_USART_Transmit_DMA()
  35. HAL_USART_Receive_IT() and HAL_USART_TransmitReceive_IT() APIs):
  36. (+++) Declare a DMA handle structure for the Tx/Rx stream.
  37. (+++) Enable the DMAx interface clock.
  38. (+++) Configure the declared DMA handle structure with the required Tx/Rx parameters.
  39. (+++) Configure the DMA Tx/Rx Stream.
  40. (+++) Associate the initialized DMA handle to the USART DMA Tx/Rx handle.
  41. (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx/Rx Stream.
  42. (#) Program the Baud Rate, Word Length, Stop Bit, Parity, Hardware
  43. flow control and Mode(Receiver/Transmitter) in the husart Init structure.
  44. (#) Initialize the USART registers by calling the HAL_USART_Init() API:
  45. (++) These API's configures also the low level Hardware (GPIO, CLOCK, CORTEX...etc)
  46. by calling the customed HAL_USART_MspInit(&husart) API.
  47. @endverbatim
  48. ******************************************************************************
  49. * @attention
  50. *
  51. * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
  52. *
  53. * Redistribution and use in source and binary forms, with or without modification,
  54. * are permitted provided that the following conditions are met:
  55. * 1. Redistributions of source code must retain the above copyright notice,
  56. * this list of conditions and the following disclaimer.
  57. * 2. Redistributions in binary form must reproduce the above copyright notice,
  58. * this list of conditions and the following disclaimer in the documentation
  59. * and/or other materials provided with the distribution.
  60. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  61. * may be used to endorse or promote products derived from this software
  62. * without specific prior written permission.
  63. *
  64. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  65. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  66. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  67. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  68. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  69. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  70. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  71. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  72. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  73. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  74. *
  75. ******************************************************************************
  76. */
  77. /* Includes ------------------------------------------------------------------*/
  78. #include "stm32f7xx_hal.h"
  79. /** @addtogroup STM32F7xx_HAL_Driver
  80. * @{
  81. */
  82. /** @defgroup USART USART
  83. * @brief HAL USART Synchronous module driver
  84. * @{
  85. */
  86. #ifdef HAL_USART_MODULE_ENABLED
  87. /* Private typedef -----------------------------------------------------------*/
  88. /* Private define ------------------------------------------------------------*/
  89. /** @addtogroup USART_Private_Constants
  90. * @{
  91. */
  92. #define DUMMY_DATA ((uint16_t) 0xFFFF)
  93. #define TEACK_REACK_TIMEOUT ((uint32_t) 1000)
  94. #define USART_TXDMA_TIMEOUTVALUE 22000
  95. #define USART_TIMEOUT_VALUE 22000
  96. #define USART_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | \
  97. USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8))
  98. #define USART_CR2_FIELDS ((uint32_t)(USART_CR2_CPHA | USART_CR2_CPOL | \
  99. USART_CR2_CLKEN | USART_CR2_LBCL | USART_CR2_STOP))
  100. /**
  101. * @}
  102. */
  103. /* Private macro -------------------------------------------------------------*/
  104. /* Private variables ---------------------------------------------------------*/
  105. /* Private function prototypes -----------------------------------------------*/
  106. /* Private functions ---------------------------------------------------------*/
  107. /** @addtogroup USART_Private_Functions
  108. * @{
  109. */
  110. static void USART_DMATransmitCplt(DMA_HandleTypeDef *hdma);
  111. static void USART_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
  112. static void USART_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
  113. static void USART_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
  114. static void USART_DMAError(DMA_HandleTypeDef *hdma);
  115. static HAL_StatusTypeDef USART_WaitOnFlagUntilTimeout(USART_HandleTypeDef *husart, uint32_t Flag, FlagStatus Status, uint32_t Timeout);
  116. static HAL_StatusTypeDef USART_SetConfig(USART_HandleTypeDef *husart);
  117. static HAL_StatusTypeDef USART_CheckIdleState(USART_HandleTypeDef *husart);
  118. static HAL_StatusTypeDef USART_Transmit_IT(USART_HandleTypeDef *husart);
  119. static HAL_StatusTypeDef USART_EndTransmit_IT(USART_HandleTypeDef *husart);
  120. static HAL_StatusTypeDef USART_Receive_IT(USART_HandleTypeDef *husart);
  121. static HAL_StatusTypeDef USART_TransmitReceive_IT(USART_HandleTypeDef *husart);
  122. /**
  123. * @}
  124. */
  125. /* Exported functions --------------------------------------------------------*/
  126. /** @defgroup USART_Exported_Functions USART Exported Functions
  127. * @{
  128. */
  129. /** @defgroup USART_Exported_Functions_Group1 USART Initialization and de-initialization functions
  130. * @brief Initialization and Configuration functions
  131. *
  132. @verbatim
  133. ===============================================================================
  134. ##### Initialization and Configuration functions #####
  135. ===============================================================================
  136. [..]
  137. This subsection provides a set of functions allowing to initialize the USART
  138. in asynchronous and in synchronous modes.
  139. (+) For the asynchronous mode only these parameters can be configured:
  140. (++) Baud Rate
  141. (++) Word Length
  142. (++) Stop Bit
  143. (++) Parity: If the parity is enabled, then the MSB bit of the data written
  144. in the data register is transmitted but is changed by the parity bit.
  145. Depending on the frame length defined by the M1 and M0 bits (7-bit,
  146. 8-bit or 9-bit), the possible USART frame formats are as listed in the
  147. following table:
  148. (+++) +---------------------------------------------------------------+
  149. (+++) | M1M0 bits | PCE bit | USART frame |
  150. (+++) |-----------------------|---------------------------------------|
  151. (+++) | 10 | 0 | | SB | 7-bit data | STB | |
  152. (+++) |-----------|-----------|---------------------------------------|
  153. (+++) | 10 | 1 | | SB | 6-bit data | PB | STB | |
  154. (+++) +---------------------------------------------------------------+
  155. (++) USART polarity
  156. (++) USART phase
  157. (++) USART LastBit
  158. (++) Receiver/transmitter modes
  159. [..]
  160. The HAL_USART_Init() function follows the USART synchronous configuration
  161. procedure (details for the procedure are available in reference manual).
  162. @endverbatim
  163. * @{
  164. */
  165. /**
  166. * @brief Initializes the USART mode according to the specified
  167. * parameters in the USART_InitTypeDef and create the associated handle.
  168. * @param husart: USART handle
  169. * @retval HAL status
  170. */
  171. HAL_StatusTypeDef HAL_USART_Init(USART_HandleTypeDef *husart)
  172. {
  173. /* Check the USART handle allocation */
  174. if(husart == NULL)
  175. {
  176. return HAL_ERROR;
  177. }
  178. /* Check the parameters */
  179. assert_param(IS_USART_INSTANCE(husart->Instance));
  180. if(husart->State == HAL_USART_STATE_RESET)
  181. {
  182. /* Allocate lock resource and initialize it */
  183. husart->Lock = HAL_UNLOCKED;
  184. /* Init the low level hardware : GPIO, CLOCK */
  185. HAL_USART_MspInit(husart);
  186. }
  187. husart->State = HAL_USART_STATE_BUSY;
  188. /* Disable the Peripheral */
  189. __HAL_USART_DISABLE(husart);
  190. /* Set the Usart Communication parameters */
  191. if (USART_SetConfig(husart) == HAL_ERROR)
  192. {
  193. return HAL_ERROR;
  194. }
  195. /* In Synchronous mode, the following bits must be kept cleared:
  196. - LINEN bit in the USART_CR2 register
  197. - HDSEL, SCEN and IREN bits in the USART_CR3 register.*/
  198. husart->Instance->CR2 &= ~USART_CR2_LINEN;
  199. husart->Instance->CR3 &= ~(USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN);
  200. /* Enable the Peripheral */
  201. __HAL_USART_ENABLE(husart);
  202. /* TEACK and/or REACK to check before moving husart->State to Ready */
  203. return (USART_CheckIdleState(husart));
  204. }
  205. /**
  206. * @brief DeInitializes the USART peripheral
  207. * @param husart: USART handle
  208. * @retval HAL status
  209. */
  210. HAL_StatusTypeDef HAL_USART_DeInit(USART_HandleTypeDef *husart)
  211. {
  212. /* Check the USART handle allocation */
  213. if(husart == NULL)
  214. {
  215. return HAL_ERROR;
  216. }
  217. /* Check the parameters */
  218. assert_param(IS_USART_INSTANCE(husart->Instance));
  219. husart->State = HAL_USART_STATE_BUSY;
  220. husart->Instance->CR1 = 0x0;
  221. husart->Instance->CR2 = 0x0;
  222. husart->Instance->CR3 = 0x0;
  223. /* DeInit the low level hardware */
  224. HAL_USART_MspDeInit(husart);
  225. husart->ErrorCode = HAL_USART_ERROR_NONE;
  226. husart->State = HAL_USART_STATE_RESET;
  227. /* Process Unlock */
  228. __HAL_UNLOCK(husart);
  229. return HAL_OK;
  230. }
  231. /**
  232. * @brief USART MSP Init
  233. * @param husart: USART handle
  234. * @retval None
  235. */
  236. __weak void HAL_USART_MspInit(USART_HandleTypeDef *husart)
  237. {
  238. /* NOTE : This function should not be modified, when the callback is needed,
  239. the HAL_USART_MspInit can be implemented in the user file
  240. */
  241. }
  242. /**
  243. * @brief USART MSP DeInit
  244. * @param husart: USART handle
  245. * @retval None
  246. */
  247. __weak void HAL_USART_MspDeInit(USART_HandleTypeDef *husart)
  248. {
  249. /* NOTE : This function should not be modified, when the callback is needed,
  250. the HAL_USART_MspDeInit can be implemented in the user file
  251. */
  252. }
  253. /**
  254. * @}
  255. */
  256. /** @defgroup USART_Exported_Functions_Group2 IO operation functions
  257. * @brief USART Transmit and Receive functions
  258. *
  259. @verbatim
  260. ===============================================================================
  261. ##### IO operation functions #####
  262. ===============================================================================
  263. This subsection provides a set of functions allowing to manage the USART synchronous
  264. data transfers.
  265. [..] The USART supports master mode only: it cannot receive or send data related to an input
  266. clock (SCLK is always an output).
  267. (#) There are two mode of transfer:
  268. (++) Blocking mode: The communication is performed in polling mode.
  269. The HAL status of all data processing is returned by the same function
  270. after finishing transfer.
  271. (++) No-Blocking mode: The communication is performed using Interrupts
  272. or DMA, These API's return the HAL status.
  273. The end of the data processing will be indicated through the
  274. dedicated USART IRQ when using Interrupt mode or the DMA IRQ when
  275. using DMA mode.
  276. The HAL_USART_TxCpltCallback(), HAL_USART_RxCpltCallback() and HAL_USART_TxRxCpltCallback() user callbacks
  277. will be executed respectively at the end of the transmit or Receive process
  278. The HAL_USART_ErrorCallback()user callback will be executed when a communication error is detected
  279. (#) Blocking mode API's are :
  280. (++) HAL_USART_Transmit()in simplex mode
  281. (++) HAL_USART_Receive() in full duplex receive only
  282. (++) HAL_USART_TransmitReceive() in full duplex mode
  283. (#) Non-Blocking mode API's with Interrupt are :
  284. (++) HAL_USART_Transmit_IT()in simplex mode
  285. (++) HAL_USART_Receive_IT() in full duplex receive only
  286. (++) HAL_USART_TransmitReceive_IT()in full duplex mode
  287. (++) HAL_USART_IRQHandler()
  288. (#) No-Blocking mode functions with DMA are :
  289. (++) HAL_USART_Transmit_DMA()in simplex mode
  290. (++) HAL_USART_Receive_DMA() in full duplex receive only
  291. (++) HAL_USART_TransmitReceive_DMA() in full duplex mode
  292. (++) HAL_USART_DMAPause()
  293. (++) HAL_USART_DMAResume()
  294. (++) HAL_USART_DMAStop()
  295. (#) A set of Transfer Complete Callbacks are provided in No_Blocking mode:
  296. (++) HAL_USART_TxCpltCallback()
  297. (++) HAL_USART_RxCpltCallback()
  298. (++) HAL_USART_TxHalfCpltCallback()
  299. (++) HAL_USART_RxHalfCpltCallback()
  300. (++) HAL_USART_ErrorCallback()
  301. (++) HAL_USART_TxRxCpltCallback()
  302. @endverbatim
  303. * @{
  304. */
  305. /**
  306. * @brief Simplex Send an amount of data in blocking mode
  307. * @param husart: USART handle
  308. * @param pTxData: pointer to data buffer
  309. * @param Size: amount of data to be sent
  310. * @param Timeout : Timeout duration
  311. * @retval HAL status
  312. */
  313. HAL_StatusTypeDef HAL_USART_Transmit(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size, uint32_t Timeout)
  314. {
  315. uint16_t* tmp;
  316. if(husart->State == HAL_USART_STATE_READY)
  317. {
  318. if((pTxData == NULL) || (Size == 0))
  319. {
  320. return HAL_ERROR;
  321. }
  322. /* Process Locked */
  323. __HAL_LOCK(husart);
  324. husart->ErrorCode = HAL_USART_ERROR_NONE;
  325. husart->State = HAL_USART_STATE_BUSY_TX;
  326. husart->TxXferSize = Size;
  327. husart->TxXferCount = Size;
  328. /* Check the remaining data to be sent */
  329. while(husart->TxXferCount > 0)
  330. {
  331. husart->TxXferCount--;
  332. if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TC, RESET, Timeout) != HAL_OK)
  333. {
  334. return HAL_TIMEOUT;
  335. }
  336. if((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  337. {
  338. tmp = (uint16_t*) pTxData;
  339. husart->Instance->TDR = (*tmp & (uint16_t)0x01FF);
  340. pTxData += 2;
  341. }
  342. else
  343. {
  344. husart->Instance->TDR = (*pTxData++ & (uint8_t)0xFF);
  345. }
  346. }
  347. if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TC, RESET, Timeout) != HAL_OK)
  348. {
  349. return HAL_TIMEOUT;
  350. }
  351. husart->State = HAL_USART_STATE_READY;
  352. /* Process Unlocked */
  353. __HAL_UNLOCK(husart);
  354. return HAL_OK;
  355. }
  356. else
  357. {
  358. return HAL_BUSY;
  359. }
  360. }
  361. /**
  362. * @brief Receive an amount of data in blocking mode
  363. * @note To receive synchronous data, dummy data are simultaneously transmitted
  364. * @param husart: USART handle
  365. * @param pRxData: pointer to data buffer
  366. * @param Size: amount of data to be received
  367. * @param Timeout : Timeout duration
  368. * @retval HAL status
  369. */
  370. HAL_StatusTypeDef HAL_USART_Receive(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size, uint32_t Timeout)
  371. {
  372. uint16_t* tmp;
  373. uint16_t uhMask;
  374. if(husart->State == HAL_USART_STATE_READY)
  375. {
  376. if((pRxData == NULL) || (Size == 0))
  377. {
  378. return HAL_ERROR;
  379. }
  380. /* Process Locked */
  381. __HAL_LOCK(husart);
  382. husart->ErrorCode = HAL_USART_ERROR_NONE;
  383. husart->State = HAL_USART_STATE_BUSY_RX;
  384. husart->RxXferSize = Size;
  385. husart->RxXferCount = Size;
  386. /* Computation of USART mask to apply to RDR register */
  387. __HAL_USART_MASK_COMPUTATION(husart);
  388. uhMask = husart->Mask;
  389. /* as long as data have to be received */
  390. while(husart->RxXferCount > 0)
  391. {
  392. husart->RxXferCount--;
  393. /* Wait until TC flag is set to send dummy byte in order to generate the
  394. * clock for the slave to send data.
  395. * Whatever the frame length (7, 8 or 9-bit long), the same dummy value
  396. * can be written for all the cases. */
  397. if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TC, RESET, Timeout) != HAL_OK)
  398. {
  399. return HAL_TIMEOUT;
  400. }
  401. husart->Instance->TDR = (DUMMY_DATA & (uint16_t)0x0FF);
  402. /* Wait for RXNE Flag */
  403. if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, Timeout) != HAL_OK)
  404. {
  405. return HAL_TIMEOUT;
  406. }
  407. if((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  408. {
  409. tmp = (uint16_t*) pRxData ;
  410. *tmp = (uint16_t)(husart->Instance->RDR & uhMask);
  411. pRxData +=2;
  412. }
  413. else
  414. {
  415. *pRxData++ = (uint8_t)(husart->Instance->RDR & (uint8_t)uhMask);
  416. }
  417. }
  418. husart->State = HAL_USART_STATE_READY;
  419. /* Process Unlocked */
  420. __HAL_UNLOCK(husart);
  421. return HAL_OK;
  422. }
  423. else
  424. {
  425. return HAL_BUSY;
  426. }
  427. }
  428. /**
  429. * @brief Full-Duplex Send and Receive an amount of data in blocking mode
  430. * @param husart: USART handle
  431. * @param pTxData: pointer to TX data buffer
  432. * @param pRxData: pointer to RX data buffer
  433. * @param Size: amount of data to be sent (same amount to be received)
  434. * @param Timeout : Timeout duration
  435. * @retval HAL status
  436. */
  437. HAL_StatusTypeDef HAL_USART_TransmitReceive(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size, uint32_t Timeout)
  438. {
  439. uint16_t* tmp;
  440. uint16_t uhMask;
  441. if(husart->State == HAL_USART_STATE_READY)
  442. {
  443. if((pTxData == NULL) || (pRxData == NULL) || (Size == 0))
  444. {
  445. return HAL_ERROR;
  446. }
  447. /* Process Locked */
  448. __HAL_LOCK(husart);
  449. husart->ErrorCode = HAL_USART_ERROR_NONE;
  450. husart->State = HAL_USART_STATE_BUSY_RX;
  451. husart->RxXferSize = Size;
  452. husart->TxXferSize = Size;
  453. husart->TxXferCount = Size;
  454. husart->RxXferCount = Size;
  455. /* Computation of USART mask to apply to RDR register */
  456. __HAL_USART_MASK_COMPUTATION(husart);
  457. uhMask = husart->Mask;
  458. /* Check the remain data to be sent */
  459. while(husart->TxXferCount > 0)
  460. {
  461. husart->TxXferCount--;
  462. husart->RxXferCount--;
  463. /* Wait until TC flag is set to send data */
  464. if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TC, RESET, Timeout) != HAL_OK)
  465. {
  466. return HAL_TIMEOUT;
  467. }
  468. if((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  469. {
  470. tmp = (uint16_t*) pTxData;
  471. husart->Instance->TDR = (*tmp & uhMask);
  472. pTxData += 2;
  473. }
  474. else
  475. {
  476. husart->Instance->TDR = (*pTxData++ & (uint8_t)uhMask);
  477. }
  478. /* Wait for RXNE Flag */
  479. if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, Timeout) != HAL_OK)
  480. {
  481. return HAL_TIMEOUT;
  482. }
  483. if((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  484. {
  485. tmp = (uint16_t*) pRxData ;
  486. *tmp = (uint16_t)(husart->Instance->RDR & uhMask);
  487. pRxData +=2;
  488. }
  489. else
  490. {
  491. *pRxData++ = (uint8_t)(husart->Instance->RDR & (uint8_t)uhMask);
  492. }
  493. }
  494. husart->State = HAL_USART_STATE_READY;
  495. /* Process Unlocked */
  496. __HAL_UNLOCK(husart);
  497. return HAL_OK;
  498. }
  499. else
  500. {
  501. return HAL_BUSY;
  502. }
  503. }
  504. /**
  505. * @brief Send an amount of data in interrupt mode
  506. * @param husart: USART handle
  507. * @param pTxData: pointer to data buffer
  508. * @param Size: amount of data to be sent
  509. * @retval HAL status
  510. */
  511. HAL_StatusTypeDef HAL_USART_Transmit_IT(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size)
  512. {
  513. if(husart->State == HAL_USART_STATE_READY)
  514. {
  515. if((pTxData == NULL ) || (Size == 0))
  516. {
  517. return HAL_ERROR;
  518. }
  519. /* Process Locked */
  520. __HAL_LOCK(husart);
  521. husart->pTxBuffPtr = pTxData;
  522. husart->TxXferSize = Size;
  523. husart->TxXferCount = Size;
  524. husart->ErrorCode = HAL_USART_ERROR_NONE;
  525. husart->State = HAL_USART_STATE_BUSY_TX;
  526. /* The USART Error Interrupts: (Frame error, noise error, overrun error)
  527. are not managed by the USART Transmit Process to avoid the overrun interrupt
  528. when the usart mode is configured for transmit and receive "USART_MODE_TX_RX"
  529. to benefit for the frame error and noise interrupts the usart mode should be
  530. configured only for transmit "USART_MODE_TX" */
  531. /* Process Unlocked */
  532. __HAL_UNLOCK(husart);
  533. /* Enable the USART Transmit Data Register Empty Interrupt */
  534. __HAL_USART_ENABLE_IT(husart, USART_IT_TXE);
  535. return HAL_OK;
  536. }
  537. else
  538. {
  539. return HAL_BUSY;
  540. }
  541. }
  542. /**
  543. * @brief Receive an amount of data in blocking mode
  544. * To receive synchronous data, dummy data are simultaneously transmitted
  545. * @param husart: USART handle
  546. * @param pRxData: pointer to data buffer
  547. * @param Size: amount of data to be received
  548. * @retval HAL status
  549. */
  550. HAL_StatusTypeDef HAL_USART_Receive_IT(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size)
  551. {
  552. if(husart->State == HAL_USART_STATE_READY)
  553. {
  554. if((pRxData == NULL ) || (Size == 0))
  555. {
  556. return HAL_ERROR;
  557. }
  558. /* Process Locked */
  559. __HAL_LOCK(husart);
  560. husart->pRxBuffPtr = pRxData;
  561. husart->RxXferSize = Size;
  562. husart->RxXferCount = Size;
  563. __HAL_USART_MASK_COMPUTATION(husart);
  564. husart->ErrorCode = HAL_USART_ERROR_NONE;
  565. husart->State = HAL_USART_STATE_BUSY_RX;
  566. /* Enable the USART Parity Error Interrupt */
  567. __HAL_USART_ENABLE_IT(husart, USART_IT_PE);
  568. /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  569. __HAL_USART_ENABLE_IT(husart, USART_IT_ERR);
  570. /* Enable the USART Data Register not empty Interrupt */
  571. __HAL_USART_ENABLE_IT(husart, USART_IT_RXNE);
  572. /* Process Unlocked */
  573. __HAL_UNLOCK(husart);
  574. /* Send dummy byte in order to generate the clock for the Slave to send the next data */
  575. if(husart->Init.WordLength == USART_WORDLENGTH_9B)
  576. {
  577. husart->Instance->TDR = (DUMMY_DATA & (uint16_t)0x01FF);
  578. }
  579. else
  580. {
  581. husart->Instance->TDR = (DUMMY_DATA & (uint16_t)0x00FF);
  582. }
  583. return HAL_OK;
  584. }
  585. else
  586. {
  587. return HAL_BUSY;
  588. }
  589. }
  590. /**
  591. * @brief Full-Duplex Send and Receive an amount of data in interrupt mode
  592. * @param husart: USART handle
  593. * @param pTxData: pointer to TX data buffer
  594. * @param pRxData: pointer to RX data buffer
  595. * @param Size: amount of data to be sent (same amount to be received)
  596. * @retval HAL status
  597. */
  598. HAL_StatusTypeDef HAL_USART_TransmitReceive_IT(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size)
  599. {
  600. if(husart->State == HAL_USART_STATE_READY)
  601. {
  602. if((pTxData == NULL) || (pRxData == NULL) || (Size == 0))
  603. {
  604. return HAL_ERROR;
  605. }
  606. /* Process Locked */
  607. __HAL_LOCK(husart);
  608. husart->pRxBuffPtr = pRxData;
  609. husart->RxXferSize = Size;
  610. husart->RxXferCount = Size;
  611. husart->pTxBuffPtr = pTxData;
  612. husart->TxXferSize = Size;
  613. husart->TxXferCount = Size;
  614. /* Computation of USART mask to apply to RDR register */
  615. __HAL_USART_MASK_COMPUTATION(husart);
  616. husart->ErrorCode = HAL_USART_ERROR_NONE;
  617. husart->State = HAL_USART_STATE_BUSY_TX_RX;
  618. /* Enable the USART Data Register not empty Interrupt */
  619. __HAL_USART_ENABLE_IT(husart, USART_IT_RXNE);
  620. /* Enable the USART Parity Error Interrupt */
  621. __HAL_USART_ENABLE_IT(husart, USART_IT_PE);
  622. /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  623. __HAL_USART_ENABLE_IT(husart, USART_IT_ERR);
  624. /* Process Unlocked */
  625. __HAL_UNLOCK(husart);
  626. /* Enable the USART Transmit Data Register Empty Interrupt */
  627. __HAL_USART_ENABLE_IT(husart, USART_IT_TXE);
  628. return HAL_OK;
  629. }
  630. else
  631. {
  632. return HAL_BUSY;
  633. }
  634. }
  635. /**
  636. * @brief Send an amount of data in DMA mode
  637. * @param husart: USART handle
  638. * @param pTxData: pointer to data buffer
  639. * @param Size: amount of data to be sent
  640. * @retval HAL status
  641. */
  642. HAL_StatusTypeDef HAL_USART_Transmit_DMA(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size)
  643. {
  644. uint32_t *tmp;
  645. if(husart->State == HAL_USART_STATE_READY)
  646. {
  647. if((pTxData == NULL ) || (Size == 0))
  648. {
  649. return HAL_ERROR;
  650. }
  651. /* Process Locked */
  652. __HAL_LOCK(husart);
  653. husart->pTxBuffPtr = pTxData;
  654. husart->TxXferSize = Size;
  655. husart->TxXferCount = Size;
  656. husart->ErrorCode = HAL_USART_ERROR_NONE;
  657. husart->State = HAL_USART_STATE_BUSY_TX;
  658. /* Set the USART DMA transfer complete callback */
  659. husart->hdmatx->XferCpltCallback = USART_DMATransmitCplt;
  660. /* Set the USART DMA Half transfer complete callback */
  661. husart->hdmatx->XferHalfCpltCallback = USART_DMATxHalfCplt;
  662. /* Set the DMA error callback */
  663. husart->hdmatx->XferErrorCallback = USART_DMAError;
  664. /* Enable the USART transmit DMA channel */
  665. tmp = (uint32_t*)&pTxData;
  666. HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t*)tmp, (uint32_t)&husart->Instance->TDR, Size);
  667. /* Clear the TC flag in the SR register by writing 0 to it */
  668. __HAL_USART_CLEAR_IT(husart, USART_FLAG_TC);
  669. /* Enable the DMA transfer for transmit request by setting the DMAT bit
  670. in the USART CR3 register */
  671. husart->Instance->CR3 |= USART_CR3_DMAT;
  672. /* Process Unlocked */
  673. __HAL_UNLOCK(husart);
  674. return HAL_OK;
  675. }
  676. else
  677. {
  678. return HAL_BUSY;
  679. }
  680. }
  681. /**
  682. * @brief Receive an amount of data in DMA mode
  683. * @param husart: USART handle
  684. * @param pRxData: pointer to data buffer
  685. * @param Size: amount of data to be received
  686. * @note When the USART parity is enabled (PCE = 1), the received data contain
  687. * the parity bit (MSB position)
  688. * @retval HAL status
  689. * @note The USART DMA transmit stream must be configured in order to generate the clock for the slave.
  690. */
  691. HAL_StatusTypeDef HAL_USART_Receive_DMA(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size)
  692. {
  693. uint32_t *tmp;
  694. if(husart->State == HAL_USART_STATE_READY)
  695. {
  696. if((pRxData == NULL ) || (Size == 0))
  697. {
  698. return HAL_ERROR;
  699. }
  700. /* Process Locked */
  701. __HAL_LOCK(husart);
  702. husart->pRxBuffPtr = pRxData;
  703. husart->RxXferSize = Size;
  704. husart->pTxBuffPtr = pRxData;
  705. husart->TxXferSize = Size;
  706. husart->ErrorCode = HAL_USART_ERROR_NONE;
  707. husart->State = HAL_USART_STATE_BUSY_RX;
  708. /* Set the USART DMA Rx transfer complete callback */
  709. husart->hdmarx->XferCpltCallback = USART_DMAReceiveCplt;
  710. /* Set the USART DMA Half transfer complete callback */
  711. husart->hdmarx->XferHalfCpltCallback = USART_DMARxHalfCplt;
  712. /* Set the USART DMA Rx transfer error callback */
  713. husart->hdmarx->XferErrorCallback = USART_DMAError;
  714. /* Enable the USART receive DMA channel */
  715. tmp = (uint32_t*)&pRxData;
  716. HAL_DMA_Start_IT(husart->hdmarx, (uint32_t)&husart->Instance->RDR, *(uint32_t*)tmp, Size);
  717. /* Enable the USART transmit DMA channel: the transmit stream is used in order
  718. to generate in the non-blocking mode the clock to the slave device,
  719. this mode isn't a simplex receive mode but a full-duplex receive mode */
  720. tmp = (uint32_t*)&pRxData;
  721. HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t*)tmp, (uint32_t)&husart->Instance->TDR, Size);
  722. /* Enable the DMA transfer for the receiver request by setting the DMAR bit
  723. in the USART CR3 register */
  724. husart->Instance->CR3 |= USART_CR3_DMAR;
  725. /* Enable the DMA transfer for transmit request by setting the DMAT bit
  726. in the USART CR3 register */
  727. husart->Instance->CR3 |= USART_CR3_DMAT;
  728. /* Process Unlocked */
  729. __HAL_UNLOCK(husart);
  730. return HAL_OK;
  731. }
  732. else
  733. {
  734. return HAL_BUSY;
  735. }
  736. }
  737. /**
  738. * @brief Full-Duplex Transmit Receive an amount of data in non blocking mode
  739. * @param husart: USART handle
  740. * @param pTxData: pointer to TX data buffer
  741. * @param pRxData: pointer to RX data buffer
  742. * @param Size: amount of data to be received/sent
  743. * @note When the USART parity is enabled (PCE = 1) the data received contain the parity bit.
  744. * @retval HAL status
  745. */
  746. HAL_StatusTypeDef HAL_USART_TransmitReceive_DMA(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size)
  747. {
  748. uint32_t *tmp;
  749. if(husart->State == HAL_USART_STATE_READY)
  750. {
  751. if((pTxData == NULL) || (pRxData == NULL) || (Size == 0))
  752. {
  753. return HAL_ERROR;
  754. }
  755. /* Process Locked */
  756. __HAL_LOCK(husart);
  757. husart->pRxBuffPtr = pRxData;
  758. husart->RxXferSize = Size;
  759. husart->pTxBuffPtr = pTxData;
  760. husart->TxXferSize = Size;
  761. husart->ErrorCode = HAL_USART_ERROR_NONE;
  762. husart->State = HAL_USART_STATE_BUSY_TX_RX;
  763. /* Set the USART DMA Rx transfer complete callback */
  764. husart->hdmarx->XferCpltCallback = USART_DMAReceiveCplt;
  765. /* Set the USART DMA Half transfer complete callback */
  766. husart->hdmarx->XferHalfCpltCallback = USART_DMARxHalfCplt;
  767. /* Set the USART DMA Tx transfer complete callback */
  768. husart->hdmatx->XferCpltCallback = USART_DMATransmitCplt;
  769. /* Set the USART DMA Half transfer complete callback */
  770. husart->hdmatx->XferHalfCpltCallback = USART_DMATxHalfCplt;
  771. /* Set the USART DMA Tx transfer error callback */
  772. husart->hdmatx->XferErrorCallback = USART_DMAError;
  773. /* Set the USART DMA Rx transfer error callback */
  774. husart->hdmarx->XferErrorCallback = USART_DMAError;
  775. /* Enable the USART receive DMA channel */
  776. tmp = (uint32_t*)&pRxData;
  777. HAL_DMA_Start_IT(husart->hdmarx, (uint32_t)&husart->Instance->RDR, *(uint32_t*)tmp, Size);
  778. /* Enable the USART transmit DMA channel */
  779. tmp = (uint32_t*)&pTxData;
  780. HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t*)tmp, (uint32_t)&husart->Instance->TDR, Size);
  781. /* Clear the TC flag in the SR register by writing 0 to it */
  782. __HAL_USART_CLEAR_IT(husart, USART_FLAG_TC);
  783. /* Enable the DMA transfer for the receiver request by setting the DMAR bit
  784. in the USART CR3 register */
  785. husart->Instance->CR3 |= USART_CR3_DMAR;
  786. /* Enable the DMA transfer for transmit request by setting the DMAT bit
  787. in the USART CR3 register */
  788. husart->Instance->CR3 |= USART_CR3_DMAT;
  789. /* Process Unlocked */
  790. __HAL_UNLOCK(husart);
  791. return HAL_OK;
  792. }
  793. else
  794. {
  795. return HAL_BUSY;
  796. }
  797. }
  798. /**
  799. * @brief Pauses the DMA Transfer.
  800. * @param husart: USART handle
  801. * @retval None
  802. */
  803. HAL_StatusTypeDef HAL_USART_DMAPause(USART_HandleTypeDef *husart)
  804. {
  805. /* Process Locked */
  806. __HAL_LOCK(husart);
  807. if(husart->State == HAL_USART_STATE_BUSY_TX)
  808. {
  809. /* Disable the USART DMA Tx request */
  810. husart->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAT);
  811. }
  812. else if(husart->State == HAL_USART_STATE_BUSY_RX)
  813. {
  814. /* Disable the USART DMA Rx request */
  815. husart->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAR);
  816. }
  817. else if(husart->State == HAL_USART_STATE_BUSY_TX_RX)
  818. {
  819. /* Disable the USART DMA Tx request */
  820. husart->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAT);
  821. /* Disable the USART DMA Rx request */
  822. husart->Instance->CR3 &= (uint32_t)(~USART_CR3_DMAR);
  823. }
  824. /* Process Unlocked */
  825. __HAL_UNLOCK(husart);
  826. return HAL_OK;
  827. }
  828. /**
  829. * @brief Resumes the DMA Transfer.
  830. * @param husart: USART handle
  831. * @retval None
  832. */
  833. HAL_StatusTypeDef HAL_USART_DMAResume(USART_HandleTypeDef *husart)
  834. {
  835. /* Process Locked */
  836. __HAL_LOCK(husart);
  837. if(husart->State == HAL_USART_STATE_BUSY_TX)
  838. {
  839. /* Enable the USART DMA Tx request */
  840. husart->Instance->CR3 |= USART_CR3_DMAT;
  841. }
  842. else if(husart->State == HAL_USART_STATE_BUSY_RX)
  843. {
  844. /* Clear the Overrun flag before resuming the Rx transfer*/
  845. __HAL_USART_CLEAR_IT(husart, USART_CLEAR_OREF);
  846. /* Enable the USART DMA Rx request */
  847. husart->Instance->CR3 |= USART_CR3_DMAR;
  848. }
  849. else if(husart->State == HAL_USART_STATE_BUSY_TX_RX)
  850. {
  851. /* Clear the Overrun flag before resuming the Rx transfer*/
  852. __HAL_USART_CLEAR_IT(husart, USART_CLEAR_OREF);
  853. /* Enable the USART DMA Rx request before the DMA Tx request */
  854. husart->Instance->CR3 |= USART_CR3_DMAR;
  855. /* Enable the USART DMA Tx request */
  856. husart->Instance->CR3 |= USART_CR3_DMAT;
  857. }
  858. /* Process Unlocked */
  859. __HAL_UNLOCK(husart);
  860. return HAL_OK;
  861. }
  862. /**
  863. * @brief Stops the DMA Transfer.
  864. * @param husart: USART handle
  865. * @retval None
  866. */
  867. HAL_StatusTypeDef HAL_USART_DMAStop(USART_HandleTypeDef *husart)
  868. {
  869. /* The Lock is not implemented on this API to allow the user application
  870. to call the HAL USART API under callbacks HAL_USART_TxCpltCallback() / HAL_USART_RxCpltCallback() /
  871. HAL_USART_TxHalfCpltCallback / HAL_USART_RxHalfCpltCallback:
  872. indeed, when HAL_DMA_Abort() API is called, the DMA TX/RX Transfer or Half Transfer complete
  873. interrupt is generated if the DMA transfer interruption occurs at the middle or at the end of
  874. the stream and the corresponding call back is executed. */
  875. /* Disable the USART Tx/Rx DMA requests */
  876. husart->Instance->CR3 &= ~USART_CR3_DMAT;
  877. husart->Instance->CR3 &= ~USART_CR3_DMAR;
  878. /* Abort the USART DMA tx Stream */
  879. if(husart->hdmatx != NULL)
  880. {
  881. HAL_DMA_Abort(husart->hdmatx);
  882. }
  883. /* Abort the USART DMA rx Stream */
  884. if(husart->hdmarx != NULL)
  885. {
  886. HAL_DMA_Abort(husart->hdmarx);
  887. }
  888. husart->State = HAL_USART_STATE_READY;
  889. return HAL_OK;
  890. }
  891. /**
  892. * @brief This function handles USART interrupt request.
  893. * @param husart: USART handle
  894. * @retval None
  895. */
  896. void HAL_USART_IRQHandler(USART_HandleTypeDef *husart)
  897. {
  898. /* USART parity error interrupt occurred ------------------------------------*/
  899. if((__HAL_USART_GET_IT(husart, USART_IT_PE) != RESET) && (__HAL_USART_GET_IT_SOURCE(husart, USART_IT_PE) != RESET))
  900. {
  901. __HAL_USART_CLEAR_IT(husart, USART_CLEAR_PEF);
  902. husart->ErrorCode |= HAL_USART_ERROR_PE;
  903. /* Set the USART state ready to be able to start again the process */
  904. husart->State = HAL_USART_STATE_READY;
  905. }
  906. /* USART frame error interrupt occurred -------------------------------------*/
  907. if((__HAL_USART_GET_IT(husart, USART_IT_FE) != RESET) && (__HAL_USART_GET_IT_SOURCE(husart, USART_IT_ERR) != RESET))
  908. {
  909. __HAL_USART_CLEAR_IT(husart, USART_CLEAR_FEF);
  910. husart->ErrorCode |= HAL_USART_ERROR_FE;
  911. /* Set the USART state ready to be able to start again the process */
  912. husart->State = HAL_USART_STATE_READY;
  913. }
  914. /* USART noise error interrupt occurred -------------------------------------*/
  915. if((__HAL_USART_GET_IT(husart, USART_IT_NE) != RESET) && (__HAL_USART_GET_IT_SOURCE(husart, USART_IT_ERR) != RESET))
  916. {
  917. __HAL_USART_CLEAR_IT(husart, USART_CLEAR_NEF);
  918. husart->ErrorCode |= HAL_USART_ERROR_NE;
  919. /* Set the USART state ready to be able to start again the process */
  920. husart->State = HAL_USART_STATE_READY;
  921. }
  922. /* USART Over-Run interrupt occurred ----------------------------------------*/
  923. if((__HAL_USART_GET_IT(husart, USART_IT_ORE) != RESET) && (__HAL_USART_GET_IT_SOURCE(husart, USART_IT_ERR) != RESET))
  924. {
  925. __HAL_USART_CLEAR_IT(husart, USART_CLEAR_OREF);
  926. husart->ErrorCode |= HAL_USART_ERROR_ORE;
  927. /* Set the USART state ready to be able to start again the process */
  928. husart->State = HAL_USART_STATE_READY;
  929. }
  930. /* Call USART Error Call back function if need be --------------------------*/
  931. if(husart->ErrorCode != HAL_USART_ERROR_NONE)
  932. {
  933. HAL_USART_ErrorCallback(husart);
  934. }
  935. /* USART in mode Receiver --------------------------------------------------*/
  936. if((__HAL_USART_GET_IT(husart, USART_IT_RXNE) != RESET) && (__HAL_USART_GET_IT_SOURCE(husart, USART_IT_RXNE) != RESET))
  937. {
  938. if(husart->State == HAL_USART_STATE_BUSY_RX)
  939. {
  940. USART_Receive_IT(husart);
  941. }
  942. else
  943. {
  944. USART_TransmitReceive_IT(husart);
  945. }
  946. }
  947. /* USART in mode Transmitter -----------------------------------------------*/
  948. if((__HAL_USART_GET_IT(husart, USART_IT_TXE) != RESET) &&(__HAL_USART_GET_IT_SOURCE(husart, USART_IT_TXE) != RESET))
  949. {
  950. if(husart->State == HAL_USART_STATE_BUSY_TX)
  951. {
  952. USART_Transmit_IT(husart);
  953. }
  954. else
  955. {
  956. USART_TransmitReceive_IT(husart);
  957. }
  958. }
  959. /* USART in mode Transmitter (transmission end) -----------------------------*/
  960. if((__HAL_USART_GET_IT(husart, USART_IT_TC) != RESET) &&(__HAL_USART_GET_IT_SOURCE(husart, USART_IT_TC) != RESET))
  961. {
  962. USART_EndTransmit_IT(husart);
  963. }
  964. }
  965. /**
  966. * @brief Tx Transfer completed callbacks
  967. * @param husart: USART handle
  968. * @retval None
  969. */
  970. __weak void HAL_USART_TxCpltCallback(USART_HandleTypeDef *husart)
  971. {
  972. /* NOTE : This function should not be modified, when the callback is needed,
  973. the HAL_USART_TxCpltCallback can be implemented in the user file
  974. */
  975. }
  976. /**
  977. * @brief Tx Half Transfer completed callbacks.
  978. * @param husart: USART handle
  979. * @retval None
  980. */
  981. __weak void HAL_USART_TxHalfCpltCallback(USART_HandleTypeDef *husart)
  982. {
  983. /* NOTE: This function should not be modified, when the callback is needed,
  984. the HAL_USART_TxHalfCpltCallback can be implemented in the user file
  985. */
  986. }
  987. /**
  988. * @brief Rx Transfer completed callbacks.
  989. * @param husart: USART handle
  990. * @retval None
  991. */
  992. __weak void HAL_USART_RxCpltCallback(USART_HandleTypeDef *husart)
  993. {
  994. /* NOTE: This function should not be modified, when the callback is needed,
  995. the HAL_USART_RxCpltCallback can be implemented in the user file
  996. */
  997. }
  998. /**
  999. * @brief Rx Half Transfer completed callbacks
  1000. * @param husart: usart handle
  1001. * @retval None
  1002. */
  1003. __weak void HAL_USART_RxHalfCpltCallback(USART_HandleTypeDef *husart)
  1004. {
  1005. /* NOTE : This function should not be modified, when the callback is needed,
  1006. the HAL_USART_RxHalfCpltCallback can be implemented in the user file
  1007. */
  1008. }
  1009. /**
  1010. * @brief Tx/Rx Transfers completed callback for the non-blocking process
  1011. * @param husart: USART handle
  1012. * @retval None
  1013. */
  1014. __weak void HAL_USART_TxRxCpltCallback(USART_HandleTypeDef *husart)
  1015. {
  1016. /* NOTE : This function should not be modified, when the callback is needed,
  1017. the HAL_USART_TxRxCpltCallback can be implemented in the user file
  1018. */
  1019. }
  1020. /**
  1021. * @brief USART error callbacks
  1022. * @param husart: USART handle
  1023. * @retval None
  1024. */
  1025. __weak void HAL_USART_ErrorCallback(USART_HandleTypeDef *husart)
  1026. {
  1027. /* NOTE : This function should not be modified, when the callback is needed,
  1028. the HAL_USART_ErrorCallback can be implemented in the user file
  1029. */
  1030. }
  1031. /**
  1032. * @}
  1033. */
  1034. /** @defgroup USART_Exported_Functions_Group3 Peripheral State and Errors functions
  1035. * @brief USART State and Errors functions
  1036. *
  1037. @verbatim
  1038. ==============================================================================
  1039. ##### Peripheral State and Errors functions #####
  1040. ==============================================================================
  1041. [..]
  1042. This subsection provides a set of functions allowing to return the State of
  1043. USART communication
  1044. process, return Peripheral Errors occurred during communication process
  1045. (+) HAL_USART_GetState() API can be helpful to check in run-time the state
  1046. of the USART peripheral.
  1047. (+) HAL_USART_GetError() check in run-time errors that could be occurred during
  1048. communication.
  1049. @endverbatim
  1050. * @{
  1051. */
  1052. /**
  1053. * @brief return the USART state
  1054. * @param husart: USART handle
  1055. * @retval HAL state
  1056. */
  1057. HAL_USART_StateTypeDef HAL_USART_GetState(USART_HandleTypeDef *husart)
  1058. {
  1059. return husart->State;
  1060. }
  1061. /**
  1062. * @brief Return the USART error code
  1063. * @param husart : pointer to a USART_HandleTypeDef structure that contains
  1064. * the configuration information for the specified USART.
  1065. * @retval USART Error Code
  1066. */
  1067. uint32_t HAL_USART_GetError(USART_HandleTypeDef *husart)
  1068. {
  1069. return husart->ErrorCode;
  1070. }
  1071. /**
  1072. * @}
  1073. */
  1074. /**
  1075. * @brief Simplex Send an amount of data in non-blocking mode.
  1076. * @note Function called under interruption only, once
  1077. * interruptions have been enabled by HAL_USART_Transmit_IT().
  1078. * @param husart: USART handle
  1079. * @retval HAL status
  1080. * @note The USART errors are not managed to avoid the overrun error.
  1081. */
  1082. static HAL_StatusTypeDef USART_Transmit_IT(USART_HandleTypeDef *husart)
  1083. {
  1084. uint16_t* tmp;
  1085. if(husart->State == HAL_USART_STATE_BUSY_TX)
  1086. {
  1087. if(husart->TxXferCount == 0)
  1088. {
  1089. /* Disable the USART Transmit Complete Interrupt */
  1090. __HAL_USART_DISABLE_IT(husart, USART_IT_TXE);
  1091. /* Enable the USART Transmit Complete Interrupt */
  1092. __HAL_USART_ENABLE_IT(husart, USART_IT_TC);
  1093. return HAL_OK;
  1094. }
  1095. else
  1096. {
  1097. if((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  1098. {
  1099. tmp = (uint16_t*) husart->pTxBuffPtr;
  1100. husart->Instance->TDR = (*tmp & (uint16_t)0x01FF);
  1101. husart->pTxBuffPtr += 2;
  1102. }
  1103. else
  1104. {
  1105. husart->Instance->TDR = (uint8_t)(*husart->pTxBuffPtr++ & (uint8_t)0xFF);
  1106. }
  1107. husart->TxXferCount--;
  1108. return HAL_OK;
  1109. }
  1110. }
  1111. else
  1112. {
  1113. return HAL_BUSY;
  1114. }
  1115. }
  1116. /**
  1117. * @brief Wraps up transmission in non-blocking mode.
  1118. * @param husart: pointer to a USART_HandleTypeDef structure that contains
  1119. * the configuration information for the specified USART module.
  1120. * @retval HAL status
  1121. */
  1122. static HAL_StatusTypeDef USART_EndTransmit_IT(USART_HandleTypeDef *husart)
  1123. {
  1124. /* Disable the USART Transmit Complete Interrupt */
  1125. __HAL_USART_DISABLE_IT(husart, USART_IT_TC);
  1126. /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  1127. __HAL_USART_DISABLE_IT(husart, USART_IT_ERR);
  1128. husart->State = HAL_USART_STATE_READY;
  1129. HAL_USART_TxCpltCallback(husart);
  1130. return HAL_OK;
  1131. }
  1132. /**
  1133. * @brief Simplex Receive an amount of data in non-blocking mode.
  1134. * Function called under interruption only, once
  1135. * interruptions have been enabled by HAL_USART_Receive_IT()
  1136. * @param husart: USART handle
  1137. * @retval HAL status
  1138. */
  1139. static HAL_StatusTypeDef USART_Receive_IT(USART_HandleTypeDef *husart)
  1140. {
  1141. uint16_t* tmp;
  1142. uint16_t uhMask = husart->Mask;
  1143. if(husart->State == HAL_USART_STATE_BUSY_RX)
  1144. {
  1145. if((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  1146. {
  1147. tmp = (uint16_t*) husart->pRxBuffPtr;
  1148. *tmp = (uint16_t)(husart->Instance->RDR & uhMask);
  1149. husart->pRxBuffPtr += 2;
  1150. }
  1151. else
  1152. {
  1153. *husart->pRxBuffPtr++ = (uint8_t)(husart->Instance->RDR & (uint8_t)uhMask);
  1154. }
  1155. /* Send dummy byte in order to generate the clock for the Slave to Send the next data */
  1156. husart->Instance->TDR = (DUMMY_DATA & (uint16_t)0x00FF);
  1157. if(--husart->RxXferCount == 0)
  1158. {
  1159. __HAL_USART_DISABLE_IT(husart, USART_IT_RXNE);
  1160. /* Disable the USART Parity Error Interrupt */
  1161. __HAL_USART_DISABLE_IT(husart, USART_IT_PE);
  1162. /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  1163. __HAL_USART_DISABLE_IT(husart, USART_IT_ERR);
  1164. husart->State = HAL_USART_STATE_READY;
  1165. HAL_USART_RxCpltCallback(husart);
  1166. return HAL_OK;
  1167. }
  1168. return HAL_OK;
  1169. }
  1170. else
  1171. {
  1172. return HAL_BUSY;
  1173. }
  1174. }
  1175. /**
  1176. * @brief Full-Duplex Send receive an amount of data in full-duplex mode (non-blocking).
  1177. * Function called under interruption only, once
  1178. * interruptions have been enabled by HAL_USART_TransmitReceive_IT()
  1179. * @param husart: USART handle
  1180. * @retval HAL status
  1181. */
  1182. static HAL_StatusTypeDef USART_TransmitReceive_IT(USART_HandleTypeDef *husart)
  1183. {
  1184. uint16_t* tmp;
  1185. uint16_t uhMask = husart->Mask;
  1186. if(husart->State == HAL_USART_STATE_BUSY_TX_RX)
  1187. {
  1188. if(husart->TxXferCount != 0x00)
  1189. {
  1190. if(__HAL_USART_GET_FLAG(husart, USART_FLAG_TXE) != RESET)
  1191. {
  1192. if((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  1193. {
  1194. tmp = (uint16_t*) husart->pTxBuffPtr;
  1195. husart->Instance->TDR = (uint16_t)(*tmp & uhMask);
  1196. husart->pTxBuffPtr += 2;
  1197. }
  1198. else
  1199. {
  1200. husart->Instance->TDR = (uint8_t)(*husart->pTxBuffPtr++ & (uint8_t)uhMask);
  1201. }
  1202. husart->TxXferCount--;
  1203. /* Check the latest data transmitted */
  1204. if(husart->TxXferCount == 0)
  1205. {
  1206. __HAL_USART_DISABLE_IT(husart, USART_IT_TXE);
  1207. }
  1208. }
  1209. }
  1210. if(husart->RxXferCount != 0x00)
  1211. {
  1212. if(__HAL_USART_GET_FLAG(husart, USART_FLAG_RXNE) != RESET)
  1213. {
  1214. if((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  1215. {
  1216. tmp = (uint16_t*) husart->pRxBuffPtr;
  1217. *tmp = (uint16_t)(husart->Instance->RDR & uhMask);
  1218. husart->pRxBuffPtr += 2;
  1219. }
  1220. else
  1221. {
  1222. *husart->pRxBuffPtr++ = (uint8_t)(husart->Instance->RDR & (uint8_t)uhMask);
  1223. }
  1224. husart->RxXferCount--;
  1225. }
  1226. }
  1227. /* Check the latest data received */
  1228. if(husart->RxXferCount == 0)
  1229. {
  1230. __HAL_USART_DISABLE_IT(husart, USART_IT_RXNE);
  1231. /* Disable the USART Parity Error Interrupt */
  1232. __HAL_USART_DISABLE_IT(husart, USART_IT_PE);
  1233. /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  1234. __HAL_USART_DISABLE_IT(husart, USART_IT_ERR);
  1235. husart->State = HAL_USART_STATE_READY;
  1236. HAL_USART_TxRxCpltCallback(husart);
  1237. return HAL_OK;
  1238. }
  1239. return HAL_OK;
  1240. }
  1241. else
  1242. {
  1243. return HAL_BUSY;
  1244. }
  1245. }
  1246. /**
  1247. * @brief This function handles USART Communication Timeout.
  1248. * @param husart: USART handle
  1249. * @param Flag: specifies the USART flag to check.
  1250. * @param Status: The new Flag status (SET or RESET).
  1251. * @param Timeout: Timeout duration
  1252. * @retval HAL status
  1253. */
  1254. static HAL_StatusTypeDef USART_WaitOnFlagUntilTimeout(USART_HandleTypeDef *husart, uint32_t Flag, FlagStatus Status, uint32_t Timeout)
  1255. {
  1256. uint32_t tickstart = HAL_GetTick();
  1257. /* Wait until flag is set */
  1258. if(Status == RESET)
  1259. {
  1260. while(__HAL_USART_GET_FLAG(husart, Flag) == RESET)
  1261. {
  1262. /* Check for the Timeout */
  1263. if(Timeout != HAL_MAX_DELAY)
  1264. {
  1265. if((Timeout == 0)||((HAL_GetTick()-tickstart) >= Timeout))
  1266. {
  1267. /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
  1268. __HAL_USART_DISABLE_IT(husart, USART_IT_TXE);
  1269. __HAL_USART_DISABLE_IT(husart, USART_IT_RXNE);
  1270. __HAL_USART_DISABLE_IT(husart, USART_IT_PE);
  1271. __HAL_USART_DISABLE_IT(husart, USART_IT_ERR);
  1272. husart->State= HAL_USART_STATE_READY;
  1273. /* Process Unlocked */
  1274. __HAL_UNLOCK(husart);
  1275. return HAL_TIMEOUT;
  1276. }
  1277. }
  1278. }
  1279. }
  1280. else
  1281. {
  1282. while(__HAL_USART_GET_FLAG(husart, Flag) != RESET)
  1283. {
  1284. /* Check for the Timeout */
  1285. if(Timeout != HAL_MAX_DELAY)
  1286. {
  1287. if((Timeout == 0)||((HAL_GetTick()-tickstart) >= Timeout))
  1288. {
  1289. /* Disable TXE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts for the interrupt process */
  1290. __HAL_USART_DISABLE_IT(husart, USART_IT_TXE);
  1291. __HAL_USART_DISABLE_IT(husart, USART_IT_RXNE);
  1292. __HAL_USART_DISABLE_IT(husart, USART_IT_PE);
  1293. __HAL_USART_DISABLE_IT(husart, USART_IT_ERR);
  1294. husart->State= HAL_USART_STATE_READY;
  1295. /* Process Unlocked */
  1296. __HAL_UNLOCK(husart);
  1297. return HAL_TIMEOUT;
  1298. }
  1299. }
  1300. }
  1301. }
  1302. return HAL_OK;
  1303. }
  1304. /**
  1305. * @brief DMA USART transmit process complete callback
  1306. * @param hdma: DMA handle
  1307. * @retval None
  1308. */
  1309. static void USART_DMATransmitCplt(DMA_HandleTypeDef *hdma)
  1310. {
  1311. USART_HandleTypeDef* husart = ( USART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1312. /* DMA Normal mode */
  1313. if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0)
  1314. {
  1315. husart->TxXferCount = 0;
  1316. if(husart->State == HAL_USART_STATE_BUSY_TX)
  1317. {
  1318. /* Disable the DMA transfer for transmit request by resetting the DMAT bit
  1319. in the USART CR3 register */
  1320. husart->Instance->CR3 &= ~(USART_CR3_DMAT);
  1321. /* Enable the USART Transmit Complete Interrupt */
  1322. __HAL_USART_ENABLE_IT(husart, USART_IT_TC);
  1323. }
  1324. }
  1325. /* DMA Circular mode */
  1326. else
  1327. {
  1328. if(husart->State == HAL_USART_STATE_BUSY_TX)
  1329. {
  1330. HAL_USART_TxCpltCallback(husart);
  1331. }
  1332. }
  1333. }
  1334. /**
  1335. * @brief DMA USART transmit process half complete callback
  1336. * @param hdma : DMA handle
  1337. * @retval None
  1338. */
  1339. static void USART_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
  1340. {
  1341. USART_HandleTypeDef* husart = (USART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1342. HAL_USART_TxHalfCpltCallback(husart);
  1343. }
  1344. /**
  1345. * @brief DMA USART receive process complete callback
  1346. * @param hdma: DMA handle
  1347. * @retval None
  1348. */
  1349. static void USART_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
  1350. {
  1351. USART_HandleTypeDef* husart = ( USART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1352. /* DMA Normal mode */
  1353. if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0)
  1354. {
  1355. husart->RxXferCount = 0;
  1356. /* Disable the DMA RX transfer for the receiver request by resetting the DMAR bit
  1357. in USART CR3 register */
  1358. husart->Instance->CR3 &= (uint32_t)~((uint32_t)USART_CR3_DMAR);
  1359. /* similarly, disable the DMA TX transfer that was started to provide the
  1360. clock to the slave device */
  1361. husart->Instance->CR3 &= (uint32_t)~((uint32_t)USART_CR3_DMAT);
  1362. if(husart->State == HAL_USART_STATE_BUSY_RX)
  1363. {
  1364. HAL_USART_RxCpltCallback(husart);
  1365. }
  1366. /* The USART state is HAL_USART_STATE_BUSY_TX_RX */
  1367. else
  1368. {
  1369. HAL_USART_TxRxCpltCallback(husart);
  1370. }
  1371. husart->State= HAL_USART_STATE_READY;
  1372. }
  1373. /* DMA circular mode */
  1374. else
  1375. {
  1376. if(husart->State == HAL_USART_STATE_BUSY_RX)
  1377. {
  1378. HAL_USART_RxCpltCallback(husart);
  1379. }
  1380. /* The USART state is HAL_USART_STATE_BUSY_TX_RX */
  1381. else
  1382. {
  1383. HAL_USART_TxRxCpltCallback(husart);
  1384. }
  1385. }
  1386. }
  1387. /**
  1388. * @brief DMA USART receive process half complete callback
  1389. * @param hdma : DMA handle
  1390. * @retval None
  1391. */
  1392. static void USART_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
  1393. {
  1394. USART_HandleTypeDef* husart = (USART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent;
  1395. HAL_USART_RxHalfCpltCallback(husart);
  1396. }
  1397. /**
  1398. * @brief DMA USART communication error callback
  1399. * @param hdma: DMA handle
  1400. * @retval None
  1401. */
  1402. static void USART_DMAError(DMA_HandleTypeDef *hdma)
  1403. {
  1404. USART_HandleTypeDef* husart = ( USART_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1405. husart->RxXferCount = 0;
  1406. husart->TxXferCount = 0;
  1407. husart->ErrorCode |= HAL_USART_ERROR_DMA;
  1408. husart->State= HAL_USART_STATE_READY;
  1409. HAL_USART_ErrorCallback(husart);
  1410. }
  1411. /**
  1412. * @brief Configure the USART peripheral
  1413. * @param husart: USART handle
  1414. * @retval None
  1415. */
  1416. static HAL_StatusTypeDef USART_SetConfig(USART_HandleTypeDef *husart)
  1417. {
  1418. uint32_t tmpreg = 0x0;
  1419. USART_ClockSourceTypeDef clocksource = USART_CLOCKSOURCE_UNDEFINED;
  1420. HAL_StatusTypeDef ret = HAL_OK;
  1421. uint16_t brrtemp = 0x0000;
  1422. uint16_t usartdiv = 0x0000;
  1423. /* Check the parameters */
  1424. assert_param(IS_USART_POLARITY(husart->Init.CLKPolarity));
  1425. assert_param(IS_USART_PHASE(husart->Init.CLKPhase));
  1426. assert_param(IS_USART_LASTBIT(husart->Init.CLKLastBit));
  1427. assert_param(IS_USART_BAUDRATE(husart->Init.BaudRate));
  1428. assert_param(IS_USART_WORD_LENGTH(husart->Init.WordLength));
  1429. assert_param(IS_USART_STOPBITS(husart->Init.StopBits));
  1430. assert_param(IS_USART_PARITY(husart->Init.Parity));
  1431. assert_param(IS_USART_MODE(husart->Init.Mode));
  1432. assert_param(IS_USART_OVERSAMPLING(husart->Init.OverSampling));
  1433. /*-------------------------- USART CR1 Configuration -----------------------*/
  1434. /* Clear M, PCE, PS, TE and RE bits and configure
  1435. * the USART Word Length, Parity, Mode and OverSampling:
  1436. * set the M bits according to husart->Init.WordLength value
  1437. * set PCE and PS bits according to husart->Init.Parity value
  1438. * set TE and RE bits according to husart->Init.Mode value
  1439. * force OVER8 to 1 to allow to reach the maximum speed (Fclock/8) */
  1440. tmpreg = (uint32_t)husart->Init.WordLength | husart->Init.Parity | husart->Init.Mode | USART_CR1_OVER8;
  1441. MODIFY_REG(husart->Instance->CR1, USART_CR1_FIELDS, tmpreg);
  1442. /*---------------------------- USART CR2 Configuration ---------------------*/
  1443. /* Clear and configure the USART Clock, CPOL, CPHA, LBCL and STOP bits:
  1444. * set CPOL bit according to husart->Init.CLKPolarity value
  1445. * set CPHA bit according to husart->Init.CLKPhase value
  1446. * set LBCL bit according to husart->Init.CLKLastBit value
  1447. * set STOP[13:12] bits according to husart->Init.StopBits value */
  1448. tmpreg = (uint32_t)(USART_CLOCK_ENABLE);
  1449. tmpreg |= ((uint32_t)husart->Init.CLKPolarity | (uint32_t)husart->Init.CLKPhase);
  1450. tmpreg |= ((uint32_t)husart->Init.CLKLastBit | (uint32_t)husart->Init.StopBits);
  1451. MODIFY_REG(husart->Instance->CR2, USART_CR2_FIELDS, tmpreg);
  1452. /*-------------------------- USART CR3 Configuration -----------------------*/
  1453. /* no CR3 register configuration */
  1454. /*-------------------------- USART BRR Configuration -----------------------*/
  1455. /* BRR is filled-up according to OVER8 bit setting which is forced to 1 */
  1456. USART_GETCLOCKSOURCE(husart, clocksource);
  1457. switch (clocksource)
  1458. {
  1459. case USART_CLOCKSOURCE_PCLK1:
  1460. usartdiv = (uint16_t)((2*HAL_RCC_GetPCLK1Freq()) / husart->Init.BaudRate);
  1461. break;
  1462. case USART_CLOCKSOURCE_PCLK2:
  1463. usartdiv = (uint16_t)((2*HAL_RCC_GetPCLK2Freq()) / husart->Init.BaudRate);
  1464. break;
  1465. case USART_CLOCKSOURCE_HSI:
  1466. usartdiv = (uint16_t)((2*HSI_VALUE) / husart->Init.BaudRate);
  1467. break;
  1468. case USART_CLOCKSOURCE_SYSCLK:
  1469. usartdiv = (uint16_t)((2*HAL_RCC_GetSysClockFreq()) / husart->Init.BaudRate);
  1470. break;
  1471. case USART_CLOCKSOURCE_LSE:
  1472. usartdiv = (uint16_t)((2*LSE_VALUE) / husart->Init.BaudRate);
  1473. break;
  1474. case USART_CLOCKSOURCE_UNDEFINED:
  1475. default:
  1476. ret = HAL_ERROR;
  1477. break;
  1478. }
  1479. brrtemp = usartdiv & 0xFFF0;
  1480. brrtemp |= (uint16_t)((usartdiv & (uint16_t)0x000F) >> 1U);
  1481. husart->Instance->BRR = brrtemp;
  1482. return ret;
  1483. }
  1484. /**
  1485. * @brief Check the USART Idle State
  1486. * @param husart: USART handle
  1487. * @retval HAL status
  1488. */
  1489. static HAL_StatusTypeDef USART_CheckIdleState(USART_HandleTypeDef *husart)
  1490. {
  1491. /* Initialize the USART ErrorCode */
  1492. husart->ErrorCode = HAL_USART_ERROR_NONE;
  1493. /* Check if the Transmitter is enabled */
  1494. if((husart->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
  1495. {
  1496. /* Wait until TEACK flag is set */
  1497. if(USART_WaitOnFlagUntilTimeout(husart, USART_ISR_TEACK, RESET, TEACK_REACK_TIMEOUT) != HAL_OK)
  1498. {
  1499. husart->State= HAL_USART_STATE_TIMEOUT;
  1500. return HAL_TIMEOUT;
  1501. }
  1502. }
  1503. /* Check if the Receiver is enabled */
  1504. if((husart->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE)
  1505. {
  1506. /* Wait until REACK flag is set */
  1507. if(USART_WaitOnFlagUntilTimeout(husart, USART_ISR_REACK, RESET, TEACK_REACK_TIMEOUT) != HAL_OK)
  1508. {
  1509. husart->State= HAL_USART_STATE_TIMEOUT;
  1510. return HAL_TIMEOUT;
  1511. }
  1512. }
  1513. /* Initialize the USART state*/
  1514. husart->State= HAL_USART_STATE_READY;
  1515. /* Process Unlocked */
  1516. __HAL_UNLOCK(husart);
  1517. return HAL_OK;
  1518. }
  1519. /**
  1520. * @}
  1521. */
  1522. #endif /* HAL_USART_MODULE_ENABLED */
  1523. /**
  1524. * @}
  1525. */
  1526. /**
  1527. * @}
  1528. */
  1529. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/