Ivano Calabrese / ST-DEVKIT-LRWAN

Dependents:   DISCO-L072CZ-LRWAN1-base

Fork of SX1276GenericLib by Helmut Tschemernjak

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers arduino-mbed.cpp Source File

arduino-mbed.cpp

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 #ifdef ARDUINO
00008 
00009 using namespace std;
00010 
00011 #include "arduino-mbed.h"
00012 #include "arduino-util.h"
00013 
00014 Stream *ser;
00015 bool SerialUSB_active;
00016 
00017 void InitSerial(Stream *serial, int timeout_ms, DigitalOut *led) {
00018     ser = serial;
00019     SerialUSB_active = true;
00020     if (!timeout_ms)
00021         return;
00022     if (serial == (Stream *)&SerialUSB) {
00023         uint32_t start = ms_getTicker();
00024 
00025         SerialUSB_active = true;
00026         while(!SerialUSB) {
00027             if (ms_getTicker() > start + timeout_ms) {
00028                 SerialUSB_active = false;
00029                 break;
00030             }
00031         }
00032         if (!SerialUSB_active) {
00033             USB->DEVICE.CTRLA.bit.SWRST = 1; // disconnect the USB Port
00034             while (USB->DEVICE.CTRLA.bit.SWRST == 1);
00035             if (led) {
00036                 for (int i = 0; i < 10; i++) {
00037                     // lets blink the LED to show that SerialUSB is off.
00038                     *led = 1;
00039                     delay(80);
00040                     *led = 0;
00041                     delay(80);
00042                 }
00043             }
00044         }
00045     }
00046 }
00047 
00048 static void pinInt00(void);
00049 static void pinInt01(void);
00050 static void pinInt02(void);
00051 static void pinInt03(void);
00052 static void pinInt04(void);
00053 static void pinInt05(void);
00054 static void pinInt06(void);
00055 static void pinInt07(void);
00056 static void pinInt08(void);
00057 static void pinInt09(void);
00058 static void pinInt10(void);
00059 static void pinInt11(void);
00060 static void pinInt12(void);
00061 static void pinInt13(void);
00062 static void pinInt14(void);
00063 static void pinInt15(void);
00064 static void pinInt16(void);
00065 static void pinInt17(void);
00066 static void pinInt18(void);
00067 static void pinInt19(void);
00068 static void pinInt20(void);
00069 static void pinInt21(void);
00070 static void pinInt22(void);
00071 static void pinInt23(void);
00072 static void pinInt24(void);
00073 static void pinInt25(void);
00074 static void pinInt26(void);
00075 static void pinInt27(void);
00076 static void pinInt28(void);
00077 static void pinInt29(void);
00078 static void pinInt30(void);
00079 static void pinInt31(void);
00080 static void pinInt32(void);
00081 static void pinInt33(void);
00082 static void pinInt34(void);
00083 static void pinInt35(void);
00084 static void pinInt36(void);
00085 static void pinInt37(void);
00086 static void pinInt38(void);
00087 static void pinInt39(void);
00088 static void pinInt40(void);
00089 static void pinInt41(void);
00090 static void pinInt42(void);
00091 static void pinInt43(void);
00092 static void pinInt44(void);
00093 static void pinInt45(void);
00094 static void pinInt46(void);
00095 static void pinInt47(void);
00096 
00097 
00098 
00099 #define MAX_MCU_PINS    48
00100 class InterruptIn;
00101 struct intPtrTable {
00102     void (*func)(void);
00103     InterruptIn *context;
00104 } intPtrTable[MAX_MCU_PINS]  = {
00105     { pinInt00, NULL },
00106     { pinInt01, NULL },
00107     { pinInt02, NULL },
00108     { pinInt03, NULL },
00109     { pinInt04, NULL },
00110     { pinInt05, NULL },
00111     { pinInt06, NULL },
00112     { pinInt07, NULL },
00113     { pinInt08, NULL },
00114     { pinInt09, NULL },
00115     { pinInt10, NULL },
00116     { pinInt11, NULL },
00117     { pinInt12, NULL },
00118     { pinInt13, NULL },
00119     { pinInt14, NULL },
00120     { pinInt15, NULL },
00121     { pinInt16, NULL },
00122     { pinInt17, NULL },
00123     { pinInt18, NULL },
00124     { pinInt19, NULL },
00125     { pinInt20, NULL },
00126     { pinInt21, NULL },
00127     { pinInt22, NULL },
00128     { pinInt23, NULL },
00129     { pinInt24, NULL },
00130     { pinInt25, NULL },
00131     { pinInt26, NULL },
00132     { pinInt27, NULL },
00133     { pinInt28, NULL },
00134     { pinInt29, NULL },
00135     { pinInt30, NULL },
00136     { pinInt31, NULL },
00137     { pinInt32, NULL },
00138     { pinInt33, NULL },
00139     { pinInt34, NULL },
00140     { pinInt35, NULL },
00141     { pinInt36, NULL },
00142     { pinInt37, NULL },
00143     { pinInt38, NULL },
00144     { pinInt39, NULL },
00145     { pinInt40, NULL },
00146     { pinInt41, NULL },
00147     { pinInt42, NULL },
00148     { pinInt43, NULL },
00149     { pinInt44, NULL },
00150     { pinInt45, NULL },
00151     { pinInt46, NULL },
00152     { pinInt47, NULL }
00153 }; // our max MCUs pins
00154 
00155 
00156 
00157 static void pinInt00(void) { InterruptIn::_irq_handler(intPtrTable[0].context); }
00158 static void pinInt01(void) { InterruptIn::_irq_handler(intPtrTable[1].context); }
00159 static void pinInt02(void) { InterruptIn::_irq_handler(intPtrTable[2].context); }
00160 static void pinInt03(void) { InterruptIn::_irq_handler(intPtrTable[3].context); }
00161 static void pinInt04(void) { InterruptIn::_irq_handler(intPtrTable[4].context); }
00162 static void pinInt05(void) { InterruptIn::_irq_handler(intPtrTable[5].context); }
00163 static void pinInt06(void) { InterruptIn::_irq_handler(intPtrTable[6].context); }
00164 static void pinInt07(void) { InterruptIn::_irq_handler(intPtrTable[7].context); }
00165 static void pinInt08(void) { InterruptIn::_irq_handler(intPtrTable[8].context); }
00166 static void pinInt09(void) { InterruptIn::_irq_handler(intPtrTable[9].context); }
00167 static void pinInt10(void) { InterruptIn::_irq_handler(intPtrTable[10].context); }
00168 static void pinInt11(void) { InterruptIn::_irq_handler(intPtrTable[11].context); }
00169 static void pinInt12(void) { InterruptIn::_irq_handler(intPtrTable[12].context); }
00170 static void pinInt13(void) { InterruptIn::_irq_handler(intPtrTable[13].context); }
00171 static void pinInt14(void) { InterruptIn::_irq_handler(intPtrTable[14].context); }
00172 static void pinInt15(void) { InterruptIn::_irq_handler(intPtrTable[15].context); }
00173 static void pinInt16(void) { InterruptIn::_irq_handler(intPtrTable[16].context); }
00174 static void pinInt17(void) { InterruptIn::_irq_handler(intPtrTable[17].context); }
00175 static void pinInt18(void) { InterruptIn::_irq_handler(intPtrTable[18].context); }
00176 static void pinInt19(void) { InterruptIn::_irq_handler(intPtrTable[19].context); }
00177 static void pinInt20(void) { InterruptIn::_irq_handler(intPtrTable[20].context); }
00178 static void pinInt21(void) { InterruptIn::_irq_handler(intPtrTable[21].context); }
00179 static void pinInt22(void) { InterruptIn::_irq_handler(intPtrTable[22].context); }
00180 static void pinInt23(void) { InterruptIn::_irq_handler(intPtrTable[23].context); }
00181 static void pinInt24(void) { InterruptIn::_irq_handler(intPtrTable[24].context); }
00182 static void pinInt25(void) { InterruptIn::_irq_handler(intPtrTable[25].context); }
00183 static void pinInt26(void) { InterruptIn::_irq_handler(intPtrTable[26].context); }
00184 static void pinInt27(void) { InterruptIn::_irq_handler(intPtrTable[27].context); }
00185 static void pinInt28(void) { InterruptIn::_irq_handler(intPtrTable[28].context); }
00186 static void pinInt29(void) { InterruptIn::_irq_handler(intPtrTable[29].context); }
00187 static void pinInt30(void) { InterruptIn::_irq_handler(intPtrTable[30].context); }
00188 static void pinInt31(void) { InterruptIn::_irq_handler(intPtrTable[31].context); }
00189 static void pinInt32(void) { InterruptIn::_irq_handler(intPtrTable[32].context); }
00190 static void pinInt33(void) { InterruptIn::_irq_handler(intPtrTable[33].context); }
00191 static void pinInt34(void) { InterruptIn::_irq_handler(intPtrTable[34].context); }
00192 static void pinInt35(void) { InterruptIn::_irq_handler(intPtrTable[35].context); }
00193 static void pinInt36(void) { InterruptIn::_irq_handler(intPtrTable[36].context); }
00194 static void pinInt37(void) { InterruptIn::_irq_handler(intPtrTable[37].context); }
00195 static void pinInt38(void) { InterruptIn::_irq_handler(intPtrTable[38].context); }
00196 static void pinInt39(void) { InterruptIn::_irq_handler(intPtrTable[39].context); }
00197 static void pinInt40(void) { InterruptIn::_irq_handler(intPtrTable[40].context); }
00198 static void pinInt41(void) { InterruptIn::_irq_handler(intPtrTable[41].context); }
00199 static void pinInt42(void) { InterruptIn::_irq_handler(intPtrTable[42].context); }
00200 static void pinInt43(void) { InterruptIn::_irq_handler(intPtrTable[43].context); }
00201 static void pinInt44(void) { InterruptIn::_irq_handler(intPtrTable[44].context); }
00202 static void pinInt45(void) { InterruptIn::_irq_handler(intPtrTable[45].context); }
00203 static void pinInt46(void) { InterruptIn::_irq_handler(intPtrTable[46].context); }
00204 static void pinInt47(void) { InterruptIn::_irq_handler(intPtrTable[47].context); }
00205 
00206 
00207 void wait_ms(uint32_t ms)
00208 {
00209     uint32_t start = ms_getTicker();
00210     
00211     while (true) {
00212         uint32_t t = ms_getTicker();
00213         if (t < start) // warp.
00214             start = 0;
00215         if (t > (start + ms))
00216             break;
00217     }
00218 }
00219 
00220 struct TimeoutVector TimeOuts[MAX_TIMEOUTS];
00221 
00222 void
00223 InterruptIn::rise(Callback<void()> func) {
00224     if (_gpioPin >= MAX_MCU_PINS-1)
00225         return;
00226     if (func) {
00227         _func = func;
00228         intPtrTable[_gpioPin].context = this;
00229         attachInterrupt(MYdigitalPinToInterrupt(_gpioPin), intPtrTable[_gpioPin].func, RISING);
00230     } else {
00231         _func = InterruptIn::donothing;
00232         intPtrTable[_gpioPin].context = NULL;
00233         detachInterrupt(_gpioPin);
00234     }
00235 };
00236 
00237 void
00238 InterruptIn::fall(Callback<void()> func) {
00239     if (func) {
00240         _func = func;
00241         intPtrTable[_gpioPin].context = this;
00242         attachInterrupt(MYdigitalPinToInterrupt(_gpioPin), intPtrTable[_gpioPin].func, FALLING);
00243     } else {
00244         _func = InterruptIn::donothing;
00245         intPtrTable[_gpioPin].context = NULL;
00246         detachInterrupt(_gpioPin);
00247     }
00248 }
00249 
00250 void
00251 InterruptIn::high(Callback<void()> func) {
00252     if (func) {
00253         _func = func;
00254         intPtrTable[_gpioPin].context = this;
00255         attachInterrupt(MYdigitalPinToInterrupt(_gpioPin), intPtrTable[_gpioPin].func, HIGH);
00256     } else {
00257         _func = InterruptIn::donothing;
00258         intPtrTable[_gpioPin].context = NULL;
00259         detachInterrupt(_gpioPin);
00260     }
00261 }
00262 
00263 void
00264 InterruptIn::low(Callback<void()> func) {
00265     if (func) {
00266         _func = func;
00267         intPtrTable[_gpioPin].context = this;
00268         attachInterrupt(MYdigitalPinToInterrupt(_gpioPin), intPtrTable[_gpioPin].func, LOW);
00269     } else {
00270         _func = InterruptIn::donothing;
00271         intPtrTable[_gpioPin].context = NULL;
00272         detachInterrupt(_gpioPin);
00273     }
00274 }
00275 
00276 
00277 
00278 
00279 uint32_t s_getTicker(void)
00280 {
00281     long long ns = ns_getTicker();
00282     ns /= (long long)1000000000; // to secs
00283     
00284     int secs = ns;
00285     return secs;
00286 }
00287 
00288 
00289 uint32_t ms_getTicker(void)
00290 {
00291     uint32_t us = us_getTicker();
00292     
00293     us /= 1000; // to ms
00294     return us;
00295 }
00296 
00297 uint32_t us_getTicker(void)
00298 {
00299     long long ns = ns_getTicker();
00300 
00301     ns /= (long long)1000; // to us
00302     uint32_t us = ns & 0xffffffff;
00303     
00304     return us;
00305 }
00306 
00307 
00308 void
00309 Timeout::insert(void)
00310 {
00311     noInterrupts();
00312     for (int i = 0; i < MAX_TIMEOUTS-1; i++) {
00313         struct TimeoutVector *tvp = &TimeOuts[i];
00314         if (tvp->timer == this) // already here, timer has been restartet.
00315             break;
00316         if (tvp->timer == NULL) {
00317             tvp->timer = this;
00318             break;
00319         }
00320     }
00321     interrupts();
00322 }
00323 
00324 void
00325 Timeout::remove(void)
00326 {
00327     noInterrupts();
00328     for (int i = 0; i < MAX_TIMEOUTS-1; i++) {
00329         struct TimeoutVector *tvp = &TimeOuts[i];
00330         if (tvp->timer == this) {
00331             tvp->timer = NULL;
00332             break;
00333         }
00334     }
00335     interrupts();
00336 }
00337 
00338 
00339 void
00340 Timeout::restart()
00341 {
00342     Tcc *t = getTimeout_tcc();
00343     uint64_t timeout = ~0;
00344     
00345     /*
00346      * find the lowest timeout value which is our the next timeout
00347      * zero means stop the timer.
00348      */
00349     noInterrupts();
00350     for (int i = 0; i < MAX_TIMEOUTS-1; i++) {
00351         struct TimeoutVector *tvp = &TimeOuts[i];
00352         if (tvp->timer) {
00353             if (tvp->timer->_timeout < timeout) {
00354                 timeout = tvp->timer->_timeout;
00355             }
00356         }
00357     }
00358     interrupts();
00359     
00360     if (timeout == (uint64_t)~0) {
00361         stopTimer(t);
00362         return;
00363     }
00364     
00365     uint64_t nsecs = ns_getTicker();
00366     
00367     if (timeout > nsecs) {
00368         startTimer(t, (uint64_t)timeout - (uint64_t)nsecs);
00369         return;
00370     } else {
00371         startTimer(t, (uint64_t)1); // just one nsec to trigger interrrupt
00372     }
00373 }
00374 
00375 #endif // ARDUINO