From 2e746ea560c69ea7c409f66c11ea060e2133fdae Mon Sep 17 00:00:00 2001 From: Reinhold Gschweicher Date: Wed, 3 May 2023 21:06:17 +0200 Subject: [PATCH] use drivers/Hrs3300.h from InfiniTime The Hrs3300.h driver can now be used directly from InfiniTime. Some other improvement made that possible. One less simulator file, one less file to keep updated. Win-Win (for me at least). --- CMakeLists.txt | 4 +- sim/drivers/Hrs3300.cpp | 105 ---------------------------------------- sim/drivers/Hrs3300.h | 46 ------------------ 3 files changed, 2 insertions(+), 153 deletions(-) delete mode 100644 sim/drivers/Hrs3300.cpp delete mode 100644 sim/drivers/Hrs3300.h diff --git a/CMakeLists.txt b/CMakeLists.txt index f0ff06e..182714e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -138,8 +138,6 @@ target_sources(infinisim PUBLIC sim/drivers/Bma421.cpp sim/drivers/Cst816s.h sim/drivers/Cst816s.cpp - sim/drivers/Hrs3300.h - sim/drivers/Hrs3300.cpp sim/drivers/SpiMaster.h sim/drivers/SpiMaster.cpp sim/drivers/TwiMaster.h @@ -199,6 +197,8 @@ target_sources(infinisim PUBLIC ${InfiniTime_DIR}/src/components/fs/FS.cpp ${InfiniTime_DIR}/src/components/motor/MotorController.h ${InfiniTime_DIR}/src/components/motor/MotorController.cpp + ${InfiniTime_DIR}/src/drivers/Hrs3300.h + ${InfiniTime_DIR}/src/drivers/Hrs3300.cpp ${InfiniTime_DIR}/src/drivers/PinMap.h ${InfiniTime_DIR}/src/drivers/Spi.h ${InfiniTime_DIR}/src/drivers/Spi.cpp diff --git a/sim/drivers/Hrs3300.cpp b/sim/drivers/Hrs3300.cpp deleted file mode 100644 index 4374dd4..0000000 --- a/sim/drivers/Hrs3300.cpp +++ /dev/null @@ -1,105 +0,0 @@ -/* - SPDX-License-Identifier: LGPL-3.0-or-later - Original work Copyright (C) 2020 Daniel Thompson - C++ port Copyright (C) 2021 Jean-François Milants -*/ - -#include "drivers/Hrs3300.h" -#include -#include - -#include -#include -#include - -using namespace Pinetime::Drivers; -/** Driver for the HRS3300 heart rate sensor. - * Original implementation from wasp-os : https://github.com/daniel-thompson/wasp-os/blob/master/wasp/drivers/hrs3300.py - */ -Hrs3300::Hrs3300(TwiMaster& twiMaster, uint8_t twiAddress) : twiMaster {twiMaster}, twiAddress {twiAddress} { -} - -void Hrs3300::Init() { - nrf_gpio_cfg_input(30, NRF_GPIO_PIN_NOPULL); - - Disable(); -// vTaskDelay(100); - - // HRS disabled, 12.5 ms wait time between cycles, (partly) 20mA drive - WriteRegister(static_cast(Registers::Enable), 0x60); - - // (partly) 20mA drive, power on, "magic" (datasheet says both - // "reserved" and "set low nibble to 8" but 0xe gives better results - // and is used by at least two other HRS3300 drivers - WriteRegister(static_cast(Registers::PDriver), 0x6E); - - // HRS and ALS both in 16-bit mode - WriteRegister(static_cast(Registers::Res), 0x88); - - // 64x gain - WriteRegister(static_cast(Registers::Hgain), 0x10); -} - -void Hrs3300::Enable() { - NRF_LOG_INFO("ENABLE"); - auto value = ReadRegister(static_cast(Registers::Enable)); - value |= 0x80; - WriteRegister(static_cast(Registers::Enable), value); -} - -void Hrs3300::Disable() { - NRF_LOG_INFO("DISABLE"); - auto value = ReadRegister(static_cast(Registers::Enable)); - value &= ~0x80; - WriteRegister(static_cast(Registers::Enable), value); -} - -uint16_t Hrs3300::ReadHrs() { - auto m = ReadRegister(static_cast(Registers::C0DataM)); - auto h = ReadRegister(static_cast(Registers::C0DataH)); - auto l = ReadRegister(static_cast(Registers::C0dataL)); - return (m << 8) | ((h & 0x0f) << 4) | (l & 0x0f) | ((l & 0x30) << 12); -} - -uint16_t Hrs3300::ReadAls() { - auto m = ReadRegister(static_cast(Registers::C1dataM)); - auto h = ReadRegister(static_cast(Registers::C1dataH)); - auto l = ReadRegister(static_cast(Registers::C1dataL)); - return (m << 3) | ((h & 0x3f) << 11) | (l & 0x07); -} - -void Hrs3300::SetGain(uint8_t gain) { - constexpr uint8_t maxGain = 64U; - gain = std::min(gain, maxGain); - uint8_t hgain = 0; - while ((1 << hgain) < gain) { - ++hgain; - } - - WriteRegister(static_cast(Registers::Hgain), hgain << 2); -} - -void Hrs3300::SetDrive(uint8_t drive) { - auto en = ReadRegister(static_cast(Registers::Enable)); - auto pd = ReadRegister(static_cast(Registers::PDriver)); - - en = (en & 0xf7) | ((drive & 2) << 2); - pd = (pd & 0xbf) | ((drive & 1) << 6); - - WriteRegister(static_cast(Registers::Enable), en); - WriteRegister(static_cast(Registers::PDriver), pd); -} - -void Hrs3300::WriteRegister(uint8_t reg, uint8_t data) { - auto ret = twiMaster.Write(twiAddress, reg, &data, 1); - if (ret != TwiMaster::ErrorCodes::NoError) - NRF_LOG_INFO("WRITE ERROR"); -} - -uint8_t Hrs3300::ReadRegister(uint8_t reg) { - uint8_t value; - auto ret = twiMaster.Read(twiAddress, reg, &value, 1); - if (ret != TwiMaster::ErrorCodes::NoError) - NRF_LOG_INFO("READ ERROR"); - return value; -} diff --git a/sim/drivers/Hrs3300.h b/sim/drivers/Hrs3300.h deleted file mode 100644 index 01310c6..0000000 --- a/sim/drivers/Hrs3300.h +++ /dev/null @@ -1,46 +0,0 @@ -#pragma once - -#include "drivers/TwiMaster.h" - -namespace Pinetime { - namespace Drivers { - class Hrs3300 { - public: - enum class Registers : uint8_t { - Id = 0x00, - Enable = 0x01, - EnableHen = 0x80, - C1dataM = 0x08, - C0DataM = 0x09, - C0DataH = 0x0a, - PDriver = 0x0c, - C1dataH = 0x0d, - C1dataL = 0x0e, - C0dataL = 0x0f, - Res = 0x16, - Hgain = 0x17 - }; - - Hrs3300(TwiMaster& twiMaster, uint8_t twiAddress); - Hrs3300(const Hrs3300&) = delete; - Hrs3300& operator=(const Hrs3300&) = delete; - Hrs3300(Hrs3300&&) = delete; - Hrs3300& operator=(Hrs3300&&) = delete; - - void Init(); - void Enable(); - void Disable(); - uint16_t ReadHrs(); - uint16_t ReadAls(); - void SetGain(uint8_t gain); - void SetDrive(uint8_t drive); - - private: - TwiMaster& twiMaster; - uint8_t twiAddress; - - void WriteRegister(uint8_t reg, uint8_t data); - uint8_t ReadRegister(uint8_t reg); - }; - } -}