Version FC

Dependencies:   DmTftLibrary eeprom SX1280Lib filesystem mbed

Fork of MSNV2-Terminal_V1-5 by Francis CHATAIN

Revision:
3:3adb454ba1d2
Parent:
2:57f098de07c7
Child:
4:41a92fbf702a
--- a/main.cpp	Wed Aug 02 21:59:03 2017 +0000
+++ b/main.cpp	Thu Aug 03 08:52:04 2017 +0000
@@ -1,12 +1,13 @@
 #include "mbed.h"
 #include "radio.h"
 #include "sx1280-hal.h"
+#include "stdio.h"
 
 
 #define FIRMWARE_VERSION ( ( char* )"Firmware Version: 01" )    // display firmware version on RS232
 #define MODE_LORA                                               // Lora modulation 
 #define RF_FREQUENCY     2400000000UL                           // HzNominal frequency
-#define TX_OUTPUT_POWER  13                                     // Output power in dBm [-18..+13] dBm
+#define TX_OUTPUT_POWER  -18                                     // Output power in dBm [-18..+13] dBm
 
 typedef enum         // States of the application
 {
@@ -18,29 +19,26 @@
     APP_TX_TIMEOUT,
 }AppStates_t;
 
-#define BUFFER_SIZE     16          // Payload size
-//#define BUFFER_SIZE                                 34
+#define ID_TERMINAL     "1"
+#define ID_GATEWAY      "1"
+
+float temperature = 0.0 ; 
+float humidite    = 0.0 ; 
+
 
-/*!
- * Message type for this application
- */
-//const uint8_t PingMsg[] = "DATA IOT TERMINAL";
-//const uint8_t PongMsg[] = "RESPONSE GATEWAY ";
-const uint8_t PingMsg[] = "PING DATA IOT TERMINAL";
-const uint8_t PongMsg[] = "PONG";
+#define MESSAGESIZE     34                  // Defines the size of the token defining message type in the payload
+char Message[MESSAGESIZE] = "MSG DATA IOT TERMINAL";
 
-/*!
- * \brief Defines the size of the token defining message type in the payload
- */
-#define PINGPONGSIZE                    4
+int cptr = 0 ; 
 
-uint8_t BufferSize = BUFFER_SIZE;           // Size of the buffer
+#define BUFFER_SIZE     34                  // Payload size
+uint8_t BufferSize      =   BUFFER_SIZE;    // Size of the buffer
 uint8_t Buffer[BUFFER_SIZE];                // Buffer 
 
 AppStates_t AppState = APP_LOWPOWER;        // State of the application
 
-int8_t RssiValue = 0;
-int8_t SnrValue = 0;
+int8_t RssiValue    = 0;
+int8_t SnrValue     = 0;
 
 void OnTxDone       ( void );               // Function to be executed on Radio Tx Done event
 void OnRxDone       ( void );               // Function to be executed on Radio Rx Done event
@@ -64,18 +62,14 @@
 //                    mosi, miso, sclk, nss, busy, dio1, dio2, dio3, rst, callbacks...
 SX1280Hal Radio     ( D11 , D12 , D13 , D7 , D3  , D5  , NC  , NC  , A0 , &callbacks );
 
-DigitalOut ANT_SW   ( A3 );
-DigitalOut TxLed    ( A4 );
-DigitalOut RxLed    ( A5 );
-
-/*!
- * Define IO for Unused Pin
- */
-DigitalOut F_CS     ( D6 );     // MBED description of pin
-DigitalOut SD_CS    ( D8 );     // MBED description of pin
+DigitalOut ANT_SW   ( A3 )  ;
+DigitalOut TxLed    ( A4 )  ;
+DigitalOut RxLed    ( A5 )  ;
+DigitalOut F_CS     ( D6 )  ;     // MBED description of pin
+DigitalOut SD_CS    ( D8 )  ;     // MBED description of pin
 
 #define TX_TIMEOUT_VALUE        100         // ms   Number of tick size steps for tx timeout
-#define RX_TIMEOUT_VALUE        100         // ms Number of tick size steps for rx timeout
+#define RX_TIMEOUT_VALUE        100         // ms   Number of tick size steps for rx timeout
 #define RX_TIMEOUT_TICK_SIZE    RADIO_TICK_SIZE_1000_US   // Size of ticks (used for Tx and Rx timeout)
 
 uint16_t RxIrqMask = IRQ_RX_DONE | IRQ_RX_TX_TIMEOUT;       // Mask of IRQs to listen to in rx mode
@@ -84,7 +78,6 @@
 PacketParams_t PacketParams;        // Locals parameters and status for radio API
 PacketStatus_t PacketStatus;        // NEED TO BE OPTIMIZED, COPY OF STUCTURE ALREADY EXISTING
 
-bool isMaster       = true;
 ModulationParams_t modulationParams;
 
 
