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 8f0138f
Show file tree
Hide file tree
Showing 16 changed files with 959 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ or [GitLab examples](https://gitlab.com/UncleRus/esp-idf-lib/tree/master/example
| **ds3502** | Driver for nonvolatile digital potentiometer DS3502 | BSD-3-Clause | esp32, esp8266, esp32s2, esp32c3 | yes |
| **example** | An example component | ISC | esp32, esp8266, esp32s2, esp32c3 | n/a |
| **hd44780** | Driver for HD44780 compatible LCD text displays | BSD-3-Clause | esp32, esp8266, esp32s2, esp32c3 | no |
| **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 |
| **pca9685** | Driver for 16-channel, 12-bit PWM PCA9685 | BSD-3-Clause | esp32, esp8266, esp32s2, esp32c3 | yes |
| **rda5807m** | Driver for single-chip broadcast FM radio tuner RDA5807M | BSD-3-Clause | esp32, esp8266, esp32s2, esp32c3 | yes |
| **tca9548** | Driver for TCA9548A/PCA9548A low-voltage 8-channel I2C switch | BSD-3-Clause | esp32, esp8266, esp32s2, esp32c3 | yes |
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:
- misc
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

#define MP2660_SOURCE_CONTROL_REG_ADDR 0x00
#define MP2660_POWER_ON_CONFIG_REG_ADDR 0x01
#define MP2660_CURENT_CONTROL_REG_ADDR 0x02
#define MP2660_PRECHARGE_TERMINATION_CURRENT_REG_ADDR 0x03
#define MP2660_CHARGE_VOLTAGE_CONTROL_REG_ADDR 0x04
#define MP2660_CHARGE_TERMINATION_TIMER_CONTROL_REG_ADDR 0x05
#define MP2660_MISC_OPERATION_CONTROL_REG_ADDR 0x06
#define MP2660_SYSTEM_STATUS_REG_ADDR 0x07
#define MP2660_FAULT_REG_ADDR 0x08

#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 8f0138f

Please sign in to comment.