diff --git a/README.md b/README.md index c1b232a..ecf7291 100644 --- a/README.md +++ b/README.md @@ -1,35 +1,31 @@ -Sure! Here's an improved version of the README file with a markdown table showcasing the functions in the library: - ---- - # pcf8563-RTC -A library to interface ESP chips with the NXP PCF8563 Real-Time Clock (RTC) in the Arduino (C++) Framework. +The `pcf8563-RTC` library is designed to interface ESP chips with the NXP PCF8563 Real-Time Clock (RTC) within the Arduino (C++) Framework. ## Features -- Supports multiple I2C buses -> `rtc.begin(Wire1);` -- Works with timezones (RTC is set to UTC) +- Supports multiple I2C buses: `rtc.begin(Wire1);` +- Timezone support (RTC is set to UTC) - [List of timezones](https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv) -- Sets System (ESP32) time from RTC (assumes RTC is set to UTC/GMT) -- Sets RTC time from System (epoch) -- Set RTC time using WiFi example +- Sets the system (ESP32) time from the RTC (assumes RTC is set to UTC/GMT) +- Sets the RTC time from the system (epoch) +- Example of setting RTC time using WiFi ## Warnings - Not all functions are implemented - Not compatible with Lewis' library -- Only tested with the ESP32 -- Under development +- Only tested with the ESP32 and S2 variants +- Currently under development -## Setup +## Example ESP32 Code -```c++ +```cpp #include "pcf8563.h" #include "time.h" PCF8563_Class rtc; -const char *time_zone = "NZST-12NZDT,M9.5.0,M4.1.0/3"; // [List of timezones](https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv) +const char *time_zone = "NZST-12NZDT,M9.5.0,M4.1.0/3"; // [List of timezones](https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv) Wire.begin(); rtc.begin(Wire); @@ -38,9 +34,7 @@ tzset(); rtc.syncToSystem(); ``` -## Loop - -```c++ +```cpp struct tm timeinfo; getLocalTime(&timeinfo); Serial.print(&timeinfo, "%d/%m/%y %H:%M:%S"); @@ -51,18 +45,18 @@ Serial.print(&timeinfo, "%d/%m/%y %H:%M:%S"); ![Schematic](/images/schematic.png) - Uses a CR1220 coincell (though almost any 3V lithium coincell should work) -- To not discharge the battery too fast, disable output clock and alarms -- Low backup current: typical 0.25uA at 3.0V (theoretical, not tested) +- To prevent battery discharge, disable output clock and alarms +- Low backup current: typically 0.25uA at 3.0V (theoretical, not tested) - Currently testing with 4.7kohm pull-ups on SDA and SCL - Currently using a Seiko Epson Q13FC1350000400 crystal (+-20ppm) - In theory, this setup will drift by a maximum of ~11 minutes per year ([Real-Time Clock Calculator](https://www.analog.com/en/design-center/interactive-design-tools/real-time-clock-calculator.html)) - In theory, a 37mAh CR1220 battery will last ~14 years without any charging (including battery self-discharge) -- 3.3V -> diode -> resistor charging of a coincell from 3.3V is hotly debated as to whether it is a good, okay, or horrifically bad idea. I have not had any issues, but your mileage may vary -- Be careful with PCB placement (have the crystal as close to the RTC chip as possible and surround it with ground planes connected with vias) +- Charging a coincell from 3.3V through a diode and resistor is a topic of debate. It is important to exercise caution and consider individual circumstances. Using a 200-ohm resistor should limit the current to a maximum of 12mA, while a 1k-ohm resistor should limit it to a maximum of 2.6mA. +- Be careful with PCB placement; ensure the crystal is placed as close to the RTC chip as possible and surrounded by ground planes connected with vias -## Based on the awesome work of Lewis He +## Acknowledgments -Originally created by Lewis He in 2019 +This library is based on the original work of Lewis He, created in 2019. [GitHub Repository](https://github.com/lewisxhe/PCF8563_Library) @@ -70,37 +64,4 @@ Originally created by Lewis He in 2019 This library is licensed under the MIT License. See the [LICENSE](LICENSE) file for more information. ---- - -And here's the markdown table with the functions: - -| Function | Description | -| ----------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------- | -| `int begin(TwoWire &port = Wire, uint8_t addr = PCF8563_ADDRESS)` | Initializes the PCF8563 RTC. | -| `void setDateTime(uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second)` | Sets the date and time on the RTC. | -| `void setDateTime(RTC_Date date)` | Sets the date and time on the RTC using an RTC_Date object. | -| `RTC_Date getDateTime()` | Retrieves the current date and time from the RTC. | -| `RTC_Alarm getAlarm()` | Retrieves the alarm settings from the RTC. | -| `void enableAlarm()` | Enables the alarm on the RTC. | -| `void disableAlarm()` | Disables the alarm on the RTC. | -| `bool alarmActive()` | Checks if the alarm is active on the RTC. | -| `void resetAlarm()` | Resets the alarm on the RTC. | -| `void setAlarm(RTC_Alarm alarm)` | Sets the alarm on the RTC using an RTC_Alarm object. | -| `void setAlarm(uint8_t hour, uint8_t minute, uint8_t day, uint8_t weekday)` | Sets the alarm on the RTC using individual values for hour, minute, day, and weekday. | -| `bool isValid()` | Checks if the clock data on the RTC can be trusted. | -| `void setAlarmByWeekDay(uint8_t weekday)` | Sets the alarm on the RTC based on the weekday. | -| `void setAlarmByHours(uint8_t hour)` | Sets the alarm on the RTC based on the hour. | -| `void setAlarmByDays(uint8_t day)` | Sets the alarm on the RTC based on the day. | -| `void setAlarmByMinutes(uint8_t minute)` | Sets the alarm on the RTC based on the minute. | -| `bool isTimerEnable()` | Checks if the timer is enabled on the RTC. | -| `bool isTimerActive()` | Checks if the timer is active on the RTC. | -| `void enableTimer()` | Enables the timer on the RTC. | -| `void disableTimer()` | Disables the timer on the RTC. | -| `void setTimer(uint8_t val, uint8_t freq, bool enInterrupt)` | Sets the timer on the RTC with the specified values. | -| `void clearTimer()` | Clears the timer on the RTC. | -| `bool enableCLK(uint8_t freq)` | Enables the clock output on the RTC with the specified frequency. | -| `void disableCLK()` | Disables the clock output on the RTC. | -| `bool syncToSystem()` | Sets the system time from the RTC if the RTC data is valid. | -| `bool syncToRtc()` | Sets the RTC time from the system time (ESP) if the system time is between 1970 and 2100. | - -Please note that not all functions are implemented, and the library is under active development. Make sure to refer to the documentation and examples for more information on how to use the library. +Please note that not all functions are implemented, and the library is under active development. For more information on how to use the library, refer to the documentation and examples provided. \ No newline at end of file diff --git a/library.json b/library.json index 15a1c57..c681e85 100644 --- a/library.json +++ b/library.json @@ -1,28 +1,25 @@ { "name": "pcf8563-rtc", "version": "1.2.0", + "description": "An Arduino library for interfacing ESP chips with PCF8563 Real Time Clock using the I2C protocol.", "keywords": [ "rtc", - "clock" + "clock", + "pcf8563", + "nxt", + "i2c" ], - "description": "A library for the PCF8563", "repository": { "type": "git", "url": "https://github.com/CDFER/pcf8563-RTC.git" }, - "frameworks": "arduino", - "platforms": { - "espressif32": {} + "author": { + "name": "Chris Dirks", + "url": "https://keastudios.co.nz/about.htm", + "maintainer": true }, + "license": "Other", "homepage": "https://github.com/CDFER/pcf8563-RTC", - "license": "MIT", - "authors": [ - { - "name": "Your Name", - "email": "your-email@example.com" - } - ], - "bugs": { - "url": "https://github.com/CDFER/pcf8563-RTC/issues" - } + "frameworks": "arduino", + "platforms": "*" } \ No newline at end of file diff --git a/pcf8563.cpp b/pcf8563.cpp index 50a6303..d552d12 100644 --- a/pcf8563.cpp +++ b/pcf8563.cpp @@ -39,6 +39,29 @@ uint8_t PCF8563_Class::begin(TwoWire &port, int addr) { return _i2cPort->endTransmission(); // Check if the transmission was successful and return the result } +const char *PCF8563_Class::getErrorText(uint8_t errorCode) { + switch (errorCode) { + case 0: + return "Success"; + case 1: + return "I2C data too long to fit in transmit buffer"; + case 2: + return "I2C received NACK on transmit of address"; + case 3: + return "I2C received NACK on transmit of data"; + case 4: + return "I2C other error"; + case 5: + return "I2C timeout"; + case 6: + return "bytesReceived(%i) != bytesRequested(%i)"; + case 7: + return "Measurement out of range"; + default: + return "Unknown error"; + } +} + void PCF8563_Class::setDateTime(RTC_Date date) { setDateTime(date.year, date.month, date.day, date.hour, date.minute, date.second); // Call the setDateTime function with individual date and time components diff --git a/pcf8563.h b/pcf8563.h index adb60c4..6f3afe3 100644 --- a/pcf8563.h +++ b/pcf8563.h @@ -178,6 +178,28 @@ class PCF8563_Class { */ uint8_t begin(TwoWire &port = Wire, int addr = PCF8563_ADDRESS); + /** + * @brief Converts an error code into descriptive text. + * + * This function takes an error code and returns a corresponding descriptive text. It can be used to convert + * error codes returned by the RTC into human-readable error messages. + * + * @param errorCode The error code to convert. + * @return A pointer to a constant character array containing the descriptive text of the error. + * If the error code is not recognized, "Unknown error" is returned. + * + * @note The error codes and their meanings are as follows: + * - 0: Success + * - 1: I2C data too long to fit in transmit buffer + * - 2: I2C received NACK on transmit of address + * - 3: I2C received NACK on transmit of data + * - 4: I2C other error + * - 5: I2C timeout + * - 6: bytesReceived(%i) != bytesRequested(%i) + * - 7: Measurement out of range + */ + const char *getErrorText(uint8_t errorCode); + /** * @brief Sets the date and time of the RTC. * @param year The year.