/
pwrctl.c
338 lines (312 loc) · 9.72 KB
/
pwrctl.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
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 Johan Kanflo (github.com/kanflo)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "pwrctl.h"
#include "dps-model.h"
#include "pastunits.h"
#include <gpio.h>
#include <dac.h>
/** This module handles voltage and current calculations
* Calculations based on measurements found at
* https://docs.google.com/spreadsheets/d/1AhGsU_gvZjqZyr2ZYrnkz6BeUqMquzh9UNYoTqy_Zp4/edit?usp=sharing
*/
static uint32_t i_out, v_out, i_limit, v_limit;
static bool v_out_enabled;
float a_adc_k_coef = A_ADC_K;
float a_adc_c_coef = A_ADC_C;
float a_dac_k_coef = A_DAC_K;
float a_dac_c_coef = A_DAC_C;
float v_adc_k_coef = V_ADC_K;
float v_adc_c_coef = V_ADC_C;
float v_dac_k_coef = V_DAC_K;
float v_dac_c_coef = V_DAC_C;
float vin_adc_k_coef = VIN_ADC_K;
float vin_adc_c_coef = VIN_ADC_C;
/** not static as it is referred to from hw.c for performance reasons */
uint32_t pwrctl_i_limit_raw;
uint32_t pwrctl_v_limit_raw;
/**
* @brief Initialize the power control module
* @retval none
*/
void pwrctl_init(past_t *past)
{
uint32_t length;
float *p;
/** Load default calibration constants */
a_adc_k_coef = A_ADC_K;
a_adc_c_coef = A_ADC_C;
a_dac_k_coef = A_DAC_K;
a_dac_c_coef = A_DAC_C;
v_adc_k_coef = V_ADC_K;
v_adc_c_coef = V_ADC_C;
v_dac_k_coef = V_DAC_K;
v_dac_c_coef = V_DAC_C;
vin_adc_k_coef = VIN_ADC_K;
vin_adc_c_coef = VIN_ADC_C;
/** Load any calibration constants that maybe stored in non-volatile memory (past) */
if (past_read_unit(past, past_A_ADC_K, (const void**) &p, &length))
a_adc_k_coef = *p;
if (past_read_unit(past, past_A_ADC_C, (const void**) &p, &length))
a_adc_c_coef = *p;
if (past_read_unit(past, past_A_DAC_K, (const void**) &p, &length))
a_dac_k_coef = *p;
if (past_read_unit(past, past_A_DAC_C, (const void**) &p, &length))
a_dac_c_coef = *p;
if (past_read_unit(past, past_V_ADC_K, (const void**) &p, &length))
v_adc_k_coef = *p;
if (past_read_unit(past, past_V_ADC_C, (const void**) &p, &length))
v_adc_c_coef = *p;
if (past_read_unit(past, past_V_DAC_K, (const void**) &p, &length))
v_dac_k_coef = *p;
if (past_read_unit(past, past_V_DAC_C, (const void**) &p, &length))
v_dac_c_coef = *p;
if (past_read_unit(past, past_VIN_ADC_K, (const void**) &p, &length))
vin_adc_k_coef = *p;
if (past_read_unit(past, past_VIN_ADC_C, (const void**) &p, &length))
vin_adc_c_coef = *p;
pwrctl_enable_vout(false);
}
/**
* @brief Set voltage output
* @param value_mv voltage in milli volt
* @retval true requested voltage was within specs
*/
bool pwrctl_set_vout(uint32_t value_mv)
{
/** @todo Check with max Vout, currently filtered by ui.c */
v_out = value_mv;
if (v_out_enabled) {
/** Needed for the DPS5005 "communications version" (the one with BT/USB) */
DAC_DHR12R1 = pwrctl_calc_vout_dac(v_out);
} else {
DAC_DHR12R1 = 0;
}
return true;
}
/**
* @brief Set current output
* @param current_ma current in milli ampere
* @retval true requested current was within specs
*/
bool pwrctl_set_iout(uint32_t value_ma)
{
i_out = value_ma;
if (v_out_enabled) {
DAC_DHR12R2 = pwrctl_calc_iout_dac(value_ma);
} else {
DAC_DHR12R2 = 0;
}
return true;
}
/**
* @brief Get current output setting
* @retval current setting in milli amps
*/
uint32_t pwrctl_get_iout(void)
{
return i_out;
}
/**
* @brief Get voltage output setting
* @retval current setting in milli volt
*/
uint32_t pwrctl_get_vout(void)
{
return v_out;
}
/**
* @brief Set current limit
* @param value_ma limit in milliampere
* @retval true requested current was within specs
*/
bool pwrctl_set_ilimit(uint32_t value_ma)
{
/** @todo Check with I_limit, currently filtered by ui.c */
i_limit = value_ma;
pwrctl_i_limit_raw = pwrctl_calc_ilimit_adc(i_limit);
return true;
}
/**
* @brief Get current limit setting
* @retval current setting in milliampere
*/
uint32_t pwrctl_get_ilimit(void)
{
return i_limit;
}
/**
* @brief Set voltage limit
* @param value_mv limit in millivolts
* @retval true requested voltage was within specs
*/
bool pwrctl_set_vlimit(uint32_t value_mv)
{
/** @todo Check with V_limit, currently filtered by ui.c */
v_limit = value_mv;
pwrctl_v_limit_raw = pwrctl_calc_vlimit_adc(v_limit);
return true;
}
/**
* @brief Get current limit setting
* @retval current setting in milliampere
*/
uint32_t pwrctl_get_vlimit(void)
{
return v_limit;
}
/**
* @brief Enable or disable power output
* @param enable true for enable, false for disable
* @retval none
*/
void pwrctl_enable_vout(bool enable)
{
v_out_enabled = enable;
if (v_out_enabled) {
(void) pwrctl_set_vout(v_out);
(void) pwrctl_set_iout(i_out);
#if defined(DPS5015) || defined(DPS5020)
//gpio_clear(GPIOA, GPIO9); // this is power control on '5015
gpio_set(GPIOB, GPIO11); // B11 is fan control on '5015
gpio_clear(GPIOC, GPIO13); // C13 is power control on '5015
#else
gpio_clear(GPIOB, GPIO11); // B11 is power control on '5005
#endif
} else {
#if defined(DPS5015) || defined(DPS5020)
//gpio_set(GPIOA, GPIO9); // gpio_set(GPIOB, GPIO11);
gpio_clear(GPIOB, GPIO11); // B11 is fan control on '5015
gpio_set(GPIOC, GPIO13); // C13 is power control on '5015
#else
gpio_set(GPIOB, GPIO11); // B11 is power control on '5005
#endif
(void) pwrctl_set_vout(v_out);
(void) pwrctl_set_iout(i_out);
}
}
/**
* @brief Return power output status
* @retval true if power output is enabled
*/
bool pwrctl_vout_enabled(void)
{
return v_out_enabled;
}
/**
* @brief Calculate V_in based on raw ADC measurement
* @param raw value from ADC
* @retval corresponding voltage in milli volt
*/
uint32_t pwrctl_calc_vin(uint16_t raw)
{
float value = vin_adc_k_coef * raw + vin_adc_c_coef;
if (value <= 0)
return 0;
else
return value + 0.5f; /** Add 0.5f to value so it is correctly rounded when it is truncated */
}
/**
* @brief Calculate V_out based on raw ADC measurement
* @param raw value from ADC
* @retval corresponding voltage in milli volt
*/
uint32_t pwrctl_calc_vout(uint16_t raw)
{
float value = v_adc_k_coef * raw + v_adc_c_coef;
if (value <= 0)
return 0;
else
return value + 0.5f; /** Add 0.5f to value so it is correctly rounded when it is truncated */
}
/**
* @brief Calculate DAC setting for requested V_out
* @param v_out_mv requested output voltage
* @retval corresponding 12 bit DAC value
*/
uint16_t pwrctl_calc_vout_dac(uint32_t v_out_mv)
{
float value = v_dac_k_coef * v_out_mv + v_dac_c_coef;
if (value <= 0)
return 0;
else if (value >= 0xfff)
return 0xfff; /** 12 bits */
else
return value + 0.5f; /** Add 0.5f to value so correct rounding is done when truncated */
}
/**
* @brief Calculate I_out based on raw ADC measurement
* @param raw value from ADC
* @retval corresponding current in milliampere
*/
uint32_t pwrctl_calc_iout(uint16_t raw)
{
float value = a_adc_k_coef * raw + a_adc_c_coef;
if (value <= 0)
return 0;
else
return value + 0.5f; /** Add 0.5f to value so correct rounding is done when truncated */
}
/**
* @brief Calculate expected raw ADC value based on selected I_limit
* @param i_limit_ma selected I_limit
* @retval expected raw ADC value
*/
uint32_t pwrctl_calc_ilimit_adc(uint16_t i_limit_ma)
{
float value = (i_limit_ma - a_adc_c_coef) / a_adc_k_coef + 1;
if (value <= 0)
return 0;
else
return value + 0.5f; // Add 0.5 so it is correctly rounded when it is truncated
}
/**
* @brief Calculate expected raw ADC value based on selected V_limit
* @param v_limit_mv selected V_limit
* @retval expected raw ADC value
*/
uint32_t pwrctl_calc_vlimit_adc(uint16_t v_limit_mv)
{
float value = (v_limit_mv - v_adc_c_coef) / v_adc_k_coef + 1;
if (value <= 0)
return 0;
else
return value + 0.5f; // Add 0.5 so it is correctly rounded when it is truncated
}
/**
* @brief Calculate DAC setting for constant current mode
* @param i_out_ma requested constant current
* @retval corresponding 12 bit DAC value
* @note this formula is valid for the DPS5005 and would probably need changes
* for DPS:es capable of higher current output.
*/
uint16_t pwrctl_calc_iout_dac(uint32_t i_out_ma)
{
float value = a_dac_k_coef * i_out_ma + a_dac_c_coef;
if (value <= 0)
return 0;
else if (value >= 0xfff)
return 0xfff; /** 12 bits */
else
return value + 0.5f; /** Add 0.5f to value so correct rounding is done when truncated */
}