443|3

963

帖子

2

资源

纯净的硅(中级)

RT1052 (8) 驱动Analogysemi CSD203 [复制链接]

RT1052 (8) 驱动Analogysemi CSD203

       在上一篇中,驱动了spi的dac,这一篇我们用iic器件试试驱动csd203。大多数步骤和上一篇是重复的,最大的区别是代码和芯片,所以咱们看代码即可。

image-20230111112437-1.jpeg  

图1:测试环境

image-20230111112437-2.png  

图2:CSD203介绍

image-20230111112437-3.png  

图3:CSD选型手册

image-20230111112437-4.png  

图4:CSD203 demo 原理图

代码1:ANALOGYSEMICSD.C

#include "AnalogysemiCSD.h"

/*Initial CSD203*/

void CSD203_Init(CSD_CONFIG *CSD203_CFG)

{

    uint16_t Data=0,ADDR=0;

    uint8_t Datas[2];

    Data|=(CSD203_CFG->RST)<<15;

    Data|=(CSD203_CFG->Average)<<9;

    Data|=(CSD203_CFG->VBUS_Conv_Time)<<5;

    Data|=(CSD203_CFG->VShunt_Conv_Time)<<2;

    Data|=CSD203_CFG->Mode;

    ADDR=(CSD203_CFG->DeviceADDR);

    Datas[0]=Data>>8;

    Datas[1]=Data&0x00ff;

    Lpi2c_Write(ADDR,CONFIGURATION,0x01,0x02,Datas);

    Data=CalParam/((CSD203_CFG->CurrentLSB)*(CSD203_CFG->Rshunt));

    Datas[0]=Data>>8;

    Datas[1]=Data&0x00ff;

    Lpi2c_Write(ADDR,CALIBRATION,0x01,0x02,Datas);

}

/* CSD203 Alert*/

/*

void CSD203_Alert(CSD_ALERT *CSD_ALT)

{

       uint16_t Data=0,ADDR=0;

       Data|=(CSD_ALT->RST)<<15;

       IIC_DUT_W(ADDR,MASKENABLE,Data);

       printf("ALERT Register :%x\r\n",Data);

}

*/

/*Read Vbus*/

uint16_t CSD203_ReadVbus(CSD_CONFIG *CSD203_CFG)

{

 

    uint16_t Data=0,ADDR=0;

    uint8_t datas[2];

    uint8_t datah,datal;

    ADDR=(CSD203_CFG->DeviceADDR);

    //Data=IIC_DUT_R(ADDR,BUS_VOLTAGE);

    Lpi2c_Read(ADDR,BUS_VOLTAGE,0x01,0x02,datas);

    datah=datas[0];

    datal=datas[1];

    Data=datah;

    Data<<=8;

    Data|=datal;

    return Data;

}

/*Read Rshunt*/

uint16_t CSD203_ReadRshunt(CSD_CONFIG *CSD203_CFG)

{

 

    uint16_t Data=0,ADDR=0;

    uint8_t datas[2];

    uint8_t datah,datal;

    ADDR=(CSD203_CFG->DeviceADDR);

    Lpi2c_Read(ADDR,SHUNT_VOLTAGE,0x01,0x02,datas);

    datah=datas[0];

    datal=datas[1];

    Data=datah;

    Data<<=8;

    Data|=datal;

    return Data;

}

/*Read Power*/

uint16_t CSD203_ReadPower(CSD_CONFIG *CSD203_CFG)

{

    uint16_t Data=0,ADDR=0;

    uint8_t datas[2];

    uint8_t datah,datal;

    ADDR=(CSD203_CFG->DeviceADDR);

    Lpi2c_Read(ADDR,POWER,0x01,0x02,datas);

    datah=datas[0];

    datal=datas[1];

    Data=datah;

    Data<<=8;

    Data|=datal;

    return Data;

}

/*Read Curent*/

uint16_t CSD203_ReadCurrent(CSD_CONFIG *CSD203_CFG)

{

 

    uint16_t Data=0,ADDR=0;

    uint8_t datas[2];

    uint8_t datah,datal;

    ADDR=(CSD203_CFG->DeviceADDR);

    Lpi2c_Read(ADDR,CURRENT,0x01,0x02,datas);

    datah=datas[0];

    datal=datas[1];

    Data=datah;

    Data<<=8;

    Data|=datal;

    return Data;

}

 

