From c641099e9f1d39b41479492c804890420c589487 Mon Sep 17 00:00:00 2001 From: jgromes Date: Sat, 11 May 2024 16:49:51 +0100 Subject: [PATCH] [SX127x] Fixed issues found by cppcheck --- src/modules/SX127x/SX1272.h | 8 +++---- src/modules/SX127x/SX1273.h | 2 +- src/modules/SX127x/SX1276.h | 4 ++-- src/modules/SX127x/SX1277.h | 4 ++-- src/modules/SX127x/SX1278.h | 8 +++---- src/modules/SX127x/SX1279.h | 4 ++-- src/modules/SX127x/SX127x.cpp | 22 ++++++++++++------- src/modules/SX127x/SX127x.h | 40 +++++++++++++++++------------------ 8 files changed, 49 insertions(+), 43 deletions(-) diff --git a/src/modules/SX127x/SX1272.h b/src/modules/SX127x/SX1272.h index 0f9a1717..292bfd79 100644 --- a/src/modules/SX127x/SX1272.h +++ b/src/modules/SX127x/SX1272.h @@ -100,7 +100,7 @@ class SX1272: public SX127x { \brief Default constructor. Called from Arduino sketch when creating new LoRa instance. \param mod Instance of Module that will be used to communicate with the %LoRa chip. */ - SX1272(Module* mod); + SX1272(Module* mod); // cppcheck-suppress noExplicitConstructor // basic methods @@ -146,7 +146,7 @@ class SX1272: public SX127x { \param freq Carrier frequency to be set in MHz. \returns \ref status_codes */ - int16_t setFrequency(float freq); + int16_t setFrequency(float freq) override; /*! \brief Sets %LoRa link bandwidth. Allowed values are 125, 250 and 500 kHz. Only available in %LoRa mode. @@ -254,7 +254,7 @@ class SX1272: public SX127x { Overload with packet mode enabled for PhysicalLayer compatibility. \returns RSSI value in dBm. */ - float getRSSI(); + float getRSSI() override; /*! \brief Gets recorded signal strength indicator. @@ -311,7 +311,7 @@ class SX1272: public SX127x { int16_t setHeaderType(uint8_t headerType, size_t len = 0xFF); int16_t configFSK(); - void errataFix(bool rx); + void errataFix(bool rx) override; #if !RADIOLIB_GODMODE private: diff --git a/src/modules/SX127x/SX1273.h b/src/modules/SX127x/SX1273.h index 610bbccf..8e7ab1ca 100644 --- a/src/modules/SX127x/SX1273.h +++ b/src/modules/SX127x/SX1273.h @@ -20,7 +20,7 @@ class SX1273: public SX1272 { \brief Default constructor. Called from Arduino sketch when creating new LoRa instance. \param mod Instance of Module that will be used to communicate with the %LoRa chip. */ - SX1273(Module* mod); + SX1273(Module* mod); // cppcheck-suppress noExplicitConstructor // basic methods diff --git a/src/modules/SX127x/SX1276.h b/src/modules/SX127x/SX1276.h index c15f9812..89ab9f9f 100644 --- a/src/modules/SX127x/SX1276.h +++ b/src/modules/SX127x/SX1276.h @@ -20,7 +20,7 @@ class SX1276: public SX1278 { \brief Default constructor. Called from Arduino sketch when creating new LoRa instance. \param mod Instance of Module that will be used to communicate with the %LoRa chip. */ - SX1276(Module* mod); + SX1276(Module* mod); // cppcheck-suppress noExplicitConstructor // basic methods @@ -61,7 +61,7 @@ class SX1276: public SX1278 { \param freq Carrier frequency to be set in MHz. \returns \ref status_codes */ - int16_t setFrequency(float freq); + int16_t setFrequency(float freq) override; #if !RADIOLIB_GODMODE private: diff --git a/src/modules/SX127x/SX1277.h b/src/modules/SX127x/SX1277.h index 1d334cdc..c7efd95b 100644 --- a/src/modules/SX127x/SX1277.h +++ b/src/modules/SX127x/SX1277.h @@ -20,7 +20,7 @@ class SX1277: public SX1278 { \brief Default constructor. Called from Arduino sketch when creating new LoRa instance. \param mod Instance of Module that will be used to communicate with the %LoRa chip. */ - SX1277(Module* mod); + SX1277(Module* mod); // cppcheck-suppress noExplicitConstructor // basic methods @@ -61,7 +61,7 @@ class SX1277: public SX1278 { \param freq Carrier frequency to be set in MHz. \returns \ref status_codes */ - int16_t setFrequency(float freq); + int16_t setFrequency(float freq) override; /*! \brief Sets %LoRa link spreading factor. Allowed values range from 6 to 9. Only available in %LoRa mode. diff --git a/src/modules/SX127x/SX1278.h b/src/modules/SX127x/SX1278.h index a54623a9..3c2b0088 100644 --- a/src/modules/SX127x/SX1278.h +++ b/src/modules/SX127x/SX1278.h @@ -111,7 +111,7 @@ class SX1278: public SX127x { \brief Default constructor. Called from Arduino sketch when creating new LoRa instance. \param mod Instance of Module that will be used to communicate with the %LoRa chip. */ - SX1278(Module* mod); + SX1278(Module* mod); // cppcheck-suppress noExplicitConstructor // basic methods @@ -157,7 +157,7 @@ class SX1278: public SX127x { \param freq Carrier frequency to be set in MHz. \returns \ref status_codes */ - int16_t setFrequency(float freq); + int16_t setFrequency(float freq) override; /*! \brief Sets %LoRa link bandwidth. Allowed values are 10.4, 15.6, 20.8, 31.25, 41.7, 62.5, 125, 250 and 500 kHz. Only available in %LoRa mode. @@ -266,7 +266,7 @@ class SX1278: public SX127x { Overload with packet mode enabled for PhysicalLayer compatibility. \returns RSSI value in dBm. */ - float getRSSI(); + float getRSSI() override; /*! \brief Gets recorded signal strength indicator. @@ -323,7 +323,7 @@ class SX1278: public SX127x { int16_t setHeaderType(uint8_t headerType, size_t len = 0xFF); int16_t configFSK(); - void errataFix(bool rx); + void errataFix(bool rx) override; #if !RADIOLIB_GODMODE private: diff --git a/src/modules/SX127x/SX1279.h b/src/modules/SX127x/SX1279.h index e802e9c4..1dceb423 100644 --- a/src/modules/SX127x/SX1279.h +++ b/src/modules/SX127x/SX1279.h @@ -20,7 +20,7 @@ class SX1279: public SX1278 { \brief Default constructor. Called from Arduino sketch when creating new LoRa instance. \param mod Instance of Module that will be used to communicate with the %LoRa chip. */ - SX1279(Module* mod); + SX1279(Module* mod); // cppcheck-suppress noExplicitConstructor // basic methods @@ -61,7 +61,7 @@ class SX1279: public SX1278 { \param freq Carrier frequency to be set in MHz. \returns \ref status_codes */ - int16_t setFrequency(float freq); + int16_t setFrequency(float freq) override; #if !RADIOLIB_GODMODE private: diff --git a/src/modules/SX127x/SX127x.cpp b/src/modules/SX127x/SX127x.cpp index 6c9c6598..13096257 100644 --- a/src/modules/SX127x/SX127x.cpp +++ b/src/modules/SX127x/SX127x.cpp @@ -905,13 +905,13 @@ int16_t SX127x::setBitRateCommon(float br, uint8_t fracRegAddr) { RADIOLIB_ASSERT(state); // set bit rate - uint16_t bitRate = (RADIOLIB_SX127X_CRYSTAL_FREQ * 1000.0) / br; - state = this->mod->SPIsetRegValue(RADIOLIB_SX127X_REG_BITRATE_MSB, (bitRate & 0xFF00) >> 8, 7, 0); - state |= this->mod->SPIsetRegValue(RADIOLIB_SX127X_REG_BITRATE_LSB, bitRate & 0x00FF, 7, 0); + uint16_t bitRateRaw = (RADIOLIB_SX127X_CRYSTAL_FREQ * 1000.0) / br; + state = this->mod->SPIsetRegValue(RADIOLIB_SX127X_REG_BITRATE_MSB, (bitRateRaw & 0xFF00) >> 8, 7, 0); + state |= this->mod->SPIsetRegValue(RADIOLIB_SX127X_REG_BITRATE_LSB, bitRateRaw & 0x00FF, 7, 0); // set fractional part of bit rate if(!ookEnabled) { - float bitRateRem = ((RADIOLIB_SX127X_CRYSTAL_FREQ * 1000.0) / (float)br) - (float)bitRate; + float bitRateRem = ((RADIOLIB_SX127X_CRYSTAL_FREQ * 1000.0) / (float)br) - (float)bitRateRaw; uint8_t bitRateFrac = bitRateRem * 16; state |= this->mod->SPIsetRegValue(fracRegAddr, bitRateFrac, 7, 0); } @@ -1335,8 +1335,14 @@ int16_t SX127x::setRSSIConfig(uint8_t smoothingSamples, int8_t offset) { RADIOLIB_CHECK_RANGE(offset, -16, 15, RADIOLIB_ERR_INVALID_RSSI_OFFSET); + // calculate the two's complement + uint8_t offsetRaw = RADIOLIB_ABS(offset); + offsetRaw ^= 0x1F; + offsetRaw += 1; + offsetRaw &= 0x1F; + // set new register values - state = this->mod->SPIsetRegValue(RADIOLIB_SX127X_REG_RSSI_CONFIG, offset << 3, 7, 3); + state = this->mod->SPIsetRegValue(RADIOLIB_SX127X_REG_RSSI_CONFIG, offsetRaw << 3, 7, 3); state |= this->mod->SPIsetRegValue(RADIOLIB_SX127X_REG_RSSI_CONFIG, smoothingSamples, 2, 0); return(state); } @@ -1538,7 +1544,7 @@ int16_t SX127x::setPacketMode(uint8_t mode, uint8_t len) { return(state); } -bool SX127x::findChip(uint8_t* vers, uint8_t num) { +bool SX127x::findChip(const uint8_t* vers, uint8_t num) { uint8_t i = 0; bool flagFound = false; while((i < 10) && !flagFound) { @@ -1547,8 +1553,8 @@ bool SX127x::findChip(uint8_t* vers, uint8_t num) { // check version register int16_t version = getChipVersion(); - for(uint8_t i = 0; i < num; i++) { - if(version == vers[i]) { + for(uint8_t j = 0; j < num; j++) { + if(version == vers[j]) { flagFound = true; break; } diff --git a/src/modules/SX127x/SX127x.h b/src/modules/SX127x/SX127x.h index d9f3f870..4b692667 100644 --- a/src/modules/SX127x/SX127x.h +++ b/src/modules/SX127x/SX127x.h @@ -594,7 +594,7 @@ class SX127x: public PhysicalLayer { \brief Default constructor. Called internally when creating new LoRa instance. \param mod Instance of Module that will be used to communicate with the %LoRa chip. */ - SX127x(Module* mod); + SX127x(Module* mod); // cppcheck-suppress noExplicitConstructor // basic methods @@ -655,7 +655,7 @@ class SX127x: public PhysicalLayer { %Module will wake up automatically when methods like transmit or receive are called. \returns \ref status_codes */ - int16_t sleep(); + int16_t sleep() override; /*! \brief Sets the %LoRa module to standby. @@ -721,34 +721,34 @@ class SX127x: public PhysicalLayer { \brief Sets interrupt service routine to call when a packet is received. \param func ISR to call. */ - void setPacketReceivedAction(void (*func)(void)); + void setPacketReceivedAction(void (*func)(void)) override; /*! \brief Clears interrupt service routine to call when a packet is received. */ - void clearPacketReceivedAction(); + void clearPacketReceivedAction() override; /*! \brief Sets interrupt service routine to call when a packet is sent. \param func ISR to call. */ - void setPacketSentAction(void (*func)(void)); + void setPacketSentAction(void (*func)(void)) override; /*! \brief Clears interrupt service routine to call when a packet is sent. */ - void clearPacketSentAction(); + void clearPacketSentAction() override; /*! \brief Sets interrupt service routine to call when a channel scan is finished. \param func ISR to call. */ - void setChannelScanAction(void (*func)(void)); + void setChannelScanAction(void (*func)(void)) override; /*! \brief Clears interrupt service routine to call when a channel scan is finished. */ - void clearChannelScanAction(); + void clearChannelScanAction() override; /*! \brief Set interrupt service routine function to call when FIFO is empty. @@ -810,7 +810,7 @@ class SX127x: public PhysicalLayer { Implemented for compatibility with PhysicalLayer. \returns \ref status_codes */ - int16_t startReceive(); + int16_t startReceive() override; /*! \brief Interrupt-driven receive method. DIO0 will be activated when full valid packet is received. @@ -832,7 +832,7 @@ class SX127x: public PhysicalLayer { \param len Expected length of packet to be received. Required for LoRa spreading factor 6. \returns \ref status_codes */ - int16_t startReceive(uint32_t timeout, uint32_t irqFlags, uint32_t irqMask, size_t len); + int16_t startReceive(uint32_t timeout, uint32_t irqFlags, uint32_t irqMask, size_t len) override; /*! \brief Reads data that was received after calling startReceive method. When the packet length is not known in advance, @@ -904,7 +904,7 @@ class SX127x: public PhysicalLayer { \brief Gets signal-to-noise ratio of the latest received packet. Only available in LoRa mode. \returns Last packet signal-to-noise ratio (SNR). */ - float getSNR(); + float getSNR() override; /*! \brief Get data rate of the latest transmitted packet. @@ -1062,7 +1062,7 @@ class SX127x: public PhysicalLayer { \param timeoutUs Timeout in microseconds to listen for \returns Timeout value in a unit that is specific for the used module */ - RadioLibTime_t calculateRxTimeout(RadioLibTime_t timeoutUs); + RadioLibTime_t calculateRxTimeout(RadioLibTime_t timeoutUs) override; /*! \brief Create the flags that make up RxDone and RxTimeout used for receiving downlinks @@ -1070,13 +1070,13 @@ class SX127x: public PhysicalLayer { \param irqMask Mask indicating which IRQ triggers a DIO \returns \ref status_codes */ - int16_t irqRxDoneRxTimeout(uint32_t &irqFlags, uint32_t &irqMask); + int16_t irqRxDoneRxTimeout(uint32_t &irqFlags, uint32_t &irqMask) override; /*! \brief Check whether the IRQ bit for RxTimeout is set \returns Whether RxTimeout IRQ is set */ - bool isRxTimeout(); + bool isRxTimeout() override; /*! \brief Enable CRC filtering and generation. @@ -1133,7 +1133,7 @@ class SX127x: public PhysicalLayer { \brief Get one truly random byte from RSSI noise. \returns TRNG byte. */ - uint8_t randomByte(); + uint8_t randomByte() override; /*! \brief Read version SPI register. Should return SX1278_CHIP_VERSION (0x12) or SX1272_CHIP_VERSION (0x22) if SX127x is connected and working. @@ -1153,13 +1153,13 @@ class SX127x: public PhysicalLayer { \brief Set interrupt service routine function to call when data bit is received in direct mode. \param func Pointer to interrupt service routine. */ - void setDirectAction(void (*func)(void)); + void setDirectAction(void (*func)(void)) override; /*! \brief Function to read and process data bit in direct reception mode. \param pin Pin on which to read. */ - void readBit(uint32_t pin); + void readBit(uint32_t pin) override; #endif /*! @@ -1192,7 +1192,7 @@ class SX127x: public PhysicalLayer { \param value The value that indicates which function to place on that pin. See chip datasheet for details. \returns \ref status_codes */ - int16_t setDIOMapping(uint32_t pin, uint32_t value); + int16_t setDIOMapping(uint32_t pin, uint32_t value) override; /*! \brief Configure DIO mapping to use RSSI or Preamble Detect for pins that support it. @@ -1221,7 +1221,7 @@ class SX127x: public PhysicalLayer { #if !RADIOLIB_GODMODE && !RADIOLIB_LOW_LEVEL protected: #endif - Module* getMod(); + Module* getMod() override; #if !RADIOLIB_GODMODE protected: @@ -1254,7 +1254,7 @@ class SX127x: public PhysicalLayer { int16_t config(); int16_t directMode(); int16_t setPacketMode(uint8_t mode, uint8_t len); - bool findChip(uint8_t* vers, uint8_t num); + bool findChip(const uint8_t* vers, uint8_t num); int16_t setMode(uint8_t mode); int16_t setActiveModem(uint8_t modem); void clearIRQFlags();