stm32f0xx_hal_smbus.c 98 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775
  1. /**
  2. ******************************************************************************
  3. * @file stm32f0xx_hal_smbus.c
  4. * @author MCD Application Team
  5. * @brief SMBUS HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the System Management Bus (SMBus) peripheral,
  8. * based on I2C principles of operation :
  9. * + Initialization and de-initialization functions
  10. * + IO operation functions
  11. * + Peripheral State and Errors functions
  12. *
  13. ******************************************************************************
  14. * @attention
  15. *
  16. * Copyright (c) 2016 STMicroelectronics.
  17. * All rights reserved.
  18. *
  19. * This software is licensed under terms that can be found in the LICENSE file
  20. * in the root directory of this software component.
  21. * If no LICENSE file comes with this software, it is provided AS-IS.
  22. *
  23. ******************************************************************************
  24. @verbatim
  25. ==============================================================================
  26. ##### How to use this driver #####
  27. ==============================================================================
  28. [..]
  29. The SMBUS HAL driver can be used as follows:
  30. (#) Declare a SMBUS_HandleTypeDef handle structure, for example:
  31. SMBUS_HandleTypeDef hsmbus;
  32. (#)Initialize the SMBUS low level resources by implementing the HAL_SMBUS_MspInit() API:
  33. (##) Enable the SMBUSx interface clock
  34. (##) SMBUS pins configuration
  35. (+++) Enable the clock for the SMBUS GPIOs
  36. (+++) Configure SMBUS pins as alternate function open-drain
  37. (##) NVIC configuration if you need to use interrupt process
  38. (+++) Configure the SMBUSx interrupt priority
  39. (+++) Enable the NVIC SMBUS IRQ Channel
  40. (#) Configure the Communication Clock Timing, Bus Timeout, Own Address1, Master Addressing mode,
  41. Dual Addressing mode, Own Address2, Own Address2 Mask, General call, Nostretch mode,
  42. Peripheral mode and Packet Error Check mode in the hsmbus Init structure.
  43. (#) Initialize the SMBUS registers by calling the HAL_SMBUS_Init() API:
  44. (++) These API's configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
  45. by calling the customized HAL_SMBUS_MspInit(&hsmbus) API.
  46. (#) To check if target device is ready for communication, use the function HAL_SMBUS_IsDeviceReady()
  47. (#) For SMBUS IO operations, only one mode of operations is available within this driver
  48. *** Interrupt mode IO operation ***
  49. ===================================
  50. [..]
  51. (+) Transmit in master/host SMBUS mode an amount of data in non-blocking mode
  52. using HAL_SMBUS_Master_Transmit_IT()
  53. (++) At transmission end of transfer HAL_SMBUS_MasterTxCpltCallback() is executed and users can
  54. add their own code by customization of function pointer HAL_SMBUS_MasterTxCpltCallback()
  55. (+) Receive in master/host SMBUS mode an amount of data in non-blocking mode
  56. using HAL_SMBUS_Master_Receive_IT()
  57. (++) At reception end of transfer HAL_SMBUS_MasterRxCpltCallback() is executed and users can
  58. add their own code by customization of function pointer HAL_SMBUS_MasterRxCpltCallback()
  59. (+) Abort a master/host SMBUS process communication with Interrupt using HAL_SMBUS_Master_Abort_IT()
  60. (++) The associated previous transfer callback is called at the end of abort process
  61. (++) mean HAL_SMBUS_MasterTxCpltCallback() in case of previous state was master transmit
  62. (++) mean HAL_SMBUS_MasterRxCpltCallback() in case of previous state was master receive
  63. (+) Enable/disable the Address listen mode in slave/device or host/slave SMBUS mode
  64. using HAL_SMBUS_EnableListen_IT() HAL_SMBUS_DisableListen_IT()
  65. (++) When address slave/device SMBUS match, HAL_SMBUS_AddrCallback() is executed and users can
  66. add their own code to check the Address Match Code and the transmission direction
  67. request by master/host (Write/Read).
  68. (++) At Listen mode end HAL_SMBUS_ListenCpltCallback() is executed and users can
  69. add their own code by customization of function pointer HAL_SMBUS_ListenCpltCallback()
  70. (+) Transmit in slave/device SMBUS mode an amount of data in non-blocking mode
  71. using HAL_SMBUS_Slave_Transmit_IT()
  72. (++) At transmission end of transfer HAL_SMBUS_SlaveTxCpltCallback() is executed and users can
  73. add their own code by customization of function pointer HAL_SMBUS_SlaveTxCpltCallback()
  74. (+) Receive in slave/device SMBUS mode an amount of data in non-blocking mode
  75. using HAL_SMBUS_Slave_Receive_IT()
  76. (++) At reception end of transfer HAL_SMBUS_SlaveRxCpltCallback() is executed and users can
  77. add their own code by customization of function pointer HAL_SMBUS_SlaveRxCpltCallback()
  78. (+) Enable/Disable the SMBUS alert mode using
  79. HAL_SMBUS_EnableAlert_IT() or HAL_SMBUS_DisableAlert_IT()
  80. (++) When SMBUS Alert is generated HAL_SMBUS_ErrorCallback() is executed and users can
  81. add their own code by customization of function pointer HAL_SMBUS_ErrorCallback()
  82. to check the Alert Error Code using function HAL_SMBUS_GetError()
  83. (+) Get HAL state machine or error values using HAL_SMBUS_GetState() or HAL_SMBUS_GetError()
  84. (+) In case of transfer Error, HAL_SMBUS_ErrorCallback() function is executed and users can
  85. add their own code by customization of function pointer HAL_SMBUS_ErrorCallback()
  86. to check the Error Code using function HAL_SMBUS_GetError()
  87. *** SMBUS HAL driver macros list ***
  88. ==================================
  89. [..]
  90. Below the list of most used macros in SMBUS HAL driver.
  91. (+) __HAL_SMBUS_ENABLE: Enable the SMBUS peripheral
  92. (+) __HAL_SMBUS_DISABLE: Disable the SMBUS peripheral
  93. (+) __HAL_SMBUS_GET_FLAG: Check whether the specified SMBUS flag is set or not
  94. (+) __HAL_SMBUS_CLEAR_FLAG: Clear the specified SMBUS pending flag
  95. (+) __HAL_SMBUS_ENABLE_IT: Enable the specified SMBUS interrupt
  96. (+) __HAL_SMBUS_DISABLE_IT: Disable the specified SMBUS interrupt
  97. *** Callback registration ***
  98. =============================================
  99. [..]
  100. The compilation flag USE_HAL_SMBUS_REGISTER_CALLBACKS when set to 1
  101. allows the user to configure dynamically the driver callbacks.
  102. Use Functions HAL_SMBUS_RegisterCallback() or HAL_SMBUS_RegisterAddrCallback()
  103. to register an interrupt callback.
  104. [..]
  105. Function HAL_SMBUS_RegisterCallback() allows to register following callbacks:
  106. (+) MasterTxCpltCallback : callback for Master transmission end of transfer.
  107. (+) MasterRxCpltCallback : callback for Master reception end of transfer.
  108. (+) SlaveTxCpltCallback : callback for Slave transmission end of transfer.
  109. (+) SlaveRxCpltCallback : callback for Slave reception end of transfer.
  110. (+) ListenCpltCallback : callback for end of listen mode.
  111. (+) ErrorCallback : callback for error detection.
  112. (+) MspInitCallback : callback for Msp Init.
  113. (+) MspDeInitCallback : callback for Msp DeInit.
  114. This function takes as parameters the HAL peripheral handle, the Callback ID
  115. and a pointer to the user callback function.
  116. [..]
  117. For specific callback AddrCallback use dedicated register callbacks : HAL_SMBUS_RegisterAddrCallback.
  118. [..]
  119. Use function HAL_SMBUS_UnRegisterCallback to reset a callback to the default
  120. weak function.
  121. HAL_SMBUS_UnRegisterCallback takes as parameters the HAL peripheral handle,
  122. and the Callback ID.
  123. This function allows to reset following callbacks:
  124. (+) MasterTxCpltCallback : callback for Master transmission end of transfer.
  125. (+) MasterRxCpltCallback : callback for Master reception end of transfer.
  126. (+) SlaveTxCpltCallback : callback for Slave transmission end of transfer.
  127. (+) SlaveRxCpltCallback : callback for Slave reception end of transfer.
  128. (+) ListenCpltCallback : callback for end of listen mode.
  129. (+) ErrorCallback : callback for error detection.
  130. (+) MspInitCallback : callback for Msp Init.
  131. (+) MspDeInitCallback : callback for Msp DeInit.
  132. [..]
  133. For callback AddrCallback use dedicated register callbacks : HAL_SMBUS_UnRegisterAddrCallback.
  134. [..]
  135. By default, after the HAL_SMBUS_Init() and when the state is HAL_I2C_STATE_RESET
  136. all callbacks are set to the corresponding weak functions:
  137. examples HAL_SMBUS_MasterTxCpltCallback(), HAL_SMBUS_MasterRxCpltCallback().
  138. Exception done for MspInit and MspDeInit functions that are
  139. reset to the legacy weak functions in the HAL_SMBUS_Init()/ HAL_SMBUS_DeInit() only when
  140. these callbacks are null (not registered beforehand).
  141. If MspInit or MspDeInit are not null, the HAL_SMBUS_Init()/ HAL_SMBUS_DeInit()
  142. keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
  143. [..]
  144. Callbacks can be registered/unregistered in HAL_I2C_STATE_READY state only.
  145. Exception done MspInit/MspDeInit functions that can be registered/unregistered
  146. in HAL_I2C_STATE_READY or HAL_I2C_STATE_RESET state,
  147. thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
  148. Then, the user first registers the MspInit/MspDeInit user callbacks
  149. using HAL_SMBUS_RegisterCallback() before calling HAL_SMBUS_DeInit()
  150. or HAL_SMBUS_Init() function.
  151. [..]
  152. When the compilation flag USE_HAL_SMBUS_REGISTER_CALLBACKS is set to 0 or
  153. not defined, the callback registration feature is not available and all callbacks
  154. are set to the corresponding weak functions.
  155. [..]
  156. (@) You can refer to the SMBUS HAL driver header file for more useful macros
  157. @endverbatim
  158. */
  159. /* Includes ------------------------------------------------------------------*/
  160. #include "stm32f0xx_hal.h"
  161. /** @addtogroup STM32F0xx_HAL_Driver
  162. * @{
  163. */
  164. /** @defgroup SMBUS SMBUS
  165. * @brief SMBUS HAL module driver
  166. * @{
  167. */
  168. #ifdef HAL_SMBUS_MODULE_ENABLED
  169. /* Private typedef -----------------------------------------------------------*/
  170. /* Private constants ---------------------------------------------------------*/
  171. /** @defgroup SMBUS_Private_Define SMBUS Private Constants
  172. * @{
  173. */
  174. #define TIMING_CLEAR_MASK (0xF0FFFFFFUL) /*!< SMBUS TIMING clear register Mask */
  175. #define HAL_TIMEOUT_ADDR (10000U) /*!< 10 s */
  176. #define HAL_TIMEOUT_BUSY (25U) /*!< 25 ms */
  177. #define HAL_TIMEOUT_DIR (25U) /*!< 25 ms */
  178. #define HAL_TIMEOUT_RXNE (25U) /*!< 25 ms */
  179. #define HAL_TIMEOUT_STOPF (25U) /*!< 25 ms */
  180. #define HAL_TIMEOUT_TC (25U) /*!< 25 ms */
  181. #define HAL_TIMEOUT_TCR (25U) /*!< 25 ms */
  182. #define HAL_TIMEOUT_TXIS (25U) /*!< 25 ms */
  183. #define MAX_NBYTE_SIZE 255U
  184. /**
  185. * @}
  186. */
  187. /* Private macro -------------------------------------------------------------*/
  188. /* Private variables ---------------------------------------------------------*/
  189. /* Private function prototypes -----------------------------------------------*/
  190. /** @addtogroup SMBUS_Private_Functions SMBUS Private Functions
  191. * @{
  192. */
  193. /* Private functions to handle flags during polling transfer */
  194. static HAL_StatusTypeDef SMBUS_WaitOnFlagUntilTimeout(SMBUS_HandleTypeDef *hsmbus, uint32_t Flag,
  195. FlagStatus Status, uint32_t Timeout);
  196. /* Private functions for SMBUS transfer IRQ handler */
  197. static HAL_StatusTypeDef SMBUS_Master_ISR(SMBUS_HandleTypeDef *hsmbus, uint32_t StatusFlags);
  198. static HAL_StatusTypeDef SMBUS_Slave_ISR(SMBUS_HandleTypeDef *hsmbus, uint32_t StatusFlags);
  199. static void SMBUS_ITErrorHandler(SMBUS_HandleTypeDef *hsmbus);
  200. /* Private functions to centralize the enable/disable of Interrupts */
  201. static void SMBUS_Enable_IRQ(SMBUS_HandleTypeDef *hsmbus, uint32_t InterruptRequest);
  202. static void SMBUS_Disable_IRQ(SMBUS_HandleTypeDef *hsmbus, uint32_t InterruptRequest);
  203. /* Private function to flush TXDR register */
  204. static void SMBUS_Flush_TXDR(SMBUS_HandleTypeDef *hsmbus);
  205. /* Private function to handle start, restart or stop a transfer */
  206. static void SMBUS_TransferConfig(SMBUS_HandleTypeDef *hsmbus, uint16_t DevAddress, uint8_t Size,
  207. uint32_t Mode, uint32_t Request);
  208. /* Private function to Convert Specific options */
  209. static void SMBUS_ConvertOtherXferOptions(SMBUS_HandleTypeDef *hsmbus);
  210. /**
  211. * @}
  212. */
  213. /* Exported functions --------------------------------------------------------*/
  214. /** @defgroup SMBUS_Exported_Functions SMBUS Exported Functions
  215. * @{
  216. */
  217. /** @defgroup SMBUS_Exported_Functions_Group1 Initialization and de-initialization functions
  218. * @brief Initialization and Configuration functions
  219. *
  220. @verbatim
  221. ===============================================================================
  222. ##### Initialization and de-initialization functions #####
  223. ===============================================================================
  224. [..] This subsection provides a set of functions allowing to initialize and
  225. deinitialize the SMBUSx peripheral:
  226. (+) User must Implement HAL_SMBUS_MspInit() function in which he configures
  227. all related peripherals resources (CLOCK, GPIO, IT and NVIC ).
  228. (+) Call the function HAL_SMBUS_Init() to configure the selected device with
  229. the selected configuration:
  230. (++) Clock Timing
  231. (++) Bus Timeout
  232. (++) Analog Filer mode
  233. (++) Own Address 1
  234. (++) Addressing mode (Master, Slave)
  235. (++) Dual Addressing mode
  236. (++) Own Address 2
  237. (++) Own Address 2 Mask
  238. (++) General call mode
  239. (++) Nostretch mode
  240. (++) Packet Error Check mode
  241. (++) Peripheral mode
  242. (+) Call the function HAL_SMBUS_DeInit() to restore the default configuration
  243. of the selected SMBUSx peripheral.
  244. (+) Enable/Disable Analog/Digital filters with HAL_SMBUS_ConfigAnalogFilter() and
  245. HAL_SMBUS_ConfigDigitalFilter().
  246. @endverbatim
  247. * @{
  248. */
  249. /**
  250. * @brief Initialize the SMBUS according to the specified parameters
  251. * in the SMBUS_InitTypeDef and initialize the associated handle.
  252. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  253. * the configuration information for the specified SMBUS.
  254. * @retval HAL status
  255. */
  256. HAL_StatusTypeDef HAL_SMBUS_Init(SMBUS_HandleTypeDef *hsmbus)
  257. {
  258. /* Check the SMBUS handle allocation */
  259. if (hsmbus == NULL)
  260. {
  261. return HAL_ERROR;
  262. }
  263. /* Check the parameters */
  264. assert_param(IS_SMBUS_ALL_INSTANCE(hsmbus->Instance));
  265. assert_param(IS_SMBUS_ANALOG_FILTER(hsmbus->Init.AnalogFilter));
  266. assert_param(IS_SMBUS_OWN_ADDRESS1(hsmbus->Init.OwnAddress1));
  267. assert_param(IS_SMBUS_ADDRESSING_MODE(hsmbus->Init.AddressingMode));
  268. assert_param(IS_SMBUS_DUAL_ADDRESS(hsmbus->Init.DualAddressMode));
  269. assert_param(IS_SMBUS_OWN_ADDRESS2(hsmbus->Init.OwnAddress2));
  270. assert_param(IS_SMBUS_OWN_ADDRESS2_MASK(hsmbus->Init.OwnAddress2Masks));
  271. assert_param(IS_SMBUS_GENERAL_CALL(hsmbus->Init.GeneralCallMode));
  272. assert_param(IS_SMBUS_NO_STRETCH(hsmbus->Init.NoStretchMode));
  273. assert_param(IS_SMBUS_PEC(hsmbus->Init.PacketErrorCheckMode));
  274. assert_param(IS_SMBUS_PERIPHERAL_MODE(hsmbus->Init.PeripheralMode));
  275. if (hsmbus->State == HAL_SMBUS_STATE_RESET)
  276. {
  277. /* Allocate lock resource and initialize it */
  278. hsmbus->Lock = HAL_UNLOCKED;
  279. #if (USE_HAL_SMBUS_REGISTER_CALLBACKS == 1)
  280. hsmbus->MasterTxCpltCallback = HAL_SMBUS_MasterTxCpltCallback; /* Legacy weak MasterTxCpltCallback */
  281. hsmbus->MasterRxCpltCallback = HAL_SMBUS_MasterRxCpltCallback; /* Legacy weak MasterRxCpltCallback */
  282. hsmbus->SlaveTxCpltCallback = HAL_SMBUS_SlaveTxCpltCallback; /* Legacy weak SlaveTxCpltCallback */
  283. hsmbus->SlaveRxCpltCallback = HAL_SMBUS_SlaveRxCpltCallback; /* Legacy weak SlaveRxCpltCallback */
  284. hsmbus->ListenCpltCallback = HAL_SMBUS_ListenCpltCallback; /* Legacy weak ListenCpltCallback */
  285. hsmbus->ErrorCallback = HAL_SMBUS_ErrorCallback; /* Legacy weak ErrorCallback */
  286. hsmbus->AddrCallback = HAL_SMBUS_AddrCallback; /* Legacy weak AddrCallback */
  287. if (hsmbus->MspInitCallback == NULL)
  288. {
  289. hsmbus->MspInitCallback = HAL_SMBUS_MspInit; /* Legacy weak MspInit */
  290. }
  291. /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
  292. hsmbus->MspInitCallback(hsmbus);
  293. #else
  294. /* Init the low level hardware : GPIO, CLOCK, NVIC */
  295. HAL_SMBUS_MspInit(hsmbus);
  296. #endif /* USE_HAL_SMBUS_REGISTER_CALLBACKS */
  297. }
  298. hsmbus->State = HAL_SMBUS_STATE_BUSY;
  299. /* Disable the selected SMBUS peripheral */
  300. __HAL_SMBUS_DISABLE(hsmbus);
  301. /*---------------------------- SMBUSx TIMINGR Configuration ------------------------*/
  302. /* Configure SMBUSx: Frequency range */
  303. hsmbus->Instance->TIMINGR = hsmbus->Init.Timing & TIMING_CLEAR_MASK;
  304. /*---------------------------- SMBUSx TIMEOUTR Configuration ------------------------*/
  305. /* Configure SMBUSx: Bus Timeout */
  306. hsmbus->Instance->TIMEOUTR &= ~I2C_TIMEOUTR_TIMOUTEN;
  307. hsmbus->Instance->TIMEOUTR &= ~I2C_TIMEOUTR_TEXTEN;
  308. hsmbus->Instance->TIMEOUTR = hsmbus->Init.SMBusTimeout;
  309. /*---------------------------- SMBUSx OAR1 Configuration -----------------------*/
  310. /* Configure SMBUSx: Own Address1 and ack own address1 mode */
  311. hsmbus->Instance->OAR1 &= ~I2C_OAR1_OA1EN;
  312. if (hsmbus->Init.OwnAddress1 != 0UL)
  313. {
  314. if (hsmbus->Init.AddressingMode == SMBUS_ADDRESSINGMODE_7BIT)
  315. {
  316. hsmbus->Instance->OAR1 = (I2C_OAR1_OA1EN | hsmbus->Init.OwnAddress1);
  317. }
  318. else /* SMBUS_ADDRESSINGMODE_10BIT */
  319. {
  320. hsmbus->Instance->OAR1 = (I2C_OAR1_OA1EN | I2C_OAR1_OA1MODE | hsmbus->Init.OwnAddress1);
  321. }
  322. }
  323. /*---------------------------- SMBUSx CR2 Configuration ------------------------*/
  324. /* Configure SMBUSx: Addressing Master mode */
  325. if (hsmbus->Init.AddressingMode == SMBUS_ADDRESSINGMODE_10BIT)
  326. {
  327. hsmbus->Instance->CR2 = (I2C_CR2_ADD10);
  328. }
  329. /* Enable the AUTOEND by default, and enable NACK (should be disable only during Slave process) */
  330. /* AUTOEND and NACK bit will be manage during Transfer process */
  331. hsmbus->Instance->CR2 |= (I2C_CR2_AUTOEND | I2C_CR2_NACK);
  332. /*---------------------------- SMBUSx OAR2 Configuration -----------------------*/
  333. /* Configure SMBUSx: Dual mode and Own Address2 */
  334. hsmbus->Instance->OAR2 = (hsmbus->Init.DualAddressMode | hsmbus->Init.OwnAddress2 | \
  335. (hsmbus->Init.OwnAddress2Masks << 8U));
  336. /*---------------------------- SMBUSx CR1 Configuration ------------------------*/
  337. /* Configure SMBUSx: Generalcall and NoStretch mode */
  338. hsmbus->Instance->CR1 = (hsmbus->Init.GeneralCallMode | hsmbus->Init.NoStretchMode | \
  339. hsmbus->Init.PacketErrorCheckMode | hsmbus->Init.PeripheralMode | \
  340. hsmbus->Init.AnalogFilter);
  341. /* Enable Slave Byte Control only in case of Packet Error Check is enabled
  342. and SMBUS Peripheral is set in Slave mode */
  343. if ((hsmbus->Init.PacketErrorCheckMode == SMBUS_PEC_ENABLE) && \
  344. ((hsmbus->Init.PeripheralMode == SMBUS_PERIPHERAL_MODE_SMBUS_SLAVE) || \
  345. (hsmbus->Init.PeripheralMode == SMBUS_PERIPHERAL_MODE_SMBUS_SLAVE_ARP)))
  346. {
  347. hsmbus->Instance->CR1 |= I2C_CR1_SBC;
  348. }
  349. /* Enable the selected SMBUS peripheral */
  350. __HAL_SMBUS_ENABLE(hsmbus);
  351. hsmbus->ErrorCode = HAL_SMBUS_ERROR_NONE;
  352. hsmbus->PreviousState = HAL_SMBUS_STATE_READY;
  353. hsmbus->State = HAL_SMBUS_STATE_READY;
  354. return HAL_OK;
  355. }
  356. /**
  357. * @brief DeInitialize the SMBUS peripheral.
  358. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  359. * the configuration information for the specified SMBUS.
  360. * @retval HAL status
  361. */
  362. HAL_StatusTypeDef HAL_SMBUS_DeInit(SMBUS_HandleTypeDef *hsmbus)
  363. {
  364. /* Check the SMBUS handle allocation */
  365. if (hsmbus == NULL)
  366. {
  367. return HAL_ERROR;
  368. }
  369. /* Check the parameters */
  370. assert_param(IS_SMBUS_ALL_INSTANCE(hsmbus->Instance));
  371. hsmbus->State = HAL_SMBUS_STATE_BUSY;
  372. /* Disable the SMBUS Peripheral Clock */
  373. __HAL_SMBUS_DISABLE(hsmbus);
  374. #if (USE_HAL_SMBUS_REGISTER_CALLBACKS == 1)
  375. if (hsmbus->MspDeInitCallback == NULL)
  376. {
  377. hsmbus->MspDeInitCallback = HAL_SMBUS_MspDeInit; /* Legacy weak MspDeInit */
  378. }
  379. /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
  380. hsmbus->MspDeInitCallback(hsmbus);
  381. #else
  382. /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
  383. HAL_SMBUS_MspDeInit(hsmbus);
  384. #endif /* USE_HAL_SMBUS_REGISTER_CALLBACKS */
  385. hsmbus->ErrorCode = HAL_SMBUS_ERROR_NONE;
  386. hsmbus->PreviousState = HAL_SMBUS_STATE_RESET;
  387. hsmbus->State = HAL_SMBUS_STATE_RESET;
  388. /* Release Lock */
  389. __HAL_UNLOCK(hsmbus);
  390. return HAL_OK;
  391. }
  392. /**
  393. * @brief Initialize the SMBUS MSP.
  394. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  395. * the configuration information for the specified SMBUS.
  396. * @retval None
  397. */
  398. __weak void HAL_SMBUS_MspInit(SMBUS_HandleTypeDef *hsmbus)
  399. {
  400. /* Prevent unused argument(s) compilation warning */
  401. UNUSED(hsmbus);
  402. /* NOTE : This function should not be modified, when the callback is needed,
  403. the HAL_SMBUS_MspInit could be implemented in the user file
  404. */
  405. }
  406. /**
  407. * @brief DeInitialize the SMBUS MSP.
  408. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  409. * the configuration information for the specified SMBUS.
  410. * @retval None
  411. */
  412. __weak void HAL_SMBUS_MspDeInit(SMBUS_HandleTypeDef *hsmbus)
  413. {
  414. /* Prevent unused argument(s) compilation warning */
  415. UNUSED(hsmbus);
  416. /* NOTE : This function should not be modified, when the callback is needed,
  417. the HAL_SMBUS_MspDeInit could be implemented in the user file
  418. */
  419. }
  420. /**
  421. * @brief Configure Analog noise filter.
  422. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  423. * the configuration information for the specified SMBUS.
  424. * @param AnalogFilter This parameter can be one of the following values:
  425. * @arg @ref SMBUS_ANALOGFILTER_ENABLE
  426. * @arg @ref SMBUS_ANALOGFILTER_DISABLE
  427. * @retval HAL status
  428. */
  429. HAL_StatusTypeDef HAL_SMBUS_ConfigAnalogFilter(SMBUS_HandleTypeDef *hsmbus, uint32_t AnalogFilter)
  430. {
  431. /* Check the parameters */
  432. assert_param(IS_SMBUS_ALL_INSTANCE(hsmbus->Instance));
  433. assert_param(IS_SMBUS_ANALOG_FILTER(AnalogFilter));
  434. if (hsmbus->State == HAL_SMBUS_STATE_READY)
  435. {
  436. /* Process Locked */
  437. __HAL_LOCK(hsmbus);
  438. hsmbus->State = HAL_SMBUS_STATE_BUSY;
  439. /* Disable the selected SMBUS peripheral */
  440. __HAL_SMBUS_DISABLE(hsmbus);
  441. /* Reset ANOFF bit */
  442. hsmbus->Instance->CR1 &= ~(I2C_CR1_ANFOFF);
  443. /* Set analog filter bit*/
  444. hsmbus->Instance->CR1 |= AnalogFilter;
  445. __HAL_SMBUS_ENABLE(hsmbus);
  446. hsmbus->State = HAL_SMBUS_STATE_READY;
  447. /* Process Unlocked */
  448. __HAL_UNLOCK(hsmbus);
  449. return HAL_OK;
  450. }
  451. else
  452. {
  453. return HAL_BUSY;
  454. }
  455. }
  456. /**
  457. * @brief Configure Digital noise filter.
  458. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  459. * the configuration information for the specified SMBUS.
  460. * @param DigitalFilter Coefficient of digital noise filter between Min_Data=0x00 and Max_Data=0x0F.
  461. * @retval HAL status
  462. */
  463. HAL_StatusTypeDef HAL_SMBUS_ConfigDigitalFilter(SMBUS_HandleTypeDef *hsmbus, uint32_t DigitalFilter)
  464. {
  465. uint32_t tmpreg;
  466. /* Check the parameters */
  467. assert_param(IS_SMBUS_ALL_INSTANCE(hsmbus->Instance));
  468. assert_param(IS_SMBUS_DIGITAL_FILTER(DigitalFilter));
  469. if (hsmbus->State == HAL_SMBUS_STATE_READY)
  470. {
  471. /* Process Locked */
  472. __HAL_LOCK(hsmbus);
  473. hsmbus->State = HAL_SMBUS_STATE_BUSY;
  474. /* Disable the selected SMBUS peripheral */
  475. __HAL_SMBUS_DISABLE(hsmbus);
  476. /* Get the old register value */
  477. tmpreg = hsmbus->Instance->CR1;
  478. /* Reset I2C DNF bits [11:8] */
  479. tmpreg &= ~(I2C_CR1_DNF);
  480. /* Set I2Cx DNF coefficient */
  481. tmpreg |= DigitalFilter << I2C_CR1_DNF_Pos;
  482. /* Store the new register value */
  483. hsmbus->Instance->CR1 = tmpreg;
  484. __HAL_SMBUS_ENABLE(hsmbus);
  485. hsmbus->State = HAL_SMBUS_STATE_READY;
  486. /* Process Unlocked */
  487. __HAL_UNLOCK(hsmbus);
  488. return HAL_OK;
  489. }
  490. else
  491. {
  492. return HAL_BUSY;
  493. }
  494. }
  495. #if (USE_HAL_SMBUS_REGISTER_CALLBACKS == 1)
  496. /**
  497. * @brief Register a User SMBUS Callback
  498. * To be used instead of the weak predefined callback
  499. * @note The HAL_SMBUS_RegisterCallback() may be called before HAL_SMBUS_Init() in
  500. * HAL_SMBUS_STATE_RESET to register callbacks for HAL_SMBUS_MSPINIT_CB_ID and
  501. * HAL_SMBUS_MSPDEINIT_CB_ID.
  502. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  503. * the configuration information for the specified SMBUS.
  504. * @param CallbackID ID of the callback to be registered
  505. * This parameter can be one of the following values:
  506. * @arg @ref HAL_SMBUS_MASTER_TX_COMPLETE_CB_ID Master Tx Transfer completed callback ID
  507. * @arg @ref HAL_SMBUS_MASTER_RX_COMPLETE_CB_ID Master Rx Transfer completed callback ID
  508. * @arg @ref HAL_SMBUS_SLAVE_TX_COMPLETE_CB_ID Slave Tx Transfer completed callback ID
  509. * @arg @ref HAL_SMBUS_SLAVE_RX_COMPLETE_CB_ID Slave Rx Transfer completed callback ID
  510. * @arg @ref HAL_SMBUS_LISTEN_COMPLETE_CB_ID Listen Complete callback ID
  511. * @arg @ref HAL_SMBUS_ERROR_CB_ID Error callback ID
  512. * @arg @ref HAL_SMBUS_MSPINIT_CB_ID MspInit callback ID
  513. * @arg @ref HAL_SMBUS_MSPDEINIT_CB_ID MspDeInit callback ID
  514. * @param pCallback pointer to the Callback function
  515. * @retval HAL status
  516. */
  517. HAL_StatusTypeDef HAL_SMBUS_RegisterCallback(SMBUS_HandleTypeDef *hsmbus,
  518. HAL_SMBUS_CallbackIDTypeDef CallbackID,
  519. pSMBUS_CallbackTypeDef pCallback)
  520. {
  521. HAL_StatusTypeDef status = HAL_OK;
  522. if (pCallback == NULL)
  523. {
  524. /* Update the error code */
  525. hsmbus->ErrorCode |= HAL_SMBUS_ERROR_INVALID_CALLBACK;
  526. return HAL_ERROR;
  527. }
  528. if (HAL_SMBUS_STATE_READY == hsmbus->State)
  529. {
  530. switch (CallbackID)
  531. {
  532. case HAL_SMBUS_MASTER_TX_COMPLETE_CB_ID :
  533. hsmbus->MasterTxCpltCallback = pCallback;
  534. break;
  535. case HAL_SMBUS_MASTER_RX_COMPLETE_CB_ID :
  536. hsmbus->MasterRxCpltCallback = pCallback;
  537. break;
  538. case HAL_SMBUS_SLAVE_TX_COMPLETE_CB_ID :
  539. hsmbus->SlaveTxCpltCallback = pCallback;
  540. break;
  541. case HAL_SMBUS_SLAVE_RX_COMPLETE_CB_ID :
  542. hsmbus->SlaveRxCpltCallback = pCallback;
  543. break;
  544. case HAL_SMBUS_LISTEN_COMPLETE_CB_ID :
  545. hsmbus->ListenCpltCallback = pCallback;
  546. break;
  547. case HAL_SMBUS_ERROR_CB_ID :
  548. hsmbus->ErrorCallback = pCallback;
  549. break;
  550. case HAL_SMBUS_MSPINIT_CB_ID :
  551. hsmbus->MspInitCallback = pCallback;
  552. break;
  553. case HAL_SMBUS_MSPDEINIT_CB_ID :
  554. hsmbus->MspDeInitCallback = pCallback;
  555. break;
  556. default :
  557. /* Update the error code */
  558. hsmbus->ErrorCode |= HAL_SMBUS_ERROR_INVALID_CALLBACK;
  559. /* Return error status */
  560. status = HAL_ERROR;
  561. break;
  562. }
  563. }
  564. else if (HAL_SMBUS_STATE_RESET == hsmbus->State)
  565. {
  566. switch (CallbackID)
  567. {
  568. case HAL_SMBUS_MSPINIT_CB_ID :
  569. hsmbus->MspInitCallback = pCallback;
  570. break;
  571. case HAL_SMBUS_MSPDEINIT_CB_ID :
  572. hsmbus->MspDeInitCallback = pCallback;
  573. break;
  574. default :
  575. /* Update the error code */
  576. hsmbus->ErrorCode |= HAL_SMBUS_ERROR_INVALID_CALLBACK;
  577. /* Return error status */
  578. status = HAL_ERROR;
  579. break;
  580. }
  581. }
  582. else
  583. {
  584. /* Update the error code */
  585. hsmbus->ErrorCode |= HAL_SMBUS_ERROR_INVALID_CALLBACK;
  586. /* Return error status */
  587. status = HAL_ERROR;
  588. }
  589. return status;
  590. }
  591. /**
  592. * @brief Unregister an SMBUS Callback
  593. * SMBUS callback is redirected to the weak predefined callback
  594. * @note The HAL_SMBUS_UnRegisterCallback() may be called before HAL_SMBUS_Init() in
  595. * HAL_SMBUS_STATE_RESET to un-register callbacks for HAL_SMBUS_MSPINIT_CB_ID and
  596. * HAL_SMBUS_MSPDEINIT_CB_ID
  597. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  598. * the configuration information for the specified SMBUS.
  599. * @param CallbackID ID of the callback to be unregistered
  600. * This parameter can be one of the following values:
  601. * This parameter can be one of the following values:
  602. * @arg @ref HAL_SMBUS_MASTER_TX_COMPLETE_CB_ID Master Tx Transfer completed callback ID
  603. * @arg @ref HAL_SMBUS_MASTER_RX_COMPLETE_CB_ID Master Rx Transfer completed callback ID
  604. * @arg @ref HAL_SMBUS_SLAVE_TX_COMPLETE_CB_ID Slave Tx Transfer completed callback ID
  605. * @arg @ref HAL_SMBUS_SLAVE_RX_COMPLETE_CB_ID Slave Rx Transfer completed callback ID
  606. * @arg @ref HAL_SMBUS_LISTEN_COMPLETE_CB_ID Listen Complete callback ID
  607. * @arg @ref HAL_SMBUS_ERROR_CB_ID Error callback ID
  608. * @arg @ref HAL_SMBUS_MSPINIT_CB_ID MspInit callback ID
  609. * @arg @ref HAL_SMBUS_MSPDEINIT_CB_ID MspDeInit callback ID
  610. * @retval HAL status
  611. */
  612. HAL_StatusTypeDef HAL_SMBUS_UnRegisterCallback(SMBUS_HandleTypeDef *hsmbus,
  613. HAL_SMBUS_CallbackIDTypeDef CallbackID)
  614. {
  615. HAL_StatusTypeDef status = HAL_OK;
  616. if (HAL_SMBUS_STATE_READY == hsmbus->State)
  617. {
  618. switch (CallbackID)
  619. {
  620. case HAL_SMBUS_MASTER_TX_COMPLETE_CB_ID :
  621. hsmbus->MasterTxCpltCallback = HAL_SMBUS_MasterTxCpltCallback; /* Legacy weak MasterTxCpltCallback */
  622. break;
  623. case HAL_SMBUS_MASTER_RX_COMPLETE_CB_ID :
  624. hsmbus->MasterRxCpltCallback = HAL_SMBUS_MasterRxCpltCallback; /* Legacy weak MasterRxCpltCallback */
  625. break;
  626. case HAL_SMBUS_SLAVE_TX_COMPLETE_CB_ID :
  627. hsmbus->SlaveTxCpltCallback = HAL_SMBUS_SlaveTxCpltCallback; /* Legacy weak SlaveTxCpltCallback */
  628. break;
  629. case HAL_SMBUS_SLAVE_RX_COMPLETE_CB_ID :
  630. hsmbus->SlaveRxCpltCallback = HAL_SMBUS_SlaveRxCpltCallback; /* Legacy weak SlaveRxCpltCallback */
  631. break;
  632. case HAL_SMBUS_LISTEN_COMPLETE_CB_ID :
  633. hsmbus->ListenCpltCallback = HAL_SMBUS_ListenCpltCallback; /* Legacy weak ListenCpltCallback */
  634. break;
  635. case HAL_SMBUS_ERROR_CB_ID :
  636. hsmbus->ErrorCallback = HAL_SMBUS_ErrorCallback; /* Legacy weak ErrorCallback */
  637. break;
  638. case HAL_SMBUS_MSPINIT_CB_ID :
  639. hsmbus->MspInitCallback = HAL_SMBUS_MspInit; /* Legacy weak MspInit */
  640. break;
  641. case HAL_SMBUS_MSPDEINIT_CB_ID :
  642. hsmbus->MspDeInitCallback = HAL_SMBUS_MspDeInit; /* Legacy weak MspDeInit */
  643. break;
  644. default :
  645. /* Update the error code */
  646. hsmbus->ErrorCode |= HAL_SMBUS_ERROR_INVALID_CALLBACK;
  647. /* Return error status */
  648. status = HAL_ERROR;
  649. break;
  650. }
  651. }
  652. else if (HAL_SMBUS_STATE_RESET == hsmbus->State)
  653. {
  654. switch (CallbackID)
  655. {
  656. case HAL_SMBUS_MSPINIT_CB_ID :
  657. hsmbus->MspInitCallback = HAL_SMBUS_MspInit; /* Legacy weak MspInit */
  658. break;
  659. case HAL_SMBUS_MSPDEINIT_CB_ID :
  660. hsmbus->MspDeInitCallback = HAL_SMBUS_MspDeInit; /* Legacy weak MspDeInit */
  661. break;
  662. default :
  663. /* Update the error code */
  664. hsmbus->ErrorCode |= HAL_SMBUS_ERROR_INVALID_CALLBACK;
  665. /* Return error status */
  666. status = HAL_ERROR;
  667. break;
  668. }
  669. }
  670. else
  671. {
  672. /* Update the error code */
  673. hsmbus->ErrorCode |= HAL_SMBUS_ERROR_INVALID_CALLBACK;
  674. /* Return error status */
  675. status = HAL_ERROR;
  676. }
  677. return status;
  678. }
  679. /**
  680. * @brief Register the Slave Address Match SMBUS Callback
  681. * To be used instead of the weak HAL_SMBUS_AddrCallback() predefined callback
  682. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  683. * the configuration information for the specified SMBUS.
  684. * @param pCallback pointer to the Address Match Callback function
  685. * @retval HAL status
  686. */
  687. HAL_StatusTypeDef HAL_SMBUS_RegisterAddrCallback(SMBUS_HandleTypeDef *hsmbus,
  688. pSMBUS_AddrCallbackTypeDef pCallback)
  689. {
  690. HAL_StatusTypeDef status = HAL_OK;
  691. if (pCallback == NULL)
  692. {
  693. /* Update the error code */
  694. hsmbus->ErrorCode |= HAL_SMBUS_ERROR_INVALID_CALLBACK;
  695. return HAL_ERROR;
  696. }
  697. if (HAL_SMBUS_STATE_READY == hsmbus->State)
  698. {
  699. hsmbus->AddrCallback = pCallback;
  700. }
  701. else
  702. {
  703. /* Update the error code */
  704. hsmbus->ErrorCode |= HAL_SMBUS_ERROR_INVALID_CALLBACK;
  705. /* Return error status */
  706. status = HAL_ERROR;
  707. }
  708. return status;
  709. }
  710. /**
  711. * @brief UnRegister the Slave Address Match SMBUS Callback
  712. * Info Ready SMBUS Callback is redirected to the weak HAL_SMBUS_AddrCallback() predefined callback
  713. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  714. * the configuration information for the specified SMBUS.
  715. * @retval HAL status
  716. */
  717. HAL_StatusTypeDef HAL_SMBUS_UnRegisterAddrCallback(SMBUS_HandleTypeDef *hsmbus)
  718. {
  719. HAL_StatusTypeDef status = HAL_OK;
  720. if (HAL_SMBUS_STATE_READY == hsmbus->State)
  721. {
  722. hsmbus->AddrCallback = HAL_SMBUS_AddrCallback; /* Legacy weak AddrCallback */
  723. }
  724. else
  725. {
  726. /* Update the error code */
  727. hsmbus->ErrorCode |= HAL_SMBUS_ERROR_INVALID_CALLBACK;
  728. /* Return error status */
  729. status = HAL_ERROR;
  730. }
  731. return status;
  732. }
  733. #endif /* USE_HAL_SMBUS_REGISTER_CALLBACKS */
  734. /**
  735. * @}
  736. */
  737. /** @defgroup SMBUS_Exported_Functions_Group2 Input and Output operation functions
  738. * @brief Data transfers functions
  739. *
  740. @verbatim
  741. ===============================================================================
  742. ##### IO operation functions #####
  743. ===============================================================================
  744. [..]
  745. This subsection provides a set of functions allowing to manage the SMBUS data
  746. transfers.
  747. (#) Blocking mode function to check if device is ready for usage is :
  748. (++) HAL_SMBUS_IsDeviceReady()
  749. (#) There is only one mode of transfer:
  750. (++) Non-Blocking mode : The communication is performed using Interrupts.
  751. These functions return the status of the transfer startup.
  752. The end of the data processing will be indicated through the
  753. dedicated SMBUS IRQ when using Interrupt mode.
  754. (#) Non-Blocking mode functions with Interrupt are :
  755. (++) HAL_SMBUS_Master_Transmit_IT()
  756. (++) HAL_SMBUS_Master_Receive_IT()
  757. (++) HAL_SMBUS_Slave_Transmit_IT()
  758. (++) HAL_SMBUS_Slave_Receive_IT()
  759. (++) HAL_SMBUS_EnableListen_IT() or alias HAL_SMBUS_EnableListen_IT()
  760. (++) HAL_SMBUS_DisableListen_IT()
  761. (++) HAL_SMBUS_EnableAlert_IT()
  762. (++) HAL_SMBUS_DisableAlert_IT()
  763. (#) A set of Transfer Complete Callbacks are provided in non-Blocking mode:
  764. (++) HAL_SMBUS_MasterTxCpltCallback()
  765. (++) HAL_SMBUS_MasterRxCpltCallback()
  766. (++) HAL_SMBUS_SlaveTxCpltCallback()
  767. (++) HAL_SMBUS_SlaveRxCpltCallback()
  768. (++) HAL_SMBUS_AddrCallback()
  769. (++) HAL_SMBUS_ListenCpltCallback()
  770. (++) HAL_SMBUS_ErrorCallback()
  771. @endverbatim
  772. * @{
  773. */
  774. /**
  775. * @brief Transmit in master/host SMBUS mode an amount of data in non-blocking mode with Interrupt.
  776. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  777. * the configuration information for the specified SMBUS.
  778. * @param DevAddress Target device address: The device 7 bits address value
  779. * in datasheet must be shifted to the left before calling the interface
  780. * @param pData Pointer to data buffer
  781. * @param Size Amount of data to be sent
  782. * @param XferOptions Options of Transfer, value of @ref SMBUS_XferOptions_definition
  783. * @retval HAL status
  784. */
  785. HAL_StatusTypeDef HAL_SMBUS_Master_Transmit_IT(SMBUS_HandleTypeDef *hsmbus, uint16_t DevAddress,
  786. uint8_t *pData, uint16_t Size, uint32_t XferOptions)
  787. {
  788. uint32_t tmp;
  789. /* Check the parameters */
  790. assert_param(IS_SMBUS_TRANSFER_OPTIONS_REQUEST(XferOptions));
  791. if (hsmbus->State == HAL_SMBUS_STATE_READY)
  792. {
  793. /* Process Locked */
  794. __HAL_LOCK(hsmbus);
  795. hsmbus->State = HAL_SMBUS_STATE_MASTER_BUSY_TX;
  796. hsmbus->ErrorCode = HAL_SMBUS_ERROR_NONE;
  797. /* Prepare transfer parameters */
  798. hsmbus->pBuffPtr = pData;
  799. hsmbus->XferCount = Size;
  800. hsmbus->XferOptions = XferOptions;
  801. /* In case of Quick command, remove autoend mode */
  802. /* Manage the stop generation by software */
  803. if (hsmbus->pBuffPtr == NULL)
  804. {
  805. hsmbus->XferOptions &= ~SMBUS_AUTOEND_MODE;
  806. }
  807. if (Size > MAX_NBYTE_SIZE)
  808. {
  809. hsmbus->XferSize = MAX_NBYTE_SIZE;
  810. }
  811. else
  812. {
  813. hsmbus->XferSize = Size;
  814. }
  815. /* Send Slave Address */
  816. /* Set NBYTES to write and reload if size > MAX_NBYTE_SIZE and generate RESTART */
  817. if ((hsmbus->XferSize < hsmbus->XferCount) && (hsmbus->XferSize == MAX_NBYTE_SIZE))
  818. {
  819. SMBUS_TransferConfig(hsmbus, DevAddress, (uint8_t)hsmbus->XferSize,
  820. SMBUS_RELOAD_MODE | (hsmbus->XferOptions & SMBUS_SENDPEC_MODE),
  821. SMBUS_GENERATE_START_WRITE);
  822. }
  823. else
  824. {
  825. /* If transfer direction not change, do not generate Restart Condition */
  826. /* Mean Previous state is same as current state */
  827. /* Store current volatile XferOptions, misra rule */
  828. tmp = hsmbus->XferOptions;
  829. if ((hsmbus->PreviousState == HAL_SMBUS_STATE_MASTER_BUSY_TX) && \
  830. (IS_SMBUS_TRANSFER_OTHER_OPTIONS_REQUEST(tmp) == 0))
  831. {
  832. SMBUS_TransferConfig(hsmbus, DevAddress, (uint8_t)hsmbus->XferSize, hsmbus->XferOptions,
  833. SMBUS_NO_STARTSTOP);
  834. }
  835. /* Else transfer direction change, so generate Restart with new transfer direction */
  836. else
  837. {
  838. /* Convert OTHER_xxx XferOptions if any */
  839. SMBUS_ConvertOtherXferOptions(hsmbus);
  840. /* Handle Transfer */
  841. SMBUS_TransferConfig(hsmbus, DevAddress, (uint8_t)hsmbus->XferSize,
  842. hsmbus->XferOptions,
  843. SMBUS_GENERATE_START_WRITE);
  844. }
  845. /* If PEC mode is enable, size to transmit manage by SW part should be Size-1 byte, corresponding to PEC byte */
  846. /* PEC byte is automatically sent by HW block, no need to manage it in Transmit process */
  847. if (SMBUS_GET_PEC_MODE(hsmbus) != 0UL)
  848. {
  849. hsmbus->XferSize--;
  850. hsmbus->XferCount--;
  851. }
  852. }
  853. /* Process Unlocked */
  854. __HAL_UNLOCK(hsmbus);
  855. /* Note : The SMBUS interrupts must be enabled after unlocking current process
  856. to avoid the risk of SMBUS interrupt handle execution before current
  857. process unlock */
  858. SMBUS_Enable_IRQ(hsmbus, SMBUS_IT_TX);
  859. return HAL_OK;
  860. }
  861. else
  862. {
  863. return HAL_BUSY;
  864. }
  865. }
  866. /**
  867. * @brief Receive in master/host SMBUS mode an amount of data in non-blocking mode with Interrupt.
  868. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  869. * the configuration information for the specified SMBUS.
  870. * @param DevAddress Target device address: The device 7 bits address value
  871. * in datasheet must be shifted to the left before calling the interface
  872. * @param pData Pointer to data buffer
  873. * @param Size Amount of data to be sent
  874. * @param XferOptions Options of Transfer, value of @ref SMBUS_XferOptions_definition
  875. * @retval HAL status
  876. */
  877. HAL_StatusTypeDef HAL_SMBUS_Master_Receive_IT(SMBUS_HandleTypeDef *hsmbus, uint16_t DevAddress, uint8_t *pData,
  878. uint16_t Size, uint32_t XferOptions)
  879. {
  880. uint32_t tmp;
  881. /* Check the parameters */
  882. assert_param(IS_SMBUS_TRANSFER_OPTIONS_REQUEST(XferOptions));
  883. if (hsmbus->State == HAL_SMBUS_STATE_READY)
  884. {
  885. /* Process Locked */
  886. __HAL_LOCK(hsmbus);
  887. hsmbus->State = HAL_SMBUS_STATE_MASTER_BUSY_RX;
  888. hsmbus->ErrorCode = HAL_SMBUS_ERROR_NONE;
  889. /* Prepare transfer parameters */
  890. hsmbus->pBuffPtr = pData;
  891. hsmbus->XferCount = Size;
  892. hsmbus->XferOptions = XferOptions;
  893. /* In case of Quick command, remove autoend mode */
  894. /* Manage the stop generation by software */
  895. if (hsmbus->pBuffPtr == NULL)
  896. {
  897. hsmbus->XferOptions &= ~SMBUS_AUTOEND_MODE;
  898. }
  899. if (Size > MAX_NBYTE_SIZE)
  900. {
  901. hsmbus->XferSize = MAX_NBYTE_SIZE;
  902. }
  903. else
  904. {
  905. hsmbus->XferSize = Size;
  906. }
  907. /* Send Slave Address */
  908. /* Set NBYTES to write and reload if size > MAX_NBYTE_SIZE and generate RESTART */
  909. if ((hsmbus->XferSize < hsmbus->XferCount) && (hsmbus->XferSize == MAX_NBYTE_SIZE))
  910. {
  911. SMBUS_TransferConfig(hsmbus, DevAddress, (uint8_t)hsmbus->XferSize,
  912. SMBUS_RELOAD_MODE | (hsmbus->XferOptions & SMBUS_SENDPEC_MODE),
  913. SMBUS_GENERATE_START_READ);
  914. }
  915. else
  916. {
  917. /* If transfer direction not change, do not generate Restart Condition */
  918. /* Mean Previous state is same as current state */
  919. /* Store current volatile XferOptions, Misra rule */
  920. tmp = hsmbus->XferOptions;
  921. if ((hsmbus->PreviousState == HAL_SMBUS_STATE_MASTER_BUSY_RX) && \
  922. (IS_SMBUS_TRANSFER_OTHER_OPTIONS_REQUEST(tmp) == 0))
  923. {
  924. SMBUS_TransferConfig(hsmbus, DevAddress, (uint8_t)hsmbus->XferSize, hsmbus->XferOptions,
  925. SMBUS_NO_STARTSTOP);
  926. }
  927. /* Else transfer direction change, so generate Restart with new transfer direction */
  928. else
  929. {
  930. /* Convert OTHER_xxx XferOptions if any */
  931. SMBUS_ConvertOtherXferOptions(hsmbus);
  932. /* Handle Transfer */
  933. SMBUS_TransferConfig(hsmbus, DevAddress, (uint8_t)hsmbus->XferSize,
  934. hsmbus->XferOptions,
  935. SMBUS_GENERATE_START_READ);
  936. }
  937. }
  938. /* Process Unlocked */
  939. __HAL_UNLOCK(hsmbus);
  940. /* Note : The SMBUS interrupts must be enabled after unlocking current process
  941. to avoid the risk of SMBUS interrupt handle execution before current
  942. process unlock */
  943. SMBUS_Enable_IRQ(hsmbus, SMBUS_IT_RX);
  944. return HAL_OK;
  945. }
  946. else
  947. {
  948. return HAL_BUSY;
  949. }
  950. }
  951. /**
  952. * @brief Abort a master/host SMBUS process communication with Interrupt.
  953. * @note This abort can be called only if state is ready
  954. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  955. * the configuration information for the specified SMBUS.
  956. * @param DevAddress Target device address: The device 7 bits address value
  957. * in datasheet must be shifted to the left before calling the interface
  958. * @retval HAL status
  959. */
  960. HAL_StatusTypeDef HAL_SMBUS_Master_Abort_IT(SMBUS_HandleTypeDef *hsmbus, uint16_t DevAddress)
  961. {
  962. if (hsmbus->State == HAL_SMBUS_STATE_READY)
  963. {
  964. /* Process Locked */
  965. __HAL_LOCK(hsmbus);
  966. /* Keep the same state as previous */
  967. /* to perform as well the call of the corresponding end of transfer callback */
  968. if (hsmbus->PreviousState == HAL_SMBUS_STATE_MASTER_BUSY_TX)
  969. {
  970. hsmbus->State = HAL_SMBUS_STATE_MASTER_BUSY_TX;
  971. }
  972. else if (hsmbus->PreviousState == HAL_SMBUS_STATE_MASTER_BUSY_RX)
  973. {
  974. hsmbus->State = HAL_SMBUS_STATE_MASTER_BUSY_RX;
  975. }
  976. else
  977. {
  978. /* Wrong usage of abort function */
  979. /* This function should be used only in case of abort monitored by master device */
  980. return HAL_ERROR;
  981. }
  982. hsmbus->ErrorCode = HAL_SMBUS_ERROR_NONE;
  983. /* Set NBYTES to 1 to generate a dummy read on SMBUS peripheral */
  984. /* Set AUTOEND mode, this will generate a NACK then STOP condition to abort the current transfer */
  985. SMBUS_TransferConfig(hsmbus, DevAddress, 1, SMBUS_AUTOEND_MODE, SMBUS_NO_STARTSTOP);
  986. /* Process Unlocked */
  987. __HAL_UNLOCK(hsmbus);
  988. /* Note : The SMBUS interrupts must be enabled after unlocking current process
  989. to avoid the risk of SMBUS interrupt handle execution before current
  990. process unlock */
  991. if (hsmbus->State == HAL_SMBUS_STATE_MASTER_BUSY_TX)
  992. {
  993. SMBUS_Enable_IRQ(hsmbus, SMBUS_IT_TX);
  994. }
  995. else if (hsmbus->State == HAL_SMBUS_STATE_MASTER_BUSY_RX)
  996. {
  997. SMBUS_Enable_IRQ(hsmbus, SMBUS_IT_RX);
  998. }
  999. else
  1000. {
  1001. /* Nothing to do */
  1002. }
  1003. return HAL_OK;
  1004. }
  1005. else
  1006. {
  1007. return HAL_BUSY;
  1008. }
  1009. }
  1010. /**
  1011. * @brief Transmit in slave/device SMBUS mode an amount of data in non-blocking mode with Interrupt.
  1012. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  1013. * the configuration information for the specified SMBUS.
  1014. * @param pData Pointer to data buffer
  1015. * @param Size Amount of data to be sent
  1016. * @param XferOptions Options of Transfer, value of @ref SMBUS_XferOptions_definition
  1017. * @retval HAL status
  1018. */
  1019. HAL_StatusTypeDef HAL_SMBUS_Slave_Transmit_IT(SMBUS_HandleTypeDef *hsmbus, uint8_t *pData, uint16_t Size,
  1020. uint32_t XferOptions)
  1021. {
  1022. /* Check the parameters */
  1023. assert_param(IS_SMBUS_TRANSFER_OPTIONS_REQUEST(XferOptions));
  1024. if ((hsmbus->State & HAL_SMBUS_STATE_LISTEN) == HAL_SMBUS_STATE_LISTEN)
  1025. {
  1026. if ((pData == NULL) || (Size == 0UL))
  1027. {
  1028. hsmbus->ErrorCode = HAL_SMBUS_ERROR_INVALID_PARAM;
  1029. return HAL_ERROR;
  1030. }
  1031. /* Disable Interrupts, to prevent preemption during treatment in case of multicall */
  1032. SMBUS_Disable_IRQ(hsmbus, SMBUS_IT_ADDR | SMBUS_IT_TX);
  1033. /* Process Locked */
  1034. __HAL_LOCK(hsmbus);
  1035. hsmbus->State = (HAL_SMBUS_STATE_SLAVE_BUSY_TX | HAL_SMBUS_STATE_LISTEN);
  1036. hsmbus->ErrorCode = HAL_SMBUS_ERROR_NONE;
  1037. /* Set SBC bit to manage Acknowledge at each bit */
  1038. hsmbus->Instance->CR1 |= I2C_CR1_SBC;
  1039. /* Enable Address Acknowledge */
  1040. hsmbus->Instance->CR2 &= ~I2C_CR2_NACK;
  1041. /* Prepare transfer parameters */
  1042. hsmbus->pBuffPtr = pData;
  1043. hsmbus->XferCount = Size;
  1044. hsmbus->XferOptions = XferOptions;
  1045. /* Convert OTHER_xxx XferOptions if any */
  1046. SMBUS_ConvertOtherXferOptions(hsmbus);
  1047. if (Size > MAX_NBYTE_SIZE)
  1048. {
  1049. hsmbus->XferSize = MAX_NBYTE_SIZE;
  1050. }
  1051. else
  1052. {
  1053. hsmbus->XferSize = Size;
  1054. }
  1055. /* Set NBYTES to write and reload if size > MAX_NBYTE_SIZE and generate RESTART */
  1056. if ((hsmbus->XferSize < hsmbus->XferCount) && (hsmbus->XferSize == MAX_NBYTE_SIZE))
  1057. {
  1058. SMBUS_TransferConfig(hsmbus, 0, (uint8_t)hsmbus->XferSize,
  1059. SMBUS_RELOAD_MODE | (hsmbus->XferOptions & SMBUS_SENDPEC_MODE),
  1060. SMBUS_NO_STARTSTOP);
  1061. }
  1062. else
  1063. {
  1064. /* Set NBYTE to transmit */
  1065. SMBUS_TransferConfig(hsmbus, 0, (uint8_t)hsmbus->XferSize, hsmbus->XferOptions,
  1066. SMBUS_NO_STARTSTOP);
  1067. /* If PEC mode is enable, size to transmit should be Size-1 byte, corresponding to PEC byte */
  1068. /* PEC byte is automatically sent by HW block, no need to manage it in Transmit process */
  1069. if (SMBUS_GET_PEC_MODE(hsmbus) != 0UL)
  1070. {
  1071. hsmbus->XferSize--;
  1072. hsmbus->XferCount--;
  1073. }
  1074. }
  1075. /* Clear ADDR flag after prepare the transfer parameters */
  1076. /* This action will generate an acknowledge to the HOST */
  1077. __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_ADDR);
  1078. /* Process Unlocked */
  1079. __HAL_UNLOCK(hsmbus);
  1080. /* Note : The SMBUS interrupts must be enabled after unlocking current process
  1081. to avoid the risk of SMBUS interrupt handle execution before current
  1082. process unlock */
  1083. /* REnable ADDR interrupt */
  1084. SMBUS_Enable_IRQ(hsmbus, SMBUS_IT_TX | SMBUS_IT_ADDR);
  1085. return HAL_OK;
  1086. }
  1087. else
  1088. {
  1089. return HAL_BUSY;
  1090. }
  1091. }
  1092. /**
  1093. * @brief Receive in slave/device SMBUS mode an amount of data in non-blocking mode with Interrupt.
  1094. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  1095. * the configuration information for the specified SMBUS.
  1096. * @param pData Pointer to data buffer
  1097. * @param Size Amount of data to be sent
  1098. * @param XferOptions Options of Transfer, value of @ref SMBUS_XferOptions_definition
  1099. * @retval HAL status
  1100. */
  1101. HAL_StatusTypeDef HAL_SMBUS_Slave_Receive_IT(SMBUS_HandleTypeDef *hsmbus, uint8_t *pData, uint16_t Size,
  1102. uint32_t XferOptions)
  1103. {
  1104. /* Check the parameters */
  1105. assert_param(IS_SMBUS_TRANSFER_OPTIONS_REQUEST(XferOptions));
  1106. if ((hsmbus->State & HAL_SMBUS_STATE_LISTEN) == HAL_SMBUS_STATE_LISTEN)
  1107. {
  1108. if ((pData == NULL) || (Size == 0UL))
  1109. {
  1110. hsmbus->ErrorCode = HAL_SMBUS_ERROR_INVALID_PARAM;
  1111. return HAL_ERROR;
  1112. }
  1113. /* Disable Interrupts, to prevent preemption during treatment in case of multicall */
  1114. SMBUS_Disable_IRQ(hsmbus, SMBUS_IT_ADDR | SMBUS_IT_RX);
  1115. /* Process Locked */
  1116. __HAL_LOCK(hsmbus);
  1117. hsmbus->State = (HAL_SMBUS_STATE_SLAVE_BUSY_RX | HAL_SMBUS_STATE_LISTEN);
  1118. hsmbus->ErrorCode = HAL_SMBUS_ERROR_NONE;
  1119. /* Set SBC bit to manage Acknowledge at each bit */
  1120. hsmbus->Instance->CR1 |= I2C_CR1_SBC;
  1121. /* Enable Address Acknowledge */
  1122. hsmbus->Instance->CR2 &= ~I2C_CR2_NACK;
  1123. /* Prepare transfer parameters */
  1124. hsmbus->pBuffPtr = pData;
  1125. hsmbus->XferSize = Size;
  1126. hsmbus->XferCount = Size;
  1127. hsmbus->XferOptions = XferOptions;
  1128. /* Convert OTHER_xxx XferOptions if any */
  1129. SMBUS_ConvertOtherXferOptions(hsmbus);
  1130. /* Set NBYTE to receive */
  1131. /* If XferSize equal "1", or XferSize equal "2" with PEC requested (mean 1 data byte + 1 PEC byte */
  1132. /* no need to set RELOAD bit mode, a ACK will be automatically generated in that case */
  1133. /* else need to set RELOAD bit mode to generate an automatic ACK at each byte Received */
  1134. /* This RELOAD bit will be reset for last BYTE to be receive in SMBUS_Slave_ISR */
  1135. if (((SMBUS_GET_PEC_MODE(hsmbus) != 0UL) && (hsmbus->XferSize == 2U)) || (hsmbus->XferSize == 1U))
  1136. {
  1137. SMBUS_TransferConfig(hsmbus, 0, (uint8_t)hsmbus->XferSize, hsmbus->XferOptions,
  1138. SMBUS_NO_STARTSTOP);
  1139. }
  1140. else
  1141. {
  1142. SMBUS_TransferConfig(hsmbus, 0, 1, hsmbus->XferOptions | SMBUS_RELOAD_MODE, SMBUS_NO_STARTSTOP);
  1143. }
  1144. /* Clear ADDR flag after prepare the transfer parameters */
  1145. /* This action will generate an acknowledge to the HOST */
  1146. __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_ADDR);
  1147. /* Process Unlocked */
  1148. __HAL_UNLOCK(hsmbus);
  1149. /* Note : The SMBUS interrupts must be enabled after unlocking current process
  1150. to avoid the risk of SMBUS interrupt handle execution before current
  1151. process unlock */
  1152. /* REnable ADDR interrupt */
  1153. SMBUS_Enable_IRQ(hsmbus, SMBUS_IT_RX | SMBUS_IT_ADDR);
  1154. return HAL_OK;
  1155. }
  1156. else
  1157. {
  1158. return HAL_BUSY;
  1159. }
  1160. }
  1161. /**
  1162. * @brief Enable the Address listen mode with Interrupt.
  1163. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  1164. * the configuration information for the specified SMBUS.
  1165. * @retval HAL status
  1166. */
  1167. HAL_StatusTypeDef HAL_SMBUS_EnableListen_IT(SMBUS_HandleTypeDef *hsmbus)
  1168. {
  1169. hsmbus->State = HAL_SMBUS_STATE_LISTEN;
  1170. /* Enable the Address Match interrupt */
  1171. SMBUS_Enable_IRQ(hsmbus, SMBUS_IT_ADDR);
  1172. return HAL_OK;
  1173. }
  1174. /**
  1175. * @brief Disable the Address listen mode with Interrupt.
  1176. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  1177. * the configuration information for the specified SMBUS.
  1178. * @retval HAL status
  1179. */
  1180. HAL_StatusTypeDef HAL_SMBUS_DisableListen_IT(SMBUS_HandleTypeDef *hsmbus)
  1181. {
  1182. /* Disable Address listen mode only if a transfer is not ongoing */
  1183. if (hsmbus->State == HAL_SMBUS_STATE_LISTEN)
  1184. {
  1185. hsmbus->State = HAL_SMBUS_STATE_READY;
  1186. /* Disable the Address Match interrupt */
  1187. SMBUS_Disable_IRQ(hsmbus, SMBUS_IT_ADDR);
  1188. return HAL_OK;
  1189. }
  1190. else
  1191. {
  1192. return HAL_BUSY;
  1193. }
  1194. }
  1195. /**
  1196. * @brief Enable the SMBUS alert mode with Interrupt.
  1197. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  1198. * the configuration information for the specified SMBUSx peripheral.
  1199. * @retval HAL status
  1200. */
  1201. HAL_StatusTypeDef HAL_SMBUS_EnableAlert_IT(SMBUS_HandleTypeDef *hsmbus)
  1202. {
  1203. /* Enable SMBus alert */
  1204. hsmbus->Instance->CR1 |= I2C_CR1_ALERTEN;
  1205. /* Clear ALERT flag */
  1206. __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_ALERT);
  1207. /* Enable Alert Interrupt */
  1208. SMBUS_Enable_IRQ(hsmbus, SMBUS_IT_ALERT);
  1209. return HAL_OK;
  1210. }
  1211. /**
  1212. * @brief Disable the SMBUS alert mode with Interrupt.
  1213. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  1214. * the configuration information for the specified SMBUSx peripheral.
  1215. * @retval HAL status
  1216. */
  1217. HAL_StatusTypeDef HAL_SMBUS_DisableAlert_IT(SMBUS_HandleTypeDef *hsmbus)
  1218. {
  1219. /* Enable SMBus alert */
  1220. hsmbus->Instance->CR1 &= ~I2C_CR1_ALERTEN;
  1221. /* Disable Alert Interrupt */
  1222. SMBUS_Disable_IRQ(hsmbus, SMBUS_IT_ALERT);
  1223. return HAL_OK;
  1224. }
  1225. /**
  1226. * @brief Check if target device is ready for communication.
  1227. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  1228. * the configuration information for the specified SMBUS.
  1229. * @param DevAddress Target device address: The device 7 bits address value
  1230. * in datasheet must be shifted to the left before calling the interface
  1231. * @param Trials Number of trials
  1232. * @param Timeout Timeout duration
  1233. * @retval HAL status
  1234. */
  1235. HAL_StatusTypeDef HAL_SMBUS_IsDeviceReady(SMBUS_HandleTypeDef *hsmbus, uint16_t DevAddress, uint32_t Trials,
  1236. uint32_t Timeout)
  1237. {
  1238. uint32_t tickstart;
  1239. __IO uint32_t SMBUS_Trials = 0UL;
  1240. FlagStatus tmp1;
  1241. FlagStatus tmp2;
  1242. if (hsmbus->State == HAL_SMBUS_STATE_READY)
  1243. {
  1244. if (__HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_BUSY) != RESET)
  1245. {
  1246. return HAL_BUSY;
  1247. }
  1248. /* Process Locked */
  1249. __HAL_LOCK(hsmbus);
  1250. hsmbus->State = HAL_SMBUS_STATE_BUSY;
  1251. hsmbus->ErrorCode = HAL_SMBUS_ERROR_NONE;
  1252. do
  1253. {
  1254. /* Generate Start */
  1255. hsmbus->Instance->CR2 = SMBUS_GENERATE_START(hsmbus->Init.AddressingMode, DevAddress);
  1256. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  1257. /* Wait until STOPF flag is set or a NACK flag is set*/
  1258. tickstart = HAL_GetTick();
  1259. tmp1 = __HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_STOPF);
  1260. tmp2 = __HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_AF);
  1261. while ((tmp1 == RESET) && (tmp2 == RESET))
  1262. {
  1263. if (Timeout != HAL_MAX_DELAY)
  1264. {
  1265. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0UL))
  1266. {
  1267. /* Device is ready */
  1268. hsmbus->State = HAL_SMBUS_STATE_READY;
  1269. /* Update SMBUS error code */
  1270. hsmbus->ErrorCode |= HAL_SMBUS_ERROR_HALTIMEOUT;
  1271. /* Process Unlocked */
  1272. __HAL_UNLOCK(hsmbus);
  1273. return HAL_ERROR;
  1274. }
  1275. }
  1276. tmp1 = __HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_STOPF);
  1277. tmp2 = __HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_AF);
  1278. }
  1279. /* Check if the NACKF flag has not been set */
  1280. if (__HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_AF) == RESET)
  1281. {
  1282. /* Wait until STOPF flag is reset */
  1283. if (SMBUS_WaitOnFlagUntilTimeout(hsmbus, SMBUS_FLAG_STOPF, RESET, Timeout) != HAL_OK)
  1284. {
  1285. return HAL_ERROR;
  1286. }
  1287. /* Clear STOP Flag */
  1288. __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_STOPF);
  1289. /* Device is ready */
  1290. hsmbus->State = HAL_SMBUS_STATE_READY;
  1291. /* Process Unlocked */
  1292. __HAL_UNLOCK(hsmbus);
  1293. return HAL_OK;
  1294. }
  1295. else
  1296. {
  1297. /* Wait until STOPF flag is reset */
  1298. if (SMBUS_WaitOnFlagUntilTimeout(hsmbus, SMBUS_FLAG_STOPF, RESET, Timeout) != HAL_OK)
  1299. {
  1300. return HAL_ERROR;
  1301. }
  1302. /* Clear NACK Flag */
  1303. __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_AF);
  1304. /* Clear STOP Flag, auto generated with autoend*/
  1305. __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_STOPF);
  1306. }
  1307. /* Check if the maximum allowed number of trials has been reached */
  1308. if (SMBUS_Trials == Trials)
  1309. {
  1310. /* Generate Stop */
  1311. hsmbus->Instance->CR2 |= I2C_CR2_STOP;
  1312. /* Wait until STOPF flag is reset */
  1313. if (SMBUS_WaitOnFlagUntilTimeout(hsmbus, SMBUS_FLAG_STOPF, RESET, Timeout) != HAL_OK)
  1314. {
  1315. return HAL_ERROR;
  1316. }
  1317. /* Clear STOP Flag */
  1318. __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_STOPF);
  1319. }
  1320. /* Increment Trials */
  1321. SMBUS_Trials++;
  1322. } while (SMBUS_Trials < Trials);
  1323. hsmbus->State = HAL_SMBUS_STATE_READY;
  1324. /* Update SMBUS error code */
  1325. hsmbus->ErrorCode |= HAL_SMBUS_ERROR_HALTIMEOUT;
  1326. /* Process Unlocked */
  1327. __HAL_UNLOCK(hsmbus);
  1328. return HAL_ERROR;
  1329. }
  1330. else
  1331. {
  1332. return HAL_BUSY;
  1333. }
  1334. }
  1335. /**
  1336. * @}
  1337. */
  1338. /** @defgroup SMBUS_IRQ_Handler_and_Callbacks IRQ Handler and Callbacks
  1339. * @{
  1340. */
  1341. /**
  1342. * @brief Handle SMBUS event interrupt request.
  1343. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  1344. * the configuration information for the specified SMBUS.
  1345. * @retval None
  1346. */
  1347. void HAL_SMBUS_EV_IRQHandler(SMBUS_HandleTypeDef *hsmbus)
  1348. {
  1349. /* Use a local variable to store the current ISR flags */
  1350. /* This action will avoid a wrong treatment due to ISR flags change during interrupt handler */
  1351. uint32_t tmpisrvalue = READ_REG(hsmbus->Instance->ISR);
  1352. uint32_t tmpcr1value = READ_REG(hsmbus->Instance->CR1);
  1353. /* SMBUS in mode Transmitter ---------------------------------------------------*/
  1354. if ((SMBUS_CHECK_IT_SOURCE(tmpcr1value, (SMBUS_IT_TCI | SMBUS_IT_STOPI |
  1355. SMBUS_IT_NACKI | SMBUS_IT_TXI)) != RESET) &&
  1356. ((SMBUS_CHECK_FLAG(tmpisrvalue, SMBUS_FLAG_TXIS) != RESET) ||
  1357. (SMBUS_CHECK_FLAG(tmpisrvalue, SMBUS_FLAG_TCR) != RESET) ||
  1358. (SMBUS_CHECK_FLAG(tmpisrvalue, SMBUS_FLAG_TC) != RESET) ||
  1359. (SMBUS_CHECK_FLAG(tmpisrvalue, SMBUS_FLAG_STOPF) != RESET) ||
  1360. (SMBUS_CHECK_FLAG(tmpisrvalue, SMBUS_FLAG_AF) != RESET)))
  1361. {
  1362. /* Slave mode selected */
  1363. if ((hsmbus->State & HAL_SMBUS_STATE_SLAVE_BUSY_TX) == HAL_SMBUS_STATE_SLAVE_BUSY_TX)
  1364. {
  1365. (void)SMBUS_Slave_ISR(hsmbus, tmpisrvalue);
  1366. }
  1367. /* Master mode selected */
  1368. else if ((hsmbus->State & HAL_SMBUS_STATE_MASTER_BUSY_TX) == HAL_SMBUS_STATE_MASTER_BUSY_TX)
  1369. {
  1370. (void)SMBUS_Master_ISR(hsmbus, tmpisrvalue);
  1371. }
  1372. else
  1373. {
  1374. /* Nothing to do */
  1375. }
  1376. }
  1377. /* SMBUS in mode Receiver ----------------------------------------------------*/
  1378. if ((SMBUS_CHECK_IT_SOURCE(tmpcr1value, (SMBUS_IT_TCI | SMBUS_IT_STOPI |
  1379. SMBUS_IT_NACKI | SMBUS_IT_RXI)) != RESET) &&
  1380. ((SMBUS_CHECK_FLAG(tmpisrvalue, SMBUS_FLAG_RXNE) != RESET) ||
  1381. (SMBUS_CHECK_FLAG(tmpisrvalue, SMBUS_FLAG_TCR) != RESET) ||
  1382. (SMBUS_CHECK_FLAG(tmpisrvalue, SMBUS_FLAG_TC) != RESET) ||
  1383. (SMBUS_CHECK_FLAG(tmpisrvalue, SMBUS_FLAG_STOPF) != RESET) ||
  1384. (SMBUS_CHECK_FLAG(tmpisrvalue, SMBUS_FLAG_AF) != RESET)))
  1385. {
  1386. /* Slave mode selected */
  1387. if ((hsmbus->State & HAL_SMBUS_STATE_SLAVE_BUSY_RX) == HAL_SMBUS_STATE_SLAVE_BUSY_RX)
  1388. {
  1389. (void)SMBUS_Slave_ISR(hsmbus, tmpisrvalue);
  1390. }
  1391. /* Master mode selected */
  1392. else if ((hsmbus->State & HAL_SMBUS_STATE_MASTER_BUSY_RX) == HAL_SMBUS_STATE_MASTER_BUSY_RX)
  1393. {
  1394. (void)SMBUS_Master_ISR(hsmbus, tmpisrvalue);
  1395. }
  1396. else
  1397. {
  1398. /* Nothing to do */
  1399. }
  1400. }
  1401. /* SMBUS in mode Listener Only --------------------------------------------------*/
  1402. if (((SMBUS_CHECK_IT_SOURCE(tmpcr1value, SMBUS_IT_ADDRI) != RESET) ||
  1403. (SMBUS_CHECK_IT_SOURCE(tmpcr1value, SMBUS_IT_STOPI) != RESET) ||
  1404. (SMBUS_CHECK_IT_SOURCE(tmpcr1value, SMBUS_IT_NACKI) != RESET)) &&
  1405. ((SMBUS_CHECK_FLAG(tmpisrvalue, SMBUS_FLAG_ADDR) != RESET) ||
  1406. (SMBUS_CHECK_FLAG(tmpisrvalue, SMBUS_FLAG_STOPF) != RESET) ||
  1407. (SMBUS_CHECK_FLAG(tmpisrvalue, SMBUS_FLAG_AF) != RESET)))
  1408. {
  1409. if ((hsmbus->State & HAL_SMBUS_STATE_LISTEN) == HAL_SMBUS_STATE_LISTEN)
  1410. {
  1411. (void)SMBUS_Slave_ISR(hsmbus, tmpisrvalue);
  1412. }
  1413. }
  1414. }
  1415. /**
  1416. * @brief Handle SMBUS error interrupt request.
  1417. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  1418. * the configuration information for the specified SMBUS.
  1419. * @retval None
  1420. */
  1421. void HAL_SMBUS_ER_IRQHandler(SMBUS_HandleTypeDef *hsmbus)
  1422. {
  1423. SMBUS_ITErrorHandler(hsmbus);
  1424. }
  1425. /**
  1426. * @brief Master Tx Transfer completed callback.
  1427. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  1428. * the configuration information for the specified SMBUS.
  1429. * @retval None
  1430. */
  1431. __weak void HAL_SMBUS_MasterTxCpltCallback(SMBUS_HandleTypeDef *hsmbus)
  1432. {
  1433. /* Prevent unused argument(s) compilation warning */
  1434. UNUSED(hsmbus);
  1435. /* NOTE : This function should not be modified, when the callback is needed,
  1436. the HAL_SMBUS_MasterTxCpltCallback() could be implemented in the user file
  1437. */
  1438. }
  1439. /**
  1440. * @brief Master Rx Transfer completed callback.
  1441. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  1442. * the configuration information for the specified SMBUS.
  1443. * @retval None
  1444. */
  1445. __weak void HAL_SMBUS_MasterRxCpltCallback(SMBUS_HandleTypeDef *hsmbus)
  1446. {
  1447. /* Prevent unused argument(s) compilation warning */
  1448. UNUSED(hsmbus);
  1449. /* NOTE : This function should not be modified, when the callback is needed,
  1450. the HAL_SMBUS_MasterRxCpltCallback() could be implemented in the user file
  1451. */
  1452. }
  1453. /** @brief Slave Tx Transfer completed callback.
  1454. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  1455. * the configuration information for the specified SMBUS.
  1456. * @retval None
  1457. */
  1458. __weak void HAL_SMBUS_SlaveTxCpltCallback(SMBUS_HandleTypeDef *hsmbus)
  1459. {
  1460. /* Prevent unused argument(s) compilation warning */
  1461. UNUSED(hsmbus);
  1462. /* NOTE : This function should not be modified, when the callback is needed,
  1463. the HAL_SMBUS_SlaveTxCpltCallback() could be implemented in the user file
  1464. */
  1465. }
  1466. /**
  1467. * @brief Slave Rx Transfer completed callback.
  1468. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  1469. * the configuration information for the specified SMBUS.
  1470. * @retval None
  1471. */
  1472. __weak void HAL_SMBUS_SlaveRxCpltCallback(SMBUS_HandleTypeDef *hsmbus)
  1473. {
  1474. /* Prevent unused argument(s) compilation warning */
  1475. UNUSED(hsmbus);
  1476. /* NOTE : This function should not be modified, when the callback is needed,
  1477. the HAL_SMBUS_SlaveRxCpltCallback() could be implemented in the user file
  1478. */
  1479. }
  1480. /**
  1481. * @brief Slave Address Match callback.
  1482. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  1483. * the configuration information for the specified SMBUS.
  1484. * @param TransferDirection Master request Transfer Direction (Write/Read)
  1485. * @param AddrMatchCode Address Match Code
  1486. * @retval None
  1487. */
  1488. __weak void HAL_SMBUS_AddrCallback(SMBUS_HandleTypeDef *hsmbus, uint8_t TransferDirection,
  1489. uint16_t AddrMatchCode)
  1490. {
  1491. /* Prevent unused argument(s) compilation warning */
  1492. UNUSED(hsmbus);
  1493. UNUSED(TransferDirection);
  1494. UNUSED(AddrMatchCode);
  1495. /* NOTE : This function should not be modified, when the callback is needed,
  1496. the HAL_SMBUS_AddrCallback() could be implemented in the user file
  1497. */
  1498. }
  1499. /**
  1500. * @brief Listen Complete callback.
  1501. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  1502. * the configuration information for the specified SMBUS.
  1503. * @retval None
  1504. */
  1505. __weak void HAL_SMBUS_ListenCpltCallback(SMBUS_HandleTypeDef *hsmbus)
  1506. {
  1507. /* Prevent unused argument(s) compilation warning */
  1508. UNUSED(hsmbus);
  1509. /* NOTE : This function should not be modified, when the callback is needed,
  1510. the HAL_SMBUS_ListenCpltCallback() could be implemented in the user file
  1511. */
  1512. }
  1513. /**
  1514. * @brief SMBUS error callback.
  1515. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  1516. * the configuration information for the specified SMBUS.
  1517. * @retval None
  1518. */
  1519. __weak void HAL_SMBUS_ErrorCallback(SMBUS_HandleTypeDef *hsmbus)
  1520. {
  1521. /* Prevent unused argument(s) compilation warning */
  1522. UNUSED(hsmbus);
  1523. /* NOTE : This function should not be modified, when the callback is needed,
  1524. the HAL_SMBUS_ErrorCallback() could be implemented in the user file
  1525. */
  1526. }
  1527. /**
  1528. * @}
  1529. */
  1530. /** @defgroup SMBUS_Exported_Functions_Group3 Peripheral State and Errors functions
  1531. * @brief Peripheral State and Errors functions
  1532. *
  1533. @verbatim
  1534. ===============================================================================
  1535. ##### Peripheral State and Errors functions #####
  1536. ===============================================================================
  1537. [..]
  1538. This subsection permits to get in run-time the status of the peripheral
  1539. and the data flow.
  1540. @endverbatim
  1541. * @{
  1542. */
  1543. /**
  1544. * @brief Return the SMBUS handle state.
  1545. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  1546. * the configuration information for the specified SMBUS.
  1547. * @retval HAL state
  1548. */
  1549. uint32_t HAL_SMBUS_GetState(const SMBUS_HandleTypeDef *hsmbus)
  1550. {
  1551. /* Return SMBUS handle state */
  1552. return hsmbus->State;
  1553. }
  1554. /**
  1555. * @brief Return the SMBUS error code.
  1556. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  1557. * the configuration information for the specified SMBUS.
  1558. * @retval SMBUS Error Code
  1559. */
  1560. uint32_t HAL_SMBUS_GetError(const SMBUS_HandleTypeDef *hsmbus)
  1561. {
  1562. return hsmbus->ErrorCode;
  1563. }
  1564. /**
  1565. * @}
  1566. */
  1567. /**
  1568. * @}
  1569. */
  1570. /** @addtogroup SMBUS_Private_Functions SMBUS Private Functions
  1571. * @brief Data transfers Private functions
  1572. * @{
  1573. */
  1574. /**
  1575. * @brief Interrupt Sub-Routine which handle the Interrupt Flags Master Mode.
  1576. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  1577. * the configuration information for the specified SMBUS.
  1578. * @param StatusFlags Value of Interrupt Flags.
  1579. * @retval HAL status
  1580. */
  1581. static HAL_StatusTypeDef SMBUS_Master_ISR(SMBUS_HandleTypeDef *hsmbus, uint32_t StatusFlags)
  1582. {
  1583. uint16_t DevAddress;
  1584. /* Process Locked */
  1585. __HAL_LOCK(hsmbus);
  1586. if (SMBUS_CHECK_FLAG(StatusFlags, SMBUS_FLAG_AF) != RESET)
  1587. {
  1588. /* Clear NACK Flag */
  1589. __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_AF);
  1590. /* Set corresponding Error Code */
  1591. /* No need to generate STOP, it is automatically done */
  1592. hsmbus->ErrorCode |= HAL_SMBUS_ERROR_ACKF;
  1593. /* Flush TX register */
  1594. SMBUS_Flush_TXDR(hsmbus);
  1595. /* Process Unlocked */
  1596. __HAL_UNLOCK(hsmbus);
  1597. /* Call the Error callback to inform upper layer */
  1598. #if (USE_HAL_SMBUS_REGISTER_CALLBACKS == 1)
  1599. hsmbus->ErrorCallback(hsmbus);
  1600. #else
  1601. HAL_SMBUS_ErrorCallback(hsmbus);
  1602. #endif /* USE_HAL_SMBUS_REGISTER_CALLBACKS */
  1603. }
  1604. else if (SMBUS_CHECK_FLAG(StatusFlags, SMBUS_FLAG_STOPF) != RESET)
  1605. {
  1606. /* Check and treat errors if errors occurs during STOP process */
  1607. SMBUS_ITErrorHandler(hsmbus);
  1608. /* Call the corresponding callback to inform upper layer of End of Transfer */
  1609. if (hsmbus->State == HAL_SMBUS_STATE_MASTER_BUSY_TX)
  1610. {
  1611. /* Disable Interrupt */
  1612. SMBUS_Disable_IRQ(hsmbus, SMBUS_IT_TX);
  1613. /* Clear STOP Flag */
  1614. __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_STOPF);
  1615. /* Clear Configuration Register 2 */
  1616. SMBUS_RESET_CR2(hsmbus);
  1617. /* Flush remaining data in Fifo register in case of error occurs before TXEmpty */
  1618. /* Disable the selected SMBUS peripheral */
  1619. __HAL_SMBUS_DISABLE(hsmbus);
  1620. hsmbus->PreviousState = HAL_SMBUS_STATE_READY;
  1621. hsmbus->State = HAL_SMBUS_STATE_READY;
  1622. /* Process Unlocked */
  1623. __HAL_UNLOCK(hsmbus);
  1624. /* Re-enable the selected SMBUS peripheral */
  1625. __HAL_SMBUS_ENABLE(hsmbus);
  1626. /* Call the corresponding callback to inform upper layer of End of Transfer */
  1627. #if (USE_HAL_SMBUS_REGISTER_CALLBACKS == 1)
  1628. hsmbus->MasterTxCpltCallback(hsmbus);
  1629. #else
  1630. HAL_SMBUS_MasterTxCpltCallback(hsmbus);
  1631. #endif /* USE_HAL_SMBUS_REGISTER_CALLBACKS */
  1632. }
  1633. else if (hsmbus->State == HAL_SMBUS_STATE_MASTER_BUSY_RX)
  1634. {
  1635. /* Store Last receive data if any */
  1636. if (SMBUS_CHECK_FLAG(StatusFlags, SMBUS_FLAG_RXNE) != RESET)
  1637. {
  1638. /* Read data from RXDR */
  1639. *hsmbus->pBuffPtr = (uint8_t)(hsmbus->Instance->RXDR);
  1640. /* Increment Buffer pointer */
  1641. hsmbus->pBuffPtr++;
  1642. if ((hsmbus->XferSize > 0U))
  1643. {
  1644. hsmbus->XferSize--;
  1645. hsmbus->XferCount--;
  1646. }
  1647. }
  1648. /* Disable Interrupt */
  1649. SMBUS_Disable_IRQ(hsmbus, SMBUS_IT_RX);
  1650. /* Clear STOP Flag */
  1651. __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_STOPF);
  1652. /* Clear Configuration Register 2 */
  1653. SMBUS_RESET_CR2(hsmbus);
  1654. hsmbus->PreviousState = HAL_SMBUS_STATE_READY;
  1655. hsmbus->State = HAL_SMBUS_STATE_READY;
  1656. /* Process Unlocked */
  1657. __HAL_UNLOCK(hsmbus);
  1658. /* Call the corresponding callback to inform upper layer of End of Transfer */
  1659. #if (USE_HAL_SMBUS_REGISTER_CALLBACKS == 1)
  1660. hsmbus->MasterRxCpltCallback(hsmbus);
  1661. #else
  1662. HAL_SMBUS_MasterRxCpltCallback(hsmbus);
  1663. #endif /* USE_HAL_SMBUS_REGISTER_CALLBACKS */
  1664. }
  1665. else
  1666. {
  1667. /* Nothing to do */
  1668. }
  1669. }
  1670. else if (SMBUS_CHECK_FLAG(StatusFlags, SMBUS_FLAG_RXNE) != RESET)
  1671. {
  1672. /* Read data from RXDR */
  1673. *hsmbus->pBuffPtr = (uint8_t)(hsmbus->Instance->RXDR);
  1674. /* Increment Buffer pointer */
  1675. hsmbus->pBuffPtr++;
  1676. /* Increment Size counter */
  1677. hsmbus->XferSize--;
  1678. hsmbus->XferCount--;
  1679. }
  1680. else if (SMBUS_CHECK_FLAG(StatusFlags, SMBUS_FLAG_TXIS) != RESET)
  1681. {
  1682. /* Write data to TXDR */
  1683. hsmbus->Instance->TXDR = *hsmbus->pBuffPtr;
  1684. /* Increment Buffer pointer */
  1685. hsmbus->pBuffPtr++;
  1686. /* Increment Size counter */
  1687. hsmbus->XferSize--;
  1688. hsmbus->XferCount--;
  1689. }
  1690. else if (SMBUS_CHECK_FLAG(StatusFlags, SMBUS_FLAG_TCR) != RESET)
  1691. {
  1692. if ((hsmbus->XferCount != 0U) && (hsmbus->XferSize == 0U))
  1693. {
  1694. DevAddress = (uint16_t)(hsmbus->Instance->CR2 & I2C_CR2_SADD);
  1695. if (hsmbus->XferCount > MAX_NBYTE_SIZE)
  1696. {
  1697. SMBUS_TransferConfig(hsmbus, DevAddress, MAX_NBYTE_SIZE,
  1698. (SMBUS_RELOAD_MODE | (hsmbus->XferOptions & SMBUS_SENDPEC_MODE)),
  1699. SMBUS_NO_STARTSTOP);
  1700. hsmbus->XferSize = MAX_NBYTE_SIZE;
  1701. }
  1702. else
  1703. {
  1704. hsmbus->XferSize = hsmbus->XferCount;
  1705. SMBUS_TransferConfig(hsmbus, DevAddress, (uint8_t)hsmbus->XferSize, hsmbus->XferOptions,
  1706. SMBUS_NO_STARTSTOP);
  1707. /* If PEC mode is enable, size to transmit should be Size-1 byte, corresponding to PEC byte */
  1708. /* PEC byte is automatically sent by HW block, no need to manage it in Transmit process */
  1709. if (SMBUS_GET_PEC_MODE(hsmbus) != 0UL)
  1710. {
  1711. hsmbus->XferSize--;
  1712. hsmbus->XferCount--;
  1713. }
  1714. }
  1715. }
  1716. else if ((hsmbus->XferCount == 0U) && (hsmbus->XferSize == 0U))
  1717. {
  1718. /* Call TxCpltCallback() if no stop mode is set */
  1719. if (SMBUS_GET_STOP_MODE(hsmbus) != SMBUS_AUTOEND_MODE)
  1720. {
  1721. /* Call the corresponding callback to inform upper layer of End of Transfer */
  1722. if (hsmbus->State == HAL_SMBUS_STATE_MASTER_BUSY_TX)
  1723. {
  1724. /* Disable Interrupt */
  1725. SMBUS_Disable_IRQ(hsmbus, SMBUS_IT_TX);
  1726. hsmbus->PreviousState = hsmbus->State;
  1727. hsmbus->State = HAL_SMBUS_STATE_READY;
  1728. /* Process Unlocked */
  1729. __HAL_UNLOCK(hsmbus);
  1730. /* Call the corresponding callback to inform upper layer of End of Transfer */
  1731. #if (USE_HAL_SMBUS_REGISTER_CALLBACKS == 1)
  1732. hsmbus->MasterTxCpltCallback(hsmbus);
  1733. #else
  1734. HAL_SMBUS_MasterTxCpltCallback(hsmbus);
  1735. #endif /* USE_HAL_SMBUS_REGISTER_CALLBACKS */
  1736. }
  1737. else if (hsmbus->State == HAL_SMBUS_STATE_MASTER_BUSY_RX)
  1738. {
  1739. SMBUS_Disable_IRQ(hsmbus, SMBUS_IT_RX);
  1740. hsmbus->PreviousState = hsmbus->State;
  1741. hsmbus->State = HAL_SMBUS_STATE_READY;
  1742. /* Process Unlocked */
  1743. __HAL_UNLOCK(hsmbus);
  1744. /* Call the corresponding callback to inform upper layer of End of Transfer */
  1745. #if (USE_HAL_SMBUS_REGISTER_CALLBACKS == 1)
  1746. hsmbus->MasterRxCpltCallback(hsmbus);
  1747. #else
  1748. HAL_SMBUS_MasterRxCpltCallback(hsmbus);
  1749. #endif /* USE_HAL_SMBUS_REGISTER_CALLBACKS */
  1750. }
  1751. else
  1752. {
  1753. /* Nothing to do */
  1754. }
  1755. }
  1756. }
  1757. else
  1758. {
  1759. /* Nothing to do */
  1760. }
  1761. }
  1762. else if (SMBUS_CHECK_FLAG(StatusFlags, SMBUS_FLAG_TC) != RESET)
  1763. {
  1764. if (hsmbus->XferCount == 0U)
  1765. {
  1766. /* Specific use case for Quick command */
  1767. if (hsmbus->pBuffPtr == NULL)
  1768. {
  1769. /* Generate a Stop command */
  1770. hsmbus->Instance->CR2 |= I2C_CR2_STOP;
  1771. }
  1772. /* Call TxCpltCallback() if no stop mode is set */
  1773. else if (SMBUS_GET_STOP_MODE(hsmbus) != SMBUS_AUTOEND_MODE)
  1774. {
  1775. /* No Generate Stop, to permit restart mode */
  1776. /* The stop will be done at the end of transfer, when SMBUS_AUTOEND_MODE enable */
  1777. /* Call the corresponding callback to inform upper layer of End of Transfer */
  1778. if (hsmbus->State == HAL_SMBUS_STATE_MASTER_BUSY_TX)
  1779. {
  1780. /* Disable Interrupt */
  1781. SMBUS_Disable_IRQ(hsmbus, SMBUS_IT_TX);
  1782. hsmbus->PreviousState = hsmbus->State;
  1783. hsmbus->State = HAL_SMBUS_STATE_READY;
  1784. /* Process Unlocked */
  1785. __HAL_UNLOCK(hsmbus);
  1786. /* Call the corresponding callback to inform upper layer of End of Transfer */
  1787. #if (USE_HAL_SMBUS_REGISTER_CALLBACKS == 1)
  1788. hsmbus->MasterTxCpltCallback(hsmbus);
  1789. #else
  1790. HAL_SMBUS_MasterTxCpltCallback(hsmbus);
  1791. #endif /* USE_HAL_SMBUS_REGISTER_CALLBACKS */
  1792. }
  1793. else if (hsmbus->State == HAL_SMBUS_STATE_MASTER_BUSY_RX)
  1794. {
  1795. SMBUS_Disable_IRQ(hsmbus, SMBUS_IT_RX);
  1796. hsmbus->PreviousState = hsmbus->State;
  1797. hsmbus->State = HAL_SMBUS_STATE_READY;
  1798. /* Process Unlocked */
  1799. __HAL_UNLOCK(hsmbus);
  1800. /* Call the corresponding callback to inform upper layer of End of Transfer */
  1801. #if (USE_HAL_SMBUS_REGISTER_CALLBACKS == 1)
  1802. hsmbus->MasterRxCpltCallback(hsmbus);
  1803. #else
  1804. HAL_SMBUS_MasterRxCpltCallback(hsmbus);
  1805. #endif /* USE_HAL_SMBUS_REGISTER_CALLBACKS */
  1806. }
  1807. else
  1808. {
  1809. /* Nothing to do */
  1810. }
  1811. }
  1812. else
  1813. {
  1814. /* Nothing to do */
  1815. }
  1816. }
  1817. }
  1818. else
  1819. {
  1820. /* Nothing to do */
  1821. }
  1822. /* Process Unlocked */
  1823. __HAL_UNLOCK(hsmbus);
  1824. return HAL_OK;
  1825. }
  1826. /**
  1827. * @brief Interrupt Sub-Routine which handle the Interrupt Flags Slave Mode.
  1828. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  1829. * the configuration information for the specified SMBUS.
  1830. * @param StatusFlags Value of Interrupt Flags.
  1831. * @retval HAL status
  1832. */
  1833. static HAL_StatusTypeDef SMBUS_Slave_ISR(SMBUS_HandleTypeDef *hsmbus, uint32_t StatusFlags)
  1834. {
  1835. uint8_t TransferDirection;
  1836. uint16_t SlaveAddrCode;
  1837. /* Process Locked */
  1838. __HAL_LOCK(hsmbus);
  1839. if (SMBUS_CHECK_FLAG(StatusFlags, SMBUS_FLAG_AF) != RESET)
  1840. {
  1841. /* Check that SMBUS transfer finished */
  1842. /* if yes, normal usecase, a NACK is sent by the HOST when Transfer is finished */
  1843. /* Mean XferCount == 0*/
  1844. /* So clear Flag NACKF only */
  1845. if (hsmbus->XferCount == 0U)
  1846. {
  1847. /* Clear NACK Flag */
  1848. __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_AF);
  1849. /* Flush TX register */
  1850. SMBUS_Flush_TXDR(hsmbus);
  1851. /* Process Unlocked */
  1852. __HAL_UNLOCK(hsmbus);
  1853. }
  1854. else
  1855. {
  1856. /* if no, error usecase, a Non-Acknowledge of last Data is generated by the HOST*/
  1857. /* Clear NACK Flag */
  1858. __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_AF);
  1859. /* Set HAL State to "Idle" State, mean to LISTEN state */
  1860. /* So reset Slave Busy state */
  1861. hsmbus->PreviousState = hsmbus->State;
  1862. hsmbus->State &= ~((uint32_t)HAL_SMBUS_STATE_SLAVE_BUSY_TX);
  1863. hsmbus->State &= ~((uint32_t)HAL_SMBUS_STATE_SLAVE_BUSY_RX);
  1864. /* Disable RX/TX Interrupts, keep only ADDR Interrupt */
  1865. SMBUS_Disable_IRQ(hsmbus, SMBUS_IT_RX | SMBUS_IT_TX);
  1866. /* Set ErrorCode corresponding to a Non-Acknowledge */
  1867. hsmbus->ErrorCode |= HAL_SMBUS_ERROR_ACKF;
  1868. /* Flush TX register */
  1869. SMBUS_Flush_TXDR(hsmbus);
  1870. /* Process Unlocked */
  1871. __HAL_UNLOCK(hsmbus);
  1872. /* Call the Error callback to inform upper layer */
  1873. #if (USE_HAL_SMBUS_REGISTER_CALLBACKS == 1)
  1874. hsmbus->ErrorCallback(hsmbus);
  1875. #else
  1876. HAL_SMBUS_ErrorCallback(hsmbus);
  1877. #endif /* USE_HAL_SMBUS_REGISTER_CALLBACKS */
  1878. }
  1879. }
  1880. else if (SMBUS_CHECK_FLAG(StatusFlags, SMBUS_FLAG_ADDR) != RESET)
  1881. {
  1882. TransferDirection = (uint8_t)(SMBUS_GET_DIR(hsmbus));
  1883. SlaveAddrCode = (uint16_t)(SMBUS_GET_ADDR_MATCH(hsmbus));
  1884. /* Disable ADDR interrupt to prevent multiple ADDRInterrupt*/
  1885. /* Other ADDRInterrupt will be treat in next Listen usecase */
  1886. __HAL_SMBUS_DISABLE_IT(hsmbus, SMBUS_IT_ADDRI);
  1887. /* Process Unlocked */
  1888. __HAL_UNLOCK(hsmbus);
  1889. /* Call Slave Addr callback */
  1890. #if (USE_HAL_SMBUS_REGISTER_CALLBACKS == 1)
  1891. hsmbus->AddrCallback(hsmbus, TransferDirection, SlaveAddrCode);
  1892. #else
  1893. HAL_SMBUS_AddrCallback(hsmbus, TransferDirection, SlaveAddrCode);
  1894. #endif /* USE_HAL_SMBUS_REGISTER_CALLBACKS */
  1895. }
  1896. else if ((SMBUS_CHECK_FLAG(StatusFlags, SMBUS_FLAG_RXNE) != RESET) ||
  1897. (SMBUS_CHECK_FLAG(StatusFlags, SMBUS_FLAG_TCR) != RESET))
  1898. {
  1899. if ((hsmbus->State & HAL_SMBUS_STATE_SLAVE_BUSY_RX) == HAL_SMBUS_STATE_SLAVE_BUSY_RX)
  1900. {
  1901. /* Read data from RXDR */
  1902. *hsmbus->pBuffPtr = (uint8_t)(hsmbus->Instance->RXDR);
  1903. /* Increment Buffer pointer */
  1904. hsmbus->pBuffPtr++;
  1905. hsmbus->XferSize--;
  1906. hsmbus->XferCount--;
  1907. if (hsmbus->XferCount == 1U)
  1908. {
  1909. /* Receive last Byte, can be PEC byte in case of PEC BYTE enabled */
  1910. /* or only the last Byte of Transfer */
  1911. /* So reset the RELOAD bit mode */
  1912. hsmbus->XferOptions &= ~SMBUS_RELOAD_MODE;
  1913. SMBUS_TransferConfig(hsmbus, 0, 1, hsmbus->XferOptions, SMBUS_NO_STARTSTOP);
  1914. }
  1915. else if (hsmbus->XferCount == 0U)
  1916. {
  1917. /* Last Byte is received, disable Interrupt */
  1918. SMBUS_Disable_IRQ(hsmbus, SMBUS_IT_RX);
  1919. /* Remove HAL_SMBUS_STATE_SLAVE_BUSY_RX, keep only HAL_SMBUS_STATE_LISTEN */
  1920. hsmbus->PreviousState = hsmbus->State;
  1921. hsmbus->State &= ~((uint32_t)HAL_SMBUS_STATE_SLAVE_BUSY_RX);
  1922. /* Process Unlocked */
  1923. __HAL_UNLOCK(hsmbus);
  1924. /* Call the corresponding callback to inform upper layer of End of Transfer */
  1925. #if (USE_HAL_SMBUS_REGISTER_CALLBACKS == 1)
  1926. hsmbus->SlaveRxCpltCallback(hsmbus);
  1927. #else
  1928. HAL_SMBUS_SlaveRxCpltCallback(hsmbus);
  1929. #endif /* USE_HAL_SMBUS_REGISTER_CALLBACKS */
  1930. }
  1931. else
  1932. {
  1933. /* Set Reload for next Bytes */
  1934. SMBUS_TransferConfig(hsmbus, 0, 1,
  1935. SMBUS_RELOAD_MODE | (hsmbus->XferOptions & SMBUS_SENDPEC_MODE),
  1936. SMBUS_NO_STARTSTOP);
  1937. /* Ack last Byte Read */
  1938. hsmbus->Instance->CR2 &= ~I2C_CR2_NACK;
  1939. }
  1940. }
  1941. else if ((hsmbus->State & HAL_SMBUS_STATE_SLAVE_BUSY_TX) == HAL_SMBUS_STATE_SLAVE_BUSY_TX)
  1942. {
  1943. if ((hsmbus->XferCount != 0U) && (hsmbus->XferSize == 0U))
  1944. {
  1945. if (hsmbus->XferCount > MAX_NBYTE_SIZE)
  1946. {
  1947. SMBUS_TransferConfig(hsmbus, 0, MAX_NBYTE_SIZE,
  1948. (SMBUS_RELOAD_MODE | (hsmbus->XferOptions & SMBUS_SENDPEC_MODE)),
  1949. SMBUS_NO_STARTSTOP);
  1950. hsmbus->XferSize = MAX_NBYTE_SIZE;
  1951. }
  1952. else
  1953. {
  1954. hsmbus->XferSize = hsmbus->XferCount;
  1955. SMBUS_TransferConfig(hsmbus, 0, (uint8_t)hsmbus->XferSize, hsmbus->XferOptions,
  1956. SMBUS_NO_STARTSTOP);
  1957. /* If PEC mode is enable, size to transmit should be Size-1 byte, corresponding to PEC byte */
  1958. /* PEC byte is automatically sent by HW block, no need to manage it in Transmit process */
  1959. if (SMBUS_GET_PEC_MODE(hsmbus) != 0UL)
  1960. {
  1961. hsmbus->XferSize--;
  1962. hsmbus->XferCount--;
  1963. }
  1964. }
  1965. }
  1966. }
  1967. else
  1968. {
  1969. /* Nothing to do */
  1970. }
  1971. }
  1972. else if (SMBUS_CHECK_FLAG(StatusFlags, SMBUS_FLAG_TXIS) != RESET)
  1973. {
  1974. /* Write data to TXDR only if XferCount not reach "0" */
  1975. /* A TXIS flag can be set, during STOP treatment */
  1976. /* Check if all Data have already been sent */
  1977. /* If it is the case, this last write in TXDR is not sent, correspond to a dummy TXIS event */
  1978. if (hsmbus->XferCount > 0U)
  1979. {
  1980. /* Write data to TXDR */
  1981. hsmbus->Instance->TXDR = *hsmbus->pBuffPtr;
  1982. /* Increment Buffer pointer */
  1983. hsmbus->pBuffPtr++;
  1984. hsmbus->XferCount--;
  1985. hsmbus->XferSize--;
  1986. }
  1987. if (hsmbus->XferCount == 0U)
  1988. {
  1989. /* Last Byte is Transmitted */
  1990. /* Remove HAL_SMBUS_STATE_SLAVE_BUSY_TX, keep only HAL_SMBUS_STATE_LISTEN */
  1991. SMBUS_Disable_IRQ(hsmbus, SMBUS_IT_TX);
  1992. hsmbus->PreviousState = hsmbus->State;
  1993. hsmbus->State &= ~((uint32_t)HAL_SMBUS_STATE_SLAVE_BUSY_TX);
  1994. /* Process Unlocked */
  1995. __HAL_UNLOCK(hsmbus);
  1996. /* Call the corresponding callback to inform upper layer of End of Transfer */
  1997. #if (USE_HAL_SMBUS_REGISTER_CALLBACKS == 1)
  1998. hsmbus->SlaveTxCpltCallback(hsmbus);
  1999. #else
  2000. HAL_SMBUS_SlaveTxCpltCallback(hsmbus);
  2001. #endif /* USE_HAL_SMBUS_REGISTER_CALLBACKS */
  2002. }
  2003. }
  2004. else
  2005. {
  2006. /* Nothing to do */
  2007. }
  2008. /* Check if STOPF is set */
  2009. if (SMBUS_CHECK_FLAG(StatusFlags, SMBUS_FLAG_STOPF) != RESET)
  2010. {
  2011. if ((hsmbus->State & HAL_SMBUS_STATE_LISTEN) == HAL_SMBUS_STATE_LISTEN)
  2012. {
  2013. /* Store Last receive data if any */
  2014. if (__HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_RXNE) != RESET)
  2015. {
  2016. /* Read data from RXDR */
  2017. *hsmbus->pBuffPtr = (uint8_t)(hsmbus->Instance->RXDR);
  2018. /* Increment Buffer pointer */
  2019. hsmbus->pBuffPtr++;
  2020. if ((hsmbus->XferSize > 0U))
  2021. {
  2022. hsmbus->XferSize--;
  2023. hsmbus->XferCount--;
  2024. }
  2025. }
  2026. /* Disable RX and TX Interrupts */
  2027. SMBUS_Disable_IRQ(hsmbus, SMBUS_IT_RX | SMBUS_IT_TX);
  2028. /* Disable ADDR Interrupt */
  2029. SMBUS_Disable_IRQ(hsmbus, SMBUS_IT_ADDR);
  2030. /* Disable Address Acknowledge */
  2031. hsmbus->Instance->CR2 |= I2C_CR2_NACK;
  2032. /* Clear Configuration Register 2 */
  2033. SMBUS_RESET_CR2(hsmbus);
  2034. /* Clear STOP Flag */
  2035. __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_STOPF);
  2036. /* Clear ADDR flag */
  2037. __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_ADDR);
  2038. hsmbus->XferOptions = 0;
  2039. hsmbus->PreviousState = hsmbus->State;
  2040. hsmbus->State = HAL_SMBUS_STATE_READY;
  2041. /* Process Unlocked */
  2042. __HAL_UNLOCK(hsmbus);
  2043. /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */
  2044. #if (USE_HAL_SMBUS_REGISTER_CALLBACKS == 1)
  2045. hsmbus->ListenCpltCallback(hsmbus);
  2046. #else
  2047. HAL_SMBUS_ListenCpltCallback(hsmbus);
  2048. #endif /* USE_HAL_SMBUS_REGISTER_CALLBACKS */
  2049. }
  2050. }
  2051. /* Process Unlocked */
  2052. __HAL_UNLOCK(hsmbus);
  2053. return HAL_OK;
  2054. }
  2055. /**
  2056. * @brief Manage the enabling of Interrupts.
  2057. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  2058. * the configuration information for the specified SMBUS.
  2059. * @param InterruptRequest Value of @ref SMBUS_Interrupt_configuration_definition.
  2060. * @retval HAL status
  2061. */
  2062. static void SMBUS_Enable_IRQ(SMBUS_HandleTypeDef *hsmbus, uint32_t InterruptRequest)
  2063. {
  2064. uint32_t tmpisr = 0UL;
  2065. if ((InterruptRequest & SMBUS_IT_ALERT) == SMBUS_IT_ALERT)
  2066. {
  2067. /* Enable ERR interrupt */
  2068. tmpisr |= SMBUS_IT_ERRI;
  2069. }
  2070. if ((InterruptRequest & SMBUS_IT_ADDR) == SMBUS_IT_ADDR)
  2071. {
  2072. /* Enable ADDR, STOP interrupt */
  2073. tmpisr |= SMBUS_IT_ADDRI | SMBUS_IT_STOPI | SMBUS_IT_NACKI | SMBUS_IT_ERRI;
  2074. }
  2075. if ((InterruptRequest & SMBUS_IT_TX) == SMBUS_IT_TX)
  2076. {
  2077. /* Enable ERR, TC, STOP, NACK, RXI interrupt */
  2078. tmpisr |= SMBUS_IT_ERRI | SMBUS_IT_TCI | SMBUS_IT_STOPI | SMBUS_IT_NACKI | SMBUS_IT_TXI;
  2079. }
  2080. if ((InterruptRequest & SMBUS_IT_RX) == SMBUS_IT_RX)
  2081. {
  2082. /* Enable ERR, TC, STOP, NACK, TXI interrupt */
  2083. tmpisr |= SMBUS_IT_ERRI | SMBUS_IT_TCI | SMBUS_IT_STOPI | SMBUS_IT_NACKI | SMBUS_IT_RXI;
  2084. }
  2085. /* Enable interrupts only at the end */
  2086. /* to avoid the risk of SMBUS interrupt handle execution before */
  2087. /* all interrupts requested done */
  2088. __HAL_SMBUS_ENABLE_IT(hsmbus, tmpisr);
  2089. }
  2090. /**
  2091. * @brief Manage the disabling of Interrupts.
  2092. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  2093. * the configuration information for the specified SMBUS.
  2094. * @param InterruptRequest Value of @ref SMBUS_Interrupt_configuration_definition.
  2095. * @retval HAL status
  2096. */
  2097. static void SMBUS_Disable_IRQ(SMBUS_HandleTypeDef *hsmbus, uint32_t InterruptRequest)
  2098. {
  2099. uint32_t tmpisr = 0UL;
  2100. uint32_t tmpstate = hsmbus->State;
  2101. if ((tmpstate == HAL_SMBUS_STATE_READY) && ((InterruptRequest & SMBUS_IT_ALERT) == SMBUS_IT_ALERT))
  2102. {
  2103. /* Disable ERR interrupt */
  2104. tmpisr |= SMBUS_IT_ERRI;
  2105. }
  2106. if ((InterruptRequest & SMBUS_IT_TX) == SMBUS_IT_TX)
  2107. {
  2108. /* Disable TC, STOP, NACK and TXI interrupt */
  2109. tmpisr |= SMBUS_IT_TCI | SMBUS_IT_TXI;
  2110. if ((SMBUS_GET_ALERT_ENABLED(hsmbus) == 0UL)
  2111. && ((tmpstate & HAL_SMBUS_STATE_LISTEN) != HAL_SMBUS_STATE_LISTEN))
  2112. {
  2113. /* Disable ERR interrupt */
  2114. tmpisr |= SMBUS_IT_ERRI;
  2115. }
  2116. if ((tmpstate & HAL_SMBUS_STATE_LISTEN) != HAL_SMBUS_STATE_LISTEN)
  2117. {
  2118. /* Disable STOP and NACK interrupt */
  2119. tmpisr |= SMBUS_IT_STOPI | SMBUS_IT_NACKI;
  2120. }
  2121. }
  2122. if ((InterruptRequest & SMBUS_IT_RX) == SMBUS_IT_RX)
  2123. {
  2124. /* Disable TC, STOP, NACK and RXI interrupt */
  2125. tmpisr |= SMBUS_IT_TCI | SMBUS_IT_RXI;
  2126. if ((SMBUS_GET_ALERT_ENABLED(hsmbus) == 0UL)
  2127. && ((tmpstate & HAL_SMBUS_STATE_LISTEN) != HAL_SMBUS_STATE_LISTEN))
  2128. {
  2129. /* Disable ERR interrupt */
  2130. tmpisr |= SMBUS_IT_ERRI;
  2131. }
  2132. if ((tmpstate & HAL_SMBUS_STATE_LISTEN) != HAL_SMBUS_STATE_LISTEN)
  2133. {
  2134. /* Disable STOP and NACK interrupt */
  2135. tmpisr |= SMBUS_IT_STOPI | SMBUS_IT_NACKI;
  2136. }
  2137. }
  2138. if ((InterruptRequest & SMBUS_IT_ADDR) == SMBUS_IT_ADDR)
  2139. {
  2140. /* Disable ADDR, STOP and NACK interrupt */
  2141. tmpisr |= SMBUS_IT_ADDRI | SMBUS_IT_STOPI | SMBUS_IT_NACKI;
  2142. if (SMBUS_GET_ALERT_ENABLED(hsmbus) == 0UL)
  2143. {
  2144. /* Disable ERR interrupt */
  2145. tmpisr |= SMBUS_IT_ERRI;
  2146. }
  2147. }
  2148. /* Disable interrupts only at the end */
  2149. /* to avoid a breaking situation like at "t" time */
  2150. /* all disable interrupts request are not done */
  2151. __HAL_SMBUS_DISABLE_IT(hsmbus, tmpisr);
  2152. }
  2153. /**
  2154. * @brief SMBUS interrupts error handler.
  2155. * @param hsmbus SMBUS handle.
  2156. * @retval None
  2157. */
  2158. static void SMBUS_ITErrorHandler(SMBUS_HandleTypeDef *hsmbus)
  2159. {
  2160. uint32_t itflags = READ_REG(hsmbus->Instance->ISR);
  2161. uint32_t itsources = READ_REG(hsmbus->Instance->CR1);
  2162. uint32_t tmpstate;
  2163. uint32_t tmperror;
  2164. /* SMBUS Bus error interrupt occurred ------------------------------------*/
  2165. if (((itflags & SMBUS_FLAG_BERR) == SMBUS_FLAG_BERR) && \
  2166. ((itsources & SMBUS_IT_ERRI) == SMBUS_IT_ERRI))
  2167. {
  2168. hsmbus->ErrorCode |= HAL_SMBUS_ERROR_BERR;
  2169. /* Clear BERR flag */
  2170. __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_BERR);
  2171. }
  2172. /* SMBUS Over-Run/Under-Run interrupt occurred ----------------------------------------*/
  2173. if (((itflags & SMBUS_FLAG_OVR) == SMBUS_FLAG_OVR) && \
  2174. ((itsources & SMBUS_IT_ERRI) == SMBUS_IT_ERRI))
  2175. {
  2176. hsmbus->ErrorCode |= HAL_SMBUS_ERROR_OVR;
  2177. /* Clear OVR flag */
  2178. __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_OVR);
  2179. }
  2180. /* SMBUS Arbitration Loss error interrupt occurred ------------------------------------*/
  2181. if (((itflags & SMBUS_FLAG_ARLO) == SMBUS_FLAG_ARLO) && \
  2182. ((itsources & SMBUS_IT_ERRI) == SMBUS_IT_ERRI))
  2183. {
  2184. hsmbus->ErrorCode |= HAL_SMBUS_ERROR_ARLO;
  2185. /* Clear ARLO flag */
  2186. __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_ARLO);
  2187. }
  2188. /* SMBUS Timeout error interrupt occurred ---------------------------------------------*/
  2189. if (((itflags & SMBUS_FLAG_TIMEOUT) == SMBUS_FLAG_TIMEOUT) && \
  2190. ((itsources & SMBUS_IT_ERRI) == SMBUS_IT_ERRI))
  2191. {
  2192. hsmbus->ErrorCode |= HAL_SMBUS_ERROR_BUSTIMEOUT;
  2193. /* Clear TIMEOUT flag */
  2194. __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_TIMEOUT);
  2195. }
  2196. /* SMBUS Alert error interrupt occurred -----------------------------------------------*/
  2197. if (((itflags & SMBUS_FLAG_ALERT) == SMBUS_FLAG_ALERT) && \
  2198. ((itsources & SMBUS_IT_ERRI) == SMBUS_IT_ERRI))
  2199. {
  2200. hsmbus->ErrorCode |= HAL_SMBUS_ERROR_ALERT;
  2201. /* Clear ALERT flag */
  2202. __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_ALERT);
  2203. }
  2204. /* SMBUS Packet Error Check error interrupt occurred ----------------------------------*/
  2205. if (((itflags & SMBUS_FLAG_PECERR) == SMBUS_FLAG_PECERR) && \
  2206. ((itsources & SMBUS_IT_ERRI) == SMBUS_IT_ERRI))
  2207. {
  2208. hsmbus->ErrorCode |= HAL_SMBUS_ERROR_PECERR;
  2209. /* Clear PEC error flag */
  2210. __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_PECERR);
  2211. }
  2212. /* Flush TX register */
  2213. SMBUS_Flush_TXDR(hsmbus);
  2214. /* Store current volatile hsmbus->ErrorCode, misra rule */
  2215. tmperror = hsmbus->ErrorCode;
  2216. /* Call the Error Callback in case of Error detected */
  2217. if ((tmperror != HAL_SMBUS_ERROR_NONE) && (tmperror != HAL_SMBUS_ERROR_ACKF))
  2218. {
  2219. /* Do not Reset the HAL state in case of ALERT error */
  2220. if ((tmperror & HAL_SMBUS_ERROR_ALERT) != HAL_SMBUS_ERROR_ALERT)
  2221. {
  2222. /* Store current volatile hsmbus->State, misra rule */
  2223. tmpstate = hsmbus->State;
  2224. if (((tmpstate & HAL_SMBUS_STATE_SLAVE_BUSY_TX) == HAL_SMBUS_STATE_SLAVE_BUSY_TX)
  2225. || ((tmpstate & HAL_SMBUS_STATE_SLAVE_BUSY_RX) == HAL_SMBUS_STATE_SLAVE_BUSY_RX))
  2226. {
  2227. /* Reset only HAL_SMBUS_STATE_SLAVE_BUSY_XX */
  2228. /* keep HAL_SMBUS_STATE_LISTEN if set */
  2229. hsmbus->PreviousState = HAL_SMBUS_STATE_READY;
  2230. hsmbus->State = HAL_SMBUS_STATE_LISTEN;
  2231. }
  2232. }
  2233. /* Call the Error callback to inform upper layer */
  2234. #if (USE_HAL_SMBUS_REGISTER_CALLBACKS == 1)
  2235. hsmbus->ErrorCallback(hsmbus);
  2236. #else
  2237. HAL_SMBUS_ErrorCallback(hsmbus);
  2238. #endif /* USE_HAL_SMBUS_REGISTER_CALLBACKS */
  2239. }
  2240. }
  2241. /**
  2242. * @brief Handle SMBUS Communication Timeout.
  2243. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  2244. * the configuration information for the specified SMBUS.
  2245. * @param Flag Specifies the SMBUS flag to check.
  2246. * @param Status The new Flag status (SET or RESET).
  2247. * @param Timeout Timeout duration
  2248. * @retval HAL status
  2249. */
  2250. static HAL_StatusTypeDef SMBUS_WaitOnFlagUntilTimeout(SMBUS_HandleTypeDef *hsmbus, uint32_t Flag,
  2251. FlagStatus Status, uint32_t Timeout)
  2252. {
  2253. uint32_t tickstart = HAL_GetTick();
  2254. /* Wait until flag is set */
  2255. while ((FlagStatus)(__HAL_SMBUS_GET_FLAG(hsmbus, Flag)) == Status)
  2256. {
  2257. /* Check for the Timeout */
  2258. if (Timeout != HAL_MAX_DELAY)
  2259. {
  2260. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0UL))
  2261. {
  2262. hsmbus->PreviousState = hsmbus->State;
  2263. hsmbus->State = HAL_SMBUS_STATE_READY;
  2264. /* Update SMBUS error code */
  2265. hsmbus->ErrorCode |= HAL_SMBUS_ERROR_HALTIMEOUT;
  2266. /* Process Unlocked */
  2267. __HAL_UNLOCK(hsmbus);
  2268. return HAL_ERROR;
  2269. }
  2270. }
  2271. }
  2272. return HAL_OK;
  2273. }
  2274. /**
  2275. * @brief SMBUS Tx data register flush process.
  2276. * @param hsmbus SMBUS handle.
  2277. * @retval None
  2278. */
  2279. static void SMBUS_Flush_TXDR(SMBUS_HandleTypeDef *hsmbus)
  2280. {
  2281. /* If a pending TXIS flag is set */
  2282. /* Write a dummy data in TXDR to clear it */
  2283. if (__HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_TXIS) != RESET)
  2284. {
  2285. hsmbus->Instance->TXDR = 0x00U;
  2286. }
  2287. /* Flush TX register if not empty */
  2288. if (__HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_TXE) == RESET)
  2289. {
  2290. __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_TXE);
  2291. }
  2292. }
  2293. /**
  2294. * @brief Handle SMBUSx communication when starting transfer or during transfer (TC or TCR flag are set).
  2295. * @param hsmbus SMBUS handle.
  2296. * @param DevAddress specifies the slave address to be programmed.
  2297. * @param Size specifies the number of bytes to be programmed.
  2298. * This parameter must be a value between 0 and 255.
  2299. * @param Mode New state of the SMBUS START condition generation.
  2300. * This parameter can be one or a combination of the following values:
  2301. * @arg @ref SMBUS_RELOAD_MODE Enable Reload mode.
  2302. * @arg @ref SMBUS_AUTOEND_MODE Enable Automatic end mode.
  2303. * @arg @ref SMBUS_SOFTEND_MODE Enable Software end mode and Reload mode.
  2304. * @arg @ref SMBUS_SENDPEC_MODE Enable Packet Error Calculation mode.
  2305. * @param Request New state of the SMBUS START condition generation.
  2306. * This parameter can be one of the following values:
  2307. * @arg @ref SMBUS_NO_STARTSTOP Don't Generate stop and start condition.
  2308. * @arg @ref SMBUS_GENERATE_STOP Generate stop condition (Size should be set to 0).
  2309. * @arg @ref SMBUS_GENERATE_START_READ Generate Restart for read request.
  2310. * @arg @ref SMBUS_GENERATE_START_WRITE Generate Restart for write request.
  2311. * @retval None
  2312. */
  2313. static void SMBUS_TransferConfig(SMBUS_HandleTypeDef *hsmbus, uint16_t DevAddress, uint8_t Size,
  2314. uint32_t Mode, uint32_t Request)
  2315. {
  2316. /* Check the parameters */
  2317. assert_param(IS_SMBUS_ALL_INSTANCE(hsmbus->Instance));
  2318. assert_param(IS_SMBUS_TRANSFER_MODE(Mode));
  2319. assert_param(IS_SMBUS_TRANSFER_REQUEST(Request));
  2320. /* update CR2 register */
  2321. MODIFY_REG(hsmbus->Instance->CR2,
  2322. ((I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | \
  2323. (I2C_CR2_RD_WRN & (uint32_t)(Request >> (31UL - I2C_CR2_RD_WRN_Pos))) | \
  2324. I2C_CR2_START | I2C_CR2_STOP | I2C_CR2_PECBYTE)), \
  2325. (uint32_t)(((uint32_t)DevAddress & I2C_CR2_SADD) | \
  2326. (((uint32_t)Size << I2C_CR2_NBYTES_Pos) & I2C_CR2_NBYTES) | \
  2327. (uint32_t)Mode | (uint32_t)Request));
  2328. }
  2329. /**
  2330. * @brief Convert SMBUSx OTHER_xxx XferOptions to functional XferOptions.
  2331. * @param hsmbus SMBUS handle.
  2332. * @retval None
  2333. */
  2334. static void SMBUS_ConvertOtherXferOptions(SMBUS_HandleTypeDef *hsmbus)
  2335. {
  2336. /* if user set XferOptions to SMBUS_OTHER_FRAME_NO_PEC */
  2337. /* it request implicitly to generate a restart condition */
  2338. /* set XferOptions to SMBUS_FIRST_FRAME */
  2339. if (hsmbus->XferOptions == SMBUS_OTHER_FRAME_NO_PEC)
  2340. {
  2341. hsmbus->XferOptions = SMBUS_FIRST_FRAME;
  2342. }
  2343. /* else if user set XferOptions to SMBUS_OTHER_FRAME_WITH_PEC */
  2344. /* it request implicitly to generate a restart condition */
  2345. /* set XferOptions to SMBUS_FIRST_FRAME | SMBUS_SENDPEC_MODE */
  2346. else if (hsmbus->XferOptions == SMBUS_OTHER_FRAME_WITH_PEC)
  2347. {
  2348. hsmbus->XferOptions = SMBUS_FIRST_FRAME | SMBUS_SENDPEC_MODE;
  2349. }
  2350. /* else if user set XferOptions to SMBUS_OTHER_AND_LAST_FRAME_NO_PEC */
  2351. /* it request implicitly to generate a restart condition */
  2352. /* then generate a stop condition at the end of transfer */
  2353. /* set XferOptions to SMBUS_FIRST_AND_LAST_FRAME_NO_PEC */
  2354. else if (hsmbus->XferOptions == SMBUS_OTHER_AND_LAST_FRAME_NO_PEC)
  2355. {
  2356. hsmbus->XferOptions = SMBUS_FIRST_AND_LAST_FRAME_NO_PEC;
  2357. }
  2358. /* else if user set XferOptions to SMBUS_OTHER_AND_LAST_FRAME_WITH_PEC */
  2359. /* it request implicitly to generate a restart condition */
  2360. /* then generate a stop condition at the end of transfer */
  2361. /* set XferOptions to SMBUS_FIRST_AND_LAST_FRAME_WITH_PEC */
  2362. else if (hsmbus->XferOptions == SMBUS_OTHER_AND_LAST_FRAME_WITH_PEC)
  2363. {
  2364. hsmbus->XferOptions = SMBUS_FIRST_AND_LAST_FRAME_WITH_PEC;
  2365. }
  2366. else
  2367. {
  2368. /* Nothing to do */
  2369. }
  2370. }
  2371. /**
  2372. * @}
  2373. */
  2374. #endif /* HAL_SMBUS_MODULE_ENABLED */
  2375. /**
  2376. * @}
  2377. */
  2378. /**
  2379. * @}
  2380. */