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