2018-04-24 04:58 AM
Hi,
I have for me big problem with I2C. I would like communicate with DS3231 RTC chip.
I wroted small easy code :
void DS3231_DateTime_get(void)
{ DS3231.ds3231_TX_buff[0] = 0x00; HAL_I2C_Master_Transmit(&hi2c1, DS3231_address << 1, &DS3231.ds3231_TX_buff[0], 1, 1000); HAL_I2C_Master_Receive(&hi2c1, DS3231_address << 1, &DS3231.ds3231_RX_buff[0], 7, 1000);//HAL_I2C_Mem_Read(&hi2c1, DS3231_address << 1, 0x00, I2C_MEMADD_SIZE_8BIT, DS3231.ds3231_RX_buff, 7, 1000);
DS3231.second = BCD2DEC(DS3231.ds3231_RX_buff[0]); DS3231.minute = BCD2DEC(DS3231.ds3231_RX_buff[1]); DS3231.hour = BCD2DEC(DS3231.ds3231_RX_buff[2]); DS3231.day = BCD2DEC(DS3231.ds3231_RX_buff[3]); DS3231.date = BCD2DEC(DS3231.ds3231_RX_buff[4]); DS3231.month = BCD2DEC(DS3231.ds3231_RX_buff[5]); DS3231.year = BCD2DEC(DS3231.ds3231_TX_buff[6]);}
In first cycle I read data good (data is visualized with VIsual GDB in Visual studio in debugging mode, but I tried with stmstudio or send over uart equal is same ) - data is hexadecimal
after first cycle in following cycles I read every register 0x00...
after restart procvesor first cycle to same good every following 0x00.
I use CubeMX 4.25
STM32L151RDT6
FW 1.8.1
I tried event interrupt enable.
please help.
thank you
#stm32l1 #i2c-analysis #i2c-setup #cubemx-i2c #register2018-04-24 07:49 AM
It works, now work out how you broke it...
don't recheck so quickly ??
step it out, check where the buffer is zero'd and where it is failing...
but it works... try to work out how to not break it...
2018-04-24 10:33 AM
uint8_t ds3231_read_reg(uint16_t register_address)
{uint8_t result[1] = {0};
if (HAL_I2C_Mem_Read_DMA(&hi2c1, (uint16_t)(DS3231_ADDRESS<<1), (uint16_t)register_address, I2C_MEMADD_SIZE_8BIT, (uint8_t*)(&result),1) != HAL_OK)
{ Error_Handler(); } while (HAL_I2C_GetState(&hi2c1) != HAL_I2C_STATE_READY) { }return result[0];
}void ds3231_write_reg(uint16_t register_address, uint8_t value){if (HAL_I2C_Mem_Write_DMA(&hi2c1, (uint16_t)(DS3231_ADDRESS<<1), (uint16_t)register_address, I2C_MEMADD_SIZE_8BIT, (uint8_t*)(&value), 1) != HAL_OK)
{ Error_Handler(); } while (HAL_I2C_GetState(&hi2c1) != HAL_I2C_STATE_READY) { } while (HAL_I2C_IsDeviceReady(&hi2c1, (uint16_t)(DS3231_ADDRESS<<1), 10, 300) == HAL_TIMEOUT); while (HAL_I2C_GetState(&hi2c1) != HAL_I2C_STATE_READY) { }}