/*
* Copyright (c) 2006-2018, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020-02-17 zh. first version
*/
#include <board.h>
#include <drv_qspi.h>
#include <rtdevice.h>
#include <rthw.h>
#include <finsh.h>
#ifdef BSP_USING_I2C_AXP152
#define AXP152_CHIP_VERSION 0x03
#define AXP152_DCDC1_VOLTAGE 0x26
#define AXP152_DCDC2_VOLTAGE 0x23
#define AXP152_DCDC3_VOLTAGE 0x27
#define AXP152_DCDC4_VOLTAGE 0x2B
#define AXP152_DLDO1_VOLTAGE 0x29
#define AXP152_DLDO2_VOLTAGE 0x2A
#define AXP152_ALDO1_ALD02_VOLTAGE 0x28
#define AXP152_SHUTDOWN 0x32
#define AXP152_POWEROFF (1 << 7)
/* For axp_gpio.c */
#define AXP152_ADDR (0x60 >> 1)
#define AXP_GPIO0_CTRL 0x90
#define AXP_GPIO1_CTRL 0x91
#define AXP_GPIO2_CTRL 0x92
#define AXP_GPIO3_CTRL 0x93
#define AXP_GPIO_CTRL_OUTPUT_LOW 0x00 /* Drive pin low */
#define AXP_GPIO_CTRL_OUTPUT_HIGH 0x01 /* Drive pin high */
#define AXP_GPIO_CTRL_INPUT 0x02 /* Input */
#define AXP_GPIO_STATE 0x97
#define AXP_GPIO_STATE_OFFSET 0x00
/* For axp152 i2c bus*/
#define AXP152_I2CBUS_NAME "i2c1"
static struct rt_i2c_bus_device *axp152_i2c_bus = RT_NULL;
static rt_err_t i2c_write_nbyte(rt_uint8_t slave_addr, rt_uint8_t cmd, rt_uint8_t *value, rt_uint32_t len)
{
rt_uint32_t i;
rt_uint8_t *buffer;
rt_size_t result;
buffer = rt_malloc(len + 1);
buffer[0] = cmd;
for(i = 0;i < len;i ++)
{
buffer[i+1] = value[i];
}
result = rt_i2c_master_send(axp152_i2c_bus, slave_addr, RT_I2C_WR, buffer, len+1);
rt_free(buffer);
if (result == (len+1))
{
return RT_EOK;
}
else
{
return -RT_ERROR;
}
}
static rt_err_t i2c_read_nbyte(rt_uint8_t slave_addr, rt_uint8_t cmd, rt_uint8_t *buf, rt_uint32_t len)
{
rt_i2c_master_send(axp152_i2c_bus, slave_addr, RT_I2C_WR, &cmd, 1);
rt_i2c_master_recv(axp152_i2c_bus, slave_addr, RT_I2C_RD, buf, len);
return RT_EOK;
}
static rt_uint8_t axp152_mvolt_to_target(rt_uint32_t mvolt, rt_uint32_t min, rt_uint32_t max, rt_uint32_t div)
{
if(mvolt < min) mvolt = min;
else if (mvolt > max) mvolt = max;
return (mvolt - min) / div;
}
rt_err_t axp152_set_dcdc1(rt_uint32_t mvolt)
{
rt_uint8_t target = axp152_mvolt_to_target(mvolt, 1700, 3500, 100);
if(mvolt >= 2400 || mvolt <= 2800)target = target - 2;
if(mvolt >= 3000 || mvolt <= 3500)target = target - 3;
return i2c_write_nbyte(AXP152_ADDR,AXP152_DCDC1_VOLTAGE,&target,1);
}
rt_err_t axp152_set_dcdc2(rt_uint32_t mvolt)
{
rt_uint8_t target = axp152_mvolt_to_target(mvolt, 700, 2275, 25);
return i2c_write_nbyte(AXP152_ADDR,AXP152_DCDC2_VOLTAGE,&target,1);
}
rt_err_t axp152_set_dcdc3(rt_uint32_t mvolt)
{
rt_uint8_t target = axp152_mvolt_to_target(mvolt, 700, 3500, 50);
return i2c_write_nbyte(AXP152_ADDR,AXP152_DCDC3_VOLTAGE,&target,1);
}
rt_err_t axp152_set_dcdc4(rt_uint32_t mvolt)
{
rt_uint8_t target = axp152_mvolt_to_target(mvolt, 700, 3500, 25);
return i2c_write_nbyte(AXP152_ADDR,AXP152_DCDC4_VOLTAGE,&target,1);
}
rt_err_t axp152_set_dldo1(rt_uint32_t mvolt)
{
rt_uint8_t target = axp152_mvolt_to_target(mvolt, 700, 3500, 100);
return i2c_write_nbyte(AXP152_ADDR,AXP152_DLDO1_VOLTAGE,&target,1);
}
rt_err_t axp152_set_dldo2(rt_uint32_t mvolt)
{
rt_uint8_t target = axp152_mvolt_to_target(mvolt, 700, 3500, 100);
return i2c_write_nbyte(AXP152_ADDR,AXP152_DLDO2_VOLTAGE,&target,1);
}
rt_err_t axp152_set_aldo1(rt_uint32_t mvolt)
{
rt_uint8_t data;
rt_uint8_t target;
if(mvolt >= 1200 && mvolt <= 2000)
{
target = axp152_mvolt_to_target(mvolt, 1200, 2000, 100);
}
else if(mvolt == 2500)
{
target = 0x09;
}
else if(mvolt == 2700)
{
target = 0x0A;
}
else if(mvolt == 2800)
{
target = 0x0B;
}
else if(mvolt >= 3000 || mvolt <= 3300)
{
target = axp152_mvolt_to_target(mvolt, 3000, 3300, 100);
target += 0x0C;
}
i2c_read_nbyte(AXP152_ADDR,AXP152_ALDO1_ALD02_VOLTAGE,&data,1);
data &= 0x0F;
target = target << 4;
target &= 0xF0;
target = data | target;
return i2c_write_nbyte(AXP152_ADDR,AXP152_ALDO1_ALD02_VOLTAGE,&target,1);
}
rt_err_t axp152_set_aldo2(rt_uint32_t mvolt)
{
rt_uint8_t data;
rt_uint8_t target;
if(mvolt >= 1200 && mvolt <= 2000)
{
target = axp152_mvolt_to_target(mvolt, 1200, 2000, 100);
}
else if(mvolt == 2500)
{
target = 0x09;
}
else if(mvolt == 2700)
{
target = 0x0A;
}
else if(mvolt == 2800)
{
target = 0x0B;
}
else if(mvolt >= 3000 || mvolt <= 3300)
{
target = axp152_mvolt_to_target(mvolt, 3000, 3300, 100);
target += 0x0C;
}
i2c_read_nbyte(AXP152_ADDR,AXP152_ALDO1_ALD02_VOLTAGE,&data,1);
data &= 0xF0;
target = target & 0x0F;
target = data | target;
return i2c_write_nbyte(AXP152_ADDR,AXP152_ALDO1_ALD02_VOLTAGE,&target,1);
}
int axp152_init(void)
{
rt_uint8_t version;
axp152_i2c_bus = rt_i2c_bus_device_find(AXP152_I2CBUS_NAME);
if(axp152_i2c_bus == RT_NULL)
{
rt_kprintf("i2c_bus %s for axp152 not found!\n", axp152_i2c_bus);
return -RT_ERROR;
}
/* detect axp152 */
i2c_read_nbyte(AXP152_ADDR, AXP152_CHIP_VERSION, &version, 1);
if(version != 0x05)
{
rt_kprintf("[AXP152] probe fail!\n");
return -RT_ERROR;
}
else
{
rt_kprintf("[AXP152] probe ok! Version is 0x%02X\n",version);
}
/* config axp152 */
axp152_set_dcdc1(AXP_DCDC1_VOLT);
axp152_set_dcdc2(AXP_DCDC2_VOLT);
axp152_set_dcdc3(AXP_DCDC3_VOLT);
axp152_set_dcdc4(AXP_DCDC4_VOLT);
axp152_set_dldo1(AXP_DLDO1_VOLT);
axp152_set_dldo2(AXP_DLDO2_VOLT);
axp152_set_aldo1(AXP_ALDO1_VOLT);
axp152_set_aldo2(AXP_ALDO2_VOLT);
return RT_EOK;
}
INIT_DEVICE_EXPORT(axp152_init);
#endif /* BSP_USING_I2C_AXP152 */
不错的例子
汇总:https://bbs.21ic.com/icview-2922670-1-3.html
@21ic小喇叭 :好滴,收到了哦
建议您把这个帖子的链接,发到论坛【征集评测贴】的活动里,可以得到奖品哦