From 755da537dd80350ebf067f441b908cc22c40286d Mon Sep 17 00:00:00 2001 From: Jonathan Williamson Date: Mon, 30 May 2022 09:04:04 +0100 Subject: [PATCH] fixed bug in set_timer() --- drivers/pcf85063a/pcf85063a.cpp | 12 +++++++----- drivers/pcf85063a/pcf85063a.hpp | 8 ++++---- examples/pico_rtc/demo.cpp | 4 ++-- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/drivers/pcf85063a/pcf85063a.cpp b/drivers/pcf85063a/pcf85063a.cpp index 9312dba1..7e4599c0 100644 --- a/drivers/pcf85063a/pcf85063a.cpp +++ b/drivers/pcf85063a/pcf85063a.cpp @@ -137,11 +137,13 @@ namespace pimoroni { void PCF85063A::set_timer(uint8_t ticks, TimerTickPeriod ttp) { uint8_t bits = i2c->reg_read_uint8(address, Registers::TIMER_MODE); - // mask out timer tick period value and set new value - bits = (bits & ~0x18) | ttp; - // enable timer - bits |= 0x04; - i2c->reg_write_uint8(address, Registers::TIMER_MODE, bits); + + uint8_t timer[2] = { + ticks, + uint8_t((bits & ~0x18) | ttp | 0x04) // mask out current ttp and set new + enable + }; + + i2c->write_bytes(address, Registers::TIMER_VALUE, timer, 2); } bool PCF85063A::read_timer_flag() { diff --git a/drivers/pcf85063a/pcf85063a.hpp b/drivers/pcf85063a/pcf85063a.hpp index 6c0fe037..5dd45c9c 100644 --- a/drivers/pcf85063a/pcf85063a.hpp +++ b/drivers/pcf85063a/pcf85063a.hpp @@ -38,10 +38,10 @@ namespace pimoroni { }; enum TimerTickPeriod : int8_t { - TIMER_TICK_4096HZ = 0, - TIMER_TICK_64HZ = 1, - TIMER_TICK_1HZ = 2, - TIMER_TICK_1_OVER_60HZ = 3 + TIMER_TICK_4096HZ = 0b00 << 3, + TIMER_TICK_64HZ = 0b01 << 3, + TIMER_TICK_1HZ = 0b10 << 3, + TIMER_TICK_1_OVER_60HZ = 0b11 << 3 }; //-------------------------------------------------- diff --git a/examples/pico_rtc/demo.cpp b/examples/pico_rtc/demo.cpp index 1aaa7ad4..7ff5200f 100644 --- a/examples/pico_rtc/demo.cpp +++ b/examples/pico_rtc/demo.cpp @@ -93,8 +93,8 @@ int main() { printf("check timer function\n"); printf("\n"); - printf("- setting timer for in 3 seconds\n"); - rtc.set_timer(3); + printf("- setting timer for in 5 seconds\n"); + rtc.set_timer(5); printf("\n"); printf("- waiting for timer flag\n");