/* * PCF8523 slightly changed its 7 bytes encoded in BCD: * 03h - Seconds - 00-59 * 04h - Minutes - 00-59 * 05h - Hours - 00-23 * 06h - monthday - 01-31 * 07h - weekday - 00-06 * 08h - Month - 01-12 * 09h - Year - 00-99 * */ time_t rtc_readValue() { i2c_cmd_handle_t cmd = i2c_cmd_link_create(); ESP_ERROR_CHECK(i2c_master_start(cmd)); ESP_ERROR_CHECK(i2c_master_write_byte(cmd, (RTC_ADDRESS << 1) | I2C_MASTER_WRITE, true /* expect ack */)); ESP_ERROR_CHECK(i2c_master_write_byte(cmd, 0x03, 1)); // start address ESP_ERROR_CHECK(i2c_master_start(cmd)); ESP_ERROR_CHECK(i2c_master_write_byte(cmd, (RTC_ADDRESS << 1) | I2C_MASTER_READ, true /* expect ack */)); uint8_t data[7]; ESP_ERROR_CHECK(i2c_master_read(cmd, data, 7, false)); ESP_ERROR_CHECK(i2c_master_stop(cmd)); COMMANDCHECKOKERR(i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000/portTICK_PERIOD_MS),"RTC COMMAND"); i2c_cmd_link_delete(cmd); int i; for (i=0; i<7; i++) { ESP_LOGD(tag, "%d: 0x%.2x", i, data[i]); } struct tm tm; tm.tm_sec = bcdToInt(data[0]); tm.tm_min = bcdToInt(data[1]); tm.tm_hour = bcdToInt(data[2]); tm.tm_mday = bcdToInt(data[3]); tm.tm_mon = bcdToInt(data[5]) - 1; // 0-11 - Note: The month on the PCF8523 is 1-12. tm.tm_year = bcdToInt(data[6]) + 100; // Years since 1900 time_t readTime = mktime(&tm); return readTime; }
uint8_t DS3231::getTimeYear() { prepRead(TIMEYEAR, 1); return bcdToInt(Wire.read()); }
uint8_t DS3231::getTimeDate() { prepRead(TIMEDATE, 1); return bcdToInt(Wire.read()); }
uint8_t DS3231::getTimeMonth() { prepRead(TIMECENTURYMONTH, 1); // century bit is tied into month and should be ignored if set. return Wire.peek() & 0x80 ? bcdToInt(Wire.read() & ~0x80) : bcdToInt(Wire.read()); }
uint8_t DS3231::getTimeDayOfTheWeek() { prepRead(TIMEDAY, 1); return bcdToInt(Wire.read()); }
bool DS3231::getTimeHoursIsPM() { prepRead(TIMEHOUR, 1); // if 12 hour bit 0x40 is set then return 0x20 AM/PM bit, else calculate AM/PM based off 24 hour clock return Wire.peek() & 0x40 ? Wire.read() & 0x20 : bcdToInt(Wire.read()) > 12 ? true : false; }
uint8_t DS3231::getTimeHours() { prepRead(TIMEHOUR, 1); // if 12 hour clock 0x40 bit is set then ignore 12 hour and BAM/PM 0x20 bit before returning return Wire.peek() & 0x40 ? bcdToInt(Wire.read() & ~0x60) : bcdToInt(Wire.read()); }
uint8_t DS3231::getTimeMinutes() { prepRead(TIMEMINUTE, 1); return bcdToInt(Wire.read()); }
uint8_t DS3231::getTimeSeconds() { prepRead(TIMESECOND, 1); return bcdToInt(Wire.read()); }