stm32f7xx_hal_spi.c 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728
  1. /**
  2. ******************************************************************************
  3. * @file stm32f7xx_hal_spi.c
  4. * @author MCD Application Team
  5. * @version V1.0.1
  6. * @date 25-June-2015
  7. * @brief SPI HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of the Serial Peripheral Interface (SPI) peripheral:
  10. * + Initialization and de-initialization functions
  11. * + IO operation functions
  12. * + Peripheral Control functions
  13. * + Peripheral State functions
  14. @verbatim
  15. ==============================================================================
  16. ##### How to use this driver #####
  17. ==============================================================================
  18. [..]
  19. The SPI HAL driver can be used as follows:
  20. (#) Declare a SPI_HandleTypeDef handle structure, for example:
  21. SPI_HandleTypeDef hspi;
  22. (#)Initialize the SPI low level resources by implementing the HAL_SPI_MspInit ()API:
  23. (##) Enable the SPIx interface clock
  24. (##) SPI pins configuration
  25. (+++) Enable the clock for the SPI GPIOs
  26. (+++) Configure these SPI pins as alternate function push-pull
  27. (##) NVIC configuration if you need to use interrupt process
  28. (+++) Configure the SPIx interrupt priority
  29. (+++) Enable the NVIC SPI IRQ handle
  30. (##) DMA Configuration if you need to use DMA process
  31. (+++) Declare a DMA_HandleTypeDef handle structure for the transmit or receive channel
  32. (+++) Enable the DMAx clock
  33. (+++) Configure the DMA handle parameters
  34. (+++) Configure the DMA Tx or Rx channel
  35. (+++) Associate the initialized hdma_tx handle to the hspi DMA Tx or Rx handle
  36. (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx or Rx channel
  37. (#) Program the Mode, BidirectionalMode , Data size, Baudrate Prescaler, NSS
  38. management, Clock polarity and phase, FirstBit and CRC configuration in the hspi Init structure.
  39. (#) Initialize the SPI registers by calling the HAL_SPI_Init() API:
  40. (++) This API configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
  41. by calling the customised HAL_SPI_MspInit() API.
  42. [..]
  43. Circular mode restriction:
  44. (#) The DMA circular mode cannot be used when the SPI is configured in these modes:
  45. (##) Master 2Lines RxOnly
  46. (##) Master 1Line Rx
  47. (#) The CRC feature is not managed when the DMA circular mode is enabled
  48. (#) When the SPI DMA Pause/Stop features are used, we must use the following APIs
  49. the HAL_SPI_DMAPause()/ HAL_SPI_DMAStop() only under the SPI callbacks
  50. @endverbatim
  51. ******************************************************************************
  52. * @attention
  53. *
  54. * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
  55. *
  56. * Redistribution and use in source and binary forms, with or without modification,
  57. * are permitted provided that the following conditions are met:
  58. * 1. Redistributions of source code must retain the above copyright notice,
  59. * this list of conditions and the following disclaimer.
  60. * 2. Redistributions in binary form must reproduce the above copyright notice,
  61. * this list of conditions and the following disclaimer in the documentation
  62. * and/or other materials provided with the distribution.
  63. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  64. * may be used to endorse or promote products derived from this software
  65. * without specific prior written permission.
  66. *
  67. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  68. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  69. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  70. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  71. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  72. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  73. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  74. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  75. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  76. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  77. *
  78. ******************************************************************************
  79. */
  80. /* Includes ------------------------------------------------------------------*/
  81. #include "stm32f7xx_hal.h"
  82. /** @addtogroup STM32F7xx_HAL_Driver
  83. * @{
  84. */
  85. /** @defgroup SPI SPI
  86. * @brief SPI HAL module driver
  87. * @{
  88. */
  89. #ifdef HAL_SPI_MODULE_ENABLED
  90. /* Private typedef -----------------------------------------------------------*/
  91. /* Private defines -----------------------------------------------------------*/
  92. /** @defgroup SPI_Private_Constants SPI Private Constants
  93. * @{
  94. */
  95. #define SPI_DEFAULT_TIMEOUT 50
  96. /**
  97. * @}
  98. */
  99. /* Private macro -------------------------------------------------------------*/
  100. /* Private variables ---------------------------------------------------------*/
  101. /* Private function prototypes -----------------------------------------------*/
  102. /** @addtogroup SPI_Private_Functions
  103. * @{
  104. */
  105. static void SPI_DMATransmitCplt(DMA_HandleTypeDef *hdma);
  106. static void SPI_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
  107. static void SPI_DMATransmitReceiveCplt(DMA_HandleTypeDef *hdma);
  108. static void SPI_DMAHalfTransmitCplt(DMA_HandleTypeDef *hdma);
  109. static void SPI_DMAHalfReceiveCplt(DMA_HandleTypeDef *hdma);
  110. static void SPI_DMAHalfTransmitReceiveCplt(DMA_HandleTypeDef *hdma);
  111. static void SPI_DMAError(DMA_HandleTypeDef *hdma);
  112. static HAL_StatusTypeDef SPI_WaitFlagStateUntilTimeout(SPI_HandleTypeDef *hspi, uint32_t Flag, uint32_t State, uint32_t Timeout);
  113. static HAL_StatusTypeDef SPI_WaitFifoStateUntilTimeout(SPI_HandleTypeDef *hspi, uint32_t Fifo, uint32_t State, uint32_t Timeout);
  114. static void SPI_TxISR_8BIT(struct __SPI_HandleTypeDef *hspi);
  115. static void SPI_TxISR_16BIT(struct __SPI_HandleTypeDef *hspi);
  116. static void SPI_RxISR_8BIT(struct __SPI_HandleTypeDef *hspi);
  117. static void SPI_RxISR_8BITCRC(struct __SPI_HandleTypeDef *hspi);
  118. static void SPI_RxISR_16BIT(struct __SPI_HandleTypeDef *hspi);
  119. static void SPI_RxISR_16BITCRC(struct __SPI_HandleTypeDef *hspi);
  120. static void SPI_2linesRxISR_8BIT(struct __SPI_HandleTypeDef *hspi);
  121. static void SPI_2linesRxISR_8BITCRC(struct __SPI_HandleTypeDef *hspi);
  122. static void SPI_2linesTxISR_8BIT(struct __SPI_HandleTypeDef *hspi);
  123. static void SPI_2linesTxISR_16BIT(struct __SPI_HandleTypeDef *hspi);
  124. static void SPI_2linesRxISR_16BIT(struct __SPI_HandleTypeDef *hspi);
  125. static void SPI_2linesRxISR_16BITCRC(struct __SPI_HandleTypeDef *hspi);
  126. static void SPI_CloseRxTx_ISR(SPI_HandleTypeDef *hspi);
  127. static void SPI_CloseRx_ISR(SPI_HandleTypeDef *hspi);
  128. static void SPI_CloseTx_ISR(SPI_HandleTypeDef *hspi);
  129. static HAL_StatusTypeDef SPI_EndRxTransaction(SPI_HandleTypeDef *hspi, uint32_t Timeout);
  130. static HAL_StatusTypeDef SPI_EndRxTxTransaction(SPI_HandleTypeDef *hspi, uint32_t Timeout);
  131. /**
  132. * @}
  133. */
  134. /* Exported functions ---------------------------------------------------------*/
  135. /** @defgroup SPI_Exported_Functions SPI Exported Functions
  136. * @{
  137. */
  138. /** @defgroup SPI_Exported_Functions_Group1 Initialization and de-initialization functions
  139. * @brief Initialization and Configuration functions
  140. *
  141. @verbatim
  142. ===============================================================================
  143. ##### Initialization and de-initialization functions #####
  144. ===============================================================================
  145. [..] This subsection provides a set of functions allowing to initialize and
  146. de-initialize the SPIx peripheral:
  147. (+) User must implement HAL_SPI_MspInit() function in which he configures
  148. all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC ).
  149. (+) Call the function HAL_SPI_Init() to configure the selected device with
  150. the selected configuration:
  151. (++) Mode
  152. (++) Direction
  153. (++) Data Size
  154. (++) Clock Polarity and Phase
  155. (++) NSS Management
  156. (++) BaudRate Prescaler
  157. (++) FirstBit
  158. (++) TIMode
  159. (++) CRC Calculation
  160. (++) CRC Polynomial if CRC enabled
  161. (++) CRC Length, used only with Data8 and Data16
  162. (++) FIFO reception threshold
  163. (+) Call the function HAL_SPI_DeInit() to restore the default configuration
  164. of the selected SPIx peripheral.
  165. @endverbatim
  166. * @{
  167. */
  168. /**
  169. * @brief Initializes the SPI according to the specified parameters
  170. * in the SPI_InitTypeDef and create the associated handle.
  171. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  172. * the configuration information for SPI module.
  173. * @retval HAL status
  174. */
  175. HAL_StatusTypeDef HAL_SPI_Init(SPI_HandleTypeDef *hspi)
  176. {
  177. uint32_t frxth;
  178. /* Check the SPI handle allocation */
  179. if(hspi == NULL)
  180. {
  181. return HAL_ERROR;
  182. }
  183. /* Check the parameters */
  184. assert_param(IS_SPI_ALL_INSTANCE(hspi->Instance));
  185. assert_param(IS_SPI_MODE(hspi->Init.Mode));
  186. assert_param(IS_SPI_DIRECTION(hspi->Init.Direction));
  187. assert_param(IS_SPI_DATASIZE(hspi->Init.DataSize));
  188. assert_param(IS_SPI_CPOL(hspi->Init.CLKPolarity));
  189. assert_param(IS_SPI_CPHA(hspi->Init.CLKPhase));
  190. assert_param(IS_SPI_NSS(hspi->Init.NSS));
  191. assert_param(IS_SPI_NSSP(hspi->Init.NSSPMode));
  192. assert_param(IS_SPI_BAUDRATE_PRESCALER(hspi->Init.BaudRatePrescaler));
  193. assert_param(IS_SPI_FIRST_BIT(hspi->Init.FirstBit));
  194. assert_param(IS_SPI_TIMODE(hspi->Init.TIMode));
  195. assert_param(IS_SPI_CRC_CALCULATION(hspi->Init.CRCCalculation));
  196. assert_param(IS_SPI_CRC_POLYNOMIAL(hspi->Init.CRCPolynomial));
  197. assert_param(IS_SPI_CRC_LENGTH(hspi->Init.CRCLength));
  198. if(hspi->State == HAL_SPI_STATE_RESET)
  199. {
  200. /* Allocate lock resource and initialize it */
  201. hspi->Lock = HAL_UNLOCKED;
  202. /* Init the low level hardware : GPIO, CLOCK, NVIC... */
  203. HAL_SPI_MspInit(hspi);
  204. }
  205. hspi->State = HAL_SPI_STATE_BUSY;
  206. /* Disable the selected SPI peripheral */
  207. __HAL_SPI_DISABLE(hspi);
  208. /* Align by default the rs fifo threshold on the data size */
  209. if(hspi->Init.DataSize > SPI_DATASIZE_8BIT)
  210. {
  211. frxth = SPI_RXFIFO_THRESHOLD_HF;
  212. }
  213. else
  214. {
  215. frxth = SPI_RXFIFO_THRESHOLD_QF;
  216. }
  217. /* CRC calculation is valid only for 16Bit and 8 Bit */
  218. if(( hspi->Init.DataSize != SPI_DATASIZE_16BIT ) && ( hspi->Init.DataSize != SPI_DATASIZE_8BIT ))
  219. {
  220. /* CRC must be disabled */
  221. hspi->Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  222. }
  223. /* Align the CRC Length on the data size */
  224. if( hspi->Init.CRCLength == SPI_CRC_LENGTH_DATASIZE)
  225. {
  226. /* CRC Length aligned on the data size : value set by default */
  227. if(hspi->Init.DataSize > SPI_DATASIZE_8BIT)
  228. {
  229. hspi->Init.CRCLength = SPI_CRC_LENGTH_16BIT;
  230. }
  231. else
  232. {
  233. hspi->Init.CRCLength = SPI_CRC_LENGTH_8BIT;
  234. }
  235. }
  236. /*---------------------------- SPIx CR1 & CR2 Configuration ------------------------*/
  237. /* Configure : SPI Mode, Communication Mode, Clock polarity and phase, NSS management,
  238. Communication speed, First bit, CRC calculation state, CRC Length */
  239. hspi->Instance->CR1 = (hspi->Init.Mode | hspi->Init.Direction |
  240. hspi->Init.CLKPolarity | hspi->Init.CLKPhase | (hspi->Init.NSS & SPI_CR1_SSM) |
  241. hspi->Init.BaudRatePrescaler | hspi->Init.FirstBit | hspi->Init.CRCCalculation);
  242. if( hspi->Init.CRCLength == SPI_CRC_LENGTH_16BIT)
  243. {
  244. hspi->Instance->CR1|= SPI_CR1_CRCL;
  245. }
  246. /* Configure : NSS management */
  247. /* Configure : Rx Fifo Threshold */
  248. hspi->Instance->CR2 = (((hspi->Init.NSS >> 16) & SPI_CR2_SSOE) | hspi->Init.TIMode | hspi->Init.NSSPMode |
  249. hspi->Init.DataSize ) | frxth;
  250. /*---------------------------- SPIx CRCPOLY Configuration --------------------*/
  251. /* Configure : CRC Polynomial */
  252. hspi->Instance->CRCPR = hspi->Init.CRCPolynomial;
  253. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  254. hspi->State= HAL_SPI_STATE_READY;
  255. return HAL_OK;
  256. }
  257. /**
  258. * @brief DeInitializes the SPI peripheral
  259. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  260. * the configuration information for SPI module.
  261. * @retval HAL status
  262. */
  263. HAL_StatusTypeDef HAL_SPI_DeInit(SPI_HandleTypeDef *hspi)
  264. {
  265. /* Check the SPI handle allocation */
  266. if(hspi == NULL)
  267. {
  268. return HAL_ERROR;
  269. }
  270. /* Check the parameters */
  271. assert_param(IS_SPI_ALL_INSTANCE(hspi->Instance));
  272. hspi->State = HAL_SPI_STATE_BUSY;
  273. /* check flag before the SPI disable */
  274. SPI_WaitFifoStateUntilTimeout(hspi, SPI_FLAG_FTLVL, SPI_FTLVL_EMPTY, SPI_DEFAULT_TIMEOUT);
  275. SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_BSY, RESET, SPI_DEFAULT_TIMEOUT);
  276. SPI_WaitFifoStateUntilTimeout(hspi, SPI_FLAG_FRLVL, SPI_FRLVL_EMPTY, SPI_DEFAULT_TIMEOUT);
  277. /* Disable the SPI Peripheral Clock */
  278. __HAL_SPI_DISABLE(hspi);
  279. /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */
  280. HAL_SPI_MspDeInit(hspi);
  281. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  282. hspi->State = HAL_SPI_STATE_RESET;
  283. __HAL_UNLOCK(hspi);
  284. return HAL_OK;
  285. }
  286. /**
  287. * @brief SPI MSP Init
  288. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  289. * the configuration information for SPI module.
  290. * @retval None
  291. */
  292. __weak void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi)
  293. {
  294. /* NOTE : This function should not be modified, when the callback is needed,
  295. the HAL_SPI_MspInit should be implemented in the user file
  296. */
  297. }
  298. /**
  299. * @brief SPI MSP DeInit
  300. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  301. * the configuration information for SPI module.
  302. * @retval None
  303. */
  304. __weak void HAL_SPI_MspDeInit(SPI_HandleTypeDef *hspi)
  305. {
  306. /* NOTE : This function should not be modified, when the callback is needed,
  307. the HAL_SPI_MspDeInit should be implemented in the user file
  308. */
  309. }
  310. /**
  311. * @}
  312. */
  313. /** @defgroup SPI_Exported_Functions_Group2 IO operation functions
  314. * @brief Data transfers functions
  315. *
  316. @verbatim
  317. ==============================================================================
  318. ##### IO operation functions #####
  319. ===============================================================================
  320. This subsection provides a set of functions allowing to manage the SPI
  321. data transfers.
  322. [..] The SPI supports master and slave mode :
  323. (#) There are two modes of transfer:
  324. (++) Blocking mode: The communication is performed in polling mode.
  325. The HAL status of all data processing is returned by the same function
  326. after finishing transfer.
  327. (++) No-Blocking mode: The communication is performed using Interrupts
  328. or DMA, These APIs return the HAL status.
  329. The end of the data processing will be indicated through the
  330. dedicated SPI IRQ when using Interrupt mode or the DMA IRQ when
  331. using DMA mode.
  332. The HAL_SPI_TxCpltCallback(), HAL_SPI_RxCpltCallback() and HAL_SPI_TxRxCpltCallback() user callbacks
  333. will be executed respectively at the end of the transmit or Receive process
  334. The HAL_SPI_ErrorCallback()user callback will be executed when a communication error is detected
  335. (#) APIs provided for these 2 transfer modes (Blocking mode or Non blocking mode using either Interrupt or DMA)
  336. exist for 1Line (simplex) and 2Lines (full duplex) modes.
  337. @endverbatim
  338. * @{
  339. */
  340. /**
  341. * @brief Transmit an amount of data in blocking mode
  342. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  343. * the configuration information for SPI module.
  344. * @param pData: pointer to data buffer
  345. * @param Size: amount of data to be sent
  346. * @param Timeout: Timeout duration
  347. * @retval HAL status
  348. */
  349. HAL_StatusTypeDef HAL_SPI_Transmit(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  350. {
  351. assert_param(IS_SPI_DIRECTION_2LINES_OR_1LINE(hspi->Init.Direction));
  352. /* Process Locked */
  353. __HAL_LOCK(hspi);
  354. if(hspi->State != HAL_SPI_STATE_READY)
  355. {
  356. hspi->State = HAL_SPI_STATE_READY;
  357. /* Process Unlocked */
  358. __HAL_UNLOCK(hspi);
  359. return HAL_BUSY;
  360. }
  361. if((pData == NULL ) || (Size == 0))
  362. {
  363. hspi->State = HAL_SPI_STATE_READY;
  364. /* Process Unlocked */
  365. __HAL_UNLOCK(hspi);
  366. return HAL_ERROR;
  367. }
  368. /* Set the transaction information */
  369. hspi->State = HAL_SPI_STATE_BUSY_TX;
  370. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  371. hspi->pTxBuffPtr = pData;
  372. hspi->TxXferSize = Size;
  373. hspi->TxXferCount = Size;
  374. hspi->pRxBuffPtr = (uint8_t *)NULL;
  375. hspi->RxXferSize = 0;
  376. hspi->RxXferCount = 0;
  377. /* Configure communication direction : 1Line */
  378. if(hspi->Init.Direction == SPI_DIRECTION_1LINE)
  379. {
  380. SPI_1LINE_TX(hspi);
  381. }
  382. /* Reset CRC Calculation */
  383. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  384. {
  385. SPI_RESET_CRC(hspi);
  386. }
  387. /* Check if the SPI is already enabled */
  388. if((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
  389. {
  390. /* Enable SPI peripheral */
  391. __HAL_SPI_ENABLE(hspi);
  392. }
  393. /* Transmit data in 16 Bit mode */
  394. if(hspi->Init.DataSize > SPI_DATASIZE_8BIT)
  395. {
  396. /* Transmit data in 16 Bit mode */
  397. while (hspi->TxXferCount > 0)
  398. {
  399. /* Wait until TXE flag is set to send data */
  400. if(SPI_WaitFlagStateUntilTimeout(hspi,SPI_FLAG_TXE,SPI_FLAG_TXE,Timeout) != HAL_OK)
  401. {
  402. hspi->State = HAL_SPI_STATE_READY;
  403. /* Process Unlocked */
  404. __HAL_UNLOCK(hspi);
  405. return HAL_TIMEOUT;
  406. }
  407. hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr);
  408. hspi->pTxBuffPtr += sizeof(uint16_t);
  409. hspi->TxXferCount--;
  410. }
  411. }
  412. /* Transmit data in 8 Bit mode */
  413. else
  414. {
  415. while (hspi->TxXferCount > 0)
  416. {
  417. if(hspi->TxXferCount != 0x1)
  418. {
  419. /* Wait until TXE flag is set to send data */
  420. if(SPI_WaitFlagStateUntilTimeout(hspi,SPI_FLAG_TXE,SPI_FLAG_TXE,Timeout) != HAL_OK)
  421. {
  422. hspi->State = HAL_SPI_STATE_READY;
  423. /* Process Unlocked */
  424. __HAL_UNLOCK(hspi);
  425. return HAL_TIMEOUT;
  426. }
  427. hspi->Instance->DR = *((uint16_t*)hspi->pTxBuffPtr);
  428. hspi->pTxBuffPtr += sizeof(uint16_t);
  429. hspi->TxXferCount -= 2;
  430. }
  431. else
  432. {
  433. /* Wait until TXE flag is set to send data */
  434. if(SPI_WaitFlagStateUntilTimeout(hspi,SPI_FLAG_TXE,SPI_FLAG_TXE,Timeout) != HAL_OK)
  435. {
  436. return HAL_TIMEOUT;
  437. }
  438. *((__IO uint8_t*)&hspi->Instance->DR) = (*hspi->pTxBuffPtr++);
  439. hspi->TxXferCount--;
  440. }
  441. }
  442. }
  443. /* Enable CRC Transmission */
  444. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  445. {
  446. hspi->Instance->CR1|= SPI_CR1_CRCNEXT;
  447. }
  448. /* Check the end of the transaction */
  449. if(SPI_EndRxTxTransaction(hspi,Timeout) != HAL_OK)
  450. {
  451. return HAL_TIMEOUT;
  452. }
  453. /* Clear OVERUN flag in 2 Lines communication mode because received is not read */
  454. if(hspi->Init.Direction == SPI_DIRECTION_2LINES)
  455. {
  456. __HAL_SPI_CLEAR_OVRFLAG(hspi);
  457. }
  458. hspi->State = HAL_SPI_STATE_READY;
  459. /* Process Unlocked */
  460. __HAL_UNLOCK(hspi);
  461. if(hspi->ErrorCode != HAL_SPI_ERROR_NONE)
  462. {
  463. return HAL_ERROR;
  464. }
  465. else
  466. {
  467. return HAL_OK;
  468. }
  469. }
  470. /**
  471. * @brief Receive an amount of data in blocking mode
  472. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  473. * the configuration information for SPI module.
  474. * @param pData: pointer to data buffer
  475. * @param Size: amount of data to be received
  476. * @param Timeout: Timeout duration
  477. * @retval HAL status
  478. */
  479. HAL_StatusTypeDef HAL_SPI_Receive(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  480. {
  481. __IO uint16_t tmpreg;
  482. if(hspi->State != HAL_SPI_STATE_READY)
  483. {
  484. return HAL_BUSY;
  485. }
  486. if((pData == NULL ) || (Size == 0))
  487. {
  488. return HAL_ERROR;
  489. }
  490. if((hspi->Init.Mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES))
  491. {
  492. /* the receive process is not supported in 2Lines direction master mode */
  493. /* in this case we call the transmitReceive process */
  494. return HAL_SPI_TransmitReceive(hspi,pData,pData,Size,Timeout);
  495. }
  496. /* Process Locked */
  497. __HAL_LOCK(hspi);
  498. hspi->State = HAL_SPI_STATE_BUSY_RX;
  499. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  500. hspi->pRxBuffPtr = pData;
  501. hspi->RxXferSize = Size;
  502. hspi->RxXferCount = Size;
  503. hspi->pTxBuffPtr = (uint8_t *)NULL;
  504. hspi->TxXferSize = 0;
  505. hspi->TxXferCount = 0;
  506. /* Reset CRC Calculation */
  507. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  508. {
  509. SPI_RESET_CRC(hspi);
  510. /* this is done to handle the CRCNEXT before the latest data */
  511. hspi->RxXferCount--;
  512. }
  513. /* Set the Rx Fido threshold */
  514. if(hspi->Init.DataSize > SPI_DATASIZE_8BIT)
  515. {
  516. /* set fiforxthreshold according the reception data length: 16bit */
  517. CLEAR_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
  518. }
  519. else
  520. {
  521. /* set fiforxthreshold according the reception data length: 8bit */
  522. SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
  523. }
  524. /* Configure communication direction 1Line and enabled SPI if needed */
  525. if(hspi->Init.Direction == SPI_DIRECTION_1LINE)
  526. {
  527. SPI_1LINE_RX(hspi);
  528. }
  529. /* Check if the SPI is already enabled */
  530. if((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
  531. {
  532. /* Enable SPI peripheral */
  533. __HAL_SPI_ENABLE(hspi);
  534. }
  535. /* Receive data in 8 Bit mode */
  536. if(hspi->Init.DataSize <= SPI_DATASIZE_8BIT)
  537. {
  538. while(hspi->RxXferCount > 1)
  539. {
  540. /* Wait until the RXNE flag */
  541. if(SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SPI_FLAG_RXNE, Timeout) != HAL_OK)
  542. {
  543. return HAL_TIMEOUT;
  544. }
  545. (*hspi->pRxBuffPtr++)= *(__IO uint8_t *)&hspi->Instance->DR;
  546. hspi->RxXferCount--;
  547. }
  548. }
  549. else /* Receive data in 16 Bit mode */
  550. {
  551. while(hspi->RxXferCount > 1 )
  552. {
  553. /* Wait until RXNE flag is reset to read data */
  554. if(SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SPI_FLAG_RXNE, Timeout) != HAL_OK)
  555. {
  556. return HAL_TIMEOUT;
  557. }
  558. *((uint16_t*)hspi->pRxBuffPtr) = hspi->Instance->DR;
  559. hspi->pRxBuffPtr += sizeof(uint16_t);
  560. hspi->RxXferCount--;
  561. }
  562. }
  563. /* Enable CRC Transmission */
  564. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  565. {
  566. hspi->Instance->CR1 |= SPI_CR1_CRCNEXT;
  567. }
  568. /* Wait until RXNE flag is set */
  569. if(SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SPI_FLAG_RXNE, Timeout) != HAL_OK)
  570. {
  571. return HAL_TIMEOUT;
  572. }
  573. /* Receive last data in 16 Bit mode */
  574. if(hspi->Init.DataSize > SPI_DATASIZE_8BIT)
  575. {
  576. *((uint16_t*)hspi->pRxBuffPtr) = hspi->Instance->DR;
  577. hspi->pRxBuffPtr += sizeof(uint16_t);
  578. }
  579. /* Receive last data in 8 Bit mode */
  580. else
  581. {
  582. (*hspi->pRxBuffPtr++) = *(__IO uint8_t *)&hspi->Instance->DR;
  583. }
  584. hspi->RxXferCount--;
  585. /* Read CRC from DR to close CRC calculation process */
  586. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  587. {
  588. /* Wait until TXE flag */
  589. if(SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SPI_FLAG_RXNE, Timeout) != HAL_OK)
  590. {
  591. /* Error on the CRC reception */
  592. hspi->ErrorCode|= HAL_SPI_ERROR_CRC;
  593. }
  594. if(hspi->Init.DataSize > SPI_DATASIZE_8BIT)
  595. {
  596. tmpreg = hspi->Instance->DR;
  597. UNUSED(tmpreg); /* To avoid GCC warning */
  598. }
  599. else
  600. {
  601. tmpreg = *(__IO uint8_t *)&hspi->Instance->DR;
  602. UNUSED(tmpreg); /* To avoid GCC warning */
  603. if((hspi->Init.DataSize == SPI_DATASIZE_8BIT) && (hspi->Init.CRCLength == SPI_CRC_LENGTH_16BIT))
  604. {
  605. if(SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SPI_FLAG_RXNE, Timeout) != HAL_OK)
  606. {
  607. /* Error on the CRC reception */
  608. hspi->ErrorCode|= HAL_SPI_ERROR_FLAG;
  609. }
  610. tmpreg = *(__IO uint8_t *)&hspi->Instance->DR;
  611. UNUSED(tmpreg); /* To avoid GCC warning */
  612. }
  613. }
  614. }
  615. /* Check the end of the transaction */
  616. if(SPI_EndRxTransaction(hspi,Timeout) != HAL_OK)
  617. {
  618. return HAL_TIMEOUT;
  619. }
  620. hspi->State = HAL_SPI_STATE_READY;
  621. /* Check if CRC error occurred */
  622. if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET)
  623. {
  624. hspi->ErrorCode|= HAL_SPI_ERROR_CRC;
  625. __HAL_SPI_CLEAR_CRCERRFLAG(hspi);
  626. /* Process Unlocked */
  627. __HAL_UNLOCK(hspi);
  628. return HAL_ERROR;
  629. }
  630. /* Process Unlocked */
  631. __HAL_UNLOCK(hspi);
  632. if(hspi->ErrorCode != HAL_SPI_ERROR_NONE)
  633. {
  634. return HAL_ERROR;
  635. }
  636. else
  637. {
  638. return HAL_OK;
  639. }
  640. }
  641. /**
  642. * @brief Transmit and Receive an amount of data in blocking mode
  643. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  644. * the configuration information for SPI module.
  645. * @param pTxData: pointer to transmission data buffer
  646. * @param pRxData: pointer to reception data buffer
  647. * @param Size: amount of data to be sent and received
  648. * @param Timeout: Timeout duration
  649. * @retval HAL status
  650. */
  651. HAL_StatusTypeDef HAL_SPI_TransmitReceive(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size, uint32_t Timeout)
  652. {
  653. __IO uint16_t tmpreg = 0;
  654. uint32_t tickstart = HAL_GetTick();
  655. assert_param(IS_SPI_DIRECTION_2LINES(hspi->Init.Direction));
  656. if(hspi->State != HAL_SPI_STATE_READY)
  657. {
  658. return HAL_BUSY;
  659. }
  660. if((pTxData == NULL) || (pRxData == NULL) || (Size == 0))
  661. {
  662. return HAL_ERROR;
  663. }
  664. /* Process Locked */
  665. __HAL_LOCK(hspi);
  666. hspi->State = HAL_SPI_STATE_BUSY_TX_RX;
  667. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  668. hspi->pRxBuffPtr = pRxData;
  669. hspi->RxXferCount = Size;
  670. hspi->RxXferSize = Size;
  671. hspi->pTxBuffPtr = pTxData;
  672. hspi->TxXferCount = Size;
  673. hspi->TxXferSize = Size;
  674. /* Reset CRC Calculation */
  675. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  676. {
  677. SPI_RESET_CRC(hspi);
  678. }
  679. /* Set the Rx Fido threshold */
  680. if((hspi->Init.DataSize > SPI_DATASIZE_8BIT) || (hspi->RxXferCount > 1))
  681. {
  682. /* set fiforxthreshold according the reception data length: 16bit */
  683. CLEAR_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
  684. }
  685. else
  686. {
  687. /* set fiforxthreshold according the reception data length: 8bit */
  688. SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
  689. }
  690. /* Check if the SPI is already enabled */
  691. if((hspi->Instance->CR1 &SPI_CR1_SPE) != SPI_CR1_SPE)
  692. {
  693. /* Enable SPI peripheral */
  694. __HAL_SPI_ENABLE(hspi);
  695. }
  696. /* Transmit and Receive data in 16 Bit mode */
  697. if(hspi->Init.DataSize > SPI_DATASIZE_8BIT)
  698. {
  699. while ((hspi->TxXferCount > 0 ) || (hspi->RxXferCount > 0))
  700. {
  701. /* Check TXE flag */
  702. if((hspi->TxXferCount > 0) && ((hspi->Instance->SR & SPI_FLAG_TXE) == SPI_FLAG_TXE))
  703. {
  704. hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr);
  705. hspi->pTxBuffPtr += sizeof(uint16_t);
  706. hspi->TxXferCount--;
  707. /* Enable CRC Transmission */
  708. if((hspi->TxXferCount == 0) && (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE))
  709. {
  710. SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
  711. }
  712. }
  713. /* Check RXNE flag */
  714. if((hspi->RxXferCount > 0) && ((hspi->Instance->SR & SPI_FLAG_RXNE) == SPI_FLAG_RXNE))
  715. {
  716. *((uint16_t *)hspi->pRxBuffPtr) = hspi->Instance->DR;
  717. hspi->pRxBuffPtr += sizeof(uint16_t);
  718. hspi->RxXferCount--;
  719. }
  720. if(Timeout != HAL_MAX_DELAY)
  721. {
  722. if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
  723. {
  724. hspi->State = HAL_SPI_STATE_READY;
  725. __HAL_UNLOCK(hspi);
  726. return HAL_TIMEOUT;
  727. }
  728. }
  729. }
  730. }
  731. /* Transmit and Receive data in 8 Bit mode */
  732. else
  733. {
  734. while((hspi->TxXferCount > 0) || (hspi->RxXferCount > 0))
  735. {
  736. /* check TXE flag */
  737. if((hspi->TxXferCount > 0) && ((hspi->Instance->SR & SPI_FLAG_TXE) == SPI_FLAG_TXE))
  738. {
  739. if(hspi->TxXferCount > 1)
  740. {
  741. hspi->Instance->DR = *((uint16_t*)hspi->pTxBuffPtr);
  742. hspi->pTxBuffPtr += sizeof(uint16_t);
  743. hspi->TxXferCount -= 2;
  744. }
  745. else
  746. {
  747. *(__IO uint8_t *)&hspi->Instance->DR = (*hspi->pTxBuffPtr++);
  748. hspi->TxXferCount--;
  749. }
  750. /* Enable CRC Transmission */
  751. if((hspi->TxXferCount == 0) && (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE))
  752. {
  753. SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
  754. }
  755. }
  756. /* Wait until RXNE flag is reset */
  757. if((hspi->RxXferCount > 0) && ((hspi->Instance->SR & SPI_FLAG_RXNE) == SPI_FLAG_RXNE))
  758. {
  759. if(hspi->RxXferCount > 1)
  760. {
  761. *((uint16_t*)hspi->pRxBuffPtr) = hspi->Instance->DR;
  762. hspi->pRxBuffPtr += sizeof(uint16_t);
  763. hspi->RxXferCount -= 2;
  764. if(hspi->RxXferCount <= 1)
  765. {
  766. /* set fiforxthreshold before to switch on 8 bit data size */
  767. SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
  768. }
  769. }
  770. else
  771. {
  772. (*hspi->pRxBuffPtr++) = *(__IO uint8_t *)&hspi->Instance->DR;
  773. hspi->RxXferCount--;
  774. }
  775. }
  776. if(Timeout != HAL_MAX_DELAY)
  777. {
  778. if((Timeout == 0) || ((HAL_GetTick()-tickstart) > Timeout))
  779. {
  780. hspi->State = HAL_SPI_STATE_READY;
  781. __HAL_UNLOCK(hspi);
  782. return HAL_TIMEOUT;
  783. }
  784. }
  785. }
  786. }
  787. /* Read CRC from DR to close CRC calculation process */
  788. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  789. {
  790. /* Wait until TXE flag */
  791. if(SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SPI_FLAG_RXNE, Timeout) != HAL_OK)
  792. {
  793. /* Error on the CRC reception */
  794. hspi->ErrorCode|= HAL_SPI_ERROR_CRC;
  795. }
  796. if(hspi->Init.DataSize == SPI_DATASIZE_16BIT)
  797. {
  798. tmpreg = hspi->Instance->DR;
  799. UNUSED(tmpreg); /* To avoid GCC warning */
  800. }
  801. else
  802. {
  803. tmpreg = *(__IO uint8_t *)&hspi->Instance->DR;
  804. UNUSED(tmpreg); /* To avoid GCC warning */
  805. if(hspi->Init.CRCLength == SPI_CRC_LENGTH_16BIT)
  806. {
  807. if(SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SPI_FLAG_RXNE, Timeout) != HAL_OK)
  808. {
  809. /* Error on the CRC reception */
  810. hspi->ErrorCode|= HAL_SPI_ERROR_CRC;
  811. }
  812. tmpreg = *(__IO uint8_t *)&hspi->Instance->DR;
  813. UNUSED(tmpreg); /* To avoid GCC warning */
  814. }
  815. }
  816. }
  817. /* Check the end of the transaction */
  818. if(SPI_EndRxTxTransaction(hspi,Timeout) != HAL_OK)
  819. {
  820. return HAL_TIMEOUT;
  821. }
  822. hspi->State = HAL_SPI_STATE_READY;
  823. /* Check if CRC error occurred */
  824. if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET)
  825. {
  826. hspi->ErrorCode|= HAL_SPI_ERROR_CRC;
  827. /* Clear CRC Flag */
  828. __HAL_SPI_CLEAR_CRCERRFLAG(hspi);
  829. /* Process Unlocked */
  830. __HAL_UNLOCK(hspi);
  831. return HAL_ERROR;
  832. }
  833. /* Process Unlocked */
  834. __HAL_UNLOCK(hspi);
  835. if(hspi->ErrorCode != HAL_SPI_ERROR_NONE)
  836. {
  837. return HAL_ERROR;
  838. }
  839. else
  840. {
  841. return HAL_OK;
  842. }
  843. }
  844. /**
  845. * @brief Transmit an amount of data in no-blocking mode with Interrupt
  846. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  847. * the configuration information for SPI module.
  848. * @param pData: pointer to data buffer
  849. * @param Size: amount of data to be sent
  850. * @retval HAL status
  851. */
  852. HAL_StatusTypeDef HAL_SPI_Transmit_IT(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size)
  853. {
  854. assert_param(IS_SPI_DIRECTION_2LINES_OR_1LINE(hspi->Init.Direction));
  855. if(hspi->State == HAL_SPI_STATE_READY)
  856. {
  857. if((pData == NULL) || (Size == 0))
  858. {
  859. return HAL_ERROR;
  860. }
  861. /* Process Locked */
  862. __HAL_LOCK(hspi);
  863. hspi->State = HAL_SPI_STATE_BUSY_TX;
  864. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  865. hspi->pTxBuffPtr = pData;
  866. hspi->TxXferSize = Size;
  867. hspi->TxXferCount = Size;
  868. hspi->pRxBuffPtr = NULL;
  869. hspi->RxXferSize = 0;
  870. hspi->RxXferCount = 0;
  871. /* Set the function for IT treatement */
  872. if(hspi->Init.DataSize > SPI_DATASIZE_8BIT )
  873. {
  874. hspi->RxISR = NULL;
  875. hspi->TxISR = SPI_TxISR_16BIT;
  876. }
  877. else
  878. {
  879. hspi->RxISR = NULL;
  880. hspi->TxISR = SPI_TxISR_8BIT;
  881. }
  882. /* Configure communication direction : 1Line */
  883. if(hspi->Init.Direction == SPI_DIRECTION_1LINE)
  884. {
  885. SPI_1LINE_TX(hspi);
  886. }
  887. /* Reset CRC Calculation */
  888. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  889. {
  890. SPI_RESET_CRC(hspi);
  891. }
  892. /* Enable TXE and ERR interrupt */
  893. __HAL_SPI_ENABLE_IT(hspi,(SPI_IT_TXE));
  894. /* Process Unlocked */
  895. __HAL_UNLOCK(hspi);
  896. /* Note : The SPI must be enabled after unlocking current process
  897. to avoid the risk of SPI interrupt handle execution before current
  898. process unlock */
  899. /* Check if the SPI is already enabled */
  900. if((hspi->Instance->CR1 &SPI_CR1_SPE) != SPI_CR1_SPE)
  901. {
  902. /* Enable SPI peripheral */
  903. __HAL_SPI_ENABLE(hspi);
  904. }
  905. return HAL_OK;
  906. }
  907. else
  908. {
  909. return HAL_BUSY;
  910. }
  911. }
  912. /**
  913. * @brief Receive an amount of data in no-blocking mode with Interrupt
  914. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  915. * the configuration information for SPI module.
  916. * @param pData: pointer to data buffer
  917. * @param Size: amount of data to be sent
  918. * @retval HAL status
  919. */
  920. HAL_StatusTypeDef HAL_SPI_Receive_IT(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size)
  921. {
  922. if(hspi->State == HAL_SPI_STATE_READY)
  923. {
  924. if((pData == NULL) || (Size == 0))
  925. {
  926. return HAL_ERROR;
  927. }
  928. /* Process Locked */
  929. __HAL_LOCK(hspi);
  930. /* Configure communication */
  931. hspi->State = HAL_SPI_STATE_BUSY_RX;
  932. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  933. hspi->pRxBuffPtr = pData;
  934. hspi->RxXferSize = Size;
  935. hspi->RxXferCount = Size;
  936. hspi->pTxBuffPtr = NULL;
  937. hspi->TxXferSize = 0;
  938. hspi->TxXferCount = 0;
  939. if((hspi->Init.Mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES))
  940. {
  941. /* Process Unlocked */
  942. __HAL_UNLOCK(hspi);
  943. /* the receive process is not supported in 2Lines direction master mode */
  944. /* in this we call the transmitReceive process */
  945. return HAL_SPI_TransmitReceive_IT(hspi,pData,pData,Size);
  946. }
  947. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  948. {
  949. hspi->CRCSize = 1;
  950. if((hspi->Init.DataSize <= SPI_DATASIZE_8BIT) && (hspi->Init.CRCLength == SPI_CRC_LENGTH_16BIT))
  951. {
  952. hspi->CRCSize = 2;
  953. }
  954. }
  955. else
  956. {
  957. hspi->CRCSize = 0;
  958. }
  959. /* check the data size to adapt Rx threshold and the set the function for IT treatment */
  960. if(hspi->Init.DataSize > SPI_DATASIZE_8BIT )
  961. {
  962. /* set fiforxthreshold according the reception data length: 16 bit */
  963. CLEAR_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
  964. hspi->RxISR = SPI_RxISR_16BIT;
  965. hspi->TxISR = NULL;
  966. }
  967. else
  968. {
  969. /* set fiforxthreshold according the reception data length: 8 bit */
  970. SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
  971. hspi->RxISR = SPI_RxISR_8BIT;
  972. hspi->TxISR = NULL;
  973. }
  974. /* Configure communication direction : 1Line */
  975. if(hspi->Init.Direction == SPI_DIRECTION_1LINE)
  976. {
  977. SPI_1LINE_RX(hspi);
  978. }
  979. /* Reset CRC Calculation */
  980. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  981. {
  982. SPI_RESET_CRC(hspi);
  983. }
  984. /* Enable TXE and ERR interrupt */
  985. __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_RXNE | SPI_IT_ERR));
  986. /* Process Unlocked */
  987. __HAL_UNLOCK(hspi);
  988. /* Note : The SPI must be enabled after unlocking current process
  989. to avoid the risk of SPI interrupt handle execution before current
  990. process unlock */
  991. /* Check if the SPI is already enabled */
  992. if((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
  993. {
  994. /* Enable SPI peripheral */
  995. __HAL_SPI_ENABLE(hspi);
  996. }
  997. return HAL_OK;
  998. }
  999. else
  1000. {
  1001. return HAL_BUSY;
  1002. }
  1003. }
  1004. /**
  1005. * @brief Transmit and Receive an amount of data in no-blocking mode with Interrupt
  1006. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1007. * the configuration information for SPI module.
  1008. * @param pTxData: pointer to transmission data buffer
  1009. * @param pRxData: pointer to reception data buffer
  1010. * @param Size: amount of data to be sent and received
  1011. * @retval HAL status
  1012. */
  1013. HAL_StatusTypeDef HAL_SPI_TransmitReceive_IT(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size)
  1014. {
  1015. assert_param(IS_SPI_DIRECTION_2LINES(hspi->Init.Direction));
  1016. if((hspi->State == HAL_SPI_STATE_READY) || \
  1017. ((hspi->Init.Mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES) && (hspi->State == HAL_SPI_STATE_BUSY_RX)))
  1018. {
  1019. if((pTxData == NULL ) || (pRxData == NULL ) || (Size == 0))
  1020. {
  1021. return HAL_ERROR;
  1022. }
  1023. /* Process locked */
  1024. __HAL_LOCK(hspi);
  1025. hspi->CRCSize = 0;
  1026. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  1027. {
  1028. hspi->CRCSize = 1;
  1029. if((hspi->Init.DataSize <= SPI_DATASIZE_8BIT) && (hspi->Init.CRCLength == SPI_CRC_LENGTH_16BIT))
  1030. {
  1031. hspi->CRCSize = 2;
  1032. }
  1033. }
  1034. if(hspi->State != HAL_SPI_STATE_BUSY_RX)
  1035. {
  1036. hspi->State = HAL_SPI_STATE_BUSY_TX_RX;
  1037. }
  1038. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  1039. hspi->pTxBuffPtr = pTxData;
  1040. hspi->TxXferSize = Size;
  1041. hspi->TxXferCount = Size;
  1042. hspi->pRxBuffPtr = pRxData;
  1043. hspi->RxXferSize = Size;
  1044. hspi->RxXferCount = Size;
  1045. /* Set the function for IT treatement */
  1046. if(hspi->Init.DataSize > SPI_DATASIZE_8BIT )
  1047. {
  1048. hspi->RxISR = SPI_2linesRxISR_16BIT;
  1049. hspi->TxISR = SPI_2linesTxISR_16BIT;
  1050. }
  1051. else
  1052. {
  1053. hspi->RxISR = SPI_2linesRxISR_8BIT;
  1054. hspi->TxISR = SPI_2linesTxISR_8BIT;
  1055. }
  1056. /* Reset CRC Calculation */
  1057. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  1058. {
  1059. SPI_RESET_CRC(hspi);
  1060. }
  1061. /* check if packing mode is enabled and if there is more than 2 data to receive */
  1062. if((hspi->Init.DataSize > SPI_DATASIZE_8BIT) || (hspi->RxXferCount >= 2))
  1063. {
  1064. /* set fiforxthreshold according the reception data length: 16 bit */
  1065. CLEAR_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
  1066. }
  1067. else
  1068. {
  1069. /* set fiforxthreshold according the reception data length: 8 bit */
  1070. SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
  1071. }
  1072. /* Enable TXE, RXNE and ERR interrupt */
  1073. __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_TXE | SPI_IT_RXNE | SPI_IT_ERR));
  1074. /* Process Unlocked */
  1075. __HAL_UNLOCK(hspi);
  1076. /* Check if the SPI is already enabled */
  1077. if((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
  1078. {
  1079. /* Enable SPI peripheral */
  1080. __HAL_SPI_ENABLE(hspi);
  1081. }
  1082. return HAL_OK;
  1083. }
  1084. else
  1085. {
  1086. return HAL_BUSY;
  1087. }
  1088. }
  1089. /**
  1090. * @brief Transmit an amount of data in no-blocking mode with DMA
  1091. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1092. * the configuration information for SPI module.
  1093. * @param pData: pointer to data buffer
  1094. * @param Size: amount of data to be sent
  1095. * @retval HAL status
  1096. */
  1097. HAL_StatusTypeDef HAL_SPI_Transmit_DMA(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size)
  1098. {
  1099. assert_param(IS_SPI_DIRECTION_2LINES_OR_1LINE(hspi->Init.Direction));
  1100. if(hspi->State != HAL_SPI_STATE_READY)
  1101. {
  1102. return HAL_BUSY;
  1103. }
  1104. if((pData == NULL) || (Size == 0))
  1105. {
  1106. return HAL_ERROR;
  1107. }
  1108. /* Process Locked */
  1109. __HAL_LOCK(hspi);
  1110. hspi->State = HAL_SPI_STATE_BUSY_TX;
  1111. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  1112. hspi->pTxBuffPtr = pData;
  1113. hspi->TxXferSize = Size;
  1114. hspi->TxXferCount = Size;
  1115. hspi->pRxBuffPtr = (uint8_t *)NULL;
  1116. hspi->RxXferSize = 0;
  1117. hspi->RxXferCount = 0;
  1118. /* Configure communication direction : 1Line */
  1119. if(hspi->Init.Direction == SPI_DIRECTION_1LINE)
  1120. {
  1121. SPI_1LINE_TX(hspi);
  1122. }
  1123. /* Reset CRC Calculation */
  1124. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  1125. {
  1126. SPI_RESET_CRC(hspi);
  1127. }
  1128. /* Set the SPI TxDMA Half transfer complete callback */
  1129. hspi->hdmatx->XferHalfCpltCallback = SPI_DMAHalfTransmitCplt;
  1130. /* Set the SPI TxDMA transfer complete callback */
  1131. hspi->hdmatx->XferCpltCallback = SPI_DMATransmitCplt;
  1132. /* Set the DMA error callback */
  1133. hspi->hdmatx->XferErrorCallback = SPI_DMAError;
  1134. CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_LDMATX);
  1135. /* packing mode is enabled only if the DMA setting is HALWORD */
  1136. if((hspi->Init.DataSize <= SPI_DATASIZE_8BIT) && (hspi->hdmatx->Init.MemDataAlignment == DMA_MDATAALIGN_HALFWORD))
  1137. {
  1138. /* Check the even/odd of the data size + crc if enabled */
  1139. if((hspi->TxXferCount & 0x1) == 0)
  1140. {
  1141. CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_LDMATX);
  1142. hspi->TxXferCount = (hspi->TxXferCount >> 1);
  1143. }
  1144. else
  1145. {
  1146. SET_BIT(hspi->Instance->CR2, SPI_CR2_LDMATX);
  1147. hspi->TxXferCount = (hspi->TxXferCount >> 1) + 1;
  1148. }
  1149. }
  1150. /* Enable the Tx DMA channel */
  1151. HAL_DMA_Start_IT(hspi->hdmatx, (uint32_t)hspi->pTxBuffPtr, (uint32_t)&hspi->Instance->DR, hspi->TxXferCount);
  1152. /* Check if the SPI is already enabled */
  1153. if((hspi->Instance->CR1 &SPI_CR1_SPE) != SPI_CR1_SPE)
  1154. {
  1155. /* Enable SPI peripheral */
  1156. __HAL_SPI_ENABLE(hspi);
  1157. }
  1158. /* Enable Tx DMA Request */
  1159. hspi->Instance->CR2 |= SPI_CR2_TXDMAEN;
  1160. /* Process Unlocked */
  1161. __HAL_UNLOCK(hspi);
  1162. return HAL_OK;
  1163. }
  1164. /**
  1165. * @brief Receive an amount of data in no-blocking mode with DMA
  1166. * @param hspi: SPI handle
  1167. * @param pData: pointer to data buffer
  1168. * @param Size: amount of data to be sent
  1169. * @retval HAL status
  1170. */
  1171. HAL_StatusTypeDef HAL_SPI_Receive_DMA(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size)
  1172. {
  1173. if(hspi->State != HAL_SPI_STATE_READY)
  1174. {
  1175. return HAL_BUSY;
  1176. }
  1177. if((pData == NULL) || (Size == 0))
  1178. {
  1179. return HAL_ERROR;
  1180. }
  1181. /* Process Locked */
  1182. __HAL_LOCK(hspi);
  1183. hspi->State = HAL_SPI_STATE_BUSY_RX;
  1184. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  1185. hspi->pRxBuffPtr = pData;
  1186. hspi->RxXferSize = Size;
  1187. hspi->RxXferCount = Size;
  1188. hspi->pTxBuffPtr = (uint8_t *)NULL;
  1189. hspi->TxXferSize = 0;
  1190. hspi->TxXferCount = 0;
  1191. if((hspi->Init.Mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES))
  1192. {
  1193. /* Process Unlocked */
  1194. __HAL_UNLOCK(hspi);
  1195. /* the receive process is not supported in 2Lines direction master mode */
  1196. /* in this case we call the transmitReceive process */
  1197. return HAL_SPI_TransmitReceive_DMA(hspi,pData,pData,Size);
  1198. }
  1199. /* Configure communication direction : 1Line */
  1200. if(hspi->Init.Direction == SPI_DIRECTION_1LINE)
  1201. {
  1202. SPI_1LINE_RX(hspi);
  1203. }
  1204. /* Reset CRC Calculation */
  1205. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  1206. {
  1207. SPI_RESET_CRC(hspi);
  1208. }
  1209. /* packing mode management is enabled by the DMA settings */
  1210. if((hspi->Init.DataSize <= SPI_DATASIZE_8BIT) && (hspi->hdmarx->Init.MemDataAlignment == DMA_MDATAALIGN_HALFWORD))
  1211. {
  1212. /* Process Locked */
  1213. __HAL_UNLOCK(hspi);
  1214. /* Restriction the DMA data received is not allowed in this mode */
  1215. return HAL_ERROR;
  1216. }
  1217. CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_LDMARX);
  1218. if( hspi->Init.DataSize > SPI_DATASIZE_8BIT)
  1219. {
  1220. /* set fiforxthreshold according the reception data length: 16bit */
  1221. CLEAR_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
  1222. }
  1223. else
  1224. {
  1225. /* set fiforxthreshold according the reception data length: 8bit */
  1226. SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
  1227. }
  1228. /* Set the SPI RxDMA Half transfer complete callback */
  1229. hspi->hdmarx->XferHalfCpltCallback = SPI_DMAHalfReceiveCplt;
  1230. /* Set the SPI Rx DMA transfer complete callback */
  1231. hspi->hdmarx->XferCpltCallback = SPI_DMAReceiveCplt;
  1232. /* Set the DMA error callback */
  1233. hspi->hdmarx->XferErrorCallback = SPI_DMAError;
  1234. /* Enable Rx DMA Request */
  1235. hspi->Instance->CR2 |= SPI_CR2_RXDMAEN;
  1236. /* Enable the Rx DMA channel */
  1237. HAL_DMA_Start_IT(hspi->hdmarx, (uint32_t)&hspi->Instance->DR, (uint32_t)hspi->pRxBuffPtr, hspi->RxXferCount);
  1238. /* Process Unlocked */
  1239. __HAL_UNLOCK(hspi);
  1240. /* Check if the SPI is already enabled */
  1241. if((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
  1242. {
  1243. /* Enable SPI peripheral */
  1244. __HAL_SPI_ENABLE(hspi);
  1245. }
  1246. return HAL_OK;
  1247. }
  1248. /**
  1249. * @brief Transmit and Receive an amount of data in no-blocking mode with DMA
  1250. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1251. * the configuration information for SPI module.
  1252. * @param pTxData: pointer to transmission data buffer
  1253. * @param pRxData: pointer to reception data buffer
  1254. * @note When the CRC feature is enabled the pRxData Length must be Size + 1
  1255. * @param Size: amount of data to be sent
  1256. * @retval HAL status
  1257. */
  1258. HAL_StatusTypeDef HAL_SPI_TransmitReceive_DMA(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size)
  1259. {
  1260. assert_param(IS_SPI_DIRECTION_2LINES(hspi->Init.Direction));
  1261. if((hspi->State == HAL_SPI_STATE_READY) ||
  1262. ((hspi->Init.Mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES) && (hspi->State == HAL_SPI_STATE_BUSY_RX)))
  1263. {
  1264. if((pTxData == NULL ) || (pRxData == NULL ) || (Size == 0))
  1265. {
  1266. return HAL_ERROR;
  1267. }
  1268. /* Process locked */
  1269. __HAL_LOCK(hspi);
  1270. /* check if the transmit Receive function is not called by a receive master */
  1271. if(hspi->State != HAL_SPI_STATE_BUSY_RX)
  1272. {
  1273. hspi->State = HAL_SPI_STATE_BUSY_TX_RX;
  1274. }
  1275. hspi->ErrorCode = HAL_SPI_ERROR_NONE;
  1276. hspi->pTxBuffPtr = (uint8_t *)pTxData;
  1277. hspi->TxXferSize = Size;
  1278. hspi->TxXferCount = Size;
  1279. hspi->pRxBuffPtr = (uint8_t *)pRxData;
  1280. hspi->RxXferSize = Size;
  1281. hspi->RxXferCount = Size;
  1282. /* Reset CRC Calculation + increase the rxsize */
  1283. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  1284. {
  1285. SPI_RESET_CRC(hspi);
  1286. }
  1287. /* Reset the threshold bit */
  1288. CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_LDMATX);
  1289. CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_LDMARX);
  1290. /* the packing mode management is enabled by the DMA settings according the spi data size */
  1291. if(hspi->Init.DataSize > SPI_DATASIZE_8BIT)
  1292. {
  1293. /* set fiforxthreshold according the reception data length: 16bit */
  1294. CLEAR_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
  1295. }
  1296. else
  1297. {
  1298. /* set fiforxthreshold according the reception data length: 8bit */
  1299. SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
  1300. if(hspi->hdmatx->Init.MemDataAlignment == DMA_MDATAALIGN_HALFWORD)
  1301. {
  1302. if((hspi->TxXferSize & 0x1) == 0x0 )
  1303. {
  1304. CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_LDMATX);
  1305. hspi->TxXferCount = hspi->TxXferCount >> 1;
  1306. }
  1307. else
  1308. {
  1309. SET_BIT(hspi->Instance->CR2, SPI_CR2_LDMATX);
  1310. hspi->TxXferCount = (hspi->TxXferCount >> 1) + 1;
  1311. }
  1312. }
  1313. if(hspi->hdmarx->Init.MemDataAlignment == DMA_MDATAALIGN_HALFWORD)
  1314. {
  1315. /* set fiforxthreshold according the reception data length: 16bit */
  1316. CLEAR_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
  1317. /* Size must include the CRC length */
  1318. if((hspi->RxXferCount & 0x1) == 0x0 )
  1319. {
  1320. CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_LDMARX);
  1321. hspi->RxXferCount = hspi->RxXferCount >> 1;
  1322. }
  1323. else
  1324. {
  1325. SET_BIT(hspi->Instance->CR2, SPI_CR2_LDMARX);
  1326. hspi->RxXferCount = (hspi->RxXferCount >> 1) + 1;
  1327. }
  1328. }
  1329. }
  1330. /* Set the SPI Rx DMA transfer complete callback because the last generated transfer request is
  1331. the reception request (RXNE) */
  1332. if(hspi->State == HAL_SPI_STATE_BUSY_RX)
  1333. {
  1334. hspi->hdmarx->XferHalfCpltCallback = SPI_DMAHalfReceiveCplt;
  1335. hspi->hdmarx->XferCpltCallback = SPI_DMAReceiveCplt;
  1336. }
  1337. else
  1338. {
  1339. hspi->hdmarx->XferHalfCpltCallback = SPI_DMAHalfTransmitReceiveCplt;
  1340. hspi->hdmarx->XferCpltCallback = SPI_DMATransmitReceiveCplt;
  1341. }
  1342. /* Set the DMA error callback */
  1343. hspi->hdmarx->XferErrorCallback = SPI_DMAError;
  1344. /* Enable Rx DMA Request */
  1345. hspi->Instance->CR2 |= SPI_CR2_RXDMAEN;
  1346. /* Enable the Rx DMA channel */
  1347. HAL_DMA_Start_IT(hspi->hdmarx, (uint32_t)&hspi->Instance->DR, (uint32_t) hspi->pRxBuffPtr, hspi->RxXferCount);
  1348. /* Set the SPI Tx DMA transfer complete callback as NULL because the communication closing
  1349. is performed in DMA reception complete callback */
  1350. hspi->hdmatx->XferHalfCpltCallback = NULL;
  1351. hspi->hdmatx->XferCpltCallback = NULL;
  1352. if(hspi->State == HAL_SPI_STATE_BUSY_TX_RX)
  1353. {
  1354. /* Set the DMA error callback */
  1355. hspi->hdmatx->XferErrorCallback = SPI_DMAError;
  1356. }
  1357. else
  1358. {
  1359. hspi->hdmatx->XferErrorCallback = NULL;
  1360. }
  1361. /* Enable the Tx DMA channel */
  1362. HAL_DMA_Start_IT(hspi->hdmatx, (uint32_t)hspi->pTxBuffPtr, (uint32_t)&hspi->Instance->DR, hspi->TxXferCount);
  1363. /* Process Unlocked */
  1364. __HAL_UNLOCK(hspi);
  1365. /* Check if the SPI is already enabled */
  1366. if((hspi->Instance->CR1 &SPI_CR1_SPE) != SPI_CR1_SPE)
  1367. {
  1368. /* Enable SPI peripheral */
  1369. __HAL_SPI_ENABLE(hspi);
  1370. }
  1371. /* Enable Tx DMA Request */
  1372. hspi->Instance->CR2 |= SPI_CR2_TXDMAEN;
  1373. return HAL_OK;
  1374. }
  1375. else
  1376. {
  1377. return HAL_BUSY;
  1378. }
  1379. }
  1380. /**
  1381. * @brief Pauses the DMA Transfer.
  1382. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1383. * the configuration information for the specified SPI module.
  1384. * @retval HAL status
  1385. */
  1386. HAL_StatusTypeDef HAL_SPI_DMAPause(SPI_HandleTypeDef *hspi)
  1387. {
  1388. /* Process Locked */
  1389. __HAL_LOCK(hspi);
  1390. /* Disable the SPI DMA Tx & Rx requests */
  1391. CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN);
  1392. /* Process Unlocked */
  1393. __HAL_UNLOCK(hspi);
  1394. return HAL_OK;
  1395. }
  1396. /**
  1397. * @brief Resumes the DMA Transfer.
  1398. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1399. * the configuration information for the specified SPI module.
  1400. * @retval HAL status
  1401. */
  1402. HAL_StatusTypeDef HAL_SPI_DMAResume(SPI_HandleTypeDef *hspi)
  1403. {
  1404. /* Process Locked */
  1405. __HAL_LOCK(hspi);
  1406. /* Enable the SPI DMA Tx & Rx requests */
  1407. SET_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN);
  1408. /* Process Unlocked */
  1409. __HAL_UNLOCK(hspi);
  1410. return HAL_OK;
  1411. }
  1412. /**
  1413. * @brief Stops the DMA Transfer.
  1414. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1415. * the configuration information for the specified SPI module.
  1416. * @retval HAL status
  1417. */
  1418. HAL_StatusTypeDef HAL_SPI_DMAStop(SPI_HandleTypeDef *hspi)
  1419. {
  1420. /* The Lock is not implemented on this API to allow the user application
  1421. to call the HAL SPI API under callbacks HAL_SPI_TxCpltCallback() or HAL_SPI_RxCpltCallback() or HAL_SPI_TxRxCpltCallback():
  1422. when calling HAL_DMA_Abort() API the DMA TX/RX Transfer complete interrupt is generated
  1423. and the correspond call back is executed HAL_SPI_TxCpltCallback() or HAL_SPI_RxCpltCallback() or HAL_SPI_TxRxCpltCallback()
  1424. */
  1425. /* Abort the SPI DMA tx Stream */
  1426. if(hspi->hdmatx != NULL)
  1427. {
  1428. HAL_DMA_Abort(hspi->hdmatx);
  1429. }
  1430. /* Abort the SPI DMA rx Stream */
  1431. if(hspi->hdmarx != NULL)
  1432. {
  1433. HAL_DMA_Abort(hspi->hdmarx);
  1434. }
  1435. /* Disable the SPI DMA Tx & Rx requests */
  1436. CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN);
  1437. hspi->State = HAL_SPI_STATE_READY;
  1438. return HAL_OK;
  1439. }
  1440. /**
  1441. * @brief This function handles SPI interrupt request.
  1442. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1443. * the configuration information for the specified SPI module.
  1444. * @retval None
  1445. */
  1446. void HAL_SPI_IRQHandler(SPI_HandleTypeDef *hspi)
  1447. {
  1448. /* SPI in mode Receiver ----------------------------------------------------*/
  1449. if((__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_OVR) == RESET) &&
  1450. (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_RXNE) != RESET) && (__HAL_SPI_GET_IT_SOURCE(hspi, SPI_IT_RXNE) != RESET))
  1451. {
  1452. hspi->RxISR(hspi);
  1453. return;
  1454. }
  1455. /* SPI in mode Transmitter ---------------------------------------------------*/
  1456. if((__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_TXE) != RESET) && (__HAL_SPI_GET_IT_SOURCE(hspi, SPI_IT_TXE) != RESET))
  1457. {
  1458. hspi->TxISR(hspi);
  1459. return;
  1460. }
  1461. /* SPI in ERROR Treatment ---------------------------------------------------*/
  1462. if((hspi->Instance->SR & (SPI_FLAG_MODF | SPI_FLAG_OVR | SPI_FLAG_FRE)) != RESET)
  1463. {
  1464. /* SPI Overrun error interrupt occurred -------------------------------------*/
  1465. if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_OVR) != RESET)
  1466. {
  1467. if(hspi->State != HAL_SPI_STATE_BUSY_TX)
  1468. {
  1469. hspi->ErrorCode |= HAL_SPI_ERROR_OVR;
  1470. __HAL_SPI_CLEAR_OVRFLAG(hspi);
  1471. }
  1472. else
  1473. {
  1474. return;
  1475. }
  1476. }
  1477. /* SPI Mode Fault error interrupt occurred -------------------------------------*/
  1478. if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_MODF) != RESET)
  1479. {
  1480. hspi->ErrorCode |= HAL_SPI_ERROR_MODF;
  1481. __HAL_SPI_CLEAR_MODFFLAG(hspi);
  1482. }
  1483. /* SPI Frame error interrupt occurred ----------------------------------------*/
  1484. if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_FRE) != RESET)
  1485. {
  1486. hspi->ErrorCode |= HAL_SPI_ERROR_FRE;
  1487. __HAL_SPI_CLEAR_FREFLAG(hspi);
  1488. }
  1489. __HAL_SPI_DISABLE_IT(hspi, SPI_IT_RXNE | SPI_IT_TXE | SPI_IT_ERR);
  1490. hspi->State = HAL_SPI_STATE_READY;
  1491. HAL_SPI_ErrorCallback(hspi);
  1492. return;
  1493. }
  1494. }
  1495. /**
  1496. * @brief Tx Transfer completed callback
  1497. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1498. * the configuration information for SPI module.
  1499. * @retval None
  1500. */
  1501. __weak void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi)
  1502. {
  1503. /* NOTE : This function should not be modified, when the callback is needed,
  1504. the HAL_SPI_TxCpltCallback should be implemented in the user file
  1505. */
  1506. }
  1507. /**
  1508. * @brief Rx Transfer completed callbacks
  1509. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1510. * the configuration information for SPI module.
  1511. * @retval None
  1512. */
  1513. __weak void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi)
  1514. {
  1515. /* NOTE : This function should not be modified, when the callback is needed,
  1516. the HAL_SPI_RxCpltCallback should be implemented in the user file
  1517. */
  1518. }
  1519. /**
  1520. * @brief Tx and Rx Transfer completed callback
  1521. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1522. * the configuration information for SPI module.
  1523. * @retval None
  1524. */
  1525. __weak void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi)
  1526. {
  1527. /* NOTE : This function should not be modified, when the callback is needed,
  1528. the HAL_SPI_TxRxCpltCallback should be implemented in the user file
  1529. */
  1530. }
  1531. /**
  1532. * @brief Tx Half Transfer completed callback
  1533. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1534. * the configuration information for SPI module.
  1535. * @retval None
  1536. */
  1537. __weak void HAL_SPI_TxHalfCpltCallback(SPI_HandleTypeDef *hspi)
  1538. {
  1539. /* NOTE : This function should not be modified, when the callback is needed,
  1540. the HAL_SPI_TxHalfCpltCallback should be implemented in the user file
  1541. */
  1542. }
  1543. /**
  1544. * @brief Rx Half Transfer completed callback
  1545. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1546. * the configuration information for SPI module.
  1547. * @retval None
  1548. */
  1549. __weak void HAL_SPI_RxHalfCpltCallback(SPI_HandleTypeDef *hspi)
  1550. {
  1551. /* NOTE : This function should not be modified, when the callback is needed,
  1552. the HAL_SPI_RxHalfCpltCallback() should be implemented in the user file
  1553. */
  1554. }
  1555. /**
  1556. * @brief Tx and Rx Half Transfer callback
  1557. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1558. * the configuration information for SPI module.
  1559. * @retval None
  1560. */
  1561. __weak void HAL_SPI_TxRxHalfCpltCallback(SPI_HandleTypeDef *hspi)
  1562. {
  1563. /* NOTE : This function should not be modified, when the callback is needed,
  1564. the HAL_SPI_TxRxHalfCpltCallback() should be implemented in the user file
  1565. */
  1566. }
  1567. /**
  1568. * @brief SPI error callback
  1569. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1570. * the configuration information for SPI module.
  1571. * @retval None
  1572. */
  1573. __weak void HAL_SPI_ErrorCallback(SPI_HandleTypeDef *hspi)
  1574. {
  1575. /* NOTE : This function should not be modified, when the callback is needed,
  1576. the HAL_SPI_ErrorCallback should be implemented in the user file
  1577. */
  1578. /* NOTE : The ErrorCode parameter in the hspi handle is updated by the SPI processes
  1579. and user can use HAL_SPI_GetError() API to check the latest error occurred
  1580. */
  1581. }
  1582. /**
  1583. * @}
  1584. */
  1585. /**
  1586. * @}
  1587. */
  1588. /** @defgroup SPI_Exported_Functions_Group3 Peripheral State and Errors functions
  1589. * @brief SPI control functions
  1590. *
  1591. @verbatim
  1592. ===============================================================================
  1593. ##### Peripheral State and Errors functions #####
  1594. ===============================================================================
  1595. [..]
  1596. This subsection provides a set of functions allowing to control the SPI.
  1597. (+) HAL_SPI_GetState() API can be helpful to check in run-time the state of the SPI peripheral
  1598. (+) HAL_SPI_GetError() check in run-time Errors occurring during communication
  1599. @endverbatim
  1600. * @{
  1601. */
  1602. /**
  1603. * @brief Return the SPI state
  1604. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1605. * the configuration information for SPI module.
  1606. * @retval SPI state
  1607. */
  1608. HAL_SPI_StateTypeDef HAL_SPI_GetState(SPI_HandleTypeDef *hspi)
  1609. {
  1610. return hspi->State;
  1611. }
  1612. /**
  1613. * @brief Return the SPI error code
  1614. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1615. * the configuration information for SPI module.
  1616. * @retval SPI error code in bitmap format
  1617. */
  1618. uint32_t HAL_SPI_GetError(SPI_HandleTypeDef *hspi)
  1619. {
  1620. return hspi->ErrorCode;
  1621. }
  1622. /**
  1623. * @}
  1624. */
  1625. /**
  1626. * @}
  1627. */
  1628. /** @defgroup SPI_Private_Functions SPI Private Functions
  1629. * @{
  1630. */
  1631. /**
  1632. * @brief DMA SPI transmit process complete callback
  1633. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1634. * the configuration information for the specified DMA module.
  1635. * @retval None
  1636. */
  1637. static void SPI_DMATransmitCplt(DMA_HandleTypeDef *hdma)
  1638. {
  1639. SPI_HandleTypeDef* hspi = ( SPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1640. /* DMA Normal Mode */
  1641. if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0)
  1642. {
  1643. /* Disable Tx DMA Request */
  1644. CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN);
  1645. /* Clear OVERUN flag in 2 Lines communication mode because received data is not read */
  1646. if(hspi->Init.Direction == SPI_DIRECTION_2LINES)
  1647. {
  1648. __HAL_SPI_CLEAR_OVRFLAG(hspi);
  1649. }
  1650. hspi->TxXferCount = 0;
  1651. hspi->State = HAL_SPI_STATE_READY;
  1652. if(hspi->ErrorCode != HAL_SPI_ERROR_NONE)
  1653. {
  1654. HAL_SPI_ErrorCallback(hspi);
  1655. return;
  1656. }
  1657. }
  1658. HAL_SPI_TxCpltCallback(hspi);
  1659. }
  1660. /**
  1661. * @brief DMA SPI receive process complete callback
  1662. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1663. * the configuration information for the specified DMA module.
  1664. * @retval None
  1665. */
  1666. static void SPI_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
  1667. {
  1668. __IO uint16_t tmpreg;
  1669. SPI_HandleTypeDef* hspi = ( SPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1670. /* DMA Normal mode */
  1671. if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0)
  1672. {
  1673. /* CRC handling */
  1674. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  1675. {
  1676. /* Wait until TXE flag */
  1677. if(SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SPI_FLAG_RXNE, SPI_DEFAULT_TIMEOUT) != HAL_OK)
  1678. {
  1679. /* Error on the CRC reception */
  1680. hspi->ErrorCode|= HAL_SPI_ERROR_CRC;
  1681. }
  1682. if(hspi->Init.DataSize > SPI_DATASIZE_8BIT)
  1683. {
  1684. tmpreg = hspi->Instance->DR;
  1685. UNUSED(tmpreg); /* To avoid GCC warning */
  1686. }
  1687. else
  1688. {
  1689. tmpreg = *(__IO uint8_t *)&hspi->Instance->DR;
  1690. UNUSED(tmpreg); /* To avoid GCC warning */
  1691. if(hspi->Init.CRCLength == SPI_CRC_LENGTH_16BIT)
  1692. {
  1693. if(SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SPI_FLAG_RXNE, SPI_DEFAULT_TIMEOUT) != HAL_OK)
  1694. {
  1695. /* Error on the CRC reception */
  1696. hspi->ErrorCode|= HAL_SPI_ERROR_CRC;
  1697. }
  1698. tmpreg = *(__IO uint8_t *)&hspi->Instance->DR;
  1699. UNUSED(tmpreg); /* To avoid GCC warning */
  1700. }
  1701. }
  1702. }
  1703. /* Disable Rx DMA Request */
  1704. hspi->Instance->CR2 &= (uint32_t)(~SPI_CR2_RXDMAEN);
  1705. /* Disable Tx DMA Request (done by default to handle the case master rx direction 2 lines) */
  1706. hspi->Instance->CR2 &= (uint32_t)(~SPI_CR2_TXDMAEN);
  1707. /* Check the end of the transaction */
  1708. SPI_EndRxTransaction(hspi,SPI_DEFAULT_TIMEOUT);
  1709. hspi->RxXferCount = 0;
  1710. hspi->State = HAL_SPI_STATE_READY;
  1711. /* Check if CRC error occurred */
  1712. if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET)
  1713. {
  1714. hspi->ErrorCode|= HAL_SPI_ERROR_CRC;
  1715. __HAL_SPI_CLEAR_CRCERRFLAG(hspi);
  1716. HAL_SPI_RxCpltCallback(hspi);
  1717. }
  1718. else
  1719. {
  1720. if(hspi->ErrorCode == HAL_SPI_ERROR_NONE)
  1721. {
  1722. HAL_SPI_RxCpltCallback(hspi);
  1723. }
  1724. else
  1725. {
  1726. HAL_SPI_ErrorCallback(hspi);
  1727. }
  1728. }
  1729. }
  1730. else
  1731. {
  1732. HAL_SPI_RxCpltCallback(hspi);
  1733. }
  1734. }
  1735. /**
  1736. * @brief DMA SPI transmit receive process complete callback
  1737. * @param hdma : pointer to a DMA_HandleTypeDef structure that contains
  1738. * the configuration information for the specified DMA module.
  1739. * @retval None
  1740. */
  1741. static void SPI_DMATransmitReceiveCplt(DMA_HandleTypeDef *hdma)
  1742. {
  1743. __IO int16_t tmpreg;
  1744. SPI_HandleTypeDef* hspi = ( SPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1745. /* CRC handling */
  1746. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  1747. {
  1748. if((hspi->Init.DataSize == SPI_DATASIZE_8BIT) && (hspi->Init.CRCLength == SPI_CRC_LENGTH_8BIT))
  1749. {
  1750. if(SPI_WaitFifoStateUntilTimeout(hspi, SPI_FLAG_FRLVL, SPI_FRLVL_QUARTER_FULL, SPI_DEFAULT_TIMEOUT) != HAL_OK)
  1751. {
  1752. /* Error on the CRC reception */
  1753. hspi->ErrorCode|= HAL_SPI_ERROR_CRC;
  1754. }
  1755. tmpreg = *(__IO uint8_t *)&hspi->Instance->DR;
  1756. UNUSED(tmpreg); /* To avoid GCC warning */
  1757. }
  1758. else
  1759. {
  1760. if(SPI_WaitFifoStateUntilTimeout(hspi, SPI_FLAG_FRLVL, SPI_FRLVL_HALF_FULL, SPI_DEFAULT_TIMEOUT) != HAL_OK)
  1761. {
  1762. /* Error on the CRC reception */
  1763. hspi->ErrorCode|= HAL_SPI_ERROR_CRC;
  1764. }
  1765. tmpreg = hspi->Instance->DR;
  1766. UNUSED(tmpreg); /* To avoid GCC warning */
  1767. }
  1768. }
  1769. /* Check the end of the transaction */
  1770. SPI_EndRxTxTransaction(hspi,SPI_DEFAULT_TIMEOUT);
  1771. /* Disable Tx DMA Request */
  1772. CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN);
  1773. /* Disable Rx DMA Request */
  1774. CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_RXDMAEN);
  1775. hspi->TxXferCount = 0;
  1776. hspi->RxXferCount = 0;
  1777. hspi->State = HAL_SPI_STATE_READY;
  1778. /* Check if CRC error occurred */
  1779. if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET)
  1780. {
  1781. hspi->ErrorCode = HAL_SPI_ERROR_CRC;
  1782. __HAL_SPI_CLEAR_CRCERRFLAG(hspi);
  1783. HAL_SPI_ErrorCallback(hspi);
  1784. }
  1785. else
  1786. {
  1787. if(hspi->ErrorCode == HAL_SPI_ERROR_NONE)
  1788. {
  1789. HAL_SPI_TxRxCpltCallback(hspi);
  1790. }
  1791. else
  1792. {
  1793. HAL_SPI_ErrorCallback(hspi);
  1794. }
  1795. }
  1796. }
  1797. /**
  1798. * @brief DMA SPI half transmit process complete callback
  1799. * @param hdma : pointer to a DMA_HandleTypeDef structure that contains
  1800. * the configuration information for the specified DMA module.
  1801. * @retval None
  1802. */
  1803. static void SPI_DMAHalfTransmitCplt(DMA_HandleTypeDef *hdma)
  1804. {
  1805. SPI_HandleTypeDef* hspi = ( SPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1806. HAL_SPI_TxHalfCpltCallback(hspi);
  1807. }
  1808. /**
  1809. * @brief DMA SPI half receive process complete callback
  1810. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1811. * the configuration information for the specified DMA module.
  1812. * @retval None
  1813. */
  1814. static void SPI_DMAHalfReceiveCplt(DMA_HandleTypeDef *hdma)
  1815. {
  1816. SPI_HandleTypeDef* hspi = ( SPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1817. HAL_SPI_RxHalfCpltCallback(hspi);
  1818. }
  1819. /**
  1820. * @brief DMA SPI Half transmit receive process complete callback
  1821. * @param hdma : pointer to a DMA_HandleTypeDef structure that contains
  1822. * the configuration information for the specified DMA module.
  1823. * @retval None
  1824. */
  1825. static void SPI_DMAHalfTransmitReceiveCplt(DMA_HandleTypeDef *hdma)
  1826. {
  1827. SPI_HandleTypeDef* hspi = ( SPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1828. HAL_SPI_TxRxHalfCpltCallback(hspi);
  1829. }
  1830. /**
  1831. * @brief DMA SPI communication error callback
  1832. * @param hdma : pointer to a DMA_HandleTypeDef structure that contains
  1833. * the configuration information for the specified DMA module.
  1834. * @retval None
  1835. */
  1836. static void SPI_DMAError(DMA_HandleTypeDef *hdma)
  1837. {
  1838. SPI_HandleTypeDef* hspi = ( SPI_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1839. /* Stop the disable DMA transfer on SPI side */
  1840. CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN);
  1841. hspi->ErrorCode|= HAL_SPI_ERROR_DMA;
  1842. hspi->State = HAL_SPI_STATE_READY;
  1843. HAL_SPI_ErrorCallback(hspi);
  1844. }
  1845. /**
  1846. * @brief Rx Handler for Transmit and Receive in Interrupt mode
  1847. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1848. * the configuration information for SPI module.
  1849. * @retval None
  1850. */
  1851. static void SPI_2linesRxISR_8BIT(struct __SPI_HandleTypeDef *hspi)
  1852. {
  1853. /* Receive data in packing mode */
  1854. if(hspi->RxXferCount > 1)
  1855. {
  1856. *((uint16_t*)hspi->pRxBuffPtr) = hspi->Instance->DR;
  1857. hspi->pRxBuffPtr += sizeof(uint16_t);
  1858. hspi->RxXferCount -= 2;
  1859. if(hspi->RxXferCount == 1)
  1860. {
  1861. /* set fiforxthreshold according the reception data length: 8bit */
  1862. SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
  1863. }
  1864. }
  1865. /* Receive data in 8 Bit mode */
  1866. else
  1867. {
  1868. *hspi->pRxBuffPtr++ = *((__IO uint8_t *)&hspi->Instance->DR);
  1869. hspi->RxXferCount--;
  1870. }
  1871. /* check end of the reception */
  1872. if(hspi->RxXferCount == 0)
  1873. {
  1874. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  1875. {
  1876. SET_BIT(hspi->Instance->CR2, SPI_RXFIFO_THRESHOLD);
  1877. hspi->RxISR = SPI_2linesRxISR_8BITCRC;
  1878. return;
  1879. }
  1880. /* Disable RXNE interrupt */
  1881. __HAL_SPI_DISABLE_IT(hspi, SPI_IT_RXNE);
  1882. if(hspi->TxXferCount == 0)
  1883. {
  1884. SPI_CloseRxTx_ISR(hspi);
  1885. }
  1886. }
  1887. }
  1888. /**
  1889. * @brief Rx Handler for Transmit and Receive in Interrupt mode
  1890. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1891. * the configuration information for SPI module.
  1892. * @retval None
  1893. */
  1894. static void SPI_2linesRxISR_8BITCRC(struct __SPI_HandleTypeDef *hspi)
  1895. {
  1896. __IO uint8_t tmpreg;
  1897. tmpreg = *((__IO uint8_t *)&hspi->Instance->DR);
  1898. UNUSED(tmpreg); /* To avoid GCC warning */
  1899. hspi->CRCSize--;
  1900. /* check end of the reception */
  1901. if(hspi->CRCSize == 0)
  1902. {
  1903. /* Disable RXNE interrupt */
  1904. __HAL_SPI_DISABLE_IT(hspi, SPI_IT_RXNE);
  1905. if(hspi->TxXferCount == 0)
  1906. {
  1907. SPI_CloseRxTx_ISR(hspi);
  1908. }
  1909. }
  1910. }
  1911. /**
  1912. * @brief Tx Handler for Transmit and Receive in Interrupt mode
  1913. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1914. * the configuration information for SPI module.
  1915. * @retval None
  1916. */
  1917. static void SPI_2linesTxISR_8BIT(struct __SPI_HandleTypeDef *hspi)
  1918. {
  1919. /* Transmit data in packing Bit mode */
  1920. if(hspi->TxXferCount >= 2)
  1921. {
  1922. hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr);
  1923. hspi->pTxBuffPtr += sizeof(uint16_t);
  1924. hspi->TxXferCount -= 2;
  1925. }
  1926. /* Transmit data in 8 Bit mode */
  1927. else
  1928. {
  1929. *(__IO uint8_t *)&hspi->Instance->DR = (*hspi->pTxBuffPtr++);
  1930. hspi->TxXferCount--;
  1931. }
  1932. /* check the end of the transmission */
  1933. if(hspi->TxXferCount == 0)
  1934. {
  1935. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  1936. {
  1937. hspi->Instance->CR1 |= SPI_CR1_CRCNEXT;
  1938. }
  1939. /* Disable TXE interrupt */
  1940. __HAL_SPI_DISABLE_IT(hspi, SPI_IT_TXE);
  1941. if(hspi->RxXferCount == 0)
  1942. {
  1943. SPI_CloseRxTx_ISR(hspi);
  1944. }
  1945. }
  1946. }
  1947. /**
  1948. * @brief Rx 16Bit Handler for Transmit and Receive in Interrupt mode
  1949. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1950. * the configuration information for SPI module.
  1951. * @retval None
  1952. */
  1953. static void SPI_2linesRxISR_16BIT(struct __SPI_HandleTypeDef *hspi)
  1954. {
  1955. /* Receive data in 16 Bit mode */
  1956. *((uint16_t*)hspi->pRxBuffPtr) = hspi->Instance->DR;
  1957. hspi->pRxBuffPtr += sizeof(uint16_t);
  1958. hspi->RxXferCount--;
  1959. if(hspi->RxXferCount == 0)
  1960. {
  1961. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  1962. {
  1963. hspi->RxISR = SPI_2linesRxISR_16BITCRC;
  1964. return;
  1965. }
  1966. /* Disable RXNE interrupt */
  1967. __HAL_SPI_DISABLE_IT(hspi, SPI_IT_RXNE);
  1968. if(hspi->TxXferCount == 0)
  1969. {
  1970. SPI_CloseRxTx_ISR(hspi);
  1971. }
  1972. }
  1973. }
  1974. /**
  1975. * @brief Manage the CRC 16bit receive for Transmit and Receive in Interrupt mode
  1976. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1977. * the configuration information for SPI module.
  1978. * @retval None
  1979. */
  1980. static void SPI_2linesRxISR_16BITCRC(struct __SPI_HandleTypeDef *hspi)
  1981. {
  1982. /* Receive data in 16 Bit mode */
  1983. __IO uint16_t tmpreg = hspi->Instance->DR;
  1984. UNUSED(tmpreg); /* To avoid GCC warning */
  1985. /* Disable RXNE interrupt */
  1986. __HAL_SPI_DISABLE_IT(hspi, SPI_IT_RXNE);
  1987. SPI_CloseRxTx_ISR(hspi);
  1988. }
  1989. /**
  1990. * @brief Tx Handler for Transmit and Receive in Interrupt mode
  1991. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  1992. * the configuration information for SPI module.
  1993. * @retval None
  1994. */
  1995. static void SPI_2linesTxISR_16BIT(struct __SPI_HandleTypeDef *hspi)
  1996. {
  1997. /* Transmit data in 16 Bit mode */
  1998. hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr);
  1999. hspi->pTxBuffPtr += sizeof(uint16_t);
  2000. hspi->TxXferCount--;
  2001. /* Enable CRC Transmission */
  2002. if(hspi->TxXferCount == 0)
  2003. {
  2004. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  2005. {
  2006. hspi->Instance->CR1 |= SPI_CR1_CRCNEXT;
  2007. }
  2008. /* Disable TXE interrupt */
  2009. __HAL_SPI_DISABLE_IT(hspi, SPI_IT_TXE);
  2010. if(hspi->RxXferCount == 0)
  2011. {
  2012. SPI_CloseRxTx_ISR(hspi);
  2013. }
  2014. }
  2015. }
  2016. /**
  2017. * @brief Manage the CRC receive in Interrupt context
  2018. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  2019. * the configuration information for SPI module.
  2020. * @retval None
  2021. */
  2022. static void SPI_RxISR_8BITCRC(struct __SPI_HandleTypeDef *hspi)
  2023. {
  2024. __IO uint8_t tmpreg;
  2025. tmpreg = *((__IO uint8_t*)&hspi->Instance->DR);
  2026. UNUSED(tmpreg); /* To avoid GCC warning */
  2027. hspi->CRCSize--;
  2028. if(hspi->CRCSize == 0)
  2029. {
  2030. SPI_CloseRx_ISR(hspi);
  2031. }
  2032. }
  2033. /**
  2034. * @brief Manage the receive in Interrupt context
  2035. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  2036. * the configuration information for SPI module.
  2037. * @retval None
  2038. */
  2039. static void SPI_RxISR_8BIT(struct __SPI_HandleTypeDef *hspi)
  2040. {
  2041. *hspi->pRxBuffPtr++ = (*(__IO uint8_t *)&hspi->Instance->DR);
  2042. hspi->RxXferCount--;
  2043. /* Enable CRC Transmission */
  2044. if((hspi->RxXferCount == 1) && (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE))
  2045. {
  2046. hspi->Instance->CR1 |= SPI_CR1_CRCNEXT;
  2047. }
  2048. if(hspi->RxXferCount == 0)
  2049. {
  2050. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  2051. {
  2052. hspi->RxISR = SPI_RxISR_8BITCRC;
  2053. return;
  2054. }
  2055. SPI_CloseRx_ISR(hspi);
  2056. }
  2057. }
  2058. /**
  2059. * @brief Manage the CRC 16bit receive in Interrupt context
  2060. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  2061. * the configuration information for SPI module.
  2062. * @retval None
  2063. */
  2064. static void SPI_RxISR_16BITCRC(struct __SPI_HandleTypeDef *hspi)
  2065. {
  2066. __IO uint16_t tmpreg;
  2067. tmpreg = hspi->Instance->DR;
  2068. UNUSED(tmpreg); /* To avoid GCC warning */
  2069. /* Disable RXNE and ERR interrupt */
  2070. __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_RXNE | SPI_IT_ERR));
  2071. SPI_CloseRx_ISR(hspi);
  2072. }
  2073. /**
  2074. * @brief Manage the 16Bit receive in Interrupt context
  2075. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  2076. * the configuration information for SPI module.
  2077. * @retval None
  2078. */
  2079. static void SPI_RxISR_16BIT(struct __SPI_HandleTypeDef *hspi)
  2080. {
  2081. *((uint16_t *)hspi->pRxBuffPtr) = hspi->Instance->DR;
  2082. hspi->pRxBuffPtr += sizeof(uint16_t);
  2083. hspi->RxXferCount--;
  2084. /* Enable CRC Transmission */
  2085. if((hspi->RxXferCount == 1) && (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE))
  2086. {
  2087. hspi->Instance->CR1 |= SPI_CR1_CRCNEXT;
  2088. }
  2089. if(hspi->RxXferCount == 0)
  2090. {
  2091. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  2092. {
  2093. hspi->RxISR = SPI_RxISR_16BITCRC;
  2094. return;
  2095. }
  2096. SPI_CloseRx_ISR(hspi);
  2097. }
  2098. }
  2099. /**
  2100. * @brief Handle the data 8Bit transmit in Interrupt mode
  2101. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  2102. * the configuration information for SPI module.
  2103. * @retval None
  2104. */
  2105. static void SPI_TxISR_8BIT(struct __SPI_HandleTypeDef *hspi)
  2106. {
  2107. *(__IO uint8_t *)&hspi->Instance->DR = (*hspi->pTxBuffPtr++);
  2108. hspi->TxXferCount--;
  2109. if(hspi->TxXferCount == 0)
  2110. {
  2111. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  2112. {
  2113. /* Enable CRC Transmission */
  2114. hspi->Instance->CR1 |= SPI_CR1_CRCNEXT;
  2115. }
  2116. SPI_CloseTx_ISR(hspi);
  2117. }
  2118. }
  2119. /**
  2120. * @brief Handle the data 16Bit transmit in Interrupt mode
  2121. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  2122. * the configuration information for SPI module.
  2123. * @retval None
  2124. */
  2125. static void SPI_TxISR_16BIT(struct __SPI_HandleTypeDef *hspi)
  2126. {
  2127. /* Transmit data in 16 Bit mode */
  2128. hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr);
  2129. hspi->pTxBuffPtr += sizeof(uint16_t);
  2130. hspi->TxXferCount--;
  2131. if(hspi->TxXferCount == 0)
  2132. {
  2133. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  2134. {
  2135. /* Enable CRC Transmission */
  2136. hspi->Instance->CR1 |= SPI_CR1_CRCNEXT;
  2137. }
  2138. SPI_CloseTx_ISR(hspi);
  2139. }
  2140. }
  2141. /**
  2142. * @brief This function handles SPI Communication Timeout.
  2143. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  2144. * the configuration information for SPI module.
  2145. * @param Flag : SPI flag to check
  2146. * @param State : flag state to check
  2147. * @param Timeout : Timeout duration
  2148. * @retval HAL status
  2149. */
  2150. static HAL_StatusTypeDef SPI_WaitFlagStateUntilTimeout(SPI_HandleTypeDef *hspi, uint32_t Flag, uint32_t State, uint32_t Timeout)
  2151. {
  2152. uint32_t tickstart = HAL_GetTick();
  2153. while((hspi->Instance->SR & Flag) != State)
  2154. {
  2155. if(Timeout != HAL_MAX_DELAY)
  2156. {
  2157. if((Timeout == 0) || ((HAL_GetTick()-tickstart) >= Timeout))
  2158. {
  2159. /* Disable the SPI and reset the CRC: the CRC value should be cleared
  2160. on both master and slave sides in order to resynchronize the master
  2161. and slave for their respective CRC calculation */
  2162. /* Disable TXE, RXNE and ERR interrupts for the interrupt process */
  2163. __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_TXE | SPI_IT_RXNE | SPI_IT_ERR));
  2164. if((hspi->Init.Mode == SPI_MODE_MASTER)&&((hspi->Init.Direction == SPI_DIRECTION_1LINE)||(hspi->Init.Direction == SPI_DIRECTION_2LINES_RXONLY)))
  2165. {
  2166. /* Disable SPI peripheral */
  2167. __HAL_SPI_DISABLE(hspi);
  2168. }
  2169. /* Reset CRC Calculation */
  2170. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  2171. {
  2172. SPI_RESET_CRC(hspi);
  2173. }
  2174. hspi->State= HAL_SPI_STATE_READY;
  2175. /* Process Unlocked */
  2176. __HAL_UNLOCK(hspi);
  2177. return HAL_TIMEOUT;
  2178. }
  2179. }
  2180. }
  2181. return HAL_OK;
  2182. }
  2183. /**
  2184. * @brief This function handles SPI Communication Timeout.
  2185. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  2186. * the configuration information for SPI module.
  2187. * @param Fifo : Fifo to check
  2188. * @param State : Fifo state to check
  2189. * @param Timeout : Timeout duration
  2190. * @retval HAL status
  2191. */
  2192. static HAL_StatusTypeDef SPI_WaitFifoStateUntilTimeout(SPI_HandleTypeDef *hspi, uint32_t Fifo, uint32_t State, uint32_t Timeout)
  2193. {
  2194. __IO uint8_t tmpreg;
  2195. uint32_t tickstart = HAL_GetTick();
  2196. while((hspi->Instance->SR & Fifo) != State)
  2197. {
  2198. if((Fifo == SPI_SR_FRLVL) && (State == SPI_FRLVL_EMPTY))
  2199. {
  2200. tmpreg = *((__IO uint8_t*)&hspi->Instance->DR);
  2201. UNUSED(tmpreg); /* To avoid GCC warning */
  2202. }
  2203. if(Timeout != HAL_MAX_DELAY)
  2204. {
  2205. if((Timeout == 0) || ((HAL_GetTick()-tickstart) >= Timeout))
  2206. {
  2207. /* Disable the SPI and reset the CRC: the CRC value should be cleared
  2208. on both master and slave sides in order to resynchronize the master
  2209. and slave for their respective CRC calculation */
  2210. /* Disable TXE, RXNE and ERR interrupts for the interrupt process */
  2211. __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_TXE | SPI_IT_RXNE | SPI_IT_ERR));
  2212. if((hspi->Init.Mode == SPI_MODE_MASTER)&&((hspi->Init.Direction == SPI_DIRECTION_1LINE)||(hspi->Init.Direction == SPI_DIRECTION_2LINES_RXONLY)))
  2213. {
  2214. /* Disable SPI peripheral */
  2215. __HAL_SPI_DISABLE(hspi);
  2216. }
  2217. /* Reset CRC Calculation */
  2218. if(hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
  2219. {
  2220. SPI_RESET_CRC(hspi);
  2221. }
  2222. hspi->State = HAL_SPI_STATE_READY;
  2223. /* Process Unlocked */
  2224. __HAL_UNLOCK(hspi);
  2225. return HAL_TIMEOUT;
  2226. }
  2227. }
  2228. }
  2229. return HAL_OK;
  2230. }
  2231. /**
  2232. * @brief This function handles the check of the RX transaction complete.
  2233. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  2234. * the configuration information for SPI module.
  2235. * @param Timeout : Timeout duration
  2236. * @retval None
  2237. */
  2238. static HAL_StatusTypeDef SPI_EndRxTransaction(SPI_HandleTypeDef *hspi, uint32_t Timeout)
  2239. {
  2240. if((hspi->Init.Mode == SPI_MODE_MASTER)&&((hspi->Init.Direction == SPI_DIRECTION_1LINE)||(hspi->Init.Direction == SPI_DIRECTION_2LINES_RXONLY)))
  2241. {
  2242. /* Disable SPI peripheral */
  2243. __HAL_SPI_DISABLE(hspi);
  2244. }
  2245. if(SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_BSY, RESET, Timeout) != HAL_OK)
  2246. {
  2247. hspi->ErrorCode |= HAL_SPI_ERROR_FLAG;
  2248. return HAL_TIMEOUT;
  2249. }
  2250. if(SPI_WaitFifoStateUntilTimeout(hspi, SPI_FLAG_FRLVL, SPI_FRLVL_EMPTY, Timeout) != HAL_OK)
  2251. {
  2252. hspi->ErrorCode |= HAL_SPI_ERROR_FLAG;
  2253. return HAL_TIMEOUT;
  2254. }
  2255. return HAL_OK;
  2256. }
  2257. /**
  2258. * @brief This function handles the check of the RXTX or TX transaction complete.
  2259. * @param hspi: SPI handle
  2260. * @param Timeout : Timeout duration
  2261. */
  2262. static HAL_StatusTypeDef SPI_EndRxTxTransaction(SPI_HandleTypeDef *hspi, uint32_t Timeout)
  2263. {
  2264. /* Procedure to check the transaction complete */
  2265. if(SPI_WaitFifoStateUntilTimeout(hspi, SPI_FLAG_FTLVL, SPI_FTLVL_EMPTY, Timeout) != HAL_OK)
  2266. {
  2267. hspi->ErrorCode |= HAL_SPI_ERROR_FLAG;
  2268. return HAL_TIMEOUT;
  2269. }
  2270. if(SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_BSY, RESET, Timeout) != HAL_OK)
  2271. {
  2272. hspi->ErrorCode |= HAL_SPI_ERROR_FLAG;
  2273. return HAL_TIMEOUT;
  2274. }
  2275. if(SPI_WaitFifoStateUntilTimeout(hspi, SPI_FLAG_FRLVL, SPI_FRLVL_EMPTY, Timeout) != HAL_OK)
  2276. {
  2277. hspi->ErrorCode |= HAL_SPI_ERROR_FLAG;
  2278. return HAL_TIMEOUT;
  2279. }
  2280. return HAL_OK;
  2281. }
  2282. /**
  2283. * @brief This function handles the close of the RXTX transaction.
  2284. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  2285. * the configuration information for SPI module.
  2286. * @retval None
  2287. */
  2288. static void SPI_CloseRxTx_ISR(SPI_HandleTypeDef *hspi)
  2289. {
  2290. /* Disable ERR interrupt */
  2291. __HAL_SPI_DISABLE_IT(hspi, SPI_IT_ERR);
  2292. /* Check if CRC error occurred */
  2293. if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET)
  2294. {
  2295. hspi->State = HAL_SPI_STATE_READY;
  2296. hspi->ErrorCode|= HAL_SPI_ERROR_CRC;
  2297. __HAL_SPI_CLEAR_CRCERRFLAG(hspi);
  2298. HAL_SPI_ErrorCallback(hspi);
  2299. }
  2300. else
  2301. {
  2302. if(hspi->ErrorCode == HAL_SPI_ERROR_NONE)
  2303. {
  2304. if(hspi->State == HAL_SPI_STATE_BUSY_RX)
  2305. {
  2306. hspi->State = HAL_SPI_STATE_READY;
  2307. HAL_SPI_RxCpltCallback(hspi);
  2308. }
  2309. else
  2310. {
  2311. hspi->State = HAL_SPI_STATE_READY;
  2312. HAL_SPI_TxRxCpltCallback(hspi);
  2313. }
  2314. }
  2315. else
  2316. {
  2317. hspi->State = HAL_SPI_STATE_READY;
  2318. HAL_SPI_ErrorCallback(hspi);
  2319. }
  2320. }
  2321. }
  2322. /**
  2323. * @brief This function handles the close of the RX transaction.
  2324. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  2325. * the configuration information for SPI module.
  2326. * @retval None
  2327. */
  2328. static void SPI_CloseRx_ISR(SPI_HandleTypeDef *hspi)
  2329. {
  2330. /* Disable RXNE and ERR interrupt */
  2331. __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_RXNE | SPI_IT_ERR));
  2332. /* Check the end of the transaction */
  2333. SPI_EndRxTransaction(hspi,SPI_DEFAULT_TIMEOUT);
  2334. hspi->State = HAL_SPI_STATE_READY;
  2335. /* Check if CRC error occurred */
  2336. if(__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET)
  2337. {
  2338. hspi->ErrorCode|= HAL_SPI_ERROR_CRC;
  2339. __HAL_SPI_CLEAR_CRCERRFLAG(hspi);
  2340. HAL_SPI_ErrorCallback(hspi);
  2341. }
  2342. else
  2343. {
  2344. if(hspi->ErrorCode == HAL_SPI_ERROR_NONE)
  2345. {
  2346. HAL_SPI_RxCpltCallback(hspi);
  2347. }
  2348. else
  2349. {
  2350. HAL_SPI_ErrorCallback(hspi);
  2351. }
  2352. }
  2353. }
  2354. /**
  2355. * @brief This function handles the close of the TX transaction.
  2356. * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
  2357. * the configuration information for SPI module.
  2358. * @retval None
  2359. */
  2360. static void SPI_CloseTx_ISR(SPI_HandleTypeDef *hspi)
  2361. {
  2362. /* Disable TXE and ERR interrupt */
  2363. __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_TXE | SPI_IT_ERR));
  2364. /* Clear OVERUN flag in 2 Lines communication mode because received is not read */
  2365. if(hspi->Init.Direction == SPI_DIRECTION_2LINES)
  2366. {
  2367. __HAL_SPI_CLEAR_OVRFLAG(hspi);
  2368. }
  2369. hspi->State = HAL_SPI_STATE_READY;
  2370. if(hspi->ErrorCode != HAL_SPI_ERROR_NONE)
  2371. {
  2372. HAL_SPI_ErrorCallback(hspi);
  2373. }
  2374. else
  2375. {
  2376. HAL_SPI_TxCpltCallback(hspi);
  2377. }
  2378. }
  2379. /**
  2380. * @}
  2381. */
  2382. #endif /* HAL_SPI_MODULE_ENABLED */
  2383. /**
  2384. * @}
  2385. */
  2386. /**
  2387. * @}
  2388. */
  2389. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/