Nagaraj Krishnamurthy / LoRaWAN-NAMote72-Application-Demo_IoTium

Dependencies:   LoRaWAN-lib SX1272Lib lib_gps lib_mma8451q lib_mpl3115a2 mbed

Fork of LoRaWAN-NAMote72-Application-Demo_Multitech by Nagaraj Krishnamurthy

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers board.cpp Source File

board.cpp

00001 /*
00002  / _____)             _              | |
00003 ( (____  _____ ____ _| |_ _____  ____| |__
00004  \____ \| ___ |    (_   _) ___ |/ ___)  _ \
00005  _____) ) ____| | | || |_| ____( (___| | | |
00006 (______/|_____)_|_|_| \__)_____)\____)_| |_|
00007     (C)2015 Semtech
00008 
00009 Description: Target board general functions implementation
00010 
00011 License: Revised BSD License, see LICENSE.TXT file include in the project
00012 
00013 Maintainer: Miguel Luis and Gregory Cristian
00014 */
00015 #include "mbed.h"
00016 #include "board.h"
00017 
00018 MoteVersion_t BoardGetVersion( void );
00019 
00020 DigitalOut RedLed( PB_1 );     // Active Low
00021 DigitalOut YellowLed( PB_10 ); // Active Low
00022 DigitalOut GreenLed( PC_3 );   // Active Low
00023 DigitalOut UsrLed( PA_5 );     // Active High
00024 
00025 
00026 GPS Gps( PB_6, PB_7, PB_11 ); // Gps(tx, rx, en);
00027 
00028 DigitalIn I2cInterrupt( PB_4 );
00029 I2C I2c(I2C_SDA, I2C_SCL);
00030 
00031 MPL3115A2 Mpl3115a2( I2c, I2cInterrupt );
00032 MMA8451Q Mma8451q(I2c, I2cInterrupt);
00033 
00034 DigitalOut Pc7( PC_7 );
00035 DigitalIn Pc1( PC_1 );
00036 
00037 // Used for Push button application demo
00038 DigitalIn PC0( PC_0, PullUp ); 
00039 
00040 AnalogIn *Battery;
00041 
00042 #define AIN_VREF            3.3     // STM32 internal refernce
00043 #define AIN_VBAT_DIV        2       // Resistor divider
00044 
00045 SX1272MB2xAS Radio( NULL );
00046 
00047 void BoardInit( void )
00048 {
00049     // Initalize LEDs
00050     RedLed = 1;     // Active Low
00051     GreenLed = 1;   // Active Low
00052     YellowLed = 1; // Active Low
00053     UsrLed = 0;     // Active High
00054 
00055     TimerTimeCounterInit( );
00056 
00057     switch( BoardGetVersion( ) )
00058     {
00059         case MOTE_VERSION_2:
00060             Battery = new AnalogIn( PA_0 );
00061             Gps.en_invert = true;
00062             break;
00063         case MOTE_VERSION_3:
00064             Battery = new AnalogIn( PA_1 );
00065             Gps.en_invert = false;
00066             break;
00067         default:
00068             break;
00069     }
00070     Gps.init( );
00071     Gps.enable( 1 );
00072     
00073     Mpl3115a2.init( );
00074     Mma8451q.orient_detect( );
00075 }
00076 
00077 
00078 uint8_t BoardGetBatteryLevel( void ) 
00079 {
00080     // Per LoRaWAN spec; 0 = Charging; 1...254 = level, 255 = N/A
00081     return ( Battery->read_u16( ) >> 8 ) + ( Battery->read_u16( ) >> 9 );
00082 }
00083 
00084 float BoardGetBatteryVoltage( void ) 
00085 {
00086     return ( Battery->read( ) * AIN_VREF * AIN_VBAT_DIV );
00087 }
00088 
00089 uint32_t BoardGetRandomSeed( void )
00090 {
00091     return ( ( *( uint32_t* )ID1 ) ^ ( *( uint32_t* )ID2 ) ^ ( *( uint32_t* )ID3 ) );
00092 }
00093 
00094 void BoardGetDevEUI( uint8_t *id )
00095 {
00096     uint32_t *pDevEuiHWord = ( uint32_t* )&id[4];
00097 
00098     if( *pDevEuiHWord == 0 )
00099     {        
00100         *pDevEuiHWord = BoardGetRandomSeed( );
00101     }
00102     
00103 }
00104 
00105 void BoardGetUniqueId( uint8_t *id )
00106 {
00107     id[7] = ( ( *( uint32_t* )ID1 )+ ( *( uint32_t* )ID3 ) ) >> 24;
00108     id[6] = ( ( *( uint32_t* )ID1 )+ ( *( uint32_t* )ID3 ) ) >> 16;
00109     id[5] = ( ( *( uint32_t* )ID1 )+ ( *( uint32_t* )ID3 ) ) >> 8;
00110     id[4] = ( ( *( uint32_t* )ID1 )+ ( *( uint32_t* )ID3 ) );
00111     id[3] = ( ( *( uint32_t* )ID2 ) ) >> 24;
00112     id[2] = ( ( *( uint32_t* )ID2 ) ) >> 16;
00113     id[1] = ( ( *( uint32_t* )ID2 ) ) >> 8;
00114     id[0] = ( ( *( uint32_t* )ID2 ) );
00115 }
00116 
00117 MoteVersion_t BoardGetVersion( void )
00118 {
00119     Pc7 = 1;
00120     char first = Pc1;
00121     Pc7 = 0;
00122     
00123     if( first && !Pc1 )
00124     {
00125         return MOTE_VERSION_2;
00126     }
00127     else
00128     {
00129         return MOTE_VERSION_3;
00130     }
00131 }