Clock working, I2C communication to be proven

This commit is contained in:
StefansE
2026-03-26 19:26:58 +01:00
parent 6b3f588fb0
commit ba8855eb21
6 changed files with 153 additions and 21 deletions

View File

@@ -65,10 +65,16 @@ static ds1307_err_t ds1307_write_byte(uint8_t ds1307_reg_addres, uint8_t data){
*/
static uint8_t ds1307_read_byte(uint8_t ds1307_reg_addres){
HAL_StatusTypeDef status = HAL_ERROR;
uint8_t data;
HAL_I2C_Master_Transmit(&DS1307_HANDLER, DS1307_ADDRES << 1, &ds1307_reg_addres, 1, DS1307_TIMEOUT);
HAL_I2C_Master_Receive(&DS1307_HANDLER, DS1307_ADDRES << 1, &data, 1, DS1307_TIMEOUT);
return data;
status = HAL_I2C_Master_Transmit(&DS1307_HANDLER, DS1307_ADDRES << 1, &ds1307_reg_addres, 1, DS1307_TIMEOUT);
status = HAL_I2C_Master_Receive(&DS1307_HANDLER, DS1307_ADDRES << 1, &data, 1, DS1307_TIMEOUT);
if(HAL_OK == status){
return data;
}
else{
return 255;
}
}
/**
* @brief BCD decode
@@ -113,7 +119,10 @@ void ds1307_set_clock_halt(uint8_t halt){
* @return uint8_t
*/
uint8_t ds1307_get_clock_halt(void){
return (ds1307_read_byte(DS1307_SECONDS) & 0x80) >> 7;
uint8_t read = ds1307_read_byte(DS1307_SECONDS);
if(read != 255){
return (read & 0x80) >> 7;
}
}
/**
@@ -150,7 +159,13 @@ void ds1307_set_second(uint8_t second){
* @return uint8_t
*/
uint8_t ds1307_get_second(void){
return ds1307_bcd_decode(ds1307_read_byte(DS1307_SECONDS) & 0x7F);
uint8_t decode = ds1307_read_byte(DS1307_SECONDS);
if(decode != 255){
return ds1307_bcd_decode(decode & 0x7F);
}
else{
return 255;
}
}
/**
* @brief ds1307_set_minutes
@@ -273,9 +288,17 @@ int8_t ds1307_get_time_zone_min(void){
* @param dev: ds1307 pointer
*/
void ds1307_update(ds1307_dev_t *ds1307_dev){
uint8_t val = 0;
ds1307_dev->seconds = ds1307_get_second();
ds1307_dev->minutes = ds1307_get_minutes();
val = ds1307_get_second();
if(val != 255){
ds1307_dev->seconds = val;
}
val = ds1307_get_minutes();
if(val != 255){
ds1307_dev->minutes = val;
}
ds1307_dev->hours = ds1307_get_hour();
ds1307_dev->day = ds1307_get_day();
ds1307_dev->date = ds1307_get_date();