Blame view

recepteur/recepteur.cpp 5.13 KB
8c2393cd   rcavalie   travail reception...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
  ////////////////////////////////////////////////////////////////////////////////
  /*RECEPTEUR LoRa
  par : Robin Cavalieri
        IMA5SC
  PFE 2018 */
  ////////////////////////////////////////////////////////////////////////////////
  //Headers
  #include "mbed.h"
  #include "main.h"
  #include "sx1276-hal.h"
  #include "debug.h"
  ////////////////////////////////////////////////////////////////////////////////
  #define DEBUG_MESSAGE   1
  ////////////////////////////////////////////////////////////////////////////////
  
  ////////////////////////////////////////////////////////////////////////////////
  //Modulation LoRa active
  #define USE_MODEM_LORA  1
  #define USE_MODEM_FSK   !USE_MODEM_LORA
  //Paramètres
  #define RF_FREQUENCY 868000000 //Hz : Fréquence de travail pour UE
  #define TX_OUTPUT_POWER 14     //dBm : puissance de sortie du signal
  #define LORA_BANDWIDTH 1         // 0:125 kHz de BP
  #define LORA_SPREADING_FACTOR 10 // [SF7..SF12] Portée du débit. grande portée inutile
  #define LORA_CODINGRATE 1
  #define LORA_PREAMBLE_LENGTH 8   // Same for Tx and Rx
  #define LORA_SYMBOL_TIMEOUT 5    // Symbols
  #define LORA_FIX_LENGTH_PAYLOAD_ON false
  #define LORA_FHSS_ENABLED true
  #define LORA_NB_SYMB_HOP 4
  #define LORA_IQ_INVERSION_ON false
  #define LORA_CRC_ENABLED true
  #define RX_TIMEOUT_VALUE 0      // in ms
  #define BUFFER_SIZE 32        // Define the payload size here
  #define TRAME_SIZE 13
  ////////////////////////////////////////////////////////////////////////////////
  
  ////////////////////////////////////////////////////////////////////////////////
  //Variables globales
  typedef enum
  {
      LOWPOWER = 0,
      IDLE,
      RX,
      RX_TIMEOUT,
      RX_ERROR,
      CAD,
      CAD_DONE
  }AppStates_t;
  
  volatile AppStates_t State = LOWPOWER;
  static RadioEvents_t RadioEvents;
  SX1276MB1xAS Radio( NULL );
  uint16_t BufferSize = BUFFER_SIZE;
  uint8_t Buffer[BUFFER_SIZE];
  int16_t RssiValue = 0.0;
  int8_t SnrValue = 0.0;
  uint8_t receptMsg[13];
  ////////////////////////////////////////////////////////////////////////////////
  
  ////////////////////////////////////////////////////////////////////////////////
  //Main réception
  int main( void )
  {
      debug("\nRecepteur SX1276 \n\n\r");
  
      // Initialize Radio driver
      RadioEvents.RxDone = OnRxDone;
      RadioEvents.RxError = OnRxError;
      RadioEvents.RxTimeout = OnRxTimeout;
      RadioEvents.FhssChangeChannel = OnFhssChangeChannel;
      Radio.Init(&RadioEvents);
  
      //Véification de la communication avec la board
      while(Radio.Read(REG_VERSION) == 0x00)
      {
          debug("Aucune radio detectee\n\r", NULL);
          wait(1);
      }
  
      //Checking du matériel connecté
      debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1276MB1LAS ) ), "\n\r Materiel detecte : SX1276MB1LAS \n\r" );
      debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1276MB1MAS ) ), "\n\r Materiel detecte : SX1276MB1MAS \n\r" );
  
      //Paramétrage du canal
      Radio.SetChannel( HoppingFrequencies[0] );
  
      #if USE_MODEM_LORA == 1
  
          debug_if( LORA_FHSS_ENABLED, "\n\n\r             > LORA FHSS Mode < \n\n\r" );
          debug_if( !LORA_FHSS_ENABLED, "\n\n\r             > LORA Mode < \n\n\r" );
  
          Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
                               LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
                               LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0,
                               LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
                               LORA_IQ_INVERSION_ON, true );
  
      #else
          #error "Aucun modem defini"
      #endif
  
          debug_if(DEBUG_MESSAGE, "Reception en cours...\r\n");
          Radio.Rx(RX_TIMEOUT_VALUE); //Reception mode en continu
  
          while(1)
          {
              if( BufferSize > 0 )
              {
                  if( strncmp( ( const char* )Buffer, ( const char* )receptMsg, TRAME_SIZE ) == 0 )
                  {
                      debug( "Trame recue : %s\r\n",(char*)Buffer);
                  }
                  else
                  {
                      debug("Attente d'une trame...\n");
                  }
                  wait_ms(1000);
              }
          }
  }
  ////////////////////////////////////////////////////////////////////////////////
  
  ////////////////////////////////////////////////////////////////////////////////
  //Fonctions utiles
  void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr )
  {
      Radio.SetChannel( HoppingFrequencies[0] );
      Radio.Sleep( );
      BufferSize = size;
      memcpy( Buffer, payload, BufferSize );
      RssiValue = rssi;
      SnrValue = snr;
      State = RX;
      debug_if( DEBUG_MESSAGE, "> OnRxDone\n\r" );
  }
  
  void OnRxTimeout( void )
  {
      Radio.SetChannel( HoppingFrequencies[0] );
      Radio.Sleep( );
      Buffer[BufferSize] = 0;
      State = RX_TIMEOUT;
      debug_if( DEBUG_MESSAGE, "> OnRxTimeout\n\r" );
  }
  
  void OnRxError( void )
  {
      Radio.SetChannel( HoppingFrequencies[0] );
      Radio.Sleep( );
      State = RX_ERROR;
      debug_if( DEBUG_MESSAGE, "> OnRxError\n\r" );
  }
  
  void OnFhssChangeChannel( uint8_t channelIndex )
  {
      Radio.SetChannel( HoppingFrequencies[channelIndex] );
      debug_if( DEBUG_MESSAGE, "F%d-", channelIndex );
  }
  ////////////////////////////////////////////////////////////////////////////////