RadioLib/src/ArduinoHal.cpp

201 wiersze
4.8 KiB
C++

2023-04-11 02:51:29 +00:00
#include "ArduinoHal.h"
#if defined(RADIOLIB_BUILD_ARDUINO)
ArduinoHal::ArduinoHal(): RadioLibHal(INPUT, OUTPUT, LOW, HIGH, RISING, FALLING), spi(&RADIOLIB_DEFAULT_SPI), initInterface(true) {}
2023-04-11 02:51:29 +00:00
ArduinoHal::ArduinoHal(SPIClass& spi, SPISettings spiSettings): RadioLibHal(INPUT, OUTPUT, LOW, HIGH, RISING, FALLING), spi(&spi), spiSettings(spiSettings) {}
2023-04-11 02:51:29 +00:00
void ArduinoHal::init() {
if(initInterface) {
2023-04-11 02:51:29 +00:00
spiBegin();
}
}
void ArduinoHal::term() {
if(initInterface) {
2023-04-11 02:51:29 +00:00
spiEnd();
}
}
void inline ArduinoHal::pinMode(uint32_t pin, uint32_t mode) {
2023-04-22 16:08:08 +00:00
if(pin == RADIOLIB_NC) {
2023-04-11 02:51:29 +00:00
return;
}
::pinMode(pin, RADIOLIB_ARDUINOHAL_PIN_MODE_CAST mode);
}
2023-04-22 16:08:08 +00:00
void inline ArduinoHal::digitalWrite(uint32_t pin, uint32_t value) {
2023-04-22 16:08:08 +00:00
if(pin == RADIOLIB_NC) {
2023-04-11 02:51:29 +00:00
return;
}
::digitalWrite(pin, RADIOLIB_ARDUINOHAL_PIN_STATUS_CAST value);
}
2023-04-22 16:08:08 +00:00
uint32_t inline ArduinoHal::digitalRead(uint32_t pin) {
2023-04-22 16:08:08 +00:00
if(pin == RADIOLIB_NC) {
2023-04-11 02:51:29 +00:00
return 0;
}
2023-04-22 16:08:08 +00:00
return(::digitalRead(pin));
2023-04-11 02:51:29 +00:00
}
2023-04-22 16:08:08 +00:00
void inline ArduinoHal::attachInterrupt(uint32_t interruptNum, void (*interruptCb)(void), uint32_t mode) {
2023-04-22 16:08:08 +00:00
if(interruptNum == RADIOLIB_NC) {
2023-04-11 02:51:29 +00:00
return;
}
::attachInterrupt(interruptNum, interruptCb, RADIOLIB_ARDUINOHAL_INTERRUPT_MODE_CAST mode);
}
2023-04-22 16:08:08 +00:00
void inline ArduinoHal::detachInterrupt(uint32_t interruptNum) {
2023-04-22 16:08:08 +00:00
if(interruptNum == RADIOLIB_NC) {
2023-04-11 02:51:29 +00:00
return;
}
::detachInterrupt(interruptNum);
}
2023-04-22 16:08:08 +00:00
Use RadioLibTime_t (aka unsigned long) when dealing with millis() and micros() (#1075) * Use unsigned long when dealing with millis() and micros(). Although sizeof(uint32_t) == sizeof(unsigned long) on Arduino, this is not the case on 64-bit Linux, where sizeof(unsigned long) == sizeof(uint64_t). Most timestamp arithmetic and comparisons have been left alone, to reduce code churn. This is fine, as uint32_t is perfectly wide to store most timestamp deltas this library will deal with, and C will promote the integer rather than do a narrowing conversion. The real problem arises with narrowing conversions being done by assuming timestamps are 32-bit. No functional changes intended for platforms where sizeof(uint32_t) == sizeof(unsigned long) (so most 8/16/32-bit platforms). Signed-off-by: Elizabeth Myers <elizabeth.jennifer.myers@gmail.com> * Change most timestamps to use RadioLibTime_t. This makes it obvious what is and isn't a timestamp. Not everything has been converted; anything dealing with protocol and chip-level timestamps has been left alone on purpose, to make it clear that these functions do require 32-bit timestamps. No functional changes intended on platforms where sizeof(uint32_t) == sizeof(unsigned long). Signed-off-by: Elizabeth Myers <elizabeth.jennifer.myers@gmail.com> * Use uint32_t internally in getTimeOnAir. We need to not overflow the integers with the shifts and multiplications, so this is correct behaviour. Signed-off-by: Elizabeth Myers <elizabeth.jennifer.myers@gmail.com> --------- Signed-off-by: Elizabeth Myers <elizabeth.jennifer.myers@gmail.com>
2024-04-25 19:50:58 +00:00
void inline ArduinoHal::delay(RadioLibTime_t ms) {
#if !defined(RADIOLIB_CLOCK_DRIFT_MS)
2023-04-11 02:51:29 +00:00
::delay(ms);
#else
::delay(ms * 1000 / (1000 + RADIOLIB_CLOCK_DRIFT_MS));
#endif
2023-04-11 02:51:29 +00:00
}
2023-04-22 16:08:08 +00:00
Use RadioLibTime_t (aka unsigned long) when dealing with millis() and micros() (#1075) * Use unsigned long when dealing with millis() and micros(). Although sizeof(uint32_t) == sizeof(unsigned long) on Arduino, this is not the case on 64-bit Linux, where sizeof(unsigned long) == sizeof(uint64_t). Most timestamp arithmetic and comparisons have been left alone, to reduce code churn. This is fine, as uint32_t is perfectly wide to store most timestamp deltas this library will deal with, and C will promote the integer rather than do a narrowing conversion. The real problem arises with narrowing conversions being done by assuming timestamps are 32-bit. No functional changes intended for platforms where sizeof(uint32_t) == sizeof(unsigned long) (so most 8/16/32-bit platforms). Signed-off-by: Elizabeth Myers <elizabeth.jennifer.myers@gmail.com> * Change most timestamps to use RadioLibTime_t. This makes it obvious what is and isn't a timestamp. Not everything has been converted; anything dealing with protocol and chip-level timestamps has been left alone on purpose, to make it clear that these functions do require 32-bit timestamps. No functional changes intended on platforms where sizeof(uint32_t) == sizeof(unsigned long). Signed-off-by: Elizabeth Myers <elizabeth.jennifer.myers@gmail.com> * Use uint32_t internally in getTimeOnAir. We need to not overflow the integers with the shifts and multiplications, so this is correct behaviour. Signed-off-by: Elizabeth Myers <elizabeth.jennifer.myers@gmail.com> --------- Signed-off-by: Elizabeth Myers <elizabeth.jennifer.myers@gmail.com>
2024-04-25 19:50:58 +00:00
void inline ArduinoHal::delayMicroseconds(RadioLibTime_t us) {
#if !defined(RADIOLIB_CLOCK_DRIFT_MS)
2023-04-11 02:51:29 +00:00
::delayMicroseconds(us);
#else
::delayMicroseconds(us * 1000 / (1000 + RADIOLIB_CLOCK_DRIFT_MS));
#endif
2023-04-11 02:51:29 +00:00
}
2023-04-22 16:08:08 +00:00
Use RadioLibTime_t (aka unsigned long) when dealing with millis() and micros() (#1075) * Use unsigned long when dealing with millis() and micros(). Although sizeof(uint32_t) == sizeof(unsigned long) on Arduino, this is not the case on 64-bit Linux, where sizeof(unsigned long) == sizeof(uint64_t). Most timestamp arithmetic and comparisons have been left alone, to reduce code churn. This is fine, as uint32_t is perfectly wide to store most timestamp deltas this library will deal with, and C will promote the integer rather than do a narrowing conversion. The real problem arises with narrowing conversions being done by assuming timestamps are 32-bit. No functional changes intended for platforms where sizeof(uint32_t) == sizeof(unsigned long) (so most 8/16/32-bit platforms). Signed-off-by: Elizabeth Myers <elizabeth.jennifer.myers@gmail.com> * Change most timestamps to use RadioLibTime_t. This makes it obvious what is and isn't a timestamp. Not everything has been converted; anything dealing with protocol and chip-level timestamps has been left alone on purpose, to make it clear that these functions do require 32-bit timestamps. No functional changes intended on platforms where sizeof(uint32_t) == sizeof(unsigned long). Signed-off-by: Elizabeth Myers <elizabeth.jennifer.myers@gmail.com> * Use uint32_t internally in getTimeOnAir. We need to not overflow the integers with the shifts and multiplications, so this is correct behaviour. Signed-off-by: Elizabeth Myers <elizabeth.jennifer.myers@gmail.com> --------- Signed-off-by: Elizabeth Myers <elizabeth.jennifer.myers@gmail.com>
2024-04-25 19:50:58 +00:00
RadioLibTime_t inline ArduinoHal::millis() {
#if !defined(RADIOLIB_CLOCK_DRIFT_MS)
2023-04-22 16:08:08 +00:00
return(::millis());
#else
return(::millis() * 1000 / (1000 + RADIOLIB_CLOCK_DRIFT_MS));
#endif
2023-04-11 02:51:29 +00:00
}
2023-04-22 16:08:08 +00:00
Use RadioLibTime_t (aka unsigned long) when dealing with millis() and micros() (#1075) * Use unsigned long when dealing with millis() and micros(). Although sizeof(uint32_t) == sizeof(unsigned long) on Arduino, this is not the case on 64-bit Linux, where sizeof(unsigned long) == sizeof(uint64_t). Most timestamp arithmetic and comparisons have been left alone, to reduce code churn. This is fine, as uint32_t is perfectly wide to store most timestamp deltas this library will deal with, and C will promote the integer rather than do a narrowing conversion. The real problem arises with narrowing conversions being done by assuming timestamps are 32-bit. No functional changes intended for platforms where sizeof(uint32_t) == sizeof(unsigned long) (so most 8/16/32-bit platforms). Signed-off-by: Elizabeth Myers <elizabeth.jennifer.myers@gmail.com> * Change most timestamps to use RadioLibTime_t. This makes it obvious what is and isn't a timestamp. Not everything has been converted; anything dealing with protocol and chip-level timestamps has been left alone on purpose, to make it clear that these functions do require 32-bit timestamps. No functional changes intended on platforms where sizeof(uint32_t) == sizeof(unsigned long). Signed-off-by: Elizabeth Myers <elizabeth.jennifer.myers@gmail.com> * Use uint32_t internally in getTimeOnAir. We need to not overflow the integers with the shifts and multiplications, so this is correct behaviour. Signed-off-by: Elizabeth Myers <elizabeth.jennifer.myers@gmail.com> --------- Signed-off-by: Elizabeth Myers <elizabeth.jennifer.myers@gmail.com>
2024-04-25 19:50:58 +00:00
RadioLibTime_t inline ArduinoHal::micros() {
#if !defined(RADIOLIB_CLOCK_DRIFT_MS)
2023-04-22 16:08:08 +00:00
return(::micros());
#else
return(::micros() * 1000 / (1000 + RADIOLIB_CLOCK_DRIFT_MS));
#endif
2023-04-11 02:51:29 +00:00
}
2023-04-22 16:08:08 +00:00
Use RadioLibTime_t (aka unsigned long) when dealing with millis() and micros() (#1075) * Use unsigned long when dealing with millis() and micros(). Although sizeof(uint32_t) == sizeof(unsigned long) on Arduino, this is not the case on 64-bit Linux, where sizeof(unsigned long) == sizeof(uint64_t). Most timestamp arithmetic and comparisons have been left alone, to reduce code churn. This is fine, as uint32_t is perfectly wide to store most timestamp deltas this library will deal with, and C will promote the integer rather than do a narrowing conversion. The real problem arises with narrowing conversions being done by assuming timestamps are 32-bit. No functional changes intended for platforms where sizeof(uint32_t) == sizeof(unsigned long) (so most 8/16/32-bit platforms). Signed-off-by: Elizabeth Myers <elizabeth.jennifer.myers@gmail.com> * Change most timestamps to use RadioLibTime_t. This makes it obvious what is and isn't a timestamp. Not everything has been converted; anything dealing with protocol and chip-level timestamps has been left alone on purpose, to make it clear that these functions do require 32-bit timestamps. No functional changes intended on platforms where sizeof(uint32_t) == sizeof(unsigned long). Signed-off-by: Elizabeth Myers <elizabeth.jennifer.myers@gmail.com> * Use uint32_t internally in getTimeOnAir. We need to not overflow the integers with the shifts and multiplications, so this is correct behaviour. Signed-off-by: Elizabeth Myers <elizabeth.jennifer.myers@gmail.com> --------- Signed-off-by: Elizabeth Myers <elizabeth.jennifer.myers@gmail.com>
2024-04-25 19:50:58 +00:00
long inline ArduinoHal::pulseIn(uint32_t pin, uint32_t state, RadioLibTime_t timeout) {
2023-04-22 16:08:08 +00:00
if(pin == RADIOLIB_NC) {
2023-04-11 02:51:29 +00:00
return 0;
}
2023-04-22 16:08:08 +00:00
return(::pulseIn(pin, state, timeout));
2023-04-11 02:51:29 +00:00
}
2023-04-22 16:08:08 +00:00
2023-04-11 02:51:29 +00:00
void inline ArduinoHal::spiBegin() {
spi->begin();
2023-04-11 02:51:29 +00:00
}
2023-04-22 16:08:08 +00:00
2023-04-11 02:51:29 +00:00
void inline ArduinoHal::spiBeginTransaction() {
spi->beginTransaction(spiSettings);
2023-04-11 02:51:29 +00:00
}
2023-04-22 16:08:08 +00:00
2023-06-26 17:36:45 +00:00
void ArduinoHal::spiTransfer(uint8_t* out, size_t len, uint8_t* in) {
for(size_t i = 0; i < len; i++) {
in[i] = spi->transfer(out[i]);
}
2023-04-11 02:51:29 +00:00
}
2023-04-22 16:08:08 +00:00
2023-04-11 02:51:29 +00:00
void inline ArduinoHal::spiEndTransaction() {
spi->endTransaction();
2023-04-11 02:51:29 +00:00
}
2023-04-22 16:08:08 +00:00
2023-04-11 02:51:29 +00:00
void inline ArduinoHal::spiEnd() {
spi->end();
2023-04-11 02:51:29 +00:00
}
2023-04-22 16:08:08 +00:00
Use RadioLibTime_t (aka unsigned long) when dealing with millis() and micros() (#1075) * Use unsigned long when dealing with millis() and micros(). Although sizeof(uint32_t) == sizeof(unsigned long) on Arduino, this is not the case on 64-bit Linux, where sizeof(unsigned long) == sizeof(uint64_t). Most timestamp arithmetic and comparisons have been left alone, to reduce code churn. This is fine, as uint32_t is perfectly wide to store most timestamp deltas this library will deal with, and C will promote the integer rather than do a narrowing conversion. The real problem arises with narrowing conversions being done by assuming timestamps are 32-bit. No functional changes intended for platforms where sizeof(uint32_t) == sizeof(unsigned long) (so most 8/16/32-bit platforms). Signed-off-by: Elizabeth Myers <elizabeth.jennifer.myers@gmail.com> * Change most timestamps to use RadioLibTime_t. This makes it obvious what is and isn't a timestamp. Not everything has been converted; anything dealing with protocol and chip-level timestamps has been left alone on purpose, to make it clear that these functions do require 32-bit timestamps. No functional changes intended on platforms where sizeof(uint32_t) == sizeof(unsigned long). Signed-off-by: Elizabeth Myers <elizabeth.jennifer.myers@gmail.com> * Use uint32_t internally in getTimeOnAir. We need to not overflow the integers with the shifts and multiplications, so this is correct behaviour. Signed-off-by: Elizabeth Myers <elizabeth.jennifer.myers@gmail.com> --------- Signed-off-by: Elizabeth Myers <elizabeth.jennifer.myers@gmail.com>
2024-04-25 19:50:58 +00:00
void inline ArduinoHal::tone(uint32_t pin, unsigned int frequency, RadioLibTime_t duration) {
2023-04-11 02:51:29 +00:00
#if !defined(RADIOLIB_TONE_UNSUPPORTED)
2023-04-22 16:08:08 +00:00
if(pin == RADIOLIB_NC) {
return;
}
::tone(pin, frequency, duration);
2023-08-29 19:57:51 +00:00
#elif defined(RADIOLIB_ESP32)
2023-04-22 16:08:08 +00:00
// ESP32 tone() emulation
(void)duration;
if(prev == -1) {
#if !defined(ESP_IDF_VERSION) || (ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5,0,0))
2023-04-22 16:08:08 +00:00
ledcAttachPin(pin, RADIOLIB_TONE_ESP32_CHANNEL);
#endif
2023-04-22 16:08:08 +00:00
}
if(prev != frequency) {
#if !defined(ESP_IDF_VERSION) || (ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5,0,0))
2023-04-22 16:08:08 +00:00
ledcWriteTone(RADIOLIB_TONE_ESP32_CHANNEL, frequency);
#else
ledcWriteTone(pin, frequency);
#endif
2023-04-22 16:08:08 +00:00
}
prev = frequency;
2023-04-11 02:51:29 +00:00
#elif defined(RADIOLIB_MBED_TONE_OVERRIDE)
2023-04-22 16:08:08 +00:00
// better tone for mbed OS boards
(void)duration;
if(!pwmPin) {
pwmPin = new mbed::PwmOut(digitalPinToPinName(pin));
}
pwmPin->period(1.0 / frequency);
pwmPin->write(0.5);
2023-11-18 15:56:45 +00:00
#else
(void)pin;
(void)frequency;
(void)duration;
2023-04-11 02:51:29 +00:00
#endif
}
2023-04-22 16:08:08 +00:00
void inline ArduinoHal::noTone(uint32_t pin) {
2023-04-11 02:51:29 +00:00
#if !defined(RADIOLIB_TONE_UNSUPPORTED) and defined(ARDUINO_ARCH_STM32)
2023-04-22 16:08:08 +00:00
if(pin == RADIOLIB_NC) {
return;
}
::noTone(pin, false);
2023-04-11 02:51:29 +00:00
#elif !defined(RADIOLIB_TONE_UNSUPPORTED)
2023-04-22 16:08:08 +00:00
if(pin == RADIOLIB_NC) {
return;
}
::noTone(pin);
2023-08-29 19:57:51 +00:00
#elif defined(RADIOLIB_ESP32)
2023-04-22 16:08:08 +00:00
if(pin == RADIOLIB_NC) {
return;
}
// ESP32 tone() emulation
#if !defined(ESP_IDF_VERSION) || (ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5,0,0))
2023-04-22 16:08:08 +00:00
ledcDetachPin(pin);
ledcWrite(RADIOLIB_TONE_ESP32_CHANNEL, 0);
#else
ledcDetach(pin);
ledcWrite(pin, 0);
#endif
prev = -1;
2023-04-11 02:51:29 +00:00
#elif defined(RADIOLIB_MBED_TONE_OVERRIDE)
2023-04-22 16:08:08 +00:00
if(pin == RADIOLIB_NC) {
return;
}
// better tone for mbed OS boards
(void)pin;
pwmPin->suspend();
2023-11-18 15:56:45 +00:00
#else
(void)pin;
2023-04-11 02:51:29 +00:00
#endif
}
2023-04-22 16:08:08 +00:00
2023-04-11 02:51:29 +00:00
void inline ArduinoHal::yield() {
#if !defined(RADIOLIB_YIELD_UNSUPPORTED)
::yield();
#endif
}
2023-04-22 16:08:08 +00:00
uint32_t inline ArduinoHal::pinToInterrupt(uint32_t pin) {
2023-04-22 16:08:08 +00:00
return(digitalPinToInterrupt(pin));
2023-04-11 02:51:29 +00:00
}
2023-04-22 16:08:08 +00:00
2023-04-11 02:51:29 +00:00
#endif