Commit a12ed64b1506947236856d2f21add36e92f6206f

Authored by rcavalie
1 parent 7e1ad503

recepteur ok

Showing 1 changed file with 97 additions and 302 deletions   Show diff stats
emetteur/emetteur.cpp
1   -#include "../mbed-os/mbed.h"
  1 +////////////////////////////////////////////////////////////////////////////////
  2 +//Headers
  3 +#include "mbed.h"
2 4 #include "main.h"
3 5 #include "sx1276-hal.h"
4 6 #include "debug.h"
5   -
6   -/* Set this flag to '1' to display debug messages on the console */
7   -#define DEBUG_MESSAGE 0
8   -
9   -/* Set this flag to '1' to use the LoRa modulation or to '0' to use FSK modulation */
  7 +////////////////////////////////////////////////////////////////////////////////
  8 +#define DEBUG_MESSAGE 1
  9 +////////////////////////////////////////////////////////////////////////////////
  10 +//Modulation LoRa active
10 11 #define USE_MODEM_LORA 1
11 12 #define USE_MODEM_FSK !USE_MODEM_LORA
12   -
13   -#define RF_FREQUENCY 868000000 // Hz
14   -#define TX_OUTPUT_POWER 14 // 14 dBm
15   -
16   -#if USE_MODEM_LORA == 1
17   -
18   - #define LORA_BANDWIDTH 2 // [0: 125 kHz,
19   - // 1: 250 kHz,
20   - // 2: 500 kHz,
21   - // 3: Reserved]
22   - #define LORA_SPREADING_FACTOR 7 // [SF7..SF12]
23   - #define LORA_CODINGRATE 1 // [1: 4/5,
24   - // 2: 4/6,
25   - // 3: 4/7,
26   - // 4: 4/8]
27   - #define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx
28   - #define LORA_SYMBOL_TIMEOUT 5 // Symbols
29   - #define LORA_FIX_LENGTH_PAYLOAD_ON false
30   - #define LORA_FHSS_ENABLED false
31   - #define LORA_NB_SYMB_HOP 4
32   - #define LORA_IQ_INVERSION_ON false
33   - #define LORA_CRC_ENABLED true
34   -
35   -#elif USE_MODEM_FSK == 1
36   -
37   - #define FSK_FDEV 25000 // Hz
38   - #define FSK_DATARATE 19200 // bps
39   - #define FSK_BANDWIDTH 50000 // Hz
40   - #define FSK_AFC_BANDWIDTH 83333 // Hz
41   - #define FSK_PREAMBLE_LENGTH 5 // Same for Tx and Rx
42   - #define FSK_FIX_LENGTH_PAYLOAD_ON false
43   - #define FSK_CRC_ENABLED true
44   -
45   -#else
46   - #error "Please define a modem in the compiler options."
47   -#endif
48   -
49   -#define RX_TIMEOUT_VALUE 3500000 // in us
50   -#define BUFFER_SIZE 32 // Define the payload size here
51   -
52   -#if( defined ( TARGET_KL25Z ) || defined ( TARGET_LPC11U6X ) )
53   -DigitalOut led(LED2);
54   -#else
55   -DigitalOut led(LED1);
56   -#endif
57   -
58   -/*
59   - * Global variables declarations
60   - */
  13 +//Paramรจtres
  14 +#define RF_FREQUENCY 868000000 //Hz : Frรฉquence de travail pour UE
  15 +#define TX_OUTPUT_POWER 14 //dBm : puissance de sortie du signal
  16 +#define LORA_BANDWIDTH 1 // 0:125 kHz de BP
  17 +#define LORA_SPREADING_FACTOR 10 // [SF7..SF12] Portรฉe du dรฉbit. grande portรฉe inutile
  18 +#define LORA_CODINGRATE 1
  19 +#define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx
  20 +#define LORA_SYMBOL_TIMEOUT 5 // Symbols
  21 +#define LORA_FIX_LENGTH_PAYLOAD_ON false
  22 +#define LORA_FHSS_ENABLED true
  23 +#define LORA_NB_SYMB_HOP 4
  24 +#define LORA_IQ_INVERSION_ON false
  25 +#define LORA_CRC_ENABLED true
  26 +#define RX_TIMEOUT_VALUE 3500 // in ms
  27 +#define BUFFER_SIZE 32 // Define the payload size here
  28 +////////////////////////////////////////////////////////////////////////////////
  29 +
  30 +////////////////////////////////////////////////////////////////////////////////
  31 +//Variables globales
