Skip to content

Commit

Permalink
Create separate definition for the 860MHz band of the CMT2300A
Browse files Browse the repository at this point in the history
This is required to support different countries in the future. The defines where moved to function to change the values dynamically
  • Loading branch information
tbnobody committed Jan 13, 2024
1 parent abc53f4 commit 58f0121
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 <stdint.h>

#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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
21 changes: 13 additions & 8 deletions lib/CMT2300a/cmt2300wrapper.cpp
Original file line number Diff line number Diff line change
@@ -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)
{
Expand Down Expand Up @@ -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();
Expand All @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions lib/CMT2300a/cmt2300wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <stdint.h>

#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

Expand Down Expand Up @@ -86,6 +85,8 @@ class CMT2300A {

bool rxFifoAvailable();

uint32_t getBaseFrequency();

/**
* Empty the RX (receive) FIFO buffers.
*/
Expand All @@ -109,4 +110,4 @@ class CMT2300A {
int8_t _pin_cs;
int8_t _pin_fcs;
uint32_t _spi_speed;
};
};
37 changes: 15 additions & 22 deletions lib/Hoymiles/src/HoymilesRadio_CMT.cpp
Original file line number Diff line number Diff line change
@@ -1,44 +1,35 @@
// 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"
#include "crc.h"
#include <FunctionalInterrupt.h>

#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)
Expand Down Expand Up @@ -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()
Expand Down
8 changes: 4 additions & 4 deletions lib/Hoymiles/src/HoymilesRadio_CMT.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions lib/Hoymiles/src/inverters/HMS_Abstract.cpp
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -19,7 +19,7 @@ bool HMS_Abstract::sendChangeChannelRequest()
}

auto cmdChannel = _radio->prepareCommand<ChannelChangeCommand>();
cmdChannel->setChannel(HoymilesRadio_CMT::getChannelFromFrequency(Hoymiles.getRadioCmt()->getInverterTargetFrequency()));
cmdChannel->setChannel(Hoymiles.getRadioCmt()->getChannelFromFrequency(Hoymiles.getRadioCmt()->getInverterTargetFrequency()));
cmdChannel->setTargetAddress(serial());
_radio->enqueCommand(cmdChannel);

Expand Down
6 changes: 3 additions & 3 deletions lib/Hoymiles/src/inverters/HMT_Abstract.cpp
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -21,9 +21,9 @@ bool HMT_Abstract::sendChangeChannelRequest()
}

auto cmdChannel = _radio->prepareCommand<ChannelChangeCommand>();
cmdChannel->setChannel(HoymilesRadio_CMT::getChannelFromFrequency(Hoymiles.getRadioCmt()->getInverterTargetFrequency()));
cmdChannel->setChannel(Hoymiles.getRadioCmt()->getChannelFromFrequency(Hoymiles.getRadioCmt()->getInverterTargetFrequency()));
cmdChannel->setTargetAddress(serial());
_radio->enqueCommand(cmdChannel);

return true;
};
};

0 comments on commit 58f0121

Please sign in to comment.