以下代码是我初始化I2C并调用函数的代码,其中设备读地址是0xAF设备写地址是0xAE,MAX30105_SLAVE_ADDR=0x07
I2C_init();
I2C_Params_init(&SbpI2cParams);
SbpI2cHandle=I2C_open(CC2640R2_LAUNCHXL_I2C0,&SbpI2cParams);
writeRegister(0xAE,0x04);
Task_sleep(80*1000/Clock_tickPeriod);
uint8_t sample_count;
for(sample_count=0;sample_count<5;sample_count++)
{
readRegister(0xAF,Try);
Task_sleep(80*1000/Clock_tickPeriod);
}
X下面是我的读写函数,编译也没有错误
//I2C读写函数
void writeRegister(uint8 addr,uint8 buf)
{
// ICall_CSState key;
// key = ICall_enterCriticalSection();
I2C_Transaction i2cTransaction;
uint8 txBuf[2]={addr,buf};
uint8 rxBuf[2];
i2cTransaction.writeBuf=txBuf;
i2cTransaction.writeCount=3;
i2cTransaction.readBuf=rxBuf;
i2cTransaction.readCount=0;
i2cTransaction.slaveAddress=MAX30105_SLAVE_ADDR;
I2C_transfer(SbpI2cHandle,&i2cTransaction);
// ICall_leaveCriticalSection(key);
}
void readRegister(uint8 addr,uint8 *rdata)
{
// ICall_CSState key;
// key = ICall_enterCriticalSection();
I2C_Transaction i2cTransaction;
uint8 TxBuf[1]={addr};
i2cTransaction.writeBuf=TxBuf;
i2cTransaction.writeCount=1;
i2cTransaction.readBuf=rdata;
i2cTransaction.readCount=3;
i2cTransaction.slaveAddress=MAX30105_SLAVE_ADDR;
I2C_transfer(SbpI2cHandle,&i2cTransaction);
//ICall_leaveCriticalSection(key);
}
Alvin Chen:
请参考TI提供的Demo。
dev.ti.com/…/node
Alvin Chen:
回复 Alvin Chen:
你的读写函数有问题:
//写某个地址,且写的数据为一位
void writeRegister(uint8 addr, uint8 buf)
{
I2C_Transaction i2cTransaction;
uint8 txBuf[] = {addr, buf,};
uint8 rxBuf[5];
i2cTransaction.writeBuf = txBuf;
i2cTransaction.writeCount = 2;
i2cTransaction.readBuf = rxBuf;
i2cTransaction.readCount = 0;
i2cTransaction.slaveAddress = MAX30105_SLAVE_ADDR; //arbitrary for demo
I2C_transfer(SbpI2cHandle, &i2cTransaction);
}//读某个地址且返回数据为一位
void readRegister(uint8 *buf, uint8 addr)
{I2C_Transaction i2cTransaction;
uint8 txBuf[] = {addr};uint8 rxBuf[5];i2cTransaction.writeBuf = txBuf;i2cTransaction.writeCount = 1;i2cTransaction.readBuf = buf;i2cTransaction.readCount = 1;i2cTransaction.slaveAddress = MAX30105_SLAVE_ADDR; //slave addrI2C_transfer(SbpI2cHandle, &i2cTransaction);
}
user6076575:
回复 Alvin Chen:
你好,前辈,首先非常感谢你的帮助,谢谢!还有一个问题是你给我改的读函数里的uint8 *buf没有用到,为什么他和addr调换位置,我这个函数是void函数,不能返回一个值,我想问问该怎么解决?
Alvin Chen:
回复 user6076575:
那只是一个简单的demo,主要是如何填充读写buf,你自己去修改一下就可以了。
user6076575:
回复 Alvin Chen:
你好按照你给的例子,我仿真运行到图中红框处程序就跑死了,具体内容见上图
Alvin Chen:
回复 user6076575:
/******************************************************************************* *Filename:bsp_i2c.c *Revised:$Date: $ *Revision:$Revision: $ * *Description:Layer added on top of RTOS driver for backwards*compatibility with non RTOS I2C driver. * *Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/ * *Redistribution and use in source and binary forms, with or without *modification, are permitted provided that the following conditions *are met: * *Redistributions of source code must retain the above copyright *notice, this list of conditions and the following disclaimer. * *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. * *Neither the name of Texas Instruments Incorporated nor the names of *its contributors 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 *OWNER 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. * *******************************************************************************/ #ifdef TI_DRIVERS_I2C_INCLUDED/******************************************************************************** INCLUDES*/ #include <ti/sysbios/BIOS.h> #include <ti/sysbios/knl/Semaphore.h> #include <ti/sysbios/knl/Task.h> #include <ti/sysbios/family/arm/cc26xx/Power.h>#include <ti/drivers/i2c/I2CCC26XX.h>#include <driverlib/prcm.h>#include "Board.h" #include "sensor.h"#include "bsp_i2c.h"/******************************************************************************** CONSTANTS*/ #define I2C_TIMEOUT 2500/******************************************************************************** GLOBAL variables*/ extern I2CCC26XX_HWAttrs i2cCC26xxHWAttrs[];/******************************************************************************** LOCAL variables*/ static volatile uint8_t slaveAddr; static volatile uint8_t interface; static I2C_Handle i2cHandle; static I2C_Params i2cParams; static Semaphore_Struct mutex; static const I2CCC26XX_I2CPinCfg pinCfg1 = {// Pin configuration for I2C interface 1.pinSDA = Board_I2C0_SDA1,.pinSCL = Board_I2C0_SCL1 };/******************************************************************************** @fnbspI2cWrite** @briefBurst write to an I2C device** @paramdata - pointer to data buffer* @paramlen - number of bytes to write** @returntrue if success*/ bool bspI2cWrite(uint8_t *data, uint8_t len) {I2C_Transaction masterTransaction;masterTransaction.writeCount= len;masterTransaction.writeBuf= data;masterTransaction.readCount= 0;masterTransaction.readBuf= NULL;masterTransaction.slaveAddress = slaveAddr;return I2C_transfer(i2cHandle, &masterTransaction) == TRUE; }/******************************************************************************** @fnbspI2cWriteSingle** @briefSingle byte write to an I2C device** @paramdata - byte to write** @returntrue if success*/ bool bspI2cWriteSingle(uint8_t data) {uint8_t d;d = data;return bspI2cWrite(&d, 1); }/******************************************************************************** @fnbspI2cRead** @briefBurst read from an I2C device** @paramdata - pointer to data buffer* @paramlen - number of bytes to write** @returntrue if success*/ bool bspI2cRead(uint8_t *data, uint8_t len) {I2C_Transaction masterTransaction;masterTransaction.writeCount= 0;masterTransaction.writeBuf= NULL;masterTransaction.readCount= len;masterTransaction.readBuf= data;masterTransaction.slaveAddress = slaveAddr;return I2C_transfer(i2cHandle, &masterTransaction) == TRUE; }/******************************************************************************** @fnbspI2cWriteRead** @briefBurst write/read from an I2C device** @paramwdata - pointer to write data buffer* @paramwlen - number of bytes to write* @paramrdata - pointer to read data buffer* @paramrlen - number of bytes to read** @returntrue if success*/ bool bspI2cWriteRead(uint8_t *wdata, uint8_t wlen, uint8_t *rdata, uint8_t rlen) {I2C_Transaction masterTransaction;masterTransaction.writeCount= wlen;masterTransaction.writeBuf= wdata;masterTransaction.readCount= rlen;masterTransaction.readBuf= rdata;masterTransaction.slaveAddress = slaveAddr;return I2C_transfer(i2cHandle, &masterTransaction) == TRUE; }/******************************************************************************** @fnbspI2cSelect** @briefSelect an I2C interface and slave** @paramnewInterface - selected interface* @paramaddress - slave address** @returntrue if success*/ bool bspI2cSelect(uint8_t newInterface, uint8_t address) {// Acquire I2C resourceif (!Semaphore_pend(Semaphore_handle(&mutex),MS_2_TICKS(I2C_TIMEOUT))){return false;}// Store new slave addressslaveAddr = address;// Interface changed ?if (newInterface != interface){// Store new interfaceinterface = newInterface;// Shut down RTOS driverI2C_close(i2cHandle);// Sets custom to NULL, selects I2C interface 0I2C_Params_init(&i2cParams);// Assign I2C data/clock pins according to selected I2C interface 1if (interface == BSP_I2C_INTERFACE_1){i2cParams.custom = (void*)&pinCfg1;}// Re-open RTOS driver with new bus pin assignmenti2cHandle = I2C_open(Board_I2C, &i2cParams);}return true; }/******************************************************************************** @fnbspI2cDeselect** @briefAllow other tasks to access the I2C driver** @paramnone** @returnnone*/ void bspI2cDeselect(void) {// Release I2C resourceSemaphore_post(Semaphore_handle(&mutex));}/******************************************************************************** @fnbspI2cInit** @briefInitialise the RTOS I2C driver (must be called only once)** @paramnone** @returnnone*/ void bspI2cInit(void) {Semaphore_Params semParamsMutex;// Create protection semaphoreSemaphore_Params_init(&semParamsMutex);semParamsMutex.mode = Semaphore_Mode_BINARY;Semaphore_construct(&mutex, 1, &semParamsMutex);// Reset the I2C controllerHapiResetPeripheral(PRCM_PERIPH_I2C0);I2C_init();I2C_Params_init(&i2cParams);i2cParams.bitRate = I2C_400kHz;i2cHandle = I2C_open(Board_I2C, &i2cParams);// Initialise local variablesslaveAddr = 0xFF;interface = BSP_I2C_INTERFACE_0;if (i2cHandle == NULL){Task_exit();} }/******************************************************************************** @fnbspI2cReset** @briefReset the RTOS I2C driver** @paramnone** @returnnone*/ void bspI2cReset(void) {// Acquire I2C resource */if (!Semaphore_pend(Semaphore_handle(&mutex),MS_2_TICKS(I2C_TIMEOUT))){return;}// Close the driverI2C_close(i2cHandle);// Reset the I2C controllerHapiResetPeripheral(PRCM_PERIPH_I2C0);// Reset local variablesslaveAddr = 0xFF;interface = BSP_I2C_INTERFACE_0;// Open driveri2cHandle = I2C_open(Board_I2C, &i2cParams);// Release I2C resourceSemaphore_post(Semaphore_handle(&mutex));}#endif上面这个段程序出自sensortag 请参考并自行做修改。
user6076575:
回复 Alvin Chen:
谢谢你
TI中文支持网




