From fc3a85abf9f30885860b2f5231d7830d6b7e3248 Mon Sep 17 00:00:00 2001 From: jgromes Date: Sat, 1 Oct 2022 22:54:36 +0200 Subject: [PATCH] [SX128x] Added post-transaction error checking --- src/modules/SX128x/SX128x.cpp | 76 ++++++++++++++++++++++++++++++++--- src/modules/SX128x/SX128x.h | 10 +++++ 2 files changed, 81 insertions(+), 5 deletions(-) diff --git a/src/modules/SX128x/SX128x.cpp b/src/modules/SX128x/SX128x.cpp index 39df7628..188fe127 100644 --- a/src/modules/SX128x/SX128x.cpp +++ b/src/modules/SX128x/SX128x.cpp @@ -1243,6 +1243,10 @@ uint8_t SX128x::randomByte() { return(0); } +int16_t SX128x::getLastError() { + return(_lastError); +} + #if !defined(RADIOLIB_EXCLUDE_DIRECT_RECEIVE) void SX128x::setDirectAction(void (*func)(void)) { // SX128x is unable to perform direct mode reception @@ -1270,7 +1274,12 @@ int16_t SX128x::writeRegister(uint16_t addr, uint8_t* data, uint8_t numBytes) { int16_t SX128x::readRegister(uint16_t addr, uint8_t* data, uint8_t numBytes) { uint8_t cmd[] = { RADIOLIB_SX128X_CMD_READ_REGISTER, (uint8_t)((addr >> 8) & 0xFF), (uint8_t)(addr & 0xFF) }; - return(SX128x::SPItransfer(cmd, 3, false, NULL, data, numBytes, true)); + int16_t state = SX128x::SPItransfer(cmd, 3, false, NULL, data, numBytes, true); + RADIOLIB_ASSERT(state); + + // check the status + state = checkCommandResult(); + return(state); } int16_t SX128x::writeBuffer(uint8_t* data, uint8_t numBytes, uint8_t offset) { @@ -1404,20 +1413,77 @@ int16_t SX128x::config(uint8_t modem) { return(RADIOLIB_ERR_NONE); } +int16_t SX128x::checkCommandResult() { + int16_t state = RADIOLIB_ERR_NONE; + + #if defined(RADIOLIB_SPI_PARANOID) + // get the status + uint8_t spiStatus = 0; + uint8_t cmd = RADIOLIB_SX128X_CMD_GET_STATUS; + state = SX128x::SPItransfer(&cmd, 1, false, NULL, &spiStatus, 1, true); + RADIOLIB_ASSERT(state); + + // translate to RadioLib status code + switch(spiStatus) { + case RADIOLIB_SX128X_STATUS_CMD_TIMEOUT: + _lastError = RADIOLIB_ERR_SPI_CMD_TIMEOUT; + break; + case RADIOLIB_SX128X_STATUS_CMD_ERROR: + _lastError = RADIOLIB_ERR_SPI_CMD_INVALID; + break; + case RADIOLIB_SX128X_STATUS_CMD_FAILED: + _lastError = RADIOLIB_ERR_SPI_CMD_FAILED; + break; + case RADIOLIB_SX128X_STATUS_SPI_FAILED: + _lastError = RADIOLIB_ERR_CHIP_NOT_FOUND; + break; + default: + _lastError = RADIOLIB_ERR_NONE; + } + + #endif + + return(state); +} + int16_t SX128x::SPIwriteCommand(uint8_t* cmd, uint8_t cmdLen, uint8_t* data, uint8_t numBytes, bool waitForBusy) { - return(SX128x::SPItransfer(cmd, cmdLen, true, data, NULL, numBytes, waitForBusy)); + // send the command + int16_t state = SX128x::SPItransfer(cmd, cmdLen, true, data, NULL, numBytes, waitForBusy); + RADIOLIB_ASSERT(state); + + // check the status + state = checkCommandResult(); + return(state); } int16_t SX128x::SPIwriteCommand(uint8_t cmd, uint8_t* data, uint8_t numBytes, bool waitForBusy) { - return(SX128x::SPItransfer(&cmd, 1, true, data, NULL, numBytes, waitForBusy)); + // send the command + int16_t state = SX128x::SPItransfer(&cmd, 1, true, data, NULL, numBytes, waitForBusy); + RADIOLIB_ASSERT(state); + + // check the status + state = checkCommandResult(); + return(state); } int16_t SX128x::SPIreadCommand(uint8_t* cmd, uint8_t cmdLen, uint8_t* data, uint8_t numBytes, bool waitForBusy) { - return(SX128x::SPItransfer(cmd, cmdLen, false, NULL, data, numBytes, waitForBusy)); + // send the command + int16_t state = SX128x::SPItransfer(cmd, cmdLen, false, NULL, data, numBytes, waitForBusy); + RADIOLIB_ASSERT(state); + + // check the status + state = checkCommandResult(); + return(state); } int16_t SX128x::SPIreadCommand(uint8_t cmd, uint8_t* data, uint8_t numBytes, bool waitForBusy) { - return(SX128x::SPItransfer(&cmd, 1, false, NULL, data, numBytes, waitForBusy)); + // send the command + int16_t state = SX128x::SPItransfer(&cmd, 1, false, NULL, data, numBytes, waitForBusy); + RADIOLIB_ASSERT(state); + + // check the status + state = checkCommandResult(); + return(state); } int16_t SX128x::SPItransfer(uint8_t* cmd, uint8_t cmdLen, bool write, uint8_t* dataOut, uint8_t* dataIn, uint8_t numBytes, bool waitForBusy, uint32_t timeout) { diff --git a/src/modules/SX128x/SX128x.h b/src/modules/SX128x/SX128x.h index e25bf3bb..d3006ab0 100644 --- a/src/modules/SX128x/SX128x.h +++ b/src/modules/SX128x/SX128x.h @@ -819,6 +819,13 @@ class SX128x: public PhysicalLayer { */ uint8_t randomByte(); + /*! + \brief Get the last recorded transaction error. + + \returns \ref status_codes + */ + int16_t getLastError(); + #if !defined(RADIOLIB_EXCLUDE_DIRECT_RECEIVE) /*! \brief Dummy method, to ensure PhysicalLayer compatibility. @@ -903,7 +910,10 @@ class SX128x: public PhysicalLayer { // cached BLE parameters uint8_t _connectionState = 0, _crcBLE = 0, _bleTestPayload = 0; + int16_t _lastError = RADIOLIB_ERR_NONE; + int16_t config(uint8_t modem); + int16_t checkCommandResult(); }; #endif