Revision f8cf404d components/power/bq27500.cpp
components/power/bq27500.cpp | ||
---|---|---|
2 | 2 |
|
3 | 3 |
#include <ch.hpp> |
4 | 4 |
#include <chprintf.h> |
5 |
#include <global.hpp> |
|
5 | 6 |
|
6 | 7 |
#include <cstring> |
7 | 8 |
|
... | ... | |
9 | 10 |
using namespace amiro; |
10 | 11 |
using namespace BQ27500; |
11 | 12 |
|
13 |
extern Global global; |
|
14 |
|
|
12 | 15 |
Driver::Driver(I2CDriver &i2c_driver, const GPIO_TypeDef &batgd_pingrp, const uint8_t batgd_pin, const GPIO_TypeDef &batlow_pingrp, const uint8_t batlow_pin) : |
13 | 16 |
BaseSensor<BQ27500::InitData,BQ27500::CalibData>(), i2c_driver(&i2c_driver), tx_params({I2C_ADDR, NULL, 0, NULL, 0}), |
14 | 17 |
batgd_pingrp(&batgd_pingrp), batgd_pin(batgd_pin), batlow_pingrp(&batlow_pingrp), batlow_pin(batlow_pin) |
... | ... | |
26 | 29 |
msg_t |
27 | 30 |
Driver::init(InitData* initialization_data) |
28 | 31 |
{ |
29 |
chprintf((BaseSequentialStream*) &SD1, "%s(%d): TODO\n", __FILE__, __LINE__);
|
|
32 |
chprintf((BaseSequentialStream*) &global.sercanmux1, "%s(%d): TODO\n", __FILE__, __LINE__);
|
|
30 | 33 |
return NOT_IMPLEMENTED; |
31 | 34 |
} |
32 | 35 |
|
... | ... | |
64 | 67 |
DataFlashBlock block; |
65 | 68 |
this->readDataFlashBlock(block, CONFIGURATION_Safety); |
66 | 69 |
|
67 |
chprintf((BaseSequentialStream*) &SD1, "%s(%d):\n", __FILE__, __LINE__);
|
|
70 |
chprintf((BaseSequentialStream*) &global.sercanmux1, "%s(%d):\n", __FILE__, __LINE__);
|
|
68 | 71 |
for(uint8_t i = 0; i < 32; ++i) { |
69 |
chprintf((BaseSequentialStream*) &SD1, "%02X ", block.content.data[i]);
|
|
72 |
chprintf((BaseSequentialStream*) &global.sercanmux1, "%02X ", block.content.data[i]);
|
|
70 | 73 |
} |
71 |
chprintf((BaseSequentialStream*) &SD1, "\n");
|
|
72 |
chprintf((BaseSequentialStream*) &SD1, "%02X\n", block.content.checksum);
|
|
74 |
chprintf((BaseSequentialStream*) &global.sercanmux1, "\n");
|
|
75 |
chprintf((BaseSequentialStream*) &global.sercanmux1, "%02X\n", block.content.checksum);
|
|
73 | 76 |
|
74 |
chprintf((BaseSequentialStream*) &SD1, "%s(%d): TODO\n", __FILE__, __LINE__);
|
|
77 |
chprintf((BaseSequentialStream*) &global.sercanmux1, "%s(%d): TODO\n", __FILE__, __LINE__);
|
|
75 | 78 |
return NOT_IMPLEMENTED; |
76 | 79 |
} |
77 | 80 |
#endif |
... | ... | |
89 | 92 |
{ |
90 | 93 |
return ST_FAIL_READ_HW_VERSION; |
91 | 94 |
} |
92 |
chprintf((BaseSequentialStream*) &SD1, "hardware version: %X%X-%X%X (0x%04X)\n", version.content.major_high, version.content.major_low, version.content.minor_high, version.content.minor_low, version.value);
|
|
95 |
chprintf((BaseSequentialStream*) &global.sercanmux1, "hardware version: %X%X-%X%X (0x%04X)\n", version.content.major_high, version.content.major_low, version.content.minor_high, version.content.minor_low, version.value);
|
|
93 | 96 |
|
94 | 97 |
// read firmware version |
95 | 98 |
version.value = 0; |
... | ... | |
97 | 100 |
{ |
98 | 101 |
return ST_FAIL_READ_FW_VERSION; |
99 | 102 |
} |
100 |
chprintf((BaseSequentialStream*) &SD1, "firmware version: %X%X-%X%X (0x%04X)\n", version.content.major_high, version.content.major_low, version.content.minor_high, version.content.minor_low, version.value);
|
|
103 |
chprintf((BaseSequentialStream*) &global.sercanmux1, "firmware version: %X%X-%X%X (0x%04X)\n", version.content.major_high, version.content.major_low, version.content.minor_high, version.content.minor_low, version.value);
|
|
101 | 104 |
|
102 | 105 |
// read device name |
103 | 106 |
uint8_t name_length = 0; |
... | ... | |
111 | 114 |
return ST_FAIL_READ_DEVICENAME; |
112 | 115 |
} |
113 | 116 |
name_buffer[name_length] = '\0'; |
114 |
chprintf((BaseSequentialStream*) &SD1, "device name: %s (%u characters)\n", name_buffer, name_length);
|
|
117 |
chprintf((BaseSequentialStream*) &global.sercanmux1, "device name: %s (%u characters)\n", name_buffer, name_length);
|
|
115 | 118 |
|
116 | 119 |
// read the current flags |
117 | 120 |
Flags flags; |
... | ... | |
119 | 122 |
{ |
120 | 123 |
return ST_FAIL_READ_FLAGS; |
121 | 124 |
} |
122 |
chprintf((BaseSequentialStream*) &SD1, "flags: 0x%04X\n", flags.value);
|
|
123 |
chprintf((BaseSequentialStream*) &SD1, " OTC : %u\n", flags.content.otc);
|
|
124 |
chprintf((BaseSequentialStream*) &SD1, " OTD : %u\n", flags.content.otd);
|
|
125 |
chprintf((BaseSequentialStream*) &SD1, " CHG_INH : %u\n", flags.content.chg_inh);
|
|
126 |
chprintf((BaseSequentialStream*) &SD1, " XCHG : %u\n", flags.content.xchg);
|
|
127 |
chprintf((BaseSequentialStream*) &SD1, " FC : %u\n", flags.content.fc);
|
|
128 |
chprintf((BaseSequentialStream*) &SD1, " CHG : %u\n", flags.content.chg);
|
|
129 |
chprintf((BaseSequentialStream*) &SD1, " OCV_GD : %u\n", flags.content.ocv_gd);
|
|
130 |
chprintf((BaseSequentialStream*) &SD1, " WAIT_ID : %u\n", flags.content.wait_id);
|
|
131 |
chprintf((BaseSequentialStream*) &SD1, " BAT_DET : %u\n", flags.content.bat_det);
|
|
132 |
chprintf((BaseSequentialStream*) &SD1, " SOC1 : %u\n", flags.content.soc1);
|
|
133 |
chprintf((BaseSequentialStream*) &SD1, " SOCF : %u\n", flags.content.socf);
|
|
134 |
chprintf((BaseSequentialStream*) &SD1, " DSG : %u\n", flags.content.dsg);
|
|
125 |
chprintf((BaseSequentialStream*) &global.sercanmux1, "flags: 0x%04X\n", flags.value);
|
|
126 |
chprintf((BaseSequentialStream*) &global.sercanmux1, " OTC : %u\n", flags.content.otc);
|
|
127 |
chprintf((BaseSequentialStream*) &global.sercanmux1, " OTD : %u\n", flags.content.otd);
|
|
128 |
chprintf((BaseSequentialStream*) &global.sercanmux1, " CHG_INH : %u\n", flags.content.chg_inh);
|
|
129 |
chprintf((BaseSequentialStream*) &global.sercanmux1, " XCHG : %u\n", flags.content.xchg);
|
|
130 |
chprintf((BaseSequentialStream*) &global.sercanmux1, " FC : %u\n", flags.content.fc);
|
|
131 |
chprintf((BaseSequentialStream*) &global.sercanmux1, " CHG : %u\n", flags.content.chg);
|
|
132 |
chprintf((BaseSequentialStream*) &global.sercanmux1, " OCV_GD : %u\n", flags.content.ocv_gd);
|
|
133 |
chprintf((BaseSequentialStream*) &global.sercanmux1, " WAIT_ID : %u\n", flags.content.wait_id);
|
|
134 |
chprintf((BaseSequentialStream*) &global.sercanmux1, " BAT_DET : %u\n", flags.content.bat_det);
|
|
135 |
chprintf((BaseSequentialStream*) &global.sercanmux1, " SOC1 : %u\n", flags.content.soc1);
|
|
136 |
chprintf((BaseSequentialStream*) &global.sercanmux1, " SOCF : %u\n", flags.content.socf);
|
|
137 |
chprintf((BaseSequentialStream*) &global.sercanmux1, " DSG : %u\n", flags.content.dsg);
|
|
135 | 138 |
|
136 | 139 |
// read the current controller status |
137 | 140 |
ControlStatus ctrl_status; |
... | ... | |
139 | 142 |
{ |
140 | 143 |
return ST_FAIL_READ_STATUS; |
141 | 144 |
} |
142 |
chprintf((BaseSequentialStream*) &SD1, "control status: 0x%04X\n", ctrl_status.value);
|
|
143 |
chprintf((BaseSequentialStream*) &SD1, " FAS : %u\n", ctrl_status.content.fas);
|
|
144 |
chprintf((BaseSequentialStream*) &SD1, " SS : %u\n", ctrl_status.content.ss);
|
|
145 |
chprintf((BaseSequentialStream*) &SD1, " CSV : %u\n", ctrl_status.content.csv);
|
|
146 |
chprintf((BaseSequentialStream*) &SD1, " CSA : %u\n", ctrl_status.content.cca);
|
|
147 |
chprintf((BaseSequentialStream*) &SD1, " BCA : %u\n", ctrl_status.content.bca);
|
|
148 |
chprintf((BaseSequentialStream*) &SD1, " HIBERNATE : %u\n", ctrl_status.content.hibernate);
|
|
149 |
chprintf((BaseSequentialStream*) &SD1, " SNOOZE : %u\n", ctrl_status.content.snooze);
|
|
150 |
chprintf((BaseSequentialStream*) &SD1, " SLEEP : %u\n", ctrl_status.content.sleep);
|
|
151 |
chprintf((BaseSequentialStream*) &SD1, " LDMD : %u\n", ctrl_status.content.ldmd);
|
|
152 |
chprintf((BaseSequentialStream*) &SD1, " RUP_DIS : %u\n", ctrl_status.content.rup_dis);
|
|
153 |
chprintf((BaseSequentialStream*) &SD1, " VOK : %u\n", ctrl_status.content.vok);
|
|
154 |
chprintf((BaseSequentialStream*) &SD1, " QEN : %u\n", ctrl_status.content.qen);
|
|
145 |
chprintf((BaseSequentialStream*) &global.sercanmux1, "control status: 0x%04X\n", ctrl_status.value);
|
|
146 |
chprintf((BaseSequentialStream*) &global.sercanmux1, " FAS : %u\n", ctrl_status.content.fas);
|
|
147 |
chprintf((BaseSequentialStream*) &global.sercanmux1, " SS : %u\n", ctrl_status.content.ss);
|
|
148 |
chprintf((BaseSequentialStream*) &global.sercanmux1, " CSV : %u\n", ctrl_status.content.csv);
|
|
149 |
chprintf((BaseSequentialStream*) &global.sercanmux1, " CSA : %u\n", ctrl_status.content.cca);
|
|
150 |
chprintf((BaseSequentialStream*) &global.sercanmux1, " BCA : %u\n", ctrl_status.content.bca);
|
|
151 |
chprintf((BaseSequentialStream*) &global.sercanmux1, " HIBERNATE : %u\n", ctrl_status.content.hibernate);
|
|
152 |
chprintf((BaseSequentialStream*) &global.sercanmux1, " SNOOZE : %u\n", ctrl_status.content.snooze);
|
|
153 |
chprintf((BaseSequentialStream*) &global.sercanmux1, " SLEEP : %u\n", ctrl_status.content.sleep);
|
|
154 |
chprintf((BaseSequentialStream*) &global.sercanmux1, " LDMD : %u\n", ctrl_status.content.ldmd);
|
|
155 |
chprintf((BaseSequentialStream*) &global.sercanmux1, " RUP_DIS : %u\n", ctrl_status.content.rup_dis);
|
|
156 |
chprintf((BaseSequentialStream*) &global.sercanmux1, " VOK : %u\n", ctrl_status.content.vok);
|
|
157 |
chprintf((BaseSequentialStream*) &global.sercanmux1, " QEN : %u\n", ctrl_status.content.qen);
|
|
155 | 158 |
|
156 | 159 |
// if no battery was detected, abort |
157 | 160 |
if (!flags.content.bat_det) |
... | ... | |
160 | 163 |
} |
161 | 164 |
|
162 | 165 |
// read the BATGD_N pin |
163 |
chprintf((BaseSequentialStream*) &SD1, "battery good: %s\n", (this->isBatteryGood()? "yes" : "no"));
|
|
166 |
chprintf((BaseSequentialStream*) &global.sercanmux1, "battery good: %s\n", (this->isBatteryGood()? "yes" : "no"));
|
|
164 | 167 |
|
165 | 168 |
// read temperature |
166 | 169 |
if (this->stdCommand(STD_CMD_TEMP, val)) |
167 | 170 |
{ |
168 | 171 |
return ST_FAIL_READ_TEMP; |
169 | 172 |
} |
170 |
chprintf((BaseSequentialStream*) &SD1, "temperature: %fK (%fC)\n", float(val)/10.0f, float(val)/10.0f - 273.15f);
|
|
173 |
chprintf((BaseSequentialStream*) &global.sercanmux1, "temperature: %fK (%fC)\n", float(val)/10.0f, float(val)/10.0f - 273.15f);
|
|
171 | 174 |
|
172 | 175 |
// read the full available capacity |
173 | 176 |
if (this->stdCommand(STD_CMD_FAC, val)) |
174 | 177 |
{ |
175 | 178 |
return ST_FAIL_READ_FAC; |
176 | 179 |
} |
177 |
chprintf((BaseSequentialStream*) &SD1, "full available capacity: %umAh\n", val);
|
|
180 |
chprintf((BaseSequentialStream*) &global.sercanmux1, "full available capacity: %umAh\n", val);
|
|
178 | 181 |
|
179 | 182 |
// read the full charge capacity |
180 | 183 |
if (this->stdCommand(STD_CMD_FCC, val)) |
181 | 184 |
{ |
182 | 185 |
return ST_FAIL_READ_FCC; |
183 | 186 |
} |
184 |
chprintf((BaseSequentialStream*) &SD1, "full charge capacity: %umAh\n", val);
|
|
187 |
chprintf((BaseSequentialStream*) &global.sercanmux1, "full charge capacity: %umAh\n", val);
|
|
185 | 188 |
|
186 | 189 |
// read the remaining capacity |
187 | 190 |
if (this->stdCommand(STD_CMD_RM, val)) |
188 | 191 |
{ |
189 | 192 |
return ST_FAIL_READ_RM; |
190 | 193 |
} |
191 |
chprintf((BaseSequentialStream*) &SD1, "remaining capacity capacity: %umAh\n", val);
|
|
194 |
chprintf((BaseSequentialStream*) &global.sercanmux1, "remaining capacity capacity: %umAh\n", val);
|
|
192 | 195 |
|
193 | 196 |
// read the state of charge |
194 | 197 |
if (this->stdCommand(STD_CMD_SOC, val)) |
195 | 198 |
{ |
196 | 199 |
return ST_FAIL_READ_SOC; |
197 | 200 |
} |
198 |
chprintf((BaseSequentialStream*) &SD1, "state of charge: %3u%%\n", val);
|
|
201 |
chprintf((BaseSequentialStream*) &global.sercanmux1, "state of charge: %3u%%\n", val);
|
|
199 | 202 |
|
200 | 203 |
// read voltage |
201 | 204 |
if (this->stdCommand(STD_CMD_VOLT, val)) |
202 | 205 |
{ |
203 | 206 |
return ST_FAIL_READ_VOLT; |
204 | 207 |
} |
205 |
chprintf((BaseSequentialStream*) &SD1, "voltage: %umV\n", val);
|
|
208 |
chprintf((BaseSequentialStream*) &global.sercanmux1, "voltage: %umV\n", val);
|
|
206 | 209 |
|
207 | 210 |
// read average current |
208 | 211 |
if (this->stdCommand(STD_CMD_AI, val)) |
209 | 212 |
{ |
210 | 213 |
return ST_FAIL_READ_AI; |
211 | 214 |
} |
212 |
chprintf((BaseSequentialStream*) &SD1, "average current: %dmA\n", *reinterpret_cast<int8_t*>(&val));
|
|
215 |
chprintf((BaseSequentialStream*) &global.sercanmux1, "average current: %dmA\n", *reinterpret_cast<int8_t*>(&val));
|
|
213 | 216 |
|
214 | 217 |
// read average power |
215 | 218 |
if (this->stdCommand(STD_CMD_AP, val)) |
216 | 219 |
{ |
217 | 220 |
return ST_FAIL_READ_AP; |
218 | 221 |
} |
219 |
chprintf((BaseSequentialStream*) &SD1, "average power: %dmW\n", *reinterpret_cast<int8_t*>(&val));
|
|
222 |
chprintf((BaseSequentialStream*) &global.sercanmux1, "average power: %dmW\n", *reinterpret_cast<int8_t*>(&val));
|
|
220 | 223 |
|
221 | 224 |
// read the BATLOW pin |
222 |
chprintf((BaseSequentialStream*) &SD1, "battery low: %s\n", (this->isBatteryLow()? "yes" : "no"));
|
|
225 |
chprintf((BaseSequentialStream*) &global.sercanmux1, "battery low: %s\n", (this->isBatteryLow()? "yes" : "no"));
|
|
223 | 226 |
|
224 | 227 |
// read the time to empty |
225 | 228 |
if (this->stdCommand(STD_CMD_TTE, val)) |
226 | 229 |
{ |
227 | 230 |
return ST_FAIL_READ_TTE; |
228 | 231 |
} |
229 |
chprintf((BaseSequentialStream*) &SD1, "time to empty: ");
|
|
232 |
chprintf((BaseSequentialStream*) &global.sercanmux1, "time to empty: ");
|
|
230 | 233 |
if (uint16_t(~val)) { |
231 |
chprintf((BaseSequentialStream*) &SD1, "%u minutes", val);
|
|
234 |
chprintf((BaseSequentialStream*) &global.sercanmux1, "%u minutes", val);
|
|
232 | 235 |
} else { |
233 |
chprintf((BaseSequentialStream*) &SD1, "(not discharging)");
|
|
236 |
chprintf((BaseSequentialStream*) &global.sercanmux1, "(not discharging)");
|
|
234 | 237 |
} |
235 |
chprintf((BaseSequentialStream*) &SD1, "\n");
|
|
238 |
chprintf((BaseSequentialStream*) &global.sercanmux1, "\n");
|
|
236 | 239 |
|
237 | 240 |
// read the time to full |
238 | 241 |
if (this->stdCommand(STD_CMD_TTF, val)) |
239 | 242 |
{ |
240 | 243 |
return ST_FAIL_READ_TTF; |
241 | 244 |
} |
242 |
chprintf((BaseSequentialStream*) &SD1, "time to full: ");
|
|
245 |
chprintf((BaseSequentialStream*) &global.sercanmux1, "time to full: ");
|
|
243 | 246 |
if (uint16_t(~val)) { |
244 |
chprintf((BaseSequentialStream*) &SD1, "%u minutes", val);
|
|
247 |
chprintf((BaseSequentialStream*) &global.sercanmux1, "%u minutes", val);
|
|
245 | 248 |
} else { |
246 |
chprintf((BaseSequentialStream*) &SD1, "(not charging)");
|
|
249 |
chprintf((BaseSequentialStream*) &global.sercanmux1, "(not charging)");
|
|
247 | 250 |
} |
248 |
chprintf((BaseSequentialStream*) &SD1, "\n");
|
|
251 |
chprintf((BaseSequentialStream*) &global.sercanmux1, "\n");
|
|
249 | 252 |
|
250 | 253 |
return ST_OK; |
251 | 254 |
} |
... | ... | |
300 | 303 |
dst = uint16_t((buffer[1] << 8) | buffer[0]); |
301 | 304 |
#ifndef NDEBUG |
302 | 305 |
} else { |
303 |
chprintf((BaseSequentialStream*) &SD1, "%s(%d): ERROR: i2c transmit failed (%d)\n", __FILE__ , __LINE__ , res);
|
|
306 |
chprintf((BaseSequentialStream*) &global.sercanmux1, "%s(%d): ERROR: i2c transmit failed (%d)\n", __FILE__ , __LINE__ , res);
|
|
304 | 307 |
#endif |
305 | 308 |
} |
306 | 309 |
|
... | ... | |
323 | 326 |
|
324 | 327 |
buffer[buffer[0] + 1] = '\0'; |
325 | 328 |
|
326 |
chprintf((BaseSequentialStream*) &SD1, "name: %u - %s\n", buffer[0], (char*)&buffer[1]);
|
|
329 |
chprintf((BaseSequentialStream*) &global.sercanmux1, "name: %u - %s\n", buffer[0], (char*)&buffer[1]);
|
|
327 | 330 |
|
328 | 331 |
return res; |
329 | 332 |
} |
... | ... | |
353 | 356 |
|
354 | 357 |
#ifndef NDEBUG |
355 | 358 |
if (res) { |
356 |
chprintf((BaseSequentialStream*) &SD1, "%s(%d): ERROR: i2c transmit failed (%d)\n", __FILE__ , __LINE__ , res);
|
|
359 |
chprintf((BaseSequentialStream*) &global.sercanmux1, "%s(%d): ERROR: i2c transmit failed (%d)\n", __FILE__ , __LINE__ , res);
|
|
357 | 360 |
} |
358 | 361 |
#endif |
359 | 362 |
|
... | ... | |
369 | 372 |
{ |
370 | 373 |
if (!buf) { |
371 | 374 |
#ifndef NDEBUG |
372 |
chprintf((BaseSequentialStream*) &SD1, "%s(%d): ERROR: received NULL-pointer as buffer\n", __FILE__ , __LINE__);
|
|
375 |
chprintf((BaseSequentialStream*) &global.sercanmux1, "%s(%d): ERROR: received NULL-pointer as buffer\n", __FILE__ , __LINE__);
|
|
373 | 376 |
#endif |
374 | 377 |
return ~RDY_OK; |
375 | 378 |
} |
376 | 379 |
if (rw != EXT_CMD_WRITE && rw != EXT_CMD_READ) { |
377 | 380 |
#ifndef NDEBUG |
378 |
chprintf((BaseSequentialStream*) &SD1, "%s(%d): ERROR: invalid access mode selected\n", __FILE__ , __LINE__);
|
|
381 |
chprintf((BaseSequentialStream*) &global.sercanmux1, "%s(%d): ERROR: invalid access mode selected\n", __FILE__ , __LINE__);
|
|
379 | 382 |
#endif |
380 | 383 |
return ~RDY_OK; |
381 | 384 |
} |
382 | 385 |
if (length > 33) { |
383 | 386 |
#ifndef NDEBUG |
384 |
chprintf((BaseSequentialStream*) &SD1, "%s(%d): ERROR: length exceeds maximum of 33 bytes\n", __FILE__ , __LINE__);
|
|
387 |
chprintf((BaseSequentialStream*) &global.sercanmux1, "%s(%d): ERROR: length exceeds maximum of 33 bytes\n", __FILE__ , __LINE__);
|
|
385 | 388 |
#endif |
386 | 389 |
return ~RDY_OK; |
387 | 390 |
} |
... | ... | |
402 | 405 |
|
403 | 406 |
#ifndef NDEBUG |
404 | 407 |
if (res) { |
405 |
chprintf((BaseSequentialStream*) &SD1, "%s(%d): ERROR: i2c transmit failed (%d)\n", __FILE__ , __LINE__ , res);
|
|
408 |
chprintf((BaseSequentialStream*) &global.sercanmux1, "%s(%d): ERROR: i2c transmit failed (%d)\n", __FILE__ , __LINE__ , res);
|
|
406 | 409 |
} |
407 | 410 |
#endif |
408 | 411 |
|
Also available in: Unified diff