stm32l4xx_hal_usart.c 101 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_usart.c
  4. * @author MCD Application Team
  5. * @brief USART HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Universal Synchronous/Asynchronous Receiver Transmitter
  8. * Peripheral (USART).
  9. * + Initialization and de-initialization functions
  10. * + IO operation functions
  11. * + Peripheral Control functions
  12. * + Peripheral State and Error functions
  13. *
  14. @verbatim
  15. ===============================================================================
  16. ##### How to use this driver #####
  17. ===============================================================================
  18. [..]
  19. The USART HAL driver can be used as follows:
  20. (#) Declare a USART_HandleTypeDef handle structure (eg. USART_HandleTypeDef husart).
  21. (#) Initialize the USART low level resources by implementing the HAL_USART_MspInit() API:
  22. (++) Enable the USARTx interface clock.
  23. (++) USART pins configuration:
  24. (+++) Enable the clock for the USART GPIOs.
  25. (+++) Configure these USART pins as alternate function pull-up.
  26. (++) NVIC configuration if you need to use interrupt process (HAL_USART_Transmit_IT(),
  27. HAL_USART_Receive_IT() and HAL_USART_TransmitReceive_IT() APIs):
  28. (+++) Configure the USARTx interrupt priority.
  29. (+++) Enable the NVIC USART IRQ handle.
  30. (++) USART interrupts handling:
  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_DMA() and HAL_USART_TransmitReceive_DMA() APIs):
  36. (+++) Declare a DMA handle structure for the Tx/Rx channel.
  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 channel.
  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 channel.
  42. (#) Program the Baud Rate, Word Length, Stop Bit, Parity, and Mode
  43. (Receiver/Transmitter) in the husart handle Init structure.
  44. (#) Initialize the USART registers by calling the HAL_USART_Init() API:
  45. (++) This API configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
  46. by calling the customized HAL_USART_MspInit(&husart) API.
  47. [..]
  48. (@) To configure and enable/disable the USART to wake up the MCU from stop mode, resort to UART API's
  49. HAL_UARTEx_StopModeWakeUpSourceConfig(), HAL_UARTEx_EnableStopMode() and
  50. HAL_UARTEx_DisableStopMode() in casting the USART handle to UART type UART_HandleTypeDef.
  51. @endverbatim
  52. ******************************************************************************
  53. * @attention
  54. *
  55. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  56. *
  57. * Redistribution and use in source and binary forms, with or without modification,
  58. * are permitted provided that the following conditions are met:
  59. * 1. Redistributions of source code must retain the above copyright notice,
  60. * this list of conditions and the following disclaimer.
  61. * 2. Redistributions in binary form must reproduce the above copyright notice,
  62. * this list of conditions and the following disclaimer in the documentation
  63. * and/or other materials provided with the distribution.
  64. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  65. * may be used to endorse or promote products derived from this software
  66. * without specific prior written permission.
  67. *
  68. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  69. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  70. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  71. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  72. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  73. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  74. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  75. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  76. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  77. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  78. *
  79. ******************************************************************************
  80. */
  81. /* Includes ------------------------------------------------------------------*/
  82. #include "stm32l4xx_hal.h"
  83. /** @addtogroup STM32L4xx_HAL_Driver
  84. * @{
  85. */
  86. /** @defgroup USART USART
  87. * @brief HAL USART Synchronous module driver
  88. * @{
  89. */
  90. #ifdef HAL_USART_MODULE_ENABLED
  91. /* Private typedef -----------------------------------------------------------*/
  92. /* Private define ------------------------------------------------------------*/
  93. /** @defgroup USART_Private_Constants USART Private Constants
  94. * @{
  95. */
  96. #define USART_DUMMY_DATA ((uint16_t) 0xFFFF) /*!< USART transmitted dummy data */
  97. #define USART_TEACK_REACK_TIMEOUT ((uint32_t) 1000) /*!< USART TX or RX enable acknowledge time-out value */
  98. #if defined(USART_CR1_FIFOEN)
  99. #define USART_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | \
  100. USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8 | \
  101. USART_CR1_FIFOEN )) /*!< USART CR1 fields of parameters set by USART_SetConfig API */
  102. #define USART_CR2_FIELDS ((uint32_t)(USART_CR2_CPHA | USART_CR2_CPOL | USART_CR2_CLKEN | \
  103. USART_CR2_LBCL | USART_CR2_STOP | USART_CR2_SLVEN | \
  104. USART_CR2_DIS_NSS)) /*!< USART CR2 fields of parameters set by USART_SetConfig API */
  105. #define USART_CR3_FIELDS ((uint32_t)(USART_CR3_TXFTCFG | USART_CR3_RXFTCFG )) /*!< USART or USART CR3 fields of parameters set by USART_SetConfig API */
  106. #else
  107. #define USART_CR1_FIELDS ((uint32_t)(USART_CR1_M | USART_CR1_PCE | USART_CR1_PS | \
  108. USART_CR1_TE | USART_CR1_RE | USART_CR1_OVER8)) /*!< USART CR1 fields of parameters set by USART_SetConfig API */
  109. #define USART_CR2_FIELDS ((uint32_t)(USART_CR2_CPHA | USART_CR2_CPOL | \
  110. USART_CR2_CLKEN | USART_CR2_LBCL | USART_CR2_STOP)) /*!< USART CR2 fields of parameters set by USART_SetConfig API */
  111. #endif
  112. #define USART_BRR_MIN 0x10U /* USART BRR minimum authorized value */
  113. /**
  114. * @}
  115. */
  116. /* Private macros ------------------------------------------------------------*/
  117. /* Private variables ---------------------------------------------------------*/
  118. /* Private function prototypes -----------------------------------------------*/
  119. /** @addtogroup USART_Private_Functions
  120. * @{
  121. */
  122. static void USART_EndTransfer(USART_HandleTypeDef *husart);
  123. static void USART_DMATransmitCplt(DMA_HandleTypeDef *hdma);
  124. static void USART_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
  125. static void USART_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
  126. static void USART_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
  127. static void USART_DMAError(DMA_HandleTypeDef *hdma);
  128. static void USART_DMAAbortOnError(DMA_HandleTypeDef *hdma);
  129. static void USART_DMATxAbortCallback(DMA_HandleTypeDef *hdma);
  130. static void USART_DMARxAbortCallback(DMA_HandleTypeDef *hdma);
  131. static HAL_StatusTypeDef USART_WaitOnFlagUntilTimeout(USART_HandleTypeDef *husart, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout);
  132. static HAL_StatusTypeDef USART_SetConfig(USART_HandleTypeDef *husart);
  133. static HAL_StatusTypeDef USART_CheckIdleState(USART_HandleTypeDef *husart);
  134. static void USART_TxISR_8BIT(USART_HandleTypeDef *husart);
  135. static void USART_TxISR_16BIT(USART_HandleTypeDef *husart);
  136. #if defined(USART_CR1_FIFOEN)
  137. static void USART_TxISR_8BIT_FIFOEN(USART_HandleTypeDef *husart);
  138. static void USART_TxISR_16BIT_FIFOEN(USART_HandleTypeDef *husart);
  139. #endif
  140. static void USART_EndTransmit_IT(USART_HandleTypeDef *husart);
  141. static void USART_RxISR_8BIT(USART_HandleTypeDef *husart);
  142. static void USART_RxISR_16BIT(USART_HandleTypeDef *husart);
  143. #if defined(USART_CR1_FIFOEN)
  144. static void USART_RxISR_8BIT_FIFOEN(USART_HandleTypeDef *husart);
  145. static void USART_RxISR_16BIT_FIFOEN(USART_HandleTypeDef *husart);
  146. #endif
  147. /**
  148. * @}
  149. */
  150. /* Exported functions --------------------------------------------------------*/
  151. /** @defgroup USART_Exported_Functions USART Exported Functions
  152. * @{
  153. */
  154. /** @defgroup USART_Exported_Functions_Group1 Initialization and de-initialization functions
  155. * @brief Initialization and Configuration functions
  156. *
  157. @verbatim
  158. ===============================================================================
  159. ##### Initialization and Configuration functions #####
  160. ===============================================================================
  161. [..]
  162. This subsection provides a set of functions allowing to initialize the USART
  163. in asynchronous and in synchronous modes.
  164. (+) For the asynchronous mode only these parameters can be configured:
  165. (++) Baud Rate
  166. (++) Word Length
  167. (++) Stop Bit
  168. (++) Parity: If the parity is enabled, then the MSB bit of the data written
  169. in the data register is transmitted but is changed by the parity bit.
  170. (++) USART polarity
  171. (++) USART phase
  172. (++) USART LastBit
  173. (++) Receiver/transmitter modes
  174. [..]
  175. The HAL_USART_Init() function follows the USART synchronous configuration
  176. procedure (details for the procedure are available in reference manual).
  177. @endverbatim
  178. Depending on the frame length defined by the M1 and M0 bits (7-bit,
  179. 8-bit or 9-bit), the possible USART formats are listed in the
  180. following table.
  181. Table 1. USART frame format.
  182. +-----------------------------------------------------------------------+
  183. | M1 bit | M0 bit | PCE bit | USART frame |
  184. |---------|---------|-----------|---------------------------------------|
  185. | 0 | 0 | 0 | | SB | 8 bit data | STB | |
  186. |---------|---------|-----------|---------------------------------------|
  187. | 0 | 0 | 1 | | SB | 7 bit data | PB | STB | |
  188. |---------|---------|-----------|---------------------------------------|
  189. | 0 | 1 | 0 | | SB | 9 bit data | STB | |
  190. |---------|---------|-----------|---------------------------------------|
  191. | 0 | 1 | 1 | | SB | 8 bit data | PB | STB | |
  192. |---------|---------|-----------|---------------------------------------|
  193. | 1 | 0 | 0 | | SB | 7 bit data | STB | |
  194. |---------|---------|-----------|---------------------------------------|
  195. | 1 | 0 | 1 | | SB | 6 bit data | PB | STB | |
  196. +-----------------------------------------------------------------------+
  197. * @{
  198. */
  199. /**
  200. * @brief Initialize the USART mode according to the specified
  201. * parameters in the USART_InitTypeDef and initialize the associated handle.
  202. * @param husart USART handle.
  203. * @retval HAL status
  204. */
  205. HAL_StatusTypeDef HAL_USART_Init(USART_HandleTypeDef *husart)
  206. {
  207. /* Check the USART handle allocation */
  208. if(husart == NULL)
  209. {
  210. return HAL_ERROR;
  211. }
  212. /* Check the parameters */
  213. assert_param(IS_USART_INSTANCE(husart->Instance));
  214. if(husart->State == HAL_USART_STATE_RESET)
  215. {
  216. /* Allocate lock resource and initialize it */
  217. husart->Lock = HAL_UNLOCKED;
  218. /* Init the low level hardware : GPIO, CLOCK */
  219. HAL_USART_MspInit(husart);
  220. }
  221. husart->State = HAL_USART_STATE_BUSY;
  222. /* Disable the Peripheral */
  223. __HAL_USART_DISABLE(husart);
  224. /* Set the Usart Communication parameters */
  225. if (USART_SetConfig(husart) == HAL_ERROR)
  226. {
  227. return HAL_ERROR;
  228. }
  229. /* In Synchronous mode, the following bits must be kept cleared:
  230. - LINEN bit in the USART_CR2 register
  231. - HDSEL, SCEN and IREN bits in the USART_CR3 register.*/
  232. husart->Instance->CR2 &= ~USART_CR2_LINEN;
  233. husart->Instance->CR3 &= ~(USART_CR3_SCEN | USART_CR3_HDSEL | USART_CR3_IREN);
  234. /* Enable the Peripheral */
  235. __HAL_USART_ENABLE(husart);
  236. /* TEACK and/or REACK to check before moving husart->State to Ready */
  237. return (USART_CheckIdleState(husart));
  238. }
  239. /**
  240. * @brief DeInitialize the USART peripheral.
  241. * @param husart USART handle.
  242. * @retval HAL status
  243. */
  244. HAL_StatusTypeDef HAL_USART_DeInit(USART_HandleTypeDef *husart)
  245. {
  246. /* Check the USART handle allocation */
  247. if(husart == NULL)
  248. {
  249. return HAL_ERROR;
  250. }
  251. /* Check the parameters */
  252. assert_param(IS_USART_INSTANCE(husart->Instance));
  253. husart->State = HAL_USART_STATE_BUSY;
  254. husart->Instance->CR1 = 0x0;
  255. husart->Instance->CR2 = 0x0;
  256. husart->Instance->CR3 = 0x0;
  257. /* DeInit the low level hardware */
  258. HAL_USART_MspDeInit(husart);
  259. husart->ErrorCode = HAL_USART_ERROR_NONE;
  260. husart->State = HAL_USART_STATE_RESET;
  261. /* Process Unlock */
  262. __HAL_UNLOCK(husart);
  263. return HAL_OK;
  264. }
  265. /**
  266. * @brief Initialize the USART MSP.
  267. * @param husart USART handle.
  268. * @retval None
  269. */
  270. __weak void HAL_USART_MspInit(USART_HandleTypeDef *husart)
  271. {
  272. /* Prevent unused argument(s) compilation warning */
  273. UNUSED(husart);
  274. /* NOTE : This function should not be modified, when the callback is needed,
  275. the HAL_USART_MspInit can be implemented in the user file
  276. */
  277. }
  278. /**
  279. * @brief DeInitialize the USART MSP.
  280. * @param husart USART handle.
  281. * @retval None
  282. */
  283. __weak void HAL_USART_MspDeInit(USART_HandleTypeDef *husart)
  284. {
  285. /* Prevent unused argument(s) compilation warning */
  286. UNUSED(husart);
  287. /* NOTE : This function should not be modified, when the callback is needed,
  288. the HAL_USART_MspDeInit can be implemented in the user file
  289. */
  290. }
  291. /**
  292. * @}
  293. */
  294. /** @defgroup USART_Exported_Functions_Group2 IO operation functions
  295. * @brief USART Transmit and Receive functions
  296. *
  297. @verbatim
  298. ===============================================================================
  299. ##### IO operation functions #####
  300. ===============================================================================
  301. [..] This subsection provides a set of functions allowing to manage the USART synchronous
  302. data transfers.
  303. [..] The USART supports master mode only: it cannot receive or send data related to an input
  304. clock (SCLK is always an output).
  305. (#) There are two modes of transfer:
  306. (++) Blocking mode: The communication is performed in polling mode.
  307. The HAL status of all data processing is returned by the same function
  308. after finishing transfer.
  309. (++) No-Blocking mode: The communication is performed using Interrupts
  310. or DMA, These API's return the HAL status.
  311. The end of the data processing will be indicated through the
  312. dedicated USART IRQ when using Interrupt mode or the DMA IRQ when
  313. using DMA mode.
  314. The HAL_USART_TxCpltCallback(), HAL_USART_RxCpltCallback() and HAL_USART_TxRxCpltCallback() user callbacks
  315. will be executed respectively at the end of the transmit or Receive process
  316. The HAL_USART_ErrorCallback()user callback will be executed when a communication error is detected
  317. (#) Blocking mode API's are :
  318. (++) HAL_USART_Transmit()in simplex mode
  319. (++) HAL_USART_Receive() in full duplex receive only
  320. (++) HAL_USART_TransmitReceive() in full duplex mode
  321. (#) Non-Blocking mode API's with Interrupt are :
  322. (++) HAL_USART_Transmit_IT()in simplex mode
  323. (++) HAL_USART_Receive_IT() in full duplex receive only
  324. (++) HAL_USART_TransmitReceive_IT()in full duplex mode
  325. (++) HAL_USART_IRQHandler()
  326. (#) No-Blocking mode API's with DMA are :
  327. (++) HAL_USART_Transmit_DMA()in simplex mode
  328. (++) HAL_USART_Receive_DMA() in full duplex receive only
  329. (++) HAL_USART_TransmitReceive_DMA() in full duplex mode
  330. (++) HAL_USART_DMAPause()
  331. (++) HAL_USART_DMAResume()
  332. (++) HAL_USART_DMAStop()
  333. (#) A set of Transfer Complete Callbacks are provided in Non_Blocking mode:
  334. (++) HAL_USART_TxCpltCallback()
  335. (++) HAL_USART_RxCpltCallback()
  336. (++) HAL_USART_TxHalfCpltCallback()
  337. (++) HAL_USART_RxHalfCpltCallback()
  338. (++) HAL_USART_ErrorCallback()
  339. (++) HAL_USART_TxRxCpltCallback()
  340. (#) Non-Blocking mode transfers could be aborted using Abort API's :
  341. (+) HAL_USART_Abort()
  342. (+) HAL_USART_Abort_IT()
  343. (#) For Abort services based on interrupts (HAL_USART_Abort_IT), a Abort Complete Callbacks is provided:
  344. (+) HAL_USART_AbortCpltCallback()
  345. (#) In Non-Blocking mode transfers, possible errors are split into 2 categories.
  346. Errors are handled as follows :
  347. (+) Error is considered as Recoverable and non blocking : Transfer could go till end, but error severity is
  348. to be evaluated by user : this concerns Frame Error, Parity Error or Noise Error in Interrupt mode reception .
  349. Received character is then retrieved and stored in Rx buffer, Error code is set to allow user to identify error type,
  350. and HAL_USART_ErrorCallback() user callback is executed. Transfer is kept ongoing on USART side.
  351. If user wants to abort it, Abort services should be called by user.
  352. (+) Error is considered as Blocking : Transfer could not be completed properly and is aborted.
  353. This concerns Overrun Error In Interrupt mode reception and all errors in DMA mode.
  354. Error code is set to allow user to identify error type, and HAL_USART_ErrorCallback() user callback is executed.
  355. @endverbatim
  356. * @{
  357. */
  358. /**
  359. * @brief Simplex send an amount of data in blocking mode.
  360. * @param husart USART handle.
  361. * @param pTxData Pointer to data buffer.
  362. * @param Size Amount of data to be sent.
  363. * @param Timeout Timeout duration.
  364. * @retval HAL status
  365. */
  366. HAL_StatusTypeDef HAL_USART_Transmit(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size, uint32_t Timeout)
  367. {
  368. uint16_t* tmp;
  369. uint32_t tickstart = 0;
  370. if(husart->State == HAL_USART_STATE_READY)
  371. {
  372. if((pTxData == NULL) || (Size == 0))
  373. {
  374. return HAL_ERROR;
  375. }
  376. /* Process Locked */
  377. __HAL_LOCK(husart);
  378. husart->ErrorCode = HAL_USART_ERROR_NONE;
  379. husart->State = HAL_USART_STATE_BUSY_TX;
  380. /* Init tickstart for timeout managment*/
  381. tickstart = HAL_GetTick();
  382. husart->TxXferSize = Size;
  383. husart->TxXferCount = Size;
  384. /* Check the remaining data to be sent */
  385. while(husart->TxXferCount > 0)
  386. {
  387. if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
  388. {
  389. return HAL_TIMEOUT;
  390. }
  391. if((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  392. {
  393. tmp = (uint16_t*) pTxData;
  394. husart->Instance->TDR = (*tmp & (uint16_t)0x01FF);
  395. pTxData += 2;
  396. }
  397. else
  398. {
  399. husart->Instance->TDR = (*pTxData++ & (uint8_t)0xFF);
  400. }
  401. husart->TxXferCount--;
  402. }
  403. if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TC, RESET, tickstart, Timeout) != HAL_OK)
  404. {
  405. return HAL_TIMEOUT;
  406. }
  407. /* Clear Transmission Complete Flag */
  408. __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_TCF);
  409. /* Clear overrun flag and discard the received data */
  410. __HAL_USART_CLEAR_OREFLAG(husart);
  411. __HAL_USART_SEND_REQ(husart, USART_RXDATA_FLUSH_REQUEST);
  412. __HAL_USART_SEND_REQ(husart, USART_TXDATA_FLUSH_REQUEST);
  413. /* At end of Tx process, restore husart->State to Ready */
  414. husart->State = HAL_USART_STATE_READY;
  415. /* Process Unlocked */
  416. __HAL_UNLOCK(husart);
  417. return HAL_OK;
  418. }
  419. else
  420. {
  421. return HAL_BUSY;
  422. }
  423. }
  424. /**
  425. * @brief Receive an amount of data in blocking mode.
  426. * @note To receive synchronous data, dummy data are simultaneously transmitted.
  427. * @param husart USART handle.
  428. * @param pRxData Pointer to data buffer.
  429. * @param Size Amount of data to be received.
  430. * @param Timeout Timeout duration.
  431. * @retval HAL status
  432. */
  433. HAL_StatusTypeDef HAL_USART_Receive(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size, uint32_t Timeout)
  434. {
  435. uint16_t* tmp;
  436. uint16_t uhMask;
  437. uint32_t tickstart = 0;
  438. if(husart->State == HAL_USART_STATE_READY)
  439. {
  440. if((pRxData == NULL) || (Size == 0))
  441. {
  442. return HAL_ERROR;
  443. }
  444. /* Process Locked */
  445. __HAL_LOCK(husart);
  446. husart->ErrorCode = HAL_USART_ERROR_NONE;
  447. husart->State = HAL_USART_STATE_BUSY_RX;
  448. /* Init tickstart for timeout managment*/
  449. tickstart = HAL_GetTick();
  450. husart->RxXferSize = Size;
  451. husart->RxXferCount = Size;
  452. /* Computation of USART mask to apply to RDR register */
  453. USART_MASK_COMPUTATION(husart);
  454. uhMask = husart->Mask;
  455. /* as long as data have to be received */
  456. while(husart->RxXferCount > 0)
  457. {
  458. #if defined(USART_CR2_SLVEN)
  459. if (husart->SlaveMode == USART_SLAVEMODE_DISABLE)
  460. #endif
  461. {
  462. /* Wait until TXE flag is set to send dummy byte in order to generate the
  463. * clock for the slave to send data.
  464. * Whatever the frame length (7, 8 or 9-bit long), the same dummy value
  465. * can be written for all the cases. */
  466. if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
  467. {
  468. return HAL_TIMEOUT;
  469. }
  470. husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x0FF);
  471. }
  472. /* Wait for RXNE Flag */
  473. if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
  474. {
  475. return HAL_TIMEOUT;
  476. }
  477. if((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  478. {
  479. tmp = (uint16_t*) pRxData ;
  480. *tmp = (uint16_t)(husart->Instance->RDR & uhMask);
  481. pRxData +=2;
  482. }
  483. else
  484. {
  485. *pRxData++ = (uint8_t)(husart->Instance->RDR & (uint8_t)uhMask);
  486. }
  487. husart->RxXferCount--;
  488. }
  489. #if defined(USART_CR2_SLVEN)
  490. /* Clear SPI slave underrun flag and discard transmit data */
  491. if (husart->SlaveMode == USART_SLAVEMODE_ENABLE)
  492. {
  493. __HAL_USART_CLEAR_UDRFLAG(husart);
  494. __HAL_USART_SEND_REQ(husart, USART_TXDATA_FLUSH_REQUEST);
  495. }
  496. #endif
  497. /* At end of Rx process, restore husart->State to Ready */
  498. husart->State = HAL_USART_STATE_READY;
  499. /* Process Unlocked */
  500. __HAL_UNLOCK(husart);
  501. return HAL_OK;
  502. }
  503. else
  504. {
  505. return HAL_BUSY;
  506. }
  507. }
  508. /**
  509. * @brief Full-Duplex Send and Receive an amount of data in blocking mode.
  510. * @param husart USART handle.
  511. * @param pTxData pointer to TX data buffer.
  512. * @param pRxData pointer to RX data buffer.
  513. * @param Size amount of data to be sent (same amount to be received).
  514. * @param Timeout Timeout duration.
  515. * @retval HAL status
  516. */
  517. HAL_StatusTypeDef HAL_USART_TransmitReceive(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size, uint32_t Timeout)
  518. {
  519. uint16_t* tmp;
  520. uint16_t uhMask;
  521. uint32_t tickstart = 0;
  522. if(husart->State == HAL_USART_STATE_READY)
  523. {
  524. if((pTxData == NULL) || (pRxData == NULL) || (Size == 0))
  525. {
  526. return HAL_ERROR;
  527. }
  528. /* Process Locked */
  529. __HAL_LOCK(husart);
  530. husart->ErrorCode = HAL_USART_ERROR_NONE;
  531. husart->State = HAL_USART_STATE_BUSY_RX;
  532. /* Init tickstart for timeout managment*/
  533. tickstart = HAL_GetTick();
  534. husart->RxXferSize = Size;
  535. husart->TxXferSize = Size;
  536. husart->TxXferCount = Size;
  537. husart->RxXferCount = Size;
  538. /* Computation of USART mask to apply to RDR register */
  539. USART_MASK_COMPUTATION(husart);
  540. uhMask = husart->Mask;
  541. #if defined(USART_CR2_SLVEN)
  542. if ((husart->SlaveMode == USART_SLAVEMODE_ENABLE) || (husart->TxXferCount == 0x01U))
  543. #else
  544. if (husart->TxXferCount == 0x01U)
  545. #endif
  546. {
  547. /* Wait until TXE flag is set to send data */
  548. if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
  549. {
  550. return HAL_TIMEOUT;
  551. }
  552. if((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  553. {
  554. tmp = (uint16_t*) pTxData;
  555. husart->Instance->TDR = (*tmp & uhMask);
  556. pTxData += 2U;
  557. }
  558. else
  559. {
  560. husart->Instance->TDR = (*pTxData++ & (uint8_t)uhMask);
  561. }
  562. husart->TxXferCount--;
  563. }
  564. /* Check the remain data to be sent */
  565. while ((husart->TxXferCount > 0) || (husart->RxXferCount > 0))
  566. {
  567. if (husart->TxXferCount > 0)
  568. {
  569. /* Wait until TXE flag is set to send data */
  570. if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_TXE, RESET, tickstart, Timeout) != HAL_OK)
  571. {
  572. return HAL_TIMEOUT;
  573. }
  574. if((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  575. {
  576. tmp = (uint16_t*) pTxData;
  577. husart->Instance->TDR = (*tmp & uhMask);
  578. pTxData += 2;
  579. }
  580. else
  581. {
  582. husart->Instance->TDR = (*pTxData++ & (uint8_t)uhMask);
  583. }
  584. husart->TxXferCount--;
  585. }
  586. if (husart->RxXferCount > 0)
  587. {
  588. /* Wait for RXNE Flag */
  589. if(USART_WaitOnFlagUntilTimeout(husart, USART_FLAG_RXNE, RESET, tickstart, Timeout) != HAL_OK)
  590. {
  591. return HAL_TIMEOUT;
  592. }
  593. if((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  594. {
  595. tmp = (uint16_t*) pRxData ;
  596. *tmp = (uint16_t)(husart->Instance->RDR & uhMask);
  597. pRxData +=2;
  598. }
  599. else
  600. {
  601. *pRxData++ = (uint8_t)(husart->Instance->RDR & (uint8_t)uhMask);
  602. }
  603. husart->RxXferCount--;
  604. }
  605. }
  606. /* At end of TxRx process, restore husart->State to Ready */
  607. husart->State = HAL_USART_STATE_READY;
  608. /* Process Unlocked */
  609. __HAL_UNLOCK(husart);
  610. return HAL_OK;
  611. }
  612. else
  613. {
  614. return HAL_BUSY;
  615. }
  616. }
  617. /**
  618. * @brief Send an amount of data in interrupt mode.
  619. * @param husart USART handle.
  620. * @param pTxData pointer to data buffer.
  621. * @param Size amount of data to be sent.
  622. * @retval HAL status
  623. */
  624. HAL_StatusTypeDef HAL_USART_Transmit_IT(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size)
  625. {
  626. if(husart->State == HAL_USART_STATE_READY)
  627. {
  628. if((pTxData == NULL ) || (Size == 0))
  629. {
  630. return HAL_ERROR;
  631. }
  632. /* Process Locked */
  633. __HAL_LOCK(husart);
  634. husart->pTxBuffPtr = pTxData;
  635. husart->TxXferSize = Size;
  636. husart->TxXferCount = Size;
  637. husart->TxISR = NULL;
  638. husart->ErrorCode = HAL_USART_ERROR_NONE;
  639. husart->State = HAL_USART_STATE_BUSY_TX;
  640. /* The USART Error Interrupts: (Frame error, noise error, overrun error)
  641. are not managed by the USART Transmit Process to avoid the overrun interrupt
  642. when the usart mode is configured for transmit and receive "USART_MODE_TX_RX"
  643. to benefit for the frame error and noise interrupts the usart mode should be
  644. configured only for transmit "USART_MODE_TX" */
  645. #if defined(USART_CR1_FIFOEN)
  646. /* Configure Tx interrupt processing */
  647. if (husart->FifoMode == USART_FIFOMODE_ENABLE)
  648. {
  649. /* Set the Tx ISR function pointer according to the data word length */
  650. if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  651. {
  652. husart->TxISR = USART_TxISR_16BIT_FIFOEN;
  653. }
  654. else
  655. {
  656. husart->TxISR = USART_TxISR_8BIT_FIFOEN;
  657. }
  658. /* Process Unlocked */
  659. __HAL_UNLOCK(husart);
  660. /* Enable the TX FIFO threshold interrupt */
  661. __HAL_USART_ENABLE_IT(husart, USART_IT_TXFT);
  662. }
  663. else
  664. #endif
  665. {
  666. /* Set the Tx ISR function pointer according to the data word length */
  667. if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  668. {
  669. husart->TxISR = USART_TxISR_16BIT;
  670. }
  671. else
  672. {
  673. husart->TxISR = USART_TxISR_8BIT;
  674. }
  675. /* Process Unlocked */
  676. __HAL_UNLOCK(husart);
  677. /* Enable the USART Transmit Data Register Empty Interrupt */
  678. __HAL_USART_ENABLE_IT(husart, USART_IT_TXE);
  679. }
  680. return HAL_OK;
  681. }
  682. else
  683. {
  684. return HAL_BUSY;
  685. }
  686. }
  687. /**
  688. * @brief Receive an amount of data in blocking mode.
  689. * @note To receive synchronous data, dummy data are simultaneously transmitted.
  690. * @param husart USART handle.
  691. * @param pRxData pointer to data buffer.
  692. * @param Size amount of data to be received.
  693. * @retval HAL status
  694. */
  695. HAL_StatusTypeDef HAL_USART_Receive_IT(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size)
  696. {
  697. #if defined(USART_CR1_FIFOEN)
  698. uint8_t nb_dummy_data;
  699. #endif
  700. if(husart->State == HAL_USART_STATE_READY)
  701. {
  702. if((pRxData == NULL ) || (Size == 0))
  703. {
  704. return HAL_ERROR;
  705. }
  706. /* Process Locked */
  707. __HAL_LOCK(husart);
  708. husart->pRxBuffPtr = pRxData;
  709. husart->RxXferSize = Size;
  710. husart->RxXferCount = Size;
  711. husart->RxISR = NULL;
  712. USART_MASK_COMPUTATION(husart);
  713. husart->ErrorCode = HAL_USART_ERROR_NONE;
  714. husart->State = HAL_USART_STATE_BUSY_RX;
  715. /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  716. SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
  717. #if defined(USART_CR1_FIFOEN)
  718. /* Configure Rx interrupt processing */
  719. if ((husart->FifoMode == USART_FIFOMODE_ENABLE) && (Size >= husart->NbRxDataToProcess))
  720. {
  721. /* Set the Rx ISR function pointer according to the data word length */
  722. if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  723. {
  724. husart->RxISR = USART_RxISR_16BIT_FIFOEN;
  725. }
  726. else
  727. {
  728. husart->RxISR = USART_RxISR_8BIT_FIFOEN;
  729. }
  730. /* Process Unlocked */
  731. __HAL_UNLOCK(husart);
  732. /* Enable the USART Parity Error interrupt and RX FIFO Threshold interrupt */
  733. SET_BIT(husart->Instance->CR1, USART_CR1_PEIE);
  734. SET_BIT(husart->Instance->CR3, USART_CR3_RXFTIE);
  735. }
  736. else
  737. #endif
  738. {
  739. /* Set the Rx ISR function pointer according to the data word length */
  740. if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  741. {
  742. husart->RxISR = USART_RxISR_16BIT;
  743. }
  744. else
  745. {
  746. husart->RxISR = USART_RxISR_8BIT;
  747. }
  748. /* Process Unlocked */
  749. __HAL_UNLOCK(husart);
  750. /* Enable the USART Parity Error and Data Register not empty Interrupts */
  751. #if defined(USART_CR1_FIFOEN)
  752. SET_BIT(husart->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE_RXFNEIE);
  753. #else
  754. SET_BIT(husart->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE);
  755. #endif
  756. }
  757. #if defined(USART_CR2_SLVEN)
  758. if (husart->SlaveMode == USART_SLAVEMODE_DISABLE)
  759. #endif
  760. {
  761. /* Send dummy data in order to generate the clock for the Slave to send the next data.
  762. When FIFO mode is disabled only one data must be transferred.
  763. When FIFO mode is enabled data must be transmitted until the RX FIFO reaches its threshold.
  764. */
  765. #if defined(USART_CR1_FIFOEN)
  766. if ((husart->FifoMode == USART_FIFOMODE_ENABLE) && (Size >= husart->NbRxDataToProcess))
  767. {
  768. for (nb_dummy_data = husart->NbRxDataToProcess ; nb_dummy_data > 0 ; nb_dummy_data--)
  769. {
  770. husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x00FF);
  771. }
  772. }
  773. else
  774. #endif
  775. {
  776. husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x00FF);
  777. }
  778. }
  779. return HAL_OK;
  780. }
  781. else
  782. {
  783. return HAL_BUSY;
  784. }
  785. }
  786. /**
  787. * @brief Full-Duplex Send and Receive an amount of data in interrupt mode.
  788. * @param husart USART handle.
  789. * @param pTxData pointer to TX data buffer.
  790. * @param pRxData pointer to RX data buffer.
  791. * @param Size amount of data to be sent (same amount to be received).
  792. * @retval HAL status
  793. */
  794. HAL_StatusTypeDef HAL_USART_TransmitReceive_IT(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size)
  795. {
  796. if(husart->State == HAL_USART_STATE_READY)
  797. {
  798. if((pTxData == NULL) || (pRxData == NULL) || (Size == 0))
  799. {
  800. return HAL_ERROR;
  801. }
  802. /* Process Locked */
  803. __HAL_LOCK(husart);
  804. husart->pRxBuffPtr = pRxData;
  805. husart->RxXferSize = Size;
  806. husart->RxXferCount = Size;
  807. husart->pTxBuffPtr = pTxData;
  808. husart->TxXferSize = Size;
  809. husart->TxXferCount = Size;
  810. /* Computation of USART mask to apply to RDR register */
  811. USART_MASK_COMPUTATION(husart);
  812. husart->ErrorCode = HAL_USART_ERROR_NONE;
  813. husart->State = HAL_USART_STATE_BUSY_TX_RX;
  814. #if defined(USART_CR1_FIFOEN)
  815. /* Configure TxRx interrupt processing */
  816. if ((husart->FifoMode == USART_FIFOMODE_ENABLE) && (Size >= husart->NbRxDataToProcess))
  817. {
  818. /* Set the Rx ISR function pointer according to the data word length */
  819. if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  820. {
  821. husart->TxISR = USART_TxISR_16BIT_FIFOEN;
  822. husart->RxISR = USART_RxISR_16BIT_FIFOEN;
  823. }
  824. else
  825. {
  826. husart->TxISR = USART_TxISR_8BIT_FIFOEN;
  827. husart->RxISR = USART_RxISR_8BIT_FIFOEN;
  828. }
  829. /* Process Locked */
  830. __HAL_UNLOCK(husart);
  831. /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  832. SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
  833. /* Enable the USART Parity Error interrupt */
  834. SET_BIT(husart->Instance->CR1, USART_CR1_PEIE);
  835. /* Enable the TX and RX FIFO Threshold interrupts */
  836. SET_BIT(husart->Instance->CR3, (USART_CR3_TXFTIE | USART_CR3_RXFTIE));
  837. }
  838. else
  839. #endif
  840. {
  841. if ((husart->Init.WordLength == USART_WORDLENGTH_9B) && (husart->Init.Parity == USART_PARITY_NONE))
  842. {
  843. husart->TxISR = USART_TxISR_16BIT;
  844. husart->RxISR = USART_RxISR_16BIT;
  845. }
  846. else
  847. {
  848. husart->TxISR = USART_TxISR_8BIT;
  849. husart->RxISR = USART_RxISR_8BIT;
  850. }
  851. /* Process Locked */
  852. __HAL_UNLOCK(husart);
  853. /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  854. SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
  855. /* Enable the USART Parity Error and USART Data Register not empty Interrupts */
  856. #if defined(USART_CR1_FIFOEN)
  857. SET_BIT(husart->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE_RXFNEIE);
  858. #else
  859. SET_BIT(husart->Instance->CR1, USART_CR1_PEIE | USART_CR1_RXNEIE);
  860. #endif
  861. /* Enable the USART Transmit Data Register Empty Interrupt */
  862. #if defined(USART_CR1_FIFOEN)
  863. SET_BIT(husart->Instance->CR1, USART_CR1_TXEIE_TXFNFIE);
  864. #else
  865. SET_BIT(husart->Instance->CR1, USART_CR1_TXEIE);
  866. #endif
  867. }
  868. return HAL_OK;
  869. }
  870. else
  871. {
  872. return HAL_BUSY;
  873. }
  874. }
  875. /**
  876. * @brief Send an amount of data in DMA mode.
  877. * @param husart USART handle.
  878. * @param pTxData pointer to data buffer.
  879. * @param Size amount of data to be sent.
  880. * @retval HAL status
  881. */
  882. HAL_StatusTypeDef HAL_USART_Transmit_DMA(USART_HandleTypeDef *husart, uint8_t *pTxData, uint16_t Size)
  883. {
  884. uint32_t *tmp;
  885. if(husart->State == HAL_USART_STATE_READY)
  886. {
  887. if((pTxData == NULL ) || (Size == 0))
  888. {
  889. return HAL_ERROR;
  890. }
  891. /* Process Locked */
  892. __HAL_LOCK(husart);
  893. husart->pTxBuffPtr = pTxData;
  894. husart->TxXferSize = Size;
  895. husart->TxXferCount = Size;
  896. husart->ErrorCode = HAL_USART_ERROR_NONE;
  897. husart->State = HAL_USART_STATE_BUSY_TX;
  898. /* Set the USART DMA transfer complete callback */
  899. husart->hdmatx->XferCpltCallback = USART_DMATransmitCplt;
  900. /* Set the USART DMA Half transfer complete callback */
  901. husart->hdmatx->XferHalfCpltCallback = USART_DMATxHalfCplt;
  902. /* Set the DMA error callback */
  903. husart->hdmatx->XferErrorCallback = USART_DMAError;
  904. /* Enable the USART transmit DMA channel */
  905. tmp = (uint32_t*)&pTxData;
  906. HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t*)tmp, (uint32_t)&husart->Instance->TDR, Size);
  907. /* Clear the TC flag in the ICR register */
  908. __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_TCF);
  909. /* Process Unlocked */
  910. __HAL_UNLOCK(husart);
  911. /* Enable the DMA transfer for transmit request by setting the DMAT bit
  912. in the USART CR3 register */
  913. SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  914. return HAL_OK;
  915. }
  916. else
  917. {
  918. return HAL_BUSY;
  919. }
  920. }
  921. /**
  922. * @brief Receive an amount of data in DMA mode.
  923. * @param husart USART handle.
  924. * @param pRxData pointer to data buffer.
  925. * @param Size amount of data to be received.
  926. * @note When the USART parity is enabled (PCE = 1), the received data contain
  927. * the parity bit (MSB position).
  928. * @note The USART DMA transmit channel must be configured in order to generate the clock for the slave.
  929. * @retval HAL status
  930. */
  931. HAL_StatusTypeDef HAL_USART_Receive_DMA(USART_HandleTypeDef *husart, uint8_t *pRxData, uint16_t Size)
  932. {
  933. uint32_t *tmp;
  934. /* Check that a Rx process is not already ongoing */
  935. if(husart->State == HAL_USART_STATE_READY)
  936. {
  937. if((pRxData == NULL ) || (Size == 0))
  938. {
  939. return HAL_ERROR;
  940. }
  941. /* Process Locked */
  942. __HAL_LOCK(husart);
  943. husart->pRxBuffPtr = pRxData;
  944. husart->RxXferSize = Size;
  945. husart->pTxBuffPtr = pRxData;
  946. husart->TxXferSize = Size;
  947. husart->ErrorCode = HAL_USART_ERROR_NONE;
  948. husart->State = HAL_USART_STATE_BUSY_RX;
  949. /* Set the USART DMA Rx transfer complete callback */
  950. husart->hdmarx->XferCpltCallback = USART_DMAReceiveCplt;
  951. /* Set the USART DMA Half transfer complete callback */
  952. husart->hdmarx->XferHalfCpltCallback = USART_DMARxHalfCplt;
  953. /* Set the USART DMA Rx transfer error callback */
  954. husart->hdmarx->XferErrorCallback = USART_DMAError;
  955. /* Enable the USART receive DMA channel */
  956. tmp = (uint32_t*)&pRxData;
  957. HAL_DMA_Start_IT(husart->hdmarx, (uint32_t)&husart->Instance->RDR, *(uint32_t*)tmp, Size);
  958. #if defined(USART_CR2_SLVEN)
  959. if (husart->SlaveMode == USART_SLAVEMODE_DISABLE)
  960. {
  961. /* Enable the USART transmit DMA channel: the transmit channel is used in order
  962. to generate in the non-blocking mode the clock to the slave device,
  963. this mode isn't a simplex receive mode but a full-duplex receive mode */
  964. tmp = (uint32_t*)&pRxData;
  965. /* Set the USART DMA Tx Complete and Error callback to Null */
  966. husart->hdmatx->XferErrorCallback = NULL;
  967. husart->hdmatx->XferHalfCpltCallback = NULL;
  968. husart->hdmatx->XferCpltCallback = NULL;
  969. HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t*)tmp, (uint32_t)&husart->Instance->TDR, Size);
  970. }
  971. #endif
  972. /* Process Unlocked */
  973. __HAL_UNLOCK(husart);
  974. /* Enable the USART Parity Error Interrupt */
  975. SET_BIT(husart->Instance->CR1, USART_CR1_PEIE);
  976. /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  977. SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
  978. /* Enable the DMA transfer for the receiver request by setting the DMAR bit
  979. in the USART CR3 register */
  980. SET_BIT(husart->Instance->CR3, USART_CR3_DMAR);
  981. /* Enable the DMA transfer for transmit request by setting the DMAT bit
  982. in the USART CR3 register */
  983. SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  984. return HAL_OK;
  985. }
  986. else
  987. {
  988. return HAL_BUSY;
  989. }
  990. }
  991. /**
  992. * @brief Full-Duplex Transmit Receive an amount of data in non-blocking mode.
  993. * @param husart USART handle.
  994. * @param pTxData pointer to TX data buffer.
  995. * @param pRxData pointer to RX data buffer.
  996. * @param Size amount of data to be received/sent.
  997. * @note When the USART parity is enabled (PCE = 1) the data received contain the parity bit.
  998. * @retval HAL status
  999. */
  1000. HAL_StatusTypeDef HAL_USART_TransmitReceive_DMA(USART_HandleTypeDef *husart, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size)
  1001. {
  1002. uint32_t *tmp;
  1003. if(husart->State == HAL_USART_STATE_READY)
  1004. {
  1005. if((pTxData == NULL) || (pRxData == NULL) || (Size == 0))
  1006. {
  1007. return HAL_ERROR;
  1008. }
  1009. /* Process Locked */
  1010. __HAL_LOCK(husart);
  1011. husart->pRxBuffPtr = pRxData;
  1012. husart->RxXferSize = Size;
  1013. husart->pTxBuffPtr = pTxData;
  1014. husart->TxXferSize = Size;
  1015. husart->ErrorCode = HAL_USART_ERROR_NONE;
  1016. husart->State = HAL_USART_STATE_BUSY_TX_RX;
  1017. /* Set the USART DMA Rx transfer complete callback */
  1018. husart->hdmarx->XferCpltCallback = USART_DMAReceiveCplt;
  1019. /* Set the USART DMA Half transfer complete callback */
  1020. husart->hdmarx->XferHalfCpltCallback = USART_DMARxHalfCplt;
  1021. /* Set the USART DMA Tx transfer complete callback */
  1022. husart->hdmatx->XferCpltCallback = USART_DMATransmitCplt;
  1023. /* Set the USART DMA Half transfer complete callback */
  1024. husart->hdmatx->XferHalfCpltCallback = USART_DMATxHalfCplt;
  1025. /* Set the USART DMA Tx transfer error callback */
  1026. husart->hdmatx->XferErrorCallback = USART_DMAError;
  1027. /* Set the USART DMA Rx transfer error callback */
  1028. husart->hdmarx->XferErrorCallback = USART_DMAError;
  1029. /* Enable the USART receive DMA channel */
  1030. tmp = (uint32_t*)&pRxData;
  1031. HAL_DMA_Start_IT(husart->hdmarx, (uint32_t)&husart->Instance->RDR, *(uint32_t*)tmp, Size);
  1032. /* Enable the USART transmit DMA channel */
  1033. tmp = (uint32_t*)&pTxData;
  1034. HAL_DMA_Start_IT(husart->hdmatx, *(uint32_t*)tmp, (uint32_t)&husart->Instance->TDR, Size);
  1035. /* Process Unlocked */
  1036. __HAL_UNLOCK(husart);
  1037. /* Enable the USART Parity Error Interrupt */
  1038. SET_BIT(husart->Instance->CR1, USART_CR1_PEIE);
  1039. /* Enable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  1040. SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
  1041. /* Clear the TC flag in the ICR register */
  1042. __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_TCF);
  1043. /* Enable the DMA transfer for the receiver request by setting the DMAR bit
  1044. in the USART CR3 register */
  1045. SET_BIT(husart->Instance->CR3, USART_CR3_DMAR);
  1046. /* Enable the DMA transfer for transmit request by setting the DMAT bit
  1047. in the USART CR3 register */
  1048. SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  1049. return HAL_OK;
  1050. }
  1051. else
  1052. {
  1053. return HAL_BUSY;
  1054. }
  1055. }
  1056. /**
  1057. * @brief Pause the DMA Transfer.
  1058. * @param husart USART handle.
  1059. * @retval HAL status
  1060. */
  1061. HAL_StatusTypeDef HAL_USART_DMAPause(USART_HandleTypeDef *husart)
  1062. {
  1063. /* Process Locked */
  1064. __HAL_LOCK(husart);
  1065. if( (husart->State == HAL_USART_STATE_BUSY_TX) &&
  1066. (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT)))
  1067. {
  1068. /* Disable the USART DMA Tx request */
  1069. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  1070. }
  1071. else if( (husart->State == HAL_USART_STATE_BUSY_RX) ||
  1072. (husart->State == HAL_USART_STATE_BUSY_TX_RX) )
  1073. {
  1074. if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT))
  1075. {
  1076. /* Disable the USART DMA Tx request */
  1077. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  1078. }
  1079. if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR))
  1080. {
  1081. /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
  1082. CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE);
  1083. CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
  1084. /* Disable the USART DMA Rx request */
  1085. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
  1086. }
  1087. }
  1088. /* Process Unlocked */
  1089. __HAL_UNLOCK(husart);
  1090. return HAL_OK;
  1091. }
  1092. /**
  1093. * @brief Resume the DMA Transfer.
  1094. * @param husart USART handle.
  1095. * @retval HAL status
  1096. */
  1097. HAL_StatusTypeDef HAL_USART_DMAResume(USART_HandleTypeDef *husart)
  1098. {
  1099. /* Process Locked */
  1100. __HAL_LOCK(husart);
  1101. if(husart->State == HAL_USART_STATE_BUSY_TX)
  1102. {
  1103. /* Enable the USART DMA Tx request */
  1104. SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  1105. }
  1106. else if( (husart->State == HAL_USART_STATE_BUSY_RX) ||
  1107. (husart->State == HAL_USART_STATE_BUSY_TX_RX) )
  1108. {
  1109. /* Clear the Overrun flag before resuming the Rx transfer*/
  1110. __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_OREF);
  1111. /* Reenable PE and ERR (Frame error, noise error, overrun error) interrupts */
  1112. SET_BIT(husart->Instance->CR1, USART_CR1_PEIE);
  1113. SET_BIT(husart->Instance->CR3, USART_CR3_EIE);
  1114. /* Enable the USART DMA Rx request before the DMA Tx request */
  1115. SET_BIT(husart->Instance->CR3, USART_CR3_DMAR);
  1116. /* Enable the USART DMA Tx request */
  1117. SET_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  1118. }
  1119. /* Process Unlocked */
  1120. __HAL_UNLOCK(husart);
  1121. return HAL_OK;
  1122. }
  1123. /**
  1124. * @brief Stop the DMA Transfer.
  1125. * @param husart USART handle.
  1126. * @retval HAL status
  1127. */
  1128. HAL_StatusTypeDef HAL_USART_DMAStop(USART_HandleTypeDef *husart)
  1129. {
  1130. /* The Lock is not implemented on this API to allow the user application
  1131. to call the HAL USART API under callbacks HAL_USART_TxCpltCallback() / HAL_USART_RxCpltCallback() /
  1132. HAL_USART_TxHalfCpltCallback / HAL_USART_RxHalfCpltCallback:
  1133. indeed, when HAL_DMA_Abort() API is called, the DMA TX/RX Transfer or Half Transfer complete
  1134. interrupt is generated if the DMA transfer interruption occurs at the middle or at the end of
  1135. the stream and the corresponding call back is executed. */
  1136. /* Disable the USART Tx/Rx DMA requests */
  1137. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  1138. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
  1139. /* Abort the USART DMA tx channel */
  1140. if(husart->hdmatx != NULL)
  1141. {
  1142. HAL_DMA_Abort(husart->hdmatx);
  1143. }
  1144. /* Abort the USART DMA rx channel */
  1145. if(husart->hdmarx != NULL)
  1146. {
  1147. HAL_DMA_Abort(husart->hdmarx);
  1148. }
  1149. USART_EndTransfer(husart);
  1150. husart->State = HAL_USART_STATE_READY;
  1151. return HAL_OK;
  1152. }
  1153. /**
  1154. * @brief Abort ongoing transfers (blocking mode).
  1155. * @param husart USART handle.
  1156. * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  1157. * This procedure performs following operations :
  1158. * - Disable USART Interrupts (Tx and Rx)
  1159. * - Disable the DMA transfer in the peripheral register (if enabled)
  1160. * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
  1161. * - Set handle State to READY
  1162. * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
  1163. * @retval HAL status
  1164. */
  1165. HAL_StatusTypeDef HAL_USART_Abort(USART_HandleTypeDef *husart)
  1166. {
  1167. #if defined(USART_CR1_FIFOEN)
  1168. /* Disable TXEIE, TCIE, RXNE, RXFT, TXFT, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1169. CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE));
  1170. CLEAR_BIT(husart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE | USART_CR3_TXFTIE));
  1171. #else
  1172. CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
  1173. CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
  1174. #endif
  1175. /* Disable the USART DMA Tx request if enabled */
  1176. if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT))
  1177. {
  1178. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  1179. /* Abort the USART DMA Tx channel : use blocking DMA Abort API (no callback) */
  1180. if(husart->hdmatx != NULL)
  1181. {
  1182. /* Set the USART DMA Abort callback to Null.
  1183. No call back execution at end of DMA abort procedure */
  1184. husart->hdmatx->XferAbortCallback = NULL;
  1185. HAL_DMA_Abort(husart->hdmatx);
  1186. }
  1187. }
  1188. /* Disable the USART DMA Rx request if enabled */
  1189. if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR))
  1190. {
  1191. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
  1192. /* Abort the USART DMA Rx channel : use blocking DMA Abort API (no callback) */
  1193. if(husart->hdmarx != NULL)
  1194. {
  1195. /* Set the USART DMA Abort callback to Null.
  1196. No call back execution at end of DMA abort procedure */
  1197. husart->hdmarx->XferAbortCallback = NULL;
  1198. HAL_DMA_Abort(husart->hdmarx);
  1199. }
  1200. }
  1201. /* Reset Tx and Rx transfer counters */
  1202. husart->TxXferCount = 0;
  1203. husart->RxXferCount = 0;
  1204. /* Clear the Error flags in the ICR register */
  1205. __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_OREF | USART_CLEAR_NEF | USART_CLEAR_PEF | USART_CLEAR_FEF);
  1206. #if defined(USART_CR1_FIFOEN)
  1207. /* Flush the whole TX FIFO (if needed) */
  1208. if (husart->FifoMode == USART_FIFOMODE_ENABLE)
  1209. {
  1210. __HAL_USART_SEND_REQ(husart, USART_TXDATA_FLUSH_REQUEST);
  1211. }
  1212. #endif
  1213. /* Discard the received data */
  1214. __HAL_USART_SEND_REQ(husart, USART_RXDATA_FLUSH_REQUEST);
  1215. /* Restore husart->State to Ready */
  1216. husart->State = HAL_USART_STATE_READY;
  1217. /* Reset Handle ErrorCode to No Error */
  1218. husart->ErrorCode = HAL_USART_ERROR_NONE;
  1219. return HAL_OK;
  1220. }
  1221. /**
  1222. * @brief Abort ongoing transfers (Interrupt mode).
  1223. * @param husart USART handle.
  1224. * @note This procedure could be used for aborting any ongoing transfer started in Interrupt or DMA mode.
  1225. * This procedure performs following operations :
  1226. * - Disable USART Interrupts (Tx and Rx)
  1227. * - Disable the DMA transfer in the peripheral register (if enabled)
  1228. * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
  1229. * - Set handle State to READY
  1230. * - At abort completion, call user abort complete callback
  1231. * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
  1232. * considered as completed only when user abort complete callback is executed (not when exiting function).
  1233. * @retval HAL status
  1234. */
  1235. HAL_StatusTypeDef HAL_USART_Abort_IT(USART_HandleTypeDef *husart)
  1236. {
  1237. uint32_t abortcplt = 1;
  1238. #if defined(USART_CR1_FIFOEN)
  1239. /* Disable TXEIE, TCIE, RXNE, RXFT, TXFT, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1240. CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE));
  1241. CLEAR_BIT(husart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE | USART_CR3_TXFTIE));
  1242. #else
  1243. CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
  1244. CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
  1245. #endif
  1246. /* If DMA Tx and/or DMA Rx Handles are associated to USART Handle, DMA Abort complete callbacks should be initialised
  1247. before any call to DMA Abort functions */
  1248. /* DMA Tx Handle is valid */
  1249. if(husart->hdmatx != NULL)
  1250. {
  1251. /* Set DMA Abort Complete callback if USART DMA Tx request if enabled.
  1252. Otherwise, set it to NULL */
  1253. if(HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT))
  1254. {
  1255. husart->hdmatx->XferAbortCallback = USART_DMATxAbortCallback;
  1256. }
  1257. else
  1258. {
  1259. husart->hdmatx->XferAbortCallback = NULL;
  1260. }
  1261. }
  1262. /* DMA Rx Handle is valid */
  1263. if(husart->hdmarx != NULL)
  1264. {
  1265. /* Set DMA Abort Complete callback if USART DMA Rx request if enabled.
  1266. Otherwise, set it to NULL */
  1267. if(HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR))
  1268. {
  1269. husart->hdmarx->XferAbortCallback = USART_DMARxAbortCallback;
  1270. }
  1271. else
  1272. {
  1273. husart->hdmarx->XferAbortCallback = NULL;
  1274. }
  1275. }
  1276. /* Disable the USART DMA Tx request if enabled */
  1277. if(HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAT))
  1278. {
  1279. /* Disable DMA Tx at USART level */
  1280. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  1281. /* Abort the USART DMA Tx channel : use non blocking DMA Abort API (callback) */
  1282. if(husart->hdmatx != NULL)
  1283. {
  1284. /* USART Tx DMA Abort callback has already been initialised :
  1285. will lead to call HAL_USART_AbortCpltCallback() at end of DMA abort procedure */
  1286. /* Abort DMA TX */
  1287. if(HAL_DMA_Abort_IT(husart->hdmatx) != HAL_OK)
  1288. {
  1289. husart->hdmatx->XferAbortCallback = NULL;
  1290. }
  1291. else
  1292. {
  1293. abortcplt = 0;
  1294. }
  1295. }
  1296. }
  1297. /* Disable the USART DMA Rx request if enabled */
  1298. if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR))
  1299. {
  1300. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
  1301. /* Abort the USART DMA Rx channel : use non blocking DMA Abort API (callback) */
  1302. if(husart->hdmarx != NULL)
  1303. {
  1304. /* USART Rx DMA Abort callback has already been initialised :
  1305. will lead to call HAL_USART_AbortCpltCallback() at end of DMA abort procedure */
  1306. /* Abort DMA RX */
  1307. if(HAL_DMA_Abort_IT(husart->hdmarx) != HAL_OK)
  1308. {
  1309. husart->hdmarx->XferAbortCallback = NULL;
  1310. abortcplt = 1;
  1311. }
  1312. else
  1313. {
  1314. abortcplt = 0;
  1315. }
  1316. }
  1317. }
  1318. /* if no DMA abort complete callback execution is required => call user Abort Complete callback */
  1319. if (abortcplt == 1)
  1320. {
  1321. /* Reset Tx and Rx transfer counters */
  1322. husart->TxXferCount = 0;
  1323. husart->RxXferCount = 0;
  1324. /* Reset errorCode */
  1325. husart->ErrorCode = HAL_USART_ERROR_NONE;
  1326. /* Clear the Error flags in the ICR register */
  1327. __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_OREF | USART_CLEAR_NEF | USART_CLEAR_PEF | USART_CLEAR_FEF);
  1328. #if defined(USART_CR1_FIFOEN)
  1329. /* Flush the whole TX FIFO (if needed) */
  1330. if (husart->FifoMode == USART_FIFOMODE_ENABLE)
  1331. {
  1332. __HAL_USART_SEND_REQ(husart, USART_TXDATA_FLUSH_REQUEST);
  1333. }
  1334. #endif
  1335. /* Discard the received data */
  1336. __HAL_USART_SEND_REQ(husart, USART_RXDATA_FLUSH_REQUEST);
  1337. /* Restore husart->State to Ready */
  1338. husart->State = HAL_USART_STATE_READY;
  1339. /* As no DMA to be aborted, call directly user Abort complete callback */
  1340. HAL_USART_AbortCpltCallback(husart);
  1341. }
  1342. return HAL_OK;
  1343. }
  1344. /**
  1345. * @brief Handle USART interrupt request.
  1346. * @param husart USART handle.
  1347. * @retval None
  1348. */
  1349. void HAL_USART_IRQHandler(USART_HandleTypeDef *husart)
  1350. {
  1351. uint32_t isrflags = READ_REG(husart->Instance->ISR);
  1352. uint32_t cr1its = READ_REG(husart->Instance->CR1);
  1353. uint32_t cr3its = READ_REG(husart->Instance->CR3);
  1354. uint32_t errorflags;
  1355. /* If no error occurs */
  1356. #if defined(USART_CR2_SLVEN)
  1357. errorflags = (isrflags & (uint32_t)(USART_ISR_PE | USART_ISR_FE | USART_ISR_ORE | USART_ISR_NE | USART_ISR_UDR));
  1358. #else
  1359. errorflags = (isrflags & (uint32_t)(USART_ISR_PE | USART_ISR_FE | USART_ISR_ORE | USART_ISR_NE));
  1360. #endif
  1361. if (errorflags == RESET)
  1362. {
  1363. /* USART in mode Receiver ---------------------------------------------------*/
  1364. #if defined(USART_CR1_FIFOEN)
  1365. if(((isrflags & USART_ISR_RXNE_RXFNE) != RESET)
  1366. && ( ((cr1its & USART_CR1_RXNEIE_RXFNEIE) != RESET)
  1367. || ((cr3its & USART_CR3_RXFTIE) != RESET)) )
  1368. #else
  1369. if(((isrflags & USART_ISR_RXNE) != RESET)
  1370. && ((cr1its & USART_CR1_RXNEIE) != RESET))
  1371. #endif
  1372. {
  1373. if (husart->RxISR != NULL) {husart->RxISR(husart);}
  1374. return;
  1375. }
  1376. }
  1377. /* If some errors occur */
  1378. #if defined(USART_CR1_FIFOEN)
  1379. if( (errorflags != RESET)
  1380. && ( ((cr3its & (USART_CR3_RXFTIE | USART_CR3_EIE)) != RESET)
  1381. || ((cr1its & (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE)) != RESET)))
  1382. #else
  1383. if( (errorflags != RESET)
  1384. && ( ((cr3its & USART_CR3_EIE) != RESET)
  1385. || ((cr1its & (USART_CR1_RXNEIE | USART_CR1_PEIE)) != RESET)))
  1386. #endif
  1387. {
  1388. /* USART parity error interrupt occurred -------------------------------------*/
  1389. if(((isrflags & USART_ISR_PE) != RESET) && ((cr1its & USART_CR1_PEIE) != RESET))
  1390. {
  1391. __HAL_USART_CLEAR_IT(husart, USART_CLEAR_PEF);
  1392. husart->ErrorCode |= HAL_USART_ERROR_PE;
  1393. }
  1394. /* USART frame error interrupt occurred --------------------------------------*/
  1395. if(((isrflags & USART_ISR_FE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
  1396. {
  1397. __HAL_USART_CLEAR_IT(husart, USART_CLEAR_FEF);
  1398. husart->ErrorCode |= HAL_USART_ERROR_FE;
  1399. }
  1400. /* USART noise error interrupt occurred --------------------------------------*/
  1401. if(((isrflags & USART_ISR_NE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
  1402. {
  1403. __HAL_USART_CLEAR_IT(husart, USART_CLEAR_NEF);
  1404. husart->ErrorCode |= HAL_USART_ERROR_NE;
  1405. }
  1406. /* USART Over-Run interrupt occurred -----------------------------------------*/
  1407. #if defined(USART_CR1_FIFOEN)
  1408. if( ((isrflags & USART_ISR_ORE) != RESET)
  1409. &&( ((cr1its & USART_CR1_RXNEIE_RXFNEIE) != RESET) ||
  1410. ((cr3its & (USART_CR3_RXFTIE | USART_CR3_EIE)) != RESET)))
  1411. #else
  1412. if( ((isrflags & USART_ISR_ORE) != RESET)
  1413. &&( ((cr1its & USART_CR1_RXNEIE) != RESET) ||
  1414. ((cr3its & USART_CR3_EIE) != RESET)))
  1415. #endif
  1416. {
  1417. __HAL_USART_CLEAR_IT(husart, USART_CLEAR_OREF);
  1418. husart->ErrorCode |= HAL_USART_ERROR_ORE;
  1419. }
  1420. #if defined(USART_CR2_SLVEN)
  1421. /* USART SPI slave underrun error interrupt occurred -------------------------*/
  1422. if(((isrflags & USART_ISR_UDR) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
  1423. {
  1424. /* Ignore SPI slave underrun errors when reception is going on */
  1425. if (husart->State == HAL_USART_STATE_BUSY_RX)
  1426. {
  1427. __HAL_USART_CLEAR_UDRFLAG(husart);
  1428. return;
  1429. }
  1430. else
  1431. {
  1432. __HAL_USART_CLEAR_UDRFLAG(husart);
  1433. husart->ErrorCode |= HAL_USART_ERROR_UDR;
  1434. }
  1435. }
  1436. #endif
  1437. /* Call USART Error Call back function if need be --------------------------*/
  1438. if(husart->ErrorCode != HAL_USART_ERROR_NONE)
  1439. {
  1440. /* USART in mode Receiver ---------------------------------------------------*/
  1441. #if defined(USART_CR1_FIFOEN)
  1442. if(((isrflags & USART_ISR_RXNE_RXFNE) != RESET)
  1443. && ( ((cr1its & USART_CR1_RXNEIE_RXFNEIE) != RESET)
  1444. || ((cr3its & USART_CR3_RXFTIE) != RESET)))
  1445. #else
  1446. if(((isrflags & USART_ISR_RXNE) != RESET)
  1447. && ((cr1its & USART_CR1_RXNEIE) != RESET))
  1448. #endif
  1449. {
  1450. if (husart->RxISR != NULL) {husart->RxISR(husart);}
  1451. }
  1452. /* If Overrun error occurs, or if any error occurs in DMA mode reception,
  1453. consider error as blocking */
  1454. if (((husart->ErrorCode & HAL_USART_ERROR_ORE) != RESET) ||
  1455. (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR)))
  1456. {
  1457. /* Blocking error : transfer is aborted
  1458. Set the USART state ready to be able to start again the process,
  1459. Disable Interrupts, and disable DMA requests, if ongoing */
  1460. USART_EndTransfer(husart);
  1461. /* Disable the USART DMA Rx request if enabled */
  1462. if (HAL_IS_BIT_SET(husart->Instance->CR3, USART_CR3_DMAR))
  1463. {
  1464. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR | USART_CR3_DMAR);
  1465. /* Abort the USART DMA Tx channel */
  1466. if(husart->hdmatx != NULL)
  1467. {
  1468. /* Set the USART Tx DMA Abort callback to NULL : no callback
  1469. executed at end of DMA abort procedure */
  1470. husart->hdmatx->XferAbortCallback = NULL;
  1471. /* Abort DMA TX */
  1472. HAL_DMA_Abort_IT(husart->hdmatx);
  1473. }
  1474. /* Abort the USART DMA Rx channel */
  1475. if(husart->hdmarx != NULL)
  1476. {
  1477. /* Set the USART Rx DMA Abort callback :
  1478. will lead to call HAL_USART_ErrorCallback() at end of DMA abort procedure */
  1479. husart->hdmarx->XferAbortCallback = USART_DMAAbortOnError;
  1480. /* Abort DMA RX */
  1481. if(HAL_DMA_Abort_IT(husart->hdmarx) != HAL_OK)
  1482. {
  1483. /* Call Directly husart->hdmarx->XferAbortCallback function in case of error */
  1484. husart->hdmarx->XferAbortCallback(husart->hdmarx);
  1485. }
  1486. }
  1487. else
  1488. {
  1489. /* Call user error callback */
  1490. HAL_USART_ErrorCallback(husart);
  1491. }
  1492. }
  1493. else
  1494. {
  1495. /* Call user error callback */
  1496. HAL_USART_ErrorCallback(husart);
  1497. }
  1498. }
  1499. else
  1500. {
  1501. /* Non Blocking error : transfer could go on.
  1502. Error is notified to user through user error callback */
  1503. HAL_USART_ErrorCallback(husart);
  1504. husart->ErrorCode = HAL_USART_ERROR_NONE;
  1505. }
  1506. }
  1507. return;
  1508. } /* End if some error occurs */
  1509. /* USART in mode Transmitter ------------------------------------------------*/
  1510. #if defined(USART_CR1_FIFOEN)
  1511. if(((isrflags & USART_ISR_TXE_TXFNF) != RESET)
  1512. && ( ((cr1its & USART_CR1_TXEIE_TXFNFIE) != RESET)
  1513. || ((cr3its & USART_CR3_TXFTIE) != RESET)))
  1514. #else
  1515. if(((isrflags & USART_ISR_TXE) != RESET)
  1516. && ((cr1its & USART_CR1_TXEIE) != RESET))
  1517. #endif
  1518. {
  1519. if (husart->TxISR != NULL) {husart->TxISR(husart);}
  1520. return;
  1521. }
  1522. /* USART in mode Transmitter (transmission end) -----------------------------*/
  1523. if(((isrflags & USART_ISR_TC) != RESET) && ((cr1its & USART_CR1_TCIE) != RESET))
  1524. {
  1525. USART_EndTransmit_IT(husart);
  1526. return;
  1527. }
  1528. #if defined(USART_CR1_FIFOEN)
  1529. /* USART TX Fifo Empty occurred ----------------------------------------------*/
  1530. if(((isrflags & USART_ISR_TXFE) != RESET) && ((cr1its & USART_CR1_TXFEIE) != RESET))
  1531. {
  1532. HAL_USARTEx_TxFifoEmptyCallback(husart);
  1533. return;
  1534. }
  1535. /* USART RX Fifo Full occurred ----------------------------------------------*/
  1536. if(((isrflags & USART_ISR_RXFF) != RESET) && ((cr1its & USART_CR1_RXFFIE) != RESET))
  1537. {
  1538. HAL_USARTEx_RxFifoFullCallback(husart);
  1539. return;
  1540. }
  1541. #endif
  1542. }
  1543. /**
  1544. * @brief Tx Transfer completed callback.
  1545. * @param husart USART handle.
  1546. * @retval None
  1547. */
  1548. __weak void HAL_USART_TxCpltCallback(USART_HandleTypeDef *husart)
  1549. {
  1550. /* Prevent unused argument(s) compilation warning */
  1551. UNUSED(husart);
  1552. /* NOTE : This function should not be modified, when the callback is needed,
  1553. the HAL_USART_TxCpltCallback can be implemented in the user file.
  1554. */
  1555. }
  1556. /**
  1557. * @brief Tx Half Transfer completed callback.
  1558. * @param husart USART handle.
  1559. * @retval None
  1560. */
  1561. __weak void HAL_USART_TxHalfCpltCallback(USART_HandleTypeDef *husart)
  1562. {
  1563. /* Prevent unused argument(s) compilation warning */
  1564. UNUSED(husart);
  1565. /* NOTE: This function should not be modified, when the callback is needed,
  1566. the HAL_USART_TxHalfCpltCallback can be implemented in the user file.
  1567. */
  1568. }
  1569. /**
  1570. * @brief Rx Transfer completed callback.
  1571. * @param husart USART handle.
  1572. * @retval None
  1573. */
  1574. __weak void HAL_USART_RxCpltCallback(USART_HandleTypeDef *husart)
  1575. {
  1576. /* Prevent unused argument(s) compilation warning */
  1577. UNUSED(husart);
  1578. /* NOTE: This function should not be modified, when the callback is needed,
  1579. the HAL_USART_RxCpltCallback can be implemented in the user file.
  1580. */
  1581. }
  1582. /**
  1583. * @brief Rx Half Transfer completed callback.
  1584. * @param husart USART handle.
  1585. * @retval None
  1586. */
  1587. __weak void HAL_USART_RxHalfCpltCallback(USART_HandleTypeDef *husart)
  1588. {
  1589. /* Prevent unused argument(s) compilation warning */
  1590. UNUSED(husart);
  1591. /* NOTE : This function should not be modified, when the callback is needed,
  1592. the HAL_USART_RxHalfCpltCallback can be implemented in the user file
  1593. */
  1594. }
  1595. /**
  1596. * @brief Tx/Rx Transfers completed callback for the non-blocking process.
  1597. * @param husart USART handle.
  1598. * @retval None
  1599. */
  1600. __weak void HAL_USART_TxRxCpltCallback(USART_HandleTypeDef *husart)
  1601. {
  1602. /* Prevent unused argument(s) compilation warning */
  1603. UNUSED(husart);
  1604. /* NOTE : This function should not be modified, when the callback is needed,
  1605. the HAL_USART_TxRxCpltCallback can be implemented in the user file
  1606. */
  1607. }
  1608. /**
  1609. * @brief USART error callback.
  1610. * @param husart USART handle.
  1611. * @retval None
  1612. */
  1613. __weak void HAL_USART_ErrorCallback(USART_HandleTypeDef *husart)
  1614. {
  1615. /* Prevent unused argument(s) compilation warning */
  1616. UNUSED(husart);
  1617. /* NOTE : This function should not be modified, when the callback is needed,
  1618. the HAL_USART_ErrorCallback can be implemented in the user file.
  1619. */
  1620. }
  1621. /**
  1622. * @brief USART Abort Complete callback.
  1623. * @param husart USART handle.
  1624. * @retval None
  1625. */
  1626. __weak void HAL_USART_AbortCpltCallback (USART_HandleTypeDef *husart)
  1627. {
  1628. /* Prevent unused argument(s) compilation warning */
  1629. UNUSED(husart);
  1630. /* NOTE : This function should not be modified, when the callback is needed,
  1631. the HAL_USART_AbortCpltCallback can be implemented in the user file.
  1632. */
  1633. }
  1634. /**
  1635. * @}
  1636. */
  1637. /** @defgroup USART_Exported_Functions_Group4 Peripheral State and Error functions
  1638. * @brief USART Peripheral State and Error functions
  1639. *
  1640. @verbatim
  1641. ==============================================================================
  1642. ##### Peripheral State and Error functions #####
  1643. ==============================================================================
  1644. [..]
  1645. This subsection provides functions allowing to :
  1646. (+) Return the USART handle state
  1647. (+) Return the USART handle error code
  1648. @endverbatim
  1649. * @{
  1650. */
  1651. /**
  1652. * @brief Return the USART handle state.
  1653. * @param husart pointer to a USART_HandleTypeDef structure that contains
  1654. * the configuration information for the specified USART.
  1655. * @retval USART handle state
  1656. */
  1657. HAL_USART_StateTypeDef HAL_USART_GetState(USART_HandleTypeDef *husart)
  1658. {
  1659. return husart->State;
  1660. }
  1661. /**
  1662. * @brief Return the USART error code.
  1663. * @param husart pointer to a USART_HandleTypeDef structure that contains
  1664. * the configuration information for the specified USART.
  1665. * @retval USART handle Error Code
  1666. */
  1667. uint32_t HAL_USART_GetError(USART_HandleTypeDef *husart)
  1668. {
  1669. return husart->ErrorCode;
  1670. }
  1671. /**
  1672. * @}
  1673. */
  1674. /**
  1675. * @}
  1676. */
  1677. /** @defgroup USART_Private_Functions USART Private Functions
  1678. * @{
  1679. */
  1680. /**
  1681. * @brief End ongoing transfer on USART peripheral (following error detection or Transfer completion).
  1682. * @param husart USART handle.
  1683. * @retval None
  1684. */
  1685. static void USART_EndTransfer(USART_HandleTypeDef *husart)
  1686. {
  1687. #if defined(USART_CR1_FIFOEN)
  1688. /* Disable TXEIE, TCIE, RXNE, RXFT, TXFT, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1689. CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE | USART_CR1_TXEIE_TXFNFIE | USART_CR1_TCIE));
  1690. CLEAR_BIT(husart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE | USART_CR3_TXFTIE));
  1691. #else
  1692. /* Disable TXEIE, TCIE, RXNE, PE and ERR (Frame error, noise error, overrun error) interrupts */
  1693. CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE | USART_CR1_TXEIE | USART_CR1_TCIE));
  1694. CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
  1695. #endif
  1696. /* At end of process, restore husart->State to Ready */
  1697. husart->State = HAL_USART_STATE_READY;
  1698. }
  1699. /**
  1700. * @brief DMA USART transmit process complete callback.
  1701. * @param hdma DMA handle.
  1702. * @retval None
  1703. */
  1704. static void USART_DMATransmitCplt(DMA_HandleTypeDef *hdma)
  1705. {
  1706. USART_HandleTypeDef* husart = (USART_HandleTypeDef*)(hdma->Parent);
  1707. /* DMA Normal mode */
  1708. if ( HAL_IS_BIT_CLR(hdma->Instance->CCR, DMA_CCR_CIRC) )
  1709. {
  1710. husart->TxXferCount = 0;
  1711. if(husart->State == HAL_USART_STATE_BUSY_TX)
  1712. {
  1713. /* Disable the DMA transfer for transmit request by resetting the DMAT bit
  1714. in the USART CR3 register */
  1715. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  1716. /* Enable the USART Transmit Complete Interrupt */
  1717. __HAL_USART_ENABLE_IT(husart, USART_IT_TC);
  1718. }
  1719. }
  1720. /* DMA Circular mode */
  1721. else
  1722. {
  1723. if(husart->State == HAL_USART_STATE_BUSY_TX)
  1724. {
  1725. HAL_USART_TxCpltCallback(husart);
  1726. }
  1727. }
  1728. }
  1729. /**
  1730. * @brief DMA USART transmit process half complete callback.
  1731. * @param hdma DMA handle.
  1732. * @retval None
  1733. */
  1734. static void USART_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
  1735. {
  1736. USART_HandleTypeDef* husart = (USART_HandleTypeDef*)(hdma->Parent);
  1737. HAL_USART_TxHalfCpltCallback(husart);
  1738. }
  1739. /**
  1740. * @brief DMA USART receive process complete callback.
  1741. * @param hdma DMA handle.
  1742. * @retval None
  1743. */
  1744. static void USART_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
  1745. {
  1746. USART_HandleTypeDef* husart = (USART_HandleTypeDef*)(hdma->Parent);
  1747. /* DMA Normal mode */
  1748. if ( HAL_IS_BIT_CLR(hdma->Instance->CCR, DMA_CCR_CIRC) )
  1749. {
  1750. husart->RxXferCount = 0;
  1751. /* Disable PE and ERR (Frame error, noise error, overrun error) interrupts */
  1752. CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE);
  1753. CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
  1754. /* Disable the DMA RX transfer for the receiver request by resetting the DMAR bit
  1755. in USART CR3 register */
  1756. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAR);
  1757. /* similarly, disable the DMA TX transfer that was started to provide the
  1758. clock to the slave device */
  1759. CLEAR_BIT(husart->Instance->CR3, USART_CR3_DMAT);
  1760. if(husart->State == HAL_USART_STATE_BUSY_RX)
  1761. {
  1762. HAL_USART_RxCpltCallback(husart);
  1763. }
  1764. /* The USART state is HAL_USART_STATE_BUSY_TX_RX */
  1765. else
  1766. {
  1767. HAL_USART_TxRxCpltCallback(husart);
  1768. }
  1769. husart->State= HAL_USART_STATE_READY;
  1770. }
  1771. /* DMA circular mode */
  1772. else
  1773. {
  1774. if(husart->State == HAL_USART_STATE_BUSY_RX)
  1775. {
  1776. HAL_USART_RxCpltCallback(husart);
  1777. }
  1778. /* The USART state is HAL_USART_STATE_BUSY_TX_RX */
  1779. else
  1780. {
  1781. HAL_USART_TxRxCpltCallback(husart);
  1782. }
  1783. }
  1784. }
  1785. /**
  1786. * @brief DMA USART receive process half complete callback.
  1787. * @param hdma DMA handle.
  1788. * @retval None
  1789. */
  1790. static void USART_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
  1791. {
  1792. USART_HandleTypeDef* husart = (USART_HandleTypeDef*)(hdma->Parent);
  1793. HAL_USART_RxHalfCpltCallback(husart);
  1794. }
  1795. /**
  1796. * @brief DMA USART communication error callback.
  1797. * @param hdma DMA handle.
  1798. * @retval None
  1799. */
  1800. static void USART_DMAError(DMA_HandleTypeDef *hdma)
  1801. {
  1802. USART_HandleTypeDef* husart = (USART_HandleTypeDef*)(hdma->Parent);
  1803. husart->RxXferCount = 0;
  1804. husart->TxXferCount = 0;
  1805. USART_EndTransfer(husart);
  1806. husart->ErrorCode |= HAL_USART_ERROR_DMA;
  1807. husart->State= HAL_USART_STATE_READY;
  1808. HAL_USART_ErrorCallback(husart);
  1809. }
  1810. /**
  1811. * @brief DMA USART communication abort callback, when initiated by HAL services on Error
  1812. * (To be called at end of DMA Abort procedure following error occurrence).
  1813. * @param hdma DMA handle.
  1814. * @retval None
  1815. */
  1816. static void USART_DMAAbortOnError(DMA_HandleTypeDef *hdma)
  1817. {
  1818. USART_HandleTypeDef* husart = (USART_HandleTypeDef*)(hdma->Parent);
  1819. husart->RxXferCount = 0;
  1820. husart->TxXferCount = 0;
  1821. HAL_USART_ErrorCallback(husart);
  1822. }
  1823. /**
  1824. * @brief DMA USART Tx communication abort callback, when initiated by user
  1825. * (To be called at end of DMA Tx Abort procedure following user abort request).
  1826. * @note When this callback is executed, User Abort complete call back is called only if no
  1827. * Abort still ongoing for Rx DMA Handle.
  1828. * @param hdma DMA handle.
  1829. * @retval None
  1830. */
  1831. static void USART_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
  1832. {
  1833. USART_HandleTypeDef* husart = (USART_HandleTypeDef* )(hdma->Parent);
  1834. husart->hdmatx->XferAbortCallback = NULL;
  1835. /* Check if an Abort process is still ongoing */
  1836. if(husart->hdmarx != NULL)
  1837. {
  1838. if(husart->hdmarx->XferAbortCallback != NULL)
  1839. {
  1840. return;
  1841. }
  1842. }
  1843. /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
  1844. husart->TxXferCount = 0;
  1845. husart->RxXferCount = 0;
  1846. /* Reset errorCode */
  1847. husart->ErrorCode = HAL_USART_ERROR_NONE;
  1848. /* Clear the Error flags in the ICR register */
  1849. __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_OREF | USART_CLEAR_NEF | USART_CLEAR_PEF | USART_CLEAR_FEF);
  1850. /* Restore husart->State to Ready */
  1851. husart->State = HAL_USART_STATE_READY;
  1852. /* Call user Abort complete callback */
  1853. HAL_USART_AbortCpltCallback(husart);
  1854. }
  1855. /**
  1856. * @brief DMA USART Rx communication abort callback, when initiated by user
  1857. * (To be called at end of DMA Rx Abort procedure following user abort request).
  1858. * @note When this callback is executed, User Abort complete call back is called only if no
  1859. * Abort still ongoing for Tx DMA Handle.
  1860. * @param hdma DMA handle.
  1861. * @retval None
  1862. */
  1863. static void USART_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
  1864. {
  1865. USART_HandleTypeDef* husart = (USART_HandleTypeDef* )(hdma->Parent);
  1866. husart->hdmarx->XferAbortCallback = NULL;
  1867. /* Check if an Abort process is still ongoing */
  1868. if(husart->hdmatx != NULL)
  1869. {
  1870. if(husart->hdmatx->XferAbortCallback != NULL)
  1871. {
  1872. return;
  1873. }
  1874. }
  1875. /* No Abort process still ongoing : All DMA channels are aborted, call user Abort Complete callback */
  1876. husart->TxXferCount = 0;
  1877. husart->RxXferCount = 0;
  1878. /* Reset errorCode */
  1879. husart->ErrorCode = HAL_USART_ERROR_NONE;
  1880. /* Clear the Error flags in the ICR register */
  1881. __HAL_USART_CLEAR_FLAG(husart, USART_CLEAR_OREF | USART_CLEAR_NEF | USART_CLEAR_PEF | USART_CLEAR_FEF);
  1882. /* Restore husart->State to Ready */
  1883. husart->State = HAL_USART_STATE_READY;
  1884. /* Call user Abort complete callback */
  1885. HAL_USART_AbortCpltCallback(husart);
  1886. }
  1887. /**
  1888. * @brief Handle USART Communication Timeout.
  1889. * @param husart USART handle.
  1890. * @param Flag Specifies the USART flag to check.
  1891. * @param Status the Flag status (SET or RESET).
  1892. * @param Tickstart Tick start value
  1893. * @param Timeout timeout duration.
  1894. * @retval HAL status
  1895. */
  1896. static HAL_StatusTypeDef USART_WaitOnFlagUntilTimeout(USART_HandleTypeDef *husart, uint32_t Flag, FlagStatus Status, uint32_t Tickstart, uint32_t Timeout)
  1897. {
  1898. /* Wait until flag is set */
  1899. while((__HAL_USART_GET_FLAG(husart, Flag) ? SET : RESET) == Status)
  1900. {
  1901. /* Check for the Timeout */
  1902. if(Timeout != HAL_MAX_DELAY)
  1903. {
  1904. if((Timeout == 0) || ((HAL_GetTick()-Tickstart) > Timeout))
  1905. {
  1906. husart->State= HAL_USART_STATE_READY;
  1907. /* Process Unlocked */
  1908. __HAL_UNLOCK(husart);
  1909. return HAL_TIMEOUT;
  1910. }
  1911. }
  1912. }
  1913. return HAL_OK;
  1914. }
  1915. /**
  1916. * @brief Configure the USART peripheral.
  1917. * @param husart USART handle.
  1918. * @retval HAL status
  1919. */
  1920. static HAL_StatusTypeDef USART_SetConfig(USART_HandleTypeDef *husart)
  1921. {
  1922. uint32_t tmpreg = 0x0;
  1923. USART_ClockSourceTypeDef clocksource = USART_CLOCKSOURCE_UNDEFINED;
  1924. HAL_StatusTypeDef ret = HAL_OK;
  1925. uint16_t brrtemp = 0x0000;
  1926. uint16_t usartdiv = 0x0000;
  1927. /* Check the parameters */
  1928. assert_param(IS_USART_POLARITY(husart->Init.CLKPolarity));
  1929. assert_param(IS_USART_PHASE(husart->Init.CLKPhase));
  1930. assert_param(IS_USART_LASTBIT(husart->Init.CLKLastBit));
  1931. assert_param(IS_USART_BAUDRATE(husart->Init.BaudRate));
  1932. assert_param(IS_USART_WORD_LENGTH(husart->Init.WordLength));
  1933. assert_param(IS_USART_STOPBITS(husart->Init.StopBits));
  1934. assert_param(IS_USART_PARITY(husart->Init.Parity));
  1935. assert_param(IS_USART_MODE(husart->Init.Mode));
  1936. #if defined(USART_PRESC_PRESCALER)
  1937. assert_param(IS_USART_PRESCALER(husart->Init.ClockPrescaler));
  1938. #endif
  1939. /*-------------------------- USART CR1 Configuration -----------------------*/
  1940. /* Clear M, PCE, PS, TE and RE bits and configure
  1941. * the USART Word Length, Parity and Mode:
  1942. * set the M bits according to husart->Init.WordLength value
  1943. * set PCE and PS bits according to husart->Init.Parity value
  1944. * set TE and RE bits according to husart->Init.Mode value
  1945. * force OVER8 to 1 to allow to reach the maximum speed (Fclock/8) */
  1946. tmpreg = (uint32_t)husart->Init.WordLength | husart->Init.Parity | husart->Init.Mode | USART_CR1_OVER8;
  1947. MODIFY_REG(husart->Instance->CR1, USART_CR1_FIELDS, tmpreg);
  1948. /*---------------------------- USART CR2 Configuration ---------------------*/
  1949. /* Clear and configure the USART Clock, CPOL, CPHA, LBCL STOP and SLVEN bits:
  1950. * set CPOL bit according to husart->Init.CLKPolarity value
  1951. * set CPHA bit according to husart->Init.CLKPhase value
  1952. * set LBCL bit according to husart->Init.CLKLastBit value (used in SPI master mode only)
  1953. * set STOP[13:12] bits according to husart->Init.StopBits value */
  1954. tmpreg = 0;
  1955. tmpreg = (uint32_t)(USART_CLOCK_ENABLE);
  1956. tmpreg |= (uint32_t)husart->Init.CLKLastBit;
  1957. tmpreg |= ((uint32_t)husart->Init.CLKPolarity | (uint32_t)husart->Init.CLKPhase);
  1958. tmpreg |= (uint32_t)husart->Init.StopBits;
  1959. MODIFY_REG(husart->Instance->CR2, USART_CR2_FIELDS, tmpreg);
  1960. #if defined(USART_PRESC_PRESCALER)
  1961. /*-------------------------- USART PRESC Configuration -----------------------*/
  1962. /* Configure
  1963. * - USART Clock Prescaler : set PRESCALER according to husart->Init.ClockPrescaler value */
  1964. MODIFY_REG(husart->Instance->PRESC, USART_PRESC_PRESCALER, husart->Init.ClockPrescaler);
  1965. #endif
  1966. /*-------------------------- USART BRR Configuration -----------------------*/
  1967. /* BRR is filled-up according to OVER8 bit setting which is forced to 1 */
  1968. USART_GETCLOCKSOURCE(husart, clocksource);
  1969. switch (clocksource)
  1970. {
  1971. case USART_CLOCKSOURCE_PCLK1:
  1972. #if defined(USART_PRESC_PRESCALER)
  1973. usartdiv = (uint16_t)(USART_DIV_SAMPLING8(HAL_RCC_GetPCLK1Freq(), husart->Init.BaudRate, husart->Init.ClockPrescaler));
  1974. #else
  1975. usartdiv = (uint16_t)(USART_DIV_SAMPLING8(HAL_RCC_GetPCLK1Freq(), husart->Init.BaudRate));
  1976. #endif
  1977. break;
  1978. case USART_CLOCKSOURCE_PCLK2:
  1979. #if defined(USART_PRESC_PRESCALER)
  1980. usartdiv = (uint16_t)(USART_DIV_SAMPLING8(HAL_RCC_GetPCLK2Freq(), husart->Init.BaudRate, husart->Init.ClockPrescaler));
  1981. #else
  1982. usartdiv = (uint16_t)(USART_DIV_SAMPLING8(HAL_RCC_GetPCLK2Freq(), husart->Init.BaudRate));
  1983. #endif
  1984. break;
  1985. case USART_CLOCKSOURCE_HSI:
  1986. #if defined(USART_PRESC_PRESCALER)
  1987. usartdiv = (uint16_t)(USART_DIV_SAMPLING8(HSI_VALUE, husart->Init.BaudRate, husart->Init.ClockPrescaler));
  1988. #else
  1989. usartdiv = (uint16_t)(USART_DIV_SAMPLING8(HSI_VALUE, husart->Init.BaudRate));
  1990. #endif
  1991. break;
  1992. case USART_CLOCKSOURCE_SYSCLK:
  1993. #if defined(USART_PRESC_PRESCALER)
  1994. usartdiv = (uint16_t)(USART_DIV_SAMPLING8(HAL_RCC_GetSysClockFreq(), husart->Init.BaudRate, husart->Init.ClockPrescaler));
  1995. #else
  1996. usartdiv = (uint16_t)(USART_DIV_SAMPLING8(HAL_RCC_GetSysClockFreq(), husart->Init.BaudRate));
  1997. #endif
  1998. break;
  1999. case USART_CLOCKSOURCE_LSE:
  2000. #if defined(USART_PRESC_PRESCALER)
  2001. usartdiv = (uint16_t)(USART_DIV_SAMPLING8(LSE_VALUE, husart->Init.BaudRate, husart->Init.ClockPrescaler));
  2002. #else
  2003. usartdiv = (uint16_t)(USART_DIV_SAMPLING8(LSE_VALUE, husart->Init.BaudRate));
  2004. #endif
  2005. break;
  2006. case USART_CLOCKSOURCE_UNDEFINED:
  2007. default:
  2008. ret = HAL_ERROR;
  2009. break;
  2010. }
  2011. /* USARTDIV must be greater than or equal to 0d16 */
  2012. if (usartdiv >= USART_BRR_MIN)
  2013. {
  2014. brrtemp = usartdiv & 0xFFF0U;
  2015. brrtemp |= (uint16_t)((usartdiv & (uint16_t)0x000FU) >> 1U);
  2016. husart->Instance->BRR = brrtemp;
  2017. }
  2018. else
  2019. {
  2020. ret = HAL_ERROR;
  2021. }
  2022. #if defined(USART_CR1_FIFOEN)
  2023. /* Initialize the number of data to process during RX/TX ISR execution */
  2024. husart->NbTxDataToProcess = 1;
  2025. husart->NbRxDataToProcess = 1;
  2026. #endif
  2027. /* Clear ISR function pointers */
  2028. husart->RxISR = NULL;
  2029. husart->TxISR = NULL;
  2030. return ret;
  2031. }
  2032. /**
  2033. * @brief Check the USART Idle State.
  2034. * @param husart: USART handle.
  2035. * @retval HAL status
  2036. */
  2037. static HAL_StatusTypeDef USART_CheckIdleState(USART_HandleTypeDef *husart)
  2038. {
  2039. uint32_t tickstart = 0;
  2040. /* Initialize the USART ErrorCode */
  2041. husart->ErrorCode = HAL_USART_ERROR_NONE;
  2042. /* Init tickstart for timeout managment*/
  2043. tickstart = HAL_GetTick();
  2044. /* Check if the Transmitter is enabled */
  2045. if((husart->Instance->CR1 & USART_CR1_TE) == USART_CR1_TE)
  2046. {
  2047. /* Wait until TEACK flag is set */
  2048. if(USART_WaitOnFlagUntilTimeout(husart, USART_ISR_TEACK, RESET, tickstart, USART_TEACK_REACK_TIMEOUT) != HAL_OK)
  2049. {
  2050. /* Timeout occurred */
  2051. return HAL_TIMEOUT;
  2052. }
  2053. }
  2054. /* Check if the Receiver is enabled */
  2055. if((husart->Instance->CR1 & USART_CR1_RE) == USART_CR1_RE)
  2056. {
  2057. /* Wait until REACK flag is set */
  2058. if(USART_WaitOnFlagUntilTimeout(husart, USART_ISR_REACK, RESET, tickstart, USART_TEACK_REACK_TIMEOUT) != HAL_OK)
  2059. {
  2060. /* Timeout occurred */
  2061. return HAL_TIMEOUT;
  2062. }
  2063. }
  2064. /* Initialize the USART state*/
  2065. husart->State= HAL_USART_STATE_READY;
  2066. /* Process Unlocked */
  2067. __HAL_UNLOCK(husart);
  2068. return HAL_OK;
  2069. }
  2070. /**
  2071. * @brief Simplex send an amount of data in non-blocking mode.
  2072. * @note Function called under interruption only, once
  2073. * interruptions have been enabled by HAL_USART_Transmit_IT().
  2074. * @note The USART errors are not managed to avoid the overrun error.
  2075. * @note ISR function executed when FIFO mode is disabled and when the
  2076. * data word length is less than 9 bits long.
  2077. * @param husart USART handle.
  2078. * @retval None
  2079. */
  2080. static void USART_TxISR_8BIT(USART_HandleTypeDef *husart)
  2081. {
  2082. /* Check that a Tx process is ongoing */
  2083. if ((husart->State == HAL_USART_STATE_BUSY_TX) ||
  2084. (husart->State == HAL_USART_STATE_BUSY_TX_RX))
  2085. {
  2086. if(husart->TxXferCount == 0)
  2087. {
  2088. /* Disable the USART Transmit data register empty interrupt */
  2089. __HAL_USART_DISABLE_IT(husart, USART_IT_TXE);
  2090. /* Enable the USART Transmit Complete Interrupt */
  2091. __HAL_USART_ENABLE_IT(husart, USART_IT_TC);
  2092. }
  2093. else
  2094. {
  2095. husart->Instance->TDR = (uint8_t)(*husart->pTxBuffPtr++ & (uint8_t)0xFF);
  2096. husart->TxXferCount--;
  2097. }
  2098. }
  2099. }
  2100. /**
  2101. * @brief Simplex send an amount of data in non-blocking mode.
  2102. * @note Function called under interruption only, once
  2103. * interruptions have been enabled by HAL_USART_Transmit_IT().
  2104. * @note The USART errors are not managed to avoid the overrun error.
  2105. * @note ISR function executed when FIFO mode is disabled and when the
  2106. * data word length is 9 bits long.
  2107. * @param husart USART handle.
  2108. * @retval None
  2109. */
  2110. static void USART_TxISR_16BIT(USART_HandleTypeDef *husart)
  2111. {
  2112. uint16_t* tmp;
  2113. if ((husart->State == HAL_USART_STATE_BUSY_TX) ||
  2114. (husart->State == HAL_USART_STATE_BUSY_TX_RX))
  2115. {
  2116. if(husart->TxXferCount == 0)
  2117. {
  2118. /* Disable the USART Transmit data register empty interrupt */
  2119. __HAL_USART_DISABLE_IT(husart, USART_IT_TXE);
  2120. /* Enable the USART Transmit Complete Interrupt */
  2121. __HAL_USART_ENABLE_IT(husart, USART_IT_TC);
  2122. }
  2123. else
  2124. {
  2125. tmp = (uint16_t*) husart->pTxBuffPtr;
  2126. husart->Instance->TDR = (*tmp & (uint16_t)0x01FF);
  2127. husart->pTxBuffPtr += 2;
  2128. husart->TxXferCount--;
  2129. }
  2130. }
  2131. }
  2132. #if defined(USART_CR1_FIFOEN)
  2133. /**
  2134. * @brief Simplex send an amount of data in non-blocking mode.
  2135. * @note Function called under interruption only, once
  2136. * interruptions have been enabled by HAL_USART_Transmit_IT().
  2137. * @note The USART errors are not managed to avoid the overrun error.
  2138. * @note ISR function executed when FIFO mode is enabled and when the
  2139. * data word length is less than 9 bits long.
  2140. * @param husart USART handle.
  2141. * @retval None
  2142. */
  2143. static void USART_TxISR_8BIT_FIFOEN(USART_HandleTypeDef *husart)
  2144. {
  2145. uint8_t nb_tx_data;
  2146. /* Check that a Tx process is ongoing */
  2147. if ((husart->State == HAL_USART_STATE_BUSY_TX) ||
  2148. (husart->State == HAL_USART_STATE_BUSY_TX_RX))
  2149. {
  2150. for(nb_tx_data = husart->NbTxDataToProcess ; nb_tx_data > 0 ; nb_tx_data--)
  2151. {
  2152. if(husart->TxXferCount == 0)
  2153. {
  2154. /* Disable the TX FIFO threshold interrupt */
  2155. __HAL_USART_DISABLE_IT(husart, USART_IT_TXFT);
  2156. /* Enable the USART Transmit Complete Interrupt */
  2157. __HAL_USART_ENABLE_IT(husart, USART_IT_TC);
  2158. break; /* force exit loop */
  2159. }
  2160. else if (__HAL_USART_GET_FLAG(husart, USART_FLAG_TXFNF) == SET)
  2161. {
  2162. husart->Instance->TDR = (uint8_t)(*husart->pTxBuffPtr++ & (uint8_t)0xFF);
  2163. husart->TxXferCount--;
  2164. }
  2165. }
  2166. }
  2167. }
  2168. /**
  2169. * @brief Simplex send an amount of data in non-blocking mode.
  2170. * @note Function called under interruption only, once
  2171. * interruptions have been enabled by HAL_USART_Transmit_IT().
  2172. * @note The USART errors are not managed to avoid the overrun error.
  2173. * @note ISR function executed when FIFO mode is enabled and when the
  2174. * data word length is 9 bits long.
  2175. * @param husart USART handle.
  2176. * @retval None
  2177. */
  2178. static void USART_TxISR_16BIT_FIFOEN(USART_HandleTypeDef *husart)
  2179. {
  2180. uint16_t* tmp;
  2181. uint8_t nb_tx_data;
  2182. /* Check that a Tx process is ongoing */
  2183. if ((husart->State == HAL_USART_STATE_BUSY_TX) ||
  2184. (husart->State == HAL_USART_STATE_BUSY_TX_RX))
  2185. {
  2186. for(nb_tx_data = husart->NbTxDataToProcess ; nb_tx_data > 0 ; nb_tx_data--)
  2187. {
  2188. if(husart->TxXferCount == 0)
  2189. {
  2190. /* Disable the TX FIFO threshold interrupt */
  2191. __HAL_USART_DISABLE_IT(husart, USART_IT_TXFT);
  2192. /* Enable the USART Transmit Complete Interrupt */
  2193. __HAL_USART_ENABLE_IT(husart, USART_IT_TC);
  2194. break; /* force exit loop */
  2195. }
  2196. else if (__HAL_USART_GET_FLAG(husart, USART_FLAG_TXFNF) == SET)
  2197. {
  2198. tmp = (uint16_t*) husart->pTxBuffPtr;
  2199. husart->Instance->TDR = (*tmp & (uint16_t)0x01FF);
  2200. husart->pTxBuffPtr += 2;
  2201. husart->TxXferCount--;
  2202. }
  2203. }
  2204. }
  2205. }
  2206. #endif
  2207. /**
  2208. * @brief Wraps up transmission in non-blocking mode.
  2209. * @param husart Pointer to a USART_HandleTypeDef structure that contains
  2210. * the configuration information for the specified USART module.
  2211. * @retval None
  2212. */
  2213. static void USART_EndTransmit_IT(USART_HandleTypeDef *husart)
  2214. {
  2215. /* Disable the USART Transmit Complete Interrupt */
  2216. __HAL_USART_DISABLE_IT(husart, USART_IT_TC);
  2217. /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  2218. __HAL_USART_DISABLE_IT(husart, USART_IT_ERR);
  2219. /* Clear TxISR function pointer */
  2220. husart->TxISR = NULL;
  2221. if (husart->State == HAL_USART_STATE_BUSY_TX)
  2222. {
  2223. /* Clear overrun flag and discard the received data */
  2224. __HAL_USART_CLEAR_OREFLAG(husart);
  2225. __HAL_USART_SEND_REQ(husart, USART_RXDATA_FLUSH_REQUEST);
  2226. /* Tx process is completed, restore husart->State to Ready */
  2227. husart->State = HAL_USART_STATE_READY;
  2228. HAL_USART_TxCpltCallback(husart);
  2229. }
  2230. else if (husart->RxXferCount == 0)
  2231. {
  2232. /* TxRx process is completed, restore husart->State to Ready */
  2233. husart->State = HAL_USART_STATE_READY;
  2234. HAL_USART_TxRxCpltCallback(husart);
  2235. }
  2236. }
  2237. /**
  2238. * @brief Simplex receive an amount of data in non-blocking mode.
  2239. * @note Function called under interruption only, once
  2240. * interruptions have been enabled by HAL_USART_Receive_IT().
  2241. * @note ISR function executed when FIFO mode is disabled and when the
  2242. * data word length is less than 9 bits long.
  2243. * @param husart USART handle
  2244. * @retval None
  2245. */
  2246. static void USART_RxISR_8BIT(USART_HandleTypeDef *husart)
  2247. {
  2248. uint16_t uhMask = husart->Mask;
  2249. if ((husart->State == HAL_USART_STATE_BUSY_RX) ||
  2250. (husart->State == HAL_USART_STATE_BUSY_TX_RX))
  2251. {
  2252. *husart->pRxBuffPtr++ = (uint8_t)(husart->Instance->RDR & (uint8_t)uhMask);
  2253. if(--husart->RxXferCount == 0)
  2254. {
  2255. /* Disable the USART Parity Error Interrupt and RXNE interrupt*/
  2256. #if defined(USART_CR1_FIFOEN)
  2257. CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE));
  2258. #else
  2259. CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  2260. #endif
  2261. /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  2262. CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
  2263. /* Clear RxISR function pointer */
  2264. husart->RxISR = NULL;
  2265. if(husart->State == HAL_USART_STATE_BUSY_RX)
  2266. {
  2267. #if defined(USART_CR2_SLVEN)
  2268. /* Clear SPI slave underrun flag and discard transmit data */
  2269. if (husart->SlaveMode == USART_SLAVEMODE_ENABLE)
  2270. {
  2271. __HAL_USART_CLEAR_UDRFLAG(husart);
  2272. __HAL_USART_SEND_REQ(husart, USART_TXDATA_FLUSH_REQUEST);
  2273. }
  2274. #endif
  2275. /* Rx process is completed, restore husart->State to Ready */
  2276. husart->State = HAL_USART_STATE_READY;
  2277. HAL_USART_RxCpltCallback(husart);
  2278. }
  2279. else if ((husart->TxXferCount == 0) &&
  2280. (READ_BIT(husart->Instance->CR1, USART_CR1_TCIE) != USART_CR1_TCIE))
  2281. {
  2282. /* TxRx process is completed, restore husart->State to Ready */
  2283. husart->State = HAL_USART_STATE_READY;
  2284. HAL_USART_TxRxCpltCallback(husart);
  2285. }
  2286. }
  2287. #if defined(USART_CR2_SLVEN)
  2288. else if ( (husart->State == HAL_USART_STATE_BUSY_RX) &&
  2289. (husart->SlaveMode == USART_SLAVEMODE_DISABLE))
  2290. #else
  2291. else if (husart->State == HAL_USART_STATE_BUSY_RX)
  2292. #endif
  2293. {
  2294. /* Send dummy byte in order to generate the clock for the Slave to Send the next data */
  2295. husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x00FF);
  2296. }
  2297. }
  2298. }
  2299. /**
  2300. * @brief Simplex receive an amount of data in non-blocking mode.
  2301. * @note Function called under interruption only, once
  2302. * interruptions have been enabled by HAL_USART_Receive_IT().
  2303. * @note ISR function executed when FIFO mode is disabled and when the
  2304. * data word length is 9 bits long.
  2305. * @param husart USART handle
  2306. * @retval None
  2307. */
  2308. static void USART_RxISR_16BIT(USART_HandleTypeDef *husart)
  2309. {
  2310. uint16_t* tmp;
  2311. uint16_t uhMask = husart->Mask;
  2312. if ((husart->State == HAL_USART_STATE_BUSY_RX) ||
  2313. (husart->State == HAL_USART_STATE_BUSY_TX_RX))
  2314. {
  2315. tmp = (uint16_t*) husart->pRxBuffPtr;
  2316. *tmp = (uint16_t)(husart->Instance->RDR & uhMask);
  2317. husart->pRxBuffPtr += 2;
  2318. if(--husart->RxXferCount == 0)
  2319. {
  2320. /* Disable the USART Parity Error Interrupt and RXNE interrupt*/
  2321. #if defined(USART_CR1_FIFOEN)
  2322. CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE_RXFNEIE | USART_CR1_PEIE));
  2323. #else
  2324. CLEAR_BIT(husart->Instance->CR1, (USART_CR1_RXNEIE | USART_CR1_PEIE));
  2325. #endif
  2326. /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) */
  2327. CLEAR_BIT(husart->Instance->CR3, USART_CR3_EIE);
  2328. /* Clear RxISR function pointer */
  2329. husart->RxISR = NULL;
  2330. if(husart->State == HAL_USART_STATE_BUSY_RX)
  2331. {
  2332. #if defined(USART_CR2_SLVEN)
  2333. /* Clear SPI slave underrun flag and discard transmit data */
  2334. if (husart->SlaveMode == USART_SLAVEMODE_ENABLE)
  2335. {
  2336. __HAL_USART_CLEAR_UDRFLAG(husart);
  2337. __HAL_USART_SEND_REQ(husart, USART_TXDATA_FLUSH_REQUEST);
  2338. }
  2339. #endif
  2340. /* Rx process is completed, restore husart->State to Ready */
  2341. husart->State = HAL_USART_STATE_READY;
  2342. HAL_USART_RxCpltCallback(husart);
  2343. }
  2344. else if ((husart->TxXferCount == 0) &&
  2345. (READ_BIT(husart->Instance->CR1, USART_CR1_TCIE) != USART_CR1_TCIE))
  2346. {
  2347. /* TxRx process is completed, restore husart->State to Ready */
  2348. husart->State = HAL_USART_STATE_READY;
  2349. HAL_USART_TxRxCpltCallback(husart);
  2350. }
  2351. }
  2352. #if defined(USART_CR2_SLVEN)
  2353. else if ( (husart->State == HAL_USART_STATE_BUSY_RX) &&
  2354. (husart->SlaveMode == USART_SLAVEMODE_DISABLE))
  2355. #else
  2356. else if (husart->State == HAL_USART_STATE_BUSY_RX)
  2357. #endif
  2358. {
  2359. /* Send dummy byte in order to generate the clock for the Slave to Send the next data */
  2360. husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x00FF);
  2361. }
  2362. }
  2363. }
  2364. #if defined(USART_CR1_FIFOEN)
  2365. /**
  2366. * @brief Simplex receive an amount of data in non-blocking mode.
  2367. * @note Function called under interruption only, once
  2368. * interruptions have been enabled by HAL_USART_Receive_IT().
  2369. * @note ISR function executed when FIFO mode is enabled and when the
  2370. * data word length is less than 9 bits long.
  2371. * @retval None
  2372. * @retval HAL status
  2373. */
  2374. static void USART_RxISR_8BIT_FIFOEN(USART_HandleTypeDef *husart)
  2375. {
  2376. uint16_t uhMask = husart->Mask;
  2377. uint8_t nb_rx_data;
  2378. /* Check that a Rx process is ongoing */
  2379. if ((husart->State == HAL_USART_STATE_BUSY_RX) ||
  2380. (husart->State == HAL_USART_STATE_BUSY_TX_RX))
  2381. {
  2382. for(nb_rx_data = husart->NbRxDataToProcess ; nb_rx_data > 0 ; nb_rx_data--)
  2383. {
  2384. if (__HAL_USART_GET_FLAG(husart, USART_FLAG_RXFNE) == SET)
  2385. {
  2386. *husart->pRxBuffPtr++ = (uint8_t)(husart->Instance->RDR & (uint8_t)uhMask);
  2387. if(--husart->RxXferCount == 0)
  2388. {
  2389. /* Disable the USART Parity Error Interrupt */
  2390. CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE);
  2391. /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) and RX FIFO Threshold interrupt */
  2392. CLEAR_BIT(husart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
  2393. /* Clear RxISR function pointer */
  2394. husart->RxISR = NULL;
  2395. if (husart->State == HAL_USART_STATE_BUSY_RX)
  2396. {
  2397. #if defined(USART_CR2_SLVEN)
  2398. /* Clear SPI slave underrun flag and discard transmit data */
  2399. if (husart->SlaveMode == USART_SLAVEMODE_ENABLE)
  2400. {
  2401. __HAL_USART_CLEAR_UDRFLAG(husart);
  2402. __HAL_USART_SEND_REQ(husart, USART_TXDATA_FLUSH_REQUEST);
  2403. }
  2404. #endif
  2405. /* Rx process is completed, restore husart->State to Ready */
  2406. husart->State = HAL_USART_STATE_READY;
  2407. HAL_USART_RxCpltCallback(husart);
  2408. }
  2409. else if ((husart->TxXferCount == 0) &&
  2410. (READ_BIT(husart->Instance->CR1, USART_CR1_TCIE) != USART_CR1_TCIE))
  2411. {
  2412. /* TxRx process is completed, restore husart->State to Ready */
  2413. husart->State = HAL_USART_STATE_READY;
  2414. HAL_USART_TxRxCpltCallback(husart);
  2415. }
  2416. }
  2417. #if defined(USART_CR2_SLVEN)
  2418. else if ((husart->State == HAL_USART_STATE_BUSY_RX) &&
  2419. (husart->SlaveMode == USART_SLAVEMODE_DISABLE))
  2420. #else
  2421. else if (husart->State == HAL_USART_STATE_BUSY_RX)
  2422. #endif
  2423. {
  2424. /* Send dummy byte in order to generate the clock for the Slave to Send the next data */
  2425. husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x00FF);
  2426. }
  2427. }
  2428. }
  2429. /* When remaining number of bytes to receive is less than the RX FIFO
  2430. threshold, next incoming frames are processed as if FIFO mode was
  2431. disabled (i.e. one interrupt per received frame).
  2432. */
  2433. if (((husart->RxXferCount != 0U)) && (husart->RxXferCount < husart->NbRxDataToProcess))
  2434. {
  2435. /* Disable the USART RXFT interrupt*/
  2436. CLEAR_BIT(husart->Instance->CR3, USART_CR3_RXFTIE);
  2437. /* Update the RxISR function pointer */
  2438. husart->RxISR = USART_RxISR_8BIT;
  2439. /* Enable the USART Data Register Not Empty interrupt */
  2440. SET_BIT(husart->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
  2441. #if defined(USART_CR2_SLVEN)
  2442. if ( (husart->State == HAL_USART_STATE_BUSY_TX_RX) &&
  2443. (husart->SlaveMode == USART_SLAVEMODE_DISABLE) &&
  2444. (husart->TxXferCount == 0U))
  2445. #else
  2446. if ( (husart->State == HAL_USART_STATE_BUSY_TX_RX) &&
  2447. (husart->TxXferCount == 0U))
  2448. #endif
  2449. {
  2450. /* Send dummy byte in order to generate the clock for the Slave to Send the next data */
  2451. husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x00FF);
  2452. }
  2453. }
  2454. }
  2455. else
  2456. {
  2457. /* Clear RXNE interrupt flag */
  2458. __HAL_USART_SEND_REQ(husart, USART_RXDATA_FLUSH_REQUEST);
  2459. }
  2460. }
  2461. /**
  2462. * @brief Simplex receive an amount of data in non-blocking mode.
  2463. * @note Function called under interruption only, once
  2464. * interruptions have been enabled by HAL_USART_Receive_IT().
  2465. * @note ISR function executed when FIFO mode is enabled and when the
  2466. * data word length is 9 bits long.
  2467. * @param husart USART handle
  2468. * @retval None
  2469. */
  2470. static void USART_RxISR_16BIT_FIFOEN(USART_HandleTypeDef *husart)
  2471. {
  2472. uint16_t* tmp;
  2473. uint16_t uhMask = husart->Mask;
  2474. uint8_t nb_rx_data;
  2475. /* Check that a Tx process is ongoing */
  2476. if ((husart->State == HAL_USART_STATE_BUSY_RX) ||
  2477. (husart->State == HAL_USART_STATE_BUSY_TX_RX))
  2478. {
  2479. for(nb_rx_data = husart->NbRxDataToProcess ; nb_rx_data > 0 ; nb_rx_data--)
  2480. {
  2481. if (__HAL_USART_GET_FLAG(husart, USART_FLAG_RXFNE) == SET)
  2482. {
  2483. tmp = (uint16_t*) husart->pRxBuffPtr;
  2484. *tmp = (uint16_t)(husart->Instance->RDR & uhMask);
  2485. husart->pRxBuffPtr += 2;
  2486. if(--husart->RxXferCount == 0)
  2487. {
  2488. /* Disable the USART Parity Error Interrupt */
  2489. CLEAR_BIT(husart->Instance->CR1, USART_CR1_PEIE);
  2490. /* Disable the USART Error Interrupt: (Frame error, noise error, overrun error) and RX FIFO Threshold interrupt */
  2491. CLEAR_BIT(husart->Instance->CR3, (USART_CR3_EIE | USART_CR3_RXFTIE));
  2492. /* Clear RxISR function pointer */
  2493. husart->RxISR = NULL;
  2494. if(husart->State == HAL_USART_STATE_BUSY_RX)
  2495. {
  2496. #if defined(USART_CR2_SLVEN)
  2497. /* Clear SPI slave underrun flag and discard transmit data */
  2498. if (husart->SlaveMode == USART_SLAVEMODE_ENABLE)
  2499. {
  2500. __HAL_USART_CLEAR_UDRFLAG(husart);
  2501. __HAL_USART_SEND_REQ(husart, USART_TXDATA_FLUSH_REQUEST);
  2502. }
  2503. #endif
  2504. /* Rx process is completed, restore husart->State to Ready */
  2505. husart->State = HAL_USART_STATE_READY;
  2506. HAL_USART_RxCpltCallback(husart);
  2507. }
  2508. else if ((husart->TxXferCount == 0) &&
  2509. (READ_BIT(husart->Instance->CR1, USART_CR1_TCIE) != USART_CR1_TCIE))
  2510. {
  2511. /* TxRx process is completed, restore husart->State to Ready */
  2512. husart->State = HAL_USART_STATE_READY;
  2513. HAL_USART_TxRxCpltCallback(husart);
  2514. }
  2515. }
  2516. #if defined(USART_CR2_SLVEN)
  2517. else if ((husart->State == HAL_USART_STATE_BUSY_RX) &&
  2518. (husart->SlaveMode == USART_SLAVEMODE_DISABLE))
  2519. #else
  2520. else if (husart->State == HAL_USART_STATE_BUSY_RX)
  2521. #endif
  2522. {
  2523. /* Send dummy byte in order to generate the clock for the Slave to Send the next data */
  2524. husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x00FF);
  2525. }
  2526. }
  2527. }
  2528. /* When remaining number of bytes to receive is less than the RX FIFO
  2529. threshold, next incoming frames are processed as if FIFO mode was
  2530. disabled (i.e. one interrupt per received frame).
  2531. */
  2532. if (((husart->RxXferCount != 0U)) && (husart->RxXferCount < husart->NbRxDataToProcess))
  2533. {
  2534. /* Disable the USART RXFT interrupt*/
  2535. CLEAR_BIT(husart->Instance->CR3, USART_CR3_RXFTIE);
  2536. /* Update the RxISR function pointer */
  2537. husart->RxISR = USART_RxISR_16BIT;
  2538. /* Enable the USART Data Register Not Empty interrupt */
  2539. SET_BIT(husart->Instance->CR1, USART_CR1_RXNEIE_RXFNEIE);
  2540. #if defined(USART_CR2_SLVEN)
  2541. if ( (husart->State == HAL_USART_STATE_BUSY_TX_RX) &&
  2542. (husart->SlaveMode == USART_SLAVEMODE_DISABLE) &&
  2543. (husart->TxXferCount == 0U))
  2544. #else
  2545. if ( (husart->State == HAL_USART_STATE_BUSY_TX_RX) &&
  2546. (husart->TxXferCount == 0U))
  2547. #endif
  2548. {
  2549. /* Send dummy byte in order to generate the clock for the Slave to Send the next data */
  2550. husart->Instance->TDR = (USART_DUMMY_DATA & (uint16_t)0x00FF);
  2551. }
  2552. }
  2553. }
  2554. else
  2555. {
  2556. /* Clear RXNE interrupt flag */
  2557. __HAL_USART_SEND_REQ(husart, USART_RXDATA_FLUSH_REQUEST);
  2558. }
  2559. }
  2560. #endif
  2561. /**
  2562. * @}
  2563. */
  2564. #endif /* HAL_USART_MODULE_ENABLED */
  2565. /**
  2566. * @}
  2567. */
  2568. /**
  2569. * @}
  2570. */
  2571. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/