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