Skip to content

Commit

Permalink
Implemented blocking write method in CMT2300 driver and use it in sen…
Browse files Browse the repository at this point in the history
…dEsbPacket.
  • Loading branch information
tbnobody committed Apr 15, 2023
1 parent 6331210 commit 5b648b6
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
35 changes: 35 additions & 0 deletions lib/CMT2300a/cmt2300wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,41 @@ bool CMT2300A::isChipConnected()
return CMT2300A_IsExist();
}

bool CMT2300A::write(const uint8_t* buf, uint8_t len)
{
CMT2300A_GoStby();
CMT2300A_ClearInterruptFlags();

/* Must clear FIFO after enable SPI to read or write the FIFO */
CMT2300A_EnableWriteFifo();
CMT2300A_ClearTxFifo();

CMT2300A_WriteReg(CMT2300A_CUS_PKT15, len); // set Tx length
/* The length need be smaller than 32 */
CMT2300A_WriteFifo(buf, len);

if (!(CMT2300A_ReadReg(CMT2300A_CUS_FIFO_FLAG) & CMT2300A_MASK_TX_FIFO_NMTY_FLG)) {
return false;
}

if (!CMT2300A_GoTx()) {
return false;
}

uint32_t timer = millis();

while (!(CMT2300A_MASK_TX_DONE_FLG & CMT2300A_ReadReg(CMT2300A_CUS_INT_CLR1))) {
if (millis() - timer > 95) {
return false;
}
}

CMT2300A_ClearInterruptFlags();
CMT2300A_GoSleep();

return true;
}

bool CMT2300A::setPALevel(int8_t level)
{
uint16_t Tx_dBm_word;
Expand Down
2 changes: 2 additions & 0 deletions lib/CMT2300a/cmt2300wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class CMT2300A {
*/
bool isChipConnected();

bool write(const uint8_t* buf, uint8_t len);

bool setPALevel(int8_t level);

private:
Expand Down
10 changes: 7 additions & 3 deletions lib/Hoymiles/src/HoymilesRadio_CMT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -485,11 +485,15 @@ void HoymilesRadio_CMT::sendEsbPacket(CommandAbstract* cmd)
cmd->getCommandName().c_str(), cmtChToFreq(cmtCurrentCh).c_str());
cmd->dumpDataPayload(Hoymiles.getMessageOutput());

// Still here for to handle CMD56 correctly (inverter serial etc.)
memcpy(cmtTxBuffer, cmd->getDataPayload(), cmd->getDataSize());
cmtTxLength = cmd->getDataSize();
_txTimeout.set(100);

cmtNextState = CMT_STATE_TX_START;
if (_radio->write(cmd->getDataPayload(), cmd->getDataSize())) {
_packetSent = false; // still bad hack, to be removed
cmtNextState = CMT_STATE_RX_START;
} else {
Hoymiles.getMessageOutput()->println("TX SPI Timeout");
}

_busyFlag = true;
_rxTimeout.set(cmd->getTimeout());
Expand Down

0 comments on commit 5b648b6

Please sign in to comment.