AnsweredAssumed Answered

BF609 TWI(I2C) C code sample for beginners?

Question asked by LeeSeokMin on Oct 11, 2015
Latest reply on Oct 19, 2015 by SachinV

Good day Everyone!

I'm studying dsp in south korea.

My question is twi(i2c) c code sample.

 

Will this registry value, as the attached picture right?

When the write fifostat_txdata in TXDATA16 to normal operation

When the read RXDATA16 is a fifostat_rxdata do not work

What should I do if modify the program?

 

I program is created with reference:

Define Device_Addr  0xDC>>1


void config(void){

Write(0x400,0x0001,2);
TWI_Clear();
Read(0x400,1);

 

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

void TWI_Clear(void){

*pREG_TWI1_CTL = 0;

*pREG_TWI1_CLKDIV = 0;

*pREG_TWI1_MSTRCTL = 0;

*pREG_TWI1_FIFOCTL = BITM_TWI_FIFOCTL_RXFLUSH | BITM_TWI_FIFOCTL_TXFLUSH;

*pREG_TWI1_MSTRADDR = 0;

*pREG_TWI1_ISTAT = 0;

}

 

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

void TWI_Init(void){

*pREG_TWI1_CTL = 0;

*pREG_TWI1_CLKDIV = ((CLKDIV_HI)<<8) | (CLKDIV_LO);

*pREG_TWI1_CTL = Prescale_value | BITM_TWI_CTL_EN;

}

 

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

void Write(unsigned short addr, unsigned short data, int count){//count 1

TWI_Init();

*pREG_TWI1_MSTRCTL = 0;

*pREG_TWI1_FIFOCTL = BITM_TWI_FIFOCTL_RXFLUSH | BITM_TWI_FIFOCTL_TXFLUSH ;
*pREG_TWI1_FIFOCTL = 0;
*pREG_TWI1_FIFOCTL = BITM_TWI_FIFOCTL_RXILEN | BITM_TWI_FIFOCTL_TXILEN;
//write address
*pREG_TWI1_MSTRADDR = Device_Addr;

//data must be available when turning TWI on
*pREG_TWI1_TXDATA16 = addr;

//enable tx transfer
*pREG_TWI1_MSTRCTL = ENUM_TWI_MSTRCTL_EN | (count << 6) | ENUM_TWI_MSTRCTL_TX | ENUM_TWI_MSTRCTL_RPT;
while (*pREG_TWI1_FIFOSTAT & BITP_TWI_FIFOSTAT_TXSTAT) {};

//send all bytes
*pREG_TWI1_TXDATA16 = data;
while (*pREG_TWI1_FIFOSTAT & BITP_TWI_FIFOSTAT_TXSTAT) {};


//wait for TWI master transfer to complete
while (*pREG_TWI1_MSTRSTAT & ENUM_TWI_MSTRSTAT_MPROG_YES) {};
}

 

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

void Read(unsigned short addr, int count){
unsigned short data;

TWI_Init();

*pREG_TWI1_MSTRCTL = 0;

*pREG_TWI1_FIFOCTL = BITM_TWI_FIFOCTL_RXFLUSH | BITM_TWI_FIFOCTL_TXFLUSH ;
*pREG_TWI1_FIFOCTL = 0;
*pREG_TWI1_FIFOCTL = BITM_TWI_FIFOCTL_RXILEN | BITM_TWI_FIFOCTL_TXILEN;

//write address
*pREG_TWI1_MSTRADDR = Device_Addr;

//data must be available when turning twi on
*pREG_TWI1_TXDATA16 = addr;

//enable tx transfer
*pREG_TWI1_MSTRCTL = ENUM_TWI_MSTRCTL_EN | (count << 6) | ENUM_TWI_MSTRCTL_TX | ENUM_TWI_MSTRCTL_RPT;
while (*pREG_TWI1_FIFOSTAT & BITP_TWI_FIFOSTAT_TXSTAT) {};

//enable rx transfer
*pREG_TWI1_MSTRCTL = ENUM_TWI_MSTRCTL_EN | (count<<6) | ENUM_TWI_MSTRCTL_RX;

//receive all bytes
data = *pREG_TWI1_RXDATA16;
printf("read data %x", data);
while(*pREG_TWI1_FIFOSTAT & BITM_TWI_FIFOSTAT_RXSTAT) {};
}

 

write.pngread.png

Attachments

Outcomes