first draft

Dependencies:   LMiC SX1272Libx mbed

Fork of LoRaWAN-lmic-app by Semtech

Committer:
tmulrooney
Date:
Tue Feb 23 15:42:41 2016 +0000
Revision:
6:fc465060b63e
Parent:
1:60184eda0066
first draft

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mluis 1:60184eda0066 1 /*******************************************************************************
mluis 1:60184eda0066 2 * Copyright (c) 2014 IBM Corporation.
mluis 1:60184eda0066 3 * All rights reserved. This program and the accompanying materials
mluis 1:60184eda0066 4 * are made available under the terms of the Eclipse Public License v1.0
mluis 1:60184eda0066 5 * which accompanies this distribution, and is available at
mluis 1:60184eda0066 6 * http://www.eclipse.org/legal/epl-v10.html
mluis 1:60184eda0066 7 *
mluis 1:60184eda0066 8 * Contributors:
mluis 1:60184eda0066 9 * IBM Zurich Research Lab - initial API, implementation and documentation
mluis 1:60184eda0066 10 * Semtech Apps Team - Modified to support the MBED sx1276 driver
mluis 1:60184eda0066 11 * library.
mluis 1:60184eda0066 12 * Possibility to use original or Semtech's MBED
mluis 1:60184eda0066 13 * radio driver. The selection is done by setting
mluis 1:60184eda0066 14 * USE_SMTC_RADIO_DRIVER preprocessing directive
mluis 1:60184eda0066 15 * in lmic.h
mluis 1:60184eda0066 16 *******************************************************************************/
mluis 1:60184eda0066 17 #include "mbed.h"
mluis 1:60184eda0066 18 #include "lmic.h"
tmulrooney 6:fc465060b63e 19 #include "debug.h"
mluis 1:60184eda0066 20
mluis 1:60184eda0066 21 #if !USE_SMTC_RADIO_DRIVER
mluis 1:60184eda0066 22
mluis 1:60184eda0066 23 extern void radio_irq_handler( u1_t dio );
mluis 1:60184eda0066 24
tmulrooney 6:fc465060b63e 25 static DigitalOut nss( PTD4 );
tmulrooney 6:fc465060b63e 26 static SPI spi( PTD6, PTD7, PTD5 ); // ( mosi, miso, sclk )
mluis 1:60184eda0066 27
mluis 1:60184eda0066 28 static DigitalInOut rst( A0 );
tmulrooney 6:fc465060b63e 29 static DigitalOut rxtx( PTC6 );
mluis 1:60184eda0066 30
tmulrooney 6:fc465060b63e 31 static InterruptIn dio0( PTC2 );
tmulrooney 6:fc465060b63e 32 static InterruptIn dio1( PTC4 );
tmulrooney 6:fc465060b63e 33 static InterruptIn dio2( PTC3 );
mluis 1:60184eda0066 34
mluis 1:60184eda0066 35 static void dio0Irq( void ) {
mluis 1:60184eda0066 36 radio_irq_handler( 0 );
mluis 1:60184eda0066 37 }
mluis 1:60184eda0066 38
mluis 1:60184eda0066 39 static void dio1Irq( void ) {
mluis 1:60184eda0066 40 radio_irq_handler( 1 );
mluis 1:60184eda0066 41 }
mluis 1:60184eda0066 42
mluis 1:60184eda0066 43 static void dio2Irq( void ) {
mluis 1:60184eda0066 44 radio_irq_handler( 2 );
mluis 1:60184eda0066 45 }
mluis 1:60184eda0066 46
mluis 1:60184eda0066 47 #endif
mluis 1:60184eda0066 48
mluis 1:60184eda0066 49 static u1_t irqlevel = 0;
mluis 1:60184eda0066 50 static u4_t ticks = 0;
mluis 1:60184eda0066 51
mluis 1:60184eda0066 52 static Timer timer;
mluis 1:60184eda0066 53 static Ticker ticker;
mluis 1:60184eda0066 54
tmulrooney 6:fc465060b63e 55 static void reset_timer( void )
tmulrooney 6:fc465060b63e 56 {
tmulrooney 6:fc465060b63e 57 debug("reset_timer enter\r\n");
mluis 1:60184eda0066 58 ticks += timer.read_us( ) >> 6;
mluis 1:60184eda0066 59 timer.reset( );
mluis 1:60184eda0066 60 }
mluis 1:60184eda0066 61
tmulrooney 6:fc465060b63e 62 void hal_init( void )
tmulrooney 6:fc465060b63e 63 {
tmulrooney 6:fc465060b63e 64 debug("hal_init enter\r\n");
mluis 1:60184eda0066 65 __disable_irq( );
mluis 1:60184eda0066 66 irqlevel = 0;
mluis 1:60184eda0066 67
mluis 1:60184eda0066 68 #if !USE_SMTC_RADIO_DRIVER
mluis 1:60184eda0066 69 // configure input lines
mluis 1:60184eda0066 70 dio0.mode( PullDown );
mluis 1:60184eda0066 71 dio0.rise( dio0Irq );
mluis 1:60184eda0066 72 dio0.enable_irq( );
mluis 1:60184eda0066 73 dio1.mode( PullDown );
mluis 1:60184eda0066 74 dio1.rise( dio1Irq );
mluis 1:60184eda0066 75 dio1.enable_irq( );
mluis 1:60184eda0066 76 dio2.mode( PullDown );
mluis 1:60184eda0066 77 dio2.rise( dio2Irq );
mluis 1:60184eda0066 78 dio2.enable_irq( );
mluis 1:60184eda0066 79 // configure reset line
mluis 1:60184eda0066 80 rst.input( );
mluis 1:60184eda0066 81 // configure spi
tmulrooney 6:fc465060b63e 82 // spi.frequency( 8000000 );
tmulrooney 6:fc465060b63e 83 spi.frequency( 1000000 );
mluis 1:60184eda0066 84 spi.format( 8, 0 );
mluis 1:60184eda0066 85 nss = 1;
mluis 1:60184eda0066 86 #endif
mluis 1:60184eda0066 87 // configure timer
mluis 1:60184eda0066 88 timer.start( );
mluis 1:60184eda0066 89 ticker.attach_us( reset_timer, 10000000 ); // reset timer every 10sec
mluis 1:60184eda0066 90 __enable_irq( );
tmulrooney 6:fc465060b63e 91 debug("hal_init exit\r\n");
mluis 1:60184eda0066 92 }
mluis 1:60184eda0066 93
mluis 1:60184eda0066 94 #if !USE_SMTC_RADIO_DRIVER
mluis 1:60184eda0066 95
tmulrooney 6:fc465060b63e 96 void hal_pin_rxtx( u1_t val )
tmulrooney 6:fc465060b63e 97 {
tmulrooney 6:fc465060b63e 98 debug("hal_pin_rxtx enter %d\r\n",val);
mluis 1:60184eda0066 99 rxtx = !val;
mluis 1:60184eda0066 100 }
mluis 1:60184eda0066 101
tmulrooney 6:fc465060b63e 102 void hal_pin_nss( u1_t val )
tmulrooney 6:fc465060b63e 103 {
tmulrooney 6:fc465060b63e 104 debug("hal_pin_nss enter %d\r\n",val);
mluis 1:60184eda0066 105 nss = val;
mluis 1:60184eda0066 106 }
mluis 1:60184eda0066 107
tmulrooney 6:fc465060b63e 108 void hal_pin_rst( u1_t val )
tmulrooney 6:fc465060b63e 109 {
tmulrooney 6:fc465060b63e 110 debug("hal_pin_rst enter %d\r\n",val);
mluis 1:60184eda0066 111 if( val == 0 || val == 1 )
mluis 1:60184eda0066 112 { // drive pin
mluis 1:60184eda0066 113 rst.output( );
mluis 1:60184eda0066 114 rst = val;
mluis 1:60184eda0066 115 }
mluis 1:60184eda0066 116 else
mluis 1:60184eda0066 117 { // keep pin floating
mluis 1:60184eda0066 118 rst.input( );
mluis 1:60184eda0066 119 }
mluis 1:60184eda0066 120 }
mluis 1:60184eda0066 121
tmulrooney 6:fc465060b63e 122 u1_t hal_spi( u1_t out )
tmulrooney 6:fc465060b63e 123 {
tmulrooney 6:fc465060b63e 124 debug("hal_spi enter\r\n");
tmulrooney 6:fc465060b63e 125 return spi.write( out );
mluis 1:60184eda0066 126 }
mluis 1:60184eda0066 127
mluis 1:60184eda0066 128 #endif
mluis 1:60184eda0066 129
tmulrooney 6:fc465060b63e 130 void hal_disableIRQs( void )
tmulrooney 6:fc465060b63e 131 {
tmulrooney 6:fc465060b63e 132 //debug("hal_disableIRQs enter\r\n");
tmulrooney 6:fc465060b63e 133 __disable_irq( );
mluis 1:60184eda0066 134 irqlevel++;
mluis 1:60184eda0066 135 }
mluis 1:60184eda0066 136
tmulrooney 6:fc465060b63e 137 void hal_enableIRQs( void )
tmulrooney 6:fc465060b63e 138 {
tmulrooney 6:fc465060b63e 139 //debug("hal_enableIRQs enter\r\n");
mluis 1:60184eda0066 140 if( --irqlevel == 0 )
mluis 1:60184eda0066 141 {
mluis 1:60184eda0066 142 __enable_irq( );
mluis 1:60184eda0066 143 }
mluis 1:60184eda0066 144 }
mluis 1:60184eda0066 145
tmulrooney 6:fc465060b63e 146 void hal_sleep( void )
tmulrooney 6:fc465060b63e 147 {
tmulrooney 6:fc465060b63e 148 //debug("hal_sleep enter\r\n");
mluis 1:60184eda0066 149 // NOP
mluis 1:60184eda0066 150 }
mluis 1:60184eda0066 151
tmulrooney 6:fc465060b63e 152 u4_t hal_ticks( void )
tmulrooney 6:fc465060b63e 153 {
mluis 1:60184eda0066 154 hal_disableIRQs( );
mluis 1:60184eda0066 155 int t = ticks + ( timer.read_us( ) >> 6 );
mluis 1:60184eda0066 156 hal_enableIRQs( );
tmulrooney 6:fc465060b63e 157 //debug("hal_ticks exit %d\r\n",t);
mluis 1:60184eda0066 158 return t;
mluis 1:60184eda0066 159 }
mluis 1:60184eda0066 160
tmulrooney 6:fc465060b63e 161 static u2_t deltaticks( u4_t time )
tmulrooney 6:fc465060b63e 162 {
tmulrooney 6:fc465060b63e 163 // debug("deltaticks enter\r\n");
mluis 1:60184eda0066 164 u4_t t = hal_ticks( );
mluis 1:60184eda0066 165 s4_t d = time - t;
mluis 1:60184eda0066 166 if( d <= 0 ) {
mluis 1:60184eda0066 167 return 0; // in the past
mluis 1:60184eda0066 168 }
mluis 1:60184eda0066 169 if( ( d >> 16 ) != 0 ) {
mluis 1:60184eda0066 170 return 0xFFFF; // far ahead
mluis 1:60184eda0066 171 }
mluis 1:60184eda0066 172 return ( u2_t )d;
mluis 1:60184eda0066 173 }
mluis 1:60184eda0066 174
tmulrooney 6:fc465060b63e 175 void hal_waitUntil( u4_t time )
tmulrooney 6:fc465060b63e 176 {
tmulrooney 6:fc465060b63e 177 debug("hal_waitUntil enter\r\n");
mluis 1:60184eda0066 178 while( deltaticks( time ) != 0 ); // busy wait until timestamp is reached
mluis 1:60184eda0066 179 }
mluis 1:60184eda0066 180
tmulrooney 6:fc465060b63e 181 u1_t hal_checkTimer( u4_t time )
tmulrooney 6:fc465060b63e 182 {
tmulrooney 6:fc465060b63e 183 // debug("hal_checkTimer enter\r\n");
mluis 1:60184eda0066 184 return ( deltaticks( time ) < 2 );
mluis 1:60184eda0066 185 }
mluis 1:60184eda0066 186
tmulrooney 6:fc465060b63e 187 void hal_failed( void )
tmulrooney 6:fc465060b63e 188 {
tmulrooney 6:fc465060b63e 189 debug("hal_failed enter\r\n");
mluis 1:60184eda0066 190 while( 1 );
mluis 1:60184eda0066 191 }