From 58f0121c1182c8cb8d765ccf4a3d44e1cafede0e Mon Sep 17 00:00:00 2001 From: Thomas Basler Date: Sat, 13 Jan 2024 14:33:55 +0100 Subject: [PATCH] Create separate definition for the 860MHz band of the CMT2300A This is required to support different countries in the future. The defines where moved to function to change the values dynamically --- ...mt2300a_params.h => cmt2300a_params_860.h} | 18 +++++---- lib/CMT2300a/cmt2300wrapper.cpp | 21 +++++++---- lib/CMT2300a/cmt2300wrapper.h | 5 ++- lib/Hoymiles/src/HoymilesRadio_CMT.cpp | 37 ++++++++----------- lib/Hoymiles/src/HoymilesRadio_CMT.h | 8 ++-- lib/Hoymiles/src/inverters/HMS_Abstract.cpp | 4 +- lib/Hoymiles/src/inverters/HMT_Abstract.cpp | 6 +-- 7 files changed, 50 insertions(+), 49 deletions(-) rename lib/CMT2300a/{cmt2300a_params.h => cmt2300a_params_860.h} (88%) diff --git a/lib/CMT2300a/cmt2300a_params.h b/lib/CMT2300a/cmt2300a_params_860.h similarity index 88% rename from lib/CMT2300a/cmt2300a_params.h rename to lib/CMT2300a/cmt2300a_params_860.h index 4e10f6a25..7c49c4f71 100644 --- a/lib/CMT2300a/cmt2300a_params.h +++ b/lib/CMT2300a/cmt2300a_params_860.h @@ -85,14 +85,16 @@ ; RSSI Offset = 0 ; RSSI Offset Sign = 0 */ -#ifndef __CMT2300A_PARAMS_H -#define __CMT2300A_PARAMS_H +#ifndef __CMT2300A_PARAMS_860_H +#define __CMT2300A_PARAMS_860_H #include "cmt2300a_defs.h" #include +#define CMT_BASE_FREQ_860 860000000 + /* [CMT Bank] with RSSI offset of +- 0 (and Tx power double bit not set) */ -static uint8_t g_cmt2300aCmtBank[CMT2300A_CMT_BANK_SIZE] = { +static uint8_t g_cmt2300aCmtBank_860[CMT2300A_CMT_BANK_SIZE] = { 0x00, 0x66, 0xEC, @@ -108,7 +110,7 @@ static uint8_t g_cmt2300aCmtBank[CMT2300A_CMT_BANK_SIZE] = { }; /* [System Bank] */ -static uint8_t g_cmt2300aSystemBank[CMT2300A_SYSTEM_BANK_SIZE] = { +static uint8_t g_cmt2300aSystemBank_860[CMT2300A_SYSTEM_BANK_SIZE] = { 0xAE, 0xE0, 0x35, @@ -124,7 +126,7 @@ static uint8_t g_cmt2300aSystemBank[CMT2300A_SYSTEM_BANK_SIZE] = { }; /* [Frequency Bank] 860 MHz */ -static uint8_t g_cmt2300aFrequencyBank[CMT2300A_FREQUENCY_BANK_SIZE] = { +static uint8_t g_cmt2300aFrequencyBank_860[CMT2300A_FREQUENCY_BANK_SIZE] = { 0x42, 0x32, 0xCF, @@ -136,7 +138,7 @@ static uint8_t g_cmt2300aFrequencyBank[CMT2300A_FREQUENCY_BANK_SIZE] = { }; /* [Data Rate Bank] */ -static uint8_t g_cmt2300aDataRateBank[CMT2300A_DATA_RATE_BANK_SIZE] = { +static uint8_t g_cmt2300aDataRateBank_860[CMT2300A_DATA_RATE_BANK_SIZE] = { 0xA6, 0xC9, 0x20, @@ -164,7 +166,7 @@ static uint8_t g_cmt2300aDataRateBank[CMT2300A_DATA_RATE_BANK_SIZE] = { }; /* [Baseband Bank] - EU */ -static uint8_t g_cmt2300aBasebandBank[CMT2300A_BASEBAND_BANK_SIZE] = { +static uint8_t g_cmt2300aBasebandBank_860[CMT2300A_BASEBAND_BANK_SIZE] = { 0x12, 0x1E, 0x00, @@ -197,7 +199,7 @@ static uint8_t g_cmt2300aBasebandBank[CMT2300A_BASEBAND_BANK_SIZE] = { }; /* [Tx Bank] 13 dBm */ -static uint8_t g_cmt2300aTxBank[CMT2300A_TX_BANK_SIZE] = { +static uint8_t g_cmt2300aTxBank_860[CMT2300A_TX_BANK_SIZE] = { 0x70, 0x4D, 0x06, diff --git a/lib/CMT2300a/cmt2300wrapper.cpp b/lib/CMT2300a/cmt2300wrapper.cpp index 21265f50a..5096d0768 100644 --- a/lib/CMT2300a/cmt2300wrapper.cpp +++ b/lib/CMT2300a/cmt2300wrapper.cpp @@ -1,10 +1,10 @@ // SPDX-License-Identifier: GPL-2.0-or-later /* - * Copyright (C) 2023 Thomas Basler and others + * Copyright (C) 2023-2024 Thomas Basler and others */ #include "cmt2300wrapper.h" #include "cmt2300a.h" -#include "cmt2300a_params.h" +#include "cmt2300a_params_860.h" CMT2300A::CMT2300A(uint8_t pin_sdio, uint8_t pin_clk, uint8_t pin_cs, uint8_t pin_fcs, uint32_t spi_speed) { @@ -242,6 +242,11 @@ bool CMT2300A::rxFifoAvailable() ) & CMT2300A_ReadReg(CMT2300A_CUS_INT_FLAG); } +uint32_t CMT2300A::getBaseFrequency() +{ + return CMT_BASE_FREQ_860; // from Frequency Bank in cmt2300a_params.h +} + void CMT2300A::flush_rx(void) { CMT2300A_ClearRxFifo(); @@ -261,12 +266,12 @@ bool CMT2300A::_init_radio() } /* config registers */ - CMT2300A_ConfigRegBank(CMT2300A_CMT_BANK_ADDR, g_cmt2300aCmtBank, CMT2300A_CMT_BANK_SIZE); - CMT2300A_ConfigRegBank(CMT2300A_SYSTEM_BANK_ADDR, g_cmt2300aSystemBank, CMT2300A_SYSTEM_BANK_SIZE); - CMT2300A_ConfigRegBank(CMT2300A_FREQUENCY_BANK_ADDR, g_cmt2300aFrequencyBank, CMT2300A_FREQUENCY_BANK_SIZE); - CMT2300A_ConfigRegBank(CMT2300A_DATA_RATE_BANK_ADDR, g_cmt2300aDataRateBank, CMT2300A_DATA_RATE_BANK_SIZE); - CMT2300A_ConfigRegBank(CMT2300A_BASEBAND_BANK_ADDR, g_cmt2300aBasebandBank, CMT2300A_BASEBAND_BANK_SIZE); - CMT2300A_ConfigRegBank(CMT2300A_TX_BANK_ADDR, g_cmt2300aTxBank, CMT2300A_TX_BANK_SIZE); + CMT2300A_ConfigRegBank(CMT2300A_CMT_BANK_ADDR, g_cmt2300aCmtBank_860, CMT2300A_CMT_BANK_SIZE); + CMT2300A_ConfigRegBank(CMT2300A_SYSTEM_BANK_ADDR, g_cmt2300aSystemBank_860, CMT2300A_SYSTEM_BANK_SIZE); + CMT2300A_ConfigRegBank(CMT2300A_FREQUENCY_BANK_ADDR, g_cmt2300aFrequencyBank_860, CMT2300A_FREQUENCY_BANK_SIZE); + CMT2300A_ConfigRegBank(CMT2300A_DATA_RATE_BANK_ADDR, g_cmt2300aDataRateBank_860, CMT2300A_DATA_RATE_BANK_SIZE); + CMT2300A_ConfigRegBank(CMT2300A_BASEBAND_BANK_ADDR, g_cmt2300aBasebandBank_860, CMT2300A_BASEBAND_BANK_SIZE); + CMT2300A_ConfigRegBank(CMT2300A_TX_BANK_ADDR, g_cmt2300aTxBank_860, CMT2300A_TX_BANK_SIZE); // xosc_aac_code[2:0] = 2 uint8_t tmp; diff --git a/lib/CMT2300a/cmt2300wrapper.h b/lib/CMT2300a/cmt2300wrapper.h index 5f76b06f2..601ebfc28 100644 --- a/lib/CMT2300a/cmt2300wrapper.h +++ b/lib/CMT2300a/cmt2300wrapper.h @@ -4,7 +4,6 @@ #include #define CMT2300A_ONE_STEP_SIZE 2500 // frequency channel step size for fast frequency hopping operation: One step size is 2.5 kHz. -#define CMT_BASE_FREQ 860000000 // from Frequency Bank in cmt2300a_params.h #define FH_OFFSET 100 // value * CMT2300A_ONE_STEP_SIZE = channel frequency offset #define CMT_SPI_SPEED 4000000 // 4 MHz @@ -86,6 +85,8 @@ class CMT2300A { bool rxFifoAvailable(); + uint32_t getBaseFrequency(); + /** * Empty the RX (receive) FIFO buffers. */ @@ -109,4 +110,4 @@ class CMT2300A { int8_t _pin_cs; int8_t _pin_fcs; uint32_t _spi_speed; -}; \ No newline at end of file +}; diff --git a/lib/Hoymiles/src/HoymilesRadio_CMT.cpp b/lib/Hoymiles/src/HoymilesRadio_CMT.cpp index d42b4d2e8..a06085cf1 100644 --- a/lib/Hoymiles/src/HoymilesRadio_CMT.cpp +++ b/lib/Hoymiles/src/HoymilesRadio_CMT.cpp @@ -1,6 +1,6 @@ // SPDX-License-Identifier: GPL-2.0-or-later /* - * Copyright (C) 2024 Thomas Basler and others + * Copyright (C) 2023-2024 Thomas Basler and others */ #include "HoymilesRadio_CMT.h" #include "Hoymiles.h" @@ -8,37 +8,28 @@ #include #define HOY_BOOT_FREQ 868000000 // Hoymiles boot/init frequency after power up inverter or connection lost for 15 min -#define HOY_BASE_FREQ 860000000 -// offset from initalized CMT base frequency to Hoy base frequency in channels -#define CMT_BASE_CH_OFFSET ((CMT_BASE_FREQ - HOY_BASE_FREQ) / CMT2300A_ONE_STEP_SIZE / FH_OFFSET) -// frequency can not be lower than actual initailized base freq + 250000 -#define MIN_FREQ ((HOY_BASE_FREQ + (CMT_BASE_CH_OFFSET > 1 ? CMT_BASE_CH_OFFSET : 1) * CMT2300A_ONE_STEP_SIZE * FH_OFFSET)) - -// =923500, 0xFF does not work -#define MAX_FREQ ((HOY_BASE_FREQ + 0xFE * CMT2300A_ONE_STEP_SIZE * FH_OFFSET)) - -uint32_t HoymilesRadio_CMT::getFrequencyFromChannel(const uint8_t channel) +uint32_t HoymilesRadio_CMT::getFrequencyFromChannel(const uint8_t channel) const { - return (CMT_BASE_FREQ + (CMT_BASE_CH_OFFSET + channel) * FH_OFFSET * CMT2300A_ONE_STEP_SIZE); + return (_radio->getBaseFrequency() + channel * getChannelWidth()); } -uint8_t HoymilesRadio_CMT::getChannelFromFrequency(const uint32_t frequency) +uint8_t HoymilesRadio_CMT::getChannelFromFrequency(const uint32_t frequency) const { - if ((frequency % 250000) != 0) { - Hoymiles.getMessageOutput()->printf("%.3f MHz is not divisible by 250 kHz!\r\n", frequency / 1000000.0); + if ((frequency % getChannelWidth()) != 0) { + Hoymiles.getMessageOutput()->printf("%.3f MHz is not divisible by %d kHz!\r\n", frequency / 1000000.0, getChannelWidth()); return 0xFF; // ERROR } - if (frequency < MIN_FREQ || frequency > MAX_FREQ) { + if (frequency < getMinFrequency() || frequency > getMaxFrequency()) { Hoymiles.getMessageOutput()->printf("%.2f MHz is out of Hoymiles/CMT range! (%.2f MHz - %.2f MHz)\r\n", - frequency / 1000000.0, MIN_FREQ / 1000000.0, MAX_FREQ / 1000000.0); + frequency / 1000000.0, getMinFrequency() / 1000000.0, getMaxFrequency() / 1000000.0); return 0xFF; // ERROR } if (frequency < 863000000 || frequency > 870000000) { Hoymiles.getMessageOutput()->printf("!!! caution: %.2f MHz is out of EU legal range! (863 - 870 MHz)\r\n", frequency / 1000000.0); } - return (frequency - CMT_BASE_FREQ) / CMT2300A_ONE_STEP_SIZE / FH_OFFSET - CMT_BASE_CH_OFFSET; // frequency to channel + return (frequency - _radio->getBaseFrequency()) / getChannelWidth(); // frequency to channel } bool HoymilesRadio_CMT::cmtSwitchDtuFreq(const uint32_t to_frequency) @@ -191,14 +182,16 @@ bool HoymilesRadio_CMT::isConnected() const return _radio->isChipConnected(); } -uint32_t HoymilesRadio_CMT::getMinFrequency() +uint32_t HoymilesRadio_CMT::getMinFrequency() const { - return MIN_FREQ; + // frequency can not be lower than actual initailized base freq + 250000 + return _radio->getBaseFrequency() + getChannelWidth(); } -uint32_t HoymilesRadio_CMT::getMaxFrequency() +uint32_t HoymilesRadio_CMT::getMaxFrequency() const { - return MAX_FREQ; + // =923500, 0xFF does not work + return _radio->getBaseFrequency() + 0xFE * getChannelWidth(); } uint32_t HoymilesRadio_CMT::getChannelWidth() diff --git a/lib/Hoymiles/src/HoymilesRadio_CMT.h b/lib/Hoymiles/src/HoymilesRadio_CMT.h index d9858c832..667f471bb 100644 --- a/lib/Hoymiles/src/HoymilesRadio_CMT.h +++ b/lib/Hoymiles/src/HoymilesRadio_CMT.h @@ -26,12 +26,12 @@ class HoymilesRadio_CMT : public HoymilesRadio { bool isConnected() const; - static uint32_t getMinFrequency(); - static uint32_t getMaxFrequency(); + uint32_t getMinFrequency() const; + uint32_t getMaxFrequency() const; static uint32_t getChannelWidth(); - static uint32_t getFrequencyFromChannel(const uint8_t channel); - static uint8_t getChannelFromFrequency(const uint32_t frequency); + uint32_t getFrequencyFromChannel(const uint8_t channel) const; + uint8_t getChannelFromFrequency(const uint32_t frequency) const; private: void ARDUINO_ISR_ATTR handleInt1(); diff --git a/lib/Hoymiles/src/inverters/HMS_Abstract.cpp b/lib/Hoymiles/src/inverters/HMS_Abstract.cpp index 235a9ba77..e75f99511 100644 --- a/lib/Hoymiles/src/inverters/HMS_Abstract.cpp +++ b/lib/Hoymiles/src/inverters/HMS_Abstract.cpp @@ -1,6 +1,6 @@ // SPDX-License-Identifier: GPL-2.0-or-later /* - * Copyright (C) 2023 Thomas Basler and others + * Copyright (C) 2023-2024 Thomas Basler and others */ #include "HMS_Abstract.h" #include "Hoymiles.h" @@ -19,7 +19,7 @@ bool HMS_Abstract::sendChangeChannelRequest() } auto cmdChannel = _radio->prepareCommand(); - cmdChannel->setChannel(HoymilesRadio_CMT::getChannelFromFrequency(Hoymiles.getRadioCmt()->getInverterTargetFrequency())); + cmdChannel->setChannel(Hoymiles.getRadioCmt()->getChannelFromFrequency(Hoymiles.getRadioCmt()->getInverterTargetFrequency())); cmdChannel->setTargetAddress(serial()); _radio->enqueCommand(cmdChannel); diff --git a/lib/Hoymiles/src/inverters/HMT_Abstract.cpp b/lib/Hoymiles/src/inverters/HMT_Abstract.cpp index 578233ee1..c4ebcf318 100644 --- a/lib/Hoymiles/src/inverters/HMT_Abstract.cpp +++ b/lib/Hoymiles/src/inverters/HMT_Abstract.cpp @@ -1,6 +1,6 @@ // SPDX-License-Identifier: GPL-2.0-or-later /* - * Copyright (C) 2023 Thomas Basler and others + * Copyright (C) 2023-2024 Thomas Basler and others */ #include "HMT_Abstract.h" #include "Hoymiles.h" @@ -21,9 +21,9 @@ bool HMT_Abstract::sendChangeChannelRequest() } auto cmdChannel = _radio->prepareCommand(); - cmdChannel->setChannel(HoymilesRadio_CMT::getChannelFromFrequency(Hoymiles.getRadioCmt()->getInverterTargetFrequency())); + cmdChannel->setChannel(Hoymiles.getRadioCmt()->getChannelFromFrequency(Hoymiles.getRadioCmt()->getInverterTargetFrequency())); cmdChannel->setTargetAddress(serial()); _radio->enqueCommand(cmdChannel); return true; -}; \ No newline at end of file +};