61 32 typedef enum
62 33 {
63 34 LOWPOWER = 0,
64 35 IDLE,
65   -
66 36 RX,
67 37 RX_TIMEOUT,
68 38 RX_ERROR,
69   -
70 39 TX,
71 40 TX_TIMEOUT,
72   -
73 41 CAD,
74 42 CAD_DONE
75 43 }AppStates_t;
76 44  
77 45 volatile AppStates_t State = LOWPOWER;
78   -
79   -/*!
80   - * Radio events function pointer
81   - */
82 46 static RadioEvents_t RadioEvents;
83   -
84   -/*
85   - * Global variables declarations
86   - */
87 47 SX1276MB1xAS Radio( NULL );
88   -
89   -const uint8_t PingMsg[] = "PING";
90   -const uint8_t PongMsg[] = "PONG";
91   -
92 48 uint16_t BufferSize = BUFFER_SIZE;
93 49 uint8_t Buffer[BUFFER_SIZE];
94   -
95 50 int16_t RssiValue = 0.0;
96 51 int8_t SnrValue = 0.0;
  52 +const uint8_t TestMsg[] = "TEST";
  53 +////////////////////////////////////////////////////////////////////////////////
  54 +
  55 +////////////////////////////////////////////////////////////////////////////////
  56 +//Fonctions utiles
97 57  
98   -int main()
  58 +//Debug pour savoir si le message a รฉtรฉ transmis
  59 +void OnTxDone( void )
99 60 {
100   - uint8_t i;
101   - bool isMaster = true;
  61 + Radio.SetChannel( HoppingFrequencies[0] );
  62 + Radio.Sleep( );
  63 + State = TX;
  64 + debug_if( DEBUG_MESSAGE, "Envoye\n\r" );
  65 +}
102 66  
103   - debug( "\n\n\r SX1276 Ping Pong Demo Application \n\n\r" );
  67 +//Time out de transmission. ร‰chec de l'envoi
  68 +void OnTxTimeout( void )
  69 +{
  70 + Radio.SetChannel(HoppingFrequencies[0]);
  71 + Radio.Sleep( );
  72 + State = TX_TIMEOUT;
  73 + debug_if( DEBUG_MESSAGE, "Echec de l'envoi\n\r" );
  74 +}
  75 +
  76 +//Modification du canal d'emission
  77 +void OnFhssChangeChannel( uint8_t channelIndex )
  78 +{
  79 + Radio.SetChannel(HoppingFrequencies[channelIndex]);
  80 + debug_if(DEBUG_MESSAGE, "F%d-", channelIndex);
  81 +}
  82 +////////////////////////////////////////////////////////////////////////////////
  83 +
  84 +////////////////////////////////////////////////////////////////////////////////
  85 +//Main emission
  86 +int main( void )
  87 +{
  88 + uint8_t i;
  89 + debug("\n\n\rEmetteur SX1276\n\n\r");
104 90  
105 91 // Initialize Radio driver
106 92 RadioEvents.TxDone = OnTxDone;
107   - RadioEvents.RxDone = OnRxDone;
108   - RadioEvents.RxError = OnRxError;
109 93 RadioEvents.TxTimeout = OnTxTimeout;
110   - RadioEvents.RxTimeout = OnRxTimeout;
111   - Radio.Init( &RadioEvents );
  94 + RadioEvents.FhssChangeChannel = OnFhssChangeChannel;
  95 + Radio.Init(&RadioEvents);
112 96  
113   - // verify the connection with the board
114   - while( Radio.Read( REG_VERSION ) == 0x00 )
  97 + //Vรฉification de la connexion avec la carte
  98 + while(Radio.Read(REG_VERSION) == 0x00)
115 99 {
116   - debug( "Radio could not be detected!\n\r", NULL );
117   - wait( 1 );
  100 + debug("Aucune radio detectee\n\r", NULL);
  101 + wait(1);
118 102 }
119 103  
120   - debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1276MB1LAS ) ) , "\n\r > Board Type: SX1276MB1LAS < \n\r" );
121   - debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1276MB1MAS ) ) , "\n\r > Board Type: SX1276MB1MAS < \n\r" );
122   -
123   - Radio.SetChannel( RF_FREQUENCY );
124   -
125   -#if USE_MODEM_LORA == 1
126   -
127   - debug_if( LORA_FHSS_ENABLED, "\n\n\r > LORA FHSS Mode < \n\n\r");
128   - debug_if( !LORA_FHSS_ENABLED, "\n\n\r > LORA Mode < \n\n\r");
129   -
130   - Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
131   - LORA_SPREADING_FACTOR, LORA_CODINGRATE,
132   - LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
133   - LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
134   - LORA_IQ_INVERSION_ON, 2000000 );
135   -
136   - Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
137   - LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
138   - LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0,
139   - LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
140   - LORA_IQ_INVERSION_ON, true );
141   -
142   -#elif USE_MODEM_FSK == 1
143   -
144   - debug("\n\n\r > FSK Mode < \n\n\r");
145   - Radio.SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0,
146   - FSK_DATARATE, 0,
147   - FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON,
148   - FSK_CRC_ENABLED, 0, 0, 0, 2000000 );
149   -
150   - Radio.SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE,
151   - 0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH,
152   - 0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, FSK_CRC_ENABLED,
153   - 0, 0, false, true );
  104 + //Checking du matรฉriel connectรฉ
  105 + debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1276MB1LAS ) ), "\n\rMateriel detecte : SX1276MB1LAS < \n\r" );
  106 + debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1276MB1MAS ) ), "\n\rMateriel detecte : SX1276MB1MAS < \n\r" );