代码2:ANALOGYSEMICSD.H

#ifndef AnalogysemiCSD_H

#define AnalogysemiCSD_H

/* include mcu lib*/

 

//#include "stm32f4xx_hal.h"

#include "stdio.h"

#include "bsp_lpi2c.h"

//#include "IIC.h"

 

/* CSD203 Regsistor map */

#define CONFIGURATION   0X00

/*Read Only*/

#define SHUNT_VOLTAGE   0X01

#define BUS_VOLTAGE     0X02

#define POWER           0X03

#define CURRENT         0x04

/*Read Only*/

#define CALIBRATION     0x05

#define MASKENABLE      0x06

#define ALERTLIMIT      0x07

 

 

/*Calibration Calculation parameter*/

/* This Parameter Gain*10000K*/

#define CalParam        51200

/* ↓ add your shunt here ↓   */

#define CSD_CONFIG_Rs_1mR                                      1

#define CSD_CONFIG_Rs_2mR                                      2

#define CSD_CONFIG_Rs_5mR                                      5

#define CSD_CONFIG_Rs_10mR                             10

#define CSD_CONFIG_Rs_20mR                             20

#define CSD_CONFIG_Rs_50mR                             50

#define CSD_CONFIG_CurrentLsb1mA   10

#define CSD_CONFIG_CurrentLsb2mA   20

#define CSD_CONFIG_CurrentLsb5mA   50

#define CSD_CONFIG_CurrentLsb10mA  100

#define CSD_CONFIG_CurrentLsb20mA  200

/*Calibration Calculation parameter*/

 

 

 

 

/* CSD203 Regsistor Config*/

#define CSD_CONFIG_RST          1

#define CSD_CONFIG_UnRST      0

 

#define CSD_CONFIG_Avg1              0

#define CSD_CONFIG_Avg4              1

#define CSD_CONFIG_Avg16            2

#define CSD_CONFIG_Avg64            3

#define CSD_CONFIG_Avg128          4

#define CSD_CONFIG_Avg256          5

#define CSD_CONFIG_Avg512          6

#define CSD_CONFIG_Avg1024  7

 

#define CSD_CONFIG_VBUS_CT1_1mS 4

#define CSD_CONFIG_VShunt_CT1_1mS 4

 

#define CSD_CONFIG_ShuntBus_CON 7

 

#define CSD_CONFIG_ADDR_A1_GND_A0_GND 0x80

#define CSD_CONFIG_ADDR_A1_GND_VS_GND 0x82

#define CSD_CONFIG_ADDR_A1_GND_SDA_GND 0x84

#define CSD_CONFIG_ADDR_A1_GND_SCL_GND 0x86

#define CSD_CONFIG_ADDR_A1_VS_A0_GND 0x88

#define CSD_CONFIG_ADDR_A1_VS_A0_VS 0x8a

#define CSD_CONFIG_ADDR_A1_VS_A0_SDA 0x8c

#define CSD_CONFIG_ADDR_A1_VS_A0_SCL 0x8e

#define CSD_CONFIG_ADDR_A1_SDA_A0_GND 0x90

#define CSD_CONFIG_ADDR_A1_SDA_A0_VS 0x92

#define CSD_CONFIG_ADDR_A1_SDA_A0_SDA 0x94

#define CSD_CONFIG_ADDR_A1_SDA_A0_SCL 0x96

#define CSD_CONFIG_ADDR_A1_SCL_A0_GND 0x98

#define CSD_CONFIG_ADDR_A1_SCL_A0_VS 0x9a

#define CSD_CONFIG_ADDR_A1_SCL_A0_SDA 0x9c

#define CSD_CONFIG_ADDR_A1_SCL_A0_SCL 0x9e

/******************/

/*CSD Alert Option*/

/******************/

/*Vshunt Voltage Over Voltage*/

#define CSD_ALERT_VShunt_OVA_ON     1

#define CSD_ALERT_VShunt_OVA_OFF    0

/*Vshunt Voltage Under Voltage*/

#define CSD_ALERT_VShunt_UVA_ON      1

#define CSD_ALERT_VShunt_UVA_OFF     0

/*VBus Voltage Over Voltage*/

#define CSD_ALERT_VBUS_OVA_ON        1

