I see, great tip!
I found the BQ25120.h file:
#ifndef BQ25120A_h
#define BQ25120A_h
#define BQ25120A_ADDRESS 0x6A
// Regsiter Map
// https://www.ti.com/lit/ds/symlink/bq25120a.pdf?ts=1610608851953&ref_url=https%253A%252F%252Fwww.startpage.com%252F
#define BQ25120A_STATUS 0x00
#define BQ25120A_FAULTS 0x01
#define BQ25120A_TS_CONTROL 0x02
#define BQ25120A_FAST_CHG 0x03
#define BQ25120A_TERMINATION_CURR 0x04
#define BQ25120A_BATTERY_CTRL 0x05
#define BQ25120A_SYS_VOUT_CTRL 0x06
#define BQ25120A_LDO_CTRL 0x07
#define BQ25120A_PUSH_BUTT_CTRL 0x08
#define BQ25120A_ILIM_UVLO_CTRL 0x09
#define BQ25120A_BATT_MON 0x0A
#define BQ25120A_VIN_DPM 0x0B
#include "Wire.h"
class BQ25120A
{
public:
BQ25120A() {};
uint8_t getStatus();
void writeByte(uint8_t address, uint8_t subAddress, uint8_t data);
uint8_t readByte(uint8_t address, uint8_t subAddress);
};
#endif
But I still have some trouble reading the register. I simply tried using this:
int value = BQ25120A().getStatus()
Maybe my programming skills are rusty, or I need to use some functions from wire.h to properly communicate over I2C?
I get the following error through the serial port:
++ MbedOS Error Info ++
Error Status: 0x80010133 Code: 307 Module: 1
Error Message: Mutex: 0x0, Parameter error
Location: 0x1AA47
Error Value: 0x0
Current Thread: main Id: 0x20003D54 Entry: 0x1A973 StackSize: 0xC00 StackMem: 0x20003130 SP: 0x2000FF44
For more info, visit: https://mbed.com/s/error?error=0x80010133&tgt=NICLA
-- MbedOS Error Info --
I didn't even try to access the bits, and I wouldn't even know what subAddress means?
Edit: seems like I just forgot nicla::begin() in setup(). The error doesn't happen now, but I get a reading of 129 without a connected battery, and 65 with. No idea what that means, but it"s a start!
Edit2: I guess it is a simple way to read all addresses at the same time, and 65 is probably 01000001, however, if I am reading this correctly in the datasheet, this will only tell me the battery percentage above 60%, correct?
Edit3: yeah, should read the datasheet more, I kinda get it now.
In the end I used:
uint8_t value = BQ25120A().readByte(BQ25120A_ADDRESS, BQ25120A_BATT_MON );
While the battery is full, I get 108, or 1101100, which according to the datasheet is a 94% of VBATREG:
Looking at this table in the datasheet, that would mean a full battery and probably my VBATREG is 4.35V ( I measure only 4V, but otherwise I would have gotten more than 94% for a full battery, right?)
Looking at this, I would be at almost an empty battery (which is the only thing I need) when I have a VBMON of <=82%, so 1001000 , or decimal 72.
Please tell me if this is correct, and I hope this helps other people trying to do this.
Last edit: even though it works, it seems like reading out the register stops the battery from working, kinda... if I try to use the Nicla only with the battery, it doesn't do anything if I read the register. Even my simple green LED doesn't work. However, if I start the Nicla with the USB cable connected, and then disconnect it, the program on it doesn't work, but the green LED stays lit, so I guess the battery works, only the program just stops. If someone has a tip, maybe I need to do something else after I read the status of the register?