stm32f4xx_hal_smbus.c 94 KB

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