@@ -92,8 +85,7 @@
 void baud   ( int baudrate )    { Serial s( USBTX, USBRX ); s.baud( baudrate );}
 
 void setup () {
-    baud    (115200);
-    
+    baud    (115200); 
     printf( "\n\n\r     SX1280 Terminal IoT LORA MODULATION 2.4GHz (%s)\n\n\r", FIRMWARE_VERSION );
 
     F_CS   = 1  ;
@@ -105,7 +97,6 @@
     wait_ms( 500 ); // wait for on board DC/DC start-up time
 
     Radio.Init              ( );
-    
     Radio.SetRegulatorMode  ( USE_DCDC ); // Can also be set in LDO mode but consume more power
 
     memset  ( &Buffer, 0x00, BufferSize );
@@ -122,20 +113,17 @@
     PacketParams.PacketType                         =   PACKET_TYPE_LORA        ;
     PacketParams.Params.LoRa.PreambleLength         =   0x08                    ;
     PacketParams.Params.LoRa.HeaderType             =   LORA_PACKET_VARIABLE_LENGTH;
-    PacketParams.Params.LoRa.PayloadLength          =   15                      ;
-    PacketParams.Params.LoRa.CrcMode        = LORA_CRC_ON;
-    PacketParams.Params.LoRa.InvertIQ       = LORA_IQ_INVERTED;
-
+    PacketParams.Params.LoRa.PayloadLength          =   34                      ;
+    PacketParams.Params.LoRa.CrcMode                =   LORA_CRC_ON             ;
+    PacketParams.Params.LoRa.InvertIQ               =   LORA_IQ_INVERTED        ;
 
     Radio.SetStandby                ( STDBY_RC );
     Radio.SetPacketType             ( modulationParams.PacketType );
     Radio.SetModulationParams       ( &modulationParams );
     Radio.SetPacketParams           ( &PacketParams );
-
     Radio.SetRfFrequency            ( RF_FREQUENCY );
     Radio.SetBufferBaseAddresses    ( 0x00, 0x00 );
     Radio.SetTxParams               ( TX_OUTPUT_POWER, RADIO_RAMP_20_US );
-
     Radio.SetDioIrqParams           ( RxIrqMask, RxIrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE );
     Radio.SetRx                     ( ( TickTime_t ) { RX_TIMEOUT_TICK_SIZE, RX_TIMEOUT_VALUE } );
 
@@ -144,133 +132,22 @@
     }
 
 void loop () {   
-        switch( AppState )
-        {
-            case APP_RX:
-            printf( "*** TERM ***  APP_RX      \r\n" ); 
-                AppState            =   APP_LOWPOWER;
-                RxLed               =   !RxLed;
-                Radio.GetPayload    ( Buffer, &BufferSize, BUFFER_SIZE );
-                
-                printf  ( "Message Received : %s \r\n", Buffer );
-               
-                if( isMaster == true )
-                {
-                    if( BufferSize > 0 )
-                    {
-                        if( strncmp( ( const char* )Buffer, ( const char* )PongMsg, PINGPONGSIZE ) == 0 )
-                        {
-                            printf  ( "<Master> Recv ...Pong\r\n" );
-                            wait_ms (2000) ; 
-                            memcpy( Buffer, PingMsg, PINGPONGSIZE );
-                            Radio.SetDioIrqParams       ( TxIrqMask, TxIrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE );
-                            Radio.SendPayload           ( Buffer, BufferSize,( TickTime_t ){ RX_TIMEOUT_TICK_SIZE, TX_TIMEOUT_VALUE } );
-                        }
-                                                
-                        else // valid reception but neither a PING or a PONG message
-                        {    // Set device as master ans start again
-                            printf( "<Master> Unknown message \r\n" );
-         
-                            isMaster = true;
-                            Radio.SetDioIrqParams   ( RxIrqMask, RxIrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE );
-                            Radio.SetRx             ( ( TickTime_t ) { RX_TIMEOUT_TICK_SIZE, RX_TIMEOUT_VALUE } );
-                        }
-                    }
-                }
-                
-                // Receiver 
-                else
-                {
-                    if( BufferSize > 0 )
-                    {
-                        if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, PINGPONGSIZE ) == 0 )
-                        {
-                            printf                  ( "...Ping\r\n" );
-                            memcpy                  ( Buffer, PongMsg, 4 );
-                            Radio.SetDioIrqParams   ( TxIrqMask, TxIrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE );
-                            Radio.SendPayload       ( Buffer, BufferSize, ( TickTime_t ){ RX_TIMEOUT_TICK_SIZE, TX_TIMEOUT_VALUE } );
-                        }
-                        else // valid reception but not a PING as expected
-                        {
-                            printf( "<Slave> Unknown message \r\n" );
-                            isMaster = true;
-                            Radio.SetDioIrqParams   ( RxIrqMask, RxIrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE );
-                            Radio.SetRx             ( ( TickTime_t ) { RX_TIMEOUT_TICK_SIZE, RX_TIMEOUT_VALUE } );
-                        }
-                    }
-                }
-                break;
-
-            case APP_TX:
-            printf( "*** TERM ***  APP_TX      \r\n" ); 
-                AppState = APP_LOWPOWER;
-                TxLed = !TxLed;
-                if( isMaster == true )          printf( "<Master> Send Ping...\r\n" );
-                else                            printf( "<Slave>  Send Pong...\r\n" );
-
-                Radio.SetDioIrqParams       ( RxIrqMask, RxIrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE );
-                Radio.SetRx                 ( ( TickTime_t ) { RX_TIMEOUT_TICK_SIZE, RX_TIMEOUT_VALUE } );
-                break;
-
-            case APP_RX_TIMEOUT:
-            printf( "*** TERM ***  APP_RX_TIMEOUT      \r\n" ); 
-                AppState = APP_LOWPOWER;
-                if( isMaster == true )
-                {
-                    // Send the next PING frame
-                    memcpy                  ( Buffer, PingMsg, PINGPONGSIZE );
-                    Radio.SetDioIrqParams   ( TxIrqMask, TxIrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE );
-                    Radio.SendPayload       ( Buffer, BufferSize, ( TickTime_t ){ RX_TIMEOUT_TICK_SIZE, TX_TIMEOUT_VALUE } );
-                }
-                else
-                {
-                    Radio.SetDioIrqParams   ( RxIrqMask, RxIrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE );
-                    Radio.SetRx             ( ( TickTime_t ) { RX_TIMEOUT_TICK_SIZE, RX_TIMEOUT_VALUE } );
-                }
-                break;
-
-            case APP_RX_ERROR:
-            printf( "*** TERM ***  APP_RX_ERROR      \r\n" ); 
-                AppState = APP_LOWPOWER;
-                // We have received a Packet with a CRC error, send reply as if packet was correct
-                if( isMaster == true )
-                {
-                    // Send the next PING frame
-                    memcpy                  ( Buffer, PingMsg, PINGPONGSIZE );
-                    Radio.SetDioIrqParams   ( TxIrqMask, TxIrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE );
-                    Radio.SendPayload       ( Buffer, BufferSize, ( TickTime_t ){ RX_TIMEOUT_TICK_SIZE, TX_TIMEOUT_VALUE } );
-                }
-                else
-                {
-                    // Send the next PONG frame
-                    memcpy                  ( Buffer, PongMsg, PINGPONGSIZE );
-                    Radio.SetDioIrqParams   ( TxIrqMask, TxIrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE );
-                    Radio.SendPayload       ( Buffer, BufferSize, ( TickTime_t ){ RX_TIMEOUT_TICK_SIZE, TX_TIMEOUT_VALUE } );
-                }
-                break;
-
-            case APP_TX_TIMEOUT:
-           printf( "*** TERM ***  APP_TX_TIMEOUT      \r\n" ); 
-                AppState        = APP_LOWPOWER;
-                Radio.SetDioIrqParams       ( RxIrqMask, RxIrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE );
-                Radio.SetRx                 ( ( TickTime_t ) { RX_TIMEOUT_TICK_SIZE, RX_TIMEOUT_VALUE } );
-                break;
-
-            case APP_LOWPOWER:
-                break;
-
-            default:
-                // Set low power
-                break;
-        }
+    TxLed = 1 ; 
+    int n = sprintf                      ( Message, "MSG DATA IOT TERMINAL: %d", cptr++) ; 
+    //sprintf                     ( Message, "MSG DATA IOT TERMINAL: %d", cptr++) ; 
+    memcpy                      ( Buffer    , Message   , MESSAGESIZE );
+    printf                      ( "*** TERM ***  Message = %s \r\n", Buffer );
+    Radio.SetDioIrqParams       ( TxIrqMask , TxIrqMask , IRQ_RADIO_NONE, IRQ_RADIO_NONE );
+    Radio.SendPayload           ( Buffer    , BufferSize,( TickTime_t ){ RX_TIMEOUT_TICK_SIZE, TX_TIMEOUT_VALUE } );
+    TxLed = 0 ; 
+    wait_ms (2000) ;  
 }
-   
-  
+ 
     
 
 int main( )  { setup () ; while (1) loop () ; }
 
-void OnTxDone       ( void )                            { printf( "*** TERM ***  OnTxDone      \r\n" ); AppState = APP_TX         ;   }
+void OnTxDone       ( void )                            { /*printf( "*** TERM ***  OnTxDone      \r\n" ); */ AppState = APP_TX         ;   }
 void OnRxDone       ( void )                            { printf( "*** TERM ***  OnRxDone      \r\n" ); AppState = APP_RX         ;   }
 void OnTxTimeout    ( void )                            { printf( "*** TERM ***  OnTxTimeout   \r\n" ); AppState = APP_TX_TIMEOUT ;   }
 void OnRxTimeout    ( void )                            { printf( "*** TERM ***  OnRxTimeout   \r\n" ); AppState = APP_RX_TIMEOUT ;   }