123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020 |
- /**
- ******************************************************************************
- * @file stm32f1xx_hal_spi.c
- * @author MCD Application Team
- * @brief SPI HAL module driver.
- * This file provides firmware functions to manage the following
- * functionalities of the Serial Peripheral Interface (SPI) peripheral:
- * + Initialization and de-initialization functions
- * + IO operation functions
- * + Peripheral Control functions
- * + Peripheral State functions
- ******************************************************************************
- * @attention
- *
- * Copyright (c) 2016 STMicroelectronics.
- * All rights reserved.
- *
- * This software is licensed under terms that can be found in the LICENSE file
- * in the root directory of this software component.
- * If no LICENSE file comes with this software, it is provided AS-IS.
- *
- ******************************************************************************
- @verbatim
- ==============================================================================
- ##### How to use this driver #####
- ==============================================================================
- [..]
- The SPI HAL driver can be used as follows:
- (#) Declare a SPI_HandleTypeDef handle structure, for example:
- SPI_HandleTypeDef hspi;
- (#)Initialize the SPI low level resources by implementing the HAL_SPI_MspInit() API:
- (##) Enable the SPIx interface clock
- (##) SPI pins configuration
- (+++) Enable the clock for the SPI GPIOs
- (+++) Configure these SPI pins as alternate function push-pull
- (##) NVIC configuration if you need to use interrupt process
- (+++) Configure the SPIx interrupt priority
- (+++) Enable the NVIC SPI IRQ handle
- (##) DMA Configuration if you need to use DMA process
- (+++) Declare a DMA_HandleTypeDef handle structure for the transmit or receive Stream/Channel
- (+++) Enable the DMAx clock
- (+++) Configure the DMA handle parameters
- (+++) Configure the DMA Tx or Rx Stream/Channel
- (+++) Associate the initialized hdma_tx(or _rx) handle to the hspi DMA Tx or Rx handle
- (+++) Configure the priority and enable the NVIC for the transfer complete interrupt on the DMA Tx or Rx Stream/Channel
- (#) Program the Mode, BidirectionalMode , Data size, Baudrate Prescaler, NSS
- management, Clock polarity and phase, FirstBit and CRC configuration in the hspi Init structure.
- (#) Initialize the SPI registers by calling the HAL_SPI_Init() API:
- (++) This API configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
- by calling the customized HAL_SPI_MspInit() API.
- [..]
- Circular mode restriction:
- (#) The DMA circular mode cannot be used when the SPI is configured in these modes:
- (##) Master 2Lines RxOnly
- (##) Master 1Line Rx
- (#) The CRC feature is not managed when the DMA circular mode is enabled
- (#) When the SPI DMA Pause/Stop features are used, we must use the following APIs
- the HAL_SPI_DMAPause()/ HAL_SPI_DMAStop() only under the SPI callbacks
- [..]
- Master Receive mode restriction:
- (#) In Master unidirectional receive-only mode (MSTR =1, BIDIMODE=0, RXONLY=1) or
- bidirectional receive mode (MSTR=1, BIDIMODE=1, BIDIOE=0), to ensure that the SPI
- does not initiate a new transfer the following procedure has to be respected:
- (##) HAL_SPI_DeInit()
- (##) HAL_SPI_Init()
- [..]
- Callback registration:
- (#) The compilation flag USE_HAL_SPI_REGISTER_CALLBACKS when set to 1U
- allows the user to configure dynamically the driver callbacks.
- Use Functions HAL_SPI_RegisterCallback() to register an interrupt callback.
- Function HAL_SPI_RegisterCallback() allows to register following callbacks:
- (++) TxCpltCallback : SPI Tx Completed callback
- (++) RxCpltCallback : SPI Rx Completed callback
- (++) TxRxCpltCallback : SPI TxRx Completed callback
- (++) TxHalfCpltCallback : SPI Tx Half Completed callback
- (++) RxHalfCpltCallback : SPI Rx Half Completed callback
- (++) TxRxHalfCpltCallback : SPI TxRx Half Completed callback
- (++) ErrorCallback : SPI Error callback
- (++) AbortCpltCallback : SPI Abort callback
- (++) MspInitCallback : SPI Msp Init callback
- (++) MspDeInitCallback : SPI Msp DeInit callback
- This function takes as parameters the HAL peripheral handle, the Callback ID
- and a pointer to the user callback function.
- (#) Use function HAL_SPI_UnRegisterCallback to reset a callback to the default
- weak function.
- HAL_SPI_UnRegisterCallback takes as parameters the HAL peripheral handle,
- and the Callback ID.
- This function allows to reset following callbacks:
- (++) TxCpltCallback : SPI Tx Completed callback
- (++) RxCpltCallback : SPI Rx Completed callback
- (++) TxRxCpltCallback : SPI TxRx Completed callback
- (++) TxHalfCpltCallback : SPI Tx Half Completed callback
- (++) RxHalfCpltCallback : SPI Rx Half Completed callback
- (++) TxRxHalfCpltCallback : SPI TxRx Half Completed callback
- (++) ErrorCallback : SPI Error callback
- (++) AbortCpltCallback : SPI Abort callback
- (++) MspInitCallback : SPI Msp Init callback
- (++) MspDeInitCallback : SPI Msp DeInit callback
- [..]
- By default, after the HAL_SPI_Init() and when the state is HAL_SPI_STATE_RESET
- all callbacks are set to the corresponding weak functions:
- examples HAL_SPI_MasterTxCpltCallback(), HAL_SPI_MasterRxCpltCallback().
- Exception done for MspInit and MspDeInit functions that are
- reset to the legacy weak functions in the HAL_SPI_Init()/ HAL_SPI_DeInit() only when
- these callbacks are null (not registered beforehand).
- If MspInit or MspDeInit are not null, the HAL_SPI_Init()/ HAL_SPI_DeInit()
- keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
- [..]
- Callbacks can be registered/unregistered in HAL_SPI_STATE_READY state only.
- Exception done MspInit/MspDeInit functions that can be registered/unregistered
- in HAL_SPI_STATE_READY or HAL_SPI_STATE_RESET state,
- thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
- Then, the user first registers the MspInit/MspDeInit user callbacks
- using HAL_SPI_RegisterCallback() before calling HAL_SPI_DeInit()
- or HAL_SPI_Init() function.
- [..]
- When the compilation define USE_HAL_PPP_REGISTER_CALLBACKS is set to 0 or
- not defined, the callback registering feature is not available
- and weak (surcharged) callbacks are used.
- [..]
- Using the HAL it is not possible to reach all supported SPI frequency with the different SPI Modes,
- the following table resume the max SPI frequency reached with data size 8bits/16bits,
- according to frequency of the APBx Peripheral Clock (fPCLK) used by the SPI instance.
- @endverbatim
- Additional table :
- DataSize = SPI_DATASIZE_8BIT:
- +----------------------------------------------------------------------------------------------+
- | | | 2Lines Fullduplex | 2Lines RxOnly | 1Line |
- | Process | Transfer mode |---------------------|----------------------|----------------------|
- | | | Master | Slave | Master | Slave | Master | Slave |
- |==============================================================================================|
- | T | Polling | Fpclk/2 | Fpclk/2 | NA | NA | NA | NA |
- | X |----------------|----------|----------|-----------|----------|-----------|----------|
- | / | Interrupt | Fpclk/4 | Fpclk/8 | NA | NA | NA | NA |
- | R |----------------|----------|----------|-----------|----------|-----------|----------|
- | X | DMA | Fpclk/2 | Fpclk/2 | NA | NA | NA | NA |
- |=========|================|==========|==========|===========|==========|===========|==========|
- | | Polling | Fpclk/2 | Fpclk/2 | Fpclk/64 | Fpclk/2 | Fpclk/64 | Fpclk/2 |
- | |----------------|----------|----------|-----------|----------|-----------|----------|
- | R | Interrupt | Fpclk/8 | Fpclk/8 | Fpclk/64 | Fpclk/2 | Fpclk/64 | Fpclk/2 |
- | X |----------------|----------|----------|-----------|----------|-----------|----------|
- | | DMA | Fpclk/2 | Fpclk/2 | Fpclk/64 | Fpclk/2 | Fpclk/128 | Fpclk/2 |
- |=========|================|==========|==========|===========|==========|===========|==========|
- | | Polling | Fpclk/2 | Fpclk/4 | NA | NA | Fpclk/2 | Fpclk/64 |
- | |----------------|----------|----------|-----------|----------|-----------|----------|
- | T | Interrupt | Fpclk/2 | Fpclk/4 | NA | NA | Fpclk/2 | Fpclk/64 |
- | X |----------------|----------|----------|-----------|----------|-----------|----------|
- | | DMA | Fpclk/2 | Fpclk/2 | NA | NA | Fpclk/2 | Fpclk/128|
- +----------------------------------------------------------------------------------------------+
- DataSize = SPI_DATASIZE_16BIT:
- +----------------------------------------------------------------------------------------------+
- | | | 2Lines Fullduplex | 2Lines RxOnly | 1Line |
- | Process | Transfer mode |---------------------|----------------------|----------------------|
- | | | Master | Slave | Master | Slave | Master | Slave |
- |==============================================================================================|
- | T | Polling | Fpclk/2 | Fpclk/2 | NA | NA | NA | NA |
- | X |----------------|----------|----------|-----------|----------|-----------|----------|
- | / | Interrupt | Fpclk/4 | Fpclk/4 | NA | NA | NA | NA |
- | R |----------------|----------|----------|-----------|----------|-----------|----------|
- | X | DMA | Fpclk/2 | Fpclk/2 | NA | NA | NA | NA |
- |=========|================|==========|==========|===========|==========|===========|==========|
- | | Polling | Fpclk/2 | Fpclk/2 | Fpclk/64 | Fpclk/2 | Fpclk/32 | Fpclk/2 |
- | |----------------|----------|----------|-----------|----------|-----------|----------|
- | R | Interrupt | Fpclk/4 | Fpclk/4 | Fpclk/64 | Fpclk/2 | Fpclk/64 | Fpclk/2 |
- | X |----------------|----------|----------|-----------|----------|-----------|----------|
- | | DMA | Fpclk/2 | Fpclk/2 | Fpclk/64 | Fpclk/2 | Fpclk/128 | Fpclk/2 |
- |=========|================|==========|==========|===========|==========|===========|==========|
- | | Polling | Fpclk/2 | Fpclk/2 | NA | NA | Fpclk/2 | Fpclk/32 |
- | |----------------|----------|----------|-----------|----------|-----------|----------|
- | T | Interrupt | Fpclk/2 | Fpclk/2 | NA | NA | Fpclk/2 | Fpclk/64 |
- | X |----------------|----------|----------|-----------|----------|-----------|----------|
- | | DMA | Fpclk/2 | Fpclk/2 | NA | NA | Fpclk/2 | Fpclk/128|
- +----------------------------------------------------------------------------------------------+
- @note The max SPI frequency depend on SPI data size (8bits, 16bits),
- SPI mode(2 Lines fullduplex, 2 lines RxOnly, 1 line TX/RX) and Process mode (Polling, IT, DMA).
- @note
- (#) TX/RX processes are HAL_SPI_TransmitReceive(), HAL_SPI_TransmitReceive_IT() and HAL_SPI_TransmitReceive_DMA()
- (#) RX processes are HAL_SPI_Receive(), HAL_SPI_Receive_IT() and HAL_SPI_Receive_DMA()
- (#) TX processes are HAL_SPI_Transmit(), HAL_SPI_Transmit_IT() and HAL_SPI_Transmit_DMA()
- */
- /* Includes ------------------------------------------------------------------*/
- #include "stm32f1xx_hal.h"
- /** @addtogroup STM32F1xx_HAL_Driver
- * @{
- */
- /** @defgroup SPI SPI
- * @brief SPI HAL module driver
- * @{
- */
- #ifdef HAL_SPI_MODULE_ENABLED
- /* Private typedef -----------------------------------------------------------*/
- /* Private defines -----------------------------------------------------------*/
- #if (USE_SPI_CRC != 0U) && defined(SPI_CRC_ERROR_WORKAROUND_FEATURE)
- /* CRC WORKAROUND FEATURE: Variable used to determine if device is impacted by implementation
- * of workaround related to wrong CRC errors detection on SPI2. Conditions in which this workaround
- * has to be applied, are:
- * - STM32F101CDE/STM32F103CDE
- * - Revision ID : Z
- * - SPI2
- * - In receive only mode, with CRC calculation enabled, at the end of the CRC reception,
- * the software needs to check the CRCERR flag. If it is found set, read back the SPI_RXCRC:
- * + If the value is 0, the complete data transfer is successful.
- * + Otherwise, one or more errors have been detected during the data transfer by CPU or DMA.
- * If CRCERR is found reset, the complete data transfer is considered successful.
- *
- * Check RevisionID value for identifying if Device is Rev Z (0x0001) in order to enable workaround for
- * CRC errors wrongly detected
- */
- /* Pb is that ES_STM32F10xxCDE also identify an issue in Debug registers access while not in Debug mode
- * Revision ID information is only available in Debug mode, so Workaround could not be implemented
- * to distinguish Rev Z devices (issue present) from more recent version (issue fixed).
- * So, in case of Revision Z F101 or F103 devices, below define should be assigned to 1.
- */
- #define USE_SPI_CRC_ERROR_WORKAROUND 0U
- #endif /* USE_SPI_CRC */
- /** @defgroup SPI_Private_Constants SPI Private Constants
- * @{
- */
- #define SPI_DEFAULT_TIMEOUT 100U
- /**
- * @}
- */
- /* Private macros ------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- /* Private function prototypes -----------------------------------------------*/
- /** @defgroup SPI_Private_Functions SPI Private Functions
- * @{
- */
- static void SPI_DMATransmitCplt(DMA_HandleTypeDef *hdma);
- static void SPI_DMAReceiveCplt(DMA_HandleTypeDef *hdma);
- static void SPI_DMATransmitReceiveCplt(DMA_HandleTypeDef *hdma);
- static void SPI_DMAHalfTransmitCplt(DMA_HandleTypeDef *hdma);
- static void SPI_DMAHalfReceiveCplt(DMA_HandleTypeDef *hdma);
- static void SPI_DMAHalfTransmitReceiveCplt(DMA_HandleTypeDef *hdma);
- static void SPI_DMAError(DMA_HandleTypeDef *hdma);
- static void SPI_DMAAbortOnError(DMA_HandleTypeDef *hdma);
- static void SPI_DMATxAbortCallback(DMA_HandleTypeDef *hdma);
- static void SPI_DMARxAbortCallback(DMA_HandleTypeDef *hdma);
- static HAL_StatusTypeDef SPI_WaitFlagStateUntilTimeout(SPI_HandleTypeDef *hspi, uint32_t Flag, FlagStatus State,
- uint32_t Timeout, uint32_t Tickstart);
- static void SPI_TxISR_8BIT(struct __SPI_HandleTypeDef *hspi);
- static void SPI_TxISR_16BIT(struct __SPI_HandleTypeDef *hspi);
- static void SPI_RxISR_8BIT(struct __SPI_HandleTypeDef *hspi);
- static void SPI_RxISR_16BIT(struct __SPI_HandleTypeDef *hspi);
- static void SPI_2linesRxISR_8BIT(struct __SPI_HandleTypeDef *hspi);
- static void SPI_2linesTxISR_8BIT(struct __SPI_HandleTypeDef *hspi);
- static void SPI_2linesTxISR_16BIT(struct __SPI_HandleTypeDef *hspi);
- static void SPI_2linesRxISR_16BIT(struct __SPI_HandleTypeDef *hspi);
- #if (USE_SPI_CRC != 0U)
- static void SPI_RxISR_8BITCRC(struct __SPI_HandleTypeDef *hspi);
- static void SPI_RxISR_16BITCRC(struct __SPI_HandleTypeDef *hspi);
- static void SPI_2linesRxISR_8BITCRC(struct __SPI_HandleTypeDef *hspi);
- static void SPI_2linesRxISR_16BITCRC(struct __SPI_HandleTypeDef *hspi);
- #endif /* USE_SPI_CRC */
- static void SPI_AbortRx_ISR(SPI_HandleTypeDef *hspi);
- static void SPI_AbortTx_ISR(SPI_HandleTypeDef *hspi);
- static void SPI_CloseRxTx_ISR(SPI_HandleTypeDef *hspi);
- static void SPI_CloseRx_ISR(SPI_HandleTypeDef *hspi);
- static void SPI_CloseTx_ISR(SPI_HandleTypeDef *hspi);
- static HAL_StatusTypeDef SPI_EndRxTransaction(SPI_HandleTypeDef *hspi, uint32_t Timeout, uint32_t Tickstart);
- static HAL_StatusTypeDef SPI_EndRxTxTransaction(SPI_HandleTypeDef *hspi, uint32_t Timeout, uint32_t Tickstart);
- /**
- * @}
- */
- /* Exported functions --------------------------------------------------------*/
- /** @defgroup SPI_Exported_Functions SPI Exported Functions
- * @{
- */
- /** @defgroup SPI_Exported_Functions_Group1 Initialization and de-initialization functions
- * @brief Initialization and Configuration functions
- *
- @verbatim
- ===============================================================================
- ##### Initialization and de-initialization functions #####
- ===============================================================================
- [..] This subsection provides a set of functions allowing to initialize and
- de-initialize the SPIx peripheral:
- (+) User must implement HAL_SPI_MspInit() function in which he configures
- all related peripherals resources (CLOCK, GPIO, DMA, IT and NVIC ).
- (+) Call the function HAL_SPI_Init() to configure the selected device with
- the selected configuration:
- (++) Mode
- (++) Direction
- (++) Data Size
- (++) Clock Polarity and Phase
- (++) NSS Management
- (++) BaudRate Prescaler
- (++) FirstBit
- (++) TIMode
- (++) CRC Calculation
- (++) CRC Polynomial if CRC enabled
- (+) Call the function HAL_SPI_DeInit() to restore the default configuration
- of the selected SPIx peripheral.
- @endverbatim
- * @{
- */
- /**
- * @brief Initialize the SPI according to the specified parameters
- * in the SPI_InitTypeDef and initialize the associated handle.
- * @param hspi pointer to a SPI_HandleTypeDef structure that contains
- * the configuration information for SPI module.
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_SPI_Init(SPI_HandleTypeDef *hspi)
- {
- /* Check the SPI handle allocation */
- if (hspi == NULL)
- {
- return HAL_ERROR;
- }
- /* Check the parameters */
- assert_param(IS_SPI_ALL_INSTANCE(hspi->Instance));
- assert_param(IS_SPI_MODE(hspi->Init.Mode));
- assert_param(IS_SPI_DIRECTION(hspi->Init.Direction));
- assert_param(IS_SPI_DATASIZE(hspi->Init.DataSize));
- assert_param(IS_SPI_NSS(hspi->Init.NSS));
- assert_param(IS_SPI_BAUDRATE_PRESCALER(hspi->Init.BaudRatePrescaler));
- assert_param(IS_SPI_FIRST_BIT(hspi->Init.FirstBit));
- /* TI mode is not supported on this device.
- TIMode parameter is mandatory equal to SPI_TIMODE_DISABLE */
- assert_param(IS_SPI_TIMODE(hspi->Init.TIMode));
- if (hspi->Init.TIMode == SPI_TIMODE_DISABLE)
- {
- assert_param(IS_SPI_CPOL(hspi->Init.CLKPolarity));
- assert_param(IS_SPI_CPHA(hspi->Init.CLKPhase));
- if (hspi->Init.Mode == SPI_MODE_MASTER)
- {
- assert_param(IS_SPI_BAUDRATE_PRESCALER(hspi->Init.BaudRatePrescaler));
- }
- else
- {
- /* Baudrate prescaler not use in Motoraola Slave mode. force to default value */
- hspi->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
- }
- }
- else
- {
- assert_param(IS_SPI_BAUDRATE_PRESCALER(hspi->Init.BaudRatePrescaler));
- /* Force polarity and phase to TI protocaol requirements */
- hspi->Init.CLKPolarity = SPI_POLARITY_LOW;
- hspi->Init.CLKPhase = SPI_PHASE_1EDGE;
- }
- #if (USE_SPI_CRC != 0U)
- assert_param(IS_SPI_CRC_CALCULATION(hspi->Init.CRCCalculation));
- if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
- {
- assert_param(IS_SPI_CRC_POLYNOMIAL(hspi->Init.CRCPolynomial));
- }
- #else
- hspi->Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
- #endif /* USE_SPI_CRC */
- if (hspi->State == HAL_SPI_STATE_RESET)
- {
- /* Allocate lock resource and initialize it */
- hspi->Lock = HAL_UNLOCKED;
- #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
- /* Init the SPI Callback settings */
- hspi->TxCpltCallback = HAL_SPI_TxCpltCallback; /* Legacy weak TxCpltCallback */
- hspi->RxCpltCallback = HAL_SPI_RxCpltCallback; /* Legacy weak RxCpltCallback */
- hspi->TxRxCpltCallback = HAL_SPI_TxRxCpltCallback; /* Legacy weak TxRxCpltCallback */
- hspi->TxHalfCpltCallback = HAL_SPI_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */
- hspi->RxHalfCpltCallback = HAL_SPI_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */
- hspi->TxRxHalfCpltCallback = HAL_SPI_TxRxHalfCpltCallback; /* Legacy weak TxRxHalfCpltCallback */
- hspi->ErrorCallback = HAL_SPI_ErrorCallback; /* Legacy weak ErrorCallback */
- hspi->AbortCpltCallback = HAL_SPI_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
- if (hspi->MspInitCallback == NULL)
- {
- hspi->MspInitCallback = HAL_SPI_MspInit; /* Legacy weak MspInit */
- }
- /* Init the low level hardware : GPIO, CLOCK, NVIC... */
- hspi->MspInitCallback(hspi);
- #else
- /* Init the low level hardware : GPIO, CLOCK, NVIC... */
- HAL_SPI_MspInit(hspi);
- #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
- }
- hspi->State = HAL_SPI_STATE_BUSY;
- /* Disable the selected SPI peripheral */
- __HAL_SPI_DISABLE(hspi);
- /*----------------------- SPIx CR1 & CR2 Configuration ---------------------*/
- /* Configure : SPI Mode, Communication Mode, Data size, Clock polarity and phase, NSS management,
- Communication speed, First bit and CRC calculation state */
- WRITE_REG(hspi->Instance->CR1, ((hspi->Init.Mode & (SPI_CR1_MSTR | SPI_CR1_SSI)) |
- (hspi->Init.Direction & (SPI_CR1_RXONLY | SPI_CR1_BIDIMODE)) |
- (hspi->Init.DataSize & SPI_CR1_DFF) |
- (hspi->Init.CLKPolarity & SPI_CR1_CPOL) |
- (hspi->Init.CLKPhase & SPI_CR1_CPHA) |
- (hspi->Init.NSS & SPI_CR1_SSM) |
- (hspi->Init.BaudRatePrescaler & SPI_CR1_BR_Msk) |
- (hspi->Init.FirstBit & SPI_CR1_LSBFIRST) |
- (hspi->Init.CRCCalculation & SPI_CR1_CRCEN)));
- /* Configure : NSS management */
- WRITE_REG(hspi->Instance->CR2, ((hspi->Init.NSS >> 16U) & SPI_CR2_SSOE));
- #if (USE_SPI_CRC != 0U)
- /*---------------------------- SPIx CRCPOLY Configuration ------------------*/
- /* Configure : CRC Polynomial */
- if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
- {
- WRITE_REG(hspi->Instance->CRCPR, (hspi->Init.CRCPolynomial & SPI_CRCPR_CRCPOLY_Msk));
- }
- #endif /* USE_SPI_CRC */
- #if defined(SPI_I2SCFGR_I2SMOD)
- /* Activate the SPI mode (Make sure that I2SMOD bit in I2SCFGR register is reset) */
- CLEAR_BIT(hspi->Instance->I2SCFGR, SPI_I2SCFGR_I2SMOD);
- #endif /* SPI_I2SCFGR_I2SMOD */
- hspi->ErrorCode = HAL_SPI_ERROR_NONE;
- hspi->State = HAL_SPI_STATE_READY;
- return HAL_OK;
- }
- /**
- * @brief De-Initialize the SPI peripheral.
- * @param hspi pointer to a SPI_HandleTypeDef structure that contains
- * the configuration information for SPI module.
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_SPI_DeInit(SPI_HandleTypeDef *hspi)
- {
- /* Check the SPI handle allocation */
- if (hspi == NULL)
- {
- return HAL_ERROR;
- }
- /* Check SPI Instance parameter */
- assert_param(IS_SPI_ALL_INSTANCE(hspi->Instance));
- hspi->State = HAL_SPI_STATE_BUSY;
- /* Disable the SPI Peripheral Clock */
- __HAL_SPI_DISABLE(hspi);
- #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
- if (hspi->MspDeInitCallback == NULL)
- {
- hspi->MspDeInitCallback = HAL_SPI_MspDeInit; /* Legacy weak MspDeInit */
- }
- /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */
- hspi->MspDeInitCallback(hspi);
- #else
- /* DeInit the low level hardware: GPIO, CLOCK, NVIC... */
- HAL_SPI_MspDeInit(hspi);
- #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
- hspi->ErrorCode = HAL_SPI_ERROR_NONE;
- hspi->State = HAL_SPI_STATE_RESET;
- /* Release Lock */
- __HAL_UNLOCK(hspi);
- return HAL_OK;
- }
- /**
- * @brief Initialize the SPI MSP.
- * @param hspi pointer to a SPI_HandleTypeDef structure that contains
- * the configuration information for SPI module.
- * @retval None
- */
- __weak void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(hspi);
- /* NOTE : This function should not be modified, when the callback is needed,
- the HAL_SPI_MspInit should be implemented in the user file
- */
- }
- /**
- * @brief De-Initialize the SPI MSP.
- * @param hspi pointer to a SPI_HandleTypeDef structure that contains
- * the configuration information for SPI module.
- * @retval None
- */
- __weak void HAL_SPI_MspDeInit(SPI_HandleTypeDef *hspi)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(hspi);
- /* NOTE : This function should not be modified, when the callback is needed,
- the HAL_SPI_MspDeInit should be implemented in the user file
- */
- }
- #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
- /**
- * @brief Register a User SPI Callback
- * To be used instead of the weak predefined callback
- * @param hspi Pointer to a SPI_HandleTypeDef structure that contains
- * the configuration information for the specified SPI.
- * @param CallbackID ID of the callback to be registered
- * @param pCallback pointer to the Callback function
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_SPI_RegisterCallback(SPI_HandleTypeDef *hspi, HAL_SPI_CallbackIDTypeDef CallbackID,
- pSPI_CallbackTypeDef pCallback)
- {
- HAL_StatusTypeDef status = HAL_OK;
- if (pCallback == NULL)
- {
- /* Update the error code */
- hspi->ErrorCode |= HAL_SPI_ERROR_INVALID_CALLBACK;
- return HAL_ERROR;
- }
- /* Process locked */
- __HAL_LOCK(hspi);
- if (HAL_SPI_STATE_READY == hspi->State)
- {
- switch (CallbackID)
- {
- case HAL_SPI_TX_COMPLETE_CB_ID :
- hspi->TxCpltCallback = pCallback;
- break;
- case HAL_SPI_RX_COMPLETE_CB_ID :
- hspi->RxCpltCallback = pCallback;
- break;
- case HAL_SPI_TX_RX_COMPLETE_CB_ID :
- hspi->TxRxCpltCallback = pCallback;
- break;
- case HAL_SPI_TX_HALF_COMPLETE_CB_ID :
- hspi->TxHalfCpltCallback = pCallback;
- break;
- case HAL_SPI_RX_HALF_COMPLETE_CB_ID :
- hspi->RxHalfCpltCallback = pCallback;
- break;
- case HAL_SPI_TX_RX_HALF_COMPLETE_CB_ID :
- hspi->TxRxHalfCpltCallback = pCallback;
- break;
- case HAL_SPI_ERROR_CB_ID :
- hspi->ErrorCallback = pCallback;
- break;
- case HAL_SPI_ABORT_CB_ID :
- hspi->AbortCpltCallback = pCallback;
- break;
- case HAL_SPI_MSPINIT_CB_ID :
- hspi->MspInitCallback = pCallback;
- break;
- case HAL_SPI_MSPDEINIT_CB_ID :
- hspi->MspDeInitCallback = pCallback;
- break;
- default :
- /* Update the error code */
- SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_INVALID_CALLBACK);
- /* Return error status */
- status = HAL_ERROR;
- break;
- }
- }
- else if (HAL_SPI_STATE_RESET == hspi->State)
- {
- switch (CallbackID)
- {
- case HAL_SPI_MSPINIT_CB_ID :
- hspi->MspInitCallback = pCallback;
- break;
- case HAL_SPI_MSPDEINIT_CB_ID :
- hspi->MspDeInitCallback = pCallback;
- break;
- default :
- /* Update the error code */
- SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_INVALID_CALLBACK);
- /* Return error status */
- status = HAL_ERROR;
- break;
- }
- }
- else
- {
- /* Update the error code */
- SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_INVALID_CALLBACK);
- /* Return error status */
- status = HAL_ERROR;
- }
- /* Release Lock */
- __HAL_UNLOCK(hspi);
- return status;
- }
- /**
- * @brief Unregister an SPI Callback
- * SPI callback is redirected to the weak predefined callback
- * @param hspi Pointer to a SPI_HandleTypeDef structure that contains
- * the configuration information for the specified SPI.
- * @param CallbackID ID of the callback to be unregistered
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_SPI_UnRegisterCallback(SPI_HandleTypeDef *hspi, HAL_SPI_CallbackIDTypeDef CallbackID)
- {
- HAL_StatusTypeDef status = HAL_OK;
- /* Process locked */
- __HAL_LOCK(hspi);
- if (HAL_SPI_STATE_READY == hspi->State)
- {
- switch (CallbackID)
- {
- case HAL_SPI_TX_COMPLETE_CB_ID :
- hspi->TxCpltCallback = HAL_SPI_TxCpltCallback; /* Legacy weak TxCpltCallback */
- break;
- case HAL_SPI_RX_COMPLETE_CB_ID :
- hspi->RxCpltCallback = HAL_SPI_RxCpltCallback; /* Legacy weak RxCpltCallback */
- break;
- case HAL_SPI_TX_RX_COMPLETE_CB_ID :
- hspi->TxRxCpltCallback = HAL_SPI_TxRxCpltCallback; /* Legacy weak TxRxCpltCallback */
- break;
- case HAL_SPI_TX_HALF_COMPLETE_CB_ID :
- hspi->TxHalfCpltCallback = HAL_SPI_TxHalfCpltCallback; /* Legacy weak TxHalfCpltCallback */
- break;
- case HAL_SPI_RX_HALF_COMPLETE_CB_ID :
- hspi->RxHalfCpltCallback = HAL_SPI_RxHalfCpltCallback; /* Legacy weak RxHalfCpltCallback */
- break;
- case HAL_SPI_TX_RX_HALF_COMPLETE_CB_ID :
- hspi->TxRxHalfCpltCallback = HAL_SPI_TxRxHalfCpltCallback; /* Legacy weak TxRxHalfCpltCallback */
- break;
- case HAL_SPI_ERROR_CB_ID :
- hspi->ErrorCallback = HAL_SPI_ErrorCallback; /* Legacy weak ErrorCallback */
- break;
- case HAL_SPI_ABORT_CB_ID :
- hspi->AbortCpltCallback = HAL_SPI_AbortCpltCallback; /* Legacy weak AbortCpltCallback */
- break;
- case HAL_SPI_MSPINIT_CB_ID :
- hspi->MspInitCallback = HAL_SPI_MspInit; /* Legacy weak MspInit */
- break;
- case HAL_SPI_MSPDEINIT_CB_ID :
- hspi->MspDeInitCallback = HAL_SPI_MspDeInit; /* Legacy weak MspDeInit */
- break;
- default :
- /* Update the error code */
- SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_INVALID_CALLBACK);
- /* Return error status */
- status = HAL_ERROR;
- break;
- }
- }
- else if (HAL_SPI_STATE_RESET == hspi->State)
- {
- switch (CallbackID)
- {
- case HAL_SPI_MSPINIT_CB_ID :
- hspi->MspInitCallback = HAL_SPI_MspInit; /* Legacy weak MspInit */
- break;
- case HAL_SPI_MSPDEINIT_CB_ID :
- hspi->MspDeInitCallback = HAL_SPI_MspDeInit; /* Legacy weak MspDeInit */
- break;
- default :
- /* Update the error code */
- SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_INVALID_CALLBACK);
- /* Return error status */
- status = HAL_ERROR;
- break;
- }
- }
- else
- {
- /* Update the error code */
- SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_INVALID_CALLBACK);
- /* Return error status */
- status = HAL_ERROR;
- }
- /* Release Lock */
- __HAL_UNLOCK(hspi);
- return status;
- }
- #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
- /**
- * @}
- */
- /** @defgroup SPI_Exported_Functions_Group2 IO operation functions
- * @brief Data transfers functions
- *
- @verbatim
- ==============================================================================
- ##### IO operation functions #####
- ===============================================================================
- [..]
- This subsection provides a set of functions allowing to manage the SPI
- data transfers.
- [..] The SPI supports master and slave mode :
- (#) There are two modes of transfer:
- (++) Blocking mode: The communication is performed in polling mode.
- The HAL status of all data processing is returned by the same function
- after finishing transfer.
- (++) No-Blocking mode: The communication is performed using Interrupts
- or DMA, These APIs return the HAL status.
- The end of the data processing will be indicated through the
- dedicated SPI IRQ when using Interrupt mode or the DMA IRQ when
- using DMA mode.
- The HAL_SPI_TxCpltCallback(), HAL_SPI_RxCpltCallback() and HAL_SPI_TxRxCpltCallback() user callbacks
- will be executed respectively at the end of the transmit or Receive process
- The HAL_SPI_ErrorCallback()user callback will be executed when a communication error is detected
- (#) APIs provided for these 2 transfer modes (Blocking mode or Non blocking mode using either Interrupt or DMA)
- exist for 1Line (simplex) and 2Lines (full duplex) modes.
- @endverbatim
- * @{
- */
- /**
- * @brief Transmit an amount of data in blocking mode.
- * @param hspi pointer to a SPI_HandleTypeDef structure that contains
- * the configuration information for SPI module.
- * @param pData pointer to data buffer
- * @param Size amount of data to be sent
- * @param Timeout Timeout duration
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_SPI_Transmit(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout)
- {
- uint32_t tickstart;
- HAL_StatusTypeDef errorcode = HAL_OK;
- uint16_t initial_TxXferCount;
- /* Check Direction parameter */
- assert_param(IS_SPI_DIRECTION_2LINES_OR_1LINE(hspi->Init.Direction));
- /* Process Locked */
- __HAL_LOCK(hspi);
- /* Init tickstart for timeout management*/
- tickstart = HAL_GetTick();
- initial_TxXferCount = Size;
- if (hspi->State != HAL_SPI_STATE_READY)
- {
- errorcode = HAL_BUSY;
- goto error;
- }
- if ((pData == NULL) || (Size == 0U))
- {
- errorcode = HAL_ERROR;
- goto error;
- }
- /* Set the transaction information */
- hspi->State = HAL_SPI_STATE_BUSY_TX;
- hspi->ErrorCode = HAL_SPI_ERROR_NONE;
- hspi->pTxBuffPtr = (uint8_t *)pData;
- hspi->TxXferSize = Size;
- hspi->TxXferCount = Size;
- /*Init field not used in handle to zero */
- hspi->pRxBuffPtr = (uint8_t *)NULL;
- hspi->RxXferSize = 0U;
- hspi->RxXferCount = 0U;
- hspi->TxISR = NULL;
- hspi->RxISR = NULL;
- /* Configure communication direction : 1Line */
- if (hspi->Init.Direction == SPI_DIRECTION_1LINE)
- {
- /* Disable SPI Peripheral before set 1Line direction (BIDIOE bit) */
- __HAL_SPI_DISABLE(hspi);
- SPI_1LINE_TX(hspi);
- }
- #if (USE_SPI_CRC != 0U)
- /* Reset CRC Calculation */
- if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
- {
- SPI_RESET_CRC(hspi);
- }
- #endif /* USE_SPI_CRC */
- /* Check if the SPI is already enabled */
- if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
- {
- /* Enable SPI peripheral */
- __HAL_SPI_ENABLE(hspi);
- }
- /* Transmit data in 16 Bit mode */
- if (hspi->Init.DataSize == SPI_DATASIZE_16BIT)
- {
- if ((hspi->Init.Mode == SPI_MODE_SLAVE) || (initial_TxXferCount == 0x01U))
- {
- hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr);
- hspi->pTxBuffPtr += sizeof(uint16_t);
- hspi->TxXferCount--;
- }
- /* Transmit data in 16 Bit mode */
- while (hspi->TxXferCount > 0U)
- {
- /* Wait until TXE flag is set to send data */
- if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_TXE))
- {
- hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr);
- hspi->pTxBuffPtr += sizeof(uint16_t);
- hspi->TxXferCount--;
- }
- else
- {
- /* Timeout management */
- if ((((HAL_GetTick() - tickstart) >= Timeout) && (Timeout != HAL_MAX_DELAY)) || (Timeout == 0U))
- {
- errorcode = HAL_TIMEOUT;
- hspi->State = HAL_SPI_STATE_READY;
- goto error;
- }
- }
- }
- }
- /* Transmit data in 8 Bit mode */
- else
- {
- if ((hspi->Init.Mode == SPI_MODE_SLAVE) || (initial_TxXferCount == 0x01U))
- {
- *((__IO uint8_t *)&hspi->Instance->DR) = (*hspi->pTxBuffPtr);
- hspi->pTxBuffPtr += sizeof(uint8_t);
- hspi->TxXferCount--;
- }
- while (hspi->TxXferCount > 0U)
- {
- /* Wait until TXE flag is set to send data */
- if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_TXE))
- {
- *((__IO uint8_t *)&hspi->Instance->DR) = (*hspi->pTxBuffPtr);
- hspi->pTxBuffPtr += sizeof(uint8_t);
- hspi->TxXferCount--;
- }
- else
- {
- /* Timeout management */
- if ((((HAL_GetTick() - tickstart) >= Timeout) && (Timeout != HAL_MAX_DELAY)) || (Timeout == 0U))
- {
- errorcode = HAL_TIMEOUT;
- hspi->State = HAL_SPI_STATE_READY;
- goto error;
- }
- }
- }
- }
- #if (USE_SPI_CRC != 0U)
- /* Enable CRC Transmission */
- if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
- {
- SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
- }
- #endif /* USE_SPI_CRC */
- /* Check the end of the transaction */
- if (SPI_EndRxTxTransaction(hspi, Timeout, tickstart) != HAL_OK)
- {
- hspi->ErrorCode = HAL_SPI_ERROR_FLAG;
- }
- /* Clear overrun flag in 2 Lines communication mode because received is not read */
- if (hspi->Init.Direction == SPI_DIRECTION_2LINES)
- {
- __HAL_SPI_CLEAR_OVRFLAG(hspi);
- }
- if (hspi->ErrorCode != HAL_SPI_ERROR_NONE)
- {
- errorcode = HAL_ERROR;
- }
- else
- {
- hspi->State = HAL_SPI_STATE_READY;
- }
- error:
- /* Process Unlocked */
- __HAL_UNLOCK(hspi);
- return errorcode;
- }
- /**
- * @brief Receive an amount of data in blocking mode.
- * @param hspi pointer to a SPI_HandleTypeDef structure that contains
- * the configuration information for SPI module.
- * @param pData pointer to data buffer
- * @param Size amount of data to be received
- * @param Timeout Timeout duration
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_SPI_Receive(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout)
- {
- #if (USE_SPI_CRC != 0U)
- __IO uint32_t tmpreg = 0U;
- #endif /* USE_SPI_CRC */
- uint32_t tickstart;
- HAL_StatusTypeDef errorcode = HAL_OK;
- if (hspi->State != HAL_SPI_STATE_READY)
- {
- errorcode = HAL_BUSY;
- goto error;
- }
- if ((hspi->Init.Mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES))
- {
- hspi->State = HAL_SPI_STATE_BUSY_RX;
- /* Call transmit-receive function to send Dummy data on Tx line and generate clock on CLK line */
- return HAL_SPI_TransmitReceive(hspi, pData, pData, Size, Timeout);
- }
- /* Process Locked */
- __HAL_LOCK(hspi);
- /* Init tickstart for timeout management*/
- tickstart = HAL_GetTick();
- if ((pData == NULL) || (Size == 0U))
- {
- errorcode = HAL_ERROR;
- goto error;
- }
- /* Set the transaction information */
- hspi->State = HAL_SPI_STATE_BUSY_RX;
- hspi->ErrorCode = HAL_SPI_ERROR_NONE;
- hspi->pRxBuffPtr = (uint8_t *)pData;
- hspi->RxXferSize = Size;
- hspi->RxXferCount = Size;
- /*Init field not used in handle to zero */
- hspi->pTxBuffPtr = (uint8_t *)NULL;
- hspi->TxXferSize = 0U;
- hspi->TxXferCount = 0U;
- hspi->RxISR = NULL;
- hspi->TxISR = NULL;
- #if (USE_SPI_CRC != 0U)
- /* Reset CRC Calculation */
- if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
- {
- SPI_RESET_CRC(hspi);
- /* this is done to handle the CRCNEXT before the latest data */
- hspi->RxXferCount--;
- }
- #endif /* USE_SPI_CRC */
- /* Configure communication direction: 1Line */
- if (hspi->Init.Direction == SPI_DIRECTION_1LINE)
- {
- /* Disable SPI Peripheral before set 1Line direction (BIDIOE bit) */
- __HAL_SPI_DISABLE(hspi);
- SPI_1LINE_RX(hspi);
- }
- /* Check if the SPI is already enabled */
- if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
- {
- /* Enable SPI peripheral */
- __HAL_SPI_ENABLE(hspi);
- }
- /* Receive data in 8 Bit mode */
- if (hspi->Init.DataSize == SPI_DATASIZE_8BIT)
- {
- /* Transfer loop */
- while (hspi->RxXferCount > 0U)
- {
- /* Check the RXNE flag */
- if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_RXNE))
- {
- /* read the received data */
- (* (uint8_t *)hspi->pRxBuffPtr) = *(__IO uint8_t *)&hspi->Instance->DR;
- hspi->pRxBuffPtr += sizeof(uint8_t);
- hspi->RxXferCount--;
- }
- else
- {
- /* Timeout management */
- if ((((HAL_GetTick() - tickstart) >= Timeout) && (Timeout != HAL_MAX_DELAY)) || (Timeout == 0U))
- {
- errorcode = HAL_TIMEOUT;
- hspi->State = HAL_SPI_STATE_READY;
- goto error;
- }
- }
- }
- }
- else
- {
- /* Transfer loop */
- while (hspi->RxXferCount > 0U)
- {
- /* Check the RXNE flag */
- if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_RXNE))
- {
- *((uint16_t *)hspi->pRxBuffPtr) = (uint16_t)hspi->Instance->DR;
- hspi->pRxBuffPtr += sizeof(uint16_t);
- hspi->RxXferCount--;
- }
- else
- {
- /* Timeout management */
- if ((((HAL_GetTick() - tickstart) >= Timeout) && (Timeout != HAL_MAX_DELAY)) || (Timeout == 0U))
- {
- errorcode = HAL_TIMEOUT;
- hspi->State = HAL_SPI_STATE_READY;
- goto error;
- }
- }
- }
- }
- #if (USE_SPI_CRC != 0U)
- /* Handle the CRC Transmission */
- if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
- {
- /* freeze the CRC before the latest data */
- SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
- /* Check if CRCNEXT is well reset by hardware */
- if (READ_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT))
- {
- /* Workaround to force CRCNEXT bit to zero in case of CRCNEXT is not reset automatically by hardware */
- CLEAR_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
- }
- /* Read the latest data */
- if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SET, Timeout, tickstart) != HAL_OK)
- {
- /* the latest data has not been received */
- errorcode = HAL_TIMEOUT;
- goto error;
- }
- /* Receive last data in 16 Bit mode */
- if (hspi->Init.DataSize == SPI_DATASIZE_16BIT)
- {
- *((uint16_t *)hspi->pRxBuffPtr) = (uint16_t)hspi->Instance->DR;
- }
- /* Receive last data in 8 Bit mode */
- else
- {
- (*(uint8_t *)hspi->pRxBuffPtr) = *(__IO uint8_t *)&hspi->Instance->DR;
- }
- /* Wait the CRC data */
- if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SET, Timeout, tickstart) != HAL_OK)
- {
- SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
- errorcode = HAL_TIMEOUT;
- goto error;
- }
- /* Read CRC to Flush DR and RXNE flag */
- tmpreg = READ_REG(hspi->Instance->DR);
- /* To avoid GCC warning */
- UNUSED(tmpreg);
- }
- #endif /* USE_SPI_CRC */
- /* Check the end of the transaction */
- if (SPI_EndRxTransaction(hspi, Timeout, tickstart) != HAL_OK)
- {
- hspi->ErrorCode = HAL_SPI_ERROR_FLAG;
- }
- #if (USE_SPI_CRC != 0U)
- /* Check if CRC error occurred */
- if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET)
- {
- /* Check if CRC error is valid or not (workaround to be applied or not) */
- if (SPI_ISCRCErrorValid(hspi) == SPI_VALID_CRC_ERROR)
- {
- SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
- /* Reset CRC Calculation */
- SPI_RESET_CRC(hspi);
- }
- else
- {
- __HAL_SPI_CLEAR_CRCERRFLAG(hspi);
- }
- }
- #endif /* USE_SPI_CRC */
- if (hspi->ErrorCode != HAL_SPI_ERROR_NONE)
- {
- errorcode = HAL_ERROR;
- }
- else
- {
- hspi->State = HAL_SPI_STATE_READY;
- }
- error :
- __HAL_UNLOCK(hspi);
- return errorcode;
- }
- /**
- * @brief Transmit and Receive an amount of data in blocking mode.
- * @param hspi pointer to a SPI_HandleTypeDef structure that contains
- * the configuration information for SPI module.
- * @param pTxData pointer to transmission data buffer
- * @param pRxData pointer to reception data buffer
- * @param Size amount of data to be sent and received
- * @param Timeout Timeout duration
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_SPI_TransmitReceive(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size,
- uint32_t Timeout)
- {
- uint16_t initial_TxXferCount;
- uint32_t tmp_mode;
- HAL_SPI_StateTypeDef tmp_state;
- uint32_t tickstart;
- #if (USE_SPI_CRC != 0U)
- __IO uint32_t tmpreg = 0U;
- #endif /* USE_SPI_CRC */
- /* Variable used to alternate Rx and Tx during transfer */
- uint32_t txallowed = 1U;
- HAL_StatusTypeDef errorcode = HAL_OK;
- /* Check Direction parameter */
- assert_param(IS_SPI_DIRECTION_2LINES(hspi->Init.Direction));
- /* Process Locked */
- __HAL_LOCK(hspi);
- /* Init tickstart for timeout management*/
- tickstart = HAL_GetTick();
- /* Init temporary variables */
- tmp_state = hspi->State;
- tmp_mode = hspi->Init.Mode;
- initial_TxXferCount = Size;
- if (!((tmp_state == HAL_SPI_STATE_READY) || \
- ((tmp_mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES) && (tmp_state == HAL_SPI_STATE_BUSY_RX))))
- {
- errorcode = HAL_BUSY;
- goto error;
- }
- if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0U))
- {
- errorcode = HAL_ERROR;
- goto error;
- }
- /* Don't overwrite in case of HAL_SPI_STATE_BUSY_RX */
- if (hspi->State != HAL_SPI_STATE_BUSY_RX)
- {
- hspi->State = HAL_SPI_STATE_BUSY_TX_RX;
- }
- /* Set the transaction information */
- hspi->ErrorCode = HAL_SPI_ERROR_NONE;
- hspi->pRxBuffPtr = (uint8_t *)pRxData;
- hspi->RxXferCount = Size;
- hspi->RxXferSize = Size;
- hspi->pTxBuffPtr = (uint8_t *)pTxData;
- hspi->TxXferCount = Size;
- hspi->TxXferSize = Size;
- /*Init field not used in handle to zero */
- hspi->RxISR = NULL;
- hspi->TxISR = NULL;
- #if (USE_SPI_CRC != 0U)
- /* Reset CRC Calculation */
- if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
- {
- SPI_RESET_CRC(hspi);
- }
- #endif /* USE_SPI_CRC */
- /* Check if the SPI is already enabled */
- if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
- {
- /* Enable SPI peripheral */
- __HAL_SPI_ENABLE(hspi);
- }
- /* Transmit and Receive data in 16 Bit mode */
- if (hspi->Init.DataSize == SPI_DATASIZE_16BIT)
- {
- if ((hspi->Init.Mode == SPI_MODE_SLAVE) || (initial_TxXferCount == 0x01U))
- {
- hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr);
- hspi->pTxBuffPtr += sizeof(uint16_t);
- hspi->TxXferCount--;
- }
- while ((hspi->TxXferCount > 0U) || (hspi->RxXferCount > 0U))
- {
- /* Check TXE flag */
- if ((__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_TXE)) && (hspi->TxXferCount > 0U) && (txallowed == 1U))
- {
- hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr);
- hspi->pTxBuffPtr += sizeof(uint16_t);
- hspi->TxXferCount--;
- /* Next Data is a reception (Rx). Tx not allowed */
- txallowed = 0U;
- #if (USE_SPI_CRC != 0U)
- /* Enable CRC Transmission */
- if ((hspi->TxXferCount == 0U) && (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE))
- {
- SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
- }
- #endif /* USE_SPI_CRC */
- }
- /* Check RXNE flag */
- if ((__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_RXNE)) && (hspi->RxXferCount > 0U))
- {
- *((uint16_t *)hspi->pRxBuffPtr) = (uint16_t)hspi->Instance->DR;
- hspi->pRxBuffPtr += sizeof(uint16_t);
- hspi->RxXferCount--;
- /* Next Data is a Transmission (Tx). Tx is allowed */
- txallowed = 1U;
- }
- if (((HAL_GetTick() - tickstart) >= Timeout) && (Timeout != HAL_MAX_DELAY))
- {
- errorcode = HAL_TIMEOUT;
- hspi->State = HAL_SPI_STATE_READY;
- goto error;
- }
- }
- }
- /* Transmit and Receive data in 8 Bit mode */
- else
- {
- if ((hspi->Init.Mode == SPI_MODE_SLAVE) || (initial_TxXferCount == 0x01U))
- {
- *((__IO uint8_t *)&hspi->Instance->DR) = (*hspi->pTxBuffPtr);
- hspi->pTxBuffPtr += sizeof(uint8_t);
- hspi->TxXferCount--;
- }
- while ((hspi->TxXferCount > 0U) || (hspi->RxXferCount > 0U))
- {
- /* Check TXE flag */
- if ((__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_TXE)) && (hspi->TxXferCount > 0U) && (txallowed == 1U))
- {
- *(__IO uint8_t *)&hspi->Instance->DR = (*hspi->pTxBuffPtr);
- hspi->pTxBuffPtr++;
- hspi->TxXferCount--;
- /* Next Data is a reception (Rx). Tx not allowed */
- txallowed = 0U;
- #if (USE_SPI_CRC != 0U)
- /* Enable CRC Transmission */
- if ((hspi->TxXferCount == 0U) && (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE))
- {
- SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
- }
- #endif /* USE_SPI_CRC */
- }
- /* Wait until RXNE flag is reset */
- if ((__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_RXNE)) && (hspi->RxXferCount > 0U))
- {
- (*(uint8_t *)hspi->pRxBuffPtr) = hspi->Instance->DR;
- hspi->pRxBuffPtr++;
- hspi->RxXferCount--;
- /* Next Data is a Transmission (Tx). Tx is allowed */
- txallowed = 1U;
- }
- if ((((HAL_GetTick() - tickstart) >= Timeout) && ((Timeout != HAL_MAX_DELAY))) || (Timeout == 0U))
- {
- errorcode = HAL_TIMEOUT;
- hspi->State = HAL_SPI_STATE_READY;
- goto error;
- }
- }
- }
- #if (USE_SPI_CRC != 0U)
- /* Read CRC from DR to close CRC calculation process */
- if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
- {
- /* Wait until TXE flag */
- if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SET, Timeout, tickstart) != HAL_OK)
- {
- /* Error on the CRC reception */
- SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
- errorcode = HAL_TIMEOUT;
- goto error;
- }
- /* Read CRC */
- tmpreg = READ_REG(hspi->Instance->DR);
- /* To avoid GCC warning */
- UNUSED(tmpreg);
- }
- /* Check if CRC error occurred */
- if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET)
- {
- /* Check if CRC error is valid or not (workaround to be applied or not) */
- if (SPI_ISCRCErrorValid(hspi) == SPI_VALID_CRC_ERROR)
- {
- SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
- /* Reset CRC Calculation */
- SPI_RESET_CRC(hspi);
- errorcode = HAL_ERROR;
- }
- else
- {
- __HAL_SPI_CLEAR_CRCERRFLAG(hspi);
- }
- }
- #endif /* USE_SPI_CRC */
- /* Check the end of the transaction */
- if (SPI_EndRxTxTransaction(hspi, Timeout, tickstart) != HAL_OK)
- {
- errorcode = HAL_ERROR;
- hspi->ErrorCode = HAL_SPI_ERROR_FLAG;
- goto error;
- }
- /* Clear overrun flag in 2 Lines communication mode because received is not read */
- if (hspi->Init.Direction == SPI_DIRECTION_2LINES)
- {
- __HAL_SPI_CLEAR_OVRFLAG(hspi);
- }
- if (hspi->ErrorCode != HAL_SPI_ERROR_NONE)
- {
- errorcode = HAL_ERROR;
- }
- else
- {
- hspi->State = HAL_SPI_STATE_READY;
- }
-
- error :
- __HAL_UNLOCK(hspi);
- return errorcode;
- }
- /**
- * @brief Transmit an amount of data in non-blocking mode with Interrupt.
- * @param hspi pointer to a SPI_HandleTypeDef structure that contains
- * the configuration information for SPI module.
- * @param pData pointer to data buffer
- * @param Size amount of data to be sent
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_SPI_Transmit_IT(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size)
- {
- HAL_StatusTypeDef errorcode = HAL_OK;
- /* Check Direction parameter */
- assert_param(IS_SPI_DIRECTION_2LINES_OR_1LINE(hspi->Init.Direction));
- /* Process Locked */
- __HAL_LOCK(hspi);
- if ((pData == NULL) || (Size == 0U))
- {
- errorcode = HAL_ERROR;
- goto error;
- }
- if (hspi->State != HAL_SPI_STATE_READY)
- {
- errorcode = HAL_BUSY;
- goto error;
- }
- /* Set the transaction information */
- hspi->State = HAL_SPI_STATE_BUSY_TX;
- hspi->ErrorCode = HAL_SPI_ERROR_NONE;
- hspi->pTxBuffPtr = (uint8_t *)pData;
- hspi->TxXferSize = Size;
- hspi->TxXferCount = Size;
- /* Init field not used in handle to zero */
- hspi->pRxBuffPtr = (uint8_t *)NULL;
- hspi->RxXferSize = 0U;
- hspi->RxXferCount = 0U;
- hspi->RxISR = NULL;
- /* Set the function for IT treatment */
- if (hspi->Init.DataSize > SPI_DATASIZE_8BIT)
- {
- hspi->TxISR = SPI_TxISR_16BIT;
- }
- else
- {
- hspi->TxISR = SPI_TxISR_8BIT;
- }
- /* Configure communication direction : 1Line */
- if (hspi->Init.Direction == SPI_DIRECTION_1LINE)
- {
- /* Disable SPI Peripheral before set 1Line direction (BIDIOE bit) */
- __HAL_SPI_DISABLE(hspi);
- SPI_1LINE_TX(hspi);
- }
- #if (USE_SPI_CRC != 0U)
- /* Reset CRC Calculation */
- if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
- {
- SPI_RESET_CRC(hspi);
- }
- #endif /* USE_SPI_CRC */
- /* Enable TXE and ERR interrupt */
- __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_TXE | SPI_IT_ERR));
- /* Check if the SPI is already enabled */
- if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
- {
- /* Enable SPI peripheral */
- __HAL_SPI_ENABLE(hspi);
- }
- error :
- __HAL_UNLOCK(hspi);
- return errorcode;
- }
- /**
- * @brief Receive an amount of data in non-blocking mode with Interrupt.
- * @param hspi pointer to a SPI_HandleTypeDef structure that contains
- * the configuration information for SPI module.
- * @param pData pointer to data buffer
- * @param Size amount of data to be sent
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_SPI_Receive_IT(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size)
- {
- HAL_StatusTypeDef errorcode = HAL_OK;
- if (hspi->State != HAL_SPI_STATE_READY)
- {
- errorcode = HAL_BUSY;
- goto error;
- }
- if ((hspi->Init.Direction == SPI_DIRECTION_2LINES) && (hspi->Init.Mode == SPI_MODE_MASTER))
- {
- hspi->State = HAL_SPI_STATE_BUSY_RX;
- /* Call transmit-receive function to send Dummy data on Tx line and generate clock on CLK line */
- return HAL_SPI_TransmitReceive_IT(hspi, pData, pData, Size);
- }
- /* Process Locked */
- __HAL_LOCK(hspi);
- if ((pData == NULL) || (Size == 0U))
- {
- errorcode = HAL_ERROR;
- goto error;
- }
- /* Set the transaction information */
- hspi->State = HAL_SPI_STATE_BUSY_RX;
- hspi->ErrorCode = HAL_SPI_ERROR_NONE;
- hspi->pRxBuffPtr = (uint8_t *)pData;
- hspi->RxXferSize = Size;
- hspi->RxXferCount = Size;
- /* Init field not used in handle to zero */
- hspi->pTxBuffPtr = (uint8_t *)NULL;
- hspi->TxXferSize = 0U;
- hspi->TxXferCount = 0U;
- hspi->TxISR = NULL;
- /* Set the function for IT treatment */
- if (hspi->Init.DataSize > SPI_DATASIZE_8BIT)
- {
- hspi->RxISR = SPI_RxISR_16BIT;
- }
- else
- {
- hspi->RxISR = SPI_RxISR_8BIT;
- }
- /* Configure communication direction : 1Line */
- if (hspi->Init.Direction == SPI_DIRECTION_1LINE)
- {
- /* Disable SPI Peripheral before set 1Line direction (BIDIOE bit) */
- __HAL_SPI_DISABLE(hspi);
- SPI_1LINE_RX(hspi);
- }
- #if (USE_SPI_CRC != 0U)
- /* Reset CRC Calculation */
- if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
- {
- SPI_RESET_CRC(hspi);
- }
- #endif /* USE_SPI_CRC */
- /* Enable TXE and ERR interrupt */
- __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_RXNE | SPI_IT_ERR));
- /* Note : The SPI must be enabled after unlocking current process
- to avoid the risk of SPI interrupt handle execution before current
- process unlock */
- /* Check if the SPI is already enabled */
- if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
- {
- /* Enable SPI peripheral */
- __HAL_SPI_ENABLE(hspi);
- }
- error :
- /* Process Unlocked */
- __HAL_UNLOCK(hspi);
- return errorcode;
- }
- /**
- * @brief Transmit and Receive an amount of data in non-blocking mode with Interrupt.
- * @param hspi pointer to a SPI_HandleTypeDef structure that contains
- * the configuration information for SPI module.
- * @param pTxData pointer to transmission data buffer
- * @param pRxData pointer to reception data buffer
- * @param Size amount of data to be sent and received
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_SPI_TransmitReceive_IT(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size)
- {
- uint32_t tmp_mode;
- HAL_SPI_StateTypeDef tmp_state;
- HAL_StatusTypeDef errorcode = HAL_OK;
- /* Check Direction parameter */
- assert_param(IS_SPI_DIRECTION_2LINES(hspi->Init.Direction));
- /* Process locked */
- __HAL_LOCK(hspi);
- /* Init temporary variables */
- tmp_state = hspi->State;
- tmp_mode = hspi->Init.Mode;
- if (!((tmp_state == HAL_SPI_STATE_READY) || \
- ((tmp_mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES) && (tmp_state == HAL_SPI_STATE_BUSY_RX))))
- {
- errorcode = HAL_BUSY;
- goto error;
- }
- if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0U))
- {
- errorcode = HAL_ERROR;
- goto error;
- }
- /* Don't overwrite in case of HAL_SPI_STATE_BUSY_RX */
- if (hspi->State != HAL_SPI_STATE_BUSY_RX)
- {
- hspi->State = HAL_SPI_STATE_BUSY_TX_RX;
- }
- /* Set the transaction information */
- hspi->ErrorCode = HAL_SPI_ERROR_NONE;
- hspi->pTxBuffPtr = (uint8_t *)pTxData;
- hspi->TxXferSize = Size;
- hspi->TxXferCount = Size;
- hspi->pRxBuffPtr = (uint8_t *)pRxData;
- hspi->RxXferSize = Size;
- hspi->RxXferCount = Size;
- /* Set the function for IT treatment */
- if (hspi->Init.DataSize > SPI_DATASIZE_8BIT)
- {
- hspi->RxISR = SPI_2linesRxISR_16BIT;
- hspi->TxISR = SPI_2linesTxISR_16BIT;
- }
- else
- {
- hspi->RxISR = SPI_2linesRxISR_8BIT;
- hspi->TxISR = SPI_2linesTxISR_8BIT;
- }
- #if (USE_SPI_CRC != 0U)
- /* Reset CRC Calculation */
- if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
- {
- SPI_RESET_CRC(hspi);
- }
- #endif /* USE_SPI_CRC */
- /* Enable TXE, RXNE and ERR interrupt */
- __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_TXE | SPI_IT_RXNE | SPI_IT_ERR));
- /* Check if the SPI is already enabled */
- if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
- {
- /* Enable SPI peripheral */
- __HAL_SPI_ENABLE(hspi);
- }
- error :
- /* Process Unlocked */
- __HAL_UNLOCK(hspi);
- return errorcode;
- }
- /**
- * @brief Transmit an amount of data in non-blocking mode with DMA.
- * @param hspi pointer to a SPI_HandleTypeDef structure that contains
- * the configuration information for SPI module.
- * @param pData pointer to data buffer
- * @param Size amount of data to be sent
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_SPI_Transmit_DMA(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size)
- {
- HAL_StatusTypeDef errorcode = HAL_OK;
- /* Check tx dma handle */
- assert_param(IS_SPI_DMA_HANDLE(hspi->hdmatx));
- /* Check Direction parameter */
- assert_param(IS_SPI_DIRECTION_2LINES_OR_1LINE(hspi->Init.Direction));
- /* Process Locked */
- __HAL_LOCK(hspi);
- if (hspi->State != HAL_SPI_STATE_READY)
- {
- errorcode = HAL_BUSY;
- goto error;
- }
- if ((pData == NULL) || (Size == 0U))
- {
- errorcode = HAL_ERROR;
- goto error;
- }
- /* Set the transaction information */
- hspi->State = HAL_SPI_STATE_BUSY_TX;
- hspi->ErrorCode = HAL_SPI_ERROR_NONE;
- hspi->pTxBuffPtr = (uint8_t *)pData;
- hspi->TxXferSize = Size;
- hspi->TxXferCount = Size;
- /* Init field not used in handle to zero */
- hspi->pRxBuffPtr = (uint8_t *)NULL;
- hspi->TxISR = NULL;
- hspi->RxISR = NULL;
- hspi->RxXferSize = 0U;
- hspi->RxXferCount = 0U;
- /* Configure communication direction : 1Line */
- if (hspi->Init.Direction == SPI_DIRECTION_1LINE)
- {
- /* Disable SPI Peripheral before set 1Line direction (BIDIOE bit) */
- __HAL_SPI_DISABLE(hspi);
- SPI_1LINE_TX(hspi);
- }
- #if (USE_SPI_CRC != 0U)
- /* Reset CRC Calculation */
- if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
- {
- SPI_RESET_CRC(hspi);
- }
- #endif /* USE_SPI_CRC */
- /* Set the SPI TxDMA Half transfer complete callback */
- hspi->hdmatx->XferHalfCpltCallback = SPI_DMAHalfTransmitCplt;
- /* Set the SPI TxDMA transfer complete callback */
- hspi->hdmatx->XferCpltCallback = SPI_DMATransmitCplt;
- /* Set the DMA error callback */
- hspi->hdmatx->XferErrorCallback = SPI_DMAError;
- /* Set the DMA AbortCpltCallback */
- hspi->hdmatx->XferAbortCallback = NULL;
- /* Enable the Tx DMA Stream/Channel */
- if (HAL_OK != HAL_DMA_Start_IT(hspi->hdmatx, (uint32_t)hspi->pTxBuffPtr, (uint32_t)&hspi->Instance->DR,
- hspi->TxXferCount))
- {
- /* Update SPI error code */
- SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_DMA);
- errorcode = HAL_ERROR;
- goto error;
- }
- /* Check if the SPI is already enabled */
- if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
- {
- /* Enable SPI peripheral */
- __HAL_SPI_ENABLE(hspi);
- }
- /* Enable the SPI Error Interrupt Bit */
- __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_ERR));
- /* Enable Tx DMA Request */
- SET_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN);
- error :
- /* Process Unlocked */
- __HAL_UNLOCK(hspi);
- return errorcode;
- }
- /**
- * @brief Receive an amount of data in non-blocking mode with DMA.
- * @note In case of MASTER mode and SPI_DIRECTION_2LINES direction, hdmatx shall be defined.
- * @param hspi pointer to a SPI_HandleTypeDef structure that contains
- * the configuration information for SPI module.
- * @param pData pointer to data buffer
- * @note When the CRC feature is enabled the pData Length must be Size + 1.
- * @param Size amount of data to be sent
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_SPI_Receive_DMA(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size)
- {
- HAL_StatusTypeDef errorcode = HAL_OK;
- /* Check rx dma handle */
- assert_param(IS_SPI_DMA_HANDLE(hspi->hdmarx));
-
- if (hspi->State != HAL_SPI_STATE_READY)
- {
- errorcode = HAL_BUSY;
- goto error;
- }
-
- if ((hspi->Init.Direction == SPI_DIRECTION_2LINES) && (hspi->Init.Mode == SPI_MODE_MASTER))
- {
- hspi->State = HAL_SPI_STATE_BUSY_RX;
- /* Check tx dma handle */
- assert_param(IS_SPI_DMA_HANDLE(hspi->hdmatx));
- /* Call transmit-receive function to send Dummy data on Tx line and generate clock on CLK line */
- return HAL_SPI_TransmitReceive_DMA(hspi, pData, pData, Size);
- }
- /* Process Locked */
- __HAL_LOCK(hspi);
- if ((pData == NULL) || (Size == 0U))
- {
- errorcode = HAL_ERROR;
- goto error;
- }
- /* Set the transaction information */
- hspi->State = HAL_SPI_STATE_BUSY_RX;
- hspi->ErrorCode = HAL_SPI_ERROR_NONE;
- hspi->pRxBuffPtr = (uint8_t *)pData;
- hspi->RxXferSize = Size;
- hspi->RxXferCount = Size;
- /*Init field not used in handle to zero */
- hspi->RxISR = NULL;
- hspi->TxISR = NULL;
- hspi->TxXferSize = 0U;
- hspi->TxXferCount = 0U;
- /* Configure communication direction : 1Line */
- if (hspi->Init.Direction == SPI_DIRECTION_1LINE)
- {
- /* Disable SPI Peripheral before set 1Line direction (BIDIOE bit) */
- __HAL_SPI_DISABLE(hspi);
- SPI_1LINE_RX(hspi);
- }
- #if (USE_SPI_CRC != 0U)
- /* Reset CRC Calculation */
- if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
- {
- SPI_RESET_CRC(hspi);
- }
- #endif /* USE_SPI_CRC */
- /* Set the SPI RxDMA Half transfer complete callback */
- hspi->hdmarx->XferHalfCpltCallback = SPI_DMAHalfReceiveCplt;
- /* Set the SPI Rx DMA transfer complete callback */
- hspi->hdmarx->XferCpltCallback = SPI_DMAReceiveCplt;
- /* Set the DMA error callback */
- hspi->hdmarx->XferErrorCallback = SPI_DMAError;
- /* Set the DMA AbortCpltCallback */
- hspi->hdmarx->XferAbortCallback = NULL;
- /* Enable the Rx DMA Stream/Channel */
- if (HAL_OK != HAL_DMA_Start_IT(hspi->hdmarx, (uint32_t)&hspi->Instance->DR, (uint32_t)hspi->pRxBuffPtr,
- hspi->RxXferCount))
- {
- /* Update SPI error code */
- SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_DMA);
- errorcode = HAL_ERROR;
- goto error;
- }
- /* Check if the SPI is already enabled */
- if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
- {
- /* Enable SPI peripheral */
- __HAL_SPI_ENABLE(hspi);
- }
- /* Enable the SPI Error Interrupt Bit */
- __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_ERR));
- /* Enable Rx DMA Request */
- SET_BIT(hspi->Instance->CR2, SPI_CR2_RXDMAEN);
- error:
- /* Process Unlocked */
- __HAL_UNLOCK(hspi);
- return errorcode;
- }
- /**
- * @brief Transmit and Receive an amount of data in non-blocking mode with DMA.
- * @param hspi pointer to a SPI_HandleTypeDef structure that contains
- * the configuration information for SPI module.
- * @param pTxData pointer to transmission data buffer
- * @param pRxData pointer to reception data buffer
- * @note When the CRC feature is enabled the pRxData Length must be Size + 1
- * @param Size amount of data to be sent
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_SPI_TransmitReceive_DMA(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData,
- uint16_t Size)
- {
- uint32_t tmp_mode;
- HAL_SPI_StateTypeDef tmp_state;
- HAL_StatusTypeDef errorcode = HAL_OK;
- /* Check rx & tx dma handles */
- assert_param(IS_SPI_DMA_HANDLE(hspi->hdmarx));
- assert_param(IS_SPI_DMA_HANDLE(hspi->hdmatx));
- /* Check Direction parameter */
- assert_param(IS_SPI_DIRECTION_2LINES(hspi->Init.Direction));
- /* Process locked */
- __HAL_LOCK(hspi);
- /* Init temporary variables */
- tmp_state = hspi->State;
- tmp_mode = hspi->Init.Mode;
- if (!((tmp_state == HAL_SPI_STATE_READY) ||
- ((tmp_mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES) && (tmp_state == HAL_SPI_STATE_BUSY_RX))))
- {
- errorcode = HAL_BUSY;
- goto error;
- }
- if ((pTxData == NULL) || (pRxData == NULL) || (Size == 0U))
- {
- errorcode = HAL_ERROR;
- goto error;
- }
- /* Don't overwrite in case of HAL_SPI_STATE_BUSY_RX */
- if (hspi->State != HAL_SPI_STATE_BUSY_RX)
- {
- hspi->State = HAL_SPI_STATE_BUSY_TX_RX;
- }
- /* Set the transaction information */
- hspi->ErrorCode = HAL_SPI_ERROR_NONE;
- hspi->pTxBuffPtr = (uint8_t *)pTxData;
- hspi->TxXferSize = Size;
- hspi->TxXferCount = Size;
- hspi->pRxBuffPtr = (uint8_t *)pRxData;
- hspi->RxXferSize = Size;
- hspi->RxXferCount = Size;
- /* Init field not used in handle to zero */
- hspi->RxISR = NULL;
- hspi->TxISR = NULL;
- #if (USE_SPI_CRC != 0U)
- /* Reset CRC Calculation */
- if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
- {
- SPI_RESET_CRC(hspi);
- }
- #endif /* USE_SPI_CRC */
- /* Check if we are in Rx only or in Rx/Tx Mode and configure the DMA transfer complete callback */
- if (hspi->State == HAL_SPI_STATE_BUSY_RX)
- {
- /* Set the SPI Rx DMA Half transfer complete callback */
- hspi->hdmarx->XferHalfCpltCallback = SPI_DMAHalfReceiveCplt;
- hspi->hdmarx->XferCpltCallback = SPI_DMAReceiveCplt;
- }
- else
- {
- /* Set the SPI Tx/Rx DMA Half transfer complete callback */
- hspi->hdmarx->XferHalfCpltCallback = SPI_DMAHalfTransmitReceiveCplt;
- hspi->hdmarx->XferCpltCallback = SPI_DMATransmitReceiveCplt;
- }
- /* Set the DMA error callback */
- hspi->hdmarx->XferErrorCallback = SPI_DMAError;
- /* Set the DMA AbortCpltCallback */
- hspi->hdmarx->XferAbortCallback = NULL;
- /* Enable the Rx DMA Stream/Channel */
- if (HAL_OK != HAL_DMA_Start_IT(hspi->hdmarx, (uint32_t)&hspi->Instance->DR, (uint32_t)hspi->pRxBuffPtr,
- hspi->RxXferCount))
- {
- /* Update SPI error code */
- SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_DMA);
- errorcode = HAL_ERROR;
- goto error;
- }
- /* Enable Rx DMA Request */
- SET_BIT(hspi->Instance->CR2, SPI_CR2_RXDMAEN);
- /* Set the SPI Tx DMA transfer complete callback as NULL because the communication closing
- is performed in DMA reception complete callback */
- hspi->hdmatx->XferHalfCpltCallback = NULL;
- hspi->hdmatx->XferCpltCallback = NULL;
- hspi->hdmatx->XferErrorCallback = NULL;
- hspi->hdmatx->XferAbortCallback = NULL;
- /* Enable the Tx DMA Stream/Channel */
- if (HAL_OK != HAL_DMA_Start_IT(hspi->hdmatx, (uint32_t)hspi->pTxBuffPtr, (uint32_t)&hspi->Instance->DR,
- hspi->TxXferCount))
- {
- /* Update SPI error code */
- SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_DMA);
- errorcode = HAL_ERROR;
- goto error;
- }
- /* Check if the SPI is already enabled */
- if ((hspi->Instance->CR1 & SPI_CR1_SPE) != SPI_CR1_SPE)
- {
- /* Enable SPI peripheral */
- __HAL_SPI_ENABLE(hspi);
- }
- /* Enable the SPI Error Interrupt Bit */
- __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_ERR));
- /* Enable Tx DMA Request */
- SET_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN);
- error :
- /* Process Unlocked */
- __HAL_UNLOCK(hspi);
- return errorcode;
- }
- /**
- * @brief Abort ongoing transfer (blocking mode).
- * @param hspi SPI handle.
- * @note This procedure could be used for aborting any ongoing transfer (Tx and Rx),
- * started in Interrupt or DMA mode.
- * This procedure performs following operations :
- * - Disable SPI Interrupts (depending of transfer direction)
- * - Disable the DMA transfer in the peripheral register (if enabled)
- * - Abort DMA transfer by calling HAL_DMA_Abort (in case of transfer in DMA mode)
- * - Set handle State to READY
- * @note This procedure is executed in blocking mode : when exiting function, Abort is considered as completed.
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_SPI_Abort(SPI_HandleTypeDef *hspi)
- {
- HAL_StatusTypeDef errorcode;
- __IO uint32_t count;
- __IO uint32_t resetcount;
- /* Initialized local variable */
- errorcode = HAL_OK;
- resetcount = SPI_DEFAULT_TIMEOUT * (SystemCoreClock / 24U / 1000U);
- count = resetcount;
- /* Clear ERRIE interrupt to avoid error interrupts generation during Abort procedure */
- CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_ERRIE);
- /* Disable TXEIE, RXNEIE and ERRIE(mode fault event, overrun error, TI frame error) interrupts */
- if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_TXEIE))
- {
- hspi->TxISR = SPI_AbortTx_ISR;
- /* Wait HAL_SPI_STATE_ABORT state */
- do
- {
- if (count == 0U)
- {
- SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
- break;
- }
- count--;
- } while (hspi->State != HAL_SPI_STATE_ABORT);
- /* Reset Timeout Counter */
- count = resetcount;
- }
- if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_RXNEIE))
- {
- hspi->RxISR = SPI_AbortRx_ISR;
- /* Wait HAL_SPI_STATE_ABORT state */
- do
- {
- if (count == 0U)
- {
- SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
- break;
- }
- count--;
- } while (hspi->State != HAL_SPI_STATE_ABORT);
- /* Reset Timeout Counter */
- count = resetcount;
- }
- /* Disable the SPI DMA Tx request if enabled */
- if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_TXDMAEN))
- {
- /* Abort the SPI DMA Tx Stream/Channel : use blocking DMA Abort API (no callback) */
- if (hspi->hdmatx != NULL)
- {
- /* Set the SPI DMA Abort callback :
- will lead to call HAL_SPI_AbortCpltCallback() at end of DMA abort procedure */
- hspi->hdmatx->XferAbortCallback = NULL;
- /* Abort DMA Tx Handle linked to SPI Peripheral */
- if (HAL_DMA_Abort(hspi->hdmatx) != HAL_OK)
- {
- hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
- }
- /* Disable Tx DMA Request */
- CLEAR_BIT(hspi->Instance->CR2, (SPI_CR2_TXDMAEN));
- /* Wait until TXE flag is set */
- do
- {
- if (count == 0U)
- {
- SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
- break;
- }
- count--;
- } while ((hspi->Instance->SR & SPI_FLAG_TXE) == RESET);
- }
- }
- /* Disable the SPI DMA Rx request if enabled */
- if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_RXDMAEN))
- {
- /* Abort the SPI DMA Rx Stream/Channel : use blocking DMA Abort API (no callback) */
- if (hspi->hdmarx != NULL)
- {
- /* Set the SPI DMA Abort callback :
- will lead to call HAL_SPI_AbortCpltCallback() at end of DMA abort procedure */
- hspi->hdmarx->XferAbortCallback = NULL;
- /* Abort DMA Rx Handle linked to SPI Peripheral */
- if (HAL_DMA_Abort(hspi->hdmarx) != HAL_OK)
- {
- hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
- }
- /* Disable peripheral */
- __HAL_SPI_DISABLE(hspi);
- /* Disable Rx DMA Request */
- CLEAR_BIT(hspi->Instance->CR2, (SPI_CR2_RXDMAEN));
- }
- }
- /* Reset Tx and Rx transfer counters */
- hspi->RxXferCount = 0U;
- hspi->TxXferCount = 0U;
- /* Check error during Abort procedure */
- if (hspi->ErrorCode == HAL_SPI_ERROR_ABORT)
- {
- /* return HAL_Error in case of error during Abort procedure */
- errorcode = HAL_ERROR;
- }
- else
- {
- /* Reset errorCode */
- hspi->ErrorCode = HAL_SPI_ERROR_NONE;
- }
- /* Clear the Error flags in the SR register */
- __HAL_SPI_CLEAR_OVRFLAG(hspi);
- /* Restore hspi->state to ready */
- hspi->State = HAL_SPI_STATE_READY;
- return errorcode;
- }
- /**
- * @brief Abort ongoing transfer (Interrupt mode).
- * @param hspi SPI handle.
- * @note This procedure could be used for aborting any ongoing transfer (Tx and Rx),
- * started in Interrupt or DMA mode.
- * This procedure performs following operations :
- * - Disable SPI Interrupts (depending of transfer direction)
- * - Disable the DMA transfer in the peripheral register (if enabled)
- * - Abort DMA transfer by calling HAL_DMA_Abort_IT (in case of transfer in DMA mode)
- * - Set handle State to READY
- * - At abort completion, call user abort complete callback
- * @note This procedure is executed in Interrupt mode, meaning that abort procedure could be
- * considered as completed only when user abort complete callback is executed (not when exiting function).
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_SPI_Abort_IT(SPI_HandleTypeDef *hspi)
- {
- HAL_StatusTypeDef errorcode;
- uint32_t abortcplt ;
- __IO uint32_t count;
- __IO uint32_t resetcount;
- /* Initialized local variable */
- errorcode = HAL_OK;
- abortcplt = 1U;
- resetcount = SPI_DEFAULT_TIMEOUT * (SystemCoreClock / 24U / 1000U);
- count = resetcount;
- /* Clear ERRIE interrupt to avoid error interrupts generation during Abort procedure */
- CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_ERRIE);
- /* Change Rx and Tx Irq Handler to Disable TXEIE, RXNEIE and ERRIE interrupts */
- if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_TXEIE))
- {
- hspi->TxISR = SPI_AbortTx_ISR;
- /* Wait HAL_SPI_STATE_ABORT state */
- do
- {
- if (count == 0U)
- {
- SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
- break;
- }
- count--;
- } while (hspi->State != HAL_SPI_STATE_ABORT);
- /* Reset Timeout Counter */
- count = resetcount;
- }
- if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_RXNEIE))
- {
- hspi->RxISR = SPI_AbortRx_ISR;
- /* Wait HAL_SPI_STATE_ABORT state */
- do
- {
- if (count == 0U)
- {
- SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
- break;
- }
- count--;
- } while (hspi->State != HAL_SPI_STATE_ABORT);
- /* Reset Timeout Counter */
- count = resetcount;
- }
- /* If DMA Tx and/or DMA Rx Handles are associated to SPI Handle, DMA Abort complete callbacks should be initialised
- before any call to DMA Abort functions */
- /* DMA Tx Handle is valid */
- if (hspi->hdmatx != NULL)
- {
- /* Set DMA Abort Complete callback if UART DMA Tx request if enabled.
- Otherwise, set it to NULL */
- if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_TXDMAEN))
- {
- hspi->hdmatx->XferAbortCallback = SPI_DMATxAbortCallback;
- }
- else
- {
- hspi->hdmatx->XferAbortCallback = NULL;
- }
- }
- /* DMA Rx Handle is valid */
- if (hspi->hdmarx != NULL)
- {
- /* Set DMA Abort Complete callback if UART DMA Rx request if enabled.
- Otherwise, set it to NULL */
- if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_RXDMAEN))
- {
- hspi->hdmarx->XferAbortCallback = SPI_DMARxAbortCallback;
- }
- else
- {
- hspi->hdmarx->XferAbortCallback = NULL;
- }
- }
- /* Disable the SPI DMA Tx request if enabled */
- if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_TXDMAEN))
- {
- /* Abort the SPI DMA Tx Stream/Channel */
- if (hspi->hdmatx != NULL)
- {
- /* Abort DMA Tx Handle linked to SPI Peripheral */
- if (HAL_DMA_Abort_IT(hspi->hdmatx) != HAL_OK)
- {
- hspi->hdmatx->XferAbortCallback = NULL;
- hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
- }
- else
- {
- abortcplt = 0U;
- }
- }
- }
- /* Disable the SPI DMA Rx request if enabled */
- if (HAL_IS_BIT_SET(hspi->Instance->CR2, SPI_CR2_RXDMAEN))
- {
- /* Abort the SPI DMA Rx Stream/Channel */
- if (hspi->hdmarx != NULL)
- {
- /* Abort DMA Rx Handle linked to SPI Peripheral */
- if (HAL_DMA_Abort_IT(hspi->hdmarx) != HAL_OK)
- {
- hspi->hdmarx->XferAbortCallback = NULL;
- hspi->ErrorCode = HAL_SPI_ERROR_ABORT;
- }
- else
- {
- abortcplt = 0U;
- }
- }
- }
- if (abortcplt == 1U)
- {
- /* Reset Tx and Rx transfer counters */
- hspi->RxXferCount = 0U;
- hspi->TxXferCount = 0U;
- /* Check error during Abort procedure */
- if (hspi->ErrorCode == HAL_SPI_ERROR_ABORT)
- {
- /* return HAL_Error in case of error during Abort procedure */
- errorcode = HAL_ERROR;
- }
- else
- {
- /* Reset errorCode */
- hspi->ErrorCode = HAL_SPI_ERROR_NONE;
- }
- /* Clear the Error flags in the SR register */
- __HAL_SPI_CLEAR_OVRFLAG(hspi);
- /* Restore hspi->State to Ready */
- hspi->State = HAL_SPI_STATE_READY;
- /* As no DMA to be aborted, call directly user Abort complete callback */
- #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
- hspi->AbortCpltCallback(hspi);
- #else
- HAL_SPI_AbortCpltCallback(hspi);
- #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
- }
- return errorcode;
- }
- /**
- * @brief Pause the DMA Transfer.
- * @param hspi pointer to a SPI_HandleTypeDef structure that contains
- * the configuration information for the specified SPI module.
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_SPI_DMAPause(SPI_HandleTypeDef *hspi)
- {
- /* Process Locked */
- __HAL_LOCK(hspi);
- /* Disable the SPI DMA Tx & Rx requests */
- CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN);
- /* Process Unlocked */
- __HAL_UNLOCK(hspi);
- return HAL_OK;
- }
- /**
- * @brief Resume the DMA Transfer.
- * @param hspi pointer to a SPI_HandleTypeDef structure that contains
- * the configuration information for the specified SPI module.
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_SPI_DMAResume(SPI_HandleTypeDef *hspi)
- {
- /* Process Locked */
- __HAL_LOCK(hspi);
- /* Enable the SPI DMA Tx & Rx requests */
- SET_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN);
- /* Process Unlocked */
- __HAL_UNLOCK(hspi);
- return HAL_OK;
- }
- /**
- * @brief Stop the DMA Transfer.
- * @param hspi pointer to a SPI_HandleTypeDef structure that contains
- * the configuration information for the specified SPI module.
- * @retval HAL status
- */
- HAL_StatusTypeDef HAL_SPI_DMAStop(SPI_HandleTypeDef *hspi)
- {
- HAL_StatusTypeDef errorcode = HAL_OK;
- /* The Lock is not implemented on this API to allow the user application
- to call the HAL SPI API under callbacks HAL_SPI_TxCpltCallback() or HAL_SPI_RxCpltCallback() or HAL_SPI_TxRxCpltCallback():
- when calling HAL_DMA_Abort() API the DMA TX/RX Transfer complete interrupt is generated
- and the correspond call back is executed HAL_SPI_TxCpltCallback() or HAL_SPI_RxCpltCallback() or HAL_SPI_TxRxCpltCallback()
- */
- /* Abort the SPI DMA tx Stream/Channel */
- if (hspi->hdmatx != NULL)
- {
- if (HAL_OK != HAL_DMA_Abort(hspi->hdmatx))
- {
- SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_DMA);
- errorcode = HAL_ERROR;
- }
- }
- /* Abort the SPI DMA rx Stream/Channel */
- if (hspi->hdmarx != NULL)
- {
- if (HAL_OK != HAL_DMA_Abort(hspi->hdmarx))
- {
- SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_DMA);
- errorcode = HAL_ERROR;
- }
- }
- /* Disable the SPI DMA Tx & Rx requests */
- CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN);
- hspi->State = HAL_SPI_STATE_READY;
- return errorcode;
- }
- /**
- * @brief Handle SPI interrupt request.
- * @param hspi pointer to a SPI_HandleTypeDef structure that contains
- * the configuration information for the specified SPI module.
- * @retval None
- */
- void HAL_SPI_IRQHandler(SPI_HandleTypeDef *hspi)
- {
- uint32_t itsource = hspi->Instance->CR2;
- uint32_t itflag = hspi->Instance->SR;
- /* SPI in mode Receiver ----------------------------------------------------*/
- if ((SPI_CHECK_FLAG(itflag, SPI_FLAG_OVR) == RESET) &&
- (SPI_CHECK_FLAG(itflag, SPI_FLAG_RXNE) != RESET) && (SPI_CHECK_IT_SOURCE(itsource, SPI_IT_RXNE) != RESET))
- {
- hspi->RxISR(hspi);
- return;
- }
- /* SPI in mode Transmitter -------------------------------------------------*/
- if ((SPI_CHECK_FLAG(itflag, SPI_FLAG_TXE) != RESET) && (SPI_CHECK_IT_SOURCE(itsource, SPI_IT_TXE) != RESET))
- {
- hspi->TxISR(hspi);
- return;
- }
- /* SPI in Error Treatment --------------------------------------------------*/
- if (((SPI_CHECK_FLAG(itflag, SPI_FLAG_MODF) != RESET) || (SPI_CHECK_FLAG(itflag, SPI_FLAG_OVR) != RESET))
- && (SPI_CHECK_IT_SOURCE(itsource, SPI_IT_ERR) != RESET))
- {
- /* SPI Overrun error interrupt occurred ----------------------------------*/
- if (SPI_CHECK_FLAG(itflag, SPI_FLAG_OVR) != RESET)
- {
- if (hspi->State != HAL_SPI_STATE_BUSY_TX)
- {
- SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_OVR);
- __HAL_SPI_CLEAR_OVRFLAG(hspi);
- }
- else
- {
- __HAL_SPI_CLEAR_OVRFLAG(hspi);
- return;
- }
- }
- /* SPI Mode Fault error interrupt occurred -------------------------------*/
- if (SPI_CHECK_FLAG(itflag, SPI_FLAG_MODF) != RESET)
- {
- SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_MODF);
- __HAL_SPI_CLEAR_MODFFLAG(hspi);
- }
- /* SPI Frame error interrupt occurred ------------------------------------*/
- if (hspi->ErrorCode != HAL_SPI_ERROR_NONE)
- {
- /* Disable all interrupts */
- __HAL_SPI_DISABLE_IT(hspi, SPI_IT_RXNE | SPI_IT_TXE | SPI_IT_ERR);
- hspi->State = HAL_SPI_STATE_READY;
- /* Disable the SPI DMA requests if enabled */
- if ((HAL_IS_BIT_SET(itsource, SPI_CR2_TXDMAEN)) || (HAL_IS_BIT_SET(itsource, SPI_CR2_RXDMAEN)))
- {
- CLEAR_BIT(hspi->Instance->CR2, (SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN));
- /* Abort the SPI DMA Rx channel */
- if (hspi->hdmarx != NULL)
- {
- /* Set the SPI DMA Abort callback :
- will lead to call HAL_SPI_ErrorCallback() at end of DMA abort procedure */
- hspi->hdmarx->XferAbortCallback = SPI_DMAAbortOnError;
- if (HAL_OK != HAL_DMA_Abort_IT(hspi->hdmarx))
- {
- SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
- }
- }
- /* Abort the SPI DMA Tx channel */
- if (hspi->hdmatx != NULL)
- {
- /* Set the SPI DMA Abort callback :
- will lead to call HAL_SPI_ErrorCallback() at end of DMA abort procedure */
- hspi->hdmatx->XferAbortCallback = SPI_DMAAbortOnError;
- if (HAL_OK != HAL_DMA_Abort_IT(hspi->hdmatx))
- {
- SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
- }
- }
- }
- else
- {
- /* Call user error callback */
- #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
- hspi->ErrorCallback(hspi);
- #else
- HAL_SPI_ErrorCallback(hspi);
- #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
- }
- }
- return;
- }
- }
- /**
- * @brief Tx Transfer completed callback.
- * @param hspi pointer to a SPI_HandleTypeDef structure that contains
- * the configuration information for SPI module.
- * @retval None
- */
- __weak void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(hspi);
- /* NOTE : This function should not be modified, when the callback is needed,
- the HAL_SPI_TxCpltCallback should be implemented in the user file
- */
- }
- /**
- * @brief Rx Transfer completed callback.
- * @param hspi pointer to a SPI_HandleTypeDef structure that contains
- * the configuration information for SPI module.
- * @retval None
- */
- __weak void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(hspi);
- /* NOTE : This function should not be modified, when the callback is needed,
- the HAL_SPI_RxCpltCallback should be implemented in the user file
- */
- }
- /**
- * @brief Tx and Rx Transfer completed callback.
- * @param hspi pointer to a SPI_HandleTypeDef structure that contains
- * the configuration information for SPI module.
- * @retval None
- */
- __weak void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(hspi);
- /* NOTE : This function should not be modified, when the callback is needed,
- the HAL_SPI_TxRxCpltCallback should be implemented in the user file
- */
- }
- /**
- * @brief Tx Half Transfer completed callback.
- * @param hspi pointer to a SPI_HandleTypeDef structure that contains
- * the configuration information for SPI module.
- * @retval None
- */
- __weak void HAL_SPI_TxHalfCpltCallback(SPI_HandleTypeDef *hspi)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(hspi);
- /* NOTE : This function should not be modified, when the callback is needed,
- the HAL_SPI_TxHalfCpltCallback should be implemented in the user file
- */
- }
- /**
- * @brief Rx Half Transfer completed callback.
- * @param hspi pointer to a SPI_HandleTypeDef structure that contains
- * the configuration information for SPI module.
- * @retval None
- */
- __weak void HAL_SPI_RxHalfCpltCallback(SPI_HandleTypeDef *hspi)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(hspi);
- /* NOTE : This function should not be modified, when the callback is needed,
- the HAL_SPI_RxHalfCpltCallback() should be implemented in the user file
- */
- }
- /**
- * @brief Tx and Rx Half Transfer callback.
- * @param hspi pointer to a SPI_HandleTypeDef structure that contains
- * the configuration information for SPI module.
- * @retval None
- */
- __weak void HAL_SPI_TxRxHalfCpltCallback(SPI_HandleTypeDef *hspi)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(hspi);
- /* NOTE : This function should not be modified, when the callback is needed,
- the HAL_SPI_TxRxHalfCpltCallback() should be implemented in the user file
- */
- }
- /**
- * @brief SPI error callback.
- * @param hspi pointer to a SPI_HandleTypeDef structure that contains
- * the configuration information for SPI module.
- * @retval None
- */
- __weak void HAL_SPI_ErrorCallback(SPI_HandleTypeDef *hspi)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(hspi);
- /* NOTE : This function should not be modified, when the callback is needed,
- the HAL_SPI_ErrorCallback should be implemented in the user file
- */
- /* NOTE : The ErrorCode parameter in the hspi handle is updated by the SPI processes
- and user can use HAL_SPI_GetError() API to check the latest error occurred
- */
- }
- /**
- * @brief SPI Abort Complete callback.
- * @param hspi SPI handle.
- * @retval None
- */
- __weak void HAL_SPI_AbortCpltCallback(SPI_HandleTypeDef *hspi)
- {
- /* Prevent unused argument(s) compilation warning */
- UNUSED(hspi);
- /* NOTE : This function should not be modified, when the callback is needed,
- the HAL_SPI_AbortCpltCallback can be implemented in the user file.
- */
- }
- /**
- * @}
- */
- /** @defgroup SPI_Exported_Functions_Group3 Peripheral State and Errors functions
- * @brief SPI control functions
- *
- @verbatim
- ===============================================================================
- ##### Peripheral State and Errors functions #####
- ===============================================================================
- [..]
- This subsection provides a set of functions allowing to control the SPI.
- (+) HAL_SPI_GetState() API can be helpful to check in run-time the state of the SPI peripheral
- (+) HAL_SPI_GetError() check in run-time Errors occurring during communication
- @endverbatim
- * @{
- */
- /**
- * @brief Return the SPI handle state.
- * @param hspi pointer to a SPI_HandleTypeDef structure that contains
- * the configuration information for SPI module.
- * @retval SPI state
- */
- HAL_SPI_StateTypeDef HAL_SPI_GetState(SPI_HandleTypeDef *hspi)
- {
- /* Return SPI handle state */
- return hspi->State;
- }
- /**
- * @brief Return the SPI error code.
- * @param hspi pointer to a SPI_HandleTypeDef structure that contains
- * the configuration information for SPI module.
- * @retval SPI error code in bitmap format
- */
- uint32_t HAL_SPI_GetError(SPI_HandleTypeDef *hspi)
- {
- /* Return SPI ErrorCode */
- return hspi->ErrorCode;
- }
- /**
- * @}
- */
- /**
- * @}
- */
- /** @addtogroup SPI_Private_Functions
- * @brief Private functions
- * @{
- */
- /**
- * @brief DMA SPI transmit process complete callback.
- * @param hdma pointer to a DMA_HandleTypeDef structure that contains
- * the configuration information for the specified DMA module.
- * @retval None
- */
- static void SPI_DMATransmitCplt(DMA_HandleTypeDef *hdma)
- {
- SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
- uint32_t tickstart;
- /* Init tickstart for timeout management*/
- tickstart = HAL_GetTick();
- /* DMA Normal Mode */
- if ((hdma->Instance->CCR & DMA_CCR_CIRC) != DMA_CCR_CIRC)
- {
- /* Disable ERR interrupt */
- __HAL_SPI_DISABLE_IT(hspi, SPI_IT_ERR);
- /* Disable Tx DMA Request */
- CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN);
- /* Check the end of the transaction */
- if (SPI_EndRxTxTransaction(hspi, SPI_DEFAULT_TIMEOUT, tickstart) != HAL_OK)
- {
- SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
- }
- /* Clear overrun flag in 2 Lines communication mode because received data is not read */
- if (hspi->Init.Direction == SPI_DIRECTION_2LINES)
- {
- __HAL_SPI_CLEAR_OVRFLAG(hspi);
- }
- hspi->TxXferCount = 0U;
- hspi->State = HAL_SPI_STATE_READY;
- if (hspi->ErrorCode != HAL_SPI_ERROR_NONE)
- {
- /* Call user error callback */
- #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
- hspi->ErrorCallback(hspi);
- #else
- HAL_SPI_ErrorCallback(hspi);
- #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
- return;
- }
- }
- /* Call user Tx complete callback */
- #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
- hspi->TxCpltCallback(hspi);
- #else
- HAL_SPI_TxCpltCallback(hspi);
- #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
- }
- /**
- * @brief DMA SPI receive process complete callback.
- * @param hdma pointer to a DMA_HandleTypeDef structure that contains
- * the configuration information for the specified DMA module.
- * @retval None
- */
- static void SPI_DMAReceiveCplt(DMA_HandleTypeDef *hdma)
- {
- SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
- uint32_t tickstart;
- #if (USE_SPI_CRC != 0U)
- __IO uint32_t tmpreg = 0U;
- #endif /* USE_SPI_CRC */
- /* Init tickstart for timeout management*/
- tickstart = HAL_GetTick();
- /* DMA Normal Mode */
- if ((hdma->Instance->CCR & DMA_CCR_CIRC) != DMA_CCR_CIRC)
- {
- /* Disable ERR interrupt */
- __HAL_SPI_DISABLE_IT(hspi, SPI_IT_ERR);
- #if (USE_SPI_CRC != 0U)
- /* CRC handling */
- if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
- {
- /* Wait until RXNE flag */
- if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SET, SPI_DEFAULT_TIMEOUT, tickstart) != HAL_OK)
- {
- /* Error on the CRC reception */
- SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
- }
- /* Read CRC */
- tmpreg = READ_REG(hspi->Instance->DR);
- /* To avoid GCC warning */
- UNUSED(tmpreg);
- }
- #endif /* USE_SPI_CRC */
- /* Check if we are in Master RX 2 line mode */
- if ((hspi->Init.Direction == SPI_DIRECTION_2LINES) && (hspi->Init.Mode == SPI_MODE_MASTER))
- {
- /* Disable Rx/Tx DMA Request (done by default to handle the case master rx direction 2 lines) */
- CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN);
- }
- else
- {
- /* Normal case */
- CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_RXDMAEN);
- }
- /* Check the end of the transaction */
- if (SPI_EndRxTransaction(hspi, SPI_DEFAULT_TIMEOUT, tickstart) != HAL_OK)
- {
- hspi->ErrorCode = HAL_SPI_ERROR_FLAG;
- }
- hspi->RxXferCount = 0U;
- hspi->State = HAL_SPI_STATE_READY;
- #if (USE_SPI_CRC != 0U)
- /* Check if CRC error occurred */
- if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET)
- {
- /* Check if CRC error is valid or not (workaround to be applied or not) */
- if (SPI_ISCRCErrorValid(hspi) == SPI_VALID_CRC_ERROR)
- {
- SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
- /* Reset CRC Calculation */
- SPI_RESET_CRC(hspi);
- }
- else
- {
- __HAL_SPI_CLEAR_CRCERRFLAG(hspi);
- }
- }
- #endif /* USE_SPI_CRC */
- if (hspi->ErrorCode != HAL_SPI_ERROR_NONE)
- {
- /* Call user error callback */
- #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
- hspi->ErrorCallback(hspi);
- #else
- HAL_SPI_ErrorCallback(hspi);
- #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
- return;
- }
- }
- /* Call user Rx complete callback */
- #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
- hspi->RxCpltCallback(hspi);
- #else
- HAL_SPI_RxCpltCallback(hspi);
- #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
- }
- /**
- * @brief DMA SPI transmit receive process complete callback.
- * @param hdma pointer to a DMA_HandleTypeDef structure that contains
- * the configuration information for the specified DMA module.
- * @retval None
- */
- static void SPI_DMATransmitReceiveCplt(DMA_HandleTypeDef *hdma)
- {
- SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
- uint32_t tickstart;
- #if (USE_SPI_CRC != 0U)
- __IO uint32_t tmpreg = 0U;
- #endif /* USE_SPI_CRC */
- /* Init tickstart for timeout management*/
- tickstart = HAL_GetTick();
- /* DMA Normal Mode */
- if ((hdma->Instance->CCR & DMA_CCR_CIRC) != DMA_CCR_CIRC)
- {
- /* Disable ERR interrupt */
- __HAL_SPI_DISABLE_IT(hspi, SPI_IT_ERR);
- #if (USE_SPI_CRC != 0U)
- /* CRC handling */
- if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
- {
- /* Wait the CRC data */
- if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, SET, SPI_DEFAULT_TIMEOUT, tickstart) != HAL_OK)
- {
- SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
- }
- /* Read CRC to Flush DR and RXNE flag */
- tmpreg = READ_REG(hspi->Instance->DR);
- /* To avoid GCC warning */
- UNUSED(tmpreg);
- }
- #endif /* USE_SPI_CRC */
- /* Check the end of the transaction */
- if (SPI_EndRxTxTransaction(hspi, SPI_DEFAULT_TIMEOUT, tickstart) != HAL_OK)
- {
- SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
- }
- /* Disable Rx/Tx DMA Request */
- CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN);
- hspi->TxXferCount = 0U;
- hspi->RxXferCount = 0U;
- hspi->State = HAL_SPI_STATE_READY;
- #if (USE_SPI_CRC != 0U)
- /* Check if CRC error occurred */
- if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET)
- {
- /* Check if CRC error is valid or not (workaround to be applied or not) */
- if (SPI_ISCRCErrorValid(hspi) == SPI_VALID_CRC_ERROR)
- {
- SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
- /* Reset CRC Calculation */
- SPI_RESET_CRC(hspi);
- }
- else
- {
- __HAL_SPI_CLEAR_CRCERRFLAG(hspi);
- }
- }
- #endif /* USE_SPI_CRC */
- if (hspi->ErrorCode != HAL_SPI_ERROR_NONE)
- {
- /* Call user error callback */
- #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
- hspi->ErrorCallback(hspi);
- #else
- HAL_SPI_ErrorCallback(hspi);
- #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
- return;
- }
- }
- /* Call user TxRx complete callback */
- #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
- hspi->TxRxCpltCallback(hspi);
- #else
- HAL_SPI_TxRxCpltCallback(hspi);
- #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
- }
- /**
- * @brief DMA SPI half transmit process complete callback.
- * @param hdma pointer to a DMA_HandleTypeDef structure that contains
- * the configuration information for the specified DMA module.
- * @retval None
- */
- static void SPI_DMAHalfTransmitCplt(DMA_HandleTypeDef *hdma)
- {
- SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
- /* Call user Tx half complete callback */
- #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
- hspi->TxHalfCpltCallback(hspi);
- #else
- HAL_SPI_TxHalfCpltCallback(hspi);
- #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
- }
- /**
- * @brief DMA SPI half receive process complete callback
- * @param hdma pointer to a DMA_HandleTypeDef structure that contains
- * the configuration information for the specified DMA module.
- * @retval None
- */
- static void SPI_DMAHalfReceiveCplt(DMA_HandleTypeDef *hdma)
- {
- SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
- /* Call user Rx half complete callback */
- #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
- hspi->RxHalfCpltCallback(hspi);
- #else
- HAL_SPI_RxHalfCpltCallback(hspi);
- #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
- }
- /**
- * @brief DMA SPI half transmit receive process complete callback.
- * @param hdma pointer to a DMA_HandleTypeDef structure that contains
- * the configuration information for the specified DMA module.
- * @retval None
- */
- static void SPI_DMAHalfTransmitReceiveCplt(DMA_HandleTypeDef *hdma)
- {
- SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
- /* Call user TxRx half complete callback */
- #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
- hspi->TxRxHalfCpltCallback(hspi);
- #else
- HAL_SPI_TxRxHalfCpltCallback(hspi);
- #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
- }
- /**
- * @brief DMA SPI communication error callback.
- * @param hdma pointer to a DMA_HandleTypeDef structure that contains
- * the configuration information for the specified DMA module.
- * @retval None
- */
- static void SPI_DMAError(DMA_HandleTypeDef *hdma)
- {
- SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
- /* Stop the disable DMA transfer on SPI side */
- CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN | SPI_CR2_RXDMAEN);
- SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_DMA);
- hspi->State = HAL_SPI_STATE_READY;
- /* Call user error callback */
- #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
- hspi->ErrorCallback(hspi);
- #else
- HAL_SPI_ErrorCallback(hspi);
- #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
- }
- /**
- * @brief DMA SPI communication abort callback, when initiated by HAL services on Error
- * (To be called at end of DMA Abort procedure following error occurrence).
- * @param hdma DMA handle.
- * @retval None
- */
- static void SPI_DMAAbortOnError(DMA_HandleTypeDef *hdma)
- {
- SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
- hspi->RxXferCount = 0U;
- hspi->TxXferCount = 0U;
- /* Call user error callback */
- #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
- hspi->ErrorCallback(hspi);
- #else
- HAL_SPI_ErrorCallback(hspi);
- #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
- }
- /**
- * @brief DMA SPI Tx communication abort callback, when initiated by user
- * (To be called at end of DMA Tx Abort procedure following user abort request).
- * @note When this callback is executed, User Abort complete call back is called only if no
- * Abort still ongoing for Rx DMA Handle.
- * @param hdma DMA handle.
- * @retval None
- */
- static void SPI_DMATxAbortCallback(DMA_HandleTypeDef *hdma)
- {
- SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
- __IO uint32_t count;
- hspi->hdmatx->XferAbortCallback = NULL;
- count = SPI_DEFAULT_TIMEOUT * (SystemCoreClock / 24U / 1000U);
- /* Disable Tx DMA Request */
- CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_TXDMAEN);
- /* Wait until TXE flag is set */
- do
- {
- if (count == 0U)
- {
- SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
- break;
- }
- count--;
- } while ((hspi->Instance->SR & SPI_FLAG_TXE) == RESET);
- /* Check if an Abort process is still ongoing */
- if (hspi->hdmarx != NULL)
- {
- if (hspi->hdmarx->XferAbortCallback != NULL)
- {
- return;
- }
- }
- /* No Abort process still ongoing : All DMA Stream/Channel are aborted, call user Abort Complete callback */
- hspi->RxXferCount = 0U;
- hspi->TxXferCount = 0U;
- /* Check no error during Abort procedure */
- if (hspi->ErrorCode != HAL_SPI_ERROR_ABORT)
- {
- /* Reset errorCode */
- hspi->ErrorCode = HAL_SPI_ERROR_NONE;
- }
- /* Clear the Error flags in the SR register */
- __HAL_SPI_CLEAR_OVRFLAG(hspi);
- /* Restore hspi->State to Ready */
- hspi->State = HAL_SPI_STATE_READY;
- /* Call user Abort complete callback */
- #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
- hspi->AbortCpltCallback(hspi);
- #else
- HAL_SPI_AbortCpltCallback(hspi);
- #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
- }
- /**
- * @brief DMA SPI Rx communication abort callback, when initiated by user
- * (To be called at end of DMA Rx Abort procedure following user abort request).
- * @note When this callback is executed, User Abort complete call back is called only if no
- * Abort still ongoing for Tx DMA Handle.
- * @param hdma DMA handle.
- * @retval None
- */
- static void SPI_DMARxAbortCallback(DMA_HandleTypeDef *hdma)
- {
- SPI_HandleTypeDef *hspi = (SPI_HandleTypeDef *)(((DMA_HandleTypeDef *)hdma)->Parent); /* Derogation MISRAC2012-Rule-11.5 */
- /* Disable SPI Peripheral */
- __HAL_SPI_DISABLE(hspi);
- hspi->hdmarx->XferAbortCallback = NULL;
- /* Disable Rx DMA Request */
- CLEAR_BIT(hspi->Instance->CR2, SPI_CR2_RXDMAEN);
- /* Check Busy flag */
- if (SPI_EndRxTxTransaction(hspi, SPI_DEFAULT_TIMEOUT, HAL_GetTick()) != HAL_OK)
- {
- SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
- }
- /* Check if an Abort process is still ongoing */
- if (hspi->hdmatx != NULL)
- {
- if (hspi->hdmatx->XferAbortCallback != NULL)
- {
- return;
- }
- }
- /* No Abort process still ongoing : All DMA Stream/Channel are aborted, call user Abort Complete callback */
- hspi->RxXferCount = 0U;
- hspi->TxXferCount = 0U;
- /* Check no error during Abort procedure */
- if (hspi->ErrorCode != HAL_SPI_ERROR_ABORT)
- {
- /* Reset errorCode */
- hspi->ErrorCode = HAL_SPI_ERROR_NONE;
- }
- /* Clear the Error flags in the SR register */
- __HAL_SPI_CLEAR_OVRFLAG(hspi);
- /* Restore hspi->State to Ready */
- hspi->State = HAL_SPI_STATE_READY;
- /* Call user Abort complete callback */
- #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
- hspi->AbortCpltCallback(hspi);
- #else
- HAL_SPI_AbortCpltCallback(hspi);
- #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
- }
- /**
- * @brief Rx 8-bit handler for Transmit and Receive in Interrupt mode.
- * @param hspi pointer to a SPI_HandleTypeDef structure that contains
- * the configuration information for SPI module.
- * @retval None
- */
- static void SPI_2linesRxISR_8BIT(struct __SPI_HandleTypeDef *hspi)
- {
- /* Receive data in 8bit mode */
- *hspi->pRxBuffPtr = *((__IO uint8_t *)&hspi->Instance->DR);
- hspi->pRxBuffPtr++;
- hspi->RxXferCount--;
- /* Check end of the reception */
- if (hspi->RxXferCount == 0U)
- {
- #if (USE_SPI_CRC != 0U)
- if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
- {
- hspi->RxISR = SPI_2linesRxISR_8BITCRC;
- return;
- }
- #endif /* USE_SPI_CRC */
- /* Disable RXNE and ERR interrupt */
- __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_RXNE | SPI_IT_ERR));
- if (hspi->TxXferCount == 0U)
- {
- SPI_CloseRxTx_ISR(hspi);
- }
- }
- }
- #if (USE_SPI_CRC != 0U)
- /**
- * @brief Rx 8-bit handler for Transmit and Receive in Interrupt mode.
- * @param hspi pointer to a SPI_HandleTypeDef structure that contains
- * the configuration information for SPI module.
- * @retval None
- */
- static void SPI_2linesRxISR_8BITCRC(struct __SPI_HandleTypeDef *hspi)
- {
- __IO uint8_t *ptmpreg8;
- __IO uint8_t tmpreg8 = 0;
- /* Initialize the 8bit temporary pointer */
- ptmpreg8 = (__IO uint8_t *)&hspi->Instance->DR;
- /* Read 8bit CRC to flush Data Register */
- tmpreg8 = *ptmpreg8;
- /* To avoid GCC warning */
- UNUSED(tmpreg8);
- /* Disable RXNE and ERR interrupt */
- __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_RXNE | SPI_IT_ERR));
- if (hspi->TxXferCount == 0U)
- {
- SPI_CloseRxTx_ISR(hspi);
- }
- }
- #endif /* USE_SPI_CRC */
- /**
- * @brief Tx 8-bit handler for Transmit and Receive in Interrupt mode.
- * @param hspi pointer to a SPI_HandleTypeDef structure that contains
- * the configuration information for SPI module.
- * @retval None
- */
- static void SPI_2linesTxISR_8BIT(struct __SPI_HandleTypeDef *hspi)
- {
- *(__IO uint8_t *)&hspi->Instance->DR = (*hspi->pTxBuffPtr);
- hspi->pTxBuffPtr++;
- hspi->TxXferCount--;
- /* Check the end of the transmission */
- if (hspi->TxXferCount == 0U)
- {
- #if (USE_SPI_CRC != 0U)
- if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
- {
- /* Set CRC Next Bit to send CRC */
- SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
- /* Disable TXE interrupt */
- __HAL_SPI_DISABLE_IT(hspi, SPI_IT_TXE);
- return;
- }
- #endif /* USE_SPI_CRC */
- /* Disable TXE interrupt */
- __HAL_SPI_DISABLE_IT(hspi, SPI_IT_TXE);
- if (hspi->RxXferCount == 0U)
- {
- SPI_CloseRxTx_ISR(hspi);
- }
- }
- }
- /**
- * @brief Rx 16-bit handler for Transmit and Receive in Interrupt mode.
- * @param hspi pointer to a SPI_HandleTypeDef structure that contains
- * the configuration information for SPI module.
- * @retval None
- */
- static void SPI_2linesRxISR_16BIT(struct __SPI_HandleTypeDef *hspi)
- {
- /* Receive data in 16 Bit mode */
- *((uint16_t *)hspi->pRxBuffPtr) = (uint16_t)(hspi->Instance->DR);
- hspi->pRxBuffPtr += sizeof(uint16_t);
- hspi->RxXferCount--;
- if (hspi->RxXferCount == 0U)
- {
- #if (USE_SPI_CRC != 0U)
- if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
- {
- hspi->RxISR = SPI_2linesRxISR_16BITCRC;
- return;
- }
- #endif /* USE_SPI_CRC */
- /* Disable RXNE interrupt */
- __HAL_SPI_DISABLE_IT(hspi, SPI_IT_RXNE);
- if (hspi->TxXferCount == 0U)
- {
- SPI_CloseRxTx_ISR(hspi);
- }
- }
- }
- #if (USE_SPI_CRC != 0U)
- /**
- * @brief Manage the CRC 16-bit receive for Transmit and Receive in Interrupt mode.
- * @param hspi pointer to a SPI_HandleTypeDef structure that contains
- * the configuration information for SPI module.
- * @retval None
- */
- static void SPI_2linesRxISR_16BITCRC(struct __SPI_HandleTypeDef *hspi)
- {
- __IO uint32_t tmpreg = 0U;
- /* Read 16bit CRC to flush Data Register */
- tmpreg = READ_REG(hspi->Instance->DR);
- /* To avoid GCC warning */
- UNUSED(tmpreg);
- /* Disable RXNE interrupt */
- __HAL_SPI_DISABLE_IT(hspi, SPI_IT_RXNE);
- SPI_CloseRxTx_ISR(hspi);
- }
- #endif /* USE_SPI_CRC */
- /**
- * @brief Tx 16-bit handler for Transmit and Receive in Interrupt mode.
- * @param hspi pointer to a SPI_HandleTypeDef structure that contains
- * the configuration information for SPI module.
- * @retval None
- */
- static void SPI_2linesTxISR_16BIT(struct __SPI_HandleTypeDef *hspi)
- {
- /* Transmit data in 16 Bit mode */
- hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr);
- hspi->pTxBuffPtr += sizeof(uint16_t);
- hspi->TxXferCount--;
- /* Enable CRC Transmission */
- if (hspi->TxXferCount == 0U)
- {
- #if (USE_SPI_CRC != 0U)
- if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
- {
- /* Set CRC Next Bit to send CRC */
- SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
- /* Disable TXE interrupt */
- __HAL_SPI_DISABLE_IT(hspi, SPI_IT_TXE);
- return;
- }
- #endif /* USE_SPI_CRC */
- /* Disable TXE interrupt */
- __HAL_SPI_DISABLE_IT(hspi, SPI_IT_TXE);
- if (hspi->RxXferCount == 0U)
- {
- SPI_CloseRxTx_ISR(hspi);
- }
- }
- }
- #if (USE_SPI_CRC != 0U)
- /**
- * @brief Manage the CRC 8-bit receive in Interrupt context.
- * @param hspi pointer to a SPI_HandleTypeDef structure that contains
- * the configuration information for SPI module.
- * @retval None
- */
- static void SPI_RxISR_8BITCRC(struct __SPI_HandleTypeDef *hspi)
- {
- __IO uint8_t *ptmpreg8;
- __IO uint8_t tmpreg8 = 0;
- /* Initialize the 8bit temporary pointer */
- ptmpreg8 = (__IO uint8_t *)&hspi->Instance->DR;
- /* Read 8bit CRC to flush Data Register */
- tmpreg8 = *ptmpreg8;
- /* To avoid GCC warning */
- UNUSED(tmpreg8);
- SPI_CloseRx_ISR(hspi);
- }
- #endif /* USE_SPI_CRC */
- /**
- * @brief Manage the receive 8-bit in Interrupt context.
- * @param hspi pointer to a SPI_HandleTypeDef structure that contains
- * the configuration information for SPI module.
- * @retval None
- */
- static void SPI_RxISR_8BIT(struct __SPI_HandleTypeDef *hspi)
- {
- *hspi->pRxBuffPtr = (*(__IO uint8_t *)&hspi->Instance->DR);
- hspi->pRxBuffPtr++;
- hspi->RxXferCount--;
- #if (USE_SPI_CRC != 0U)
- /* Enable CRC Transmission */
- if ((hspi->RxXferCount == 1U) && (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE))
- {
- SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
- }
- /* Check if CRCNEXT is well reset by hardware */
- if (READ_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT))
- {
- /* Workaround to force CRCNEXT bit to zero in case of CRCNEXT is not reset automatically by hardware */
- CLEAR_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
- }
- #endif /* USE_SPI_CRC */
- if (hspi->RxXferCount == 0U)
- {
- #if (USE_SPI_CRC != 0U)
- if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
- {
- hspi->RxISR = SPI_RxISR_8BITCRC;
- return;
- }
- #endif /* USE_SPI_CRC */
- SPI_CloseRx_ISR(hspi);
- }
- }
- #if (USE_SPI_CRC != 0U)
- /**
- * @brief Manage the CRC 16-bit receive in Interrupt context.
- * @param hspi pointer to a SPI_HandleTypeDef structure that contains
- * the configuration information for SPI module.
- * @retval None
- */
- static void SPI_RxISR_16BITCRC(struct __SPI_HandleTypeDef *hspi)
- {
- __IO uint32_t tmpreg = 0U;
- /* Read 16bit CRC to flush Data Register */
- tmpreg = READ_REG(hspi->Instance->DR);
- /* To avoid GCC warning */
- UNUSED(tmpreg);
- /* Disable RXNE and ERR interrupt */
- __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_RXNE | SPI_IT_ERR));
- SPI_CloseRx_ISR(hspi);
- }
- #endif /* USE_SPI_CRC */
- /**
- * @brief Manage the 16-bit receive in Interrupt context.
- * @param hspi pointer to a SPI_HandleTypeDef structure that contains
- * the configuration information for SPI module.
- * @retval None
- */
- static void SPI_RxISR_16BIT(struct __SPI_HandleTypeDef *hspi)
- {
- *((uint16_t *)hspi->pRxBuffPtr) = (uint16_t)(hspi->Instance->DR);
- hspi->pRxBuffPtr += sizeof(uint16_t);
- hspi->RxXferCount--;
- #if (USE_SPI_CRC != 0U)
- /* Enable CRC Transmission */
- if ((hspi->RxXferCount == 1U) && (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE))
- {
- SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
- }
- /* Check if CRCNEXT is well reset by hardware */
- if (READ_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT))
- {
- /* Workaround to force CRCNEXT bit to zero in case of CRCNEXT is not reset automatically by hardware */
- CLEAR_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
- }
- #endif /* USE_SPI_CRC */
- if (hspi->RxXferCount == 0U)
- {
- #if (USE_SPI_CRC != 0U)
- if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
- {
- hspi->RxISR = SPI_RxISR_16BITCRC;
- return;
- }
- #endif /* USE_SPI_CRC */
- SPI_CloseRx_ISR(hspi);
- }
- }
- /**
- * @brief Handle the data 8-bit transmit in Interrupt mode.
- * @param hspi pointer to a SPI_HandleTypeDef structure that contains
- * the configuration information for SPI module.
- * @retval None
- */
- static void SPI_TxISR_8BIT(struct __SPI_HandleTypeDef *hspi)
- {
- *(__IO uint8_t *)&hspi->Instance->DR = (*hspi->pTxBuffPtr);
- hspi->pTxBuffPtr++;
- hspi->TxXferCount--;
- if (hspi->TxXferCount == 0U)
- {
- #if (USE_SPI_CRC != 0U)
- if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
- {
- /* Enable CRC Transmission */
- SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
- }
- #endif /* USE_SPI_CRC */
- SPI_CloseTx_ISR(hspi);
- }
- }
- /**
- * @brief Handle the data 16-bit transmit in Interrupt mode.
- * @param hspi pointer to a SPI_HandleTypeDef structure that contains
- * the configuration information for SPI module.
- * @retval None
- */
- static void SPI_TxISR_16BIT(struct __SPI_HandleTypeDef *hspi)
- {
- /* Transmit data in 16 Bit mode */
- hspi->Instance->DR = *((uint16_t *)hspi->pTxBuffPtr);
- hspi->pTxBuffPtr += sizeof(uint16_t);
- hspi->TxXferCount--;
- if (hspi->TxXferCount == 0U)
- {
- #if (USE_SPI_CRC != 0U)
- if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
- {
- /* Enable CRC Transmission */
- SET_BIT(hspi->Instance->CR1, SPI_CR1_CRCNEXT);
- }
- #endif /* USE_SPI_CRC */
- SPI_CloseTx_ISR(hspi);
- }
- }
- /**
- * @brief Handle SPI Communication Timeout.
- * @param hspi pointer to a SPI_HandleTypeDef structure that contains
- * the configuration information for SPI module.
- * @param Flag SPI flag to check
- * @param State flag state to check
- * @param Timeout Timeout duration
- * @param Tickstart tick start value
- * @retval HAL status
- */
- static HAL_StatusTypeDef SPI_WaitFlagStateUntilTimeout(SPI_HandleTypeDef *hspi, uint32_t Flag, FlagStatus State,
- uint32_t Timeout, uint32_t Tickstart)
- {
- __IO uint32_t count;
- uint32_t tmp_timeout;
- uint32_t tmp_tickstart;
- /* Adjust Timeout value in case of end of transfer */
- tmp_timeout = Timeout - (HAL_GetTick() - Tickstart);
- tmp_tickstart = HAL_GetTick();
- /* Calculate Timeout based on a software loop to avoid blocking issue if Systick is disabled */
- count = tmp_timeout * ((SystemCoreClock * 32U) >> 20U);
- while ((__HAL_SPI_GET_FLAG(hspi, Flag) ? SET : RESET) != State)
- {
- if (Timeout != HAL_MAX_DELAY)
- {
- if (((HAL_GetTick() - tmp_tickstart) >= tmp_timeout) || (tmp_timeout == 0U))
- {
- /* Disable the SPI and reset the CRC: the CRC value should be cleared
- on both master and slave sides in order to resynchronize the master
- and slave for their respective CRC calculation */
- /* Disable TXE, RXNE and ERR interrupts for the interrupt process */
- __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_TXE | SPI_IT_RXNE | SPI_IT_ERR));
- if ((hspi->Init.Mode == SPI_MODE_MASTER) && ((hspi->Init.Direction == SPI_DIRECTION_1LINE)
- || (hspi->Init.Direction == SPI_DIRECTION_2LINES_RXONLY)))
- {
- /* Disable SPI peripheral */
- __HAL_SPI_DISABLE(hspi);
- }
- /* Reset CRC Calculation */
- if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLE)
- {
- SPI_RESET_CRC(hspi);
- }
- hspi->State = HAL_SPI_STATE_READY;
- /* Process Unlocked */
- __HAL_UNLOCK(hspi);
- return HAL_TIMEOUT;
- }
- /* If Systick is disabled or not incremented, deactivate timeout to go in disable loop procedure */
- if (count == 0U)
- {
- tmp_timeout = 0U;
- }
- count--;
- }
- }
- return HAL_OK;
- }
- /**
- * @brief Handle the check of the RX transaction complete.
- * @param hspi pointer to a SPI_HandleTypeDef structure that contains
- * the configuration information for SPI module.
- * @param Timeout Timeout duration
- * @param Tickstart tick start value
- * @retval HAL status
- */
- static HAL_StatusTypeDef SPI_EndRxTransaction(SPI_HandleTypeDef *hspi, uint32_t Timeout, uint32_t Tickstart)
- {
- if ((hspi->Init.Mode == SPI_MODE_MASTER) && ((hspi->Init.Direction == SPI_DIRECTION_1LINE)
- || (hspi->Init.Direction == SPI_DIRECTION_2LINES_RXONLY)))
- {
- /* Disable SPI peripheral */
- __HAL_SPI_DISABLE(hspi);
- }
- if ((hspi->Init.Mode == SPI_MODE_MASTER) && (hspi->Init.Direction == SPI_DIRECTION_2LINES_RXONLY))
- {
- /* Wait the RXNE reset */
- if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_RXNE, RESET, Timeout, Tickstart) != HAL_OK)
- {
- SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
- return HAL_TIMEOUT;
- }
- }
- else
- {
- /* Control the BSY flag */
- if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_BSY, RESET, Timeout, Tickstart) != HAL_OK)
- {
- SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
- return HAL_TIMEOUT;
- }
- }
- return HAL_OK;
- }
- /**
- * @brief Handle the check of the RXTX or TX transaction complete.
- * @param hspi SPI handle
- * @param Timeout Timeout duration
- * @param Tickstart tick start value
- * @retval HAL status
- */
- static HAL_StatusTypeDef SPI_EndRxTxTransaction(SPI_HandleTypeDef *hspi, uint32_t Timeout, uint32_t Tickstart)
- {
- /* Control the BSY flag */
- if (SPI_WaitFlagStateUntilTimeout(hspi, SPI_FLAG_BSY, RESET, Timeout, Tickstart) != HAL_OK)
- {
- SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
- return HAL_TIMEOUT;
- }
- return HAL_OK;
- }
- /**
- * @brief Handle the end of the RXTX transaction.
- * @param hspi pointer to a SPI_HandleTypeDef structure that contains
- * the configuration information for SPI module.
- * @retval None
- */
- static void SPI_CloseRxTx_ISR(SPI_HandleTypeDef *hspi)
- {
- uint32_t tickstart;
- __IO uint32_t count = SPI_DEFAULT_TIMEOUT * (SystemCoreClock / 24U / 1000U);
- /* Init tickstart for timeout management */
- tickstart = HAL_GetTick();
- /* Disable ERR interrupt */
- __HAL_SPI_DISABLE_IT(hspi, SPI_IT_ERR);
- /* Wait until TXE flag is set */
- do
- {
- if (count == 0U)
- {
- SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
- break;
- }
- count--;
- } while ((hspi->Instance->SR & SPI_FLAG_TXE) == RESET);
- /* Check the end of the transaction */
- if (SPI_EndRxTxTransaction(hspi, SPI_DEFAULT_TIMEOUT, tickstart) != HAL_OK)
- {
- SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
- }
- /* Clear overrun flag in 2 Lines communication mode because received is not read */
- if (hspi->Init.Direction == SPI_DIRECTION_2LINES)
- {
- __HAL_SPI_CLEAR_OVRFLAG(hspi);
- }
- #if (USE_SPI_CRC != 0U)
- /* Check if CRC error occurred */
- if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET)
- {
- /* Check if CRC error is valid or not (workaround to be applied or not) */
- if (SPI_ISCRCErrorValid(hspi) == SPI_VALID_CRC_ERROR)
- {
- hspi->State = HAL_SPI_STATE_READY;
- SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
- /* Reset CRC Calculation */
- SPI_RESET_CRC(hspi);
- /* Call user error callback */
- #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
- hspi->ErrorCallback(hspi);
- #else
- HAL_SPI_ErrorCallback(hspi);
- #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
- }
- else
- {
- __HAL_SPI_CLEAR_CRCERRFLAG(hspi);
- }
- }
- else
- {
- #endif /* USE_SPI_CRC */
- if (hspi->ErrorCode == HAL_SPI_ERROR_NONE)
- {
- if (hspi->State == HAL_SPI_STATE_BUSY_RX)
- {
- hspi->State = HAL_SPI_STATE_READY;
- /* Call user Rx complete callback */
- #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
- hspi->RxCpltCallback(hspi);
- #else
- HAL_SPI_RxCpltCallback(hspi);
- #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
- }
- else
- {
- hspi->State = HAL_SPI_STATE_READY;
- /* Call user TxRx complete callback */
- #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
- hspi->TxRxCpltCallback(hspi);
- #else
- HAL_SPI_TxRxCpltCallback(hspi);
- #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
- }
- }
- else
- {
- hspi->State = HAL_SPI_STATE_READY;
- /* Call user error callback */
- #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
- hspi->ErrorCallback(hspi);
- #else
- HAL_SPI_ErrorCallback(hspi);
- #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
- }
- #if (USE_SPI_CRC != 0U)
- }
- #endif /* USE_SPI_CRC */
- }
- /**
- * @brief Handle the end of the RX transaction.
- * @param hspi pointer to a SPI_HandleTypeDef structure that contains
- * the configuration information for SPI module.
- * @retval None
- */
- static void SPI_CloseRx_ISR(SPI_HandleTypeDef *hspi)
- {
- /* Disable RXNE and ERR interrupt */
- __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_RXNE | SPI_IT_ERR));
- /* Check the end of the transaction */
- if (SPI_EndRxTransaction(hspi, SPI_DEFAULT_TIMEOUT, HAL_GetTick()) != HAL_OK)
- {
- SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
- }
- /* Clear overrun flag in 2 Lines communication mode because received is not read */
- if (hspi->Init.Direction == SPI_DIRECTION_2LINES)
- {
- __HAL_SPI_CLEAR_OVRFLAG(hspi);
- }
- hspi->State = HAL_SPI_STATE_READY;
- #if (USE_SPI_CRC != 0U)
- /* Check if CRC error occurred */
- if (__HAL_SPI_GET_FLAG(hspi, SPI_FLAG_CRCERR) != RESET)
- {
- /* Check if CRC error is valid or not (workaround to be applied or not) */
- if (SPI_ISCRCErrorValid(hspi) == SPI_VALID_CRC_ERROR)
- {
- SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_CRC);
- /* Reset CRC Calculation */
- SPI_RESET_CRC(hspi);
- /* Call user error callback */
- #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
- hspi->ErrorCallback(hspi);
- #else
- HAL_SPI_ErrorCallback(hspi);
- #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
- }
- else
- {
- __HAL_SPI_CLEAR_CRCERRFLAG(hspi);
- }
- }
- else
- {
- #endif /* USE_SPI_CRC */
- if (hspi->ErrorCode == HAL_SPI_ERROR_NONE)
- {
- /* Call user Rx complete callback */
- #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
- hspi->RxCpltCallback(hspi);
- #else
- HAL_SPI_RxCpltCallback(hspi);
- #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
- }
- else
- {
- /* Call user error callback */
- #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
- hspi->ErrorCallback(hspi);
- #else
- HAL_SPI_ErrorCallback(hspi);
- #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
- }
- #if (USE_SPI_CRC != 0U)
- }
- #endif /* USE_SPI_CRC */
- }
- /**
- * @brief Handle the end of the TX transaction.
- * @param hspi pointer to a SPI_HandleTypeDef structure that contains
- * the configuration information for SPI module.
- * @retval None
- */
- static void SPI_CloseTx_ISR(SPI_HandleTypeDef *hspi)
- {
- uint32_t tickstart;
- __IO uint32_t count = SPI_DEFAULT_TIMEOUT * (SystemCoreClock / 24U / 1000U);
- /* Init tickstart for timeout management*/
- tickstart = HAL_GetTick();
- /* Wait until TXE flag is set */
- do
- {
- if (count == 0U)
- {
- SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
- break;
- }
- count--;
- } while ((hspi->Instance->SR & SPI_FLAG_TXE) == RESET);
- /* Disable TXE and ERR interrupt */
- __HAL_SPI_DISABLE_IT(hspi, (SPI_IT_TXE | SPI_IT_ERR));
- /* Check the end of the transaction */
- if (SPI_EndRxTxTransaction(hspi, SPI_DEFAULT_TIMEOUT, tickstart) != HAL_OK)
- {
- SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_FLAG);
- }
- /* Clear overrun flag in 2 Lines communication mode because received is not read */
- if (hspi->Init.Direction == SPI_DIRECTION_2LINES)
- {
- __HAL_SPI_CLEAR_OVRFLAG(hspi);
- }
- hspi->State = HAL_SPI_STATE_READY;
- if (hspi->ErrorCode != HAL_SPI_ERROR_NONE)
- {
- /* Call user error callback */
- #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
- hspi->ErrorCallback(hspi);
- #else
- HAL_SPI_ErrorCallback(hspi);
- #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
- }
- else
- {
- /* Call user Rx complete callback */
- #if (USE_HAL_SPI_REGISTER_CALLBACKS == 1U)
- hspi->TxCpltCallback(hspi);
- #else
- HAL_SPI_TxCpltCallback(hspi);
- #endif /* USE_HAL_SPI_REGISTER_CALLBACKS */
- }
- }
- /**
- * @brief Handle abort a Rx transaction.
- * @param hspi pointer to a SPI_HandleTypeDef structure that contains
- * the configuration information for SPI module.
- * @retval None
- */
- static void SPI_AbortRx_ISR(SPI_HandleTypeDef *hspi)
- {
- __IO uint32_t tmpreg = 0U;
- __IO uint32_t count = SPI_DEFAULT_TIMEOUT * (SystemCoreClock / 24U / 1000U);
- /* Wait until TXE flag is set */
- do
- {
- if (count == 0U)
- {
- SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_ABORT);
- break;
- }
- count--;
- } while ((hspi->Instance->SR & SPI_FLAG_TXE) == RESET);
- /* Disable SPI Peripheral */
- __HAL_SPI_DISABLE(hspi);
- /* Disable TXEIE, RXNEIE and ERRIE(mode fault event, overrun error, TI frame error) interrupts */
- CLEAR_BIT(hspi->Instance->CR2, (SPI_CR2_TXEIE | SPI_CR2_RXNEIE | SPI_CR2_ERRIE));
- /* Flush Data Register by a blank read */
- tmpreg = READ_REG(hspi->Instance->DR);
- /* To avoid GCC warning */
- UNUSED(tmpreg);
- hspi->State = HAL_SPI_STATE_ABORT;
- }
- /**
- * @brief Handle abort a Tx or Rx/Tx transaction.
- * @param hspi pointer to a SPI_HandleTypeDef structure that contains
- * the configuration information for SPI module.
- * @retval None
- */
- static void SPI_AbortTx_ISR(SPI_HandleTypeDef *hspi)
- {
- /* Disable TXEIE interrupt */
- CLEAR_BIT(hspi->Instance->CR2, (SPI_CR2_TXEIE));
- /* Disable SPI Peripheral */
- __HAL_SPI_DISABLE(hspi);
- hspi->State = HAL_SPI_STATE_ABORT;
- }
- #if (USE_SPI_CRC != 0U)
- /**
- * @brief Checks if encountered CRC error could be corresponding to wrongly detected errors
- * according to SPI instance, Device type, and revision ID.
- * @param hspi: pointer to a SPI_HandleTypeDef structure that contains
- * the configuration information for SPI module.
- * @retval CRC error validity (SPI_INVALID_CRC_ERROR or SPI_VALID_CRC_ERROR).
- */
- uint8_t SPI_ISCRCErrorValid(SPI_HandleTypeDef *hspi)
- {
- #if defined(SPI_CRC_ERROR_WORKAROUND_FEATURE) && (USE_SPI_CRC_ERROR_WORKAROUND != 0U)
- /* Check how to handle this CRC error (workaround to be applied or not) */
- /* If CRC errors could be wrongly detected (issue 2.15.2 in STM32F10xxC/D/E silicon limitations ES (DocID14732 Rev 13) */
- if (hspi->Instance == SPI2)
- {
- if (hspi->Instance->RXCRCR == 0U)
- {
- return (SPI_INVALID_CRC_ERROR);
- }
- }
- #endif /* USE_SPI_CRC_ERROR_WORKAROUND */
- /* Prevent unused argument(s) compilation warning */
- UNUSED(hspi);
- return (SPI_VALID_CRC_ERROR);
- }
- #endif /* USE_SPI_CRC */
- /**
- * @}
- */
- #endif /* HAL_SPI_MODULE_ENABLED */
- /**
- * @}
- */
- /**
- * @}
- */
|