Fork of the Simple Ping-Pong demo application between two SX1272MB2xAs demo board. It's now a simple application demonstrating simple Rx (Receive) from a SX1272 boards.
Fork of SX1272PingPong by
main.cpp@17:c44bd9aea979, 2017-03-13 (annotated)
- Committer:
- Antoine38
- Date:
- Mon Mar 13 10:58:11 2017 +0000
- Revision:
- 17:c44bd9aea979
- Parent:
- 15:dc867bef95d9
- Child:
- 18:79e1a9635c6d
Receiver is now stateless
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
GregCr | 0:1ed39951ab7b | 1 | #include "mbed.h" |
GregCr | 4:5ece30264cd9 | 2 | #include "main.h" |
GregCr | 13:edb9b443c1dd | 3 | #include "sx1272-hal.h" |
GregCr | 8:f956dee63a56 | 4 | #include "debug.h" |
GregCr | 0:1ed39951ab7b | 5 | |
GregCr | 0:1ed39951ab7b | 6 | /* Set this flag to '1' to display debug messages on the console */ |
GregCr | 13:edb9b443c1dd | 7 | #define DEBUG_MESSAGE 1 |
GregCr | 0:1ed39951ab7b | 8 | |
GregCr | 0:1ed39951ab7b | 9 | |
Antoine38 | 14:6c312f8635fe | 10 | #define RF_FREQUENCY 868000000 // Hz |
GregCr | 0:1ed39951ab7b | 11 | |
Antoine38 | 14:6c312f8635fe | 12 | #define LORA_BANDWIDTH 2 // [0: 125 kHz, |
Antoine38 | 15:dc867bef95d9 | 13 | // 1: 250 kHz, |
Antoine38 | 15:dc867bef95d9 | 14 | // 2: 500 kHz, |
Antoine38 | 15:dc867bef95d9 | 15 | // 3: Reserved] |
Antoine38 | 15:dc867bef95d9 | 16 | |
Antoine38 | 14:6c312f8635fe | 17 | #define LORA_SPREADING_FACTOR 7 // [SF7..SF12] |
Antoine38 | 14:6c312f8635fe | 18 | #define LORA_CODINGRATE 1 // [1: 4/5, |
Antoine38 | 15:dc867bef95d9 | 19 | // 2: 4/6, |
Antoine38 | 15:dc867bef95d9 | 20 | // 3: 4/7, |
Antoine38 | 15:dc867bef95d9 | 21 | // 4: 4/8] |
Antoine38 | 15:dc867bef95d9 | 22 | |
Antoine38 | 15:dc867bef95d9 | 23 | #define LORA_PREAMBLE_LENGTH 8 |
Antoine38 | 14:6c312f8635fe | 24 | #define LORA_SYMBOL_TIMEOUT 5 // Symbols |
Antoine38 | 14:6c312f8635fe | 25 | #define LORA_FIX_LENGTH_PAYLOAD_ON false |
Antoine38 | 14:6c312f8635fe | 26 | #define LORA_FHSS_ENABLED false |
Antoine38 | 14:6c312f8635fe | 27 | #define LORA_NB_SYMB_HOP 4 |
Antoine38 | 14:6c312f8635fe | 28 | #define LORA_IQ_INVERSION_ON false |
Antoine38 | 14:6c312f8635fe | 29 | #define LORA_CRC_ENABLED true |
GregCr | 0:1ed39951ab7b | 30 | |
Antoine38 | 14:6c312f8635fe | 31 | #define RX_TIMEOUT_VALUE 3500000 // in us |
Antoine38 | 14:6c312f8635fe | 32 | #define BUFFER_SIZE 32 // Define the payload size here |
GregCr | 0:1ed39951ab7b | 33 | |
GregCr | 3:8b9e2a4df4b5 | 34 | DigitalOut led(LED1); |
GregCr | 3:8b9e2a4df4b5 | 35 | |
mluis | 10:7af820d1e1df | 36 | /*! |
mluis | 10:7af820d1e1df | 37 | * Radio events function pointer |
mluis | 10:7af820d1e1df | 38 | */ |
mluis | 10:7af820d1e1df | 39 | static RadioEvents_t RadioEvents; |
mluis | 10:7af820d1e1df | 40 | |
mluis | 10:7af820d1e1df | 41 | /* |
mluis | 10:7af820d1e1df | 42 | * Global variables declarations |
mluis | 10:7af820d1e1df | 43 | */ |
GregCr | 13:edb9b443c1dd | 44 | SX1272MB2xAS Radio( NULL ); |
GregCr | 0:1ed39951ab7b | 45 | |
GregCr | 0:1ed39951ab7b | 46 | uint16_t BufferSize = BUFFER_SIZE; |
GregCr | 0:1ed39951ab7b | 47 | uint8_t Buffer[BUFFER_SIZE]; |
GregCr | 0:1ed39951ab7b | 48 | |
GregCr | 5:f2431c4fe3bb | 49 | int16_t RssiValue = 0.0; |
GregCr | 5:f2431c4fe3bb | 50 | int8_t SnrValue = 0.0; |
GregCr | 0:1ed39951ab7b | 51 | |
Antoine38 | 14:6c312f8635fe | 52 | int main() |
GregCr | 0:1ed39951ab7b | 53 | { |
Antoine38 | 17:c44bd9aea979 | 54 | debug( "\n\n\r iGreenhouse Application - Receiver \n\n\r" ); |
mluis | 10:7af820d1e1df | 55 | |
mluis | 10:7af820d1e1df | 56 | // Initialize Radio driver |
mluis | 10:7af820d1e1df | 57 | RadioEvents.RxDone = OnRxDone; |
mluis | 10:7af820d1e1df | 58 | RadioEvents.RxError = OnRxError; |
mluis | 10:7af820d1e1df | 59 | RadioEvents.RxTimeout = OnRxTimeout; |
mluis | 10:7af820d1e1df | 60 | Radio.Init( &RadioEvents ); |
Antoine38 | 14:6c312f8635fe | 61 | |
GregCr | 7:c1bbd6c56979 | 62 | // verify the connection with the board |
Antoine38 | 14:6c312f8635fe | 63 | while( Radio.Read( REG_VERSION ) == 0x00 ) { |
GregCr | 7:c1bbd6c56979 | 64 | debug( "Radio could not be detected!\n\r", NULL ); |
GregCr | 7:c1bbd6c56979 | 65 | wait( 1 ); |
GregCr | 2:59e108728d71 | 66 | } |
Antoine38 | 14:6c312f8635fe | 67 | |
GregCr | 13:edb9b443c1dd | 68 | debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1272MB2XAS ) ) , "\n\r > Board Type: SX1272MB2xAS < \n\r" ); |
GregCr | 0:1ed39951ab7b | 69 | |
Antoine38 | 14:6c312f8635fe | 70 | Radio.SetChannel( RF_FREQUENCY ); |
Antoine38 | 14:6c312f8635fe | 71 | |
Antoine38 | 14:6c312f8635fe | 72 | |
GregCr | 7:c1bbd6c56979 | 73 | debug_if( LORA_FHSS_ENABLED, "\n\n\r > LORA FHSS Mode < \n\n\r"); |
GregCr | 7:c1bbd6c56979 | 74 | debug_if( !LORA_FHSS_ENABLED, "\n\n\r > LORA Mode < \n\n\r"); |
GregCr | 7:c1bbd6c56979 | 75 | |
Antoine38 | 14:6c312f8635fe | 76 | Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR, |
Antoine38 | 14:6c312f8635fe | 77 | LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH, |
Antoine38 | 14:6c312f8635fe | 78 | LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0, |
Antoine38 | 14:6c312f8635fe | 79 | LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, |
Antoine38 | 14:6c312f8635fe | 80 | LORA_IQ_INVERSION_ON, true ); |
GregCr | 0:1ed39951ab7b | 81 | |
Antoine38 | 14:6c312f8635fe | 82 | |
Antoine38 | 14:6c312f8635fe | 83 | debug_if( DEBUG_MESSAGE, "Starting listening loop\r\n" ); |
GregCr | 0:1ed39951ab7b | 84 | |
GregCr | 3:8b9e2a4df4b5 | 85 | led = 0; |
Antoine38 | 14:6c312f8635fe | 86 | |
GregCr | 0:1ed39951ab7b | 87 | Radio.Rx( RX_TIMEOUT_VALUE ); |
Antoine38 | 14:6c312f8635fe | 88 | |
Antoine38 | 14:6c312f8635fe | 89 | while( 1 ) { |
Antoine38 | 17:c44bd9aea979 | 90 | debug("\r\n========\r\nNew Packet\r\n========\r\n"); |
Antoine38 | 17:c44bd9aea979 | 91 | debug("%s \r\n", Buffer); |
Antoine38 | 17:c44bd9aea979 | 92 | wait(3); |
GregCr | 0:1ed39951ab7b | 93 | } |
GregCr | 0:1ed39951ab7b | 94 | } |
GregCr | 0:1ed39951ab7b | 95 | |
GregCr | 4:5ece30264cd9 | 96 | void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr) |
GregCr | 0:1ed39951ab7b | 97 | { |
GregCr | 0:1ed39951ab7b | 98 | Radio.Sleep( ); |
GregCr | 0:1ed39951ab7b | 99 | BufferSize = size; |
GregCr | 0:1ed39951ab7b | 100 | memcpy( Buffer, payload, BufferSize ); |
GregCr | 0:1ed39951ab7b | 101 | RssiValue = rssi; |
GregCr | 0:1ed39951ab7b | 102 | SnrValue = snr; |
GregCr | 7:c1bbd6c56979 | 103 | debug_if( DEBUG_MESSAGE, "> OnRxDone\n\r" ); |
GregCr | 0:1ed39951ab7b | 104 | } |
GregCr | 0:1ed39951ab7b | 105 | |
GregCr | 0:1ed39951ab7b | 106 | void OnRxTimeout( void ) |
GregCr | 0:1ed39951ab7b | 107 | { |
GregCr | 0:1ed39951ab7b | 108 | Radio.Sleep( ); |
GregCr | 1:126d70d374f6 | 109 | Buffer[ BufferSize ] = 0; |
GregCr | 7:c1bbd6c56979 | 110 | debug_if( DEBUG_MESSAGE, "> OnRxTimeout\n\r" ); |
GregCr | 0:1ed39951ab7b | 111 | } |
GregCr | 0:1ed39951ab7b | 112 | |
GregCr | 0:1ed39951ab7b | 113 | void OnRxError( void ) |
GregCr | 0:1ed39951ab7b | 114 | { |
GregCr | 0:1ed39951ab7b | 115 | Radio.Sleep( ); |
GregCr | 7:c1bbd6c56979 | 116 | debug_if( DEBUG_MESSAGE, "> OnRxError\n\r" ); |
GregCr | 0:1ed39951ab7b | 117 | } |
GregCr | 3:8b9e2a4df4b5 | 118 |