add error checking and return code
This commit is contained in:
parent
ba982e17c7
commit
1dbfbe3841
|
@ -45,7 +45,9 @@ bool rtc_ds1307_oscillator_disabled(void)
|
|||
{
|
||||
uint8_t data[1] = {0}; // to read data over I2C
|
||||
const uint8_t address[] = {0x00}; // memory address for data
|
||||
i2c_read(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, LENGTH(data)); // read a single byte containing CH value
|
||||
if (!i2c_read(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, LENGTH(data))) { // read a single byte containing CH value
|
||||
return false;
|
||||
}
|
||||
return data[0]&0x80; // return CH bit value to indicate if oscillator is disabled
|
||||
}
|
||||
|
||||
|
@ -55,7 +57,9 @@ uint16_t rtc_ds1307_read_square_wave(void)
|
|||
uint8_t data[1] = {0}; // to read data over I2C
|
||||
const uint16_t rtc_ds1307_rs[] = {1, 4096, 8192, 32768}; // RS1/RS0 values
|
||||
const uint8_t address[] = {0x07}; // memory address for data
|
||||
i2c_read(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, LENGTH(data)); // read a single byte containing control register
|
||||
if (!i2c_read(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, LENGTH(data))) { // read a single byte containing control register
|
||||
return 0xffff; // error occurred
|
||||
}
|
||||
if (data[0]&0x10) { // verify if the square wave is enabled (SQWE)
|
||||
to_return = rtc_ds1307_rs[data[0]&0x03]; // read RS1/RS0 and get value
|
||||
} else {
|
||||
|
@ -69,7 +73,9 @@ uint8_t rtc_ds1307_read_seconds(void)
|
|||
uint8_t to_return = 0; // seconds to return
|
||||
uint8_t data[1] = {0}; // to read data over I2C
|
||||
const uint8_t address[] = {0x00}; // memory address for data
|
||||
i2c_read(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, LENGTH(data)); // read a single byte containing seconds value
|
||||
if (!i2c_read(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, LENGTH(data))) { // read a single byte containing value
|
||||
return 0xff;
|
||||
}
|
||||
to_return = ((data[0]&0x70)>>4)*10+(data[0]&0x0f); // convert BCD coding into seconds
|
||||
return to_return;
|
||||
}
|
||||
|
@ -79,7 +85,9 @@ uint8_t rtc_ds1307_read_minutes(void)
|
|||
uint8_t to_return = 0; // minutes to return
|
||||
uint8_t data[1] = {0}; // to read data over I2C
|
||||
const uint8_t address[] = {0x01}; // memory address for data
|
||||
i2c_read(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, LENGTH(data)); // read a single byte containing minutes value
|
||||
if (!i2c_read(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, LENGTH(data))) { // read a single byte containing value
|
||||
return 0xff;
|
||||
}
|
||||
to_return = (data[0]>>4)*10+(data[0]&0x0f); // convert BCD coding into minutes
|
||||
return to_return;
|
||||
}
|
||||
|
@ -89,7 +97,9 @@ uint8_t rtc_ds1307_read_hours(void)
|
|||
uint8_t to_return = 0; // hours to return
|
||||
uint8_t data[1] = {0}; // to read data over I2C
|
||||
const uint8_t address[] = {0x02}; // memory address for data
|
||||
i2c_read(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, LENGTH(data)); // read a single byte containing hours value
|
||||
if (!i2c_read(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, LENGTH(data))) { // read a single byte containing value
|
||||
return 0xff;
|
||||
}
|
||||
if (data[0]&0x40) { // 12 hour mode
|
||||
if (data[0]&0x02) { // PM
|
||||
to_return += 12; // add the 12 hours
|
||||
|
@ -107,7 +117,9 @@ uint8_t rtc_ds1307_read_day(void)
|
|||
uint8_t to_return = 0; // day to return
|
||||
uint8_t data[1] = {0}; // to read data over I2C
|
||||
const uint8_t address[] = {0x03}; // memory address for data
|
||||
i2c_read(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, LENGTH(data)); // read a single byte containing day value
|
||||
if (!i2c_read(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, LENGTH(data))) { // read a single byte containing value
|
||||
return 0xff;
|
||||
}
|
||||
to_return = (data[0]&0x07); // convert BCD coding into days
|
||||
return to_return;
|
||||
}
|
||||
|
@ -117,7 +129,9 @@ uint8_t rtc_ds1307_read_date(void)
|
|||
uint8_t to_return = 0; // date to return
|
||||
uint8_t data[1] = {0}; // to read data over I2C
|
||||
const uint8_t address[] = {0x04}; // memory address for data
|
||||
i2c_read(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, LENGTH(data)); // read a single byte containing date value
|
||||
if (!i2c_read(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, LENGTH(data))) { // read a single byte containing value
|
||||
return 0xff;
|
||||
}
|
||||
to_return = ((data[0]&0x30)>>4)*10+(data[0]&0x0f); // convert BCD coding into date
|
||||
return to_return;
|
||||
}
|
||||
|
@ -127,7 +141,9 @@ uint8_t rtc_ds1307_read_month(void)
|
|||
uint8_t to_return = 0; // month to return
|
||||
uint8_t data[1] = {0}; // to read data over I2C
|
||||
const uint8_t address[] = {0x05}; // memory address for data
|
||||
i2c_read(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, LENGTH(data)); // read a single byte containing month value
|
||||
if (!i2c_read(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, LENGTH(data))) { // read a single byte containing value
|
||||
return 0xff;
|
||||
}
|
||||
to_return = ((data[0]&0x10)>>4)*10+(data[0]&0x0f); // convert BCD coding into month
|
||||
return to_return;
|
||||
}
|
||||
|
@ -136,7 +152,9 @@ uint8_t rtc_ds1307_read_year(void)
|
|||
{
|
||||
uint8_t data[1] = {0}; // to read data over I2C
|
||||
const uint8_t address[] = {0x06}; // memory address for data
|
||||
i2c_read(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, LENGTH(data)); // read a single byte containing year value
|
||||
if (!i2c_read(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, LENGTH(data))) { // read a single byte containing value
|
||||
return 0xff;
|
||||
}
|
||||
uint8_t to_return = ((data[0]&0xf0)>>4)*10+(data[0]&0x0f); // convert BCD coding into year
|
||||
return to_return;
|
||||
}
|
||||
|
@ -147,7 +165,7 @@ uint8_t* rtc_ds1307_read_time(void)
|
|||
uint8_t data[7] = {0}; // to read data over I2C
|
||||
const uint8_t address[] = {0x00}; // memory address for data
|
||||
if (!i2c_read(RTC_DS1307_I2C_ADDR, address, LENGTH(address), data, LENGTH(data))) { // read all time bytes
|
||||
return NULL;
|
||||
return NULL; // error occurred
|
||||
}
|
||||
time[0] = ((data[0]&0x70)>>4)*10+(data[0]&0x0f); // convert seconds from BCD
|
||||
time[1] = (data[1]>>4)*10+(data[1]&0x0f); // convert minutes from BCD
|
||||
|
|
|
@ -25,43 +25,43 @@
|
|||
*/
|
||||
void rtc_ds1307_setup(void);
|
||||
/** verify if oscillator is disabled
|
||||
* @return if oscillator is disabled
|
||||
* @return if oscillator is disabled (or if communication error occurred)
|
||||
*/
|
||||
bool rtc_ds1307_oscillator_disabled(void);
|
||||
/** read square wave output frequency (in Hz)
|
||||
* @return square wave output frequency in Hz, 0 if disabled
|
||||
* @return square wave output frequency in Hz, 0 if disabled (0xffff if communication error occurred)
|
||||
*/
|
||||
uint16_t rtc_ds1307_read_square_wave(void);
|
||||
/** read seconds from RTC IC
|
||||
* @return number of seconds (0-59) of the current time
|
||||
* @return number of seconds (0-59) of the current time (0xff if communication error occurred)
|
||||
*/
|
||||
uint8_t rtc_ds1307_read_seconds(void);
|
||||
/** read minutes from RTC IC
|
||||
* @return number of minutes (0-59) of the current time
|
||||
* @return number of minutes (0-59) of the current time (0xff if communication error occurred)
|
||||
*/
|
||||
uint8_t rtc_ds1307_read_minutes(void);
|
||||
/** read hours from RTC IC
|
||||
* @return number of hours (0-23) of the current time
|
||||
* @return number of hours (0-23) of the current time (0xff if communication error occurred)
|
||||
*/
|
||||
uint8_t rtc_ds1307_read_hours(void);
|
||||
/** read day from RTC IC
|
||||
* @return day of the week (1-7, 1 is Sunday) of the current time, 1 being Sunday
|
||||
* @return day of the week (1-7, 1 is Sunday) of the current time, 1 being Sunday (0xff if communication error occurred)
|
||||
*/
|
||||
uint8_t rtc_ds1307_read_day(void);
|
||||
/** read date from RTC IC
|
||||
* @return day of the month (1-31) of the current time
|
||||
* @return day of the month (1-31) of the current time (0xff if communication error occurred)
|
||||
*/
|
||||
uint8_t rtc_ds1307_read_date(void);
|
||||
/** read month from RTC IC
|
||||
* @return month of the year (1-12) of the current time
|
||||
* @return month of the year (1-12) of the current time (0xff if communication error occurred)
|
||||
*/
|
||||
uint8_t rtc_ds1307_read_month(void);
|
||||
/** read year from RTC IC
|
||||
* @return year of the century (00-99) of the current time
|
||||
* @return year of the century (00-99) of the current time (0xff if communication error occurred)
|
||||
*/
|
||||
uint8_t rtc_ds1307_read_year(void);
|
||||
/** read time from RTC IC
|
||||
* @return array of {seconds, minutes, hours, day, date, month, year} as defined above
|
||||
* @return array of {seconds, minutes, hours, day, date, month, year} as defined above (NULL if communication error occurred)
|
||||
*/
|
||||
uint8_t* rtc_ds1307_read_time(void);
|
||||
/** read user RAM from RTC IC
|
||||
|
|
Loading…
Reference in New Issue