SGM4056 Charger

The SGM4056 charger is able to report its status via two output pins. If these pins are connected to GPIO inputs, then the sgm4056 driver can derive the charger status.

Initialization

Initialization of the sgm4056 driver is normally done in the BSP. This is done by creating a OS device using os_dev_create.

#include "sgm4056/sgm4056.h"

...

static struct sgm4056_dev os_bsp_charger;
static struct sgm4056_dev_config os_bsp_charger_config = {
    .power_presence_pin = CHARGER_POWER_PRESENCE_PIN,
    .charge_indicator_pin = CHARGER_CHARGE_PIN,
};

void
hal_bsp_init(void)
{
    int rc;

    ...

    /* Create charge controller */
    rc = os_dev_create(&os_bsp_charger.dev, "charger",
                    OS_DEV_INIT_KERNEL, OS_DEV_INIT_PRIO_DEFAULT,
                    sgm4056_dev_init, &os_bsp_charger_config);
    assert(rc == 0);
}

First we create a instance of struct sgm4056_dev, this will be initialised later. Then we create a instance of struct sgm4056_dev_config, which contains the pin numbers of the gpio connected to the charger. Then we call os_dev_create with a pointer to the device, a device name, some defaults, the sgm4056_dev_init initializor and a pointer to config.

Usage

There a two ways to use the driver: directly or via charge-control.

When using the driver directly, you need to open the OS device and then use the driver functions.

#include "sgm4056/sgm4056.h"

...

int
main(int argc, char **argv)
{
    struct sgm4056_dev *charger;

    charger = (struct sgm4056_dev *) os_dev_open("charger", 0, 0);
    assert(charger);

    while (1) {
        rc = sgm4056_get_charger_status(charger, &charger_status);
        assert(rc == 0);

        console_printf("Charger state = %i\n", charger_status);
        console_flush();
    }
}

When using charge-control interface, you need to enable it in syscfg and then follow the general instructions at Charge control drivers.

syscfg.vals:
    # Enable charge control integration
    SGM4056_USE_CHARGE_CONTROL: 1