C++ Wrapper around the IBM LMiC LoRaWAN implementation

Dependencies:   LMiC SX1276Lib

Committer:
sillevl
Date:
Thu Jun 30 17:32:14 2016 +0000
Revision:
1:7cfc59ba0797
Parent:
hal.cpp@0:70cd1a15fe4a
otaa join working

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sillevl 0:70cd1a15fe4a 1 #include "mbed.h"
sillevl 0:70cd1a15fe4a 2 #include "lmic.h"
sillevl 0:70cd1a15fe4a 3 #include "mbed_debug.h"
sillevl 0:70cd1a15fe4a 4
sillevl 0:70cd1a15fe4a 5 static u1_t irqlevel = 0;
sillevl 0:70cd1a15fe4a 6 static u4_t ticks = 0;
sillevl 0:70cd1a15fe4a 7
sillevl 0:70cd1a15fe4a 8 static Timer timer;
sillevl 0:70cd1a15fe4a 9 static Ticker ticker;
sillevl 0:70cd1a15fe4a 10
sillevl 0:70cd1a15fe4a 11 static void reset_timer( void ) {
sillevl 0:70cd1a15fe4a 12 ticks += timer.read_us( ) >> 6;
sillevl 0:70cd1a15fe4a 13 timer.reset( );
sillevl 0:70cd1a15fe4a 14 }
sillevl 0:70cd1a15fe4a 15
sillevl 0:70cd1a15fe4a 16 void hal_init( void ) {
sillevl 0:70cd1a15fe4a 17 __disable_irq( );
sillevl 0:70cd1a15fe4a 18 irqlevel = 0;
sillevl 0:70cd1a15fe4a 19
sillevl 0:70cd1a15fe4a 20 // configure timer
sillevl 0:70cd1a15fe4a 21 timer.start( );
sillevl 0:70cd1a15fe4a 22 ticker.attach_us( reset_timer, 10000000 ); // reset timer every 10sec
sillevl 0:70cd1a15fe4a 23 __enable_irq( );
sillevl 0:70cd1a15fe4a 24 }
sillevl 0:70cd1a15fe4a 25
sillevl 0:70cd1a15fe4a 26 void hal_disableIRQs( void ) {
sillevl 0:70cd1a15fe4a 27 __disable_irq( );
sillevl 0:70cd1a15fe4a 28 irqlevel++;
sillevl 0:70cd1a15fe4a 29 }
sillevl 0:70cd1a15fe4a 30
sillevl 0:70cd1a15fe4a 31 void hal_enableIRQs( void ) {
sillevl 0:70cd1a15fe4a 32 if( --irqlevel == 0 )
sillevl 0:70cd1a15fe4a 33 {
sillevl 0:70cd1a15fe4a 34 __enable_irq( );
sillevl 0:70cd1a15fe4a 35 }
sillevl 0:70cd1a15fe4a 36 }
sillevl 0:70cd1a15fe4a 37
sillevl 0:70cd1a15fe4a 38 void hal_sleep( void ) {
sillevl 0:70cd1a15fe4a 39 // NOP
sillevl 0:70cd1a15fe4a 40 }
sillevl 0:70cd1a15fe4a 41
sillevl 0:70cd1a15fe4a 42 u4_t hal_ticks( void ) {
sillevl 0:70cd1a15fe4a 43 hal_disableIRQs( );
sillevl 0:70cd1a15fe4a 44 int t = ticks + ( timer.read_us( ) >> 6 );
sillevl 0:70cd1a15fe4a 45 hal_enableIRQs( );
sillevl 0:70cd1a15fe4a 46 return t;
sillevl 0:70cd1a15fe4a 47 }
sillevl 0:70cd1a15fe4a 48
sillevl 0:70cd1a15fe4a 49 static u2_t deltaticks( u4_t time ) {
sillevl 0:70cd1a15fe4a 50 u4_t t = hal_ticks( );
sillevl 0:70cd1a15fe4a 51 s4_t d = time - t;
sillevl 0:70cd1a15fe4a 52 if( d <= 0 ) {
sillevl 0:70cd1a15fe4a 53 return 0; // in the past
sillevl 0:70cd1a15fe4a 54 }
sillevl 0:70cd1a15fe4a 55 if( ( d >> 16 ) != 0 ) {
sillevl 0:70cd1a15fe4a 56 return 0xFFFF; // far ahead
sillevl 0:70cd1a15fe4a 57 }
sillevl 0:70cd1a15fe4a 58 return ( u2_t )d;
sillevl 0:70cd1a15fe4a 59 }
sillevl 0:70cd1a15fe4a 60
sillevl 0:70cd1a15fe4a 61 void hal_waitUntil( u4_t time ) {
sillevl 0:70cd1a15fe4a 62 while( deltaticks( time ) != 0 ); // busy wait until timestamp is reached
sillevl 0:70cd1a15fe4a 63 }
sillevl 0:70cd1a15fe4a 64
sillevl 0:70cd1a15fe4a 65 u1_t hal_checkTimer( u4_t time ) {
sillevl 0:70cd1a15fe4a 66 return ( deltaticks( time ) < 2 );
sillevl 0:70cd1a15fe4a 67 }
sillevl 0:70cd1a15fe4a 68
sillevl 0:70cd1a15fe4a 69 void hal_failed( void ) {
sillevl 0:70cd1a15fe4a 70 while( 1 );
sillevl 0:70cd1a15fe4a 71 }