1 /**
2   ******************************************************************************
3   * @file    stm32h5xx_hal_smbus_ex.c
4   * @author  MCD Application Team
5   * @brief   SMBUS Extended HAL module driver.
6   *          This file provides firmware functions to manage the following
7   *          functionalities of SMBUS Extended peripheral:
8   *           + Extended features functions
9   *
10   ******************************************************************************
11   * @attention
12   *
13   * Copyright (c) 2022 STMicroelectronics.
14   * All rights reserved.
15   *
16   * This software is licensed under terms that can be found in the LICENSE file
17   * in the root directory of this software component.
18   * If no LICENSE file comes with this software, it is provided AS-IS.
19   *
20   ******************************************************************************
21   @verbatim
22   ==============================================================================
23                ##### SMBUS peripheral Extended features  #####
24   ==============================================================================
25 
26   [..] Comparing to other previous devices, the SMBUS interface for STM32H5xx
27        devices contains the following additional features
28 
29        (+) Disable or enable wakeup from Stop mode(s)
30 
31                      ##### How to use this driver #####
32   ==============================================================================
33     (#) Configure the enable or disable of SMBUS Wake Up Mode using the functions :
34           (++) HAL_SMBUSEx_EnableWakeUp()
35           (++) HAL_SMBUSEx_DisableWakeUp()
36     (#) Configure the enable or disable of fast mode plus driving capability using the functions :
37           (++) HAL_SMBUSEx_ConfigFastModePlus()
38   @endverbatim
39   */
40 
41 /* Includes ------------------------------------------------------------------*/
42 #include "stm32h5xx_hal.h"
43 
44 /** @addtogroup STM32H5xx_HAL_Driver
45   * @{
46   */
47 
48 /** @defgroup SMBUSEx SMBUSEx
49   * @brief SMBUS Extended HAL module driver
50   * @{
51   */
52 
53 #ifdef HAL_SMBUS_MODULE_ENABLED
54 
55 /* Private typedef -----------------------------------------------------------*/
56 /* Private define ------------------------------------------------------------*/
57 /* Private macro -------------------------------------------------------------*/
58 /* Private variables ---------------------------------------------------------*/
59 /* Private function prototypes -----------------------------------------------*/
60 /* Private functions ---------------------------------------------------------*/
61 
62 /** @defgroup SMBUSEx_Exported_Functions SMBUS Extended Exported Functions
63   * @{
64   */
65 
66 /** @defgroup SMBUSEx_Exported_Functions_Group2 WakeUp Mode Functions
67   * @brief    WakeUp Mode Functions
68   *
69 @verbatim
70  ===============================================================================
71                       ##### WakeUp Mode Functions #####
72  ===============================================================================
73     [..] This section provides functions allowing to:
74       (+) Configure Wake Up Feature
75 
76 @endverbatim
77   * @{
78   */
79 
80 /**
81   * @brief  Enable SMBUS wakeup from Stop mode(s).
82   * @param  hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
83   *                the configuration information for the specified SMBUSx peripheral.
84   * @retval HAL status
85   */
HAL_SMBUSEx_EnableWakeUp(SMBUS_HandleTypeDef * hsmbus)86 HAL_StatusTypeDef HAL_SMBUSEx_EnableWakeUp(SMBUS_HandleTypeDef *hsmbus)
87 {
88   /* Check the parameters */
89   assert_param(IS_I2C_WAKEUP_FROMSTOP_INSTANCE(hsmbus->Instance));
90 
91   if (hsmbus->State == HAL_SMBUS_STATE_READY)
92   {
93     /* Process Locked */
94     __HAL_LOCK(hsmbus);
95 
96     hsmbus->State = HAL_SMBUS_STATE_BUSY;
97 
98     /* Disable the selected SMBUS peripheral */
99     __HAL_SMBUS_DISABLE(hsmbus);
100 
101     /* Enable wakeup from stop mode */
102     hsmbus->Instance->CR1 |= I2C_CR1_WUPEN;
103 
104     __HAL_SMBUS_ENABLE(hsmbus);
105 
106     hsmbus->State = HAL_SMBUS_STATE_READY;
107 
108     /* Process Unlocked */
109     __HAL_UNLOCK(hsmbus);
110 
111     return HAL_OK;
112   }
113   else
114   {
115     return HAL_BUSY;
116   }
117 }
118 
119 /**
120   * @brief  Disable SMBUS wakeup from Stop mode(s).
121   * @param  hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
122   *                the configuration information for the specified SMBUSx peripheral.
123   * @retval HAL status
124   */
HAL_SMBUSEx_DisableWakeUp(SMBUS_HandleTypeDef * hsmbus)125 HAL_StatusTypeDef HAL_SMBUSEx_DisableWakeUp(SMBUS_HandleTypeDef *hsmbus)
126 {
127   /* Check the parameters */
128   assert_param(IS_I2C_WAKEUP_FROMSTOP_INSTANCE(hsmbus->Instance));
129 
130   if (hsmbus->State == HAL_SMBUS_STATE_READY)
131   {
132     /* Process Locked */
133     __HAL_LOCK(hsmbus);
134 
135     hsmbus->State = HAL_SMBUS_STATE_BUSY;
136 
137     /* Disable the selected SMBUS peripheral */
138     __HAL_SMBUS_DISABLE(hsmbus);
139 
140     /* Disable wakeup from stop mode */
141     hsmbus->Instance->CR1 &= ~(I2C_CR1_WUPEN);
142 
143     __HAL_SMBUS_ENABLE(hsmbus);
144 
145     hsmbus->State = HAL_SMBUS_STATE_READY;
146 
147     /* Process Unlocked */
148     __HAL_UNLOCK(hsmbus);
149 
150     return HAL_OK;
151   }
152   else
153   {
154     return HAL_BUSY;
155   }
156 }
157 /**
158   * @}
159   */
160 
161 /** @defgroup SMBUSEx_Exported_Functions_Group3 Fast Mode Plus Functions
162   * @brief    Fast Mode Plus Functions
163   *
164 @verbatim
165  ===============================================================================
166                       ##### Fast Mode Plus Functions #####
167  ===============================================================================
168     [..] This section provides functions allowing to:
169       (+) Configure Fast Mode Plus
170 
171 @endverbatim
172   * @{
173   */
174 
175 /**
176   * @brief  Configure SMBUS Fast Mode Plus.
177   * @param  hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
178   *                the configuration information for the specified SMBUSx peripheral.
179   * @param  FastModePlus New state of the Fast Mode Plus.
180   * @retval HAL status
181   */
HAL_SMBUSEx_ConfigFastModePlus(SMBUS_HandleTypeDef * hsmbus,uint32_t FastModePlus)182 HAL_StatusTypeDef HAL_SMBUSEx_ConfigFastModePlus(SMBUS_HandleTypeDef *hsmbus, uint32_t FastModePlus)
183 {
184   /* Check the parameters */
185   assert_param(IS_SMBUS_ALL_INSTANCE(hsmbus->Instance));
186   assert_param(IS_SMBUS_FASTMODEPLUS(FastModePlus));
187 
188   if (hsmbus->State == HAL_SMBUS_STATE_READY)
189   {
190     /* Process Locked */
191     __HAL_LOCK(hsmbus);
192 
193     hsmbus->State = HAL_SMBUS_STATE_BUSY;
194 
195     /* Disable the selected SMBUS peripheral */
196     __HAL_SMBUS_DISABLE(hsmbus);
197 
198     if (FastModePlus == SMBUS_FASTMODEPLUS_ENABLE)
199     {
200       /* Set SMBUSx FMP bit */
201       hsmbus->Instance->CR1 |= (I2C_CR1_FMP);
202     }
203     else
204     {
205       /* Reset SMBUSx FMP bit */
206       hsmbus->Instance->CR1 &= ~(I2C_CR1_FMP);
207     }
208 
209     __HAL_SMBUS_ENABLE(hsmbus);
210 
211     hsmbus->State = HAL_SMBUS_STATE_READY;
212 
213     /* Process Unlocked */
214     __HAL_UNLOCK(hsmbus);
215 
216     return HAL_OK;
217   }
218   else
219   {
220     return HAL_BUSY;
221   }
222 }
223 
224 /**
225   * @}
226   */
227 
228 /**
229   * @}
230   */
231 
232 /**
233   * @}
234   */
235 
236 #endif /* HAL_SMBUS_MODULE_ENABLED */
237 /**
238   * @}
239   */
240 
241 /**
242   * @}
243   */
244