Hi ,
Please find below TI example code for I2C communication
/* USER CODE BEGIN (0) */
/* USER CODE END */
/* Include Files */
#include "HL_sys_common.h"
/* USER CODE BEGIN (1) */
#include "HL_i2c.h"
/* USER CODE END */
/* USER CODE BEGIN (2) */
#define DATA_COUNT 10
#define Master_Address 0x26
#define Slave_Address 0x8
#define Slave_Word_address 0x00
#define Receive_data_setup 0x1 // 1 Word address
uint8_t TX_Data_Master[DATA_COUNT] = {0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19};
uint8_t RX_Data_Master[DATA_COUNT] = { 0 };
uint8_t TX_Data_Slave[10] = { 0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29};
uint8_t RX_Data_Slave[10] = { 0 };
#define PCF8570_ADDRESS 0x57 // All A0,A1,A2 are tied to 3.3V in PCF8570
uint32_t temp1, temp2;
/* USER CODE END */
void main(void)
{
/* USER CODE BEGIN (3) */
int repeat = 0; int delay =0;
/* I2C Init as per GUI
* Mode = Master - Transmitter
* baud rate = 100KHz
* Count = 10
* Bit Count = 8bit
*/
i2cInit();
// Master Transfer Functionality //
/* Configure address of Slave to talk to */
i2cSetSlaveAdd(i2cREG1, PCF8570_ADDRESS);
/* Set direction to Transmitter */
/* Note: Optional - It is done in Init */
i2cSetDirection(i2cREG1, I2C_TRANSMITTER);
/* Configure Data count */
/* Data Count + 1 ( Word Address) */
i2cSetCount(i2cREG1, DATA_COUNT + 1);
/* Set mode as Master */
i2cSetMode(i2cREG1, I2C_MASTER);
/* Set Stop after programmed Count */
i2cSetStop(i2cREG1);
/* Transmit Start Condition */
i2cSetStart(i2cREG1);
/* Send the Word Address */
i2cSendByte(i2cREG1, Slave_Word_address);
/* Tranmit DATA_COUNT number of data in Polling mode */
i2cSend(i2cREG1, DATA_COUNT, TX_Data_Master);
/* Wait until Bus Busy is cleared */
while(i2cIsBusBusy(i2cREG1) == true);
/* Wait until Stop is detected */
while(i2cIsStopDetected(i2cREG1) == 0);
/* Clear the Stop condition */
i2cClearSCD(i2cREG1);
/* Simple Dealya before starting Next Block */
/* Depends on how quick the Slave gets ready */
for(delay=0;delay<100000;delay++);
// Master Receive Functionality //
/*****************************************/
/*****************************************/
/* wait until MST bit gets cleared, this takes
* few cycles after Bus Busy is cleared */
while(i2cIsMasterReady(i2cREG1) != true);
/* Configure address of Slave to talk to */
i2cSetSlaveAdd(i2cREG1, PCF8570_ADDRESS);
/* Set direction to Transmitter */
/* Note: Optional - It is done in Init */
i2cSetDirection(i2cREG1, I2C_TRANSMITTER);
/* Configure Data count */
/* Slave address + Word address write operation before reading */
i2cSetCount(i2cREG1, Receive_data_setup);
/* Set mode as Master */
i2cSetMode(i2cREG1, I2C_MASTER);
/* Set Stop after programmed Count */
i2cSetStop(i2cREG1);
/* Transmit Start Condition */
i2cSetStart(i2cREG1);
/* Send the Word Address */
i2cSendByte(i2cREG1, Slave_Word_address);
/* Wait until Bus Busy is cleared */
while(i2cIsBusBusy(i2cREG1) == true);
/* Wait until Stop is detected */
while(i2cIsStopDetected(i2cREG1) == 0);
/* Clear the Stop condition */
i2cClearSCD(i2cREG1);
/*****************************************/
/*****************************************/
/* wait until MST bit gets cleared, this takes
* few cycles after Bus Busy is cleared */
while(i2cIsMasterReady(i2cREG1) != true);
/* Configure address of Slave to talk to */
i2cSetSlaveAdd(i2cREG1, PCF8570_ADDRESS);
/* Set direction to receiver */
i2cSetDirection(i2cREG1, I2C_RECEIVER);
/* Configure Data count */
/* Note: Optional - It is done in Init, unless user want to change */
i2cSetCount(i2cREG1, DATA_COUNT);
/* Set mode as Master */
i2cSetMode(i2cREG1, I2C_MASTER);
/* Set Stop after programmed Count */
i2cSetStop(i2cREG1);
/* Transmit Start Condition */
i2cSetStart(i2cREG1);
/* Tranmit DATA_COUNT number of data in Polling mode */
i2cReceive(i2cREG1, DATA_COUNT, RX_Data_Master);
/* Wait until Bus Busy is cleared */
while(i2cIsBusBusy(i2cREG1) == true);
/* Wait until Stop is detected */
while(i2cIsStopDetected(i2cREG1) == 0);
/* Clear the Stop condition */
i2cClearSCD(i2cREG1);
asm(" nop");
asm(" nop");
asm(" nop");
while(1);
/* USER CODE END */
}
/* USER CODE BEGIN (4) */
/* USER CODE END */
--------------------------------------------------------------------------------
In this code ,
Question 1:
/* Transmit Start Condition */
i2cSetStart(i2cREG1);
Is above API i2cSetStart sending start condition?
Question 2:
/* Configure address of Slave to talk to */
i2cSetSlaveAdd(i2cREG1, PCF8570_ADDRESS);
is API i2cSetSlaveAdd just configuring slave address or configuring and transmitting slave address?
Question 3:
/* Set direction to Transmitter */
/* Note: Optional - It is done in Init */
i2cSetDirection(i2cREG1, I2C_TRANSMITTER);
Is i2cSetDirection API just configuring Read/Write bit or transmitting Read/Write bit?
Question 4:
As per I2C protocol , Communication sequence is
1) start condition
2) send slave address
3) send R/W control bit
4) Data bytes
.right?
how above code is following this communication sequence?
I need to write and read date and time from RTC chip with below sequence
Write mode :
1) Master sends out the “Start Condition”
2) Master sends out the “Slave Address”, ACh for the RV-3129-C3; the R/W bit = “0” for write mode
3) Acknowledgement from the RV-3129-C3
4) Master sends out the “Page & Word Address” to the RV-3129-C3
5) Acknowledgement from the RV-3129-C3
6) Master sends out the “Data” to write to the address specified in step 4)
7) Acknowledgement from the RV-3129-C3
8) Steps 6) and 7) can be repeated if necessary. Within the same Memory Page, the RV-3129-C3 will
increment the word address automatically
9) Master sends out the “Stop Condition”
Read mode :
1) Master sends out the “Start condition”
2) Master sends out the “Slave Address”, ACh for the RV-3129-C3; the R/W bit = “0” for write mode
3) Acknowledgement from the RV-3129-C3
4) Master sends out the “Page & Word Address” to the RV-3129-C3
5) Acknowledgement from the RV-3129-C3
6) Master sends out the “Stop Condition”
7) Master sends out the “Start Condition”
8) Master sends out the “Slave Address”, ADh for the RV-3129-C3; the R/W bit =”1” for read mode
9) Acknowledgement from the RV-3129-C3:
At this point, the Master becomes a Receiver, the Slave becomes the Transmitter
10) The RV-3129-C3 sends out the “Data” from the “Page & Word Address” specified in step 4)
11) Acknowledgement from the Master:
At this time, the “Page & Word” Address will be automatically incremented by 1
12) Steps 10) and 11) can be repeated if necessary. Within the same Page Address, the Word Address will
be incremented automatically
13) The Master, addressed as Receiver, can stop data transmission by not generating an acknowledge on
the last byte that has been sent from the Slave Transmitter. In this event, the Slave-Transmitter must
leave the data line HIGH to enable the Master to generate a “Stop condition”
14) Master sends out the “Stop Condition”
Do you have sample code for MS570LC4357 interfacing with RTC RV-3129-C3 for setting date and time and reading them back?