This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
想问下我用的是28027但例程DSP2802x I2C EEPROM Example怎么跑不通?已经是过几个controlsuit里的DSP2802x I2C EEPROM Example例子都不行。不知道什么原因?
是自己做的板子吗?外部是否有挂EEPROM的芯片呢?有没量一下对应的I2C管脚是时钟和信号是否正常?同时,可以单步调试看看,寄存器写入是否在在成功。
确定过是28027,能不能把我看下这个程序ti\controlSUITE\device_support\f2802x\v200\f2802x_examples\i2c_eeprom,其他的i2c_eeprom都是一样的,只是description略有不同。我的程序是跑不通。程序r单步调试之后,一直在这两条语句之间运行,if(I2cMsgOut1.MsgStatus == I2C_MSGSTAT_SEND_WITHSTOP) 和 if (I2cMsgOut1.MsgStatus == I2C_MSGSTAT_INACTIVE)。一直检测是读还是写状态
我看EEPROM程序就是要对LMP9100进行读写操作,下面是我自己根据LMP9100还有TI给的例程自己写的程序,TI提供的例程用到了中断,我的没有用中断方式读写。帮我看下有错误没有,
void LMP91000_Init(void)
{
// Initialize I2C
I2caRegs.I2CSAR = 0x0048; // LMP9100 Slave address
// I2CCLK = SYSCLK/(I2CPSC+1)
I2caRegs.I2CPSC.all = 6; // Prescaler - need 7-12 Mhz on module clk
I2caRegs.I2CCLKL = 55; // NOTE: must be non zero
I2caRegs.I2CCLKH = 35; // NOTE: must be non zero
I2caRegs.I2CMDR.all = 0x0020; // Take I2C out of reset
// Stop I2C when suspended
I2caRegs.I2CFFTX.all = 0x6000; // Enable FIFO mode and TXFIFO
I2caRegs.I2CFFRX.all = 0x2040; // Enable RXFIFO, clear RXFFINT,
return;
}
void LMP91000_Write(Uint16 address,Uint16 data)
{
TXByteCtr=2; //发送多少位
// Wait until the STP bit is cleared from any previous master communication.
// Clearing of this bit by the module is delayed until after the SCD bit is
// set. If this bit is not checked prior to initiating a new message, the
// I2C could get confused.
while (I2caRegs.I2CMDR.bit.STP == 1)
I2caRegs.I2CSAR = 0x0048;
while (I2caRegs.I2CSTR.bit.BB == 1);
I2caRegs.I2CCNT=TXByteCtr;
I2CBufferArray[1] = address; //先存储发送LMP91000需要写数据的寄存器地址到中间数组
I2CBufferArray[0] = data; //再存储发送LMP91000需要到寄存器地址的数据到中间数组
I2caRegs.I2CDXR = I2CBufferArray[1];
I2caRegs.I2CDXR = I2CBufferArray[0];
I2caRegs.I2CMDR.all = 0x6e20; //使能I2C,主模式写,7位地址,internal data counter of the I2C module counts down to 0产生STOP
return;
}
Uint16 LMP91000_Read(Uint16 address)
{
// Wait until the STP bit is cleared from any previous master communication.
// Clearing of this bit by the module is delayed until after the SCD bit is
// set. If this bit is not checked prior to initiating a new message, the
// I2C could get confused.
while (I2caRegs.I2CMDR.bit.STP == 1);
I2caRegs.I2CSAR = 0x0048;
I2caRegs.I2CCNT = 1;
I2caRegs.I2CDXR = address;
I2caRegs.I2CMDR.all = 0x2e20; //发送start位,主模式写,internal data counter of the I2C module counts down to 0产生STOP
while (I2caRegs.I2CSTR.bit.BB == 1); //总线是否忙
I2caRegs.I2CSAR = 0x0048;
I2caRegs.I2CCNT = 1;
I2caRegs.I2CMDR.all = 0x2620; //发送start位,主模式读,不产生stop
if(I2caRegs.I2CSTR.bit.NACKSNT == 1) //如果I2C模块是否发送发送NACK
{
I2caRegs.I2CMDR.bit.STP =1; //stop位置1
I2caRegs.I2CSTR.bit.NACKSNT=1;清除NACK标志位
I2CBuffer = I2caRegs.I2CDRR;
}
return I2CBuffer; //返回接收到的值
}
LMP9100的读写过程是参考LMP9100文档里的时序写的。但是进行第二次读或者写的是就过不去了。一直是总线忙,可是我已经都把STP为置1了。为什么总线还是忙呢?
您好,很感谢您的回复。在此能打搅您一下吗?我最近在做C2000的IIC的通信,但是我的I2C_write ,I2C_read还有一些问题。能否向您请教一下。
写地址时没有出现问题,但是在连续的读几次数据之后,便进入了 死循环之中。
void I2CA_Init(void) //Initialize I2C //从机的地址则在write函数中写入
{
// I2CCLK = SYSCLK/(I2CPSC+1)
#if (CPU_FRQ_40MHZ||CPU_FRQ_50MHZ) //则不符合系统的设置,则编译器将其显示为灰色的部分
I2caRegs.I2CPSC.all = 4; // Prescaler - need 7-12 Mhz on module clk
#endif
#if (CPU_FRQ_60MHZ)
I2caRegs.I2CPSC.all = 6; // Prescaler - need 7-12 Mhz on module clk
#endif
I2caRegs.I2CCLKL = 10; // NOTE: must be non zero
I2caRegs.I2CCLKH = 10; // NOTE: must be non zero frist 5
I2caRegs.I2CMDR.all = 0x0020; // Take I2C out of reset
// Stop I2C when suspended
I2caRegs.I2CIER.bit.RRDY = 0; // 使能RRDY中断
I2caRegs.I2CIER.bit.NACK = 1; //NACK中断
I2caRegs.I2CIER.bit.XRDY = 0; //禁用XRDY中断 //用户应该在此处禁用或使能中断
I2caRegs.I2CFFTX.all = 0x6000; // enable FIFO mode and TX_FIFO
I2caRegs.I2CFFRX.all = 0x2040; // enable RX_FIFO, clear RXFFINT,
I2caRegs.I2CMDR.all = 0x0020; //退出复位模式
}
/************************************************************************************
*在TI的官方论坛中找到的例程,而较为靠谱的一个write程序
*num_bytes则应该与FIFO的设置相关(而前面并没有设置相应的级数,不进行中断的比较)但是要求小于4
*char stop_byte中则是对于read,write部分的是否需要stop信号(write则为需要stop信号,read不需要stop信号)
*
************************************************************************************/
void I2CA_WriteData(unsigned char *write_data, unsigned int slave_addr, unsigned int num_bytes, char stop_bit)
{
unsigned int i; // counter
while(I2caRegs.I2CMDR.bit.STP == 1) {
} // wait until the STP bit is cleared from any previous master communication
while(I2caRegs.I2CSTR.bit.BB == 1) {
} // wait until bus not busy
I2caRegs.I2CSAR = slave_addr&0x00ff; // slave address register set
I2caRegs.I2CCNT = num_bytes; // setup number of bytes to send,同样的涉及到了I2CCNT的数值的问题,产生stop信号
for(i = 0; i < num_bytes; i++) // setup data to send, # of bytes equals num_bytes
{
I2caRegs.I2CDXR = write_data[i]&0x00ff; // write data into Data Transmit FIFO,把高八位舍去
}
if(stop_bit == 'y') // if there should be a stop bit at the end of transmission, then
{
I2caRegs.I2CMDR.all = 0x6E20; // send start as master transmitter, w/ stop condition once all bytes have been sent
//7-bit地址,发送模式,8-bit数据,FREE位
while(I2caRegs.I2CSTR.bit.SCD != 1) {} // wait until stop condition is detected
I2caRegs.I2CSTR.bit.SCD = 1; // clear stop condition detected bit in I2C status register
}
else // if there should not be a stop condition at end of transmission (master retains control of bus), then
{
I2caRegs.I2CMDR.all = 0x2620; // start condition, master mode, TX mode,仅仅只是以一个FREE的位
while(I2caRegs.I2CSTR.bit.ARDY != 1) {} // wait for I2C registers to finish working
I2caRegs.I2CSTR.bit.ARDY = 1; // clear ARDY bit in I2C status register
}
return ; // end of function, return value is void
}
/*************************************************************************************
* I2CA_ReadData函数则为先调用write的函数,之后在读取
* num_bytes1为所读取的数据
* 不使用中断模式来读取数据
*************************************************************************************/
unsigned char I2CA_ReadData(unsigned char *write_data, unsigned int slave_addr,unsigned int num_bytes1, unsigned int num_bytes)
{
unsigned int result=0;
//unsigned int a=0;
I2CA_WriteData(write_data, slave_addr, num_bytes, 0); //最后一位则不发送stop信号,发送从机地址与数据地址
//DSP28x_usDelay(50); //延时处理
I2caRegs.I2CCNT = num_bytes1; // setup number of bytes to send,同样的涉及到了I2CCNT的数值的问题,产生stop信号
I2caRegs.I2CMDR.all=0x6c20; // bit 14 FREE = 1 // bit 13 STT = 1 (ReStart condition)
// bit 11 STP =1 (Stop condition after transfer of bytes.)
// bit 10 MST = 1 Master // bit 9 TRX = 0 Receiver
// bit 5 IRS = 1 to Reset I2C bus
while(I2caRegs.I2CSTR.bit.SCD != 1) {} // wait until stop condition is detected
result=I2caRegs.I2CDRR;
I2caRegs.I2CSTR.bit.SCD = 1; // clear stop condition detected bit in I2C status register
return result;
}
我想说一句:我在用28035的I2C的时候也是没办法与EEPROM通讯,我用的24C02的芯片。问题是用controlsuit上的例程就是不行,没办法测到响应信号,最后只能用I/O口的传通方法来通讯的。到现在我也不知是为什么!
我用的AT24C02,完全用的controlsuit里面的例程,只把地址改成了0xa0,但是读不出数据,不知道怎么回事
#include "DSP28x_Project.h" // Device Headerfile and Examples Include File
// Note: I2C Macros used in this example can be found in the
// DSP2802x_I2C_defines.h file
// Prototype statements for functions found within this file.
void I2CA_Init(void);
Uint16 I2CA_WriteData(struct I2CMSG *msg);
Uint16 I2CA_ReadData(struct I2CMSG *msg);
interrupt void i2c_int1a_isr(void);
void pass(void);
void fail(void);
unsigned int a=0,b=0,c=0,d=0,e=0,f=0;
#define I2C_SLAVE_ADDR 0xa0
#define I2C_NUMBYTES 2
#define I2C_EEPROM_HIGH_ADDR 0x01
#define I2C_EEPROM_LOW_ADDR 0x30
// Global variables
// Two bytes will be used for the outgoing address,
// thus only setup 14 bytes maximum
struct I2CMSG I2cMsgOut1={I2C_MSGSTAT_SEND_WITHSTOP,
I2C_SLAVE_ADDR,
I2C_NUMBYTES,
I2C_EEPROM_HIGH_ADDR,
I2C_EEPROM_LOW_ADDR,
0x12, // Msg Byte 1
0x34}; // Msg Byte 2
struct I2CMSG I2cMsgIn1={ I2C_MSGSTAT_SEND_NOSTOP,
I2C_SLAVE_ADDR,
I2C_NUMBYTES,
I2C_EEPROM_HIGH_ADDR,
I2C_EEPROM_LOW_ADDR};
struct I2CMSG *CurrentMsgPtr; // Used in interrupts
Uint16 PassCount;
Uint16 FailCount;
void main(void)
{
Uint16 Error;
Uint16 i;
CurrentMsgPtr = &I2cMsgOut1;
// Step 1. Initialize System Control:
// PLL, WatchDog, enable Peripheral Clocks
// This example function is found in the DSP2802x_SysCtrl.c file.
InitSysCtrl();
// Step 2. Initalize GPIO:
// This example function is found in the DSP2802x_Gpio.c file and
// illustrates how to set the GPIO to it's default state.
// InitGpio();
// Setup only the GP I/O only for I2C functionality
InitI2CGpio();
// Step 3. Clear all interrupts and initialize PIE vector table:
// Disable CPU interrupts
DINT;
// Initialize PIE control registers to their default state.
// The default state is all PIE interrupts disabled and flags
// are cleared.
// This function is found in the DSP2802x_PieCtrl.c file.
InitPieCtrl();
// Disable CPU interrupts and clear all CPU interrupt flags:
IER = 0x0000;
IFR = 0x0000;
// Initialize the PIE vector table with pointers to the shell Interrupt
// Service Routines (ISR).
// This will populate the entire table, even if the interrupt
// is not used in this example. This is useful for debug purposes.
// The shell ISR routines are found in DSP2802x_DefaultIsr.c.
// This function is found in DSP2802x_PieVect.c.
InitPieVectTable();
// Interrupts that are used in this example are re-mapped to
// ISR functions found within this file.
EALLOW; // This is needed to write to EALLOW protected registers
PieVectTable.I2CINT1A = &i2c_int1a_isr;
EDIS; // This is needed to disable write to EALLOW protected registers
// Step 4. Initialize all the Device Peripherals:
// This function is found in DSP2802x_InitPeripherals.c
// InitPeripherals(); // Not required for this example
I2CA_Init();
// Step 5. User specific code
// Clear Counters
PassCount = 0;
FailCount = 0;
// Clear incoming message buffer
for (i = 0; i < (I2C_MAX_BUFFER_SIZE - 2); i++)
{
I2cMsgIn1.MsgBuffer[i] = 0x0000;
}
// Enable interrupts required for this example
// Enable I2C interrupt 1 in the PIE: Group 8 interrupt 1
PieCtrlRegs.PIEIER8.bit.INTx1 = 1;
// Enable CPU INT8 which is connected to PIE group 8
IER |= M_INT8;
EINT;
// Application loop
for(;;)
{
//////////////////////////////////
// Write data to EEPROM section //
//////////////////////////////////
// Check the outgoing message to see if it should be sent.
// In this example it is initialized to send with a stop bit.
if(I2cMsgOut1.MsgStatus == I2C_MSGSTAT_SEND_WITHSTOP)
{
a++;
Error = I2CA_WriteData(&I2cMsgOut1);
// If communication is correctly initiated, set msg status to busy
// and update CurrentMsgPtr for the interrupt service routine.
// Otherwise, do nothing and try again next loop. Once message is
// initiated, the I2C interrupts will handle the rest. Search for
// ICINTR1A_ISR in the i2c_eeprom_isr.c file.
if (Error == I2C_SUCCESS)
{
CurrentMsgPtr = &I2cMsgOut1;
I2cMsgOut1.MsgStatus = I2C_MSGSTAT_WRITE_BUSY;
}
} // end of write section
///////////////////////////////////
// Read data from EEPROM section //
///////////////////////////////////
// Check outgoing message status. Bypass read section if status is
// not inactive.
if (I2cMsgOut1.MsgStatus == I2C_MSGSTAT_INACTIVE)
{
b++;
// Check incoming message status.
if(I2cMsgIn1.MsgStatus == I2C_MSGSTAT_SEND_NOSTOP)
{
c++;
// EEPROM address setup portion
while(I2CA_ReadData(&I2cMsgIn1) != I2C_SUCCESS)
{
d++;//未进入
// Maybe setup an attempt counter to break an infinite while
// loop. The EEPROM will send back a NACK while it is performing
// a write operation. Even though the write communique is
// complete at this point, the EEPROM could still be busy
// programming the data. Therefore, multiple attempts are
// necessary.
}
// Update current message pointer and message status
CurrentMsgPtr = &I2cMsgIn1;
I2cMsgIn1.MsgStatus = I2C_MSGSTAT_SEND_NOSTOP_BUSY;
}
// Once message has progressed past setting up the internal address
// of the EEPROM, send a restart to read the data bytes from the
// EEPROM. Complete the communique with a stop bit. MsgStatus is
// updated in the interrupt service routine.
else if(I2cMsgIn1.MsgStatus == I2C_MSGSTAT_RESTART)
{
e++;//未进入
// Read data portion
while(I2CA_ReadData(&I2cMsgIn1) != I2C_SUCCESS)
{
f++;//未进入
// Maybe setup an attempt counter to break an infinite while
// loop.
}
// Update current message pointer and message status
CurrentMsgPtr = &I2cMsgIn1;
I2cMsgIn1.MsgStatus = I2C_MSGSTAT_READ_BUSY;
}
} // end of read section
} // end of for(;;)
} // end of main
void I2CA_Init(void)
{
// Initialize I2C
I2caRegs.I2CSAR = 0x00a0; // Slave address - EEPROM control code
// I2CCLK = SYSCLK/(I2CPSC+1)
#if (CPU_FRQ_40MHZ||CPU_FRQ_50MHZ)
I2caRegs.I2CPSC.all = 4; // Prescaler - need 7-12 Mhz on module clk
#endif
#if (CPU_FRQ_60MHZ)
I2caRegs.I2CPSC.all = 6; // Prescaler - need 7-12 Mhz on module clk
#endif
I2caRegs.I2CCLKL = 10; // NOTE: must be non zero
I2caRegs.I2CCLKH = 5; // NOTE: must be non zero
I2caRegs.I2CIER.all = 0x24; // Enable SCD & ARDY interrupts
I2caRegs.I2CMDR.all = 0x0020; // Take I2C out of reset
// Stop I2C when suspended
I2caRegs.I2CFFTX.all = 0x6000; // Enable FIFO mode and TXFIFO
I2caRegs.I2CFFRX.all = 0x2040; // Enable RXFIFO, clear RXFFINT,
return;
}
Uint16 I2CA_WriteData(struct I2CMSG *msg)
{
Uint16 i;
// Wait until the STP bit is cleared from any previous master communication.
// Clearing of this bit by the module is delayed until after the SCD bit is
// set. If this bit is not checked prior to initiating a new message, the
// I2C could get confused.
if (I2caRegs.I2CMDR.bit.STP == 1)
{
return I2C_STP_NOT_READY_ERROR;
}
// Setup slave address
I2caRegs.I2CSAR = msg->SlaveAddress;
// Check if bus busy
if (I2caRegs.I2CSTR.bit.BB == 1)
{
return I2C_BUS_BUSY_ERROR;
}
// Setup number of bytes to send
// MsgBuffer + Address
I2caRegs.I2CCNT = msg->NumOfBytes+2;
// Setup data to send
I2caRegs.I2CDXR = msg->MemoryHighAddr;
I2caRegs.I2CDXR = msg->MemoryLowAddr;
// for (i=0; i<msg->NumOfBytes-2; i++)
for (i=0; i<msg->NumOfBytes; i++)
{
I2caRegs.I2CDXR = *(msg->MsgBuffer+i);
}
// Send start as master transmitter
I2caRegs.I2CMDR.all = 0x6E20;
return I2C_SUCCESS;
}
Uint16 I2CA_ReadData(struct I2CMSG *msg)
{
// Wait until the STP bit is cleared from any previous master communication.
// Clearing of this bit by the module is delayed until after the SCD bit is
// set. If this bit is not checked prior to initiating a new message, the
// I2C could get confused.
if (I2caRegs.I2CMDR.bit.STP == 1)
{
return I2C_STP_NOT_READY_ERROR;
}
I2caRegs.I2CSAR = msg->SlaveAddress;
if(msg->MsgStatus == I2C_MSGSTAT_SEND_NOSTOP)
{
// Check if bus busy
if (I2caRegs.I2CSTR.bit.BB == 1)
{
return I2C_BUS_BUSY_ERROR;
}
I2caRegs.I2CCNT = 2;
I2caRegs.I2CDXR = msg->MemoryHighAddr;
I2caRegs.I2CDXR = msg->MemoryLowAddr;
I2caRegs.I2CMDR.all = 0x2620; // Send data to setup EEPROM address
}
else if(msg->MsgStatus == I2C_MSGSTAT_RESTART)
{
I2caRegs.I2CCNT = msg->NumOfBytes; // Setup how many bytes to expect
I2caRegs.I2CMDR.all = 0x2C20; // Send restart as master receiver
}
return I2C_SUCCESS;
}
interrupt void i2c_int1a_isr(void) // I2C-A
{
Uint16 IntSource, i;
// Read interrupt source
IntSource = I2caRegs.I2CISRC.all;
// Interrupt source = stop condition detected
if(IntSource == I2C_SCD_ISRC)
{
// If completed message was writing data, reset msg to inactive state
if (CurrentMsgPtr->MsgStatus == I2C_MSGSTAT_WRITE_BUSY)
{
CurrentMsgPtr->MsgStatus = I2C_MSGSTAT_INACTIVE;
}
else
{
// If a message receives a NACK during the address setup portion of the
// EEPROM read, the code further below included in the register access ready
// interrupt source code will generate a stop condition. After the stop
// condition is received (here), set the message status to try again.
// User may want to limit the number of retries before generating an error.
if(CurrentMsgPtr->MsgStatus == I2C_MSGSTAT_SEND_NOSTOP_BUSY)
{
CurrentMsgPtr->MsgStatus = I2C_MSGSTAT_SEND_NOSTOP;
}
// If completed message was reading EEPROM data, reset msg to inactive state
// and read data from FIFO.
else if (CurrentMsgPtr->MsgStatus == I2C_MSGSTAT_READ_BUSY)
{
CurrentMsgPtr->MsgStatus = I2C_MSGSTAT_INACTIVE;
for(i=0; i < I2C_NUMBYTES; i++)
{
CurrentMsgPtr->MsgBuffer[i] = I2caRegs.I2CDRR;
}
{
// Check recieved data
for(i=0; i < I2C_NUMBYTES; i++)
{
if(I2cMsgIn1.MsgBuffer[i] == I2cMsgOut1.MsgBuffer[i])
{
PassCount++;
}
else
{
FailCount++;
}
}
if(PassCount == I2C_NUMBYTES)
{
pass();
}
else
{
fail();
}
}
}
}
} // end of stop condition detected
// Interrupt source = Register Access Ready
// This interrupt is used to determine when the EEPROM address setup portion of the
// read data communication is complete. Since no stop bit is commanded, this flag
// tells us when the message has been sent instead of the SCD flag. If a NACK is
// received, clear the NACK bit and command a stop. Otherwise, move on to the read
// data portion of the communication.
else if(IntSource == I2C_ARDY_ISRC)
{
if(I2caRegs.I2CSTR.bit.NACK == 1)
{
I2caRegs.I2CMDR.bit.STP = 1;
I2caRegs.I2CSTR.all = I2C_CLR_NACK_BIT;
}
else if(CurrentMsgPtr->MsgStatus == I2C_MSGSTAT_SEND_NOSTOP_BUSY)
{
CurrentMsgPtr->MsgStatus = I2C_MSGSTAT_RESTART;
}
} // end of register access ready
else
{
// Generate some error due to invalid interrupt source
asm(" ESTOP0");
}
// Enable future I2C (PIE Group 8) interrupts
PieCtrlRegs.PIEACK.all = PIEACK_GROUP8;
}
void pass()
{
asm(" ESTOP0");
for(;;);
}
void fail()
{
asm(" ESTOP0");
for(;;);
}
//===========================================================================
// No more.
//===========================================================================
这个是观察到的,说明没有读出数据,不知道为什么
我之前学了28027的芯片,用的都是V210里面的例程,调用库函数;今年年初开始学习28069,没有库函数可以调用,学习都是配置寄存器,之前研究了I2C,成功;后来开始学习配置28027的I2C,调用库调试了一天没成功,无奈之下按照28069的方式去配置,很多基本都是把28069的全部复制进去,成功。附件里面的例程就是我配置的例程,希望你可以借鉴看看