#define CSD_ALERT_VBUS_OVA_OFF       0

/*VBus Voltage Under Voltage*/

#define CSD_ALERT_VBUS_UVA_ON 1

#define CSD_ALERT_VBUS_UVA_OFF       0

/* Power Over Limit */

#define CSD_ALERT_Power_Over_ON      1

#define CSD_ALERT_Power_Over_OFF      0

/* ADC Coversion Ready */

#define CSD_ALERT_CoversionReady_ON       1

#define CSD_ALERT_CoversionReady_OFF       0

#define CSD_ALERT_CNVR_FLAG_ON      1

#define CSD_ALERT_CNVR_FLAG_OFF      0

/*  ALERT Function Flag and Alert latch Work Together */

#define CSD_ALERT_ALERT_FLAG_ON      1

#define CSD_ALERT_ALERT_FLAG_OFF     0

#define CSD_ALERT_ALERT_Latch_ON      1

#define CSD_ALERT_ALERT_Latch_OFF     0

/*Power over flow */

#define CSD_ALERT_OVF_ON    1

#define CSD_ALERT_OVF_OFF   0

/*Alert Pin */

#define CSD_ALERT_APOL_ActiveLow      1

#define CSD_ALERT_APOL_ActiveHigh 0

 

/*CSD Basic Configuration struct*/

typedef struct{

       uint8_t DeviceADDR;

       uint8_t RST;

       uint8_t Average;

       uint8_t VBUS_Conv_Time;

       uint8_t VShunt_Conv_Time;

       uint8_t Mode;

       uint16_t CurrentLSB;

       uint16_t Rshunt;

}CSD_CONFIG;

/*CSD Basic Configuration struct*/

 

/*CSD ALERT Configuration struct*/

typedef struct{

       uint8_t VShunt_OVA;

       uint8_t VShuntUVA;

       uint8_t VBUS_OVA;

       uint8_t VBUS_UVA;

       uint8_t Power_OVER_Limit;

       uint8_t CNVR;

       uint8_t ALERT_FLAG;

       uint8_t CNVR_FLAG;

       uint8_t OVF;

       uint8_t APOL;

       uint8_t ALERT_Latch;

}CSD_ALERT;

/*CSD ALERT Configuration struct*/

 

 

/*Initial CSD203 Config*/

void CSD203_Alert(CSD_ALERT *CSD_ALT);

void CSD203_Init(CSD_CONFIG *CSD203_CFG);

/* Read Vbus Voltage*/

uint16_t CSD203_ReadVbus(CSD_CONFIG *CSD203_CFG);

/*Read Rshunt*/

uint16_t CSD203_ReadRshunt(CSD_CONFIG *CSD203_CFG);

/*Read Power*/

uint16_t CSD203_ReadPower(CSD_CONFIG *CSD203_CFG);

/*Read Current*/

uint16_t CSD203_ReadCurrent(CSD_CONFIG *CSD203_CFG);

 

#endif

 

代码3:LPI2C.c

#include "fsl_iomuxc.h"

#include "fsl_gpio.h"

#include "fsl_lpi2c.h"

#include "pad_config.h"

#include "bsp_lpi2c.h"

 

 

static void LPI2C_GPIO_Init(void);

 

static void LPI2C_GPIO_Init(void)

{

        IOMUXC_SetPinMux(

        IOMUXC_GPIO_AD_B1_00_LPI2C1_SCL, /* GPIO_AD_B1_00 is configured as LPI2C1_SCL */

        1U);

        /* Software Input On Field: Input Path is determined by functionality */

        IOMUXC_SetPinMux(

        IOMUXC_GPIO_AD_B1_01_LPI2C1_SDA, /* GPIO_AD_B1_01 is configured as LPI2C1_SDA */

        1U); /* Software Input On Field: Input Path is determined by functionality */

        IOMUXC_SetPinConfig(IOMUXC_GPIO_AD_B1_00_LPI2C1_SCL, 0x18B0U);

        IOMUXC_SetPinConfig(IOMUXC_GPIO_AD_B1_01_LPI2C1_SDA, 0x18B0U);

}

void LPI2C_Init(void)

