22#include "px_rtc_m41t00.h"
33 uint8_t century_hours;
49#define PX_M41T00_ST_ADR 0x00
50#define PX_M41T00_ST_BIT (1 << 7)
53#define PX_M41T00_CEB_ADR 0x02
54#define PX_M41T00_CEB_BIT (1 << 7)
57#define PX_M41T00_CB_ADR 0x02
58#define PX_M41T00_CB_BIT (1 << 6)
63#define PX_M41T00_OUT_BIT (1 << 7)
66#define PX_M41T00_FT_BIT (1 << 6)
69#define PX_M41T00_S_BIT (1 << 5)
72#define PX_M41T00_CAL_MASK 0x1F
79static px_i2c_handle * px_m41t00_i2c_handle;
85static uint8_t px_m41t00_bcd_to_byte(uint8_t bcd_val)
87 return (bcd_val & 0xf) + ((bcd_val & 0xf0) >> 4) * 10;
90static uint8_t px_m41t00_byte_to_bcd(uint8_t byte_val)
92 return (byte_val % 10) + ((byte_val / 10) << 4);
95static bool px_m41t00_rd_data(
void)
98 px_m41t00_data.adr = 0;
102 PX_LOG_E(
"Unable to write address");
107 &px_m41t00_data.bcd_time,
108 sizeof(px_m41t00_data.bcd_time) +
sizeof(px_m41t00_data.control),
118static bool px_m41t00_wr_data(
void)
121 px_m41t00_data.adr = 0;
125 sizeof(px_m41t00_data),
138 px_m41t00_i2c_handle = px_i2c_handle;
146 px_rtc_time->
sec = 0;
147 px_rtc_time->
min = 0;
148 px_rtc_time->
hour = 0;
151 px_rtc_time->
month = 1;
152 px_rtc_time->
year = 7;
154 if(!px_m41t00_rd_data())
159 if(((uint8_t *)(&px_m41t00_data.bcd_time))[PX_M41T00_ST_ADR] & PX_M41T00_ST_BIT)
165 ((uint8_t *)(&px_m41t00_data.bcd_time))[PX_M41T00_CEB_ADR] &= ~PX_M41T00_CEB_BIT;
166 ((uint8_t *)(&px_m41t00_data.bcd_time))[PX_M41T00_CB_ADR] &= ~PX_M41T00_CB_BIT;
168 for(i = 0; i <
sizeof(*px_rtc_time); i++)
170 ((uint8_t *)px_rtc_time)[i] = px_m41t00_bcd_to_byte(((uint8_t *)(&px_m41t00_data.bcd_time))[i]);
173 if((px_rtc_time->
month < 1 ) || (px_rtc_time->
month > 12))
return false;
175 if(px_rtc_time->
hour > 23)
return false;
176 if(px_rtc_time->
min > 59)
return false;
177 if(px_rtc_time->
sec > 59)
return false;
187 for(i = 0; i <
sizeof(px_m41t00_data.bcd_time); i++)
189 ((uint8_t *)(&px_m41t00_data.bcd_time))[i] = px_m41t00_byte_to_bcd(((uint8_t *)px_rtc_time)[i]);
193 ((uint8_t *)(&px_m41t00_data.bcd_time))[PX_M41T00_ST_ADR] &= ~PX_M41T00_ST_BIT;
194 ((uint8_t *)(&px_m41t00_data.bcd_time))[PX_M41T00_CEB_ADR] &= ~PX_M41T00_CEB_BIT;
195 ((uint8_t *)(&px_m41t00_data.bcd_time))[PX_M41T00_CB_ADR] &= ~PX_M41T00_CB_BIT;
197 px_m41t00_data.control = 0x00;
199 return px_m41t00_wr_data();
#define PX_LOG_NAME(name)
Macro to declare a log name string once for each file to reduce code size.
#define PX_LOG_E(format,...)
Macro to display a formatted ERROR message.
uint8_t sec
Seconds: 0 to 59.
uint8_t year
Years: 00 to 99.
uint8_t hour
Hours: 0 to 23.
uint8_t day_of_month
Days of Month: 1 to 31 (depending on month)
uint8_t min
Minutes: 0 to 59.
uint8_t month
Months: 1 to 12.
uint8_t day_of_week
Day of Week: 1 to 7.
bool px_m41t00_get_time(px_m41t00_time_t *px_rtc_time)
Get RTC time using I2C driver.
bool px_m41t00_set_time(const px_m41t00_time_t *px_rtc_time)
Set RTC time using I2C driver.
void px_m41t00_init(px_i2c_handle_t *px_i2c_handle)
Initialise driver.
Structure to store time and date.
#define PX_I2C_FLAG_START_AND_STOP
Begin and finish an I2C transaction with a START and STOP condition.
bool px_i2c_rd(px_i2c_handle_t *handle, void *data, size_t nr_of_bytes, uint8_t flags)
Perform an I2C read transaction with an I2C slave.
#define PX_I2C_FLAG_REP_START_AND_STOP
Begin and finish an I2C transaction with a REP START and STOP condition.
#define PX_I2C_FLAG_START_AND_END
Begin and finish an I2C transaction with a START and END condition.
bool px_i2c_wr(px_i2c_handle_t *handle, const void *data, size_t nr_of_bytes, uint8_t flags)
Perform an I2C write transaction with an I2C slave.
Define I2C handle for a slave.