154 107  
155   -#else
  108 + //Paramรฉtrage du canal
  109 + Radio.SetChannel( HoppingFrequencies[0] );
156 110  
157   -#error "Please define a modem in the compiler options."
  111 + #if USE_MODEM_LORA == 1
158 112  
159   -#endif
  113 + debug_if( LORA_FHSS_ENABLED, "\n\n\rLORA FHSS Mode\n\n\r" );
  114 + debug_if( !LORA_FHSS_ENABLED, "\n\n\rLORA Mode\n\n\r" );
160 115  
161   - debug_if( DEBUG_MESSAGE, "Starting Ping-Pong loop\r\n" );
  116 + //Configuration du modem transmission
  117 + Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
  118 + LORA_SPREADING_FACTOR, LORA_CODINGRATE,
  119 + LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
  120 + LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
  121 + LORA_IQ_INVERSION_ON, 4000 );
  122 + #else
  123 + #error "Aucun modem defini"
  124 + #endif
162 125  
163   - led = 0;
  126 + debug_if( DEBUG_MESSAGE, "Procedure d'emission lancee\r\n" );
164 127  
165   - Radio.Rx( RX_TIMEOUT_VALUE );
166   -
167   - while( 1 )
  128 + //Boucle d'รฉmission infinie
  129 + while(1)
