Lora Personalized device for Everynet

Dependencies:   LMiCPersonalizedforEverynet SX1276Lib X_NUCLEO_IKS01A1 cantcoap lwip mbed-rtos mbed

Fork of LoRaWAN-test-10secs by Alcatel-Lucent IoT Development

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers hal.cpp Source File

hal.cpp

00001 /*******************************************************************************
00002  * Copyright (c) 2014 IBM Corporation.
00003  * All rights reserved. This program and the accompanying materials
00004  * are made available under the terms of the Eclipse Public License v1.0
00005  * which accompanies this distribution, and is available at
00006  * http://www.eclipse.org/legal/epl-v10.html
00007  *
00008  * Contributors:
00009  *    IBM Zurich Research Lab - initial API, implementation and documentation
00010  *    Semtech Apps Team       - Modified to support the MBED sx1276 driver
00011  *                              library.
00012  *                              Possibility to use original or Semtech's MBED
00013  *                              radio driver. The selection is done by setting
00014  *                              USE_SMTC_RADIO_DRIVER preprocessing directive
00015  *                              in lmic.h
00016  *******************************************************************************/
00017 #include "mbed.h"
00018 #include "lmic.h"
00019 #include "mbed_debug.h"
00020 
00021 #if !USE_SMTC_RADIO_DRIVER
00022 
00023 extern void radio_irq_handler( u1_t dio );
00024 
00025 static DigitalOut nss( D10 );
00026 static SPI spi( D11, D12, D13 ); // ( mosi, miso, sclk )
00027  
00028 static DigitalInOut rst( A0 );
00029 static DigitalOut rxtx( A4 );
00030  
00031 static InterruptIn dio0( D2 );
00032 static InterruptIn dio1( D3 );
00033 static InterruptIn dio2( D4 ); 
00034 
00035 static void dio0Irq( void ) {
00036     radio_irq_handler( 0 );
00037 }
00038 
00039 static void dio1Irq( void ) {
00040     radio_irq_handler( 1 );
00041 }
00042 
00043 static void dio2Irq( void ) {
00044     radio_irq_handler( 2 );
00045 }
00046 
00047 #endif
00048 
00049 static u1_t irqlevel = 0;
00050 static u4_t ticks = 0;
00051 
00052 static Timer timer;
00053 static Ticker ticker;
00054 
00055 static void reset_timer( void ) {
00056     ticks += timer.read_us( ) >> 6;
00057     timer.reset( );
00058 }
00059 
00060 void hal_init( void ) {
00061      __disable_irq( );
00062      irqlevel = 0;
00063 
00064 #if !USE_SMTC_RADIO_DRIVER
00065     // configure input lines
00066     dio0.mode( PullDown );
00067     dio0.rise( dio0Irq );
00068     dio0.enable_irq( );
00069     dio1.mode( PullDown );   
00070     dio1.rise( dio1Irq );
00071     dio1.enable_irq( );
00072     dio2.mode( PullDown );
00073     dio2.rise( dio2Irq );
00074     dio2.enable_irq( );
00075     // configure reset line
00076     rst.input( );
00077     // configure spi
00078     spi.frequency( 8000000 );
00079     spi.format( 8, 0 );
00080     nss = 1;
00081 #endif
00082     // configure timer
00083     timer.start( );
00084     ticker.attach_us( reset_timer, 10000000 ); // reset timer every 10sec
00085      __enable_irq( );
00086 }
00087 
00088 #if !USE_SMTC_RADIO_DRIVER
00089 
00090 void hal_pin_rxtx( u1_t val ) {
00091     rxtx = !val;
00092 }
00093 
00094 void hal_pin_nss( u1_t val ) {
00095     nss = val;
00096 }
00097 
00098 void hal_pin_rst( u1_t val ) {
00099     if( val == 0 || val == 1 )
00100     { // drive pin
00101         rst.output( );
00102         rst = val;
00103     } 
00104     else
00105     { // keep pin floating
00106         rst.input( );
00107     }
00108 }
00109 
00110 u1_t hal_spi( u1_t out ) {
00111     return spi.write( out );
00112 }
00113 
00114 #endif
00115 
00116 void hal_disableIRQs( void ) {
00117     __disable_irq( );
00118     irqlevel++;
00119 }
00120 
00121 void hal_enableIRQs( void ) {
00122     if( --irqlevel == 0 )
00123     {
00124         __enable_irq( );
00125     }
00126 }
00127 
00128 void hal_sleep( void ) {
00129     // NOP
00130 }
00131 
00132 u4_t hal_ticks( void ) {
00133     hal_disableIRQs( );
00134     int t = ticks + ( timer.read_us( ) >> 6 );
00135     hal_enableIRQs( );
00136     return t;
00137 }
00138 
00139 static u2_t deltaticks( u4_t time ) {
00140     u4_t t = hal_ticks( );
00141     s4_t d = time - t;
00142     if( d <= 0 ) {
00143         return 0;    // in the past
00144     }
00145     if( ( d >> 16 ) != 0 ) {
00146         return 0xFFFF; // far ahead
00147     }
00148     return ( u2_t )d;
00149 }
00150 
00151 void hal_waitUntil( u4_t time ) {
00152     while( deltaticks( time ) != 0 ); // busy wait until timestamp is reached
00153 }
00154 
00155 u1_t hal_checkTimer( u4_t time ) {
00156     return ( deltaticks( time ) < 2 );
00157 }
00158 
00159 void hal_failed( void ) {
00160     while( 1 );
00161 }