@@ -172,44 +172,82 @@ const void* g_apTCInstances[TCC_INST_NUM + TC_INST_NUM]={ TCC0, TCC1, TCC2, TC3,
172
172
173
173
#if defined(USE_BQ24195L_PMIC)
174
174
#include " wiring_private.h"
175
+ #include " delay.h"
175
176
176
177
#define PMIC_ADDRESS 0x6B
177
178
#define PMIC_REG01 0x01
178
179
#define PMIC_REG07 0x07
180
+ #define PMIC_REG08 0x08
179
181
180
182
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 );
185
183
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
+ }
187
188
PERIPH_WIRE.sendDataMasterWIRE (PMIC_REG01);
188
189
PERIPH_WIRE.sendDataMasterWIRE (0x1B ); // Charge Battery + Minimum System Voltage 3.5V
189
190
PERIPH_WIRE.prepareCommandBitsWire (WIRE_MASTER_ACT_STOP);
191
+ }
190
192
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);
192
202
}
193
203
194
204
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 );
199
205
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
+ }
201
210
PERIPH_WIRE.sendDataMasterWIRE (PMIC_REG07);
202
211
// No D+/D– detection + Safety timer not slowed by 2X during input DPM or thermal regulation +
203
212
// BAT fet disabled/enabled + charge and bat fault INT
204
213
PERIPH_WIRE.sendDataMasterWIRE (0x0B | (disabled ? 0x20 : 0x00 ));
205
214
PERIPH_WIRE.prepareCommandBitsWire (WIRE_MASTER_ACT_STOP);
215
+ }
206
216
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 );
208
234
}
209
235
210
236
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
+
211
242
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 ();
213
251
}
214
252
#endif
215
253
0 commit comments