According to datasheet there is not much to send. I have sent some values to register address 0x00 and read back from this address.
#define PORT_USED 0
void open_ds3231(void){
I2C0Init();
}
uint32_t rtc_set_time(uint8_t hour,uint8_t min,uint8_t sec)
{
uint32_t state;
/* Write SLA(W), address and one data byte */
I2CWriteLength[PORT_USED] = 5;
I2CReadLength[PORT_USED] = 0;
I2CMasterBuffer[PORT_USED][0] = DS3231_ADDR_WRITE; // 0xD0
I2CMasterBuffer[PORT_USED][1] = 0x00; // Second register address
I2CMasterBuffer[PORT_USED][2] = bin2bcd(sec);
I2CMasterBuffer[PORT_USED][3] = bin2bcd(min);
I2CMasterBuffer[PORT_USED][4] = bin2bcd(hour);
state = I2CEngine( PORT_USED );
return state;
}
uint32_t rtc_get_time(uint8_t *hour,uint8_t *min,uint8_t *sec)
{
uint32_t state;
/* Write SLA(W), address and one data byte */
I2CWriteLength[PORT_USED] = 2;
I2CReadLength[PORT_USED] = 3;
I2CMasterBuffer[PORT_USED][0] = DS3231_ADDR_READ; // 0xD1
I2CMasterBuffer[PORT_USED][1] = 0x00;
state = I2CEngine( PORT_USED );
*sec=bcd2bin(I2CSlaveBuffer[PORT_USED][0]);
*min=bcd2bin(I2CSlaveBuffer[PORT_USED][1]);
*hour=bcd2bin(I2CSlaveBuffer[PORT_USED][2]);
return state;
}
There is one thing that I noticed. By setting some breakpoints in i2c.c file in I2C0_IRQHandler() routine I found out it goes to this case after sending the first byte :
case 0x38: /* Arbitration lost, in this example, we don't deal with multiple master situation */
default:
I2CMasterState[0] = I2C_ARBITRATION_LOST;
LPC_I2C0->CONCLR = I2CONCLR_SIC;
break;