Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: DISCO-L072CZ-LRWAN1-base
Fork of SX1276GenericLib by
arduino-mbed.h
00001 /* 00002 * The file is Licensed under the Apache License, Version 2.0 00003 * (c) 2017 Helmut Tschemernjak 00004 * 30826 Garbsen (Hannover) Germany 00005 */ 00006 00007 00008 00009 #ifdef ARDUINO 00010 #ifndef __ARDUINO_MBED_H__ 00011 #define __ARDUINO_MBED_H__ 00012 00013 #include <arduino.h> 00014 #include "Callback-A.h" 00015 #include <SPI.h> 00016 #undef min 00017 #undef max 00018 #undef map 00019 00020 typedef int PinName; 00021 #define NC -1 00022 /* we need to redefine out dprintf because stdio.h uses the same name */ 00023 #define dprint dxprintf 00024 #if ARDUINO_SAMD_VARIANT_COMPLIANCE >= 10606 00025 #define MYdigitalPinToInterrupt(x) digitalPinToInterrupt(x) 00026 #else 00027 #define MYdigitalPinToInterrupt(x) (x) 00028 #endif 00029 00030 class DigitalOut; 00031 void InitSerial(Stream *serial, int timeout_ms, DigitalOut *led); 00032 extern Stream *ser; 00033 extern bool SerialUSB_active; 00034 00035 /* 00036 * Arduino_d21.cpp 00037 */ 00038 extern void startTimer(Tcc *t, uint64_t delay_ns); 00039 extern void stopTimer(Tcc *t); 00040 extern uint64_t ns_getTicker(void); 00041 extern Tcc *getTimeout_tcc(void); 00042 extern int CPUID(uint8_t *buf, int maxSize, uint32_t xorval); 00043 00044 00045 extern void sleep(void); 00046 extern void deepsleep(void); 00047 00048 #define MAX_TIMEOUTS 10 00049 class Timeout; 00050 struct TimeoutVector { 00051 Timeout *timer; 00052 }; 00053 extern TimeoutVector TimeOuts[]; 00054 00055 00056 /* 00057 * Arduino-mbed.cpp 00058 */ 00059 extern uint32_t s_getTicker(void); 00060 extern uint32_t ms_getTicker(void); 00061 extern uint32_t us_getTicker(void); 00062 extern void wait_ms(uint32_t ms); 00063 00064 00065 enum PinMode { 00066 PullUp = 1, 00067 PullDown = 2, 00068 }; 00069 00070 class DigitalInOut { 00071 public: 00072 DigitalInOut(PinName pin) { 00073 _gpioPin = pin; 00074 } 00075 void write(int value) { 00076 digitalWrite(_gpioPin, value == 0 ? LOW : HIGH); 00077 }; 00078 00079 void output() { 00080 pinMode(_gpioPin, OUTPUT); 00081 }; 00082 00083 void input() { 00084 pinMode(_gpioPin, INPUT); 00085 }; 00086 00087 void mode(PinMode pull) { 00088 switch(pull) { 00089 case PullUp: 00090 pinMode(_gpioPin, INPUT_PULLUP); 00091 break; 00092 case PullDown: 00093 pinMode(_gpioPin, INPUT_PULLDOWN); 00094 break; 00095 } 00096 } 00097 00098 int read() { 00099 if (digitalRead(_gpioPin) == HIGH) 00100 return 1; 00101 else 00102 return 0; 00103 }; 00104 operator int() { 00105 return read(); 00106 }; 00107 00108 DigitalInOut& operator= (int value) { 00109 // Underlying write is thread safe 00110 write(value); 00111 return *this; 00112 } 00113 00114 DigitalInOut& operator= (DigitalInOut& rhs) { 00115 write(rhs.read()); 00116 return *this; 00117 } 00118 00119 private: 00120 int _gpioPin; 00121 }; 00122 00123 class DigitalOut : public DigitalInOut { 00124 public: 00125 00126 DigitalOut(PinName pin) : DigitalInOut(pin) { 00127 output(); 00128 } 00129 00130 DigitalOut& operator= (int value) { 00131 write(value); 00132 return *this; 00133 } 00134 00135 }; 00136 00137 class DigitalIn : public DigitalInOut { 00138 public: 00139 00140 DigitalIn(PinName pin) : DigitalInOut(pin) { 00141 input(); 00142 } 00143 }; 00144 00145 class XSPI { 00146 public: 00147 XSPI(PinName mosi, PinName miso, PinName sclk) { 00148 _mosi = mosi; 00149 _miso = miso; 00150 _sclk = sclk; 00151 if (mosi == PIN_SPI_MOSI && miso == PIN_SPI_MISO && sclk == PIN_SPI_SCK) 00152 _spi = &SPI; 00153 #if SPI_INTERFACES_COUNT > 1 00154 else if (mosi == PIN_SPI1_MOSI && miso == PIN_SPI1_MISO && sclk == PIN_SPI1_SCK) 00155 _spi = &SPI1; 00156 #endif 00157 #if SPI_INTERFACES_COUNT > 2 00158 else if (mosi == PIN_SPI2_MOSI && miso == PIN_SPI2_MISO && sclk == PIN_SPI2_SCK) 00159 _spi = &SPI2; 00160 #endif 00161 else { 00162 _spi = NULL; 00163 return; 00164 } 00165 _hz = 1000000; 00166 _mode = SPI_MODE0; 00167 _spi->beginTransaction(SPISettings(_hz, MSBFIRST, _mode)); 00168 } 00169 ~XSPI() { 00170 _spi->endTransaction(); 00171 }; 00172 00173 void format(int bits, int mode = 0) { 00174 if (mode == 0) 00175 _mode = SPI_MODE0; 00176 else if (mode == 1) 00177 _mode = SPI_MODE1; 00178 else if (mode == 2) 00179 _mode = SPI_MODE2; 00180 else if (mode == 3) 00181 _mode = SPI_MODE3; 00182 else 00183 _mode = SPI_MODE0; 00184 _bits = bits; 00185 _spi->endTransaction(); 00186 _spi->beginTransaction(SPISettings(_hz, MSBFIRST, _mode)); 00187 } 00188 void frequency(int hz) { 00189 _hz = hz; 00190 _spi->endTransaction(); 00191 _spi->beginTransaction(SPISettings(_hz, MSBFIRST, _mode)); 00192 } 00193 00194 int write(int value) { 00195 int ret = _spi->transfer(value); 00196 return ret; 00197 } 00198 00199 private: 00200 SPIClass *_spi; 00201 int _hz; 00202 int _mode; 00203 int _bits; 00204 int _mosi, _miso, _sclk; 00205 }; 00206 00207 class InterruptIn { 00208 public: 00209 static void donothing(void) { 00210 } 00211 00212 InterruptIn(PinName pin) : _func() { 00213 _gpioPin = pin; 00214 _func = InterruptIn::donothing; 00215 pinMode(_gpioPin, INPUT); 00216 } 00217 00218 ~InterruptIn() { 00219 detachInterrupt(MYdigitalPinToInterrupt(_gpioPin)); 00220 }; 00221 00222 static void _irq_handler(InterruptIn *id) { 00223 if (id) 00224 id->_func(); 00225 } 00226 00227 void rise(Callback<void()> func); 00228 00229 void fall(Callback<void()> func); 00230 00231 void high(Callback<void()> func); 00232 00233 void low(Callback<void()> func); 00234 00235 void mode(PinMode pull) { 00236 switch(pull) { 00237 case PullUp: 00238 pinMode(_gpioPin, INPUT_PULLUP); 00239 break; 00240 case PullDown: 00241 pinMode(_gpioPin, INPUT_PULLDOWN); 00242 break; 00243 } 00244 } 00245 private: 00246 int _gpioPin; 00247 Callback<void()> _func; 00248 }; 00249 00250 00251 00252 class Timer { 00253 public: 00254 void start(void) { 00255 _time = ns_getTicker(); 00256 } 00257 uint32_t read_sec(void) { 00258 int64_t n = ns_getTicker() - (uint64_t)_time; 00259 n /= (uint64_t)1000000000; 00260 return n; 00261 } 00262 uint32_t read_ms(void) { 00263 int64_t n = ns_getTicker() - (uint64_t)_time; 00264 n /= (uint64_t)1000000; 00265 return n; 00266 } 00267 uint32_t read_us(void) { 00268 int64_t n = ns_getTicker() - (uint64_t)_time; 00269 n /= (uint64_t)1000; 00270 return n; 00271 } 00272 private: 00273 uint64_t _time; 00274 }; 00275 00276 00277 class Timeout { 00278 public: 00279 Timeout() : _func() { 00280 } 00281 ~Timeout() { 00282 detach(); 00283 } 00284 00285 void attach_sec(Callback<void()> func, uint32_t secs) { 00286 if (secs == 0) 00287 return detach(); 00288 _func = func; 00289 _timeout = ns_getTicker() + (uint64_t)secs * (uint64_t)1000000000; 00290 insert(); 00291 restart(); 00292 } 00293 00294 void attach(Callback<void()> func, uint32_t msecs) { 00295 if (msecs == 0) 00296 return detach(); 00297 _func = func; 00298 _timeout = ns_getTicker() + (uint64_t)msecs * (uint64_t)1000000; 00299 insert(); 00300 restart(); 00301 } 00302 00303 void attach_us(Callback<void()> func, long usecs) { 00304 if (usecs == 0) 00305 return detach(); 00306 _func = func; 00307 _timeout = ns_getTicker() + (uint64_t)usecs * (uint64_t)1000; 00308 insert(); 00309 restart(); 00310 } 00311 00312 void detach(void) { 00313 _func = NULL; 00314 remove(); 00315 restart(); 00316 } 00317 00318 static void _irq_handler(Timeout *tp) { 00319 if (tp) { 00320 tp->_func(); 00321 } 00322 } 00323 00324 static void restart(void); 00325 uint64_t _timeout; // in ns this lasts for 539 years. 00326 protected: 00327 void insert(void); 00328 void remove(void); 00329 private: 00330 Callback<void()> _func; 00331 }; 00332 00333 #endif // __ARDUINO_MBED_H__ 00334 00335 #endif // ARDUINO
Generated on Tue Jul 12 2022 21:48:25 by
 1.7.2
 1.7.2 
    