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