From 0fa32f3de04af228b28a82b67558c3ef3078c7b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Milants?= Date: Tue, 27 Dec 2022 21:22:57 +0100 Subject: [PATCH] Refactor TwiMaster as a generic class that implements the concept IsTwi. --- src/CMakeLists.txt | 6 ++- src/drivers/Bma421.h | 3 +- src/drivers/Cst816s.h | 2 +- src/drivers/Hrs3300.h | 1 + src/drivers/Spi.h | 3 +- src/drivers/SpiMaster.h | 3 +- src/drivers/TwiMaster.h | 72 ++++++++++++++++----------- src/drivers/{ => nrf52}/TwiMaster.cpp | 4 +- src/drivers/nrf52/TwiMaster.h | 44 ++++++++++++++++ src/main.cpp | 3 +- src/port/infinitime.h | 3 ++ src/systemtask/SystemTask.cpp | 6 --- src/systemtask/SystemTask.h | 1 - 13 files changed, 104 insertions(+), 47 deletions(-) rename src/drivers/{ => nrf52}/TwiMaster.cpp (98%) create mode 100644 src/drivers/nrf52/TwiMaster.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c3848a62..5ddfc175 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -438,6 +438,7 @@ list(APPEND SOURCE_FILES drivers/St7789.cpp drivers/nrf52/SpiMaster.cpp drivers/nrf52/Spi.cpp + drivers/nrf52/TwiMaster.cpp drivers/spiFlash/SpiNorFlash.cpp drivers/Watchdog.cpp drivers/DebugPins.cpp @@ -484,7 +485,6 @@ list(APPEND SOURCE_FILES systemtask/SystemTask.cpp systemtask/SystemMonitor.cpp - drivers/TwiMaster.cpp heartratetask/HeartRateTask.cpp components/heartrate/Ppg.cpp @@ -505,6 +505,7 @@ list(APPEND RECOVERY_SOURCE_FILES drivers/St7789.cpp drivers/nrf52/SpiMaster.cpp drivers/nrf52/Spi.cpp + drivers/nrf52/TwiMaster.cpp drivers/Watchdog.cpp drivers/DebugPins.cpp drivers/InternalFlash.cpp @@ -545,7 +546,6 @@ list(APPEND RECOVERY_SOURCE_FILES systemtask/SystemTask.cpp systemtask/SystemMonitor.cpp - drivers/TwiMaster.cpp components/gfx/Gfx.cpp components/rle/RleDecoder.cpp components/heartrate/HeartRateController.cpp @@ -567,6 +567,7 @@ list(APPEND RECOVERYLOADER_SOURCE_FILES drivers/nrf52/SpiMaster.cpp drivers/nrf52/Spi.cpp + drivers/nrf52/TwiMaster.cpp logging/NrfLogger.cpp components/rle/RleDecoder.cpp @@ -620,6 +621,7 @@ set(INCLUDE_FILES drivers/SpiNorFlash.h drivers/nrf52/SpiMaster.h drivers/nrf52/Spi.h + drivers/nrf52/TwiMaster.h drivers/spiFlash/SpiNorFlash.h drivers/Watchdog.h drivers/DebugPins.h diff --git a/src/drivers/Bma421.h b/src/drivers/Bma421.h index ac5c707f..0e5fd85d 100644 --- a/src/drivers/Bma421.h +++ b/src/drivers/Bma421.h @@ -1,9 +1,10 @@ #pragma once +#include "drivers/TwiMaster.h" +#include "port/infinitime.h" #include namespace Pinetime { namespace Drivers { - class TwiMaster; class Bma421 { public: enum class DeviceTypes : uint8_t { Unknown, BMA421, BMA425 }; diff --git a/src/drivers/Cst816s.h b/src/drivers/Cst816s.h index 9d426c9d..30618691 100644 --- a/src/drivers/Cst816s.h +++ b/src/drivers/Cst816s.h @@ -1,6 +1,6 @@ #pragma once -#include "drivers/TwiMaster.h" +#include "port/infinitime.h" namespace Pinetime { namespace Drivers { diff --git a/src/drivers/Hrs3300.h b/src/drivers/Hrs3300.h index 8bbdc69a..d3dfec44 100644 --- a/src/drivers/Hrs3300.h +++ b/src/drivers/Hrs3300.h @@ -1,6 +1,7 @@ #pragma once #include "drivers/TwiMaster.h" +#include "port/infinitime.h" namespace Pinetime { namespace Drivers { diff --git a/src/drivers/Spi.h b/src/drivers/Spi.h index 0df3e2c0..bfd9e8a5 100644 --- a/src/drivers/Spi.h +++ b/src/drivers/Spi.h @@ -17,8 +17,7 @@ namespace Pinetime { requires IsSpi class Spi { public: - Spi(T& spi) : impl {spi} { - } + explicit Spi(T& spi) : impl {spi} { } Spi(const Spi&) = delete; Spi& operator=(const Spi&) = delete; Spi(Spi&&) = delete; diff --git a/src/drivers/SpiMaster.h b/src/drivers/SpiMaster.h index c916f3bf..b28c456e 100644 --- a/src/drivers/SpiMaster.h +++ b/src/drivers/SpiMaster.h @@ -23,8 +23,7 @@ namespace Pinetime { requires IsSpiMaster class SpiMaster { public: - SpiMaster(T& spiMaster) : impl {spiMaster} { - } + explicit SpiMaster(T& spiMaster) : impl {spiMaster} { } SpiMaster(const SpiMaster&) = delete; SpiMaster& operator=(const SpiMaster&) = delete; SpiMaster(SpiMaster&&) = delete; diff --git a/src/drivers/TwiMaster.h b/src/drivers/TwiMaster.h index 30ac6c5f..0035c9db 100644 --- a/src/drivers/TwiMaster.h +++ b/src/drivers/TwiMaster.h @@ -1,41 +1,55 @@ #pragma once -#include -#include -#include // NRF_TWIM_Type +#include +#include #include namespace Pinetime { namespace Drivers { - class TwiMaster { - public: - enum class ErrorCodes { NoError, TransactionFailed }; + template + concept IsTwi = requires(TwiImpl twi, uint8_t deviceAddress, uint8_t registerAddress, uint8_t* data, const uint8_t* constData, size_t size) { + { twi.Init() }; + { twi.Write(deviceAddress, registerAddress, constData, size) }; + { twi.Read(deviceAddress, registerAddress, data, size) }; + { twi.Sleep() }; + { twi.Wakeup() }; + }; - TwiMaster(NRF_TWIM_Type* module, uint32_t frequency, uint8_t pinSda, uint8_t pinScl); + namespace Interface { + template + requires IsTwi + class TwiMaster { + public: + explicit TwiMaster(T& impl) : impl {impl} {} + TwiMaster(const TwiMaster&) = delete; + TwiMaster& operator=(const TwiMaster&) = delete; + TwiMaster(TwiMaster&&) = delete; + TwiMaster& operator=(TwiMaster&&) = delete; - void Init(); - ErrorCodes Read(uint8_t deviceAddress, uint8_t registerAddress, uint8_t* buffer, size_t size); - ErrorCodes Write(uint8_t deviceAddress, uint8_t registerAddress, const uint8_t* data, size_t size); + enum class ErrorCodes { NoError, TransactionFailed }; - void Sleep(); - void Wakeup(); + void Init() { + impl.Init(); + } - private: - ErrorCodes Read(uint8_t deviceAddress, uint8_t* buffer, size_t size, bool stop); - ErrorCodes Write(uint8_t deviceAddress, const uint8_t* data, size_t size, bool stop); - void FixHwFreezed(); - void ConfigurePins() const; + ErrorCodes Read(uint8_t deviceAddress, uint8_t registerAddress, uint8_t* buffer, size_t size) { + return static_cast(impl.Read(deviceAddress, registerAddress, buffer, size)); + } - NRF_TWIM_Type* twiBaseAddress; - SemaphoreHandle_t mutex = nullptr; - NRF_TWIM_Type* module; - uint32_t frequency; - uint8_t pinSda; - uint8_t pinScl; - static constexpr uint8_t maxDataSize {16}; - static constexpr uint8_t registerSize {1}; - uint8_t internalBuffer[maxDataSize + registerSize]; - uint32_t txStartedCycleCount = 0; - static constexpr uint32_t HwFreezedDelay {161000}; - }; + ErrorCodes Write(uint8_t deviceAddress, uint8_t registerAddress, const uint8_t* data, size_t size) { + return static_cast(impl.Write(deviceAddress, registerAddress, data, size)); + } + + void Sleep() { + impl.Sleep(); + } + + void Wakeup() { + impl.WakeUp(); + } + + private: + T& impl; + }; + } } } diff --git a/src/drivers/TwiMaster.cpp b/src/drivers/nrf52/TwiMaster.cpp similarity index 98% rename from src/drivers/TwiMaster.cpp rename to src/drivers/nrf52/TwiMaster.cpp index 25d23c28..179a01d3 100644 --- a/src/drivers/TwiMaster.cpp +++ b/src/drivers/nrf52/TwiMaster.cpp @@ -1,9 +1,9 @@ -#include "drivers/TwiMaster.h" +#include "drivers/nrf52/TwiMaster.h" #include #include #include -using namespace Pinetime::Drivers; +using namespace Pinetime::Drivers::Nrf52; // TODO use shortcut to automatically send STOP when receive LastTX, for example // TODO use DMA/IRQ diff --git a/src/drivers/nrf52/TwiMaster.h b/src/drivers/nrf52/TwiMaster.h new file mode 100644 index 00000000..64b4e63a --- /dev/null +++ b/src/drivers/nrf52/TwiMaster.h @@ -0,0 +1,44 @@ +#pragma once +#include +#include +#include // NRF_TWIM_Type +#include +#include "drivers/TwiMaster.h" + +namespace Pinetime { + namespace Drivers { + namespace Nrf52 { + class TwiMaster { + public: + enum class ErrorCodes { NoError, TransactionFailed }; + + TwiMaster(NRF_TWIM_Type* module, uint32_t frequency, uint8_t pinSda, uint8_t pinScl); + + void Init(); + ErrorCodes Read(uint8_t deviceAddress, uint8_t registerAddress, uint8_t* buffer, size_t size); + ErrorCodes Write(uint8_t deviceAddress, uint8_t registerAddress, const uint8_t* data, size_t size); + + void Sleep(); + void Wakeup(); + + private: + ErrorCodes Read(uint8_t deviceAddress, uint8_t* buffer, size_t size, bool stop); + ErrorCodes Write(uint8_t deviceAddress, const uint8_t* data, size_t size, bool stop); + void FixHwFreezed(); + void ConfigurePins() const; + + NRF_TWIM_Type* twiBaseAddress; + SemaphoreHandle_t mutex = nullptr; + NRF_TWIM_Type* module; + uint32_t frequency; + uint8_t pinSda; + uint8_t pinScl; + static constexpr uint8_t maxDataSize {16}; + static constexpr uint8_t registerSize {1}; + uint8_t internalBuffer[maxDataSize + registerSize]; + uint32_t txStartedCycleCount = 0; + static constexpr uint32_t HwFreezedDelay {161000}; + }; + } + } +} diff --git a/src/main.cpp b/src/main.cpp index 12a3f593..17fd1bd7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -88,7 +88,8 @@ Pinetime::Drivers::SpiNorFlash spiNorFlash {spiNorFlashImpl}; // respecting correct timings. According to erratas heet, this magic value makes it run // at ~390Khz with correct timings. static constexpr uint32_t MaxTwiFrequencyWithoutHardwareBug {0x06200000}; -Pinetime::Drivers::TwiMaster twiMaster {NRF_TWIM1, MaxTwiFrequencyWithoutHardwareBug, Pinetime::PinMap::TwiSda, Pinetime::PinMap::TwiScl}; +Pinetime::Drivers::Nrf52::TwiMaster twiMasterImpl {NRF_TWIM1, MaxTwiFrequencyWithoutHardwareBug, Pinetime::PinMap::TwiSda, Pinetime::PinMap::TwiScl}; +Pinetime::Drivers::TwiMaster twiMaster{twiMasterImpl}; Pinetime::Drivers::Cst816S touchPanel {twiMaster, touchPanelTwiAddress}; #ifdef PINETIME_IS_RECOVERY #include "displayapp/DummyLittleVgl.h" diff --git a/src/port/infinitime.h b/src/port/infinitime.h index 8cdef6bf..7957039b 100644 --- a/src/port/infinitime.h +++ b/src/port/infinitime.h @@ -1,12 +1,14 @@ #pragma once #include "drivers/Spi.h" #include "drivers/SpiMaster.h" +#include "drivers/TwiMaster.h" #include #include #ifdef TARGET_DEVICE_PINETIME #include #include +#include #include #endif @@ -15,6 +17,7 @@ namespace Pinetime { #ifdef TARGET_DEVICE_PINETIME using SpiMaster = Interface::SpiMaster; using Spi = Interface::Spi; + using TwiMaster = Interface::TwiMaster; using SpiNorFlash = Interface::SpiNorFlash; #else #error "No target device specified!" diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index ef631af7..b93d3da2 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -8,17 +8,11 @@ #include "displayapp/TouchEvents.h" #include "drivers/Cst816s.h" #include "drivers/St7789.h" -#include "drivers/InternalFlash.h" -#include "drivers/SpiMaster.h" -#include "drivers/SpiNorFlash.h" -#include "drivers/TwiMaster.h" #include "drivers/Hrs3300.h" #include "drivers/PinMap.h" #include "main.h" #include "BootErrors.h" -#include - using namespace Pinetime::System; namespace { diff --git a/src/systemtask/SystemTask.h b/src/systemtask/SystemTask.h index 9eb7f78a..6be19bc1 100644 --- a/src/systemtask/SystemTask.h +++ b/src/systemtask/SystemTask.h @@ -42,7 +42,6 @@ namespace Pinetime { namespace Drivers { class Cst816S; class St7789; - class TwiMaster; class Hrs3300; } namespace Controllers {