|
| 1 | +/* |
| 2 | + RadioLib Non-Arduino Tock Library helper functions |
| 3 | +
|
| 4 | + Licensed under the MIT License |
| 5 | +
|
| 6 | + Copyright (c) 2023 Alistair Francis <[email protected]> |
| 7 | +
|
| 8 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + of this software and associated documentation files (the "Software"), to deal |
| 10 | + in the Software without restriction, including without limitation the rights |
| 11 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + copies of the Software, and to permit persons to whom the Software is |
| 13 | + furnished to do so, subject to the following conditions: |
| 14 | +
|
| 15 | + The above copyright notice and this permission notice shall be included in all |
| 16 | + copies or substantial portions of the Software. |
| 17 | +
|
| 18 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | + SOFTWARE. |
| 25 | +*/ |
| 26 | + |
| 27 | +#ifndef TOCK_RADIOLIB_HAL_H |
| 28 | +#define TOCK_RADIOLIB_HAL_H |
| 29 | + |
| 30 | +// include RadioLib |
| 31 | +#include <RadioLib.h> |
| 32 | + |
| 33 | +// include all the dependencies |
| 34 | +#include "libtock/net/lora_phy.h" |
| 35 | +#include "libtock/net/syscalls/lora_phy_syscalls.h" |
| 36 | +#include "libtock-sync/net/lora_phy.h" |
| 37 | +#include "libtock/peripherals/gpio.h" |
| 38 | +#include "libtock-sync/services/alarm.h" |
| 39 | +#include "libtock/kernel/read_only_state.h" |
| 40 | + |
| 41 | +#define RADIOLIB_RADIO_BUSY 1 |
| 42 | +#define RADIOLIB_RADIO_DIO_1 2 |
| 43 | +#define RADIOLIB_RADIO_DIO_3 3 |
| 44 | +#define RADIOLIB_RADIO_RESET 4 |
| 45 | +// Skip the chips select as Tock handles this for us |
| 46 | +#define RADIOLIB_RADIO_NSS RADIOLIB_NC |
| 47 | + |
| 48 | +// define Arduino-style macros |
| 49 | +#define TOCK_RADIOLIB_PIN_LOW (0x0) |
| 50 | +#define TOCK_RADIOLIB_PIN_HIGH (0x1) |
| 51 | +#define TOCK_RADIOLIB_PIN_INPUT (0x01) |
| 52 | +#define TOCK_RADIOLIB_PIN_OUTPUT (0x03) |
| 53 | +#define TOCK_RADIOLIB_PIN_RISING (0x01) |
| 54 | +#define TOCK_RADIOLIB_PIN_FALLING (0x02) |
| 55 | + |
| 56 | +typedef void (*gpioIrqFn)(void); |
| 57 | + |
| 58 | +gpioIrqFn gpio_funcs[4] = { NULL, NULL, NULL, NULL}; |
| 59 | + |
| 60 | +static void lora_phy_gpio_Callback (int gpioPin, |
| 61 | + __attribute__ ((unused)) int arg2, |
| 62 | + __attribute__ ((unused)) int arg3, |
| 63 | + void* userdata) |
| 64 | +{ |
| 65 | + gpioIrqFn fn = gpio_funcs[gpioPin - 1]; |
| 66 | + |
| 67 | + if (fn != NULL ) { |
| 68 | + fn(); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +class TockRadioLibHal : public RadioLibHal { |
| 73 | + public: |
| 74 | + // default constructor - initializes the base HAL and any needed private members |
| 75 | + TockRadioLibHal() |
| 76 | + : RadioLibHal(TOCK_RADIOLIB_PIN_INPUT, TOCK_RADIOLIB_PIN_OUTPUT, TOCK_RADIOLIB_PIN_LOW, TOCK_RADIOLIB_PIN_HIGH, TOCK_RADIOLIB_PIN_RISING, TOCK_RADIOLIB_PIN_FALLING) { |
| 77 | + } |
| 78 | + |
| 79 | + void init() override { |
| 80 | + } |
| 81 | + |
| 82 | + void term() override { |
| 83 | + } |
| 84 | + |
| 85 | + // GPIO-related methods (pinMode, digitalWrite etc.) should check |
| 86 | + // RADIOLIB_NC as an alias for non-connected pins |
| 87 | + void pinMode(uint32_t pin, uint32_t mode) override { |
| 88 | + if(pin == RADIOLIB_NC) { |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + if (mode == TOCK_RADIOLIB_PIN_OUTPUT) { |
| 93 | + libtock_lora_phy_gpio_enable_output(pin); |
| 94 | + } else if (mode == TOCK_RADIOLIB_PIN_INPUT) { |
| 95 | + libtock_lora_phy_gpio_enable_input(pin, libtock_pull_down); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + void digitalWrite(uint32_t pin, uint32_t value) override { |
| 100 | + if(pin == RADIOLIB_NC) { |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + if (value) { |
| 105 | + libtock_lora_phy_gpio_set(pin); |
| 106 | + } else { |
| 107 | + libtock_lora_phy_gpio_clear(pin); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + uint32_t digitalRead(uint32_t pin) override { |
| 112 | + int value; |
| 113 | + |
| 114 | + if(pin == RADIOLIB_NC) { |
| 115 | + return 0; |
| 116 | + } |
| 117 | + |
| 118 | + libtock_lora_phy_gpio_read(pin, &value); |
| 119 | + |
| 120 | + return value; |
| 121 | + } |
| 122 | + |
| 123 | + void attachInterrupt(uint32_t interruptNum, gpioIrqFn interruptCb, uint32_t mode) override { |
| 124 | + if(interruptNum == RADIOLIB_NC) { |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + gpio_funcs[interruptNum - 1] = interruptCb; |
| 129 | + libtock_lora_phy_gpio_command_interrupt_callback(lora_phy_gpio_Callback, NULL); |
| 130 | + |
| 131 | + // set GPIO as input and enable interrupts on it |
| 132 | + libtock_lora_phy_gpio_enable_input(interruptNum, libtock_pull_down); |
| 133 | + libtock_lora_phy_gpio_enable_interrupt(interruptNum, libtock_change); |
| 134 | + } |
| 135 | + |
| 136 | + void detachInterrupt(uint32_t interruptNum) override { |
| 137 | + if(interruptNum == RADIOLIB_NC) { |
| 138 | + return; |
| 139 | + } |
| 140 | + |
| 141 | + gpio_funcs[interruptNum - 1] = NULL; |
| 142 | + libtock_lora_phy_gpio_disable_interrupt(interruptNum); |
| 143 | + libtock_lora_phy_gpio_enable_input(interruptNum, libtock_pull_down); |
| 144 | + } |
| 145 | + |
| 146 | + void delay(unsigned long ms) override { |
| 147 | +#if !defined(RADIOLIB_CLOCK_DRIFT_MS) |
| 148 | + libtocksync_alarm_delay_ms(ms); |
| 149 | +#else |
| 150 | + libtocksync_alarm_delay_ms(ms * 1000 / (1000 + RADIOLIB_CLOCK_DRIFT_MS)); |
| 151 | +#endif |
| 152 | + } |
| 153 | + |
| 154 | + void delayMicroseconds(unsigned long us) override { |
| 155 | +#if !defined(RADIOLIB_CLOCK_DRIFT_MS) |
| 156 | + libtocksync_alarm_delay_ms(us / 1000); |
| 157 | +#else |
| 158 | + libtocksync_alarm_delay_ms((us * 1000 / (1000 + RADIOLIB_CLOCK_DRIFT_MS)) / 1000); |
| 159 | +#endif |
| 160 | + } |
| 161 | + |
| 162 | + unsigned long millis() override { |
| 163 | + struct timeval tv; |
| 164 | + unsigned long ms; |
| 165 | + |
| 166 | + libtock_alarm_gettimeasticks(&tv); |
| 167 | + ms = tv.tv_sec * 1000 + tv.tv_usec / 1000; |
| 168 | + |
| 169 | +#if !defined(RADIOLIB_CLOCK_DRIFT_MS) |
| 170 | + return ms; |
| 171 | +#else |
| 172 | + return ms * 1000 / (1000 + RADIOLIB_CLOCK_DRIFT_MS); |
| 173 | +#endif |
| 174 | + } |
| 175 | + |
| 176 | + unsigned long micros() override { |
| 177 | + struct timeval tv; |
| 178 | + |
| 179 | + libtock_alarm_gettimeasticks(&tv); |
| 180 | + return tv.tv_sec * 1000000 + tv.tv_usec; |
| 181 | + } |
| 182 | + |
| 183 | + long pulseIn(uint32_t pin, uint32_t state, unsigned long timeout) override { |
| 184 | + return 0; |
| 185 | + } |
| 186 | + |
| 187 | + void spiBegin() { |
| 188 | + } |
| 189 | + |
| 190 | + void spiBeginTransaction() { |
| 191 | + } |
| 192 | + |
| 193 | + void spiTransfer(uint8_t* out, size_t len, uint8_t* in) { |
| 194 | + libtocksync_lora_phy_read_write(out, in, len); |
| 195 | + } |
| 196 | + |
| 197 | + void spiEndTransaction() { |
| 198 | + } |
| 199 | + |
| 200 | + void spiEnd() { |
| 201 | + } |
| 202 | + |
| 203 | + void yield() { |
| 204 | + ::yield_no_wait(); |
| 205 | + } |
| 206 | + |
| 207 | + private: |
| 208 | +}; |
| 209 | + |
| 210 | +#endif |
0 commit comments