-
Notifications
You must be signed in to change notification settings - Fork 96
/
pwr.c
254 lines (211 loc) · 7.07 KB
/
pwr.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
/*
Copyright 2019 - 2020 Benjamin Vedder [email protected]
This file is part of the VESC BMS firmware.
The VESC BMS firmware is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
The VESC BMS firmware is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "pwr.h"
#include "main.h"
#include <math.h>
#include <string.h>
// Settings
#define ADC_CHANNELS 11
// Private variables
static volatile float m_v_charge = 0.0;
static volatile float m_v_fuse = 0.0;
static volatile float m_i_in = 0.0;
static volatile float m_temps[HW_ADC_TEMP_SENSORS] = {0.0};
static volatile float m_temp_volts[HW_ADC_TEMP_SENSORS] = {0.0};
static THD_WORKING_AREA(adc_thd_wa, 2048);
#ifdef ADC_CH_V_CHARGE
#define ADC_CHx_V_CHARGE ADC_CH_V_CHARGE
#else
#define ADC_CHx_V_CHARGE 0
#endif
#ifdef ADC_CH_CURRENT
#define ADC_CHx_CURRENT ADC_CH_CURRENT
#else
#define ADC_CHx_CURRENT 0
#endif
#ifdef ADC_CH_V_FUSE
#define ADC_CHx_V_FUSE ADC_CH_V_FUSE
#else
#define ADC_CHx_V_FUSE 0
#endif
const ADCConversionGroup adcgrpcfg1 = {
.circular = false,
.num_channels = ADC_CHANNELS,
.end_cb = NULL,
.error_cb = NULL,
.cfgr = ADC_CFGR_CONT,
.cfgr2 = 0U,
.tr1 = ADC_TR(0, 4095),
.smpr = {
ADC_SMPR1_SMP_AN0(ADC_SMPR_SMP_92P5) | ADC_SMPR1_SMP_AN1(ADC_SMPR_SMP_92P5) |
ADC_SMPR1_SMP_AN2(ADC_SMPR_SMP_92P5) | ADC_SMPR1_SMP_AN3(ADC_SMPR_SMP_92P5) |
ADC_SMPR1_SMP_AN4(ADC_SMPR_SMP_92P5) | ADC_SMPR1_SMP_AN5(ADC_SMPR_SMP_92P5) |
ADC_SMPR1_SMP_AN6(ADC_SMPR_SMP_92P5) | ADC_SMPR1_SMP_AN7(ADC_SMPR_SMP_92P5) |
ADC_SMPR1_SMP_AN8(ADC_SMPR_SMP_92P5) | ADC_SMPR1_SMP_AN9(ADC_SMPR_SMP_92P5),
ADC_SMPR2_SMP_AN10(ADC_SMPR_SMP_92P5) | ADC_SMPR2_SMP_AN11(ADC_SMPR_SMP_92P5) |
ADC_SMPR2_SMP_AN12(ADC_SMPR_SMP_92P5) | ADC_SMPR2_SMP_AN13(ADC_SMPR_SMP_92P5) |
ADC_SMPR2_SMP_AN14(ADC_SMPR_SMP_92P5) | ADC_SMPR2_SMP_AN15(ADC_SMPR_SMP_92P5) |
ADC_SMPR2_SMP_AN16(ADC_SMPR_SMP_92P5) | ADC_SMPR2_SMP_AN17(ADC_SMPR_SMP_92P5) |
ADC_SMPR2_SMP_AN18(ADC_SMPR_SMP_92P5)
},
.sqr = {
ADC_SQR1_SQ1_N(ADC_CHANNEL_IN0) | ADC_SQR1_SQ2_N(ADC_CHx_V_CHARGE) |
ADC_SQR1_SQ3_N(ADC_CHx_CURRENT) | ADC_SQR1_SQ4_N(ADC_CH_TEMP0),
ADC_SQR2_SQ5_N(ADC_CH_TEMP1) | ADC_SQR2_SQ6_N(ADC_CH_TEMP2) |
ADC_SQR2_SQ7_N(ADC_CH_TEMP3) | ADC_SQR2_SQ8_N(ADC_CH_TEMP4) |
ADC_SQR2_SQ9_N(ADC_CH_TEMP5),
ADC_SQR3_SQ10_N(ADC_CH_TEMP6) | ADC_SQR3_SQ11_N(ADC_CHx_V_FUSE),
0U
}
};
#ifdef BUZZER_LINE
static PWMConfig pwmcfg = {
1000000,
1000000 / BUZZER_FREQ_HZ,
0,
{
{PWM_OUTPUT_ACTIVE_HIGH, NULL},
{PWM_OUTPUT_ACTIVE_HIGH, NULL},
{PWM_OUTPUT_ACTIVE_HIGH, NULL},
{PWM_OUTPUT_ACTIVE_HIGH, NULL}
},
0,
0
};
#endif
static THD_FUNCTION(adc_thd, p) {
(void)p;
chRegSetThreadName("ADC");
adcStart(&ADCD1, NULL);
adcSTM32EnableVREF(&ADCD1);
chThdSleepMilliseconds(1);
while (!chThdShouldTerminateX()) {
int num_samp = 8;
adcsample_t samples[num_samp * ADC_CHANNELS];
adcConvert(&ADCD1, &adcgrpcfg1, samples, num_samp);
float v_ch = 0.0;
float ref = 0.0;
float i_in = 0.0;
float v_fuse = 0.0;
float temps[HW_ADC_TEMP_SENSORS];
memset(temps, 0, sizeof(temps));
for (int i = 0;i < num_samp;i++) {
ref += samples[ADC_CHANNELS * i + 0];
v_ch += samples[ADC_CHANNELS * i + 1];
i_in += samples[ADC_CHANNELS * i + 2];
for (int j = 0;j < HW_ADC_TEMP_SENSORS;j++) {
temps[j] += samples[ADC_CHANNELS * i + 3 + j];
}
v_fuse += samples[ADC_CHANNELS * i + HW_ADC_TEMP_SENSORS + 3];
}
ref /= (float)num_samp;
v_ch /= (float)num_samp;
i_in /= (float)num_samp;
v_fuse /= (float)num_samp;
for (int j = 0;j < HW_ADC_TEMP_SENSORS;j++) {
temps[j] /= (float)num_samp;
}
#if defined(ADC_CH_V_CHARGE) || defined(ADC_CH_CURRENT) || defined(ADC_CH_V_FUSE)
uint16_t vrefint_cal = *((uint16_t*)((uint32_t)0x1FFF75AA));
float vdda = (3.0 * (float)vrefint_cal) / (float)ref;
#endif
#ifdef ADC_CH_V_CHARGE
m_v_charge = (v_ch / (4095 / vdda)) * ((R_CHARGE_TOP + R_CHARGE_BOTTOM) / R_CHARGE_BOTTOM);
#endif
#ifdef ADC_CH_CURRENT
m_i_in = -((3.3 * ((i_in / 4095.0))) - 1.65) * (1.0 / HW_SHUNT_AMP_GAIN) * (1.0 / backup.config.ext_shunt_res);
#endif
#ifdef ADC_CH_V_FUSE
m_v_fuse = (v_fuse / (4095 / vdda)) * ((R_CHARGE_TOP + R_CHARGE_BOTTOM) / R_CHARGE_BOTTOM);
#endif
for (int j = 0;j < HW_ADC_TEMP_SENSORS;j++) {
m_temps[j] = NTC_TEMP_WITH_IND(temps[j], j);
m_temp_volts[j] = 3.3 * temps[j] / 4095.0;
}
chThdSleepMilliseconds(1);
}
}
void pwr_init(void) {
#ifdef LINE_BQ_CHG_EN
palSetLineMode(LINE_BQ_CHG_EN, PAL_MODE_OUTPUT_PUSHPULL);
palSetLineMode(LINE_BQ_CP_EN, PAL_MODE_OUTPUT_PUSHPULL);
palSetLineMode(LINE_BQ_DSG_EN, PAL_MODE_OUTPUT_PUSHPULL);
palSetLineMode(LINE_BQ_PMON_EN, PAL_MODE_OUTPUT_PUSHPULL);
palSetLineMode(LINE_BQ_PCHG_EN, PAL_MODE_OUTPUT_PUSHPULL);
BQ_CHG_OFF();
BQ_CP_OFF();
BQ_DSG_OFF();
BQ_PMON_OFF();
BQ_PCHG_OFF();
#endif
#ifdef LINE_V_CHARGE
palSetLineMode(LINE_V_CHARGE, PAL_MODE_INPUT_ANALOG);
#endif
#ifdef LINE_V_FUSE
palSetLineMode(LINE_V_FUSE, PAL_MODE_INPUT_ANALOG);
#endif
#ifdef LINE_CURRENT
palSetLineMode(LINE_CURRENT, PAL_MODE_INPUT_ANALOG);
#endif
palSetLineMode(LINE_TEMP_0, PAL_MODE_INPUT_ANALOG);
palSetLineMode(LINE_TEMP_1, PAL_MODE_INPUT_ANALOG);
palSetLineMode(LINE_TEMP_2, PAL_MODE_INPUT_ANALOG);
palSetLineMode(LINE_TEMP_3, PAL_MODE_INPUT_ANALOG);
palSetLineMode(LINE_TEMP_4, PAL_MODE_INPUT_ANALOG);
palSetLineMode(LINE_TEMP_5, PAL_MODE_INPUT_ANALOG);
palSetLineMode(LINE_TEMP_6, PAL_MODE_INPUT_ANALOG);
palSetLineMode(LINE_TEMP_0_EN, PAL_MODE_OUTPUT_PUSHPULL);
palSetLineMode(LINE_TEMP_1_EN, PAL_MODE_OUTPUT_PUSHPULL);
palSetLineMode(LINE_TEMP_2_EN, PAL_MODE_OUTPUT_PUSHPULL);
palSetLineMode(LINE_TEMP_3_EN, PAL_MODE_OUTPUT_PUSHPULL);
palSetLineMode(LINE_TEMP_4_EN, PAL_MODE_OUTPUT_PUSHPULL);
palSetLineMode(LINE_TEMP_5_EN, PAL_MODE_OUTPUT_PUSHPULL);
palSetLineMode(LINE_TEMP_6_EN, PAL_MODE_OUTPUT_PUSHPULL);
TEMP_MEASURE_ON();
CURR_MEASURE_ON();
HW_CAN_ON();
#ifdef LINE_BQ_CHG_EN
BQ_CP_ON();
BQ_PMON_ON();
#endif
#ifdef BUZZER_LINE
pwmStart(&BUZZER_PWM, &pwmcfg);
palSetLineMode(BUZZER_LINE, PAL_MODE_ALTERNATE(BUZZER_AF));
BUZZER_OFF();
#endif
chThdSleepMilliseconds(10);
chThdCreateStatic(adc_thd_wa, sizeof(adc_thd_wa), NORMALPRIO, adc_thd, 0);
}
float pwr_get_vcharge(void) {
return m_v_charge;
}
float pwr_get_vfuse(void) {
return m_v_fuse;
}
float pwr_get_iin(void) {
return m_i_in;
}
float pwr_get_temp(int sensor) {
if (sensor < 0 || sensor >= HW_ADC_TEMP_SENSORS) {
return -1.0;
}
return m_temps[sensor];
}
float pwr_get_temp_volt(int sensor) {
if (sensor < 0 || sensor >= HW_ADC_TEMP_SENSORS) {
return -1.0;
}
return m_temp_volts[sensor];
}