acid-drop

- Hacking the planet from a LilyGo T-Deck using custom firmware
git clone git://git.acid.vegas/acid-drop.git
Log | Files | Refs | Archive | README | LICENSE

Module.h (13665B)

      1 #if !defined(_RADIOLIB_MODULE_H)
      2 #define _RADIOLIB_MODULE_H
      3 
      4 #include "TypeDef.h"
      5 
      6 #if defined(RADIOLIB_BUILD_ARDUINO)
      7   #include <SPI.h>
      8 #endif
      9 
     10 /*!
     11   \class Module
     12 
     13   \brief Implements all common low-level methods to control the wireless module.
     14   Every module class contains one private instance of this class.
     15 */
     16 class Module {
     17   public:
     18 
     19     #if defined(RADIOLIB_BUILD_ARDUINO)
     20 
     21     /*!
     22       \brief Arduino Module constructor. Will use the default SPI interface and automatically initialize it
     23 
     24       \param cs Arduino pin to be used as chip select.
     25 
     26       \param irq Arduino pin to be used as interrupt/GPIO.
     27 
     28       \param rst Arduino pin to be used as hardware reset for the module.
     29 
     30       \param gpio Arduino pin to be used as additional interrupt/GPIO.
     31     */
     32     Module(RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rst, RADIOLIB_PIN_TYPE gpio = RADIOLIB_NC);
     33 
     34     /*!
     35       \brief Arduino Module constructor. Will not attempt SPI interface initialization.
     36 
     37       \param cs Arduino pin to be used as chip select.
     38 
     39       \param irq Arduino pin to be used as interrupt/GPIO.
     40 
     41       \param rst Arduino pin to be used as hardware reset for the module.
     42 
     43       \param gpio Arduino pin to be used as additional interrupt/GPIO.
     44 
     45       \param spi SPI interface to be used, can also use software SPI implementations.
     46 
     47       \param spiSettings SPI interface settings.
     48     */
     49     Module(RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rst, RADIOLIB_PIN_TYPE gpio, SPIClass& spi, SPISettings spiSettings = RADIOLIB_DEFAULT_SPI_SETTINGS);
     50 
     51     #else
     52 
     53     /*!
     54       \brief Default constructor.
     55 
     56       \param cs Pin to be used as chip select.
     57 
     58       \param irq Pin to be used as interrupt/GPIO.
     59 
     60       \param rst Pin to be used as hardware reset for the module.
     61 
     62       \param gpio Pin to be used as additional interrupt/GPIO.
     63     */
     64     Module(RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rst, RADIOLIB_PIN_TYPE gpio = RADIOLIB_NC);
     65 
     66     #endif
     67 
     68     /*!
     69       \brief Copy constructor.
     70 
     71       \param mod Module instance to copy.
     72     */
     73     Module(const Module& mod);
     74 
     75     /*!
     76       \brief Overload for assignment operator.
     77 
     78       \param frame rvalue Module.
     79     */
     80     Module& operator=(const Module& mod);
     81 
     82     // public member variables
     83 
     84     /*!
     85       \brief Basic SPI read command. Defaults to 0x00.
     86     */
     87     uint8_t SPIreadCommand = 0b00000000;
     88 
     89     /*!
     90       \brief Basic SPI write command. Defaults to 0x80.
     91     */
     92     uint8_t SPIwriteCommand = 0b10000000;
     93 
     94     // basic methods
     95 
     96     /*!
     97       \brief Initialize low-level module control.
     98     */
     99     void init();
    100 
    101     /*!
    102       \brief Terminate low-level module control.
    103     */
    104     void term();
    105 
    106     // SPI methods
    107 
    108     /*!
    109       \brief SPI read method that automatically masks unused bits. This method is the preferred SPI read mechanism.
    110 
    111       \param reg Address of SPI register to read.
    112 
    113       \param msb Most significant bit of the register variable. Bits above this one will be masked out.
    114 
    115       \param lsb Least significant bit of the register variable. Bits below this one will be masked out.
    116 
    117       \returns Masked register value or status code.
    118     */
    119     int16_t SPIgetRegValue(uint8_t reg, uint8_t msb = 7, uint8_t lsb = 0);
    120 
    121     /*!
    122       \brief Overwrite-safe SPI write method with verification. This method is the preferred SPI write mechanism.
    123 
    124       \param reg Address of SPI register to write.
    125 
    126       \param value Single byte value that will be written to the SPI register.
    127 
    128       \param msb Most significant bit of the register variable. Bits above this one will not be affected by the write operation.
    129 
    130       \param lsb Least significant bit of the register variable. Bits below this one will not be affected by the write operation.
    131 
    132       \param checkInterval Number of milliseconds between register writing and verification reading. Some registers need up to 10ms to process the change.
    133 
    134       \param checkMask Mask of bits to check, only bits set to 1 will be verified.
    135 
    136       \returns \ref status_codes
    137     */
    138     int16_t SPIsetRegValue(uint8_t reg, uint8_t value, uint8_t msb = 7, uint8_t lsb = 0, uint8_t checkInterval = 2, uint8_t checkMask = 0xFF);
    139 
    140     /*!
    141       \brief SPI burst read method.
    142 
    143       \param reg Address of SPI register to read.
    144 
    145       \param numBytes Number of bytes that will be read.
    146 
    147       \param inBytes Pointer to array that will hold the read data.
    148     */
    149     void SPIreadRegisterBurst(uint8_t reg, uint8_t numBytes, uint8_t* inBytes);
    150 
    151     /*!
    152       \brief SPI basic read method. Use of this method is reserved for special cases, SPIgetRegValue should be used instead.
    153 
    154       \param reg Address of SPI register to read.
    155 
    156       \returns Value that was read from register.
    157     */
    158     uint8_t SPIreadRegister(uint8_t reg);
    159 
    160     /*!
    161       \brief SPI burst write method.
    162 
    163       \param reg Address of SPI register to write.
    164 
    165       \param data Pointer to array that holds the data that will be written.
    166 
    167       \param numBytes Number of bytes that will be written.
    168     */
    169     void SPIwriteRegisterBurst(uint8_t reg, uint8_t* data, uint8_t numBytes);
    170 
    171     /*!
    172       \brief SPI basic write method. Use of this method is reserved for special cases, SPIsetRegValue should be used instead.
    173 
    174       \param reg Address of SPI register to write.
    175 
    176       \param data Value that will be written to the register.
    177     */
    178     void SPIwriteRegister(uint8_t reg, uint8_t data);
    179 
    180     /*!
    181       \brief SPI single transfer method.
    182 
    183       \param cmd SPI access command (read/write/burst/...).
    184 
    185       \param reg Address of SPI register to transfer to/from.
    186 
    187       \param dataOut Data that will be transfered from master to slave.
    188 
    189       \param dataIn Data that was transfered from slave to master.
    190 
    191       \param numBytes Number of bytes to transfer.
    192     */
    193     void SPItransfer(uint8_t cmd, uint8_t reg, uint8_t* dataOut, uint8_t* dataIn, uint8_t numBytes);
    194 
    195     // pin number access methods
    196 
    197     /*!
    198       \brief Access method to get the pin number of SPI chip select.
    199 
    200       \returns Pin number of SPI chip select configured in the constructor.
    201     */
    202     RADIOLIB_PIN_TYPE getCs() const { return(_cs); }
    203 
    204     /*!
    205       \brief Access method to get the pin number of interrupt/GPIO.
    206 
    207       \returns Pin number of interrupt/GPIO configured in the constructor.
    208     */
    209     RADIOLIB_PIN_TYPE getIrq() const { return(_irq); }
    210 
    211     /*!
    212       \brief Access method to get the pin number of hardware reset pin.
    213 
    214       \returns Pin number of hardware reset pin configured in the constructor.
    215     */
    216     RADIOLIB_PIN_TYPE getRst() const { return(_rst); }
    217 
    218     /*!
    219       \brief Access method to get the pin number of second interrupt/GPIO.
    220 
    221       \returns Pin number of second interrupt/GPIO configured in the constructor.
    222     */
    223     RADIOLIB_PIN_TYPE getGpio() const { return(_gpio); }
    224 
    225     /*!
    226       \brief Some modules contain external RF switch controlled by two pins. This function gives RadioLib control over those two pins to automatically switch Rx and Tx state.
    227       When using automatic RF switch control, DO NOT change the pin mode of rxEn or txEn from Arduino sketch!
    228 
    229       \param rxEn RX enable pin.
    230 
    231       \param txEn TX enable pin.
    232     */
    233     void setRfSwitchPins(RADIOLIB_PIN_TYPE rxEn, RADIOLIB_PIN_TYPE txEn);
    234 
    235     /*!
    236       \brief Set RF switch state.
    237 
    238       \param rxPinState Pin state to set on Tx enable pin (usually high to transmit).
    239 
    240       \param txPinState  Pin state to set on Rx enable pin (usually high to receive).
    241     */
    242     void setRfSwitchState(RADIOLIB_PIN_STATUS rxPinState, RADIOLIB_PIN_STATUS txPinState);
    243 
    244     // Arduino core overrides
    245 
    246     /*!
    247       \brief Arduino core pinMode override that checks RADIOLIB_NC as alias for unused pin.
    248 
    249       \param pin Pin to change the mode of.
    250 
    251       \param mode Which mode to set.
    252     */
    253     void pinMode(RADIOLIB_PIN_TYPE pin, RADIOLIB_PIN_MODE mode);
    254 
    255     /*!
    256       \brief Arduino core digitalWrite override that checks RADIOLIB_NC as alias for unused pin.
    257 
    258       \param pin Pin to write to.
    259 
    260       \param value Whether to set the pin high or low.
    261     */
    262     void digitalWrite(RADIOLIB_PIN_TYPE pin, RADIOLIB_PIN_STATUS value);
    263 
    264     /*!
    265       \brief Arduino core digitalWrite override that checks RADIOLIB_NC as alias for unused pin.
    266 
    267       \param pin Pin to read from.
    268 
    269       \returns Pin value.
    270     */
    271     RADIOLIB_PIN_STATUS digitalRead(RADIOLIB_PIN_TYPE pin);
    272 
    273     /*!
    274       \brief Arduino core tone override that checks RADIOLIB_NC as alias for unused pin and RADIOLIB_TONE_UNSUPPORTED to make sure the platform does support tone.
    275 
    276       \param pin Pin to write to.
    277 
    278       \param value Frequency to output.
    279     */
    280     void tone(RADIOLIB_PIN_TYPE pin, uint16_t value, uint32_t duration = 0);
    281 
    282     /*!
    283       \brief Arduino core noTone override that checks RADIOLIB_NC as alias for unused pin and RADIOLIB_TONE_UNSUPPORTED to make sure the platform does support tone.
    284 
    285       \param pin Pin to write to.
    286     */
    287     void noTone(RADIOLIB_PIN_TYPE pin);
    288 
    289     /*!
    290       \brief Arduino core attachInterrupt override.
    291 
    292       \param interruptNum Interrupt number.
    293 
    294       \param userFunc Interrupt service routine.
    295 
    296       \param mode Pin hcange direction.
    297     */
    298     void attachInterrupt(RADIOLIB_PIN_TYPE interruptNum, void (*userFunc)(void), RADIOLIB_INTERRUPT_STATUS mode);
    299 
    300     /*!
    301       \brief Arduino core detachInterrupt override.
    302 
    303       \param interruptNum Interrupt number.
    304     */
    305     void detachInterrupt(RADIOLIB_PIN_TYPE interruptNum);
    306 
    307     /*!
    308       \brief Arduino core yield override.
    309     */
    310     void yield();
    311 
    312     /*!
    313       \brief Arduino core delay override.
    314 
    315       \param ms Delay length in milliseconds.
    316     */
    317     void delay(uint32_t ms);
    318 
    319     /*!
    320       \brief Arduino core delayMicroseconds override.
    321 
    322       \param us Delay length in microseconds.
    323     */
    324     void delayMicroseconds(uint32_t us);
    325 
    326     /*!
    327       \brief Arduino core millis override.
    328     */
    329     uint32_t millis();
    330 
    331     /*!
    332       \brief Arduino core micros override.
    333     */
    334     uint32_t micros();
    335 
    336     /*!
    337       \brief Arduino core pulseIn override.
    338     */
    339     uint32_t pulseIn(RADIOLIB_PIN_TYPE pin, RADIOLIB_PIN_STATUS state, uint32_t timeout);
    340 
    341     /*!
    342       \brief Arduino core SPI begin override.
    343     */
    344     void begin();
    345 
    346     /*!
    347       \brief Arduino core SPI beginTransaction override.
    348     */
    349     void beginTransaction();
    350 
    351     /*!
    352       \brief Arduino core SPI transfer override.
    353     */
    354     uint8_t transfer(uint8_t b);
    355 
    356     /*!
    357       \brief Arduino core SPI endTransaction override.
    358     */
    359     void endTransaction();
    360 
    361     /*!
    362       \brief Arduino core SPI end override.
    363     */
    364     void end();
    365 
    366     // helper functions to set up SPI overrides on Arduino
    367     #if defined(RADIOLIB_BUILD_ARDUINO)
    368     void SPIbegin();
    369     void SPIend();
    370     #endif
    371     virtual void SPIbeginTransaction();
    372     virtual uint8_t SPItransfer(uint8_t b);
    373     virtual void SPIendTransaction();
    374 
    375     /*!
    376       \brief Function to reflect bits within a byte.
    377     */
    378     static uint8_t flipBits(uint8_t b);
    379 
    380     /*!
    381       \brief Function to reflect bits within an integer.
    382     */
    383     static uint16_t flipBits16(uint16_t i);
    384 
    385     /*!
    386       \brief Function to dump data as hex into the debug port.
    387 
    388       \param data Data to dump.
    389 
    390       \param len Number of bytes to dump.
    391     */
    392     static void hexdump(uint8_t* data, size_t len);
    393 
    394     /*!
    395       \brief Function to dump device registers as hex into the debug port.
    396 
    397       \param start First address to dump.
    398 
    399       \param len Number of bytes to dump.
    400     */
    401     void regdump(uint8_t start, uint8_t len);
    402 
    403     // hardware abstraction layer callbacks
    404     RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_PIN_MODE);
    405     RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_DIGITAL_WRITE);
    406     RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_DIGITAL_READ);
    407     RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_TONE);
    408     RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_NO_TONE);
    409     RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_ATTACH_INTERRUPT);
    410     RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_DETACH_INTERRUPT);
    411     RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_YIELD);
    412     RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_DELAY);
    413     RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_DELAY_MICROSECONDS);
    414     RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_MILLIS);
    415     RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_MICROS);
    416     RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_PULSE_IN);
    417 
    418     #if defined(RADIOLIB_BUILD_ARDUINO)
    419     RADIOLIB_GENERATE_CALLBACK_SPI(RADIOLIB_CB_ARGS_SPI_BEGIN);
    420     RADIOLIB_GENERATE_CALLBACK_SPI(RADIOLIB_CB_ARGS_SPI_BEGIN_TRANSACTION);
    421     RADIOLIB_GENERATE_CALLBACK_SPI(RADIOLIB_CB_ARGS_SPI_TRANSFER);
    422     RADIOLIB_GENERATE_CALLBACK_SPI(RADIOLIB_CB_ARGS_SPI_END_TRANSACTION);
    423     RADIOLIB_GENERATE_CALLBACK_SPI(RADIOLIB_CB_ARGS_SPI_END);
    424     #else
    425     RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_SPI_BEGIN);
    426     RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_SPI_BEGIN_TRANSACTION);
    427     RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_SPI_TRANSFER);
    428     RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_SPI_END_TRANSACTION);
    429     RADIOLIB_GENERATE_CALLBACK(RADIOLIB_CB_ARGS_SPI_END);
    430     #endif
    431 
    432 #if !defined(RADIOLIB_GODMODE)
    433   private:
    434 #endif
    435 
    436     // pins
    437     RADIOLIB_PIN_TYPE _cs = RADIOLIB_NC;
    438     RADIOLIB_PIN_TYPE _irq = RADIOLIB_NC;
    439     RADIOLIB_PIN_TYPE _rst = RADIOLIB_NC;
    440     RADIOLIB_PIN_TYPE _gpio = RADIOLIB_NC;
    441 
    442     // SPI interface (Arduino only)
    443     #if defined(RADIOLIB_BUILD_ARDUINO)
    444     SPIClass* _spi = NULL;
    445     SPISettings _spiSettings = RADIOLIB_DEFAULT_SPI_SETTINGS;
    446     bool _initInterface = false;
    447     #endif
    448 
    449     // RF switch presence and pins
    450     bool _useRfSwitch = false;
    451     RADIOLIB_PIN_TYPE _rxEn = RADIOLIB_NC;
    452     RADIOLIB_PIN_TYPE _txEn = RADIOLIB_NC;
    453 };
    454 
    455 #endif