Line data Source code
1 : #ifndef HARDWARE_EMULATION_HPP 2 : #define HARDWARE_EMULATION_HPP 3 : 4 : #include <stdint.h> 5 : 6 : // value that is returned by the emualted radio class when performing SPI transfer to it 7 : #define EMULATED_RADIO_SPI_RETURN (0xFF) 8 : 9 : // pin indexes 10 : #define EMULATED_RADIO_NSS_PIN (1) 11 : #define EMULATED_RADIO_IRQ_PIN (2) 12 : #define EMULATED_RADIO_RST_PIN (3) 13 : #define EMULATED_RADIO_GPIO_PIN (4) 14 : 15 : enum PinFunction_t { 16 : PIN_UNASSIGNED = 0, 17 : PIN_CS, 18 : PIN_IRQ, 19 : PIN_RST, 20 : PIN_GPIO, 21 : }; 22 : 23 : // structure for emulating GPIO pins 24 : struct EmulatedPin_t { 25 : uint32_t mode; 26 : uint32_t value; 27 : bool event; 28 : PinFunction_t func; 29 : }; 30 : 31 : // structure for emulating SPI registers 32 : struct EmulatedRegister_t { 33 : uint8_t value; 34 : uint8_t readOnlyBitFlags; 35 : bool bufferAccess; 36 : }; 37 : 38 : // base class for emulated radio modules (SX126x etc.) 39 : class EmulatedRadio { 40 : public: 41 4 : void connect(EmulatedPin_t* csPin, EmulatedPin_t* irqPin, EmulatedPin_t* rstPin, EmulatedPin_t* gpioPin) { 42 4 : this->cs = csPin; 43 4 : this->cs->func = PIN_CS; 44 4 : this->irq = irqPin; 45 4 : this->irq->func = PIN_IRQ; 46 4 : this->rst = rstPin; 47 4 : this->rst->func = PIN_RST; 48 4 : this->gpio = gpioPin; 49 4 : this->gpio->func = PIN_GPIO; 50 4 : } 51 : 52 4197 : virtual uint8_t HandleSPI(uint8_t b) { 53 : (void)b; 54 : // handle the SPI input and generate output here 55 4197 : return(EMULATED_RADIO_SPI_RETURN); 56 : } 57 : 58 2934 : virtual void HandleGPIO() { 59 : // handle discrete GPIO signals here (e.g. reset state machine on NSS falling edge) 60 2934 : } 61 : 62 : protected: 63 : // pointers to emulated GPIO pins 64 : // this is done via pointers so that the same GPIO entity is shared, like with a real hardware 65 : EmulatedPin_t* cs; 66 : EmulatedPin_t* irq; 67 : EmulatedPin_t* rst; 68 : EmulatedPin_t* gpio; 69 : }; 70 : 71 : #endif