Skip to content

Commit bec2491

Browse files
authored
Merge pull request #6 from bablokb/4upstream
add support for timer and clkout
2 parents 14878b1 + cd6e319 commit bec2491

File tree

8 files changed

+231
-18
lines changed

8 files changed

+231
-18
lines changed

README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ Of course, you must import the library to use it:
7575
.. code:: python
7676
7777
import busio
78-
import adafruit_pcf8563
78+
from adafruit_pcf8563.pcf8563 import PCF8563
7979
import time
8080
8181
All the Adafruit RTC libraries take an instantiated and active I2C object
@@ -98,7 +98,7 @@ the RTC object:
9898

9999
.. code:: python
100100
101-
rtc = adafruit_pcf8563.PCF8563(i2c_bus)
101+
rtc = PCF8563(i2c_bus)
102102
103103
Date and time
104104
-------------

adafruit_pcf8563/clock.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# SPDX-FileCopyrightText: 2016 Philip R. Moyer and Radomir Dopieralski for Adafruit Industries.
2+
# SPDX-FileCopyrightText: Copyright (c) 2021 Jeff Epler for Adafruit Industries
3+
# SPDX-FileCopyrightText: Copyright (c) 2023 Bernhard Bablok
4+
#
5+
# SPDX-License-Identifier: MIT
6+
7+
"""
8+
`clock` - PCF8563 Clock module
9+
==============================
10+
11+
This class supports the clkout-feature of the PCF8563-based RTC in CircuitPython.
12+
13+
Functions are included for reading and writing registers to configure
14+
clklout frequency.
15+
16+
The class supports stand-alone usage. In this case, pass an i2-bus object
17+
to the constructor. If used together with the PCF8563 class (rtc), instantiate
18+
the rtc-object first and then pass the i2c_device attribute of the rtc
19+
to the constructor of the clock.
20+
21+
Author(s): Bernhard Bablok
22+
Date: March 2023
23+
24+
Implementation Notes
25+
--------------------
26+
27+
**Hardware:**
28+
29+
* `Seeeduino XIAO Expansion Board <https://www.adafruit.com/product/5033>`_
30+
- Works With Adafruit QT Py (Product ID: 5033)
31+
32+
**Software and Dependencies:**
33+
34+
* Adafruit CircuitPython firmware: https://github.com/adafruit/circuitpython/releases
35+
* Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Register
36+
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
37+
38+
**Notes:**
39+
40+
#. Datasheet: http://cache.nxp.com/documents/data_sheet/PCF8563.pdf
41+
"""
42+
43+
__version__ = "0.0.0+auto.0"
44+
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PCF8563.git"
45+
46+
import time
47+
48+
from adafruit_bus_device.i2c_device import I2CDevice
49+
from adafruit_register import i2c_bit
50+
from adafruit_register import i2c_bits
51+
from micropython import const
52+
53+
try:
54+
from typing import Union
55+
from busio import I2C
56+
except ImportError:
57+
pass
58+
59+
60+
class Clock: # pylint: disable=too-few-public-methods
61+
"""Interface to the clkout of the PCF8563 RTC.
62+
63+
:param I2C i2c_bus: The I2C bus object
64+
"""
65+
66+
clockout_enabled = i2c_bit.RWBit(0x0D, 7)
67+
"""True if clockout is enabled (default). To disable clockout, set to False"""
68+
69+
clockout_frequency = i2c_bits.RWBits(2, 0x0D, 0)
70+
"""Clock output frequencies generated. Default is 32.768kHz.
71+
Possible values are as shown (selection value - frequency).
72+
00 - 32.768khz
73+
01 - 1.024kHz
74+
10 - 0.032kHz (32Hz)
75+
11 - 0.001kHz (1Hz)
76+
"""
77+
78+
CLOCKOUT_FREQ_32KHZ = const(0b00)
79+
"""Clock frequency of 32 KHz"""
80+
CLOCKOUT_FREQ_1KHZ = const(0b01)
81+
"""Clock frequency of 4 KHz"""
82+
CLOCKOUT_FREQ_32HZ = const(0b10)
83+
"""Clock frequency of 32 Hz"""
84+
CLOCKOUT_FREQ_1HZ = const(0b11)
85+
"""Clock frequency of 1 Hz"""
86+
87+
def __init__(self, i2c: Union[I2C, I2CDevice]) -> None:
88+
if isinstance(i2c, I2CDevice):
89+
self.i2c_device = i2c # reuse i2c_device (from PCF8563-instance)
90+
else:
91+
time.sleep(0.05)
92+
self.i2c_device = I2CDevice(i2c, 0x51)

