Skip to content

Commit 206956d

Browse files
committed
[WIP] Enable battery charging on MKRWAN1310
Since MKRWAN1310 battery sense ADC pin is not connected we must enable the PMIC, check if it starts charging, eventually disable the charging circuitry. ATM the bootloader is a bit pick and could hang the board on first upload.
1 parent c8094e7 commit 206956d

File tree

1 file changed

+51
-13
lines changed

1 file changed

+51
-13
lines changed

variants/mkrwan1300/variant.cpp

+51-13
Original file line numberDiff line numberDiff line change
@@ -172,44 +172,82 @@ const void* g_apTCInstances[TCC_INST_NUM + TC_INST_NUM]={ TCC0, TCC1, TCC2, TC3,
172172

173173
#if defined(USE_BQ24195L_PMIC)
174174
#include "wiring_private.h"
175+
#include "delay.h"
175176

176177
#define PMIC_ADDRESS 0x6B
177178
#define PMIC_REG01 0x01
178179
#define PMIC_REG07 0x07
180+
#define PMIC_REG08 0x08
179181

180182
static inline void enable_battery_charging() {
181-
PERIPH_WIRE.initMasterWIRE(100000);
182-
PERIPH_WIRE.enableWIRE();
183-
pinPeripheral(PIN_WIRE_SDA, g_APinDescription[PIN_WIRE_SDA].ulPinType);
184-
pinPeripheral(PIN_WIRE_SCL, g_APinDescription[PIN_WIRE_SCL].ulPinType);
185183

186-
PERIPH_WIRE.startTransmissionWIRE( PMIC_ADDRESS, WIRE_WRITE_FLAG );
184+
bool ret = PERIPH_WIRE.startTransmissionWIRE( PMIC_ADDRESS, WIRE_WRITE_FLAG );
185+
if (!ret) {
186+
return;
187+
}
187188
PERIPH_WIRE.sendDataMasterWIRE(PMIC_REG01);
188189
PERIPH_WIRE.sendDataMasterWIRE(0x1B); // Charge Battery + Minimum System Voltage 3.5V
189190
PERIPH_WIRE.prepareCommandBitsWire(WIRE_MASTER_ACT_STOP);
191+
}
190192

191-
PERIPH_WIRE.disableWIRE();
193+
static inline void disable_battery_charging() {
194+
195+
bool ret = PERIPH_WIRE.startTransmissionWIRE( PMIC_ADDRESS, WIRE_WRITE_FLAG );
196+
if (!ret) {
197+
return;
198+
}
199+
PERIPH_WIRE.sendDataMasterWIRE(PMIC_REG01);
200+
PERIPH_WIRE.sendDataMasterWIRE(0x0B); // Charge Battery + Minimum System Voltage 3.5V
201+
PERIPH_WIRE.prepareCommandBitsWire(WIRE_MASTER_ACT_STOP);
192202
}
193203

194204
static inline void disable_battery_fet(bool disabled) {
195-
PERIPH_WIRE.initMasterWIRE(100000);
196-
PERIPH_WIRE.enableWIRE();
197-
pinPeripheral(PIN_WIRE_SDA, g_APinDescription[PIN_WIRE_SDA].ulPinType);
198-
pinPeripheral(PIN_WIRE_SCL, g_APinDescription[PIN_WIRE_SCL].ulPinType);
199205

200-
PERIPH_WIRE.startTransmissionWIRE( PMIC_ADDRESS, WIRE_WRITE_FLAG );
206+
bool ret = PERIPH_WIRE.startTransmissionWIRE( PMIC_ADDRESS, WIRE_WRITE_FLAG );
207+
if (!ret) {
208+
return;
209+
}
201210
PERIPH_WIRE.sendDataMasterWIRE(PMIC_REG07);
202211
// No D+/D– detection + Safety timer not slowed by 2X during input DPM or thermal regulation +
203212
// BAT fet disabled/enabled + charge and bat fault INT
204213
PERIPH_WIRE.sendDataMasterWIRE(0x0B | (disabled ? 0x20 : 0x00));
205214
PERIPH_WIRE.prepareCommandBitsWire(WIRE_MASTER_ACT_STOP);
215+
}
206216

207-
PERIPH_WIRE.disableWIRE();
217+
static inline bool is_battery_present() {
218+
219+
bool ret = PERIPH_WIRE.startTransmissionWIRE( PMIC_ADDRESS, WIRE_WRITE_FLAG );
220+
if (!ret) {
221+
return false;
222+
}
223+
PERIPH_WIRE.sendDataMasterWIRE(PMIC_REG08);
224+
PERIPH_WIRE.prepareCommandBitsWire(WIRE_MASTER_ACT_STOP);
225+
226+
ret = PERIPH_WIRE.startTransmissionWIRE( PMIC_ADDRESS, WIRE_READ_FLAG );
227+
if (!ret) {
228+
return false;
229+
}
230+
uint8_t res = PERIPH_WIRE.readDataWIRE();
231+
PERIPH_WIRE.prepareNackBitWIRE();
232+
PERIPH_WIRE.prepareCommandBitsWire(WIRE_MASTER_ACT_STOP);
233+
return ((res & 0b1000) != 0);
208234
}
209235

210236
void initVariant() {
237+
PERIPH_WIRE.initMasterWIRE(100000);
238+
PERIPH_WIRE.enableWIRE();
239+
pinPeripheral(PIN_WIRE_SDA, g_APinDescription[PIN_WIRE_SDA].ulPinType);
240+
pinPeripheral(PIN_WIRE_SCL, g_APinDescription[PIN_WIRE_SCL].ulPinType);
241+
211242
enable_battery_charging();
212-
disable_battery_fet(false);
243+
//disable_battery_fet(false);
244+
delay(100);
245+
bool batteryPresent = is_battery_present();
246+
if (!batteryPresent) {
247+
disable_battery_charging();
248+
}
249+
250+
PERIPH_WIRE.disableWIRE();
213251
}
214252
#endif
215253

0 commit comments

Comments
 (0)