From e7975e7e04b2da2bc14dca15a5fd9848fa87fd76 Mon Sep 17 00:00:00 2001 From: TD-er Date: Wed, 15 May 2024 16:17:19 +0200 Subject: [PATCH] [AS3935MI] Get in sync with pending PR to original lib --- lib/AS3935MI/src/AS3935MI.cpp | 20 ++++++++++++-------- lib/AS3935MI/src/AS3935MI.h | 9 +++++++-- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/lib/AS3935MI/src/AS3935MI.cpp b/lib/AS3935MI/src/AS3935MI.cpp index c68a4d4ec..b3ffb32f6 100644 --- a/lib/AS3935MI/src/AS3935MI.cpp +++ b/lib/AS3935MI/src/AS3935MI.cpp @@ -18,8 +18,6 @@ #include "AS3935MI.h" -#include - AS3935MI::AS3935MI(uint8_t irq) : irq_(irq) { @@ -144,6 +142,12 @@ uint32_t AS3935MI::readEnergy() uint8_t AS3935MI::readAntennaTuning() { uint8_t return_value = readRegisterValue(AS3935_REGISTER_TUN_CAP, AS3935_MASK_TUN_CAP); + if (return_value != static_cast(-1)) { + // No read error, so update the tuning_cap_cache_ + tuning_cap_cache_ = return_value; + } else { + return tuning_cap_cache_; + } return return_value; } @@ -191,13 +195,13 @@ bool AS3935MI::calibrateRCO() writeRegister(AS3935_REGISTER_CALIB_RCO, AS3935_DIRECT_CMD); //expose 1.1 MHz SRCO clock on IRQ pin - writeRegisterValue(AS3935_REGISTER_DISP_SRCO, AS3935_MASK_DISP_SRCO, static_cast(1)); + displaySRCO_on_IRQ(true); //wait for calibration to finish... delayMicroseconds(AS3935_TIMEOUT); //stop exposing clock on IRQ pin - writeRegisterValue(AS3935_REGISTER_DISP_SRCO, AS3935_MASK_DISP_SRCO, static_cast(0)); + displaySRCO_on_IRQ(false); //check calibration results. bits will be set if calibration failed. bool success_TRCO = !static_cast(readRegisterValue(AS3935_REGISTER_TRCO_CALIB_NOK, AS3935_MASK_TRCO_CALIB_NOK)); @@ -251,7 +255,7 @@ bool AS3935MI::calibrateResonanceFrequency(int32_t &frequency, uint8_t division_ //display LCO on IRQ displayLCO_on_IRQ(true); - bool irq_current = DIRECT_pinRead(irq_); + bool irq_current = digitalRead(irq_); bool irq_last = irq_current; int16_t counts = 0; @@ -261,7 +265,7 @@ bool AS3935MI::calibrateResonanceFrequency(int32_t &frequency, uint8_t division_ //count transitions for 100ms while ((millis() - time_start) < 100) { - irq_current = DIRECT_pinRead(irq_); + irq_current = digitalRead(irq_); if (irq_current != irq_last) counts++; @@ -311,7 +315,7 @@ bool AS3935MI::checkIRQ() displayLCO_on_IRQ(true); delayMicroseconds(AS3935_TIMEOUT); - bool irq_current = DIRECT_pinRead(irq_); + bool irq_current = digitalRead(irq_); bool irq_last = irq_current; int16_t counts = 0; @@ -321,7 +325,7 @@ bool AS3935MI::checkIRQ() //count transitions for 10ms while ((millis() - time_start) < 10) { - irq_current = DIRECT_pinRead(irq_); + irq_current = digitalRead(irq_); if (irq_current != irq_last) counts++; diff --git a/lib/AS3935MI/src/AS3935MI.h b/lib/AS3935MI/src/AS3935MI.h index 350fb6dec..2e3e0a74b 100644 --- a/lib/AS3935MI/src/AS3935MI.h +++ b/lib/AS3935MI/src/AS3935MI.h @@ -266,10 +266,10 @@ public: // Ideally 500 kHz signal divided by the set division ratio void displayLCO_on_IRQ(bool enable); - // Ideally 1.1 MHz signal divided by the set division ratio + // Ideally 1.1 MHz signal void displaySRCO_on_IRQ(bool enable); - // Ideally 32.768 kHz signal divided by the set division ratio + // Ideally 32.768 kHz signal void displayTRCO_on_IRQ(bool enable); private: @@ -384,6 +384,11 @@ private: uint8_t irq_; //interrupt pin + // Tuning cap value is located in the same register as the display LCO/SRCO/TRCO flags + // When those are active the device may not give an ACK when trying to read + // (via I2C) the register to update those display flags + // To overcome this issue, we keep a cache of the tuning cap parameter + // and write directly to the register instead of read/set bits/write. uint8_t tuning_cap_cache_ = 0; };