adafruit_pcf8563.py renamed to adafruit_pcf8563/pcf8563.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# SPDX-License-Identifier: MIT
55

66
"""
7-
`adafruit_pcf8563` - PCF8563 Real Time Clock module
7+
`pcf8563` - PCF8563 Real Time Clock module
88
====================================================
99
1010
This library supports the use of the PCF8563-based RTC in CircuitPython. It
@@ -35,8 +35,10 @@ class is inherited by the chip-specific subclasses.
3535
**Notes:**
3636
3737
#. Milliseconds are not supported by this RTC.
38+
#. The alarm does not support seconds. It will always fire on full minutes.
39+
#. This RTC has a single timer. The class Timer implements the interface to timer-specfic registers.
40+
#. The class Clock implements the configuration of the clkout-pin.
3841
#. Datasheet: http://cache.nxp.com/documents/data_sheet/PCF8563.pdf
39-
4042
"""
4143

4244
__version__ = "0.0.0+auto.0"
@@ -75,26 +77,23 @@ class PCF8563:
7577
alarm = i2c_bcd_alarm.BCDAlarmTimeRegister(
7678
0x09, has_seconds=False, weekday_shared=False, weekday_start=0
7779
)
78-
"""Alarm time for the alarm."""
80+
"""Alarm time for the alarm. Note that the value of the seconds-fields
81+
is ignored, i.e. alarms only fire at full minutes. For short-term
82+
alarms, use a timer instead."""
7983

8084
alarm_interrupt = i2c_bit.RWBit(0x01, 1)
8185
"""True if the interrupt pin will output when alarm is alarming."""
8286

8387
alarm_status = i2c_bit.RWBit(0x01, 3)
8488
"""True if alarm is alarming. Set to False to reset."""
8589

90+
clockout_enabled = i2c_bit.RWBit(0x0D, 7)
91+
"""True if clockout is enabled (default). To disable clockout, set to False"""
92+
8693
def __init__(self, i2c_bus: I2C) -> None:
8794
time.sleep(0.05)
8895
self.i2c_device = I2CDevice(i2c_bus, 0x51)
8996

