在一些特殊的使用场合,有需要使用AWR1642对PMIC的输出电压进行调节,或是读取PMIC 内部的工作状态,此时需要I2C接口对PMIC进行读写操作,本文介绍了在现有的mmWave SDK demo中集成I2C接口驱动所需的步骤及操作。
本文的测试环境如下:
测试软件版本:mmWave SDK 2.0.0.4
测试硬件:AWR1642BOOST EVM
1. 在mmWave SDK demo中增加I2C驱动代码
第一步是添加代码以包含和初始化I2C驱动程序。从I2C接口发送和接收均需要此驱动程序的支持,以下是在mss_main.c中添加的初始化I2C驱动程序的C代码。此测试代码可以复制到现有工程中。I2C驱动操作步骤为:GPIO初始化,I2C驱动初始化,配置I2C改写PMIC(LP87524B)寄存器以实现PMIC输出电压调节。
注意LP87524B寄存器信息来源于其datasheet:LP87524B/J-Q1具有集成开关的具有集成开关的 4-A + 2.5-A + 两个 1.5-A 降压稳压器 数据表 (Rev. A),I2C驱动参考代码如下:
#include
static int32_t I2C_GPIOInit(void)
{
#if (defined(SOC_XWR14XX))
/* Setup the PINMUX to bring out the XWR14xx I2C pins */
Pinmux_Set_OverrideCtrl(SOC_XWR14XX_PINR3_PADAH, PINMUX_OUTEN_RETAIN_HW_CTRL, PINMUX_INPEN_RETAIN_HW_CTRL);
Pinmux_Set_FuncSel(SOC_XWR14XX_PINR3_PADAH, SOC_XWR14XX_PINR3_PADAH_I2C_SDA);
Pinmux_Set_OverrideCtrl(SOC_XWR14XX_PINP4_PADAI, PINMUX_OUTEN_RETAIN_HW_CTRL, PINMUX_INPEN_RETAIN_HW_CTRL);
Pinmux_Set_FuncSel(SOC_XWR14XX_PINP4_PADAI, SOC_XWR14XX_PINP4_PADAI_I2C_SCL);
#else
/* Setup the PINMUX to bring out the XWR16xx I2C pins */
Pinmux_Set_OverrideCtrl(SOC_XWR16XX_PINF13_PADAH, PINMUX_OUTEN_RETAIN_HW_CTRL, PINMUX_INPEN_RETAIN_HW_CTRL);
Pinmux_Set_FuncSel(SOC_XWR16XX_PINF13_PADAH, SOC_XWR16XX_PINF13_PADAH_I2C_SDA);
Pinmux_Set_OverrideCtrl(SOC_XWR16XX_PING14_PADAI, PINMUX_OUTEN_RETAIN_HW_CTRL, PINMUX_INPEN_RETAIN_HW_CTRL);
Pinmux_Set_FuncSel(SOC_XWR16XX_PING14_PADAI, SOC_XWR16XX_PING14_PADAI_I2C_SCL);
#endif
return 0;
}
static int32_t I2C_Communication_Test()
{
bool retVal = false;
int32_t errCode = 0;
uint32_t arg;
uint8_t rxData[16];
uint8_t txData[16];
I2C_Transaction i2cTransaction;
I2C_Handle i2cHandle;
I2C_Params i2cParams;
/* Reset the transmit and receive buffer */
memset(&rxData, 0, sizeof (rxData));
/* Initializa the I2C driver */
I2C_init();
/* Initialize the I2C driver default parameters */
I2C_Params_init(&i2cParams);
i2cParams.transferMode = I2C_MODE_BLOCKING;
i2cParams.bitRate = I2C_100kHz;
/* Open the I2C driver */
i2cHandle = I2C_open(0, &i2cParams);
if (i2cHandle == NULL)
{
System_printf ("Error: I2C Driver Open failedn");
return -1;
}
/* Configure the I2C device in I2C_CMD_ADDR_MODE mode */
arg = 0;//arg set to 0, and the I2C addr mode is 7-bit.
errCode = I2C_control (i2cHandle, I2C_CMD_ADDR_MODE, (void* )&arg);
if (errCode < 0)
{
System_printf ("Error: I2C control Set I2C_CMD_ADDR_MODE failed [Error code %d]n", errCode);
return -1;
}
//read the LP87524B buck3 output voltage register
txData[0] = 0x10;
i2cTransaction.slaveAddress = 0x60;//LP87524B/J-Q1 Device Address = 0x60
i2cTransaction.writeBuf = txData;
i2cTransaction.writeCount = 1;
i2cTransaction.readBuf = rxData;
i2cTransaction.readCount = 1;
retVal = I2C_transfer(i2cHandle, &i2cTransaction);
if (retVal == false)
{
System_printf ("Error: I2C Transfer failedn");
return -1;
}
System_printf("I2C_READ: addr=0x%x, Val=0x%xn",txData[0],rxData[0]);
//write the LP87524B buck3 output voltage = 2.0V
txData[0] = 0x10;
txData[1] = 0xbb;//default=0xca,2.3V,0xb1=1.8V, 0xbb=2.0V
i2cTransaction.slaveAddress = 0x60;//LP87524B/J-Q1 Device Address = 0x60
i2cTransaction.writeBuf = txData;
i2cTransaction.writeCount = 2;
i2cTransaction.readBuf = rxData;
i2cTransaction.readCount = 0;
retVal = I2C_transfer(i2cHandle, &i2cTransaction);
if (retVal == false)
{
System_printf ("Error: I2C Transfer failedn");
return -1;
}
System_printf("I2C_Write: LP87524B buck3 = 2.0V Donen");
//read the LP87524B buck3 output voltage register
txData[0] = 0x10;
i2cTransaction.slaveAddress = 0x60;//LP87524B/J-Q1 Device Address = 0x60
i2cTransaction.writeBuf = txData;
i2cTransaction.writeCount = 1;
i2cTransaction.readBuf = rxData;
i2cTransaction.readCount = 1;
retVal = I2C_transfer(i2cHandle, &i2cTransaction);
if (retVal == false)
{
System_printf ("Error: I2C Transfer failedn");
return -1;
}
System_printf("I2C_READ: addr=0x%x, Val=0x%xrn",txData[0],rxData[0]);
//write the LP87524B buck2 output voltage = 1.5V
txData[0] = 0x0e;
txData[1] = 0Xa2;//default=0xb1,1.8V, 0x4d=1V 0xa2=1.5V
i2cTransaction.slaveAddress = 0x60;//LP87524B/J-Q1 Device Address = 0x60
i2cTransaction.writeBuf = txData;
i2cTransaction.writeCount = 2;
i2cTransaction.readBuf = rxData;
i2cTransaction.readCount = 0;
retVal = I2C_transfer(i2cHandle, &i2cTransaction);
if (retVal == false)
{
System_printf ("Error: I2C Transfer failedn");
return -1;
}
System_printf("I2C_Write: LP87524B buck2 = 1.5V Donen");
if (retVal == false)
{
System_printf ("Error: I2C combined Transfer to Slave failedn");
return -1;
}
/* Close I2C driver */
I2C_close(i2cHandle);
return 0;
}
//Call these function in void MmwDemo_mssInitTask(UArg arg0, UArg arg1)
errCode = I2C_GPIOInit();
if (errCode == -1)
System_printf("I2C_GPIOInit Errorrn");
errCode = I2C_Communication_Test();
if (errCode == -1)
System_printf("I2C_Communication_Test Errorn");
else
System_printf("I2C_Communication_Test Donen");
2. 将工程链接到I2C驱动库
最后一步是通过链接I2C驱动程序来构建可执行文件。 如果使用CCS项目,可以将I2C驱动程序添加到项目的链接器属性中,如图1所示。
图1. CCS Project Linker Properties
如果使用makefile编译工程,执行相同的程序。
###################################################################################
# Additional libraries which are required to build the DEMO:
###################################################################################
MSS_MMW_DEMO_STD_LIBS = $(R4F_COMMON_STD_LIB)
-llibpinmux_$(MMWAVE_SDK_DEVICE_TYPE).$(R4F_LIB_EXT)
-llibdma_$(MMWAVE_SDK_DEVICE_TYPE).$(R4F_LIB_EXT)
-llibcrc_$(MMWAVE_SDK_DEVICE_TYPE).$(R4F_LIB_EXT)
-llibuart_$(MMWAVE_SDK_DEVICE_TYPE).$(R4F_LIB_EXT)
-llibgpio_$(MMWAVE_SDK_DEVICE_TYPE).$(R4F_LIB_EXT)
-llibmailbox_$(MMWAVE_SDK_DEVICE_TYPE).$(R4F_LIB_EXT)
-llibmmwavelink_$(MMWAVE_SDK_DEVICE_TYPE).$(R4F_LIB_EXT)
-llibmmwave_$(MMWAVE_SDK_DEVICE_TYPE).$(R4F_LIB_EXT)
-llibcli_$(MMWAVE_SDK_DEVICE_TYPE).$(R4F_LIB_EXT)
-llibi2c_$(MMWAVE_SDK_DEVICE_TYPE).$(R4F_LIB_EXT)
MSS_MMW_DEMO_LOC_LIBS = $(R4F_COMMON_LOC_LIB)
-i$(MMWAVE_SDK_INSTALL_PATH)/ti/drivers/pinmux/lib
-i$(MMWAVE_SDK_INSTALL_PATH)/ti/drivers/uart/lib
-i$(MMWAVE_SDK_INSTALL_PATH)/ti/drivers/dma/lib
-i$(MMWAVE_SDK_INSTALL_PATH)/ti/drivers/crc/lib
-i$(MMWAVE_SDK_INSTALL_PATH)/ti/drivers/gpio/lib
-i$(MMWAVE_SDK_INSTALL_PATH)/ti/drivers/mailbox/lib
-i$(MMWAVE_SDK_INSTALL_PATH)/ti/control/mmwavelink/lib
-i$(MMWAVE_SDK_INSTALL_PATH)/ti/control/mmwave/lib
-i$(MMWAVE_SDK_INSTALL_PATH)/ti/utils/cli/lib
-i$(MMWAVE_SDK_INSTALL_PATH)/ti/utils/i2c/lib
3. 运行测试例程
运行测试程序后,CCS控制台输出打印信息如下,使用万用表测量LP87524输出电压即为设置的电压,I2C驱动添加成功。
**********************************************
Debug: Launching the Millimeter Wave Demo
**********************************************
I2C_READ: addr=0x10, Val=0xca
I2C_Write: LP87524B buck3 = 2.0V Done
I2C_READ: addr=0x10, Val=0xbb
I2C_Write: LP87524B buck2 = 1.5V Done
I2C_Communication_Test Done
审核编辑:郭婷
全部0条评论
快来发表一下你的评论吧 !