168 130 {
169   - switch( State )
170   - {
171   - case RX:
172   - if( isMaster == true )
173   - {
174   - if( BufferSize > 0 )
175   - {
176   - if( strncmp( ( const char* )Buffer, ( const char* )PongMsg, 4 ) == 0 )
177   - {
178   - led = !led;
179   - debug( "...Pong\r\n" );
180   - // Send the next PING frame
181   - strcpy( ( char* )Buffer, ( char* )PingMsg );
182   - // We fill the buffer with numbers for the payload
183   - for( i = 4; i < BufferSize; i++ )
184   - {
185   - Buffer[i] = i - 4;
186   - }
187   - wait_ms( 100);
188   - Radio.Send( Buffer, BufferSize );
189   - }
190   - else if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 )
191   - { // A master already exists then become a slave
192   - debug( "...Ping\r\n" );
193   - led = !led;
194   - isMaster = false;
195   - // Send the next PONG frame
196   - strcpy( ( char* )Buffer, ( char* )PongMsg );
197   - // We fill the buffer with numbers for the payload
198   - for( i = 4; i < BufferSize; i++ )
199   - {
200   - Buffer[i] = i - 4;
201   - }
202   - wait_ms( 100 );
203   - Radio.Send( Buffer, BufferSize );
204   - }
205   - else // valid reception but neither a PING or a PONG message
206   - { // Set device as master ans start again
207   - isMaster = true;
208   - Radio.Rx( RX_TIMEOUT_VALUE );
209   - }
210   - }
211   - }
212   - else
213   - {
214   - if( BufferSize > 0 )
215   - {
216   - if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 )
217   - {
218   - led = !led;
219   - debug( "...Ping\r\n" );
220   - // Send the reply to the PING string
221   - strcpy( ( char* )Buffer, ( char* )PongMsg );
222   - // We fill the buffer with numbers for the payload
223   - for( i = 4; i < BufferSize; i++ )
224   - {
225   - Buffer[i] = i - 4;
226   - }
227   - wait_ms( 10 );
228   - Radio.Send( Buffer, BufferSize );
229   - }
230   - else // valid reception but not a PING as expected
231   - { // Set device as master and start again
232   - isMaster = true;
233   - Radio.Rx( RX_TIMEOUT_VALUE );
234   - }
235   - }
236   - }
237   - State = LOWPOWER;
238   - break;
239   - case TX:
240   - led = !led;
241   - if( isMaster == true )
242   - {
243   - debug( "Ping...\r\n" );
244   - }
245   - else
246   - {
247   - debug( "Pong...\r\n" );
248   - }
249   - Radio.Rx( RX_TIMEOUT_VALUE );
250   - State = LOWPOWER;
251   - break;
252   - case RX_TIMEOUT:
253   - if( isMaster == true )
254   - {
255   - // Send the next PING frame
256   - strcpy( ( char* )Buffer, ( char* )PingMsg );
257   - for( i = 4; i < BufferSize; i++ )
258   - {
259   - Buffer[i] = i - 4;
260   - }
261   - wait_ms( 10 );
262   - Radio.Send( Buffer, BufferSize );
263   - }
264   - else
265   - {
266   - Radio.Rx( RX_TIMEOUT_VALUE );
267   - }
268   - State = LOWPOWER;
269   - break;
270   - case RX_ERROR:
271   - // We have received a Packet with a CRC error, send reply as if packet was correct
272   - if( isMaster == true )
273   - {
274   - // Send the next PING frame
275   - strcpy( ( char* )Buffer, ( char* )PingMsg );
276   - for( i = 4; i < BufferSize; i++ )
277   - {
278   - Buffer[i] = i - 4;
279   - }
280   - wait_ms( 10 );
281   - Radio.Send( Buffer, BufferSize );
282   - }
283   - else
284   - {
285   - // Send the next PONG frame
286   - strcpy( ( char* )Buffer, ( char* )PongMsg );
287   - for( i = 4; i < BufferSize; i++ )
288   - {
289   - Buffer[i] = i - 4;
290   - }
291   - wait_ms( 10 );
292   - Radio.Send( Buffer, BufferSize );
293   - }
294   - State = LOWPOWER;
295   - break;
296   - case TX_TIMEOUT:
297   - Radio.Rx( RX_TIMEOUT_VALUE );
298   - State = LOWPOWER;
299   - break;
300   - case LOWPOWER:
301   - break;
302   - default:
303   - State = LOWPOWER;
304   - break;
305   - }
  131 + debug( "Tentative d'envoi :\r\n" );
  132 + strcpy((char*)Buffer, (char*)TestMsg);
  133 + // We fill the buffer with numbers for the payload
  134 + for(i=4; i<BufferSize; i++)
  135 + {
  136 + Buffer[i] = i - 4;
  137 + }
  138 + debug( "Message envoye\r\n" );
  139 + wait_ms(1000);
  140 + Radio.Send(Buffer, BufferSize);
306 141 }
307 142 }
308   -
309   -void OnTxDone( void )
310   -{
311   - Radio.Sleep( );
312   - State = TX;
313   - debug_if( DEBUG_MESSAGE, "> OnTxDone\n\r" );
314   -}
315   -
316   -void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr)
317   -{
318   - Radio.Sleep( );
319   - BufferSize = size;
320   - memcpy( Buffer, payload, BufferSize );
321   - RssiValue = rssi;
322   - SnrValue = snr;
323   - State = RX;
324   - debug_if( DEBUG_MESSAGE, "> OnRxDone\n\r" );
325   -}
326   -
327   -void OnTxTimeout( void )
328   -{
329   - Radio.Sleep( );
330   - State = TX_TIMEOUT;
331   - debug_if( DEBUG_MESSAGE, "> OnTxTimeout\n\r" );
332   -}
333   -
334   -void OnRxTimeout( void )
335   -{
336   - Radio.Sleep( );
337   - Buffer[ BufferSize ] = 0;
338   - State = RX_TIMEOUT;
339   - debug_if( DEBUG_MESSAGE, "> OnRxTimeout\n\r" );
340   -}
341   -
342   -void OnRxError( void )
343   -{
344   - Radio.Sleep( );
345   - State = RX_ERROR;
346   - debug_if( DEBUG_MESSAGE, "> OnRxError\n\r" );
347   -}
... ...