Button Test 2
Dependencies: mbed LoRaWAN-lib SX1276Lib
Revision 0:92bca02df485, committed 2015-10-20
- Comitter:
- mluis
- Date:
- Tue Oct 20 13:23:35 2015 +0000
- Child:
- 1:352f608c3337
- Commit message:
- LoRaWAN-demo project creation using LoRaWAN-lib library
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/app/SerialDisplay.cpp Tue Oct 20 13:23:35 2015 +0000 @@ -0,0 +1,469 @@ +/* + / _____) _ | | +( (____ _____ ____ _| |_ _____ ____| |__ + \____ \| ___ | (_ _) ___ |/ ___) _ \ + _____) ) ____| | | || |_| ____( (___| | | | +(______/|_____)_|_|_| \__)_____)\____)_| |_| + (C)2015 Semtech + +Description: VT100 serial display management + +License: Revised BSD License, see LICENSE.TXT file include in the project + +Maintainer: Miguel Luis and Gregory Cristian +*/ +#include "board.h" +#include "vt100.h" +#include "SerialDisplay.h" + +//extern struct sLoRaMacUplinkStatus +//{ +// uint8_t Acked; +// int8_t Datarate; +// uint16_t UplinkCounter; +// uint8_t Port; +// uint8_t *Buffer; +// uint8_t BufferSize; +//}LoRaMacUplinkStatus; +// +//extern struct sLoRaMacDownlinkStatus +//{ +// int16_t Rssi; +// int8_t Snr; +// uint16_t DownlinkCounter; +// bool RxData; +// uint8_t Port; +// uint8_t *Buffer; +// uint8_t BufferSize; +//}LoRaMacDownlinkStatus; + +VT100 vt( USBTX, USBRX ); + +void SerialPrintCheckBox( bool activated, uint8_t color ) +{ + if( activated == true ) + { + vt.SetAttribute( VT100::ATTR_OFF, color, color ); + } + else + { + vt.SetAttribute( VT100::ATTR_OFF ); + } + vt.printf( " " ); + vt.SetAttribute( VT100::ATTR_OFF ); +} + +void SerialDisplayUpdateActivationMode( bool otaa ) +{ + vt.SetCursorPos( 4, 17 ); + SerialPrintCheckBox( otaa, VT100::WHITE ); + vt.SetCursorPos( 9, 17 ); + SerialPrintCheckBox( !otaa, VT100::WHITE ); +} + +void SerialDisplayUpdateEui( uint8_t line, uint8_t *eui ) +{ // + // REMARK + // + // EUIs must be displayed in reverse order + // + vt.SetCursorPos( line, 27 ); + for( uint8_t i = 0; i < 8; i++ ) + { + vt.printf( "%02X ", eui[7 - i] ); + } + vt.SetCursorPos( line, 50 ); + vt.printf( "]" ); +} + +void SerialDisplayUpdateKey( uint8_t line, uint8_t *key ) +{ + vt.SetCursorPos( line, 27 ); + for( uint8_t i = 0; i < 16; i++ ) + { + vt.printf( "%02X ", key[i] ); + } + vt.SetCursorPos( line, 74 ); + vt.printf( "]" ); +} + +void SerialDisplayUpdateNwkId( uint8_t id ) +{ + vt.SetCursorPos( 10, 27 ); + vt.printf( "%03d", id ); +} + +void SerialDisplayUpdateDevAddr( uint32_t addr ) +{ + vt.SetCursorPos( 11, 27 ); + vt.printf( "%02X %02X %02X %02X", ( addr >> 24 ) & 0xFF, ( addr >> 16 ) & 0xFF, ( addr >> 8 ) & 0xFF, addr & 0xFF ); +} + +void SerialDisplayUpdateFrameType( bool confirmed ) +{ + vt.SetCursorPos( 15, 17 ); + SerialPrintCheckBox( confirmed, VT100::WHITE ); + vt.SetCursorPos( 15, 32 ); + SerialPrintCheckBox( !confirmed, VT100::WHITE ); +} + +void SerialDisplayUpdateAdr( bool adr ) +{ + vt.SetCursorPos( 16, 27 ); + if( adr == true ) + { + vt.printf( " ON" ); + } + else + { + vt.printf( "OFF" ); + } +} + +void SerialDisplayUpdateDutyCycle( bool dutyCycle ) +{ + vt.SetCursorPos( 17, 27 ); + if( dutyCycle == true ) + { + vt.printf( " ON" ); + } + else + { + vt.printf( "OFF" ); + } +} + +void SerialDisplayUpdatePublicNetwork( bool network ) +{ + vt.SetCursorPos( 19, 17 ); + SerialPrintCheckBox( network, VT100::WHITE ); + vt.SetCursorPos( 19, 30 ); + SerialPrintCheckBox( !network, VT100::WHITE ); +} + +void SerialDisplayUpdateNetworkIsJoined( bool state ) +{ + vt.SetCursorPos( 20, 17 ); + SerialPrintCheckBox( !state, VT100::RED ); + vt.SetCursorPos( 20, 30 ); + SerialPrintCheckBox( state, VT100::GREEN ); +} + +void SerialDisplayUpdateLedState( uint8_t id, uint8_t state ) +{ + switch( id ) + { + case 1: + vt.SetCursorPos( 22, 17 ); + SerialPrintCheckBox( state, VT100::RED ); + break; + case 2: + vt.SetCursorPos( 22, 31 ); + SerialPrintCheckBox( state, VT100::GREEN ); + break; + case 3: + vt.SetCursorPos( 22, 45 ); + SerialPrintCheckBox( state, VT100::BLUE ); + break; + } +} + +void SerialDisplayUpdateData( uint8_t line, uint8_t *buffer, uint8_t size ) +{ + if( size != 0 ) + { + vt.SetCursorPos( line, 27 ); + for( uint8_t i = 0; i < size; i++ ) + { + vt.printf( "%02X ", buffer[i] ); + if( ( ( i + 1 ) % 16 ) == 0 ) + { + line++; + vt.SetCursorPos( line, 27 ); + } + } + for( uint8_t i = size; i < 64; i++ ) + { + vt.printf( "__ " ); + if( ( ( i + 1 ) % 16 ) == 0 ) + { + line++; + vt.SetCursorPos( line, 27 ); + } + } + vt.SetCursorPos( line - 1, 74 ); + vt.printf( "]" ); + } + else + { + vt.SetCursorPos( line, 27 ); + for( uint8_t i = 0; i < 64; i++ ) + { + vt.printf( "__ " ); + if( ( ( i + 1 ) % 16 ) == 0 ) + { + line++; + vt.SetCursorPos( line, 27 ); + } + } + vt.SetCursorPos( line - 1, 74 ); + vt.printf( "]" ); + } +} + +void SerialDisplayUpdateUplinkAcked( bool state ) +{ + vt.SetCursorPos( 24, 36 ); + SerialPrintCheckBox( state, VT100::GREEN ); +} + +void SerialDisplayUpdateUplink( bool acked, uint8_t datarate, uint16_t counter, uint8_t port, uint8_t *buffer, uint8_t bufferSize ) +{ + // Acked + SerialDisplayUpdateUplinkAcked( acked ); + // Datarate + vt.SetCursorPos( 25, 33 ); + vt.printf( "DR%d", datarate ); + // Counter + vt.SetCursorPos( 26, 27 ); + vt.printf( "%10d", counter ); + // Port + vt.SetCursorPos( 27, 34 ); + vt.printf( "%3d", port ); + // Data + SerialDisplayUpdateData( 28, buffer, bufferSize ); + // Help message + vt.SetCursorPos( 42, 1 ); + vt.printf( "To refresh screen please hit 'r' key.\r\n" ); +} + +void SerialDisplayUpdateDonwlinkRxData( bool state ) +{ + vt.SetCursorPos( 34, 4 ); + SerialPrintCheckBox( state, VT100::GREEN ); +} + +void SerialDisplayUpdateDownlink( bool rxData, int16_t rssi, int8_t snr, uint16_t counter, uint8_t port, uint8_t *buffer, uint8_t bufferSize ) +{ + // Rx data + SerialDisplayUpdateDonwlinkRxData( rxData ); + // RSSI + vt.SetCursorPos( 33, 32 ); + vt.printf( "%5d", rssi ); + // SNR + vt.SetCursorPos( 34, 32 ); + vt.printf( "%5d", snr ); + // Counter + vt.SetCursorPos( 35, 27 ); + vt.printf( "%10d", counter ); + if( rxData == true ) + { + // Port + vt.SetCursorPos( 36, 34 ); + vt.printf( "%3d", port ); + // Data + SerialDisplayUpdateData( 37, buffer, bufferSize ); + } + else + { + // Port + vt.SetCursorPos( 36, 34 ); + vt.printf( " " ); + // Data + SerialDisplayUpdateData( 37, NULL, 0 ); + } +} + +void SerialDisplayDrawFirstLine( void ) +{ + vt.PutBoxDrawingChar( 'l' ); + for( int8_t i = 0; i <= 77; i++ ) + { + vt.PutBoxDrawingChar( 'q' ); + } + vt.PutBoxDrawingChar( 'k' ); + vt.printf( "\r\n" ); +} + +void SerialDisplayDrawTitle( const char* title ) +{ + vt.PutBoxDrawingChar( 'x' ); + vt.printf( "%s", title ); + vt.PutBoxDrawingChar( 'x' ); + vt.printf( "\r\n" ); +} +void SerialDisplayDrawTopSeparator( void ) +{ + vt.PutBoxDrawingChar( 't' ); + for( int8_t i = 0; i <= 11; i++ ) + { + vt.PutBoxDrawingChar( 'q' ); + } + vt.PutBoxDrawingChar( 'w' ); + for( int8_t i = 0; i <= 64; i++ ) + { + vt.PutBoxDrawingChar( 'q' ); + } + vt.PutBoxDrawingChar( 'u' ); + vt.printf( "\r\n" ); +} + +void SerialDisplayDrawColSeparator( void ) +{ + vt.PutBoxDrawingChar( 'x' ); + for( int8_t i = 0; i <= 11; i++ ) + { + vt.PutBoxDrawingChar( ' ' ); + } + vt.PutBoxDrawingChar( 't' ); + for( int8_t i = 0; i <= 64; i++ ) + { + vt.PutBoxDrawingChar( 'q' ); + } + vt.PutBoxDrawingChar( 'u' ); + vt.printf( "\r\n" ); +} + +void SerialDisplayDrawSeparator( void ) +{ + vt.PutBoxDrawingChar( 't' ); + for( int8_t i = 0; i <= 11; i++ ) + { + vt.PutBoxDrawingChar( 'q' ); + } + vt.PutBoxDrawingChar( 'n' ); + for( int8_t i = 0; i <= 64; i++ ) + { + vt.PutBoxDrawingChar( 'q' ); + } + vt.PutBoxDrawingChar( 'u' ); + vt.printf( "\r\n" ); +} + +void SerialDisplayDrawLine( const char* firstCol, const char* secondCol ) +{ + vt.PutBoxDrawingChar( 'x' ); + vt.printf( "%s", firstCol ); + vt.PutBoxDrawingChar( 'x' ); + vt.printf( "%s", secondCol ); + vt.PutBoxDrawingChar( 'x' ); + vt.printf( "\r\n" ); +} + +void SerialDisplayDrawBottomLine( void ) +{ + vt.PutBoxDrawingChar( 'm' ); + for( int8_t i = 0; i <= 11; i++ ) + { + vt.PutBoxDrawingChar( 'q' ); + } + vt.PutBoxDrawingChar( 'v' ); + for( int8_t i = 0; i <= 64; i++ ) + { + vt.PutBoxDrawingChar( 'q' ); + } + vt.PutBoxDrawingChar( 'j' ); + vt.printf( "\r\n" ); +} + +void SerialDisplayInit( void ) +{ + vt.ClearScreen( 2 ); + vt.SetCursorMode( false ); + vt.SetCursorPos( 0, 0 ); + + // "+-----------------------------------------------------------------------------+" ); + SerialDisplayDrawFirstLine( ); + // "¦ LoRaWAN Demonstration Application ¦" ); + SerialDisplayDrawTitle( " LoRaWAN Demonstration Application " ); + // "+------------+----------------------------------------------------------------¦" ); + SerialDisplayDrawTopSeparator( ); + // "¦ Activation ¦ [ ]Over The Air ¦" ); + SerialDisplayDrawLine( " Activation ", " [ ]Over The Air " ); + // "¦ ¦ DevEui [__ __ __ __ __ __ __ __] ¦" ); + SerialDisplayDrawLine( " ", " DevEui [__ __ __ __ __ __ __ __] " ); + // "¦ ¦ AppEui [__ __ __ __ __ __ __ __] ¦" ); + SerialDisplayDrawLine( " ", " AppEui [__ __ __ __ __ __ __ __] " ); + // "¦ ¦ AppKey [__ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __] ¦" ); + SerialDisplayDrawLine( " ", " AppKey [__ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __] " ); + // "¦ +----------------------------------------------------------------¦" ); + SerialDisplayDrawColSeparator( ); + // "¦ ¦ [x]Personalisation ¦" ); + SerialDisplayDrawLine( " ", " [ ]Personalisation " ); + // "¦ ¦ NwkId [___] ¦" ); + SerialDisplayDrawLine( " ", " NwkId [___] " ); + // "¦ ¦ DevAddr [__ __ __ __] ¦" ); + SerialDisplayDrawLine( " ", " DevAddr [__ __ __ __] " ); + // "¦ ¦ NwkSKey [__ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __] ¦" ); + SerialDisplayDrawLine( " ", " NwkSKey [__ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __] " ); + // "¦ ¦ AppSKey [__ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __] ¦" ); + SerialDisplayDrawLine( " ", " AppSKey [__ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __] " ); + // "+------------+----------------------------------------------------------------¦" ); + SerialDisplayDrawSeparator( ); + // "¦ MAC params ¦ [ ]Confirmed / [ ]Unconfirmed ¦" ); + SerialDisplayDrawLine( " MAC params ", " [ ]Confirmed / [ ]Unconfirmed " ); + // "¦ ¦ ADR [ ] ¦" ); + SerialDisplayDrawLine( " ", " ADR [ ] " ); + // "¦ ¦ Duty cycle[ ] ¦" ); + SerialDisplayDrawLine( " ", " Duty cycle[ ] " ); + // "+------------+----------------------------------------------------------------¦" ); + SerialDisplayDrawSeparator( ); + // "¦ Network ¦ [ ]Public / [ ]Private ¦" ); + SerialDisplayDrawLine( " Network ", " [ ]Public / [ ]Private " ); + // "¦ ¦ [ ]Joining / [ ]Joined ¦" ); + SerialDisplayDrawLine( " ", " [ ]Joining / [ ]Joined " ); + // "+------------+----------------------------------------------------------------¦" ); + SerialDisplayDrawSeparator( ); + // "¦ LED status ¦ [ ]LED1(Tx) / [ ]LED2(Rx) / [ ]LED3(App) ¦" ); + SerialDisplayDrawLine( " LED status ", " [ ]LED1(Tx) / [ ]LED2(Rx) / [ ]LED3(App) " ); + // "+------------+----------------------------------------------------------------¦" ); + SerialDisplayDrawSeparator( ); + // "¦ Uplink ¦ Acked [ ] ¦" ); + SerialDisplayDrawLine( " Uplink ", " Acked [ ] " ); + // "¦ ¦ Datarate [ ] ¦" ); + SerialDisplayDrawLine( " ", " Datarate [ ] " ); + // "¦ ¦ Counter [ ] ¦" ); + SerialDisplayDrawLine( " ", " Counter [ ] " ); + // "¦ ¦ Port [ ] ¦" ); + SerialDisplayDrawLine( " ", " Port [ ] " ); + // "¦ ¦ Data [__ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ ¦" ); + SerialDisplayDrawLine( " ", " Data [__ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ " ); + // "¦ ¦ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ ¦" ); + SerialDisplayDrawLine( " ", " __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ " ); + // "¦ ¦ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ ¦" ); + SerialDisplayDrawLine( " ", " __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ " ); + // "¦ ¦ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ ¦" ); + SerialDisplayDrawLine( " ", " __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ " ); + // "+------------+----------------------------------------------------------------¦" ); + SerialDisplayDrawSeparator( ); + // "¦ Downlink ¦ RSSI [ ] dBm ¦" ); + SerialDisplayDrawLine( " Downlink ", " RSSI [ ] dBm " ); + // "¦ [ ]Data ¦ SNR [ ] dB ¦" ); + SerialDisplayDrawLine( " [ ]Data ", " SNR [ ] dB " ); + // "¦ ¦ Counter [ ] ¦" ); + // "¦ ¦ Counter [ ] ¦" ); + SerialDisplayDrawLine( " ", " Counter [ ] " ); + // "¦ ¦ Port [ ] ¦" ); + SerialDisplayDrawLine( " ", " Port [ ] " ); + // "¦ ¦ Data [__ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ ¦" ); + SerialDisplayDrawLine( " ", " Data [__ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ " ); + // "¦ ¦ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ ¦" ); + SerialDisplayDrawLine( " ", " __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ " ); + // "¦ ¦ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ ¦" ); + SerialDisplayDrawLine( " ", " __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ " ); + // "¦ ¦ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ ¦" ); + SerialDisplayDrawLine( " ", " __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ __ " ); + // "+------------+----------------------------------------------------------------+" ); + SerialDisplayDrawBottomLine( ); + vt.printf( "To refresh screen please hit 'r' key.\r\n" ); +} + +bool SerialDisplayReadable( void ) +{ + return vt.Readable( ); +} + +uint8_t SerialDisplayGetChar( void ) +{ + return vt.GetChar( ); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/app/SerialDisplay.h Tue Oct 20 13:23:35 2015 +0000 @@ -0,0 +1,39 @@ +/* + / _____) _ | | +( (____ _____ ____ _| |_ _____ ____| |__ + \____ \| ___ | (_ _) ___ |/ ___) _ \ + _____) ) ____| | | || |_| ____( (___| | | | +(______/|_____)_|_|_| \__)_____)\____)_| |_| + (C)2015 Semtech + +Description: VT100 serial display management + +License: Revised BSD License, see LICENSE.TXT file include in the project + +Maintainer: Miguel Luis and Gregory Cristian +*/ +#ifndef __SERIAL_DISPLAY_H__ +#define __SERIAL_DISPLAY_H__ + +void SerialDisplayInit( void ); +void SerialDisplayUpdateUplink( bool acked, uint8_t datarate, uint16_t counter, uint8_t port, uint8_t *buffer, uint8_t bufferSize ); +void SerialDisplayUpdateDownlink( bool rxData, int16_t rssi, int8_t snr, uint16_t counter, uint8_t port, uint8_t *buffer, uint8_t bufferSize ); +void SerialDisplayPrintCheckBox( bool activated ); +void SerialDisplayUpdateLedState( uint8_t id, uint8_t state ); +void SerialDisplayUpdateActivationMode( bool otaa ); +void SerialDisplayUpdateEui( uint8_t line, uint8_t *eui ); +void SerialDisplayUpdateKey( uint8_t line, uint8_t *key ); +void SerialDisplayUpdateNwkId( uint8_t id ); +void SerialDisplayUpdateDevAddr( uint32_t addr ); +void SerialDisplayUpdateFrameType( bool confirmed ); +void SerialDisplayUpdateAdr( bool adr ); +void SerialDisplayUpdateDutyCycle( bool dutyCycle ); +void SerialDisplayUpdatePublicNetwork( bool network ); +void SerialDisplayUpdateData( uint8_t *buffer ); +void SerialDisplayUpdateNetworkIsJoined( bool state ); +void SerialDisplayUpdateUplinkAcked( bool state ); +void SerialDisplayUpdateDonwlinkRxData( bool state ); +bool SerialDisplayReadable( void ); +uint8_t SerialDisplayGetChar( void ); + +#endif // __SERIAL_DISPLAY_H__
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/app/main.cpp Tue Oct 20 13:23:35 2015 +0000 @@ -0,0 +1,551 @@ +/* + / _____) _ | | +( (____ _____ ____ _| |_ _____ ____| |__ + \____ \| ___ | (_ _) ___ |/ ___) _ \ + _____) ) ____| | | || |_| ____( (___| | | | +(______/|_____)_|_|_| \__)_____)\____)_| |_| + (C)2015 Semtech + +Description: LoRaMac classA device implementation + +License: Revised BSD License, see LICENSE.TXT file include in the project + +Maintainer: Miguel Luis and Gregory Cristian +*/ +#include "mbed.h" +#include "board.h" +#include "radio.h" + +#include "LoRaMac.h" + +#include "SerialDisplay.h" + +/*! + * When set to 1 the application uses the Over-the-Air activation procedure + * When set to 0 the application uses the Personalization activation procedure + */ +#define OVER_THE_AIR_ACTIVATION 1 + +/*! + * Indicates if the end-device is to be connected to a private or public network + */ +#define LORAWAN_PUBLIC_NETWORK true + +#if( OVER_THE_AIR_ACTIVATION != 0 ) + +/*! + * Join requests trials duty cycle. + */ +#define OVER_THE_AIR_ACTIVATION_DUTYCYCLE 10000000 // 10 [s] value in us + +/*! + * Mote device IEEE EUI + * + * \remark must be written as a little endian value (reverse order of normal reading) + */ +#define LORAWAN_DEVICE_EUI { 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11 } + +/*! + * Application IEEE EUI + * + * \remark must be written as a little endian value (reverse order of normal reading) + */ +#define LORAWAN_APPLICATION_EUI { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 } + +/*! + * AES encryption/decryption cipher application key + */ +#define LORAWAN_APPLICATION_KEY { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x00 } + +#else + +/*! + * Current network ID + */ +#define LORAWAN_NETWORK_ID ( uint32_t )0 + +/*! + * Device address on the network + * + * \remark must be written as a big endian value (normal reading order) + */ +#define LORAWAN_DEVICE_ADDRESS ( uint32_t )0x12345678 + +/*! + * AES encryption/decryption cipher network session key + */ +#define LORAWAN_NWKSKEY { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C } + +/*! + * AES encryption/decryption cipher application session key + */ +#define LORAWAN_APPSKEY { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C } + +#endif + +/*! + * Defines the application data transmission duty cycle + */ +#define APP_TX_DUTYCYCLE 5000000 // 5 [s] value in us +#define APP_TX_DUTYCYCLE_RND 1000000 // 1 [s] value in us + +/*! + * LoRaWAN confirmed messages + */ +#define LORAWAN_CONFIRMED_MSG_ON true + +/*! + * LoRaWAN Adaptative Data Rate + * + * \remark Please note that when ADR is enabled the end-device should be static + */ +#define LORAWAN_ADR_ON 1 + +/*! + * LoRaWAN ETSI duty cycle control enable/disable + * + * \remark Please note that ETSI mandates duty cycled transmissions. Use only for test purposes + */ +#define LORAWAN_DUTYCYCLE_ON false + +/*! + * LoRaWAN application port + */ +#define LORAWAN_APP_PORT 15 + +/*! + * User application data buffer size + */ +#if ( LORAWAN_CONFIRMED_MSG_ON == 1 ) +#define LORAWAN_APP_DATA_SIZE 6 + +#else +#define LORAWAN_APP_DATA_SIZE 1 + +#endif + +#if( OVER_THE_AIR_ACTIVATION != 0 ) + +static uint8_t DevEui[] = LORAWAN_DEVICE_EUI; +static uint8_t AppEui[] = LORAWAN_APPLICATION_EUI; +static uint8_t AppKey[] = LORAWAN_APPLICATION_KEY; + +#else + +static uint8_t NwkSKey[] = LORAWAN_NWKSKEY; +static uint8_t AppSKey[] = LORAWAN_APPSKEY; + +#endif + +/*! + * Indicates if the MAC layer has already joined a network. + */ +static bool IsNetworkJoined = false; +static bool IsNetworkJoinedStatusUpdate = false; + +/*! + * User application data + */ +static uint8_t AppData[LORAWAN_APP_DATA_SIZE]; + +/*! + * Defines the application data transmission duty cycle + */ +static uint32_t TxDutyCycleTime; + +TimerEvent_t TxNextPacketTimer; + +#if( OVER_THE_AIR_ACTIVATION != 0 ) + +/*! + * Defines the join request timer + */ +TimerEvent_t JoinReqTimer; + +#endif + +/*! + * Indicates if a new packet can be sent + */ +static bool TxNextPacket = true; +static bool ScheduleNextTx = false; + +static bool AppLedStateOn = false; + +static LoRaMacEvent_t LoRaMacEvents; + +TimerEvent_t Led1Timer; +volatile bool Led1StateChanged = false; + +TimerEvent_t Led2Timer; +volatile bool Led2StateChanged = false; + +volatile bool Led3StateChanged = false; + +volatile bool LinkStatusUpdated = false; + +struct sLoRaMacUplinkStatus +{ + uint8_t Acked; + int8_t Datarate; + uint16_t UplinkCounter; + uint8_t Port; + uint8_t *Buffer; + uint8_t BufferSize; +}LoRaMacUplinkStatus; + +struct sLoRaMacDownlinkStatus +{ + int16_t Rssi; + int8_t Snr; + uint16_t DownlinkCounter; + bool RxData; + uint8_t Port; + uint8_t *Buffer; + uint8_t BufferSize; +}LoRaMacDownlinkStatus; + +void SerialDisplayRefresh( void ) +{ + SerialDisplayInit( ); + SerialDisplayUpdateActivationMode( OVER_THE_AIR_ACTIVATION ); + +#if( OVER_THE_AIR_ACTIVATION == 0 ) + SerialDisplayUpdateNwkId( LORAWAN_NETWORK_ID ); + SerialDisplayUpdateDevAddr( LORAWAN_DEVICE_ADDRESS ); + SerialDisplayUpdateKey( 12, NwkSKey ); + SerialDisplayUpdateKey( 13, AppSKey ); +#else + SerialDisplayUpdateEui( 5, DevEui ); + SerialDisplayUpdateEui( 6, AppEui ); + SerialDisplayUpdateKey( 7, AppKey ); +#endif + SerialDisplayUpdateNetworkIsJoined( IsNetworkJoined ); + + SerialDisplayUpdateAdr( LORAWAN_ADR_ON ); + SerialDisplayUpdateDutyCycle( LORAWAN_DUTYCYCLE_ON ); + SerialDisplayUpdatePublicNetwork( LORAWAN_PUBLIC_NETWORK ); + + SerialDisplayUpdateLedState( 3, AppLedStateOn ); +} + +void SerialRxProcess( void ) +{ + if( SerialDisplayReadable( ) == true ) + { + switch( SerialDisplayGetChar( ) ) + { + case 'R': + case 'r': + // Refresh Serial screen + SerialDisplayRefresh( ); + break; + default: + break; + } + } +} + +/*! + * Prepares the frame buffer to be sent + */ +static void PrepareTxFrame( uint8_t port ) +{ + AppData[0] = AppLedStateOn; +#if ( LORAWAN_CONFIRMED_MSG_ON == true ) + AppData[1] = LoRaMacDownlinkStatus.DownlinkCounter >> 8; + AppData[2] = LoRaMacDownlinkStatus.DownlinkCounter; + AppData[3] = LoRaMacDownlinkStatus.Rssi >> 8; + AppData[4] = LoRaMacDownlinkStatus.Rssi; + AppData[5] = LoRaMacDownlinkStatus.Snr; +#endif +} + +static void ProcessRxFrame( LoRaMacEventFlags_t *flags, LoRaMacEventInfo_t *info ) +{ + switch( info->RxPort ) // Check Rx port number + { + case 1: // The application LED can be controlled on port 1 or 2 + case 2: + if( info->RxBufferSize == 1 ) + { + AppLedStateOn = info->RxBuffer[0] & 0x01; + Led3StateChanged = true; + } + break; + default: + break; + } +} + +static bool SendFrame( void ) +{ + uint8_t sendFrameStatus = 0; + + LoRaMacUplinkStatus.Acked = false; + LoRaMacUplinkStatus.Port = LORAWAN_APP_PORT; + LoRaMacUplinkStatus.Buffer = AppData; + LoRaMacUplinkStatus.BufferSize = LORAWAN_APP_DATA_SIZE; + + SerialDisplayUpdateFrameType( LORAWAN_CONFIRMED_MSG_ON ); +#if( LORAWAN_CONFIRMED_MSG_ON == false ) + sendFrameStatus = LoRaMacSendFrame( LORAWAN_APP_PORT, AppData, LORAWAN_APP_DATA_SIZE ); +#else + sendFrameStatus = LoRaMacSendConfirmedFrame( LORAWAN_APP_PORT, AppData, LORAWAN_APP_DATA_SIZE, 8 ); +#endif + switch( sendFrameStatus ) + { + case 5: // NO_FREE_CHANNEL + // Try again later + return true; + default: + return false; + } +} + +#if( OVER_THE_AIR_ACTIVATION != 0 ) + +/*! + * \brief Function executed on JoinReq Timeout event + */ +static void OnJoinReqTimerEvent( void ) +{ + TimerStop( &JoinReqTimer ); + TxNextPacket = true; +} + +#endif + +/*! + * \brief Function executed on TxNextPacket Timeout event + */ +static void OnTxNextPacketTimerEvent( void ) +{ + TimerStop( &TxNextPacketTimer ); + TxNextPacket = true; +} + +/*! + * \brief Function executed on Led 1 Timeout event + */ +static void OnLed1TimerEvent( void ) +{ + TimerStop( &Led1Timer ); + Led1StateChanged = true; +} + +/*! + * \brief Function executed on Led 2 Timeout event + */ +static void OnLed2TimerEvent( void ) +{ + TimerStop( &Led2Timer ); + Led2StateChanged = true; +} + +/*! + * \brief Function to be executed on MAC layer event + */ +static void OnMacEvent( LoRaMacEventFlags_t *flags, LoRaMacEventInfo_t *info ) +{ + if( flags->Bits.JoinAccept == 1 ) + { +#if( OVER_THE_AIR_ACTIVATION != 0 ) + TimerStop( &JoinReqTimer ); +#endif + IsNetworkJoined = true; + IsNetworkJoinedStatusUpdate = true; + } + else + { + if( flags->Bits.Tx == 1 ) + { + } + + if( flags->Bits.Rx == 1 ) + { + if( flags->Bits.RxData == true ) + { + ProcessRxFrame( flags, info ); + } + + LoRaMacDownlinkStatus.Rssi = info->RxRssi; + if( info->RxSnr & 0x80 ) // The SNR sign bit is 1 + { + // Invert and divide by 4 + LoRaMacDownlinkStatus.Snr = ( ( ~info->RxSnr + 1 ) & 0xFF ) >> 2; + LoRaMacDownlinkStatus.Snr = -LoRaMacDownlinkStatus.Snr; + } + else + { + // Divide by 4 + LoRaMacDownlinkStatus.Snr = ( info->RxSnr & 0xFF ) >> 2; + } + LoRaMacDownlinkStatus.DownlinkCounter++; + LoRaMacDownlinkStatus.RxData = flags->Bits.RxData; + LoRaMacDownlinkStatus.Port = info->RxPort; + LoRaMacDownlinkStatus.Buffer = info->RxBuffer; + LoRaMacDownlinkStatus.BufferSize = info->RxBufferSize; + + LoRaMacUplinkStatus.Acked = info->TxAckReceived; + LoRaMacUplinkStatus.Datarate = info->TxDatarate; + LoRaMacUplinkStatus.UplinkCounter = LoRaMacGetUpLinkCounter( ) - 1; + + LinkStatusUpdated = true; + TimerStart( &Led2Timer ); + } + } + // Schedule a new transmission + ScheduleNextTx = true; +} + +/** + * Main application entry point. + */ +int main( void ) +{ + SerialDisplayInit( ); + +#if( OVER_THE_AIR_ACTIVATION != 0 ) + uint8_t sendFrameStatus = 0; +#endif + bool trySendingFrameAgain = false; + + BoardInit( ); + + LoRaMacEvents.MacEvent = OnMacEvent; + LoRaMacInit( &LoRaMacEvents, &BoardGetBatterieLevel ); + + IsNetworkJoined = false; + + SerialDisplayUpdateActivationMode( OVER_THE_AIR_ACTIVATION ); + +#if( OVER_THE_AIR_ACTIVATION == 0 ) + LoRaMacInitNwkIds( LORAWAN_NETWORK_ID, LORAWAN_DEVICE_ADDRESS, NwkSKey, AppSKey ); + IsNetworkJoined = true; + + SerialDisplayUpdateNwkId( LORAWAN_NETWORK_ID ); + SerialDisplayUpdateDevAddr( LORAWAN_DEVICE_ADDRESS ); + SerialDisplayUpdateKey( 12, NwkSKey ); + SerialDisplayUpdateKey( 13, AppSKey ); +#else + // Sends a JoinReq Command every 5 seconds until the network is joined + TimerInit( &JoinReqTimer, OnJoinReqTimerEvent ); + TimerSetValue( &JoinReqTimer, OVER_THE_AIR_ACTIVATION_DUTYCYCLE ); + + SerialDisplayUpdateEui( 5, DevEui ); + SerialDisplayUpdateEui( 6, AppEui ); + SerialDisplayUpdateKey( 7, AppKey ); + +#endif + + SerialDisplayUpdateNetworkIsJoined( IsNetworkJoined ); + + TxNextPacket = true; + TimerInit( &TxNextPacketTimer, OnTxNextPacketTimerEvent ); + + TimerInit( &Led1Timer, OnLed1TimerEvent ); + TimerSetValue( &Led1Timer, 500000 ); + + TimerInit( &Led2Timer, OnLed2TimerEvent ); + TimerSetValue( &Led2Timer, 500000 ); + + LoRaMacSetAdrOn( LORAWAN_ADR_ON ); + LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON ); + LoRaMacSetPublicNetwork( LORAWAN_PUBLIC_NETWORK ); + + SerialDisplayUpdateAdr( LORAWAN_ADR_ON ); + SerialDisplayUpdateDutyCycle( LORAWAN_DUTYCYCLE_ON ); + SerialDisplayUpdatePublicNetwork( LORAWAN_PUBLIC_NETWORK ); + + LoRaMacDownlinkStatus.DownlinkCounter = 0; + + while( 1 ) + { + while( IsNetworkJoined == false ) + { +#if( OVER_THE_AIR_ACTIVATION != 0 ) + if( TxNextPacket == true ) + { + TxNextPacket = false; + + sendFrameStatus = LoRaMacJoinReq( DevEui, AppEui, AppKey ); + switch( sendFrameStatus ) + { + case 1: // BUSY + break; + case 0: // OK + case 2: // NO_NETWORK_JOINED + case 3: // LENGTH_PORT_ERROR + case 4: // MAC_CMD_ERROR + case 6: // DEVICE_OFF + default: + // Relaunch timer for next trial + TimerStart( &JoinReqTimer ); + break; + } + } + SerialRxProcess( ); +#endif + } + + SerialRxProcess( ); + + if( IsNetworkJoinedStatusUpdate == true ) + { + IsNetworkJoinedStatusUpdate = false; + SerialDisplayUpdateNetworkIsJoined( IsNetworkJoined ); + } + if( Led1StateChanged == true ) + { + Led1StateChanged = false; + SerialDisplayUpdateLedState( 1, 0 ); + } + if( Led2StateChanged == true ) + { + Led2StateChanged = false; + SerialDisplayUpdateLedState( 2, 0 ); + } + if( Led3StateChanged == true ) + { + Led3StateChanged = false; + SerialDisplayUpdateLedState( 3, AppLedStateOn ); + } + if( LinkStatusUpdated == true ) + { + LinkStatusUpdated = false; + SerialDisplayUpdateLedState( 2, 1 ); + SerialDisplayUpdateUplink( LoRaMacUplinkStatus.Acked, LoRaMacUplinkStatus.Datarate, LoRaMacUplinkStatus.UplinkCounter, LoRaMacUplinkStatus.Port, LoRaMacUplinkStatus.Buffer, LoRaMacUplinkStatus.BufferSize ); + SerialDisplayUpdateDownlink( LoRaMacDownlinkStatus.RxData, LoRaMacDownlinkStatus.Rssi, LoRaMacDownlinkStatus.Snr, LoRaMacDownlinkStatus.DownlinkCounter, LoRaMacDownlinkStatus.Port, LoRaMacDownlinkStatus.Buffer, LoRaMacDownlinkStatus.BufferSize ); + } + + if( ScheduleNextTx == true ) + { + ScheduleNextTx = false; + // Schedule next packet transmission + TxDutyCycleTime = APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND ); + TimerSetValue( &TxNextPacketTimer, TxDutyCycleTime ); + TimerStart( &TxNextPacketTimer ); + } + + if( trySendingFrameAgain == true ) + { + trySendingFrameAgain = SendFrame( ); + } + if( TxNextPacket == true ) + { + TxNextPacket = false; + + SerialDisplayUpdateDonwlinkRxData( false ); + + PrepareTxFrame( LORAWAN_APP_PORT ); + + SerialDisplayUpdateLedState( 1, 1 ); + TimerStart( &Led1Timer ); + + trySendingFrameAgain = SendFrame( ); + + SerialDisplayUpdateUplink( LoRaMacUplinkStatus.Acked, LoRaMacUplinkStatus.Datarate, LoRaMacUplinkStatus.UplinkCounter, LoRaMacUplinkStatus.Port, LoRaMacUplinkStatus.Buffer, LoRaMacUplinkStatus.BufferSize ); + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/app/vt100.h Tue Oct 20 13:23:35 2015 +0000 @@ -0,0 +1,144 @@ +/* + / _____) _ | | +( (____ _____ ____ _| |_ _____ ____| |__ + \____ \| ___ | (_ _) ___ |/ ___) _ \ + _____) ) ____| | | || |_| ____( (___| | | | +(______/|_____)_|_|_| \__)_____)\____)_| |_| + (C)2015 Semtech + +Description: VT100 terminal support class + +License: Revised BSD License, see LICENSE.TXT file include in the project + +Maintainer: Miguel Luis and Gregory Cristian +*/ +#ifndef __VT100_H__ +#define __VT100_H__ + +class VT100 : public Serial +{ +public: + enum TextAttributes + { + ATTR_OFF = 0, + BOLD = 1, + USCORE = 4, + BLINK = 5, + REVERSE = 7, + BOLD_OFF = 21, + USCORE_OFF = 24, + BLINK_OFF = 25, + REVERSE_OFF = 27, + }; + + enum Colors + { + BLACK = 0, + RED = 1, + GREEN = 2, + BROWN = 3, + BLUE = 4, + MAGENTA = 5, + CYAN = 6, + WHITE = 7, + }; + /*! + * + */ + VT100( PinName tx, PinName rx ): Serial( tx, rx ) + { + this->baud( 115200 ); + // initializes terminal to "power-on" settings + // ESC c + this->printf( "\x1B\x63" ); + } + + void ClearScreen( uint8_t param ) + { + // ESC [ Ps J + // 0 Clear screen from cursor down + // 1 Clear screen from cursor up + // 2 Clear entire screen + + this->printf( "\x1B[%dJ", param ); + } + + void ClearLine( uint8_t param ) + { + // ESC [ Ps K + // 0 Erase from the active position to the end of the line, inclusive (default) + // 1 Erase from the start of the screen to the active position, inclusive + // 2 Erase all of the line, inclusive + + this->printf( "\x1B[%dK", param ); + } + + void SetAttribute( uint8_t attr ) + { + // ESC [ Ps;...;Ps m + this->printf( "\x1B[%dm", attr ); + } + + void SetAttribute( uint8_t attr, uint8_t fgcolor, uint8_t bgcolor ) + { + // ESC [ Ps;...;Ps m + this->printf( "\x1B[%d;%d;%dm", attr, fgcolor + 30, bgcolor + 40 ); + } + + void SetCursorMode( uint8_t visible ) + { + if( visible == true ) + { + // ESC [ ? 25 h + this->printf( "\x1B[?25h" ); + } + else + { + // ESC [ ? 25 l + this->printf( "\x1B[?25l" ); + } + } + + void SetCursorPos( uint8_t line, uint8_t col ) + { + // ESC [ Pl ; Pc H + this->printf( "\x1B[%d;%dH", line, col ); + } + + void PutStringAt( uint8_t line, uint8_t col, const char *s ) + { + this->SetCursorPos( line, col ); + this->printf( "%s", s ); + } + + void PutCharAt( uint8_t line, uint8_t col, uint8_t c ) + { + this->SetCursorPos( line, col ); + this->printf( "%c", c ); + } + + void PutHexAt( uint8_t line, uint8_t col, uint16_t n ) + { + this->SetCursorPos( line, col ); + this->printf( "%X", n ); + } + + void PutBoxDrawingChar( uint8_t c ) + { + this->printf( "\x1B(0%c\x1b(B", c ); + } + + bool Readable( void ) + { + return this->readable( ); + } + + uint8_t GetChar( void ) + { + return this->getc( ); + } +private: + +}; + +#endif // __VT100_H__
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/board/board.cpp Tue Oct 20 13:23:35 2015 +0000 @@ -0,0 +1,27 @@ +/* + / _____) _ | | +( (____ _____ ____ _| |_ _____ ____| |__ + \____ \| ___ | (_ _) ___ |/ ___) _ \ + _____) ) ____| | | || |_| ____( (___| | | | +(______/|_____)_|_|_| \__)_____)\____)_| |_| + (C)2015 Semtech + +Description: Target board general functions implementation + +License: Revised BSD License, see LICENSE.TXT file include in the project + +Maintainer: Miguel Luis and Gregory Cristian +*/ +#include "mbed.h" +#include "board.h" + +void BoardInit( void ) +{ + TimerTimeCounterInit( ); +} + + +uint8_t BoardGetBatterieLevel( void ) +{ + return 0xFE; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/board/board.h Tue Oct 20 13:23:35 2015 +0000 @@ -0,0 +1,36 @@ +/* + / _____) _ | | +( (____ _____ ____ _| |_ _____ ____| |__ + \____ \| ___ | (_ _) ___ |/ ___) _ \ + _____) ) ____| | | || |_| ____( (___| | | | +(______/|_____)_|_|_| \__)_____)\____)_| |_| + (C)2015 Semtech + +Description: Target board general functions implementation + +License: Revised BSD License, see LICENSE.TXT file include in the project + +Maintainer: Miguel Luis and Gregory Cristian +*/ +#ifndef __BOARD_H__ +#define __BOARD_H__ + +#include "mbed.h" +#include "timer.h" +#include "debug.h" +#include "utilities.h" + +/*! + * \brief Initializes the target board peripherals. + */ +void BoardInit( void ); + +/*! + * \brief Measure the Battery level + * + * \retval value battery level ( 0: very low, 254: fully charged ) + */ +uint8_t BoardGetBatterieLevel( void ); + + +#endif // __BOARD_H__
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mac/LoRaWAN-lib.lib Tue Oct 20 13:23:35 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/teams/Semtech/code/LoRaWAN-lib/#91d1a7783bb9
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Tue Oct 20 13:23:35 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/34e6b704fe68 \ No newline at end of file