1 /**
2   ******************************************************************************
3   * @file    stm32wbaxx_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 STM32WBAxx
27        devices contains the following additional features
28 
29        (+) Disable or enable wakeup from Stop mode(s)
30        (+) Disable or enable Fast Mode Plus
31 
32                      ##### How to use this driver #####
33   ==============================================================================
34     (#) Configure the enable or disable of SMBUS Wake Up Mode using the functions :
35           (++) HAL_SMBUSEx_EnableWakeUp()
36           (++) HAL_SMBUSEx_DisableWakeUp()
37     (#) Configure the enable or disable of fast mode plus driving capability using the functions :
38           (++) HAL_SMBUSEx_ConfigFastModePlus()
39     (#) Set or get or clear the autonomous mode configuration using these functions :
40           (++) HAL_SMBUSEx_SetConfigAutonomousMode()
41           (++) HAL_SMBUSEx_GetConfigAutonomousMode()
42           (++) HAL_SMBUSEx_ClearConfigAutonomousMode()
43   @endverbatim
44   */
45 
46 /* Includes ------------------------------------------------------------------*/
47 #include "stm32wbaxx_hal.h"
48 
49 /** @addtogroup STM32WBAxx_HAL_Driver
50   * @{
51   */
52 
53 /** @defgroup SMBUSEx SMBUSEx
54   * @brief SMBUS Extended HAL module driver
55   * @{
56   */
57 
58 #ifdef HAL_SMBUS_MODULE_ENABLED
59 
60 /* Private typedef -----------------------------------------------------------*/
61 /* Private define ------------------------------------------------------------*/
62 /* Private macro -------------------------------------------------------------*/
63 /* Private variables ---------------------------------------------------------*/
64 /* Private function prototypes -----------------------------------------------*/
65 /* Private functions ---------------------------------------------------------*/
66 
67 /** @defgroup SMBUSEx_Exported_Functions SMBUS Extended Exported Functions
68   * @{
69   */
70 
71 /** @defgroup SMBUSEx_Exported_Functions_Group2 WakeUp Mode Functions
72   * @brief    WakeUp Mode Functions
73   *
74 @verbatim
75  ===============================================================================
76                       ##### WakeUp Mode Functions #####
77  ===============================================================================
78     [..] This section provides functions allowing to:
79       (+) Configure Wake Up Feature
80 
81 @endverbatim
82   * @{
83   */
84 
85 /**
86   * @brief  Enable SMBUS wakeup from Stop mode(s).
87   * @param  hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
88   *                the configuration information for the specified SMBUSx peripheral.
89   * @retval HAL status
90   */
HAL_SMBUSEx_EnableWakeUp(SMBUS_HandleTypeDef * hsmbus)91 HAL_StatusTypeDef HAL_SMBUSEx_EnableWakeUp(SMBUS_HandleTypeDef *hsmbus)
92 {
93   /* Check the parameters */
94   assert_param(IS_I2C_WAKEUP_FROMSTOP_INSTANCE(hsmbus->Instance));
95 
96   if (hsmbus->State == HAL_SMBUS_STATE_READY)
97   {
98     /* Process Locked */
99     __HAL_LOCK(hsmbus);
100 
101     hsmbus->State = HAL_SMBUS_STATE_BUSY;
102 
103     /* Disable the selected SMBUS peripheral */
104     __HAL_SMBUS_DISABLE(hsmbus);
105 
106     /* Enable wakeup from stop mode */
107     hsmbus->Instance->CR1 |= I2C_CR1_WUPEN;
108 
109     __HAL_SMBUS_ENABLE(hsmbus);
110 
111     hsmbus->State = HAL_SMBUS_STATE_READY;
112 
113     /* Process Unlocked */
114     __HAL_UNLOCK(hsmbus);
115 
116     return HAL_OK;
117   }
118   else
119   {
120     return HAL_BUSY;
121   }
122 }
123 
124 /**
125   * @brief  Disable SMBUS wakeup from Stop mode(s).
126   * @param  hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
127   *                the configuration information for the specified SMBUSx peripheral.
128   * @retval HAL status
129   */
HAL_SMBUSEx_DisableWakeUp(SMBUS_HandleTypeDef * hsmbus)130 HAL_StatusTypeDef HAL_SMBUSEx_DisableWakeUp(SMBUS_HandleTypeDef *hsmbus)
131 {
132   /* Check the parameters */
133   assert_param(IS_I2C_WAKEUP_FROMSTOP_INSTANCE(hsmbus->Instance));
134 
135   if (hsmbus->State == HAL_SMBUS_STATE_READY)
136   {
137     /* Process Locked */
138     __HAL_LOCK(hsmbus);
139 
140     hsmbus->State = HAL_SMBUS_STATE_BUSY;
141 
142     /* Disable the selected SMBUS peripheral */
143     __HAL_SMBUS_DISABLE(hsmbus);
144 
145     /* Disable wakeup from stop mode */
146     hsmbus->Instance->CR1 &= ~(I2C_CR1_WUPEN);
147 
148     __HAL_SMBUS_ENABLE(hsmbus);
149 
150     hsmbus->State = HAL_SMBUS_STATE_READY;
151 
152     /* Process Unlocked */
153     __HAL_UNLOCK(hsmbus);
154 
155     return HAL_OK;
156   }
157   else
158   {
159     return HAL_BUSY;
160   }
161 }
162 /**
163   * @}
164   */
165 
166 /** @defgroup SMBUSEx_Exported_Functions_Group3 Fast Mode Plus Functions
167   * @brief    Fast Mode Plus Functions
168   *
169 @verbatim
170  ===============================================================================
171                       ##### Fast Mode Plus Functions #####
172  ===============================================================================
173     [..] This section provides functions allowing to:
174       (+) Configure Fast Mode Plus
175 
176 @endverbatim
177   * @{
178   */
179 
180 /**
181   * @brief  Configure SMBUS Fast Mode Plus.
182   * @param  hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
183   *                the configuration information for the specified SMBUSx peripheral.
184   * @param  FastModePlus New state of the Fast Mode Plus.
185   * @retval HAL status
186   */
HAL_SMBUSEx_ConfigFastModePlus(SMBUS_HandleTypeDef * hsmbus,uint32_t FastModePlus)187 HAL_StatusTypeDef HAL_SMBUSEx_ConfigFastModePlus(SMBUS_HandleTypeDef *hsmbus, uint32_t FastModePlus)
188 {
189   /* Check the parameters */
190   assert_param(IS_SMBUS_ALL_INSTANCE(hsmbus->Instance));
191   assert_param(IS_SMBUS_FASTMODEPLUS(FastModePlus));
192 
193   if (hsmbus->State == HAL_SMBUS_STATE_READY)
194   {
195     /* Process Locked */
196     __HAL_LOCK(hsmbus);
197 
198     hsmbus->State = HAL_SMBUS_STATE_BUSY;
199 
200     /* Disable the selected SMBUS peripheral */
201     __HAL_SMBUS_DISABLE(hsmbus);
202 
203     if (FastModePlus == SMBUS_FASTMODEPLUS_ENABLE)
204     {
205       /* Set SMBUSx FMP bit */
206       hsmbus->Instance->CR1 |= (I2C_CR1_FMP);
207     }
208     else
209     {
210       /* Reset SMBUSx FMP bit */
211       hsmbus->Instance->CR1 &= ~(I2C_CR1_FMP);
212     }
213 
214     __HAL_SMBUS_ENABLE(hsmbus);
215 
216     hsmbus->State = HAL_SMBUS_STATE_READY;
217 
218     /* Process Unlocked */
219     __HAL_UNLOCK(hsmbus);
220 
221     return HAL_OK;
222   }
223   else
224   {
225     return HAL_BUSY;
226   }
227 }
228 
229 /**
230   * @}
231   */
232 
233 /**
234   * @}
235   */
236 
237 /** @defgroup SMBUSEx_Exported_Functions_Group4 Autonomous Mode Functions
238   * @brief    Autonomous Mode Functions
239   *
240 @verbatim
241  ===============================================================================
242                      ##### Autonomous Mode functions #####
243  ===============================================================================
244     [..] This section provides functions allowing to:
245       (+) Configure Autonomous Mode
246 
247 @endverbatim
248   * @{
249   */
250 
251 /**
252   * @brief  Set Autonomous Mode configuration
253   * @param  hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
254   *                the configuration information for the specified SMBUSx peripheral.
255   * @param  sConfig Pointer to a SMBUS_AutonomousModeConfTypeDef structure that contains
256   *                the configuration information of the autonomous mode for the specified SMBUSx peripheral.
257   * @retval HAL status
258   */
HAL_SMBUSEx_SetConfigAutonomousMode(SMBUS_HandleTypeDef * hsmbus,const SMBUS_AutonomousModeConfTypeDef * sConfig)259 HAL_StatusTypeDef HAL_SMBUSEx_SetConfigAutonomousMode(SMBUS_HandleTypeDef *hsmbus,
260                                                       const SMBUS_AutonomousModeConfTypeDef *sConfig)
261 {
262   if (hsmbus->State == HAL_SMBUS_STATE_READY)
263   {
264     /* Process Locked */
265     __HAL_LOCK(hsmbus);
266 
267     hsmbus->State = HAL_SMBUS_STATE_BUSY;
268 
269     /* Check the parameters */
270     assert_param(IS_SMBUS_TRIG_INPUT_INSTANCE(hsmbus->Instance));
271     assert_param(IS_SMBUS_TRIG_SOURCE(hsmbus->Instance, sConfig->TriggerSelection));
272     assert_param(IS_SMBUS_AUTO_MODE_TRG_POL(sConfig->TriggerPolarity));
273 
274     /* Disable the selected SMBUS peripheral to be able to configure AUTOCR */
275     __HAL_SMBUS_DISABLE(hsmbus);
276 
277     /* SMBUSx AUTOCR Configuration */
278     WRITE_REG(hsmbus->Instance->AUTOCR,
279               (sConfig->TriggerState | \
280                ((sConfig->TriggerSelection) & I2C_AUTOCR_TRIGSEL_Msk) | \
281                sConfig->TriggerPolarity));
282 
283     /* Enable the selected SMBUS peripheral */
284     __HAL_SMBUS_ENABLE(hsmbus);
285 
286     hsmbus->State = HAL_SMBUS_STATE_READY;
287 
288     /* Process Unlocked */
289     __HAL_UNLOCK(hsmbus);
290 
291     return HAL_OK;
292   }
293   else
294   {
295     return HAL_ERROR;
296   }
297 }
298 
299 /**
300   * @brief  Get Autonomous Mode configuration
301   * @param  hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
302   *                the configuration information for the specified SMBUSx peripheral.
303   * @param  sConfig Pointer to a SMBUS_AutonomousModeConfTypeDef structure that contains
304   *                the configuration information of the autonomous mode for the specified SMBUSx peripheral.
305   * @retval HAL status
306   */
HAL_SMBUSEx_GetConfigAutonomousMode(const SMBUS_HandleTypeDef * hsmbus,SMBUS_AutonomousModeConfTypeDef * sConfig)307 HAL_StatusTypeDef HAL_SMBUSEx_GetConfigAutonomousMode(const SMBUS_HandleTypeDef *hsmbus,
308                                                       SMBUS_AutonomousModeConfTypeDef *sConfig)
309 {
310   uint32_t autocr_tmp;
311 
312   /* Check the parameters */
313   assert_param(IS_SMBUS_TRIG_INPUT_INSTANCE(hsmbus->Instance));
314 
315   autocr_tmp = hsmbus->Instance->AUTOCR;
316 
317   sConfig->TriggerState     = (autocr_tmp & I2C_AUTOCR_TRIGEN);
318 #if defined(SMBUS_TRIG_GRP1)
319   if (IS_SMBUS_GRP2_INSTANCE(hsmbus->Instance))
320   {
321     sConfig->TriggerSelection = ((autocr_tmp & I2C_AUTOCR_TRIGSEL) | SMBUS_TRIG_GRP2);
322   }
323   else
324   {
325     sConfig->TriggerSelection = ((autocr_tmp & I2C_AUTOCR_TRIGSEL) | SMBUS_TRIG_GRP1);
326   }
327 #else
328   sConfig->TriggerSelection = ((autocr_tmp & I2C_AUTOCR_TRIGSEL) | SMBUS_TRIG_GRP2);
329 #endif /* SMBUS_TRIG_GRP1 */
330   sConfig->TriggerPolarity  = (autocr_tmp & I2C_AUTOCR_TRIGPOL);
331 
332   return HAL_OK;
333 }
334 
335 /**
336   * @brief  Clear Autonomous Mode configuration
337   * @param  hsmbus Pointer to a SMBUS_HandleTypeDef structure that contains
338   *                the configuration information for the specified SMBUS peripheral.
339   * @retval HAL status
340   */
HAL_SMBUSEx_ClearConfigAutonomousMode(SMBUS_HandleTypeDef * hsmbus)341 HAL_StatusTypeDef HAL_SMBUSEx_ClearConfigAutonomousMode(SMBUS_HandleTypeDef *hsmbus)
342 {
343   if (hsmbus->State == HAL_SMBUS_STATE_READY)
344   {
345     /* Process Locked */
346     __HAL_LOCK(hsmbus);
347 
348     hsmbus->State = HAL_SMBUS_STATE_BUSY;
349 
350     /* Check the parameters */
351     assert_param(IS_SMBUS_TRIG_INPUT_INSTANCE(hsmbus->Instance));
352 
353     /* Disable the selected SMBUS peripheral to be able to clear AUTOCR */
354     __HAL_SMBUS_DISABLE(hsmbus);
355 
356     CLEAR_REG(hsmbus->Instance->AUTOCR);
357 
358     /* Enable the selected SMBUS peripheral */
359     __HAL_SMBUS_ENABLE(hsmbus);
360 
361     hsmbus->State = HAL_SMBUS_STATE_READY;
362 
363     /* Process Unlocked */
364     __HAL_UNLOCK(hsmbus);
365 
366     return HAL_OK;
367   }
368   else
369   {
370     return HAL_ERROR;
371   }
372 }
373 /**
374   * @}
375   */
376 
377 /**
378   * @}
379   */
380 
381 #endif /* HAL_SMBUS_MODULE_ENABLED */
382 /**
383   * @}
384   */
385 
386 /**
387   * @}
388   */
389