| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||||||||||||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # (C) Paul Evans, 2016-2023 -- leonerd@leonerd.org.uk | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use v5.26; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Object::Pad 0.800; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | package Device::Chip::PCF8563 0.04; | ||||||||||||||||||||||||||||
| 11 | class Device::Chip::PCF8563 | ||||||||||||||||||||||||||||
| 12 | 2 | ▶ | :isa(Device::Chip::Base::RegisteredI2C); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 2 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 2 | ▶ | use constant DEFAULT_ADDR => 0xA2 >> 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | =head1 NAME | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | C<Device::Chip::PCF8563> - chip driver for a F<PCF8563> | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | use Device::Chip::PCF8563; | ||||||||||||||||||||||||||||
| 31 | use Future::AsyncAwait; | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | use POSIX qw( mktime strftime ); | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | my $chip = Device::Chip::PCF8563->new; | ||||||||||||||||||||||||||||
| 36 | await $chip->mount( Device::Chip::Adapter::...->new ); | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | printf "The current time on this chip is ", | ||||||||||||||||||||||||||||
| 39 | await strftime( "%Y-%m-%d %H:%M:%S", localtime mktime $chip->read_time ); | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | This L<Device::Chip> subclass provides specific communication to a | ||||||||||||||||||||||||||||
| 44 | F<NXP> F<PCF8563> chip attached to a computer via an I²C adapter. | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | =cut | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | method I2C_options | ||||||||||||||||||||||||||||
| 49 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
I2C_options: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 50 | return ( | ||||||||||||||||||||||||||||
| 51 | 1 | addr => DEFAULT_ADDR, | |||||||||||||||||||||||||||
| 52 | max_bitrate => 100E3, | ||||||||||||||||||||||||||||
| 53 | ); | ||||||||||||||||||||||||||||
| 54 | 2 | } | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 2 | ▶ | use constant { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 57 | 2 | REG_CTRL1 => 0x00, | |||||||||||||||||||||||||||
| 58 | REG_CTRL2 => 0x01, | ||||||||||||||||||||||||||||
| 59 | REG_VLSECONDS => 0x02, | ||||||||||||||||||||||||||||
| 60 | 2 | }; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 6 | ▶ | sub _unpack_bcd ( $v ) { ( $v >> 4 )*10 + ( $v % 16 ) } | ||||||||||||||||||||||||||
|
Subroutine
_unpack_bcd: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 63 | 7 | ▶ | sub _pack_bcd ( $v ) { int( $v / 10 ) << 4 | ( $v % 10 ) } | ||||||||||||||||||||||||||
|
Subroutine
_pack_bcd: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | =head1 METHODS | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | The following methods documented in an C<await> expression return L<Future> | ||||||||||||||||||||||||||||
| 68 | instances. | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | =cut | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =head2 read_time | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | @tm = await $chip->read_time; | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | Returns a 7-element C<struct tm>-compatible list of values by reading the | ||||||||||||||||||||||||||||
| 77 | timekeeping registers, suitable for passing to C<POSIX::mktime>, etc... Note | ||||||||||||||||||||||||||||
| 78 | that the returned list does not contain the C<yday> or C<is_dst> fields. | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | Because the F<PCF8563> only stores a 2-digit year number plus a single century | ||||||||||||||||||||||||||||
| 81 | bit, the year is presumed to be in the range C<2000>-C<2199>. | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | This method presumes C<POSIX>-compatible semantics for the C<wday> field | ||||||||||||||||||||||||||||
| 84 | stored on the chip; i.e. that 0 is Sunday. | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | This method performs an atomic reading of all the timekeeping registers as a | ||||||||||||||||||||||||||||
| 87 | single I²C transaction, so is preferrable to invoking multiple calls to | ||||||||||||||||||||||||||||
| 88 | individual read methods. | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =cut | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 1 | ▶ | async method read_time () | ||||||||||||||||||||||||||
|
Subroutine
read_time: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||
| 93 | 1 | { | |||||||||||||||||||||||||||
| 94 | 1 | my ( $bcd_sec, $bcd_min, $bcd_hour, $bcd_mday, $wday, $bcd_mon, $bcd_year ) = | |||||||||||||||||||||||||||
| 95 | unpack "C7", await $self->read_reg( REG_VLSECONDS, 7 ); | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 1 | ▶ | die "VL bit is set; time is unreliable" if $bcd_sec & 0x80; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 1 | my $century = $bcd_mon & 0x80; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | return ( | ||||||||||||||||||||||||||||
| 102 | 1 | ▶ | _unpack_bcd( $bcd_sec ), | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | _unpack_bcd( $bcd_min & 0x7F ), | ||||||||||||||||||||||||||||
| 104 | _unpack_bcd( $bcd_hour & 0x3F ), | ||||||||||||||||||||||||||||
| 105 | _unpack_bcd( $bcd_mday & 0x3F ), | ||||||||||||||||||||||||||||
| 106 | _unpack_bcd( $bcd_mon & 0x1F ) - 1, | ||||||||||||||||||||||||||||
| 107 | _unpack_bcd( $bcd_year ) + 100 + ( $century ? 100 : 0 ), | ||||||||||||||||||||||||||||
| 108 | $wday & 0x07, | ||||||||||||||||||||||||||||
| 109 | ); | ||||||||||||||||||||||||||||
| 110 | 2 | } | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =head2 write_time | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | await $chip->write_time( @tm ); | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | Writes the timekeeping registers from a 7-element C<struct tm>-compatible list | ||||||||||||||||||||||||||||
| 117 | of values. This method ignores the C<yday> and C<is_dst> fields, if present. | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | Because the F<PCF8563> only stores a 2-digit year number and a century bit, | ||||||||||||||||||||||||||||
| 120 | the year must be in the range C<2000>-C<2199> (i.e. numerical values of C<100> | ||||||||||||||||||||||||||||
| 121 | to C<299>). | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | This method performs an atomic writing of all the timekeeping registers as a | ||||||||||||||||||||||||||||
| 124 | single I²C transaction, so is preferrable to invoking multiple calls to | ||||||||||||||||||||||||||||
| 125 | individual write methods. | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =cut | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 1 | ▶ | async method write_time ( $sec, $min, $hour, $mday, $mon, $year, $wday ) | ||||||||||||||||||||||||||
|
Subroutine
write_time: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||||||||||||||||||||
| 130 | 1 | { | |||||||||||||||||||||||||||
| 131 | 1 | ▶ | $year >= 100 and $year <= 299 or croak "Invalid year ($year)"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$year >= 100 and $year <= 299
MC/DC0%0/2
$year >= 100 and $year <= 299
$year >= 100
$year <= 299
Truth table33%1/3
A and B
A= $year >= 100B= $year <= 299
| |||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 1 | my $century = $year >= 200; | |||||||||||||||||||||||||||
| 134 | 1 | $year %= 100; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 1 | ▶ | await $self->write_reg( REG_VLSECONDS, pack "C7", | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | _pack_bcd( $sec ), | ||||||||||||||||||||||||||||
| 138 | _pack_bcd( $min ), | ||||||||||||||||||||||||||||
| 139 | _pack_bcd( $hour ), | ||||||||||||||||||||||||||||
| 140 | _pack_bcd( $mday ), | ||||||||||||||||||||||||||||
| 141 | _pack_bcd( $wday ), | ||||||||||||||||||||||||||||
| 142 | _pack_bcd( $mon + 1 ) | ( $century ? 0x80 : 0 ), | ||||||||||||||||||||||||||||
| 143 | _pack_bcd( $year ), | ||||||||||||||||||||||||||||
| 144 | ); | ||||||||||||||||||||||||||||
| 145 | 2 | } | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =cut | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 2 | 0x55AA; | |||||||||||||||||||||||||||