first draft

Dependencies:   LMiC SX1272Libx mbed

Fork of LoRaWAN-lmic-app by Semtech

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 "debug.h"
00020 
00021 #if !USE_SMTC_RADIO_DRIVER
00022 
00023 extern void radio_irq_handler( u1_t dio );
00024 
00025 static DigitalOut nss( PTD4 );
00026 static SPI spi( PTD6, PTD7, PTD5 ); // ( mosi, miso, sclk )
00027  
00028 static DigitalInOut rst( A0 );
00029 static DigitalOut rxtx( PTC6 );
00030  
00031 static InterruptIn dio0( PTC2 );
00032 static InterruptIn dio1( PTC4 );
00033 static InterruptIn dio2( PTC3 ); 
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 {
00057     debug("reset_timer enter\r\n");
00058     ticks += timer.read_us( ) >> 6;
00059     timer.reset( );
00060 }
00061 
00062 void hal_init( void ) 
00063 {
00064    debug("hal_init enter\r\n");
00065      __disable_irq( );
00066      irqlevel = 0;
00067 
00068 #if !USE_SMTC_RADIO_DRIVER
00069     // configure input lines
00070     dio0.mode( PullDown );
00071     dio0.rise( dio0Irq );
00072     dio0.enable_irq( );
00073     dio1.mode( PullDown );   
00074     dio1.rise( dio1Irq );
00075     dio1.enable_irq( );
00076     dio2.mode( PullDown );
00077     dio2.rise( dio2Irq );
00078     dio2.enable_irq( );
00079     // configure reset line
00080     rst.input( );
00081     // configure spi
00082  //   spi.frequency( 8000000 );
00083     spi.frequency( 1000000 );
00084     spi.format( 8, 0 );
00085     nss = 1;
00086 #endif
00087     // configure timer
00088     timer.start( );
00089     ticker.attach_us( reset_timer, 10000000 ); // reset timer every 10sec
00090      __enable_irq( );
00091    debug("hal_init exit\r\n");
00092 }
00093 
00094 #if !USE_SMTC_RADIO_DRIVER
00095 
00096 void hal_pin_rxtx( u1_t val ) 
00097 {
00098   debug("hal_pin_rxtx enter %d\r\n",val);
00099     rxtx = !val;
00100 }
00101 
00102 void hal_pin_nss( u1_t val ) 
00103 {
00104  debug("hal_pin_nss enter %d\r\n",val);
00105     nss = val;
00106 }
00107 
00108 void hal_pin_rst( u1_t val ) 
00109 {
00110 debug("hal_pin_rst enter %d\r\n",val);
00111     if( val == 0 || val == 1 )
00112     { // drive pin
00113         rst.output( );
00114         rst = val;
00115     } 
00116     else
00117     { // keep pin floating
00118         rst.input( );
00119     }
00120 }
00121 
00122 u1_t hal_spi( u1_t out ) 
00123 {
00124  debug("hal_spi enter\r\n");
00125    return spi.write( out );
00126 }
00127 
00128 #endif
00129 
00130 void hal_disableIRQs( void ) 
00131 {
00132  //debug("hal_disableIRQs enter\r\n");
00133    __disable_irq( );
00134     irqlevel++;
00135 }
00136 
00137 void hal_enableIRQs( void ) 
00138 {
00139 //debug("hal_enableIRQs enter\r\n");
00140     if( --irqlevel == 0 )
00141     {
00142         __enable_irq( );
00143     }
00144 }
00145 
00146 void hal_sleep( void ) 
00147 {
00148 //debug("hal_sleep enter\r\n");
00149     // NOP
00150 }
00151 
00152 u4_t hal_ticks( void ) 
00153 {
00154     hal_disableIRQs( );
00155     int t = ticks + ( timer.read_us( ) >> 6 );
00156     hal_enableIRQs( );
00157 //debug("hal_ticks exit %d\r\n",t);
00158     return t;
00159 }
00160 
00161 static u2_t deltaticks( u4_t time ) 
00162 {
00163 //    debug("deltaticks enter\r\n");
00164     u4_t t = hal_ticks( );
00165     s4_t d = time - t;
00166     if( d <= 0 ) {
00167         return 0;    // in the past
00168     }
00169     if( ( d >> 16 ) != 0 ) {
00170         return 0xFFFF; // far ahead
00171     }
00172     return ( u2_t )d;
00173 }
00174 
00175 void hal_waitUntil( u4_t time ) 
00176 {
00177     debug("hal_waitUntil enter\r\n");
00178     while( deltaticks( time ) != 0 ); // busy wait until timestamp is reached
00179 }
00180 
00181 u1_t hal_checkTimer( u4_t time ) 
00182 {
00183 //    debug("hal_checkTimer enter\r\n");
00184     return ( deltaticks( time ) < 2 );
00185 }
00186 
00187 void hal_failed( void ) 
00188 {
00189    debug("hal_failed enter\r\n");
00190     while( 1 );
00191 }