stm32l4xx_hal_smbus.c 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_smbus.c
  4. * @author MCD Application Team
  5. * @brief SMBUS HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the System Management Bus (SMBus) peripheral,
  8. * based on I2C principles of operation :
  9. * + Initialization and de-initialization functions
  10. * + IO operation functions
  11. * + Peripheral State and Errors functions
  12. *
  13. @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 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 Clock Timing, Bus Timeout, Own Address1, Master Addressing mode,
  30. Dual Addressing mode, Own Address2, Own Address2 Mask, General call, Nostretch mode,
  31. Peripheral mode and Packet Error Check mode in the hsmbus Init structure.
  32. (#) Initialize the SMBUS registers by calling the HAL_SMBUS_Init() API:
  33. (++) These API's configures also the low level Hardware GPIO, CLOCK, CORTEX...etc)
  34. by calling the customized HAL_SMBUS_MspInit(&hsmbus) API.
  35. (#) To check if target device is ready for communication, use the function HAL_SMBUS_IsDeviceReady()
  36. (#) For SMBUS IO operations, only one mode of operations is available within this driver
  37. *** Interrupt mode IO operation ***
  38. ===================================
  39. [..]
  40. (+) Transmit in master/host SMBUS mode an amount of data in non-blocking mode using HAL_SMBUS_Master_Transmit_IT()
  41. (++) At transmission end of transfer HAL_SMBUS_MasterTxCpltCallback() is executed and user can
  42. add his own code by customization of function pointer HAL_SMBUS_MasterTxCpltCallback()
  43. (+) Receive in master/host SMBUS mode an amount of data in non-blocking mode using HAL_SMBUS_Master_Receive_IT()
  44. (++) At reception end of transfer HAL_SMBUS_MasterRxCpltCallback() is executed and user can
  45. add his own code by customization of function pointer HAL_SMBUS_MasterRxCpltCallback()
  46. (+) Abort a master/host SMBUS process communication with Interrupt using HAL_SMBUS_Master_Abort_IT()
  47. (++) The associated previous transfer callback is called at the end of abort process
  48. (++) mean HAL_SMBUS_MasterTxCpltCallback() in case of previous state was master transmit
  49. (++) mean HAL_SMBUS_MasterRxCpltCallback() in case of previous state was master receive
  50. (+) Enable/disable the Address listen mode in slave/device or host/slave SMBUS mode
  51. using HAL_SMBUS_EnableListen_IT() HAL_SMBUS_DisableListen_IT()
  52. (++) When address slave/device SMBUS match, HAL_SMBUS_AddrCallback() is executed and user can
  53. add his own code to check the Address Match Code and the transmission direction request by master/host (Write/Read).
  54. (++) At Listen mode end HAL_SMBUS_ListenCpltCallback() is executed and user can
  55. add his own code by customization of function pointer HAL_SMBUS_ListenCpltCallback()
  56. (+) Transmit in slave/device SMBUS mode an amount of data in non-blocking mode using HAL_SMBUS_Slave_Transmit_IT()
  57. (++) At transmission end of transfer HAL_SMBUS_SlaveTxCpltCallback() is executed and user can
  58. add his own code by customization of function pointer HAL_SMBUS_SlaveTxCpltCallback()
  59. (+) Receive in slave/device SMBUS mode an amount of data in non-blocking mode using HAL_SMBUS_Slave_Receive_IT()
  60. (++) At reception end of transfer HAL_SMBUS_SlaveRxCpltCallback() is executed and user can
  61. add his own code by customization of function pointer HAL_SMBUS_SlaveRxCpltCallback()
  62. (+) Enable/Disable the SMBUS alert mode using HAL_SMBUS_EnableAlert_IT() HAL_SMBUS_DisableAlert_IT()
  63. (++) When SMBUS Alert is generated HAL_SMBUS_ErrorCallback() is executed and user can
  64. add his own code by customization of function pointer HAL_SMBUS_ErrorCallback()
  65. to check the Alert Error Code using function HAL_SMBUS_GetError()
  66. (+) Get HAL state machine or error values using HAL_SMBUS_GetState() or HAL_SMBUS_GetError()
  67. (+) In case of transfer Error, HAL_SMBUS_ErrorCallback() function is executed and user can
  68. add his own code by customization of function pointer HAL_SMBUS_ErrorCallback()
  69. to check the Error Code using function HAL_SMBUS_GetError()
  70. *** SMBUS HAL driver macros list ***
  71. ==================================
  72. [..]
  73. Below the list of most used macros in SMBUS HAL driver.
  74. (+) __HAL_SMBUS_ENABLE: Enable the SMBUS peripheral
  75. (+) __HAL_SMBUS_DISABLE: Disable the SMBUS peripheral
  76. (+) __HAL_SMBUS_GET_FLAG: Check whether the specified SMBUS flag is set or not
  77. (+) __HAL_SMBUS_CLEAR_FLAG: Clear the specified SMBUS pending flag
  78. (+) __HAL_SMBUS_ENABLE_IT: Enable the specified SMBUS interrupt
  79. (+) __HAL_SMBUS_DISABLE_IT: Disable the specified SMBUS interrupt
  80. [..]
  81. (@) You can refer to the SMBUS HAL driver header file for more useful macros
  82. @endverbatim
  83. ******************************************************************************
  84. * @attention
  85. *
  86. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  87. *
  88. * Redistribution and use in source and binary forms, with or without modification,
  89. * are permitted provided that the following conditions are met:
  90. * 1. Redistributions of source code must retain the above copyright notice,
  91. * this list of conditions and the following disclaimer.
  92. * 2. Redistributions in binary form must reproduce the above copyright notice,
  93. * this list of conditions and the following disclaimer in the documentation
  94. * and/or other materials provided with the distribution.
  95. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  96. * may be used to endorse or promote products derived from this software
  97. * without specific prior written permission.
  98. *
  99. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  100. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  101. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  102. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  103. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  104. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  105. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  106. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  107. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  108. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  109. *
  110. ******************************************************************************
  111. */
  112. /* Includes ------------------------------------------------------------------*/
  113. #include "stm32l4xx_hal.h"
  114. /** @addtogroup STM32L4xx_HAL_Driver
  115. * @{
  116. */
  117. /** @defgroup SMBUS SMBUS
  118. * @brief SMBUS HAL module driver
  119. * @{
  120. */
  121. #ifdef HAL_SMBUS_MODULE_ENABLED
  122. /* Private typedef -----------------------------------------------------------*/
  123. /* Private constants ---------------------------------------------------------*/
  124. /** @defgroup SMBUS_Private_Define SMBUS Private Constants
  125. * @{
  126. */
  127. #define TIMING_CLEAR_MASK (0xF0FFFFFFU) /*!< SMBUS TIMING clear register Mask */
  128. #define HAL_TIMEOUT_ADDR (10000U) /*!< 10 s */
  129. #define HAL_TIMEOUT_BUSY (25U) /*!< 25 ms */
  130. #define HAL_TIMEOUT_DIR (25U) /*!< 25 ms */
  131. #define HAL_TIMEOUT_RXNE (25U) /*!< 25 ms */
  132. #define HAL_TIMEOUT_STOPF (25U) /*!< 25 ms */
  133. #define HAL_TIMEOUT_TC (25U) /*!< 25 ms */
  134. #define HAL_TIMEOUT_TCR (25U) /*!< 25 ms */
  135. #define HAL_TIMEOUT_TXIS (25U) /*!< 25 ms */
  136. #define MAX_NBYTE_SIZE 255U
  137. /**
  138. * @}
  139. */
  140. /* Private macro -------------------------------------------------------------*/
  141. /* Private variables ---------------------------------------------------------*/
  142. /* Private function prototypes -----------------------------------------------*/
  143. /** @addtogroup SMBUS_Private_Functions SMBUS Private Functions
  144. * @{
  145. */
  146. static HAL_StatusTypeDef SMBUS_WaitOnFlagUntilTimeout(SMBUS_HandleTypeDef *hsmbus, uint32_t Flag, FlagStatus Status, uint32_t Timeout);
  147. static HAL_StatusTypeDef SMBUS_Enable_IRQ(SMBUS_HandleTypeDef *hsmbus, uint16_t InterruptRequest);
  148. static HAL_StatusTypeDef SMBUS_Disable_IRQ(SMBUS_HandleTypeDef *hsmbus, uint16_t InterruptRequest);
  149. static HAL_StatusTypeDef SMBUS_Master_ISR(SMBUS_HandleTypeDef *hsmbus);
  150. static HAL_StatusTypeDef SMBUS_Slave_ISR(SMBUS_HandleTypeDef *hsmbus);
  151. static void SMBUS_ConvertOtherXferOptions(SMBUS_HandleTypeDef *hsmbus);
  152. static void SMBUS_ITErrorHandler(SMBUS_HandleTypeDef *hsmbus);
  153. static void SMBUS_TransferConfig(SMBUS_HandleTypeDef *hsmbus, uint16_t DevAddress, uint8_t Size, uint32_t Mode, uint32_t Request);
  154. /**
  155. * @}
  156. */
  157. /* Exported functions --------------------------------------------------------*/
  158. /** @defgroup SMBUS_Exported_Functions SMBUS Exported Functions
  159. * @{
  160. */
  161. /** @defgroup SMBUS_Exported_Functions_Group1 Initialization and de-initialization functions
  162. * @brief Initialization and Configuration functions
  163. *
  164. @verbatim
  165. ===============================================================================
  166. ##### Initialization and de-initialization functions #####
  167. ===============================================================================
  168. [..] This subsection provides a set of functions allowing to initialize and
  169. deinitialize the SMBUSx peripheral:
  170. (+) User must Implement HAL_SMBUS_MspInit() function in which he configures
  171. all related peripherals resources (CLOCK, GPIO, IT and NVIC ).
  172. (+) Call the function HAL_SMBUS_Init() to configure the selected device with
  173. the selected configuration:
  174. (++) Clock Timing
  175. (++) Bus Timeout
  176. (++) Analog Filer mode
  177. (++) Own Address 1
  178. (++) Addressing mode (Master, Slave)
  179. (++) Dual Addressing mode
  180. (++) Own Address 2
  181. (++) Own Address 2 Mask
  182. (++) General call mode
  183. (++) Nostretch mode
  184. (++) Packet Error Check mode
  185. (++) Peripheral mode
  186. (+) Call the function HAL_SMBUS_DeInit() to restore the default configuration
  187. of the selected SMBUSx peripheral.
  188. (+) Enable/Disable Analog/Digital filters with HAL_SMBUS_ConfigAnalogFilter() and
  189. HAL_SMBUS_ConfigDigitalFilter().
  190. @endverbatim
  191. * @{
  192. */
  193. /**
  194. * @brief Initialize the SMBUS according to the specified parameters
  195. * in the SMBUS_InitTypeDef and initialize the associated handle.
  196. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  197. * the configuration information for the specified SMBUS.
  198. * @retval HAL status
  199. */
  200. HAL_StatusTypeDef HAL_SMBUS_Init(SMBUS_HandleTypeDef *hsmbus)
  201. {
  202. /* Check the SMBUS handle allocation */
  203. if (hsmbus == NULL)
  204. {
  205. return HAL_ERROR;
  206. }
  207. /* Check the parameters */
  208. assert_param(IS_SMBUS_ALL_INSTANCE(hsmbus->Instance));
  209. assert_param(IS_SMBUS_ANALOG_FILTER(hsmbus->Init.AnalogFilter));
  210. assert_param(IS_SMBUS_OWN_ADDRESS1(hsmbus->Init.OwnAddress1));
  211. assert_param(IS_SMBUS_ADDRESSING_MODE(hsmbus->Init.AddressingMode));
  212. assert_param(IS_SMBUS_DUAL_ADDRESS(hsmbus->Init.DualAddressMode));
  213. assert_param(IS_SMBUS_OWN_ADDRESS2(hsmbus->Init.OwnAddress2));
  214. assert_param(IS_SMBUS_OWN_ADDRESS2_MASK(hsmbus->Init.OwnAddress2Masks));
  215. assert_param(IS_SMBUS_GENERAL_CALL(hsmbus->Init.GeneralCallMode));
  216. assert_param(IS_SMBUS_NO_STRETCH(hsmbus->Init.NoStretchMode));
  217. assert_param(IS_SMBUS_PEC(hsmbus->Init.PacketErrorCheckMode));
  218. assert_param(IS_SMBUS_PERIPHERAL_MODE(hsmbus->Init.PeripheralMode));
  219. if (hsmbus->State == HAL_SMBUS_STATE_RESET)
  220. {
  221. /* Allocate lock resource and initialize it */
  222. hsmbus->Lock = HAL_UNLOCKED;
  223. /* Init the low level hardware : GPIO, CLOCK, NVIC */
  224. HAL_SMBUS_MspInit(hsmbus);
  225. }
  226. hsmbus->State = HAL_SMBUS_STATE_BUSY;
  227. /* Disable the selected SMBUS peripheral */
  228. __HAL_SMBUS_DISABLE(hsmbus);
  229. /*---------------------------- SMBUSx TIMINGR Configuration ------------------------*/
  230. /* Configure SMBUSx: Frequency range */
  231. hsmbus->Instance->TIMINGR = hsmbus->Init.Timing & TIMING_CLEAR_MASK;
  232. /*---------------------------- SMBUSx TIMEOUTR Configuration ------------------------*/
  233. /* Configure SMBUSx: Bus Timeout */
  234. hsmbus->Instance->TIMEOUTR &= ~I2C_TIMEOUTR_TIMOUTEN;
  235. hsmbus->Instance->TIMEOUTR &= ~I2C_TIMEOUTR_TEXTEN;
  236. hsmbus->Instance->TIMEOUTR = hsmbus->Init.SMBusTimeout;
  237. /*---------------------------- SMBUSx OAR1 Configuration -----------------------*/
  238. /* Configure SMBUSx: Own Address1 and ack own address1 mode */
  239. hsmbus->Instance->OAR1 &= ~I2C_OAR1_OA1EN;
  240. if (hsmbus->Init.OwnAddress1 != 0U)
  241. {
  242. if (hsmbus->Init.AddressingMode == SMBUS_ADDRESSINGMODE_7BIT)
  243. {
  244. hsmbus->Instance->OAR1 = (I2C_OAR1_OA1EN | hsmbus->Init.OwnAddress1);
  245. }
  246. else /* SMBUS_ADDRESSINGMODE_10BIT */
  247. {
  248. hsmbus->Instance->OAR1 = (I2C_OAR1_OA1EN | I2C_OAR1_OA1MODE | hsmbus->Init.OwnAddress1);
  249. }
  250. }
  251. /*---------------------------- SMBUSx CR2 Configuration ------------------------*/
  252. /* Configure SMBUSx: Addressing Master mode */
  253. if (hsmbus->Init.AddressingMode == SMBUS_ADDRESSINGMODE_10BIT)
  254. {
  255. hsmbus->Instance->CR2 = (I2C_CR2_ADD10);
  256. }
  257. /* Enable the AUTOEND by default, and enable NACK (should be disable only during Slave process) */
  258. /* AUTOEND and NACK bit will be manage during Transfer process */
  259. hsmbus->Instance->CR2 |= (I2C_CR2_AUTOEND | I2C_CR2_NACK);
  260. /*---------------------------- SMBUSx OAR2 Configuration -----------------------*/
  261. /* Configure SMBUSx: Dual mode and Own Address2 */
  262. hsmbus->Instance->OAR2 = (hsmbus->Init.DualAddressMode | hsmbus->Init.OwnAddress2 | (hsmbus->Init.OwnAddress2Masks << 8U));
  263. /*---------------------------- SMBUSx CR1 Configuration ------------------------*/
  264. /* Configure SMBUSx: Generalcall and NoStretch mode */
  265. hsmbus->Instance->CR1 = (hsmbus->Init.GeneralCallMode | hsmbus->Init.NoStretchMode | hsmbus->Init.PacketErrorCheckMode | hsmbus->Init.PeripheralMode | hsmbus->Init.AnalogFilter);
  266. /* Enable Slave Byte Control only in case of Packet Error Check is enabled and SMBUS Peripheral is set in Slave mode */
  267. if ((hsmbus->Init.PacketErrorCheckMode == SMBUS_PEC_ENABLE)
  268. && ((hsmbus->Init.PeripheralMode == SMBUS_PERIPHERAL_MODE_SMBUS_SLAVE) || (hsmbus->Init.PeripheralMode == SMBUS_PERIPHERAL_MODE_SMBUS_SLAVE_ARP)))
  269. {
  270. hsmbus->Instance->CR1 |= I2C_CR1_SBC;
  271. }
  272. /* Enable the selected SMBUS peripheral */
  273. __HAL_SMBUS_ENABLE(hsmbus);
  274. hsmbus->ErrorCode = HAL_SMBUS_ERROR_NONE;
  275. hsmbus->PreviousState = HAL_SMBUS_STATE_READY;
  276. hsmbus->State = HAL_SMBUS_STATE_READY;
  277. return HAL_OK;
  278. }
  279. /**
  280. * @brief DeInitialize the SMBUS peripheral.
  281. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  282. * the configuration information for the specified SMBUS.
  283. * @retval HAL status
  284. */
  285. HAL_StatusTypeDef HAL_SMBUS_DeInit(SMBUS_HandleTypeDef *hsmbus)
  286. {
  287. /* Check the SMBUS handle allocation */
  288. if (hsmbus == NULL)
  289. {
  290. return HAL_ERROR;
  291. }
  292. /* Check the parameters */
  293. assert_param(IS_SMBUS_ALL_INSTANCE(hsmbus->Instance));
  294. hsmbus->State = HAL_SMBUS_STATE_BUSY;
  295. /* Disable the SMBUS Peripheral Clock */
  296. __HAL_SMBUS_DISABLE(hsmbus);
  297. /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
  298. HAL_SMBUS_MspDeInit(hsmbus);
  299. hsmbus->ErrorCode = HAL_SMBUS_ERROR_NONE;
  300. hsmbus->PreviousState = HAL_SMBUS_STATE_RESET;
  301. hsmbus->State = HAL_SMBUS_STATE_RESET;
  302. /* Release Lock */
  303. __HAL_UNLOCK(hsmbus);
  304. return HAL_OK;
  305. }
  306. /**
  307. * @brief Initialize the SMBUS MSP.
  308. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  309. * the configuration information for the specified SMBUS.
  310. * @retval None
  311. */
  312. __weak void HAL_SMBUS_MspInit(SMBUS_HandleTypeDef *hsmbus)
  313. {
  314. /* Prevent unused argument(s) compilation warning */
  315. UNUSED(hsmbus);
  316. /* NOTE : This function should not be modified, when the callback is needed,
  317. the HAL_SMBUS_MspInit could be implemented in the user file
  318. */
  319. }
  320. /**
  321. * @brief DeInitialize the SMBUS MSP.
  322. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  323. * the configuration information for the specified SMBUS.
  324. * @retval None
  325. */
  326. __weak void HAL_SMBUS_MspDeInit(SMBUS_HandleTypeDef *hsmbus)
  327. {
  328. /* Prevent unused argument(s) compilation warning */
  329. UNUSED(hsmbus);
  330. /* NOTE : This function should not be modified, when the callback is needed,
  331. the HAL_SMBUS_MspDeInit could be implemented in the user file
  332. */
  333. }
  334. /**
  335. * @brief Configure Analog noise filter.
  336. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  337. * the configuration information for the specified SMBUS.
  338. * @param AnalogFilter This parameter can be one of the following values:
  339. * @arg @ref SMBUS_ANALOGFILTER_ENABLE
  340. * @arg @ref SMBUS_ANALOGFILTER_DISABLE
  341. * @retval HAL status
  342. */
  343. HAL_StatusTypeDef HAL_SMBUS_ConfigAnalogFilter(SMBUS_HandleTypeDef *hsmbus, uint32_t AnalogFilter)
  344. {
  345. /* Check the parameters */
  346. assert_param(IS_SMBUS_ALL_INSTANCE(hsmbus->Instance));
  347. assert_param(IS_SMBUS_ANALOG_FILTER(AnalogFilter));
  348. if (hsmbus->State == HAL_SMBUS_STATE_READY)
  349. {
  350. /* Process Locked */
  351. __HAL_LOCK(hsmbus);
  352. hsmbus->State = HAL_SMBUS_STATE_BUSY;
  353. /* Disable the selected SMBUS peripheral */
  354. __HAL_SMBUS_DISABLE(hsmbus);
  355. /* Reset ANOFF bit */
  356. hsmbus->Instance->CR1 &= ~(I2C_CR1_ANFOFF);
  357. /* Set analog filter bit*/
  358. hsmbus->Instance->CR1 |= AnalogFilter;
  359. __HAL_SMBUS_ENABLE(hsmbus);
  360. hsmbus->State = HAL_SMBUS_STATE_READY;
  361. /* Process Unlocked */
  362. __HAL_UNLOCK(hsmbus);
  363. return HAL_OK;
  364. }
  365. else
  366. {
  367. return HAL_BUSY;
  368. }
  369. }
  370. /**
  371. * @brief Configure Digital noise filter.
  372. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  373. * the configuration information for the specified SMBUS.
  374. * @param DigitalFilter Coefficient of digital noise filter between Min_Data=0x00 and Max_Data=0x0F.
  375. * @retval HAL status
  376. */
  377. HAL_StatusTypeDef HAL_SMBUS_ConfigDigitalFilter(SMBUS_HandleTypeDef *hsmbus, uint32_t DigitalFilter)
  378. {
  379. uint32_t tmpreg = 0U;
  380. /* Check the parameters */
  381. assert_param(IS_SMBUS_ALL_INSTANCE(hsmbus->Instance));
  382. assert_param(IS_SMBUS_DIGITAL_FILTER(DigitalFilter));
  383. if (hsmbus->State == HAL_SMBUS_STATE_READY)
  384. {
  385. /* Process Locked */
  386. __HAL_LOCK(hsmbus);
  387. hsmbus->State = HAL_SMBUS_STATE_BUSY;
  388. /* Disable the selected SMBUS peripheral */
  389. __HAL_SMBUS_DISABLE(hsmbus);
  390. /* Get the old register value */
  391. tmpreg = hsmbus->Instance->CR1;
  392. /* Reset I2C DNF bits [11:8] */
  393. tmpreg &= ~(I2C_CR1_DNF);
  394. /* Set I2Cx DNF coefficient */
  395. tmpreg |= DigitalFilter << I2C_CR1_DNF_Pos;
  396. /* Store the new register value */
  397. hsmbus->Instance->CR1 = tmpreg;
  398. __HAL_SMBUS_ENABLE(hsmbus);
  399. hsmbus->State = HAL_SMBUS_STATE_READY;
  400. /* Process Unlocked */
  401. __HAL_UNLOCK(hsmbus);
  402. return HAL_OK;
  403. }
  404. else
  405. {
  406. return HAL_BUSY;
  407. }
  408. }
  409. /**
  410. * @}
  411. */
  412. /** @defgroup SMBUS_Exported_Functions_Group2 Input and Output operation functions
  413. * @brief Data transfers functions
  414. *
  415. @verbatim
  416. ===============================================================================
  417. ##### IO operation functions #####
  418. ===============================================================================
  419. [..]
  420. This subsection provides a set of functions allowing to manage the SMBUS data
  421. transfers.
  422. (#) Blocking mode function to check if device is ready for usage is :
  423. (++) HAL_SMBUS_IsDeviceReady()
  424. (#) There is only one mode of transfer:
  425. (++) Non-Blocking mode : The communication is performed using Interrupts.
  426. These functions return the status of the transfer startup.
  427. The end of the data processing will be indicated through the
  428. dedicated SMBUS IRQ when using Interrupt mode.
  429. (#) Non-Blocking mode functions with Interrupt are :
  430. (++) HAL_SMBUS_Master_Transmit_IT()
  431. (++) HAL_SMBUS_Master_Receive_IT()
  432. (++) HAL_SMBUS_Slave_Transmit_IT()
  433. (++) HAL_SMBUS_Slave_Receive_IT()
  434. (++) HAL_SMBUS_EnableListen_IT() or alias HAL_SMBUS_EnableListen_IT()
  435. (++) HAL_SMBUS_DisableListen_IT()
  436. (++) HAL_SMBUS_EnableAlert_IT()
  437. (++) HAL_SMBUS_DisableAlert_IT()
  438. (#) A set of Transfer Complete Callbacks are provided in non-Blocking mode:
  439. (++) HAL_SMBUS_MasterTxCpltCallback()
  440. (++) HAL_SMBUS_MasterRxCpltCallback()
  441. (++) HAL_SMBUS_SlaveTxCpltCallback()
  442. (++) HAL_SMBUS_SlaveRxCpltCallback()
  443. (++) HAL_SMBUS_AddrCallback()
  444. (++) HAL_SMBUS_ListenCpltCallback()
  445. (++) HAL_SMBUS_ErrorCallback()
  446. @endverbatim
  447. * @{
  448. */
  449. /**
  450. * @brief Transmit in master/host SMBUS mode an amount of data in non-blocking mode with Interrupt.
  451. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  452. * the configuration information for the specified SMBUS.
  453. * @param DevAddress Target device address: The device 7 bits address value
  454. * in datasheet must be shift at right before call interface
  455. * @param pData Pointer to data buffer
  456. * @param Size Amount of data to be sent
  457. * @param XferOptions Options of Transfer, value of @ref SMBUS_XferOptions_definition
  458. * @retval HAL status
  459. */
  460. HAL_StatusTypeDef HAL_SMBUS_Master_Transmit_IT(SMBUS_HandleTypeDef *hsmbus, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
  461. {
  462. /* Check the parameters */
  463. assert_param(IS_SMBUS_TRANSFER_OPTIONS_REQUEST(XferOptions));
  464. if (hsmbus->State == HAL_SMBUS_STATE_READY)
  465. {
  466. /* Process Locked */
  467. __HAL_LOCK(hsmbus);
  468. hsmbus->State = HAL_SMBUS_STATE_MASTER_BUSY_TX;
  469. hsmbus->ErrorCode = HAL_SMBUS_ERROR_NONE;
  470. /* Prepare transfer parameters */
  471. hsmbus->pBuffPtr = pData;
  472. hsmbus->XferCount = Size;
  473. hsmbus->XferOptions = XferOptions;
  474. /* In case of Quick command, remove autoend mode */
  475. /* Manage the stop generation by software */
  476. if (hsmbus->pBuffPtr == NULL)
  477. {
  478. hsmbus->XferOptions &= ~SMBUS_AUTOEND_MODE;
  479. }
  480. if (Size > MAX_NBYTE_SIZE)
  481. {
  482. hsmbus->XferSize = MAX_NBYTE_SIZE;
  483. }
  484. else
  485. {
  486. hsmbus->XferSize = Size;
  487. }
  488. /* Send Slave Address */
  489. /* Set NBYTES to write and reload if size > MAX_NBYTE_SIZE and generate RESTART */
  490. if ((hsmbus->XferSize == MAX_NBYTE_SIZE) && (hsmbus->XferSize < hsmbus->XferCount))
  491. {
  492. SMBUS_TransferConfig(hsmbus, DevAddress, hsmbus->XferSize, SMBUS_RELOAD_MODE | (hsmbus->XferOptions & SMBUS_SENDPEC_MODE), SMBUS_GENERATE_START_WRITE);
  493. }
  494. else
  495. {
  496. /* If transfer direction not change, do not generate Restart Condition */
  497. /* Mean Previous state is same as current state */
  498. if ((hsmbus->PreviousState == HAL_SMBUS_STATE_MASTER_BUSY_TX) && (IS_SMBUS_TRANSFER_OTHER_OPTIONS_REQUEST(hsmbus->XferOptions) == 0))
  499. {
  500. SMBUS_TransferConfig(hsmbus, DevAddress, hsmbus->XferSize, hsmbus->XferOptions, SMBUS_NO_STARTSTOP);
  501. }
  502. /* Else transfer direction change, so generate Restart with new transfer direction */
  503. else
  504. {
  505. /* Convert OTHER_xxx XferOptions if any */
  506. SMBUS_ConvertOtherXferOptions(hsmbus);
  507. /* Handle Transfer */
  508. SMBUS_TransferConfig(hsmbus, DevAddress, hsmbus->XferSize, hsmbus->XferOptions, SMBUS_GENERATE_START_WRITE);
  509. }
  510. /* If PEC mode is enable, size to transmit manage by SW part should be Size-1 byte, corresponding to PEC byte */
  511. /* PEC byte is automatically sent by HW block, no need to manage it in Transmit process */
  512. if (SMBUS_GET_PEC_MODE(hsmbus) != RESET)
  513. {
  514. hsmbus->XferSize--;
  515. hsmbus->XferCount--;
  516. }
  517. }
  518. /* Process Unlocked */
  519. __HAL_UNLOCK(hsmbus);
  520. /* Note : The SMBUS interrupts must be enabled after unlocking current process
  521. to avoid the risk of SMBUS interrupt handle execution before current
  522. process unlock */
  523. SMBUS_Enable_IRQ(hsmbus, SMBUS_IT_TX);
  524. return HAL_OK;
  525. }
  526. else
  527. {
  528. return HAL_BUSY;
  529. }
  530. }
  531. /**
  532. * @brief Receive in master/host SMBUS mode an amount of data in non-blocking mode with Interrupt.
  533. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  534. * the configuration information for the specified SMBUS.
  535. * @param DevAddress Target device address: The device 7 bits address value
  536. * in datasheet must be shift at right before call interface
  537. * @param pData Pointer to data buffer
  538. * @param Size Amount of data to be sent
  539. * @param XferOptions Options of Transfer, value of @ref SMBUS_XferOptions_definition
  540. * @retval HAL status
  541. */
  542. HAL_StatusTypeDef HAL_SMBUS_Master_Receive_IT(SMBUS_HandleTypeDef *hsmbus, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
  543. {
  544. /* Check the parameters */
  545. assert_param(IS_SMBUS_TRANSFER_OPTIONS_REQUEST(XferOptions));
  546. if (hsmbus->State == HAL_SMBUS_STATE_READY)
  547. {
  548. /* Process Locked */
  549. __HAL_LOCK(hsmbus);
  550. hsmbus->State = HAL_SMBUS_STATE_MASTER_BUSY_RX;
  551. hsmbus->ErrorCode = HAL_SMBUS_ERROR_NONE;
  552. /* Prepare transfer parameters */
  553. hsmbus->pBuffPtr = pData;
  554. hsmbus->XferCount = Size;
  555. hsmbus->XferOptions = XferOptions;
  556. /* In case of Quick command, remove autoend mode */
  557. /* Manage the stop generation by software */
  558. if (hsmbus->pBuffPtr == NULL)
  559. {
  560. hsmbus->XferOptions &= ~SMBUS_AUTOEND_MODE;
  561. }
  562. if (Size > MAX_NBYTE_SIZE)
  563. {
  564. hsmbus->XferSize = MAX_NBYTE_SIZE;
  565. }
  566. else
  567. {
  568. hsmbus->XferSize = Size;
  569. }
  570. /* Send Slave Address */
  571. /* Set NBYTES to write and reload if size > MAX_NBYTE_SIZE and generate RESTART */
  572. if ((hsmbus->XferSize == MAX_NBYTE_SIZE) && (hsmbus->XferSize < hsmbus->XferCount))
  573. {
  574. SMBUS_TransferConfig(hsmbus, DevAddress, hsmbus->XferSize, SMBUS_RELOAD_MODE | (hsmbus->XferOptions & SMBUS_SENDPEC_MODE), SMBUS_GENERATE_START_READ);
  575. }
  576. else
  577. {
  578. /* If transfer direction not change, do not generate Restart Condition */
  579. /* Mean Previous state is same as current state */
  580. if ((hsmbus->PreviousState == HAL_SMBUS_STATE_MASTER_BUSY_RX) && (IS_SMBUS_TRANSFER_OTHER_OPTIONS_REQUEST(hsmbus->XferOptions) == 0))
  581. {
  582. SMBUS_TransferConfig(hsmbus, DevAddress, hsmbus->XferSize, hsmbus->XferOptions, SMBUS_NO_STARTSTOP);
  583. }
  584. /* Else transfer direction change, so generate Restart with new transfer direction */
  585. else
  586. {
  587. /* Convert OTHER_xxx XferOptions if any */
  588. SMBUS_ConvertOtherXferOptions(hsmbus);
  589. /* Handle Transfer */
  590. SMBUS_TransferConfig(hsmbus, DevAddress, hsmbus->XferSize, hsmbus->XferOptions, SMBUS_GENERATE_START_READ);
  591. }
  592. }
  593. /* Process Unlocked */
  594. __HAL_UNLOCK(hsmbus);
  595. /* Note : The SMBUS interrupts must be enabled after unlocking current process
  596. to avoid the risk of SMBUS interrupt handle execution before current
  597. process unlock */
  598. SMBUS_Enable_IRQ(hsmbus, SMBUS_IT_RX);
  599. return HAL_OK;
  600. }
  601. else
  602. {
  603. return HAL_BUSY;
  604. }
  605. }
  606. /**
  607. * @brief Abort a master/host SMBUS process communication with Interrupt.
  608. * @note This abort can be called only if state is ready
  609. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  610. * the configuration information for the specified SMBUS.
  611. * @param DevAddress Target device address: The device 7 bits address value
  612. * in datasheet must be shift at right before call interface
  613. * @retval HAL status
  614. */
  615. HAL_StatusTypeDef HAL_SMBUS_Master_Abort_IT(SMBUS_HandleTypeDef *hsmbus, uint16_t DevAddress)
  616. {
  617. if (hsmbus->State == HAL_SMBUS_STATE_READY)
  618. {
  619. /* Process Locked */
  620. __HAL_LOCK(hsmbus);
  621. /* Keep the same state as previous */
  622. /* to perform as well the call of the corresponding end of transfer callback */
  623. if (hsmbus->PreviousState == HAL_SMBUS_STATE_MASTER_BUSY_TX)
  624. {
  625. hsmbus->State = HAL_SMBUS_STATE_MASTER_BUSY_TX;
  626. }
  627. else if (hsmbus->PreviousState == HAL_SMBUS_STATE_MASTER_BUSY_RX)
  628. {
  629. hsmbus->State = HAL_SMBUS_STATE_MASTER_BUSY_RX;
  630. }
  631. else
  632. {
  633. /* Wrong usage of abort function */
  634. /* This function should be used only in case of abort monitored by master device */
  635. return HAL_ERROR;
  636. }
  637. hsmbus->ErrorCode = HAL_SMBUS_ERROR_NONE;
  638. /* Set NBYTES to 1 to generate a dummy read on SMBUS peripheral */
  639. /* Set AUTOEND mode, this will generate a NACK then STOP condition to abort the current transfer */
  640. SMBUS_TransferConfig(hsmbus, DevAddress, 1U, SMBUS_AUTOEND_MODE, SMBUS_NO_STARTSTOP);
  641. /* Process Unlocked */
  642. __HAL_UNLOCK(hsmbus);
  643. /* Note : The SMBUS interrupts must be enabled after unlocking current process
  644. to avoid the risk of SMBUS interrupt handle execution before current
  645. process unlock */
  646. if (hsmbus->State == HAL_SMBUS_STATE_MASTER_BUSY_TX)
  647. {
  648. SMBUS_Enable_IRQ(hsmbus, SMBUS_IT_TX);
  649. }
  650. else if (hsmbus->State == HAL_SMBUS_STATE_MASTER_BUSY_RX)
  651. {
  652. SMBUS_Enable_IRQ(hsmbus, SMBUS_IT_RX);
  653. }
  654. return HAL_OK;
  655. }
  656. else
  657. {
  658. return HAL_BUSY;
  659. }
  660. }
  661. /**
  662. * @brief Transmit in slave/device SMBUS mode an amount of data in non-blocking mode with Interrupt.
  663. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  664. * the configuration information for the specified SMBUS.
  665. * @param pData Pointer to data buffer
  666. * @param Size Amount of data to be sent
  667. * @param XferOptions Options of Transfer, value of @ref SMBUS_XferOptions_definition
  668. * @retval HAL status
  669. */
  670. HAL_StatusTypeDef HAL_SMBUS_Slave_Transmit_IT(SMBUS_HandleTypeDef *hsmbus, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
  671. {
  672. /* Check the parameters */
  673. assert_param(IS_SMBUS_TRANSFER_OPTIONS_REQUEST(XferOptions));
  674. if (hsmbus->State == HAL_SMBUS_STATE_LISTEN)
  675. {
  676. if ((pData == NULL) || (Size == 0U))
  677. {
  678. return HAL_ERROR;
  679. }
  680. /* Disable Interrupts, to prevent preemption during treatment in case of multicall */
  681. SMBUS_Disable_IRQ(hsmbus, SMBUS_IT_ADDR | SMBUS_IT_TX);
  682. /* Process Locked */
  683. __HAL_LOCK(hsmbus);
  684. hsmbus->State |= HAL_SMBUS_STATE_SLAVE_BUSY_TX;
  685. hsmbus->ErrorCode = HAL_SMBUS_ERROR_NONE;
  686. /* Set SBC bit to manage Acknowledge at each bit */
  687. hsmbus->Instance->CR1 |= I2C_CR1_SBC;
  688. /* Enable Address Acknowledge */
  689. hsmbus->Instance->CR2 &= ~I2C_CR2_NACK;
  690. /* Prepare transfer parameters */
  691. hsmbus->pBuffPtr = pData;
  692. hsmbus->XferCount = Size;
  693. hsmbus->XferOptions = XferOptions;
  694. /* Convert OTHER_xxx XferOptions if any */
  695. SMBUS_ConvertOtherXferOptions(hsmbus);
  696. if (Size > MAX_NBYTE_SIZE)
  697. {
  698. hsmbus->XferSize = MAX_NBYTE_SIZE;
  699. }
  700. else
  701. {
  702. hsmbus->XferSize = Size;
  703. }
  704. /* Set NBYTES to write and reload if size > MAX_NBYTE_SIZE and generate RESTART */
  705. if ((hsmbus->XferSize == MAX_NBYTE_SIZE) && (hsmbus->XferSize < hsmbus->XferCount))
  706. {
  707. SMBUS_TransferConfig(hsmbus, 0U, hsmbus->XferSize, SMBUS_RELOAD_MODE | (hsmbus->XferOptions & SMBUS_SENDPEC_MODE), SMBUS_NO_STARTSTOP);
  708. }
  709. else
  710. {
  711. /* Set NBYTE to transmit */
  712. SMBUS_TransferConfig(hsmbus, 0U, hsmbus->XferSize, hsmbus->XferOptions, SMBUS_NO_STARTSTOP);
  713. /* If PEC mode is enable, size to transmit should be Size-1 byte, corresponding to PEC byte */
  714. /* PEC byte is automatically sent by HW block, no need to manage it in Transmit process */
  715. if (SMBUS_GET_PEC_MODE(hsmbus) != RESET)
  716. {
  717. hsmbus->XferSize--;
  718. hsmbus->XferCount--;
  719. }
  720. }
  721. /* Clear ADDR flag after prepare the transfer parameters */
  722. /* This action will generate an acknowledge to the HOST */
  723. __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_ADDR);
  724. /* Process Unlocked */
  725. __HAL_UNLOCK(hsmbus);
  726. /* Note : The SMBUS interrupts must be enabled after unlocking current process
  727. to avoid the risk of SMBUS interrupt handle execution before current
  728. process unlock */
  729. /* REnable ADDR interrupt */
  730. SMBUS_Enable_IRQ(hsmbus, SMBUS_IT_TX | SMBUS_IT_ADDR);
  731. return HAL_OK;
  732. }
  733. else
  734. {
  735. return HAL_ERROR;
  736. }
  737. }
  738. /**
  739. * @brief Receive in slave/device SMBUS mode an amount of data in non-blocking mode with Interrupt.
  740. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  741. * the configuration information for the specified SMBUS.
  742. * @param pData Pointer to data buffer
  743. * @param Size Amount of data to be sent
  744. * @param XferOptions Options of Transfer, value of @ref SMBUS_XferOptions_definition
  745. * @retval HAL status
  746. */
  747. HAL_StatusTypeDef HAL_SMBUS_Slave_Receive_IT(SMBUS_HandleTypeDef *hsmbus, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
  748. {
  749. /* Check the parameters */
  750. assert_param(IS_SMBUS_TRANSFER_OPTIONS_REQUEST(XferOptions));
  751. if (hsmbus->State == HAL_SMBUS_STATE_LISTEN)
  752. {
  753. if ((pData == NULL) || (Size == 0U))
  754. {
  755. return HAL_ERROR;
  756. }
  757. /* Disable Interrupts, to prevent preemption during treatment in case of multicall */
  758. SMBUS_Disable_IRQ(hsmbus, SMBUS_IT_ADDR | SMBUS_IT_RX);
  759. /* Process Locked */
  760. __HAL_LOCK(hsmbus);
  761. hsmbus->State |= HAL_SMBUS_STATE_SLAVE_BUSY_RX;
  762. hsmbus->ErrorCode = HAL_SMBUS_ERROR_NONE;
  763. /* Set SBC bit to manage Acknowledge at each bit */
  764. hsmbus->Instance->CR1 |= I2C_CR1_SBC;
  765. /* Enable Address Acknowledge */
  766. hsmbus->Instance->CR2 &= ~I2C_CR2_NACK;
  767. /* Prepare transfer parameters */
  768. hsmbus->pBuffPtr = pData;
  769. hsmbus->XferSize = Size;
  770. hsmbus->XferCount = Size;
  771. hsmbus->XferOptions = XferOptions;
  772. /* Convert OTHER_xxx XferOptions if any */
  773. SMBUS_ConvertOtherXferOptions(hsmbus);
  774. /* Set NBYTE to receive */
  775. /* If XferSize equal "1", or XferSize equal "2" with PEC requested (mean 1 data byte + 1 PEC byte */
  776. /* no need to set RELOAD bit mode, a ACK will be automatically generated in that case */
  777. /* else need to set RELOAD bit mode to generate an automatic ACK at each byte Received */
  778. /* This RELOAD bit will be reset for last BYTE to be receive in SMBUS_Slave_ISR */
  779. if ((hsmbus->XferSize == 1U) || ((hsmbus->XferSize == 2U) && (SMBUS_GET_PEC_MODE(hsmbus) != RESET)))
  780. {
  781. SMBUS_TransferConfig(hsmbus, 0U, hsmbus->XferSize, hsmbus->XferOptions, SMBUS_NO_STARTSTOP);
  782. }
  783. else
  784. {
  785. SMBUS_TransferConfig(hsmbus, 0U, 1U, hsmbus->XferOptions | SMBUS_RELOAD_MODE, SMBUS_NO_STARTSTOP);
  786. }
  787. /* Clear ADDR flag after prepare the transfer parameters */
  788. /* This action will generate an acknowledge to the HOST */
  789. __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_ADDR);
  790. /* Process Unlocked */
  791. __HAL_UNLOCK(hsmbus);
  792. /* Note : The SMBUS interrupts must be enabled after unlocking current process
  793. to avoid the risk of SMBUS interrupt handle execution before current
  794. process unlock */
  795. /* REnable ADDR interrupt */
  796. SMBUS_Enable_IRQ(hsmbus, SMBUS_IT_RX | SMBUS_IT_ADDR);
  797. return HAL_OK;
  798. }
  799. else
  800. {
  801. return HAL_ERROR;
  802. }
  803. }
  804. /**
  805. * @brief Enable the Address listen mode with Interrupt.
  806. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  807. * the configuration information for the specified SMBUS.
  808. * @retval HAL status
  809. */
  810. HAL_StatusTypeDef HAL_SMBUS_EnableListen_IT(SMBUS_HandleTypeDef *hsmbus)
  811. {
  812. hsmbus->State = HAL_SMBUS_STATE_LISTEN;
  813. /* Enable the Address Match interrupt */
  814. SMBUS_Enable_IRQ(hsmbus, SMBUS_IT_ADDR);
  815. return HAL_OK;
  816. }
  817. /**
  818. * @brief Disable the Address listen mode with Interrupt.
  819. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  820. * the configuration information for the specified SMBUS.
  821. * @retval HAL status
  822. */
  823. HAL_StatusTypeDef HAL_SMBUS_DisableListen_IT(SMBUS_HandleTypeDef *hsmbus)
  824. {
  825. /* Disable Address listen mode only if a transfer is not ongoing */
  826. if (hsmbus->State == HAL_SMBUS_STATE_LISTEN)
  827. {
  828. hsmbus->State = HAL_SMBUS_STATE_READY;
  829. /* Disable the Address Match interrupt */
  830. SMBUS_Disable_IRQ(hsmbus, SMBUS_IT_ADDR);
  831. return HAL_OK;
  832. }
  833. else
  834. {
  835. return HAL_BUSY;
  836. }
  837. }
  838. /**
  839. * @brief Enable the SMBUS alert mode with Interrupt.
  840. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  841. * the configuration information for the specified SMBUSx peripheral.
  842. * @retval HAL status
  843. */
  844. HAL_StatusTypeDef HAL_SMBUS_EnableAlert_IT(SMBUS_HandleTypeDef *hsmbus)
  845. {
  846. /* Enable SMBus alert */
  847. hsmbus->Instance->CR1 |= I2C_CR1_ALERTEN;
  848. /* Clear ALERT flag */
  849. __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_ALERT);
  850. /* Enable Alert Interrupt */
  851. SMBUS_Enable_IRQ(hsmbus, SMBUS_IT_ALERT);
  852. return HAL_OK;
  853. }
  854. /**
  855. * @brief Disable the SMBUS alert mode with Interrupt.
  856. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  857. * the configuration information for the specified SMBUSx peripheral.
  858. * @retval HAL status
  859. */
  860. HAL_StatusTypeDef HAL_SMBUS_DisableAlert_IT(SMBUS_HandleTypeDef *hsmbus)
  861. {
  862. /* Enable SMBus alert */
  863. hsmbus->Instance->CR1 &= ~I2C_CR1_ALERTEN;
  864. /* Disable Alert Interrupt */
  865. SMBUS_Disable_IRQ(hsmbus, SMBUS_IT_ALERT);
  866. return HAL_OK;
  867. }
  868. /**
  869. * @brief Check if target device is ready for communication.
  870. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  871. * the configuration information for the specified SMBUS.
  872. * @param DevAddress Target device address: The device 7 bits address value
  873. * in datasheet must be shift at right before call interface
  874. * @param Trials Number of trials
  875. * @param Timeout Timeout duration
  876. * @retval HAL status
  877. */
  878. HAL_StatusTypeDef HAL_SMBUS_IsDeviceReady(SMBUS_HandleTypeDef *hsmbus, uint16_t DevAddress, uint32_t Trials, uint32_t Timeout)
  879. {
  880. uint32_t tickstart = 0U;
  881. __IO uint32_t SMBUS_Trials = 0U;
  882. if (hsmbus->State == HAL_SMBUS_STATE_READY)
  883. {
  884. if (__HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_BUSY) != RESET)
  885. {
  886. return HAL_BUSY;
  887. }
  888. /* Process Locked */
  889. __HAL_LOCK(hsmbus);
  890. hsmbus->State = HAL_SMBUS_STATE_BUSY;
  891. hsmbus->ErrorCode = HAL_SMBUS_ERROR_NONE;
  892. do
  893. {
  894. /* Generate Start */
  895. hsmbus->Instance->CR2 = SMBUS_GENERATE_START(hsmbus->Init.AddressingMode, DevAddress);
  896. /* No need to Check TC flag, with AUTOEND mode the stop is automatically generated */
  897. /* Wait until STOPF flag is set or a NACK flag is set*/
  898. tickstart = HAL_GetTick();
  899. while ((__HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_STOPF) == RESET) && (__HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_AF) == RESET) && (hsmbus->State != HAL_SMBUS_STATE_TIMEOUT))
  900. {
  901. if (Timeout != HAL_MAX_DELAY)
  902. {
  903. if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
  904. {
  905. /* Device is ready */
  906. hsmbus->State = HAL_SMBUS_STATE_READY;
  907. /* Process Unlocked */
  908. __HAL_UNLOCK(hsmbus);
  909. return HAL_TIMEOUT;
  910. }
  911. }
  912. }
  913. /* Check if the NACKF flag has not been set */
  914. if (__HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_AF) == RESET)
  915. {
  916. /* Wait until STOPF flag is reset */
  917. if (SMBUS_WaitOnFlagUntilTimeout(hsmbus, SMBUS_FLAG_STOPF, RESET, Timeout) != HAL_OK)
  918. {
  919. return HAL_TIMEOUT;
  920. }
  921. /* Clear STOP Flag */
  922. __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_STOPF);
  923. /* Device is ready */
  924. hsmbus->State = HAL_SMBUS_STATE_READY;
  925. /* Process Unlocked */
  926. __HAL_UNLOCK(hsmbus);
  927. return HAL_OK;
  928. }
  929. else
  930. {
  931. /* Wait until STOPF flag is reset */
  932. if (SMBUS_WaitOnFlagUntilTimeout(hsmbus, SMBUS_FLAG_STOPF, RESET, Timeout) != HAL_OK)
  933. {
  934. return HAL_TIMEOUT;
  935. }
  936. /* Clear NACK Flag */
  937. __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_AF);
  938. /* Clear STOP Flag, auto generated with autoend*/
  939. __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_STOPF);
  940. }
  941. /* Check if the maximum allowed number of trials has been reached */
  942. if (SMBUS_Trials++ == Trials)
  943. {
  944. /* Generate Stop */
  945. hsmbus->Instance->CR2 |= I2C_CR2_STOP;
  946. /* Wait until STOPF flag is reset */
  947. if (SMBUS_WaitOnFlagUntilTimeout(hsmbus, SMBUS_FLAG_STOPF, RESET, Timeout) != HAL_OK)
  948. {
  949. return HAL_TIMEOUT;
  950. }
  951. /* Clear STOP Flag */
  952. __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_STOPF);
  953. }
  954. }
  955. while (SMBUS_Trials < Trials);
  956. hsmbus->State = HAL_SMBUS_STATE_READY;
  957. /* Process Unlocked */
  958. __HAL_UNLOCK(hsmbus);
  959. return HAL_TIMEOUT;
  960. }
  961. else
  962. {
  963. return HAL_BUSY;
  964. }
  965. }
  966. /**
  967. * @}
  968. */
  969. /** @defgroup SMBUS_IRQ_Handler_and_Callbacks IRQ Handler and Callbacks
  970. * @{
  971. */
  972. /**
  973. * @brief Handle SMBUS event interrupt request.
  974. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  975. * the configuration information for the specified SMBUS.
  976. * @retval None
  977. */
  978. void HAL_SMBUS_EV_IRQHandler(SMBUS_HandleTypeDef *hsmbus)
  979. {
  980. uint32_t tmpisrvalue = 0U;
  981. /* Use a local variable to store the current ISR flags */
  982. /* This action will avoid a wrong treatment due to ISR flags change during interrupt handler */
  983. tmpisrvalue = SMBUS_GET_ISR_REG(hsmbus);
  984. /* SMBUS in mode Transmitter ---------------------------------------------------*/
  985. if (((SMBUS_CHECK_FLAG(tmpisrvalue, SMBUS_FLAG_TXIS) != RESET) || (SMBUS_CHECK_FLAG(tmpisrvalue, SMBUS_FLAG_TCR) != RESET) || (SMBUS_CHECK_FLAG(tmpisrvalue, SMBUS_FLAG_TC) != RESET) || (SMBUS_CHECK_FLAG(tmpisrvalue, SMBUS_FLAG_STOPF) != RESET) || (SMBUS_CHECK_FLAG(tmpisrvalue, SMBUS_FLAG_AF) != RESET)) && (__HAL_SMBUS_GET_IT_SOURCE(hsmbus, (SMBUS_IT_TCI | SMBUS_IT_STOPI | SMBUS_IT_NACKI | SMBUS_IT_TXI)) != RESET))
  986. {
  987. /* Slave mode selected */
  988. if ((hsmbus->State & HAL_SMBUS_STATE_SLAVE_BUSY_TX) == HAL_SMBUS_STATE_SLAVE_BUSY_TX)
  989. {
  990. SMBUS_Slave_ISR(hsmbus);
  991. }
  992. /* Master mode selected */
  993. else if ((hsmbus->State & HAL_SMBUS_STATE_MASTER_BUSY_TX) == HAL_SMBUS_STATE_MASTER_BUSY_TX)
  994. {
  995. SMBUS_Master_ISR(hsmbus);
  996. }
  997. }
  998. /* SMBUS in mode Receiver ----------------------------------------------------*/
  999. if (((SMBUS_CHECK_FLAG(tmpisrvalue, SMBUS_FLAG_RXNE) != RESET) || (SMBUS_CHECK_FLAG(tmpisrvalue, SMBUS_FLAG_TCR) != RESET) || (SMBUS_CHECK_FLAG(tmpisrvalue, SMBUS_FLAG_TC) != RESET) || (SMBUS_CHECK_FLAG(tmpisrvalue, SMBUS_FLAG_STOPF) != RESET) || (SMBUS_CHECK_FLAG(tmpisrvalue, SMBUS_FLAG_AF) != RESET)) && (__HAL_SMBUS_GET_IT_SOURCE(hsmbus, (SMBUS_IT_TCI | SMBUS_IT_STOPI | SMBUS_IT_NACKI | SMBUS_IT_RXI)) != RESET))
  1000. {
  1001. /* Slave mode selected */
  1002. if ((hsmbus->State & HAL_SMBUS_STATE_SLAVE_BUSY_RX) == HAL_SMBUS_STATE_SLAVE_BUSY_RX)
  1003. {
  1004. SMBUS_Slave_ISR(hsmbus);
  1005. }
  1006. /* Master mode selected */
  1007. else if ((hsmbus->State & HAL_SMBUS_STATE_MASTER_BUSY_RX) == HAL_SMBUS_STATE_MASTER_BUSY_RX)
  1008. {
  1009. SMBUS_Master_ISR(hsmbus);
  1010. }
  1011. }
  1012. /* SMBUS in mode Listener Only --------------------------------------------------*/
  1013. if (((SMBUS_CHECK_FLAG(tmpisrvalue, SMBUS_FLAG_ADDR) != RESET) || (SMBUS_CHECK_FLAG(tmpisrvalue, SMBUS_FLAG_STOPF) != RESET) || (SMBUS_CHECK_FLAG(tmpisrvalue, SMBUS_FLAG_AF) != RESET))
  1014. && ((__HAL_SMBUS_GET_IT_SOURCE(hsmbus, SMBUS_IT_ADDRI) != RESET) || (__HAL_SMBUS_GET_IT_SOURCE(hsmbus, SMBUS_IT_STOPI) != RESET) || (__HAL_SMBUS_GET_IT_SOURCE(hsmbus, SMBUS_IT_NACKI) != RESET)))
  1015. {
  1016. if (hsmbus->State == HAL_SMBUS_STATE_LISTEN)
  1017. {
  1018. SMBUS_Slave_ISR(hsmbus);
  1019. }
  1020. }
  1021. }
  1022. /**
  1023. * @brief Handle SMBUS error interrupt request.
  1024. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  1025. * the configuration information for the specified SMBUS.
  1026. * @retval None
  1027. */
  1028. void HAL_SMBUS_ER_IRQHandler(SMBUS_HandleTypeDef *hsmbus)
  1029. {
  1030. SMBUS_ITErrorHandler(hsmbus);
  1031. }
  1032. /**
  1033. * @brief Master Tx Transfer completed callback.
  1034. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  1035. * the configuration information for the specified SMBUS.
  1036. * @retval None
  1037. */
  1038. __weak void HAL_SMBUS_MasterTxCpltCallback(SMBUS_HandleTypeDef *hsmbus)
  1039. {
  1040. /* Prevent unused argument(s) compilation warning */
  1041. UNUSED(hsmbus);
  1042. /* NOTE : This function should not be modified, when the callback is needed,
  1043. the HAL_SMBUS_MasterTxCpltCallback() could be implemented in the user file
  1044. */
  1045. }
  1046. /**
  1047. * @brief Master Rx Transfer completed callback.
  1048. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  1049. * the configuration information for the specified SMBUS.
  1050. * @retval None
  1051. */
  1052. __weak void HAL_SMBUS_MasterRxCpltCallback(SMBUS_HandleTypeDef *hsmbus)
  1053. {
  1054. /* Prevent unused argument(s) compilation warning */
  1055. UNUSED(hsmbus);
  1056. /* NOTE : This function should not be modified, when the callback is needed,
  1057. the HAL_SMBUS_MasterRxCpltCallback() could be implemented in the user file
  1058. */
  1059. }
  1060. /** @brief Slave Tx Transfer completed callback.
  1061. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  1062. * the configuration information for the specified SMBUS.
  1063. * @retval None
  1064. */
  1065. __weak void HAL_SMBUS_SlaveTxCpltCallback(SMBUS_HandleTypeDef *hsmbus)
  1066. {
  1067. /* Prevent unused argument(s) compilation warning */
  1068. UNUSED(hsmbus);
  1069. /* NOTE : This function should not be modified, when the callback is needed,
  1070. the HAL_SMBUS_SlaveTxCpltCallback() could be implemented in the user file
  1071. */
  1072. }
  1073. /**
  1074. * @brief Slave Rx Transfer completed callback.
  1075. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  1076. * the configuration information for the specified SMBUS.
  1077. * @retval None
  1078. */
  1079. __weak void HAL_SMBUS_SlaveRxCpltCallback(SMBUS_HandleTypeDef *hsmbus)
  1080. {
  1081. /* Prevent unused argument(s) compilation warning */
  1082. UNUSED(hsmbus);
  1083. /* NOTE : This function should not be modified, when the callback is needed,
  1084. the HAL_SMBUS_SlaveRxCpltCallback() could be implemented in the user file
  1085. */
  1086. }
  1087. /**
  1088. * @brief Slave Address Match callback.
  1089. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  1090. * the configuration information for the specified SMBUS.
  1091. * @param TransferDirection Master request Transfer Direction (Write/Read)
  1092. * @param AddrMatchCode Address Match Code
  1093. * @retval None
  1094. */
  1095. __weak void HAL_SMBUS_AddrCallback(SMBUS_HandleTypeDef *hsmbus, uint8_t TransferDirection, uint16_t AddrMatchCode)
  1096. {
  1097. /* Prevent unused argument(s) compilation warning */
  1098. UNUSED(hsmbus);
  1099. UNUSED(TransferDirection);
  1100. UNUSED(AddrMatchCode);
  1101. /* NOTE : This function should not be modified, when the callback is needed,
  1102. the HAL_SMBUS_AddrCallback() could be implemented in the user file
  1103. */
  1104. }
  1105. /**
  1106. * @brief Listen Complete callback.
  1107. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  1108. * the configuration information for the specified SMBUS.
  1109. * @retval None
  1110. */
  1111. __weak void HAL_SMBUS_ListenCpltCallback(SMBUS_HandleTypeDef *hsmbus)
  1112. {
  1113. /* Prevent unused argument(s) compilation warning */
  1114. UNUSED(hsmbus);
  1115. /* NOTE : This function should not be modified, when the callback is needed,
  1116. the HAL_SMBUS_ListenCpltCallback() could be implemented in the user file
  1117. */
  1118. }
  1119. /**
  1120. * @brief SMBUS error callback.
  1121. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  1122. * the configuration information for the specified SMBUS.
  1123. * @retval None
  1124. */
  1125. __weak void HAL_SMBUS_ErrorCallback(SMBUS_HandleTypeDef *hsmbus)
  1126. {
  1127. /* Prevent unused argument(s) compilation warning */
  1128. UNUSED(hsmbus);
  1129. /* NOTE : This function should not be modified, when the callback is needed,
  1130. the HAL_SMBUS_ErrorCallback() could be implemented in the user file
  1131. */
  1132. }
  1133. /**
  1134. * @}
  1135. */
  1136. /** @defgroup SMBUS_Exported_Functions_Group3 Peripheral State and Errors functions
  1137. * @brief Peripheral State and Errors functions
  1138. *
  1139. @verbatim
  1140. ===============================================================================
  1141. ##### Peripheral State and Errors functions #####
  1142. ===============================================================================
  1143. [..]
  1144. This subsection permits to get in run-time the status of the peripheral
  1145. and the data flow.
  1146. @endverbatim
  1147. * @{
  1148. */
  1149. /**
  1150. * @brief Return the SMBUS handle state.
  1151. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  1152. * the configuration information for the specified SMBUS.
  1153. * @retval HAL state
  1154. */
  1155. uint32_t HAL_SMBUS_GetState(SMBUS_HandleTypeDef *hsmbus)
  1156. {
  1157. /* Return SMBUS handle state */
  1158. return hsmbus->State;
  1159. }
  1160. /**
  1161. * @brief Return the SMBUS error code.
  1162. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  1163. * the configuration information for the specified SMBUS.
  1164. * @retval SMBUS Error Code
  1165. */
  1166. uint32_t HAL_SMBUS_GetError(SMBUS_HandleTypeDef *hsmbus)
  1167. {
  1168. return hsmbus->ErrorCode;
  1169. }
  1170. /**
  1171. * @}
  1172. */
  1173. /**
  1174. * @}
  1175. */
  1176. /** @addtogroup SMBUS_Private_Functions SMBUS Private Functions
  1177. * @brief Data transfers Private functions
  1178. * @{
  1179. */
  1180. /**
  1181. * @brief Interrupt Sub-Routine which handle the Interrupt Flags Master Mode.
  1182. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  1183. * the configuration information for the specified SMBUS.
  1184. * @retval HAL status
  1185. */
  1186. static HAL_StatusTypeDef SMBUS_Master_ISR(SMBUS_HandleTypeDef *hsmbus)
  1187. {
  1188. uint16_t DevAddress;
  1189. /* Process Locked */
  1190. __HAL_LOCK(hsmbus);
  1191. if (__HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_AF) != RESET)
  1192. {
  1193. /* Clear NACK Flag */
  1194. __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_AF);
  1195. /* Set corresponding Error Code */
  1196. /* No need to generate STOP, it is automatically done */
  1197. hsmbus->ErrorCode |= HAL_SMBUS_ERROR_ACKF;
  1198. /* Process Unlocked */
  1199. __HAL_UNLOCK(hsmbus);
  1200. /* Call the Error callback to inform upper layer */
  1201. HAL_SMBUS_ErrorCallback(hsmbus);
  1202. }
  1203. else if (__HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_STOPF) != RESET)
  1204. {
  1205. /* Check and treat errors if errors occurs during STOP process */
  1206. SMBUS_ITErrorHandler(hsmbus);
  1207. /* Call the corresponding callback to inform upper layer of End of Transfer */
  1208. if (hsmbus->State == HAL_SMBUS_STATE_MASTER_BUSY_TX)
  1209. {
  1210. /* Disable Interrupt */
  1211. SMBUS_Disable_IRQ(hsmbus, SMBUS_IT_TX);
  1212. /* Clear STOP Flag */
  1213. __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_STOPF);
  1214. /* Clear Configuration Register 2 */
  1215. SMBUS_RESET_CR2(hsmbus);
  1216. /* Flush remaining data in Fifo register in case of error occurs before TXEmpty */
  1217. /* Disable the selected SMBUS peripheral */
  1218. __HAL_SMBUS_DISABLE(hsmbus);
  1219. hsmbus->PreviousState = HAL_SMBUS_STATE_READY;
  1220. hsmbus->State = HAL_SMBUS_STATE_READY;
  1221. /* Process Unlocked */
  1222. __HAL_UNLOCK(hsmbus);
  1223. /* REenable the selected SMBUS peripheral */
  1224. __HAL_SMBUS_ENABLE(hsmbus);
  1225. HAL_SMBUS_MasterTxCpltCallback(hsmbus);
  1226. }
  1227. else if (hsmbus->State == HAL_SMBUS_STATE_MASTER_BUSY_RX)
  1228. {
  1229. /* Store Last receive data if any */
  1230. if (__HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_RXNE) != RESET)
  1231. {
  1232. /* Read data from RXDR */
  1233. (*hsmbus->pBuffPtr++) = hsmbus->Instance->RXDR;
  1234. if ((hsmbus->XferSize > 0U))
  1235. {
  1236. hsmbus->XferSize--;
  1237. hsmbus->XferCount--;
  1238. }
  1239. }
  1240. /* Disable Interrupt */
  1241. SMBUS_Disable_IRQ(hsmbus, SMBUS_IT_RX);
  1242. /* Clear STOP Flag */
  1243. __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_STOPF);
  1244. /* Clear Configuration Register 2 */
  1245. SMBUS_RESET_CR2(hsmbus);
  1246. hsmbus->PreviousState = HAL_SMBUS_STATE_READY;
  1247. hsmbus->State = HAL_SMBUS_STATE_READY;
  1248. /* Process Unlocked */
  1249. __HAL_UNLOCK(hsmbus);
  1250. HAL_SMBUS_MasterRxCpltCallback(hsmbus);
  1251. }
  1252. }
  1253. else if (__HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_RXNE) != RESET)
  1254. {
  1255. /* Read data from RXDR */
  1256. (*hsmbus->pBuffPtr++) = hsmbus->Instance->RXDR;
  1257. hsmbus->XferSize--;
  1258. hsmbus->XferCount--;
  1259. }
  1260. else if (__HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_TXIS) != RESET)
  1261. {
  1262. /* Write data to TXDR */
  1263. hsmbus->Instance->TXDR = (*hsmbus->pBuffPtr++);
  1264. hsmbus->XferSize--;
  1265. hsmbus->XferCount--;
  1266. }
  1267. else if (__HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_TCR) != RESET)
  1268. {
  1269. if ((hsmbus->XferSize == 0U) && (hsmbus->XferCount != 0U))
  1270. {
  1271. DevAddress = (hsmbus->Instance->CR2 & I2C_CR2_SADD);
  1272. if (hsmbus->XferCount > MAX_NBYTE_SIZE)
  1273. {
  1274. SMBUS_TransferConfig(hsmbus, DevAddress, MAX_NBYTE_SIZE, (SMBUS_RELOAD_MODE | (hsmbus->XferOptions & SMBUS_SENDPEC_MODE)), SMBUS_NO_STARTSTOP);
  1275. hsmbus->XferSize = MAX_NBYTE_SIZE;
  1276. }
  1277. else
  1278. {
  1279. hsmbus->XferSize = hsmbus->XferCount;
  1280. SMBUS_TransferConfig(hsmbus, DevAddress, hsmbus->XferSize, hsmbus->XferOptions, SMBUS_NO_STARTSTOP);
  1281. /* If PEC mode is enable, size to transmit should be Size-1 byte, corresponding to PEC byte */
  1282. /* PEC byte is automatically sent by HW block, no need to manage it in Transmit process */
  1283. if (SMBUS_GET_PEC_MODE(hsmbus) != RESET)
  1284. {
  1285. hsmbus->XferSize--;
  1286. hsmbus->XferCount--;
  1287. }
  1288. }
  1289. }
  1290. else if ((hsmbus->XferSize == 0U) && (hsmbus->XferCount == 0U))
  1291. {
  1292. /* Call TxCpltCallback() if no stop mode is set */
  1293. if (SMBUS_GET_STOP_MODE(hsmbus) != SMBUS_AUTOEND_MODE)
  1294. {
  1295. /* Call the corresponding callback to inform upper layer of End of Transfer */
  1296. if (hsmbus->State == HAL_SMBUS_STATE_MASTER_BUSY_TX)
  1297. {
  1298. /* Disable Interrupt */
  1299. SMBUS_Disable_IRQ(hsmbus, SMBUS_IT_TX);
  1300. hsmbus->PreviousState = hsmbus->State;
  1301. hsmbus->State = HAL_SMBUS_STATE_READY;
  1302. /* Process Unlocked */
  1303. __HAL_UNLOCK(hsmbus);
  1304. HAL_SMBUS_MasterTxCpltCallback(hsmbus);
  1305. }
  1306. else if (hsmbus->State == HAL_SMBUS_STATE_MASTER_BUSY_RX)
  1307. {
  1308. SMBUS_Disable_IRQ(hsmbus, SMBUS_IT_RX);
  1309. hsmbus->PreviousState = hsmbus->State;
  1310. hsmbus->State = HAL_SMBUS_STATE_READY;
  1311. /* Process Unlocked */
  1312. __HAL_UNLOCK(hsmbus);
  1313. HAL_SMBUS_MasterRxCpltCallback(hsmbus);
  1314. }
  1315. }
  1316. }
  1317. }
  1318. else if (__HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_TC) != RESET)
  1319. {
  1320. if (hsmbus->XferCount == 0U)
  1321. {
  1322. /* Specific use case for Quick command */
  1323. if (hsmbus->pBuffPtr == NULL)
  1324. {
  1325. /* Generate a Stop command */
  1326. hsmbus->Instance->CR2 |= I2C_CR2_STOP;
  1327. }
  1328. /* Call TxCpltCallback() if no stop mode is set */
  1329. else if (SMBUS_GET_STOP_MODE(hsmbus) != SMBUS_AUTOEND_MODE)
  1330. {
  1331. /* No Generate Stop, to permit restart mode */
  1332. /* The stop will be done at the end of transfer, when SMBUS_AUTOEND_MODE enable */
  1333. /* Call the corresponding callback to inform upper layer of End of Transfer */
  1334. if (hsmbus->State == HAL_SMBUS_STATE_MASTER_BUSY_TX)
  1335. {
  1336. /* Disable Interrupt */
  1337. SMBUS_Disable_IRQ(hsmbus, SMBUS_IT_TX);
  1338. hsmbus->PreviousState = hsmbus->State;
  1339. hsmbus->State = HAL_SMBUS_STATE_READY;
  1340. /* Process Unlocked */
  1341. __HAL_UNLOCK(hsmbus);
  1342. HAL_SMBUS_MasterTxCpltCallback(hsmbus);
  1343. }
  1344. else if (hsmbus->State == HAL_SMBUS_STATE_MASTER_BUSY_RX)
  1345. {
  1346. SMBUS_Disable_IRQ(hsmbus, SMBUS_IT_RX);
  1347. hsmbus->PreviousState = hsmbus->State;
  1348. hsmbus->State = HAL_SMBUS_STATE_READY;
  1349. /* Process Unlocked */
  1350. __HAL_UNLOCK(hsmbus);
  1351. HAL_SMBUS_MasterRxCpltCallback(hsmbus);
  1352. }
  1353. }
  1354. }
  1355. }
  1356. /* Process Unlocked */
  1357. __HAL_UNLOCK(hsmbus);
  1358. return HAL_OK;
  1359. }
  1360. /**
  1361. * @brief Interrupt Sub-Routine which handle the Interrupt Flags Slave Mode.
  1362. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  1363. * the configuration information for the specified SMBUS.
  1364. * @retval HAL status
  1365. */
  1366. static HAL_StatusTypeDef SMBUS_Slave_ISR(SMBUS_HandleTypeDef *hsmbus)
  1367. {
  1368. uint8_t TransferDirection = 0U;
  1369. uint16_t SlaveAddrCode = 0U;
  1370. /* Process Locked */
  1371. __HAL_LOCK(hsmbus);
  1372. if (__HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_AF) != RESET)
  1373. {
  1374. /* Check that SMBUS transfer finished */
  1375. /* if yes, normal usecase, a NACK is sent by the HOST when Transfer is finished */
  1376. /* Mean XferCount == 0*/
  1377. /* So clear Flag NACKF only */
  1378. if (hsmbus->XferCount == 0U)
  1379. {
  1380. /* Clear NACK Flag */
  1381. __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_AF);
  1382. /* Process Unlocked */
  1383. __HAL_UNLOCK(hsmbus);
  1384. }
  1385. else
  1386. {
  1387. /* if no, error usecase, a Non-Acknowledge of last Data is generated by the HOST*/
  1388. /* Clear NACK Flag */
  1389. __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_AF);
  1390. /* Set HAL State to "Idle" State, mean to LISTEN state */
  1391. /* So reset Slave Busy state */
  1392. hsmbus->PreviousState = hsmbus->State;
  1393. hsmbus->State &= ~((uint32_t)HAL_SMBUS_STATE_SLAVE_BUSY_TX);
  1394. hsmbus->State &= ~((uint32_t)HAL_SMBUS_STATE_SLAVE_BUSY_RX);
  1395. /* Disable RX/TX Interrupts, keep only ADDR Interrupt */
  1396. SMBUS_Disable_IRQ(hsmbus, SMBUS_IT_RX | SMBUS_IT_TX);
  1397. /* Set ErrorCode corresponding to a Non-Acknowledge */
  1398. hsmbus->ErrorCode |= HAL_SMBUS_ERROR_ACKF;
  1399. /* Process Unlocked */
  1400. __HAL_UNLOCK(hsmbus);
  1401. /* Call the Error callback to inform upper layer */
  1402. HAL_SMBUS_ErrorCallback(hsmbus);
  1403. }
  1404. }
  1405. else if (__HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_ADDR) != RESET)
  1406. {
  1407. TransferDirection = SMBUS_GET_DIR(hsmbus);
  1408. SlaveAddrCode = SMBUS_GET_ADDR_MATCH(hsmbus);
  1409. /* Disable ADDR interrupt to prevent multiple ADDRInterrupt*/
  1410. /* Other ADDRInterrupt will be treat in next Listen usecase */
  1411. __HAL_SMBUS_DISABLE_IT(hsmbus, SMBUS_IT_ADDRI);
  1412. /* Process Unlocked */
  1413. __HAL_UNLOCK(hsmbus);
  1414. /* Call Slave Addr callback */
  1415. HAL_SMBUS_AddrCallback(hsmbus, TransferDirection, SlaveAddrCode);
  1416. }
  1417. else if ((__HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_RXNE) != RESET) || (__HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_TCR) != RESET))
  1418. {
  1419. if ((hsmbus->State & HAL_SMBUS_STATE_SLAVE_BUSY_RX) == HAL_SMBUS_STATE_SLAVE_BUSY_RX)
  1420. {
  1421. /* Read data from RXDR */
  1422. (*hsmbus->pBuffPtr++) = hsmbus->Instance->RXDR;
  1423. hsmbus->XferSize--;
  1424. hsmbus->XferCount--;
  1425. if (hsmbus->XferCount == 1U)
  1426. {
  1427. /* Receive last Byte, can be PEC byte in case of PEC BYTE enabled */
  1428. /* or only the last Byte of Transfer */
  1429. /* So reset the RELOAD bit mode */
  1430. hsmbus->XferOptions &= ~SMBUS_RELOAD_MODE;
  1431. SMBUS_TransferConfig(hsmbus, 0U, 1U, hsmbus->XferOptions, SMBUS_NO_STARTSTOP);
  1432. }
  1433. else if (hsmbus->XferCount == 0U)
  1434. {
  1435. /* Last Byte is received, disable Interrupt */
  1436. SMBUS_Disable_IRQ(hsmbus, SMBUS_IT_RX);
  1437. /* Remove HAL_SMBUS_STATE_SLAVE_BUSY_RX, keep only HAL_SMBUS_STATE_LISTEN */
  1438. hsmbus->PreviousState = hsmbus->State;
  1439. hsmbus->State &= ~((uint32_t)HAL_SMBUS_STATE_SLAVE_BUSY_RX);
  1440. /* Process Unlocked */
  1441. __HAL_UNLOCK(hsmbus);
  1442. /* Call the Rx complete callback to inform upper layer of the end of receive process */
  1443. HAL_SMBUS_SlaveRxCpltCallback(hsmbus);
  1444. }
  1445. else
  1446. {
  1447. /* Set Reload for next Bytes */
  1448. SMBUS_TransferConfig(hsmbus, 0U, 1U, SMBUS_RELOAD_MODE | (hsmbus->XferOptions & SMBUS_SENDPEC_MODE), SMBUS_NO_STARTSTOP);
  1449. /* Ack last Byte Read */
  1450. hsmbus->Instance->CR2 &= ~I2C_CR2_NACK;
  1451. }
  1452. }
  1453. else if ((hsmbus->State & HAL_SMBUS_STATE_SLAVE_BUSY_TX) == HAL_SMBUS_STATE_SLAVE_BUSY_TX)
  1454. {
  1455. if ((hsmbus->XferSize == 0U) && (hsmbus->XferCount != 0U))
  1456. {
  1457. if (hsmbus->XferCount > MAX_NBYTE_SIZE)
  1458. {
  1459. SMBUS_TransferConfig(hsmbus, 0U, MAX_NBYTE_SIZE, (SMBUS_RELOAD_MODE | (hsmbus->XferOptions & SMBUS_SENDPEC_MODE)), SMBUS_NO_STARTSTOP);
  1460. hsmbus->XferSize = MAX_NBYTE_SIZE;
  1461. }
  1462. else
  1463. {
  1464. hsmbus->XferSize = hsmbus->XferCount;
  1465. SMBUS_TransferConfig(hsmbus, 0U, hsmbus->XferSize, hsmbus->XferOptions, SMBUS_NO_STARTSTOP);
  1466. /* If PEC mode is enable, size to transmit should be Size-1 byte, corresponding to PEC byte */
  1467. /* PEC byte is automatically sent by HW block, no need to manage it in Transmit process */
  1468. if (SMBUS_GET_PEC_MODE(hsmbus) != RESET)
  1469. {
  1470. hsmbus->XferSize--;
  1471. hsmbus->XferCount--;
  1472. }
  1473. }
  1474. }
  1475. }
  1476. }
  1477. else if (__HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_TXIS) != RESET)
  1478. {
  1479. /* Write data to TXDR only if XferCount not reach "0" */
  1480. /* A TXIS flag can be set, during STOP treatment */
  1481. /* Check if all Data have already been sent */
  1482. /* If it is the case, this last write in TXDR is not sent, correspond to a dummy TXIS event */
  1483. if (hsmbus->XferCount > 0U)
  1484. {
  1485. /* Write data to TXDR */
  1486. hsmbus->Instance->TXDR = (*hsmbus->pBuffPtr++);
  1487. hsmbus->XferCount--;
  1488. hsmbus->XferSize--;
  1489. }
  1490. if (hsmbus->XferCount == 0U)
  1491. {
  1492. /* Last Byte is Transmitted */
  1493. /* Remove HAL_SMBUS_STATE_SLAVE_BUSY_TX, keep only HAL_SMBUS_STATE_LISTEN */
  1494. SMBUS_Disable_IRQ(hsmbus, SMBUS_IT_TX);
  1495. hsmbus->PreviousState = hsmbus->State;
  1496. hsmbus->State &= ~((uint32_t)HAL_SMBUS_STATE_SLAVE_BUSY_TX);
  1497. /* Process Unlocked */
  1498. __HAL_UNLOCK(hsmbus);
  1499. /* Call the Tx complete callback to inform upper layer of the end of transmit process */
  1500. HAL_SMBUS_SlaveTxCpltCallback(hsmbus);
  1501. }
  1502. }
  1503. /* Check if STOPF is set */
  1504. if (__HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_STOPF) != RESET)
  1505. {
  1506. if ((hsmbus->State & HAL_SMBUS_STATE_LISTEN) == HAL_SMBUS_STATE_LISTEN)
  1507. {
  1508. /* Store Last receive data if any */
  1509. if (__HAL_SMBUS_GET_FLAG(hsmbus, SMBUS_FLAG_RXNE) != RESET)
  1510. {
  1511. /* Read data from RXDR */
  1512. (*hsmbus->pBuffPtr++) = hsmbus->Instance->RXDR;
  1513. if ((hsmbus->XferSize > 0U))
  1514. {
  1515. hsmbus->XferSize--;
  1516. hsmbus->XferCount--;
  1517. }
  1518. }
  1519. /* Disable RX and TX Interrupts */
  1520. SMBUS_Disable_IRQ(hsmbus, SMBUS_IT_RX | SMBUS_IT_TX);
  1521. /* Disable ADDR Interrupt */
  1522. SMBUS_Disable_IRQ(hsmbus, SMBUS_IT_ADDR);
  1523. /* Disable Address Acknowledge */
  1524. hsmbus->Instance->CR2 |= I2C_CR2_NACK;
  1525. /* Clear Configuration Register 2 */
  1526. SMBUS_RESET_CR2(hsmbus);
  1527. /* Clear STOP Flag */
  1528. __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_STOPF);
  1529. /* Clear ADDR flag */
  1530. __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_ADDR);
  1531. hsmbus->XferOptions = 0U;
  1532. hsmbus->PreviousState = hsmbus->State;
  1533. hsmbus->State = HAL_SMBUS_STATE_READY;
  1534. /* Process Unlocked */
  1535. __HAL_UNLOCK(hsmbus);
  1536. /* Call the Listen Complete callback, to inform upper layer of the end of Listen usecase */
  1537. HAL_SMBUS_ListenCpltCallback(hsmbus);
  1538. }
  1539. }
  1540. /* Process Unlocked */
  1541. __HAL_UNLOCK(hsmbus);
  1542. return HAL_OK;
  1543. }
  1544. /**
  1545. * @brief Manage the enabling of Interrupts.
  1546. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  1547. * the configuration information for the specified SMBUS.
  1548. * @param InterruptRequest Value of @ref SMBUS_Interrupt_configuration_definition.
  1549. * @retval HAL status
  1550. */
  1551. static HAL_StatusTypeDef SMBUS_Enable_IRQ(SMBUS_HandleTypeDef *hsmbus, uint16_t InterruptRequest)
  1552. {
  1553. uint32_t tmpisr = 0U;
  1554. if ((InterruptRequest & SMBUS_IT_ALERT) == SMBUS_IT_ALERT)
  1555. {
  1556. /* Enable ERR interrupt */
  1557. tmpisr |= SMBUS_IT_ERRI;
  1558. }
  1559. if ((InterruptRequest & SMBUS_IT_ADDR) == SMBUS_IT_ADDR)
  1560. {
  1561. /* Enable ADDR, STOP interrupt */
  1562. tmpisr |= SMBUS_IT_ADDRI | SMBUS_IT_STOPI | SMBUS_IT_NACKI | SMBUS_IT_ERRI;
  1563. }
  1564. if ((InterruptRequest & SMBUS_IT_TX) == SMBUS_IT_TX)
  1565. {
  1566. /* Enable ERR, TC, STOP, NACK, RXI interrupt */
  1567. tmpisr |= SMBUS_IT_ERRI | SMBUS_IT_TCI | SMBUS_IT_STOPI | SMBUS_IT_NACKI | SMBUS_IT_TXI;
  1568. }
  1569. if ((InterruptRequest & SMBUS_IT_RX) == SMBUS_IT_RX)
  1570. {
  1571. /* Enable ERR, TC, STOP, NACK, TXI interrupt */
  1572. tmpisr |= SMBUS_IT_ERRI | SMBUS_IT_TCI | SMBUS_IT_STOPI | SMBUS_IT_NACKI | SMBUS_IT_RXI;
  1573. }
  1574. /* Enable interrupts only at the end */
  1575. /* to avoid the risk of SMBUS interrupt handle execution before */
  1576. /* all interrupts requested done */
  1577. __HAL_SMBUS_ENABLE_IT(hsmbus, tmpisr);
  1578. return HAL_OK;
  1579. }
  1580. /**
  1581. * @brief Manage the disabling of Interrupts.
  1582. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  1583. * the configuration information for the specified SMBUS.
  1584. * @param InterruptRequest Value of @ref SMBUS_Interrupt_configuration_definition.
  1585. * @retval HAL status
  1586. */
  1587. static HAL_StatusTypeDef SMBUS_Disable_IRQ(SMBUS_HandleTypeDef *hsmbus, uint16_t InterruptRequest)
  1588. {
  1589. uint32_t tmpisr = 0U;
  1590. if (((InterruptRequest & SMBUS_IT_ALERT) == SMBUS_IT_ALERT) && (hsmbus->State == HAL_SMBUS_STATE_READY))
  1591. {
  1592. /* Disable ERR interrupt */
  1593. tmpisr |= SMBUS_IT_ERRI;
  1594. }
  1595. if ((InterruptRequest & SMBUS_IT_TX) == SMBUS_IT_TX)
  1596. {
  1597. /* Disable TC, STOP, NACK, TXI interrupt */
  1598. tmpisr |= SMBUS_IT_TCI | SMBUS_IT_TXI;
  1599. if ((SMBUS_GET_ALERT_ENABLED(hsmbus) == RESET)
  1600. && ((hsmbus->State & HAL_SMBUS_STATE_LISTEN) != HAL_SMBUS_STATE_LISTEN))
  1601. {
  1602. /* Disable ERR interrupt */
  1603. tmpisr |= SMBUS_IT_ERRI;
  1604. }
  1605. if ((hsmbus->State & HAL_SMBUS_STATE_LISTEN) != HAL_SMBUS_STATE_LISTEN)
  1606. {
  1607. /* Disable STOPI, NACKI */
  1608. tmpisr |= SMBUS_IT_STOPI | SMBUS_IT_NACKI;
  1609. }
  1610. }
  1611. if ((InterruptRequest & SMBUS_IT_RX) == SMBUS_IT_RX)
  1612. {
  1613. /* Disable TC, STOP, NACK, RXI interrupt */
  1614. tmpisr |= SMBUS_IT_TCI | SMBUS_IT_RXI;
  1615. if ((SMBUS_GET_ALERT_ENABLED(hsmbus) == RESET)
  1616. && ((hsmbus->State & HAL_SMBUS_STATE_LISTEN) != HAL_SMBUS_STATE_LISTEN))
  1617. {
  1618. /* Disable ERR interrupt */
  1619. tmpisr |= SMBUS_IT_ERRI;
  1620. }
  1621. if ((hsmbus->State & HAL_SMBUS_STATE_LISTEN) != HAL_SMBUS_STATE_LISTEN)
  1622. {
  1623. /* Disable STOPI, NACKI */
  1624. tmpisr |= SMBUS_IT_STOPI | SMBUS_IT_NACKI;
  1625. }
  1626. }
  1627. if ((InterruptRequest & SMBUS_IT_ADDR) == SMBUS_IT_ADDR)
  1628. {
  1629. /* Enable ADDR, STOP interrupt */
  1630. tmpisr |= SMBUS_IT_ADDRI | SMBUS_IT_STOPI | SMBUS_IT_NACKI;
  1631. if (SMBUS_GET_ALERT_ENABLED(hsmbus) == RESET)
  1632. {
  1633. /* Disable ERR interrupt */
  1634. tmpisr |= SMBUS_IT_ERRI;
  1635. }
  1636. }
  1637. /* Disable interrupts only at the end */
  1638. /* to avoid a breaking situation like at "t" time */
  1639. /* all disable interrupts request are not done */
  1640. __HAL_SMBUS_DISABLE_IT(hsmbus, tmpisr);
  1641. return HAL_OK;
  1642. }
  1643. /**
  1644. * @brief SMBUS interrupts error handler.
  1645. * @param hsmbus SMBUS handle.
  1646. * @retval None
  1647. */
  1648. static void SMBUS_ITErrorHandler(SMBUS_HandleTypeDef *hsmbus)
  1649. {
  1650. uint32_t itflags = READ_REG(hsmbus->Instance->ISR);
  1651. uint32_t itsources = READ_REG(hsmbus->Instance->CR1);
  1652. /* SMBUS Bus error interrupt occurred ------------------------------------*/
  1653. if (((itflags & SMBUS_FLAG_BERR) != RESET) && ((itsources & SMBUS_IT_ERRI) != RESET))
  1654. {
  1655. hsmbus->ErrorCode |= HAL_SMBUS_ERROR_BERR;
  1656. /* Clear BERR flag */
  1657. __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_BERR);
  1658. }
  1659. /* SMBUS Over-Run/Under-Run interrupt occurred ----------------------------------------*/
  1660. if (((itflags & SMBUS_FLAG_OVR) != RESET) && ((itsources & SMBUS_IT_ERRI) != RESET))
  1661. {
  1662. hsmbus->ErrorCode |= HAL_SMBUS_ERROR_OVR;
  1663. /* Clear OVR flag */
  1664. __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_OVR);
  1665. }
  1666. /* SMBUS Arbitration Loss error interrupt occurred ------------------------------------*/
  1667. if (((itflags & SMBUS_FLAG_ARLO) != RESET) && ((itsources & SMBUS_IT_ERRI) != RESET))
  1668. {
  1669. hsmbus->ErrorCode |= HAL_SMBUS_ERROR_ARLO;
  1670. /* Clear ARLO flag */
  1671. __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_ARLO);
  1672. }
  1673. /* SMBUS Timeout error interrupt occurred ---------------------------------------------*/
  1674. if (((itflags & SMBUS_FLAG_TIMEOUT) != RESET) && ((itsources & SMBUS_IT_ERRI) != RESET))
  1675. {
  1676. hsmbus->ErrorCode |= HAL_SMBUS_ERROR_BUSTIMEOUT;
  1677. /* Clear TIMEOUT flag */
  1678. __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_TIMEOUT);
  1679. }
  1680. /* SMBUS Alert error interrupt occurred -----------------------------------------------*/
  1681. if (((itflags & SMBUS_FLAG_ALERT) != RESET) && ((itsources & SMBUS_IT_ERRI) != RESET))
  1682. {
  1683. hsmbus->ErrorCode |= HAL_SMBUS_ERROR_ALERT;
  1684. /* Clear ALERT flag */
  1685. __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_ALERT);
  1686. }
  1687. /* SMBUS Packet Error Check error interrupt occurred ----------------------------------*/
  1688. if (((itflags & SMBUS_FLAG_PECERR) != RESET) && ((itsources & SMBUS_IT_ERRI) != RESET))
  1689. {
  1690. hsmbus->ErrorCode |= HAL_SMBUS_ERROR_PECERR;
  1691. /* Clear PEC error flag */
  1692. __HAL_SMBUS_CLEAR_FLAG(hsmbus, SMBUS_FLAG_PECERR);
  1693. }
  1694. /* Call the Error Callback in case of Error detected */
  1695. if ((hsmbus->ErrorCode != HAL_SMBUS_ERROR_NONE) && (hsmbus->ErrorCode != HAL_SMBUS_ERROR_ACKF))
  1696. {
  1697. /* Do not Reset the HAL state in case of ALERT error */
  1698. if ((hsmbus->ErrorCode & HAL_SMBUS_ERROR_ALERT) != HAL_SMBUS_ERROR_ALERT)
  1699. {
  1700. if (((hsmbus->State & HAL_SMBUS_STATE_SLAVE_BUSY_TX) == HAL_SMBUS_STATE_SLAVE_BUSY_TX)
  1701. || ((hsmbus->State & HAL_SMBUS_STATE_SLAVE_BUSY_RX) == HAL_SMBUS_STATE_SLAVE_BUSY_RX))
  1702. {
  1703. /* Reset only HAL_SMBUS_STATE_SLAVE_BUSY_XX */
  1704. /* keep HAL_SMBUS_STATE_LISTEN if set */
  1705. hsmbus->PreviousState = HAL_SMBUS_STATE_READY;
  1706. hsmbus->State = HAL_SMBUS_STATE_LISTEN;
  1707. }
  1708. }
  1709. /* Call the Error callback to inform upper layer */
  1710. HAL_SMBUS_ErrorCallback(hsmbus);
  1711. }
  1712. }
  1713. /**
  1714. * @brief Handle SMBUS Communication Timeout.
  1715. * @param hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
  1716. * the configuration information for the specified SMBUS.
  1717. * @param Flag Specifies the SMBUS flag to check.
  1718. * @param Status The new Flag status (SET or RESET).
  1719. * @param Timeout Timeout duration
  1720. * @retval HAL status
  1721. */
  1722. static HAL_StatusTypeDef SMBUS_WaitOnFlagUntilTimeout(SMBUS_HandleTypeDef *hsmbus, uint32_t Flag, FlagStatus Status, uint32_t Timeout)
  1723. {
  1724. uint32_t tickstart = HAL_GetTick();
  1725. /* Wait until flag is set */
  1726. if (Status == RESET)
  1727. {
  1728. while (__HAL_SMBUS_GET_FLAG(hsmbus, Flag) == RESET)
  1729. {
  1730. /* Check for the Timeout */
  1731. if (Timeout != HAL_MAX_DELAY)
  1732. {
  1733. if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
  1734. {
  1735. hsmbus->PreviousState = hsmbus->State;
  1736. hsmbus->State = HAL_SMBUS_STATE_READY;
  1737. /* Process Unlocked */
  1738. __HAL_UNLOCK(hsmbus);
  1739. return HAL_TIMEOUT;
  1740. }
  1741. }
  1742. }
  1743. }
  1744. else
  1745. {
  1746. while (__HAL_SMBUS_GET_FLAG(hsmbus, Flag) != RESET)
  1747. {
  1748. /* Check for the Timeout */
  1749. if (Timeout != HAL_MAX_DELAY)
  1750. {
  1751. if ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
  1752. {
  1753. hsmbus->PreviousState = hsmbus->State;
  1754. hsmbus->State = HAL_SMBUS_STATE_READY;
  1755. /* Process Unlocked */
  1756. __HAL_UNLOCK(hsmbus);
  1757. return HAL_TIMEOUT;
  1758. }
  1759. }
  1760. }
  1761. }
  1762. return HAL_OK;
  1763. }
  1764. /**
  1765. * @brief Handle SMBUSx communication when starting transfer or during transfer (TC or TCR flag are set).
  1766. * @param hsmbus SMBUS handle.
  1767. * @param DevAddress specifies the slave address to be programmed.
  1768. * @param Size specifies the number of bytes to be programmed.
  1769. * This parameter must be a value between 0 and 255.
  1770. * @param Mode New state of the SMBUS START condition generation.
  1771. * This parameter can be one or a combination of the following values:
  1772. * @arg @ref SMBUS_RELOAD_MODE Enable Reload mode.
  1773. * @arg @ref SMBUS_AUTOEND_MODE Enable Automatic end mode.
  1774. * @arg @ref SMBUS_SOFTEND_MODE Enable Software end mode and Reload mode.
  1775. * @arg @ref SMBUS_SENDPEC_MODE Enable Packet Error Calculation mode.
  1776. * @param Request New state of the SMBUS START condition generation.
  1777. * This parameter can be one of the following values:
  1778. * @arg @ref SMBUS_NO_STARTSTOP Don't Generate stop and start condition.
  1779. * @arg @ref SMBUS_GENERATE_STOP Generate stop condition (Size should be set to 0).
  1780. * @arg @ref SMBUS_GENERATE_START_READ Generate Restart for read request.
  1781. * @arg @ref SMBUS_GENERATE_START_WRITE Generate Restart for write request.
  1782. * @retval None
  1783. */
  1784. static void SMBUS_TransferConfig(SMBUS_HandleTypeDef *hsmbus, uint16_t DevAddress, uint8_t Size, uint32_t Mode, uint32_t Request)
  1785. {
  1786. /* Check the parameters */
  1787. assert_param(IS_SMBUS_ALL_INSTANCE(hsmbus->Instance));
  1788. assert_param(IS_SMBUS_TRANSFER_MODE(Mode));
  1789. assert_param(IS_SMBUS_TRANSFER_REQUEST(Request));
  1790. /* update CR2 register */
  1791. MODIFY_REG(hsmbus->Instance->CR2, ((I2C_CR2_SADD | I2C_CR2_NBYTES | I2C_CR2_RELOAD | I2C_CR2_AUTOEND | (I2C_CR2_RD_WRN & (uint32_t)(Request >> (31U - I2C_CR2_RD_WRN_Pos))) | I2C_CR2_START | I2C_CR2_STOP | I2C_CR2_PECBYTE)), \
  1792. (uint32_t)(((uint32_t)DevAddress & I2C_CR2_SADD) | (((uint32_t)Size << I2C_CR2_NBYTES_Pos) & I2C_CR2_NBYTES) | (uint32_t)Mode | (uint32_t)Request));
  1793. }
  1794. /**
  1795. * @brief Convert SMBUSx OTHER_xxx XferOptions to functionnal XferOptions.
  1796. * @param hsmbus SMBUS handle.
  1797. * @retval None
  1798. */
  1799. static void SMBUS_ConvertOtherXferOptions(SMBUS_HandleTypeDef *hsmbus)
  1800. {
  1801. /* if user set XferOptions to SMBUS_OTHER_FRAME_NO_PEC */
  1802. /* it request implicitly to generate a restart condition */
  1803. /* set XferOptions to SMBUS_FIRST_FRAME */
  1804. if (hsmbus->XferOptions == SMBUS_OTHER_FRAME_NO_PEC)
  1805. {
  1806. hsmbus->XferOptions = SMBUS_FIRST_FRAME;
  1807. }
  1808. /* else if user set XferOptions to SMBUS_OTHER_FRAME_WITH_PEC */
  1809. /* it request implicitly to generate a restart condition */
  1810. /* set XferOptions to SMBUS_FIRST_FRAME | SMBUS_SENDPEC_MODE */
  1811. else if (hsmbus->XferOptions == SMBUS_OTHER_FRAME_WITH_PEC)
  1812. {
  1813. hsmbus->XferOptions = SMBUS_FIRST_FRAME | SMBUS_SENDPEC_MODE;
  1814. }
  1815. /* else if user set XferOptions to SMBUS_OTHER_AND_LAST_FRAME_NO_PEC */
  1816. /* it request implicitly to generate a restart condition */
  1817. /* then generate a stop condition at the end of transfer */
  1818. /* set XferOptions to SMBUS_FIRST_AND_LAST_FRAME_NO_PEC */
  1819. else if (hsmbus->XferOptions == SMBUS_OTHER_AND_LAST_FRAME_NO_PEC)
  1820. {
  1821. hsmbus->XferOptions = SMBUS_FIRST_AND_LAST_FRAME_NO_PEC;
  1822. }
  1823. /* else if user set XferOptions to SMBUS_OTHER_AND_LAST_FRAME_WITH_PEC */
  1824. /* it request implicitly to generate a restart condition */
  1825. /* then generate a stop condition at the end of transfer */
  1826. /* set XferOptions to SMBUS_FIRST_AND_LAST_FRAME_WITH_PEC */
  1827. else if (hsmbus->XferOptions == SMBUS_OTHER_AND_LAST_FRAME_WITH_PEC)
  1828. {
  1829. hsmbus->XferOptions = SMBUS_FIRST_AND_LAST_FRAME_WITH_PEC;
  1830. }
  1831. }
  1832. /**
  1833. * @}
  1834. */
  1835. #endif /* HAL_SMBUS_MODULE_ENABLED */
  1836. /**
  1837. * @}
  1838. */
  1839. /**
  1840. * @}
  1841. */
  1842. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/