Line data Source code
1 : #if !defined(_RADIOLIB_HAL_H) 2 : #define _RADIOLIB_HAL_H 3 : 4 : #include <stdint.h> 5 : #include <stddef.h> 6 : 7 : #include "BuildOpt.h" 8 : 9 : /*! \brief Global-scope function that returns timestamp since start (in microseconds). */ 10 : RadioLibTime_t rlb_time_us(); 11 : 12 : /*! 13 : \class RadioLibHal 14 : \brief Hardware abstraction library base interface. 15 : */ 16 : class RadioLibHal { 17 : public: 18 : 19 : // values for pin modes, levels and change directions 20 : // these tell RadioLib how are different logic states represented on a given platform 21 : 22 : /*! 23 : \brief Value to be used as the "input" GPIO direction. 24 : */ 25 : const uint32_t GpioModeInput; 26 : 27 : /*! 28 : \brief Value to be used as the "output" GPIO direction. 29 : */ 30 : const uint32_t GpioModeOutput; 31 : 32 : /*! 33 : \brief Value to be used as the "low" GPIO level. 34 : */ 35 : const uint32_t GpioLevelLow; 36 : 37 : /*! 38 : \brief Value to be used as the "high" GPIO level. 39 : */ 40 : const uint32_t GpioLevelHigh; 41 : 42 : /*! 43 : \brief Value to be used as the "rising" GPIO level change direction. 44 : */ 45 : const uint32_t GpioInterruptRising; 46 : 47 : /*! 48 : \brief Value to be used as the "falling" GPIO level change direction. 49 : */ 50 : const uint32_t GpioInterruptFalling; 51 : 52 : /*! 53 : \brief Default constructor. 54 : \param input Value to be used as the "input" GPIO direction. 55 : \param output Value to be used as the "output" GPIO direction. 56 : \param low Value to be used as the "low" GPIO level. 57 : \param high Value to be used as the "high" GPIO level. 58 : \param rising Value to be used as the "rising" GPIO level change direction. 59 : \param falling Value to be used as the "falling" GPIO level change direction. 60 : */ 61 : RadioLibHal(const uint32_t input, const uint32_t output, const uint32_t low, const uint32_t high, const uint32_t rising, const uint32_t falling); 62 : 63 : /*! 64 : \brief Default destructor. 65 : */ 66 4 : virtual ~RadioLibHal() = default; 67 : 68 : // pure virtual methods - these must be implemented by the hardware abstraction for RadioLib to function 69 : 70 : /*! 71 : \brief GPIO pin mode (input/output/...) configuration method. 72 : Must be implemented by the platform-specific hardware abstraction! 73 : \param pin Pin to be changed (platform-specific). 74 : \param mode Mode to be set (platform-specific). 75 : */ 76 : virtual void pinMode(uint32_t pin, uint32_t mode) = 0; 77 : 78 : /*! 79 : \brief Digital write method. 80 : Must be implemented by the platform-specific hardware abstraction! 81 : \param pin Pin to be changed (platform-specific). 82 : \param value Value to set (platform-specific). 83 : */ 84 : virtual void digitalWrite(uint32_t pin, uint32_t value) = 0; 85 : 86 : /*! 87 : \brief Digital read method. 88 : Must be implemented by the platform-specific hardware abstraction! 89 : \param pin Pin to be changed (platform-specific). 90 : \returns Value read on the pin (platform-specific). 91 : */ 92 : virtual uint32_t digitalRead(uint32_t pin) = 0; 93 : 94 : /*! 95 : \brief Method to attach function to an external interrupt. 96 : Must be implemented by the platform-specific hardware abstraction! 97 : \param interruptNum Interrupt number to attach to (platform-specific). 98 : \param interruptCb Interrupt service routine to execute. 99 : \param mode Rising/falling mode (platform-specific). 100 : */ 101 : virtual void attachInterrupt(uint32_t interruptNum, void (*interruptCb)(void), uint32_t mode) = 0; 102 : 103 : /*! 104 : \brief Method to detach function from an external interrupt. 105 : Must be implemented by the platform-specific hardware abstraction! 106 : \param interruptNum Interrupt number to detach from (platform-specific). 107 : */ 108 : virtual void detachInterrupt(uint32_t interruptNum) = 0; 109 : 110 : /*! 111 : \brief Blocking wait function. 112 : Must be implemented by the platform-specific hardware abstraction! 113 : \param ms Number of milliseconds to wait. 114 : */ 115 : virtual void delay(RadioLibTime_t ms) = 0; 116 : 117 : /*! 118 : \brief Blocking microsecond wait function. 119 : Must be implemented by the platform-specific hardware abstraction! 120 : \param us Number of microseconds to wait. 121 : */ 122 : virtual void delayMicroseconds(RadioLibTime_t us) = 0; 123 : 124 : /*! 125 : \brief Get number of milliseconds since start. 126 : Must be implemented by the platform-specific hardware abstraction! 127 : \returns Number of milliseconds since start. 128 : */ 129 : virtual RadioLibTime_t millis() = 0; 130 : 131 : /*! 132 : \brief Get number of microseconds since start. 133 : Must be implemented by the platform-specific hardware abstraction! 134 : \returns Number of microseconds since start. 135 : */ 136 : virtual RadioLibTime_t micros() = 0; 137 : 138 : /*! 139 : \brief Measure the length of incoming digital pulse in microseconds. 140 : Must be implemented by the platform-specific hardware abstraction! 141 : \param pin Pin to measure on (platform-specific). 142 : \param state Pin level to monitor (platform-specific). 143 : \param timeout Timeout in microseconds. 144 : \returns Pulse length in microseconds, or 0 if the pulse did not start before timeout. 145 : */ 146 : virtual long pulseIn(uint32_t pin, uint32_t state, RadioLibTime_t timeout) = 0; 147 : 148 : /*! 149 : \brief SPI initialization method. 150 : */ 151 : virtual void spiBegin() = 0; 152 : 153 : /*! 154 : \brief Method to start SPI transaction. 155 : */ 156 : virtual void spiBeginTransaction() = 0; 157 : 158 : /*! 159 : \brief Method to transfer buffer over SPI. 160 : \param out Buffer to send. 161 : \param len Number of data to send or receive. 162 : \param in Buffer to save received data into. 163 : */ 164 : virtual void spiTransfer(uint8_t* out, size_t len, uint8_t* in) = 0; 165 : 166 : /*! 167 : \brief Method to end SPI transaction. 168 : */ 169 : virtual void spiEndTransaction() = 0; 170 : 171 : /*! 172 : \brief SPI termination method. 173 : */ 174 : virtual void spiEnd() = 0; 175 : 176 : // virtual methods - these may or may not exists on a given platform 177 : // they exist in this implementation, but do nothing 178 : 179 : /*! 180 : \brief Module initialization method. 181 : This will be called by all radio modules at the beginning of startup. 182 : Can be used to e.g., initialize SPI interface. 183 : */ 184 : virtual void init(); 185 : 186 : /*! 187 : \brief Module termination method. 188 : This will be called by all radio modules when the destructor is called. 189 : Can be used to e.g., stop SPI interface. 190 : */ 191 : virtual void term(); 192 : 193 : /*! 194 : \brief Method to produce a square-wave with 50% duty cycle ("tone") of a given frequency at some pin. 195 : \param pin Pin to be used as the output. 196 : \param frequency Frequency of the square wave. 197 : \param duration Duration of the tone in ms. When set to 0, the tone will be infinite. 198 : */ 199 : virtual void tone(uint32_t pin, unsigned int frequency, RadioLibTime_t duration = 0); 200 : 201 : /*! 202 : \brief Method to stop producing a tone. 203 : \param pin Pin which is currently producing the tone. 204 : */ 205 : virtual void noTone(uint32_t pin); 206 : 207 : /*! 208 : \brief Yield method, called from long loops in multi-threaded environment (to prevent blocking other threads). 209 : */ 210 : virtual void yield(); 211 : 212 : /*! 213 : \brief Function to convert from pin number to interrupt number. 214 : \param pin Pin to convert from. 215 : \returns The interrupt number of a given pin. 216 : */ 217 : virtual uint32_t pinToInterrupt(uint32_t pin); 218 : }; 219 : 220 : #endif