Skip to content

Commit

Permalink
feature: Adds driver for MP2660
Browse files Browse the repository at this point in the history
  • Loading branch information
mmarkwort committed Oct 10, 2023
1 parent 6915d55 commit bd6d6ca
Show file tree
Hide file tree
Showing 16 changed files with 978 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ or [GitLab examples](https://gitlab.com/UncleRus/esp-idf-lib/tree/master/example
|--------------------------|----------------------------------------------------------------------------------|---------|--------------------|---------------|
| **lc709203f** | Driver for LC709203F battery fuel gauge | ISC | esp32, esp8266, esp32s2, esp32c3 | yes |
| **max1704x** | Driver for MAX17043/MAX17044/MAX17048/MAX17049 battery fuel gauge | BSD-3-Clause | esp32, esp8266, esp32s2, esp32c3 | yes |
| **mp2660** | Driver for MP2660 5V USB, 500mA, I2C-Controlled Linear Charger with Power Path Management for Single-Cell Li-Ion Battery | BSD-3-Clause | esp32, esp32s2, esp32c3 | yes |


### Common libraries
Expand Down Expand Up @@ -325,6 +326,7 @@ or [GitLab examples](https://gitlab.com/UncleRus/esp-idf-lib/tree/master/example
- [jsuiker](https://github.com/jsuiker): `dht`
- [Julian Doerner](https://github.com/juliandoerner): `tsl2591`
- [Lucio Tarantino](https://github.com/dianlight): `ads111x`
- [Manuel Markwort](https://github.com/mmarkwort): `mp2660`
- [Marc Luehr](https://github.com/th3link): `veml7700`
- [Nate Usher](https://github.com/nated0g): `scd30`
- Pavel Merzlyakov: `ds1302`
Expand Down
19 changes: 19 additions & 0 deletions components/mp2660/.eil.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: mp2660
description: Driver for MP2660 5V USB, 500mA, I2C-Controlled Linear Charger with Power Path Management for Single-Cell Li-Ion Battery
version: 1.0.0
groups:
- battery
code_owners:
- mmarkwort
depends:
- i2cdev
- esp_idf_lib_helpers
thread_safe: yes
targets:
- esp32
- esp32s2
- esp32c3
license: BSD-3
copyrights:
- name: mmarkwort
year: 2023
5 changes: 5 additions & 0 deletions components/mp2660/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
idf_component_register(
SRCS mp2660.c
INCLUDE_DIRS .
REQUIRES i2cdev esp_idf_lib_helpers
)
26 changes: 26 additions & 0 deletions components/mp2660/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Copyright (c) 2023 Manuel Markwort (https://github.com/mmarkwort)

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of itscontributors
may be used to endorse or promote products derived from this software without
specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2 changes: 2 additions & 0 deletions components/mp2660/component.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
COMPONENT_ADD_INCLUDEDIRS = .
COMPONENT_DEPENDS = i2cdev esp_idf_lib_helpers
178 changes: 178 additions & 0 deletions components/mp2660/mp2660.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/*
* Copyright (c) 2017 Gunar Schorcht <https://github.com/gschorcht>
* Copyright (c) 2019 Ruslan V. Uss <unclerus@gmail.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of itscontributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/**
* @file mp2660.c
*
* ESP-IDF driver for Monolithic Systems 5V USB, 500mA, I2C-Controlled Linear Charger with Power Path Management for Single-Cell Li-Ion Battery
*
* Copyright (c) 2023 Manuel Markwort <https://github.com/mmarkwort>\n
*
* BSD Licensed as described in the file LICENSE
*/

#include <esp_idf_lib_helpers.h>
#include "mp2660.h"

#define I2C_FREQ_HZ 400000 //!< 400kHz bus speed

#define MP2660_SOURCE_CONTROL_REG_ADDR 0x00 //!< Address of source control register
#define MP2660_POWER_ON_CONFIG_REG_ADDR 0x01 //!< Address of power on configuration register
#define MP2660_CURENT_CONTROL_REG_ADDR 0x02 //!< Address of current control register
#define MP2660_PRECHARGE_TERMINATION_CURRENT_REG_ADDR 0x03 //!< Address of precharge termination current register
#define MP2660_CHARGE_VOLTAGE_CONTROL_REG_ADDR 0x04 //!< Address of charge voltage control register
#define MP2660_CHARGE_TERMINATION_TIMER_CONTROL_REG_ADDR 0x05 //!< Address of charge termination timer control register
#define MP2660_MISC_OPERATION_CONTROL_REG_ADDR 0x06 //!< Address of misc operation control register
#define MP2660_SYSTEM_STATUS_REG_ADDR 0x07 //!< Address of system status register
#define MP2660_FAULT_REG_ADDR 0x08 //!< Address of fault register

#define CHECK_ARG(VAL) do { if (!(VAL)) return ESP_ERR_INVALID_ARG; } while (0)

esp_err_t mp2660_init_desc(i2c_dev_t *dev, i2c_port_t port, gpio_num_t sda_gpio, gpio_num_t scl_gpio)
{
CHECK_ARG(dev);

dev->port = port;
dev->addr = MP2660_I2C_ADDR;
dev->cfg.sda_io_num = sda_gpio;
dev->cfg.scl_io_num = scl_gpio;
#if HELPER_TARGET_IS_ESP32
dev->cfg.master.clk_speed = I2C_FREQ_HZ;
#endif

return i2c_dev_create_mutex(dev);
}

esp_err_t mp2660_free_desc(i2c_dev_t *dev)
{
CHECK_ARG(dev);

return i2c_dev_delete_mutex(dev);
}

esp_err_t mp2660_read(i2c_dev_t *dev, uint8_t addr, void* data)
{
CHECK_ARG(dev);

I2C_DEV_TAKE_MUTEX(dev);
I2C_DEV_CHECK(dev, i2c_dev_read_reg(dev, addr, data, 1));
I2C_DEV_GIVE_MUTEX(dev);

return ESP_OK;
}

esp_err_t mp2660_write(i2c_dev_t *dev, uint8_t addr, void* data)
{
CHECK_ARG(dev);

I2C_DEV_TAKE_MUTEX(dev);
I2C_DEV_CHECK(dev, i2c_dev_write_reg(dev, addr, data, 1));
I2C_DEV_GIVE_MUTEX(dev);

return ESP_OK;
}

esp_err_t mp2660_get_input_source(i2c_dev_t *dev, mp2660_input_source_t* input_source)
{
return mp2660_read(dev, MP2660_SOURCE_CONTROL_REG_ADDR, input_source);
}

esp_err_t mp2660_set_input_source(i2c_dev_t *dev, mp2660_input_source_t* input_source)
{
return mp2660_write(dev, MP2660_SOURCE_CONTROL_REG_ADDR, input_source);
}

esp_err_t mp2660_get_pwr_on_conf(i2c_dev_t *dev, mp2660_power_on_conf_t* pwr_config)
{
return mp2660_read(dev, MP2660_POWER_ON_CONFIG_REG_ADDR, pwr_config);
}

esp_err_t mp2660_set_pwr_on_conf(i2c_dev_t *dev, mp2660_power_on_conf_t* pwr_config)
{
return mp2660_write(dev, MP2660_POWER_ON_CONFIG_REG_ADDR, pwr_config);
}

esp_err_t mp2660_get_chrg_current_ctrl(i2c_dev_t *dev, mp2660_charge_current_ctrl_t* chrg_current_ctrl)
{
return mp2660_read(dev, MP2660_CURENT_CONTROL_REG_ADDR, chrg_current_ctrl);
}

esp_err_t mp2660_set_chrg_current_ctrl(i2c_dev_t *dev, mp2660_charge_current_ctrl_t* chrg_current_ctrl)
{
return mp2660_write(dev, MP2660_CURENT_CONTROL_REG_ADDR, chrg_current_ctrl);
}

esp_err_t mp2660_get_pre_chrg_term_current(i2c_dev_t *dev, mp2660_pre_charge_term_current_t* pre_chrg_term_current)
{
return mp2660_read(dev, MP2660_PRECHARGE_TERMINATION_CURRENT_REG_ADDR, pre_chrg_term_current);
}

esp_err_t mp2660_set_pre_chrg_term_current(i2c_dev_t *dev, mp2660_pre_charge_term_current_t* pre_chrg_term_current)
{
return mp2660_write(dev, MP2660_PRECHARGE_TERMINATION_CURRENT_REG_ADDR, pre_chrg_term_current);
}

esp_err_t mp2660_get_chrg_voltage_control(i2c_dev_t *dev, mp2660_charge_voltage_ctrl_t* chrg_voltage_ctrl)
{
return mp2660_read(dev, MP2660_CHARGE_VOLTAGE_CONTROL_REG_ADDR, chrg_voltage_ctrl);
}

esp_err_t mp2660_set_chrg_voltage_control(i2c_dev_t *dev, mp2660_charge_voltage_ctrl_t* chrg_voltage_ctrl)
{
return mp2660_write(dev, MP2660_CHARGE_VOLTAGE_CONTROL_REG_ADDR, chrg_voltage_ctrl);
}

esp_err_t mp2660_get_chrg_term_timer_control(i2c_dev_t *dev, mp2660_charge_term_timer_ctrl_t* charge_term_timer_ctrl)
{
return mp2660_read(dev, MP2660_CHARGE_TERMINATION_TIMER_CONTROL_REG_ADDR, charge_term_timer_ctrl);
}

esp_err_t mp2660_set_chrg_term_timer_control(i2c_dev_t *dev, mp2660_charge_term_timer_ctrl_t* charge_term_timer_ctrl)
{
return mp2660_write(dev, MP2660_CHARGE_TERMINATION_TIMER_CONTROL_REG_ADDR, charge_term_timer_ctrl);
}

esp_err_t mp2660_get_misc_op_control(i2c_dev_t *dev, mp2660_misc_op_ctrl_t* misc_op_ctrl)
{
return mp2660_read(dev, MP2660_MISC_OPERATION_CONTROL_REG_ADDR, misc_op_ctrl);
}

esp_err_t mp2660_set_misc_op_control(i2c_dev_t *dev, mp2660_misc_op_ctrl_t* misc_op_ctrl)
{
return mp2660_write(dev, MP2660_MISC_OPERATION_CONTROL_REG_ADDR, misc_op_ctrl);
}

esp_err_t mp2660_get_sys_status(i2c_dev_t *dev, mp2660_sys_status_t* sys_status)
{
return mp2660_read(dev, MP2660_SYSTEM_STATUS_REG_ADDR, sys_status);
}

esp_err_t mp2660_get_fault(i2c_dev_t *dev, mp2660_fault_t* fault)
{
return mp2660_read(dev, MP2660_FAULT_REG_ADDR, fault);
}
Loading

0 comments on commit bd6d6ca

Please sign in to comment.