-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstm32l4r9i_discovery_idd.c
364 lines (321 loc) · 7.6 KB
/
stm32l4r9i_discovery_idd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/**
******************************************************************************
* @file stm32l4r9i_discovery_idd.c
* @author MCD Application Team
* @brief This file provides a set of firmware functions to manage the
* Idd measurement driver for STM32L4R9I_DISCOVERY board.
******************************************************************************
* @attention
*
* Copyright (c) 2017 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32l4r9i_discovery_idd.h"
#include "stm32l4r9i_discovery_io.h"
/** @addtogroup BSP
* @{
*/
/** @addtogroup STM32L4R9I_DISCOVERY
* @{
*/
/** @defgroup STM32L4R9I_DISCOVERY_IDD STM32L4R9I_DISCOVERY IDD
* @brief This file includes the Idd driver for STM32L4R9I_DISCOVERY board.
* It allows user to measure MCU Idd current on board, especially in
* different low power modes.
* @{
*/
/** @defgroup STM32L4R9I_DISCOVERY_IDD_Private_Variables Private Variables
* @{
*/
static IDD_DrvTypeDef *IddDrv;
/**
* @}
*/
/** @defgroup STM32L4R9I_DISCOVERY_IDD_Exported_Functions Exported Functions
* @{
*/
/**
* @brief Configures IDD measurement component.
* @retval IDD_OK if no problem during initialization
*/
uint8_t BSP_IDD_Init(void)
{
IDD_ConfigTypeDef iddconfig = {0};
uint8_t mfxstm32l152_id = 0;
uint8_t ret = 0;
/* Initialize IO functionalities (MFX) used by control amp */
BSP_IO_Init();
/* wake up mfx component in case it went to standby mode */
mfxstm32l152_idd_drv.WakeUp(IDD_I2C_ADDRESS);
HAL_Delay(5);
/* Read ID and verify if the MFX is ready */
mfxstm32l152_id = mfxstm32l152_idd_drv.ReadID(IDD_I2C_ADDRESS);
if((mfxstm32l152_id == MFXSTM32L152_ID_1) || (mfxstm32l152_id == MFXSTM32L152_ID_2))
{
/* Initialize the Idd driver structure */
IddDrv = &mfxstm32l152_idd_drv;
/* Initialize the Idd driver */
if(IddDrv->Init != NULL)
{
IddDrv->Init(IDD_I2C_ADDRESS);
}
/* Configure Idd component with default values */
iddconfig.AmpliGain = DISCOVERY_IDD_AMPLI_GAIN;
iddconfig.VddMin = DISCOVERY_IDD_VDD_MIN;
iddconfig.Shunt0Value = DISCOVERY_IDD_SHUNT0_VALUE;
iddconfig.Shunt1Value = DISCOVERY_IDD_SHUNT1_VALUE;
iddconfig.Shunt2Value = DISCOVERY_IDD_SHUNT2_VALUE;
iddconfig.Shunt3Value = 0;
iddconfig.Shunt4Value = DISCOVERY_IDD_SHUNT4_VALUE;
iddconfig.Shunt0StabDelay = DISCOVERY_IDD_SHUNT0_STABDELAY;
iddconfig.Shunt1StabDelay = DISCOVERY_IDD_SHUNT1_STABDELAY;
iddconfig.Shunt2StabDelay = DISCOVERY_IDD_SHUNT2_STABDELAY;
iddconfig.Shunt3StabDelay = 0;
iddconfig.Shunt4StabDelay = DISCOVERY_IDD_SHUNT4_STABDELAY;
iddconfig.ShuntNbOnBoard = MFXSTM32L152_IDD_SHUNT_NB_4;
iddconfig.ShuntNbUsed = MFXSTM32L152_IDD_SHUNT_NB_4;
iddconfig.VrefMeasurement = MFXSTM32L152_IDD_VREF_AUTO_MEASUREMENT_ENABLE;
iddconfig.Calibration = MFXSTM32L152_IDD_AUTO_CALIBRATION_ENABLE;
iddconfig.PreDelayUnit = MFXSTM32L152_IDD_PREDELAY_20_MS;
iddconfig.PreDelayValue = 0x7F;
iddconfig.MeasureNb = 100;
iddconfig.DeltaDelayUnit= MFXSTM32L152_IDD_DELTADELAY_0_5_MS;
iddconfig.DeltaDelayValue = 10;
BSP_IDD_Config(iddconfig);
ret = IDD_OK;
}
else
{
ret = IDD_ERROR;
}
return ret;
}
/**
* @brief Unconfigures IDD measurement component.
* @retval IDD_OK if no problem during deinitialization
*/
void BSP_IDD_DeInit(void)
{
if(IddDrv->DeInit!= NULL)
{
IddDrv->DeInit(IDD_I2C_ADDRESS);
}
}
/**
* @brief Reset Idd measurement component.
* @retval None
*/
void BSP_IDD_Reset(void)
{
if(IddDrv->Reset != NULL)
{
IddDrv->Reset(IDD_I2C_ADDRESS);
}
}
/**
* @brief Turn Idd measurement component in low power (standby/sleep) mode
* @retval None
*/
void BSP_IDD_LowPower(void)
{
if(IddDrv->LowPower != NULL)
{
IddDrv->LowPower(IDD_I2C_ADDRESS);
}
}
/**
* @brief Start Measurement campaign
* @retval None
*/
void BSP_IDD_StartMeasure(void)
{
/* Activate the OPAMP used by the MFX to measure the current consumption */
BSP_IO_ConfigPin(IDD_AMP_CONTROL_PIN, IO_MODE_OUTPUT);
BSP_IO_WritePin(IDD_AMP_CONTROL_PIN, GPIO_PIN_RESET);
if(IddDrv->Start != NULL)
{
IddDrv->Start(IDD_I2C_ADDRESS);
}
}
/**
* @brief Configure Idd component
* @param IddConfig: structure of idd parameters
* @retval None
*/
void BSP_IDD_Config(IDD_ConfigTypeDef IddConfig)
{
if(IddDrv->Config != NULL)
{
IddDrv->Config(IDD_I2C_ADDRESS, IddConfig);
}
}
/**
* @brief Get Idd current value.
* @param IddValue: Pointer on u32 to store Idd. Value unit is 10 nA.
* @retval None
*/
void BSP_IDD_GetValue(uint32_t *IddValue)
{
/* De-activate the OPAMP used by the MFX to measure the current consumption */
BSP_IO_ConfigPin(IDD_AMP_CONTROL_PIN, IO_MODE_OUTPUT);
BSP_IO_WritePin(IDD_AMP_CONTROL_PIN, GPIO_PIN_SET);
if(IddDrv->GetValue != NULL)
{
IddDrv->GetValue(IDD_I2C_ADDRESS, IddValue);
}
}
/**
* @brief Enable Idd interrupt that warn end of measurement
* @retval None
*/
void BSP_IDD_EnableIT(void)
{
if(IddDrv->EnableIT != NULL)
{
IddDrv->EnableIT(IDD_I2C_ADDRESS);
}
}
/**
* @brief Clear Idd interrupt that warn end of measurement
* @retval None
*/
void BSP_IDD_ClearIT(void)
{
if(IddDrv->ClearIT != NULL)
{
IddDrv->ClearIT(IDD_I2C_ADDRESS);
}
}
/**
* @brief Get Idd interrupt status
* @retval status
*/
uint8_t BSP_IDD_GetITStatus(void)
{
if(IddDrv->GetITStatus != NULL)
{
return (IddDrv->GetITStatus(IDD_I2C_ADDRESS));
}
else
{
return IDD_ERROR;
}
}
/**
* @brief Disable Idd interrupt that warn end of measurement
* @retval None
*/
void BSP_IDD_DisableIT(void)
{
if(IddDrv->DisableIT != NULL)
{
IddDrv->DisableIT(IDD_I2C_ADDRESS);
}
}
/**
* @brief Get Error Code .
* @retval Error code or error status
*/
uint8_t BSP_IDD_ErrorGetCode(void)
{
if(IddDrv->ErrorGetSrc != NULL)
{
if((IddDrv->ErrorGetSrc(IDD_I2C_ADDRESS) & MFXSTM32L152_IDD_ERROR_SRC) != RESET)
{
if(IddDrv->ErrorGetCode != NULL)
{
return IddDrv->ErrorGetCode(IDD_I2C_ADDRESS);
}
else
{
return IDD_ERROR;
}
}
else
{
return IDD_ERROR;
}
}
else
{
return IDD_ERROR;
}
}
/**
* @brief Enable error interrupt that warn end of measurement
* @retval None
*/
void BSP_IDD_ErrorEnableIT(void)
{
if(IddDrv->ErrorEnableIT != NULL)
{
IddDrv->ErrorEnableIT(IDD_I2C_ADDRESS);
}
}
/**
* @brief Clear Error interrupt that warn end of measurement
* @retval None
*/
void BSP_IDD_ErrorClearIT(void)
{
if(IddDrv->ErrorClearIT != NULL)
{
IddDrv->ErrorClearIT(IDD_I2C_ADDRESS);
}
}
/**
* @brief Get Error interrupt status
* @retval Status
*/
uint8_t BSP_IDD_ErrorGetITStatus(void)
{
if(IddDrv->ErrorGetITStatus != NULL)
{
return (IddDrv->ErrorGetITStatus(IDD_I2C_ADDRESS));
}
else
{
return 0;
}
}
/**
* @brief Disable Error interrupt
* @retval None
*/
void BSP_IDD_ErrorDisableIT(void)
{
if(IddDrv->ErrorDisableIT != NULL)
{
IddDrv->ErrorDisableIT(IDD_I2C_ADDRESS);
}
}
/**
* @brief Wake up Idd measurement component.
* @retval None
*/
void BSP_IDD_WakeUp(void)
{
if(IddDrv->WakeUp != NULL)
{
IddDrv->WakeUp(IDD_I2C_ADDRESS);
}
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/