{

    //config Lpi2c clock

    CLOCK_SetMux(kCLOCK_Lpi2cMux, 0u);

    CLOCK_SetDiv(kCLOCK_Lpi2cDiv, 5u);

    //initial Gpio config

    LPI2C_GPIO_Init();

    lpi2c_master_config_t lpi2cConfig = {0};

    /*

 

    * lpi2cConfig.debugEnable = false;

 

    * lpi2cConfig.ignoreAck = false;

 

    * lpi2cConfig.pinConfig = kLPI2C_2PinOpenDrain;

 

    * lpi2cConfig.baudRate_Hz = 100000U;

 

    * lpi2cConfig.busIdleTimeout_ns = 0;

 

    * lpi2cConfig.pinLowTimeout_ns = 0;

 

    * lpi2cConfig.sdaGlitchFilterWidth_ns = 0;

 

    * lpi2cConfig.sclGlitchFilterWidth_ns = 0;

 

    */

    //lpi2cConfig.ignoreAck = true;

    //lpi2cConfig.baudRate_Hz=1000U;

    LPI2C_MasterGetDefaultConfig(&lpi2cConfig);

    LPI2C_MasterInit(LPI2C1, &lpi2cConfig, LPI2C1_CLOCK_FREQ);

 

}

 

bool Lpi2c_Write(uint8_t SlaveAdd,uint8_t subAdd,uint8_t subAddsize,uint8_t datasize,uint8_t *data)

{

    lpi2c_master_transfer_t trans;

    status_t reVal = kStatus_Fail;

    trans.data=data;

    trans.dataSize=datasize;

    trans.direction=kLPI2C_Write;

    trans.slaveAddress=(SlaveAdd>>1);

    trans.subaddress=subAdd;

    trans.subaddressSize=subAddsize;

    trans.flags=kLPI2C_TransferDefaultFlag;

    reVal = LPI2C_MasterTransferBlocking(LPI2C1,&trans);

    if (reVal != kStatus_Success)

    {

    return 1;

    }

    return 0;

}

void Lpi2c_Read(uint8_t SlaveAdd,uint8_t subAdd,uint8_t subAddsize,uint8_t datasize,uint8_t *data)

{

 

    lpi2c_master_transfer_t trans;

    status_t reVal = kStatus_Fail;

    trans.data=data;

    trans.dataSize=datasize;

    trans.direction=kLPI2C_Read;

    trans.slaveAddress=(SlaveAdd>>1);

    trans.subaddress=subAdd;

    trans.subaddressSize=subAddsize;

    trans.flags=kLPI2C_TransferDefaultFlag;

    reVal = LPI2C_MasterTransferBlocking(LPI2C1,&trans);

 

   // return data;

}

代码5:LPI2C.h

#ifndef __BSP_LPI2C_H

#define __BSP_LPI2C_H

 

#include "fsl_common.h"

 

 

 

#define LPI2C1_CLOCK_FREQ 3000000UL

 

 

void LPI2C_Init(void);

bool Lpi2c_Write(uint8_t SlaveAdd,uint8_t subAdd,uint8_t subAddsize,uint8_t datasize,uint8_t *data);

void Lpi2c_Read(uint8_t SlaveAdd,uint8_t subAdd,uint8_t subAddsize,uint8_t datasize,uint8_t *data);

 

#endif /* __BSP_LPSPI_H */

 

参考文档:

RT1052 (7) 驱动SPI-DAC

Power Monitor by AnalogySemi

此帖出自NXP MCU论坛
个人签名

希望做一些大家觉得好用的东西!


回复

963

帖子

2

资源

纯净的硅(中级)

工程文件

RTT NANO.7z (21.64 MB, 下载次数: 0)
此帖出自NXP MCU论坛
个人签名

希望做一些大家觉得好用的东西!


回复

963

帖子

2

资源

纯净的硅(中级)

CSD20X_Server_Apps Brief_Ver1.0_20221119.pdf (4.87 MB, 下载次数: 0)
此帖出自NXP MCU论坛
个人签名

希望做一些大家觉得好用的东西!


回复

963

帖子

2

资源

纯净的硅(中级)

图片.png  

此帖出自NXP MCU论坛
个人签名

希望做一些大家觉得好用的东西!


回复
您需要登录后才可以回帖 登录 | 注册

相关帖子
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/5 下一条

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区知春路23号集成电路设计园量子银座1305 电话:(010)82350740 邮编:100191

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2022 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表