90-
# Try and verify this is the RTC we expect by checking the timer B
91-
# frequency control bits which are 1 on reset and shouldn't ever be
92-
# changed.
93-
buf = bytearray(2)
94-
buf[0] = 0x12
95-
with self.i2c_device as i2c:
96-
i2c.write_then_readinto(buf, buf, out_end=1, in_start=1)
97-
9897
@property
9998
def datetime(self) -> time.struct_time:
10099
"""Gets the current date and time or sets the current date and time then starts the
@@ -106,3 +105,8 @@ def datetime(self, value: time.struct_time) -> None:
106105
# Automatically sets lost_power to false.
107106
self.datetime_register = value
108107
self.datetime_compromised = False
108+
109+
@property
110+
def lost_power(self) -> bool:
111+
"""Compatibility property for PCF8523-lib"""
112+
return self.datetime_compromised

adafruit_pcf8563/timer.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# SPDX-FileCopyrightText: 2016 Philip R. Moyer and Radomir Dopieralski for Adafruit Industries.
2+
# SPDX-FileCopyrightText: Copyright (c) 2021 Jeff Epler for Adafruit Industries
3+
# SPDX-FileCopyrightText: Copyright (c) 2023 Bernhard Bablok
4+
#
5+
# SPDX-License-Identifier: MIT
6+
7+
"""
8+
`timer` - PCF8563 Timer module
9+
==============================
10+
11+
This class supports the timer of the PCF8563-based RTC in CircuitPython.
12+
13+
Functions are included for reading and writing registers and manipulating
14+
timer objects.
15+
16+
The class supports stand-alone usage. In this case, pass an i2-bus object
17+
to the constructor. If used together with the PCF8563 class (rtc), instantiate
18+
the rtc-object first and then pass the i2c_device attribute of the rtc
19+
to the constructor of the timer.
20+
21+
Author(s): Bernhard Bablok
22+
Date: March 2023
23+
24+
Implementation Notes
25+
--------------------
26+
27+
**Hardware:**
28+
29+
* `Seeeduino XIAO Expansion Board <https://www.adafruit.com/product/5033>`_
30+
- Works With Adafruit QT Py (Product ID: 5033)
31+
32+
**Software and Dependencies:**
33+
34+
* Adafruit CircuitPython firmware: https://github.com/adafruit/circuitpython/releases
35+
* Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Register
36+
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
37+
38+
**Notes:**
39+
40+
#. Datasheet: http://cache.nxp.com/documents/data_sheet/PCF8563.pdf
41+
"""
42+
43+
__version__ = "0.0.0+auto.0"
44+
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PCF8563.git"
45+
46+
import time
47+
48+
from adafruit_bus_device.i2c_device import I2CDevice
49+
from adafruit_register import i2c_bit
50+
from adafruit_register import i2c_bits
51+
from micropython import const
52+
53+
try:
54+
from typing import Union
55+
from busio import I2C
56+
except ImportError:
57+
pass
58+
59+
60+
class Timer: # pylint: disable=too-few-public-methods
61+
"""Interface to the timer of the PCF8563 RTC.
62+
63+
:param I2C i2c_bus: The I2C bus object
64+
"""
65+
66+
timer_enabled = i2c_bit.RWBit(0x0E, 7)
67+
"""True if the timer is enabled. Default is False."""
68+
69+
timer_frequency = i2c_bits.RWBits(2, 0x0E, 0)
70+
"""Timer clock frequency. Default is 1/60Hz.
71+
Possible values are as shown (selection value - frequency).
72+
00 - 4.096kHz
73+
01 - 64Hz
74+
10 - 1Hz
75+
11 - 1/60Hz
76+
"""
77+
TIMER_FREQ_4KHZ = const(0b00)
78+
"""Timer frequency of 4 KHz"""
79+
TIMER_FREQ_64HZ = const(0b01)
80+
"""Timer frequency of 64 Hz"""
81+
TIMER_FREQ_1HZ = const(0b10)
82+
"""Timer frequency of 1 Hz"""
83+
TIMER_FREQ_1_60HZ = const(0b11)
84+
"""Timer frequency of 1/60 Hz"""
85+
86+
timer_value = i2c_bits.RWBits(8, 0x0F, 0)
87+
""" Timer value (0-255). The default is undefined.
88+
The total countdown duration is calcuated by
89+
timer_value/timer_frequency. For a higher precision, use higher values
90+
and frequencies, e.g. for a one minute timer you could use
91+
value=1, frequency=1/60Hz or value=60, frequency=1Hz. The
92+
latter will give better results. See the PCF85x3 User's Manual
93+
for details."""
94+
95+
timer_interrupt = i2c_bit.RWBit(0x01, 0)
96+
"""True if the interrupt pin will assert when timer has elapsed.
97+
Defaults to False."""
98+
99+
timer_status = i2c_bit.RWBit(0x01, 2)
100+
"""True if timer has elapsed. Set to False to reset."""
101+
102+
timer_pulsed = i2c_bit.RWBit(0x01, 4)
103+
"""True if timer asserts INT as a pulse. The default
104+
value False asserts INT permanently."""
105+
106+
def __init__(self, i2c: Union[I2C, I2CDevice]) -> None:
107+
if isinstance(i2c, I2CDevice):
108+
self.i2c_device = i2c # reuse i2c_device (from PCF8563-instance)
109+
else:
110+
time.sleep(0.05)
111+
self.i2c_device = I2CDevice(i2c, 0x51)

docs/api.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11

2-
.. automodule:: adafruit_pcf8563
2+
.. automodule:: adafruit_pcf8563.pcf8563
3+
:members:
4+
5+
.. automodule:: adafruit_pcf8563.timer
6+
:members:
7+
8+
.. automodule:: adafruit_pcf8563.clock
39
:members:

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
# Uncomment the below if you use native CircuitPython modules such as
2525
# digitalio, micropython and busio. List the modules you use. Without it, the
2626
# autodoc module docs will fail to generate with a warning.
27-
# autodoc_mock_imports = ["adafruit_bus_device", "adafruit_register"]
27+
autodoc_mock_imports = ["adafruit_bus_device", "adafruit_register"]
2828

2929
# Add any paths that contain templates here, relative to this directory.
3030
templates_path = ["_templates"]

examples/pcf8563_simpletest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
import board
1313
import busio
1414

15-
import adafruit_pcf8563
15+
from adafruit_pcf8563.pcf8563 import PCF8563
1616

1717
# Change to the appropriate I2C clock & data pins here!
1818
i2c_bus = busio.I2C(board.SCL, board.SDA)
1919

2020
# Create the RTC instance:
21-
rtc = adafruit_pcf8563.PCF8563(i2c_bus)
21+
rtc = PCF8563(i2c_bus)
2222

2323
# Lookup table for names of days (nicer printing).
2424
days = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ classifiers = [
4242
dynamic = ["dependencies", "optional-dependencies"]
4343

4444
[tool.setuptools]
45-
py-modules = ["adafruit_pcf8563"]
45+
packages = ["adafruit_pcf8563"]
4646

4747
[tool.setuptools.dynamic]
4848
dependencies = {file = ["requirements.txt"]}

0 commit comments

Comments
 (0)