Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: DHT11 LMiC SX1272Lib mbed
Fork of LoRaWAN-lmic-app by
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 * 00018 * Used by Giorgos Tsapparellas for Internet of Things (IoT) smart monitoring 00019 * device for agriculture using LoRaWAN technology. 00020 * 00021 * Date of issued copy: 25 January 2018 00022 * 00023 * Modifications: 00024 * - Modified to meet SX1272MB2xAS LoRa shield's pin allocations. 00025 * 00026 * Notice that, connectivity for SX1272MB2xAS LoRa shield is allocated as: 00027 * SX1272MB2xAS MBED Pin 00028 * SCK D13 00029 * MOSI D11 00030 * MISO D12 00031 * NSS D10 00032 * DIO0 D2 00033 * DIO1 D3 00034 * DIO2 D4 00035 * DIO3 D5 00036 * NRESET A0 00037 * 00038 * - Added some external comments for meeting good principles of 00039 * source code re-usability. 00040 *******************************************************************************/ 00041 00042 #include "mbed.h" 00043 #include "lmic.h" 00044 #include "mbed_debug.h" 00045 00046 #if !USE_SMTC_RADIO_DRIVER 00047 00048 extern void radio_irq_handler( u1_t dio ); 00049 00050 static DigitalOut nss( D10 ); // nss 00051 static SPI spi( D11, D12, D13 ); // ( mosi, miso, sclk ) 00052 00053 static DigitalInOut rst( A0 ); // rst (reset) 00054 static DigitalOut rxtx( A4 ); // rx and tx 00055 00056 static InterruptIn dio0( D2 ); // dio0 00057 static InterruptIn dio1( D3 ); // dio1 00058 static InterruptIn dio2( D4 ); // dio2 00059 00060 /* 00061 * dio0Irq function of type void. 00062 * 00063 * Input parameters: None 00064 * 00065 */ 00066 static void dio0Irq( void ) { 00067 radio_irq_handler( 0 ); 00068 }// end of dio0Irq function. 00069 00070 /* 00071 * dio1Irq function of type void. 00072 * 00073 * Input parameters: None 00074 * 00075 */ 00076 static void dio1Irq( void ) { 00077 radio_irq_handler( 1 ); 00078 }// end of dio1Irq funtion. 00079 00080 /* 00081 * dio2Irq function of type void. 00082 * 00083 * Input parameters: None 00084 * 00085 */ 00086 static void dio2Irq( void ) { 00087 radio_irq_handler( 2 ); 00088 }// end of dio2Irq function. 00089 00090 #endif 00091 00092 static u1_t irqlevel = 0; 00093 static u4_t ticks = 0; 00094 00095 static Timer timer; 00096 static Ticker ticker; 00097 00098 /* 00099 * reset_timer function of type void. 00100 * 00101 * Input parameters: None 00102 * 00103 */ 00104 static void reset_timer( void ) { 00105 ticks += timer.read_us( ) >> 6; 00106 timer.reset( ); 00107 }// end of reset_timer function. 00108 00109 /* 00110 * hal_init function of type void. 00111 * 00112 * Input parameters: None 00113 * 00114 */ 00115 void hal_init( void ) { 00116 __disable_irq( ); 00117 irqlevel = 0; 00118 00119 #if !USE_SMTC_RADIO_DRIVER 00120 // Configure input lines. 00121 dio0.mode( PullDown ); 00122 dio0.rise( dio0Irq ); 00123 dio0.enable_irq( ); 00124 dio1.mode( PullDown ); 00125 dio1.rise( dio1Irq ); 00126 dio1.enable_irq( ); 00127 dio2.mode( PullDown ); 00128 dio2.rise( dio2Irq ); 00129 dio2.enable_irq( ); 00130 // Configure reset line. 00131 rst.input( ); 00132 // Configure spi. 00133 spi.frequency( 8000000 ); 00134 spi.format( 8, 0 ); 00135 nss = 1; 00136 #endif 00137 // Configure timer. 00138 timer.start( ); 00139 ticker.attach_us( reset_timer, 10000000 ); // reset timer every 10sec 00140 __enable_irq( ); 00141 }// end of hal_init function. 00142 00143 #if !USE_SMTC_RADIO_DRIVER 00144 00145 /* 00146 * hal_pin_rxtx function of type void. 00147 * 00148 * Input parameters: unsigned char val 00149 * 00150 */ 00151 void hal_pin_rxtx( u1_t val ) { 00152 rxtx = !val; 00153 }// end of hal_pin_rxtx function. 00154 00155 /* 00156 * hal_pin_nss function of type void. 00157 * 00158 * Input parameters: unsigned char val 00159 * 00160 */ 00161 void hal_pin_nss( u1_t val ) { 00162 nss = val; 00163 }// end of hal_pin_nss function. 00164 00165 /* 00166 * hal_pin_rst function of type void. 00167 * 00168 * Input parameters: unsigned char val 00169 * 00170 */ 00171 void hal_pin_rst( u1_t val ) { 00172 if( val == 0 || val == 1 ) 00173 { // drive pin 00174 rst.output( ); 00175 rst = val; 00176 } 00177 else 00178 { // keep pin floating 00179 rst.input( ); 00180 } 00181 }//end of hal_pin_rst function. 00182 00183 /* 00184 * hal_spi function of type unsigned char. 00185 * 00186 * Input parameters: unsigned char out 00187 * 00188 * Return: spi out value 00189 */ 00190 u1_t hal_spi( u1_t out ) { 00191 return spi.write( out ); 00192 }// end of hal_spi function. 00193 00194 #endif 00195 00196 /* 00197 * hal_disableIRQs function of type void. 00198 * 00199 * Input parameters: None 00200 * 00201 */ 00202 void hal_disableIRQs( void ) { 00203 __disable_irq( ); 00204 irqlevel++; 00205 }// end of hal_disableIRQs function. 00206 00207 /* 00208 * hal_enableIRQs function of type void. 00209 * 00210 * Input parameters: None 00211 * 00212 */ 00213 void hal_enableIRQs( void ) { 00214 if( --irqlevel == 0 ) 00215 { 00216 __enable_irq( ); 00217 } 00218 }// end of hal_enableIRQs function. 00219 00220 /* 00221 * hal_sleep function of type void. 00222 * 00223 * Input parameters: None 00224 * 00225 */ 00226 void hal_sleep( void ) { 00227 // NOP 00228 }// end of hal_sleep function. 00229 00230 /* 00231 * hal_ticks function of type unsigned int. 00232 * 00233 * Input parameters: None 00234 * Return: t value 00235 * 00236 */ 00237 u4_t hal_ticks( void ) { 00238 hal_disableIRQs( ); 00239 int t = ticks + ( timer.read_us( ) >> 6 ); 00240 hal_enableIRQs( ); 00241 return t; 00242 }//end of hal_ticks function. 00243 00244 /* 00245 * deltaticks function of type unsigned short. 00246 * 00247 * Input parameters: unsigned int time 00248 * Return: d time 00249 */ 00250 static u2_t deltaticks( u4_t time ) { 00251 u4_t t = hal_ticks( ); 00252 s4_t d = time - t; 00253 if( d <= 0 ) { 00254 return 0; // in the past 00255 } 00256 if( ( d >> 16 ) != 0 ) { 00257 return 0xFFFF; // far ahead 00258 } 00259 return ( u2_t )d; 00260 }// end of deltaticks function. 00261 00262 /* 00263 * hal_waitUntil function of type void. 00264 * 00265 * Input parameters: unsigned int time 00266 * 00267 */ 00268 void hal_waitUntil( u4_t time ) { 00269 while( deltaticks( time ) != 0 ); // busy wait until timestamp is reached 00270 }// end of hal_waitUntil function. 00271 00272 /* 00273 * hal_checkTimer function of type unsigned char. 00274 * 00275 * Input parameters: unsigned int time 00276 * Return: deltaticks time 00277 * 00278 */ 00279 u1_t hal_checkTimer( u4_t time ) { 00280 return ( deltaticks( time ) < 2 ); 00281 }// end of hal_checkTimer function. 00282 00283 /* 00284 * hal_failed function of type void. 00285 * 00286 * Input parameters: None 00287 * 00288 */ 00289 void hal_failed( void ) { 00290 while( 1 ); 00291 }// end of hal_failed function.
Generated on Sat Jul 16 2022 05:48:06 by
