Commit 7e1ad503c4cce9a875145fdc7d40860e093db1d0
0 parents
fichiers départ + pingpong
Showing
10 changed files
with
890 additions
and
0 deletions
Show diff stats
1 | +++ a/emetteur/emetteur.cpp | |
... | ... | @@ -0,0 +1,347 @@ |
1 | +#include "../mbed-os/mbed.h" | |
2 | +#include "main.h" | |
3 | +#include "sx1276-hal.h" | |
4 | +#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 */ | |
10 | +#define USE_MODEM_LORA 1 | |
11 | +#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 | + */ | |
61 | +typedef enum | |
62 | +{ | |
63 | + LOWPOWER = 0, | |
64 | + IDLE, | |
65 | + | |
66 | + RX, | |
67 | + RX_TIMEOUT, | |
68 | + RX_ERROR, | |
69 | + | |
70 | + TX, | |
71 | + TX_TIMEOUT, | |
72 | + | |
73 | + CAD, | |
74 | + CAD_DONE | |
75 | +}AppStates_t; | |
76 | + | |
77 | +volatile AppStates_t State = LOWPOWER; | |
78 | + | |
79 | +/*! | |
80 | + * Radio events function pointer | |
81 | + */ | |
82 | +static RadioEvents_t RadioEvents; | |
83 | + | |
84 | +/* | |
85 | + * Global variables declarations | |
86 | + */ | |
87 | +SX1276MB1xAS Radio( NULL ); | |
88 | + | |
89 | +const uint8_t PingMsg[] = "PING"; | |
90 | +const uint8_t PongMsg[] = "PONG"; | |
91 | + | |
92 | +uint16_t BufferSize = BUFFER_SIZE; | |
93 | +uint8_t Buffer[BUFFER_SIZE]; | |
94 | + | |
95 | +int16_t RssiValue = 0.0; | |
96 | +int8_t SnrValue = 0.0; | |
97 | + | |
98 | +int main() | |
99 | +{ | |
100 | + uint8_t i; | |
101 | + bool isMaster = true; | |
102 | + | |
103 | + debug( "\n\n\r SX1276 Ping Pong Demo Application \n\n\r" ); | |
104 | + | |
105 | + // Initialize Radio driver | |
106 | + RadioEvents.TxDone = OnTxDone; | |
107 | + RadioEvents.RxDone = OnRxDone; | |
108 | + RadioEvents.RxError = OnRxError; | |
109 | + RadioEvents.TxTimeout = OnTxTimeout; | |
110 | + RadioEvents.RxTimeout = OnRxTimeout; | |
111 | + Radio.Init( &RadioEvents ); | |
112 | + | |
113 | + // verify the connection with the board | |
114 | + while( Radio.Read( REG_VERSION ) == 0x00 ) | |
115 | + { | |
116 | + debug( "Radio could not be detected!\n\r", NULL ); | |
117 | + wait( 1 ); | |
118 | + } | |
119 | + | |
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 ); | |
154 | + | |
155 | +#else | |
156 | + | |
157 | +#error "Please define a modem in the compiler options." | |
158 | + | |
159 | +#endif | |
160 | + | |
161 | + debug_if( DEBUG_MESSAGE, "Starting Ping-Pong loop\r\n" ); | |
162 | + | |
163 | + led = 0; | |
164 | + | |
165 | + Radio.Rx( RX_TIMEOUT_VALUE ); | |
166 | + | |
167 | + while( 1 ) | |
168 | + { | |
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 | + } | |
306 | + } | |
307 | +} | |
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 | +} | ... | ... |
1 | +++ a/emetteur/makefile | |
... | ... | @@ -0,0 +1,26 @@ |
1 | +TARGET=emetteur | |
2 | + | |
3 | +CC=arm-none-eabi-gcc | |
4 | +LD=arm-none-eabi-gcc | |
5 | +AR=arm-none-eabi-ar | |
6 | +AS=arm-none-eabi-as | |
7 | +CP=arm-none-eabi-objcopy | |
8 | +OD=arm-none-eabi-objdump | |
9 | +SE=arm-none-eabi-size | |
10 | + | |
11 | +CFLAGS = -std=gnu99 -g -O2 -Wall | |
12 | +CFLAGS += -mlittle-endian -mthumb -mthumb-interwork -mcpu=cortex-m0 | |
13 | +CFLAGS += -fsingle-precision-constant -Wdouble-promotion | |
14 | + | |
15 | +SRCS = emetteur.cpp | |
16 | + | |
17 | +.PHONY: $(TARGET) | |
18 | + | |
19 | +$(TARGET): $(TARGET).elf | |
20 | + | |
21 | +$(TARGET).elf: $(SRCS) | |
22 | + $(CC) --specs=nosys.specs $(INCLUDE) $(CFLAGS) $^ -o $@ | |
23 | + $(CP) -O binary $(TARGET).elf $(TARGET).bin | |
24 | + | |
25 | +clean: | |
26 | + rm -f *.o $(TARGET).elf $(TARGET).bin | ... | ... |
No preview for this file type
1 | +++ a/pingpong/files/main.cpp | |
... | ... | @@ -0,0 +1,359 @@ |
1 | +#include "mbed.h" | |
2 | +#include "main.h" | |
3 | +#include "sx1276-hal.h" | |
4 | +#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 */ | |
10 | +#define USE_MODEM_LORA 1 | |
11 | +#define USE_MODEM_FSK !USE_MODEM_LORA | |
12 | + | |
13 | +#define RF_FREQUENCY 915000000 // Hz | |
14 | +#define TX_OUTPUT_POWER 14 // 14 dBm | |
15 | + | |
16 | +#if USE_MODEM_LORA == 1 | |
17 | + | |
18 | + #define LORA_BANDWIDTH 1 // [0: 125 kHz, | |
19 | + // 1: 250 kHz, | |
20 | + // 2: 500 kHz, | |
21 | + // 3: Reserved] | |
22 | + #define LORA_SPREADING_FACTOR 10 // [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 true | |
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 3500 // in ms | |
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 | + */ | |
61 | +typedef enum | |
62 | +{ | |
63 | + LOWPOWER = 0, | |
64 | + IDLE, | |
65 | + | |
66 | + RX, | |
67 | + RX_TIMEOUT, | |
68 | + RX_ERROR, | |
69 | + | |
70 | + TX, | |
71 | + TX_TIMEOUT, | |
72 | + | |
73 | + CAD, | |
74 | + CAD_DONE | |
75 | +}AppStates_t; | |
76 | + | |
77 | +volatile AppStates_t State = LOWPOWER; | |
78 | + | |
79 | +/*! | |
80 | + * Radio events function pointer | |
81 | + */ | |
82 | +static RadioEvents_t RadioEvents; | |
83 | + | |
84 | +/* | |
85 | + * Global variables declarations | |
86 | + */ | |
87 | +SX1276MB1xAS Radio( NULL ); | |
88 | + | |
89 | +const uint8_t PingMsg[] = "PING"; | |
90 | +const uint8_t PongMsg[] = "PONG"; | |
91 | + | |
92 | +uint16_t BufferSize = BUFFER_SIZE; | |
93 | +uint8_t Buffer[BUFFER_SIZE]; | |
94 | + | |
95 | +int16_t RssiValue = 0.0; | |
96 | +int8_t SnrValue = 0.0; | |
97 | + | |
98 | +int main( void ) | |
99 | +{ | |
100 | + uint8_t i; | |
101 | + bool isMaster = true; | |
102 | + | |
103 | + debug( "\n\n\r SX1276 Ping Pong Demo Application \n\n\r" ); | |
104 | + | |
105 | + // Initialize Radio driver | |
106 | + RadioEvents.TxDone = OnTxDone; | |
107 | + RadioEvents.RxDone = OnRxDone; | |
108 | + RadioEvents.RxError = OnRxError; | |
109 | + RadioEvents.TxTimeout = OnTxTimeout; | |
110 | + RadioEvents.RxTimeout = OnRxTimeout; | |
111 | + RadioEvents.FhssChangeChannel = OnFhssChangeChannel; | |
112 | + Radio.Init( &RadioEvents ); | |
113 | + | |
114 | + // verify the connection with the board | |
115 | + while( Radio.Read( REG_VERSION ) == 0x00 ) | |
116 | + { | |
117 | + debug( "Radio could not be detected!\n\r", NULL ); | |
118 | + wait( 1 ); | |
119 | + } | |
120 | + | |
121 | + debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1276MB1LAS ) ), "\n\r > Board Type: SX1276MB1LAS < \n\r" ); | |
122 | + debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1276MB1MAS ) ), "\n\r > Board Type: SX1276MB1MAS < \n\r" ); | |
123 | + | |
124 | + Radio.SetChannel( HoppingFrequencies[0] ); | |
125 | + | |
126 | +#if USE_MODEM_LORA == 1 | |
127 | + | |
128 | + debug_if( LORA_FHSS_ENABLED, "\n\n\r > LORA FHSS Mode < \n\n\r" ); | |
129 | + debug_if( !LORA_FHSS_ENABLED, "\n\n\r > LORA Mode < \n\n\r" ); | |
130 | + | |
131 | + Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH, | |
132 | + LORA_SPREADING_FACTOR, LORA_CODINGRATE, | |
133 | + LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON, | |
134 | + LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, | |
135 | + LORA_IQ_INVERSION_ON, 4000 ); | |
136 | + | |
137 | + Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR, | |
138 | + LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH, | |
139 | + LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0, | |
140 | + LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, | |
141 | + LORA_IQ_INVERSION_ON, true ); | |
142 | + | |
143 | +#elif USE_MODEM_FSK == 1 | |
144 | + | |
145 | + debug("\n\n\r > FSK Mode < \n\n\r" ); | |
146 | + Radio.SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0, | |
147 | + FSK_DATARATE, 0, | |
148 | + FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON, | |
149 | + FSK_CRC_ENABLED, 0, 0, 0, 3000 ); | |
150 | + | |
151 | + Radio.SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE, | |
152 | + 0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH, | |
153 | + 0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, FSK_CRC_ENABLED, | |
154 | + 0, 0, false, true ); | |
155 | + | |
156 | +#else | |
157 | + | |
158 | +#error "Please define a modem in the compiler options." | |
159 | + | |
160 | +#endif | |
161 | + | |
162 | + debug_if( DEBUG_MESSAGE, "Starting Ping-Pong loop\r\n" ); | |
163 | + | |
164 | + led = 0; | |
165 | + | |
166 | + Radio.Rx( RX_TIMEOUT_VALUE ); | |
167 | + | |
168 | + while( 1 ) | |
169 | + { | |
170 | + switch( State ) | |
171 | + { | |
172 | + case RX: | |
173 | + if( isMaster == true ) | |
174 | + { | |
175 | + if( BufferSize > 0 ) | |
176 | + { | |
177 | + if( strncmp( ( const char* )Buffer, ( const char* )PongMsg, 4 ) == 0 ) | |
178 | + { | |
179 | + led = !led; | |
180 | + debug( "...Pong\r\n" ); | |
181 | + // Send the next PING frame | |
182 | + strcpy( ( char* )Buffer, ( char* )PingMsg ); | |
183 | + // We fill the buffer with numbers for the payload | |
184 | + for( i = 4; i < BufferSize; i++ ) | |
185 | + { | |
186 | + Buffer[i] = i - 4; | |
187 | + } | |
188 | + wait_ms( 10 ); | |
189 | + Radio.Send( Buffer, BufferSize ); | |
190 | + } | |
191 | + else if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 ) | |
192 | + { // A master already exists then become a slave | |
193 | + debug( "...Ping\r\n" ); | |
194 | + led = !led; | |
195 | + isMaster = false; | |
196 | + // Send the next PONG frame | |
197 | + strcpy( ( char* )Buffer, ( char* )PongMsg ); | |
198 | + // We fill the buffer with numbers for the payload | |
199 | + for( i = 4; i < BufferSize; i++ ) | |
200 | + { | |
201 | + Buffer[i] = i - 4; | |
202 | + } | |
203 | + wait_ms( 10 ); | |
204 | + Radio.Send( Buffer, BufferSize ); | |
205 | + } | |
206 | + else // valid reception but neither a PING or a PONG message | |
207 | + { // Set device as master ans start again | |
208 | + isMaster = true; | |
209 | + Radio.Rx( RX_TIMEOUT_VALUE ); | |
210 | + } | |
211 | + } | |
212 | + } | |
213 | + else | |
214 | + { | |
215 | + if( BufferSize > 0 ) | |
216 | + { | |
217 | + if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 ) | |
218 | + { | |
219 | + led = !led; | |
220 | + debug( "...Ping\r\n" ); | |
221 | + // Send the reply to the PING string | |
222 | + strcpy( ( char* )Buffer, ( char* )PongMsg ); | |
223 | + // We fill the buffer with numbers for the payload | |
224 | + for( i = 4; i < BufferSize; i++ ) | |
225 | + { | |
226 | + Buffer[i] = i - 4; | |
227 | + } | |
228 | + wait_ms( 10 ); | |
229 | + Radio.Send( Buffer, BufferSize ); | |
230 | + } | |
231 | + else // valid reception but not a PING as expected | |
232 | + { // Set device as master and start again | |
233 | + isMaster = true; | |
234 | + Radio.Rx( RX_TIMEOUT_VALUE ); | |
235 | + } | |
236 | + } | |
237 | + } | |
238 | + State = LOWPOWER; | |
239 | + break; | |
240 | + case TX: | |
241 | + led = !led; | |
242 | + if( isMaster == true ) | |
243 | + { | |
244 | + debug( "Ping...\r\n" ); | |
245 | + } | |
246 | + else | |
247 | + { | |
248 | + debug( "Pong...\r\n" ); | |
249 | + } | |
250 | + Radio.Rx( RX_TIMEOUT_VALUE ); | |
251 | + State = LOWPOWER; | |
252 | + break; | |
253 | + case RX_TIMEOUT: | |
254 | + if( isMaster == true ) | |
255 | + { | |
256 | + // Send the next PING frame | |
257 | + strcpy( ( char* )Buffer, ( char* )PingMsg ); | |
258 | + for( i = 4; i < BufferSize; i++ ) | |
259 | + { | |
260 | + Buffer[i] = i - 4; | |
261 | + } | |
262 | + wait_ms( 10 ); | |
263 | + Radio.Send( Buffer, BufferSize ); | |
264 | + } | |
265 | + else | |
266 | + { | |
267 | + Radio.Rx( RX_TIMEOUT_VALUE ); | |
268 | + } | |
269 | + State = LOWPOWER; | |
270 | + break; | |
271 | + case RX_ERROR: | |
272 | + // We have received a Packet with a CRC error, send reply as if packet was correct | |
273 | + if( isMaster == true ) | |
274 | + { | |
275 | + // Send the next PING frame | |
276 | + strcpy( ( char* )Buffer, ( char* )PingMsg ); | |
277 | + for( i = 4; i < BufferSize; i++ ) | |
278 | + { | |
279 | + Buffer[i] = i - 4; | |
280 | + } | |
281 | + wait_ms( 10 ); | |
282 | + Radio.Send( Buffer, BufferSize ); | |
283 | + } | |
284 | + else | |
285 | + { | |
286 | + // Send the next PONG frame | |
287 | + strcpy( ( char* )Buffer, ( char* )PongMsg ); | |
288 | + for( i = 4; i < BufferSize; i++ ) | |
289 | + { | |
290 | + Buffer[i] = i - 4; | |
291 | + } | |
292 | + wait_ms( 10 ); | |
293 | + Radio.Send( Buffer, BufferSize ); | |
294 | + } | |
295 | + State = LOWPOWER; | |
296 | + break; | |
297 | + case TX_TIMEOUT: | |
298 | + Radio.Rx( RX_TIMEOUT_VALUE ); | |
299 | + State = LOWPOWER; | |
300 | + break; | |
301 | + case LOWPOWER: | |
302 | + break; | |
303 | + default: | |
304 | + State = LOWPOWER; | |
305 | + break; | |
306 | + } | |
307 | + } | |
308 | +} | |
309 | + | |
310 | +void OnTxDone( void ) | |
311 | +{ | |
312 | + Radio.SetChannel( HoppingFrequencies[0] ); | |
313 | + Radio.Sleep( ); | |
314 | + State = TX; | |
315 | + debug_if( DEBUG_MESSAGE, "> OnTxDone\n\r" ); | |
316 | +} | |
317 | + | |
318 | +void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ) | |
319 | +{ | |
320 | + Radio.SetChannel( HoppingFrequencies[0] ); | |
321 | + Radio.Sleep( ); | |
322 | + BufferSize = size; | |
323 | + memcpy( Buffer, payload, BufferSize ); | |
324 | + RssiValue = rssi; | |
325 | + SnrValue = snr; | |
326 | + State = RX; | |
327 | + debug_if( DEBUG_MESSAGE, "> OnRxDone\n\r" ); | |
328 | +} | |
329 | + | |
330 | +void OnTxTimeout( void ) | |
331 | +{ | |
332 | + Radio.SetChannel( HoppingFrequencies[0] ); | |
333 | + Radio.Sleep( ); | |
334 | + State = TX_TIMEOUT; | |
335 | + debug_if( DEBUG_MESSAGE, "> OnTxTimeout\n\r" ); | |
336 | +} | |
337 | + | |
338 | +void OnRxTimeout( void ) | |
339 | +{ | |
340 | + Radio.SetChannel( HoppingFrequencies[0] ); | |
341 | + Radio.Sleep( ); | |
342 | + Buffer[BufferSize] = 0; | |
343 | + State = RX_TIMEOUT; | |
344 | + debug_if( DEBUG_MESSAGE, "> OnRxTimeout\n\r" ); | |
345 | +} | |
346 | + | |
347 | +void OnRxError( void ) | |
348 | +{ | |
349 | + Radio.SetChannel( HoppingFrequencies[0] ); | |
350 | + Radio.Sleep( ); | |
351 | + State = RX_ERROR; | |
352 | + debug_if( DEBUG_MESSAGE, "> OnRxError\n\r" ); | |
353 | +} | |
354 | + | |
355 | +void OnFhssChangeChannel( uint8_t channelIndex ) | |
356 | +{ | |
357 | + Radio.SetChannel( HoppingFrequencies[channelIndex] ); | |
358 | + debug_if( DEBUG_MESSAGE, "F%d-", channelIndex ); | |
359 | +} | ... | ... |
1 | +++ a/pingpong/files/main.h | |
... | ... | @@ -0,0 +1,115 @@ |
1 | +/* | |
2 | + / _____) _ | | | |
3 | +( (____ _____ ____ _| |_ _____ ____| |__ | |
4 | + \____ \| ___ | (_ _) ___ |/ ___) _ \ | |
5 | + _____) ) ____| | | || |_| ____( (___| | | | | |
6 | +(______/|_____)_|_|_| \__)_____)\____)_| |_| | |
7 | + ( C )2014 Semtech | |
8 | + | |
9 | +Description: Contains the callbacks for the IRQs and any application related details | |
10 | + | |
11 | +License: Revised BSD License, see LICENSE.TXT file include in the project | |
12 | + | |
13 | +Maintainer: Miguel Luis and Gregory Cristian | |
14 | +*/ | |
15 | +#ifndef __MAIN_H__ | |
16 | +#define __MAIN_H__ | |
17 | + | |
18 | + | |
19 | +/*! | |
20 | + * Frequency hopping frequencies table | |
21 | + */ | |
22 | +const uint32_t HoppingFrequencies[] = | |
23 | +{ | |
24 | + 916500000, | |
25 | + 923500000, | |
26 | + 906500000, | |
27 | + 917500000, | |
28 | + 917500000, | |
29 | + 909000000, | |
30 | + 903000000, | |
31 | + 916000000, | |
32 | + 912500000, | |
33 | + 926000000, | |
34 | + 925000000, | |
35 | + 909500000, | |
36 | + 913000000, | |
37 | + 918500000, | |
38 | + 918500000, | |
39 | + 902500000, | |
40 | + 911500000, | |
41 | + 926500000, | |
42 | + 902500000, | |
43 | + 922000000, | |
44 | + 924000000, | |
45 | + 903500000, | |
46 | + 913000000, | |
47 | + 922000000, | |
48 | + 926000000, | |
49 | + 910000000, | |
50 | + 920000000, | |
51 | + 922500000, | |
52 | + 911000000, | |
53 | + 922000000, | |
54 | + 909500000, | |
55 | + 926000000, | |
56 | + 922000000, | |
57 | + 918000000, | |
58 | + 925500000, | |
59 | + 908000000, | |
60 | + 917500000, | |
61 | + 926500000, | |
62 | + 908500000, | |
63 | + 916000000, | |
64 | + 905500000, | |
65 | + 916000000, | |
66 | + 903000000, | |
67 | + 905000000, | |
68 | + 915000000, | |
69 | + 913000000, | |
70 | + 907000000, | |
71 | + 910000000, | |
72 | + 926500000, | |
73 | + 925500000, | |
74 | + 911000000 | |
75 | +}; | |
76 | + | |
77 | +/* | |
78 | + * Callback functions prototypes | |
79 | + */ | |
80 | +/*! | |
81 | + * @brief Function to be executed on Radio Tx Done event | |
82 | + */ | |
83 | +void OnTxDone( void ); | |
84 | + | |
85 | +/*! | |
86 | + * @brief Function to be executed on Radio Rx Done event | |
87 | + */ | |
88 | +void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ); | |
89 | + | |
90 | +/*! | |
91 | + * @brief Function executed on Radio Tx Timeout event | |
92 | + */ | |
93 | +void OnTxTimeout( void ); | |
94 | + | |
95 | +/*! | |
96 | + * @brief Function executed on Radio Rx Timeout event | |
97 | + */ | |
98 | +void OnRxTimeout( void ); | |
99 | + | |
100 | +/*! | |
101 | + * @brief Function executed on Radio Rx Error event | |
102 | + */ | |
103 | +void OnRxError( void ); | |
104 | + | |
105 | +/*! | |
106 | + * @brief Function executed on Radio Fhss Change Channel event | |
107 | + */ | |
108 | +void OnFhssChangeChannel( uint8_t channelIndex ); | |
109 | + | |
110 | +/*! | |
111 | + * @brief Function executed on CAD Done event | |
112 | + */ | |
113 | +void OnCadDone( void ); | |
114 | + | |
115 | +#endif // __MAIN_H__ | ... | ... |
1 | +++ a/recepteur/makefile | |
... | ... | @@ -0,0 +1,30 @@ |
1 | +TARGET=recepteur | |
2 | + | |
3 | +CC=arm-none-eabi-gcc | |
4 | +LD=arm-none-eabi-gcc | |
5 | +AR=arm-none-eabi-ar | |
6 | +AS=arm-none-eabi-as | |
7 | +CP=arm-none-eabi-objcopy | |
8 | +OD=arm-none-eabi-objdump | |
9 | +SE=arm-none-eabi-size | |
10 | +SF=st-flash | |
11 | + | |
12 | +CFLAGS = -std=gnu99 -g -O2 -Wall | |
13 | +CFLAGS += -mlittle-endian -mthumb -mthumb-interwork -mcpu=cortex-m0 | |
14 | +CFLAGS += -fsingle-precision-constant -Wdouble-promotion | |
15 | + | |
16 | +SRCS = recepteur.c | |
17 | + | |
18 | +.PHONY: $(TARGET) | |
19 | + | |
20 | +$(TARGET): $(TARGET).elf | |
21 | + | |
22 | +$(TARGET).elf: $(SRCS) | |
23 | + $(CC) $(INCLUDE) $(CFLAGS) $^ -o $@ | |
24 | + $(CP) -O binary $(TARGET).elf $(TARGET).bin | |
25 | + | |
26 | +clean: | |
27 | + rm -f *.o $(TARGET).elf $(TARGET).bin | |
28 | + | |
29 | +flash: | |
30 | + $(SF) write $(TARGET).bin 0x8000000 | ... | ... |