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@15:dc867bef95d9, 2017-03-13 (annotated)
- Committer:
- Antoine38
- Date:
- Mon Mar 13 09:54:09 2017 +0000
- Revision:
- 15:dc867bef95d9
- Parent:
- 14:6c312f8635fe
- Child:
- 17:c44bd9aea979
On affiche dor?navant le contenu du buffer ? chaque reception
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 | |
GregCr | 0:1ed39951ab7b | 36 | /* |
GregCr | 0:1ed39951ab7b | 37 | * Global variables declarations |
GregCr | 0:1ed39951ab7b | 38 | */ |
Antoine38 | 14:6c312f8635fe | 39 | typedef enum { |
mluis | 10:7af820d1e1df | 40 | LOWPOWER = 0, |
mluis | 10:7af820d1e1df | 41 | IDLE, |
Antoine38 | 14:6c312f8635fe | 42 | |
mluis | 10:7af820d1e1df | 43 | RX, |
mluis | 10:7af820d1e1df | 44 | RX_TIMEOUT, |
mluis | 10:7af820d1e1df | 45 | RX_ERROR, |
Antoine38 | 14:6c312f8635fe | 46 | |
mluis | 10:7af820d1e1df | 47 | CAD, |
mluis | 10:7af820d1e1df | 48 | CAD_DONE |
Antoine38 | 14:6c312f8635fe | 49 | } AppStates_t; |
GregCr | 0:1ed39951ab7b | 50 | |
mluis | 10:7af820d1e1df | 51 | volatile AppStates_t State = LOWPOWER; |
mluis | 10:7af820d1e1df | 52 | |
mluis | 10:7af820d1e1df | 53 | /*! |
mluis | 10:7af820d1e1df | 54 | * Radio events function pointer |
mluis | 10:7af820d1e1df | 55 | */ |
mluis | 10:7af820d1e1df | 56 | static RadioEvents_t RadioEvents; |
mluis | 10:7af820d1e1df | 57 | |
mluis | 10:7af820d1e1df | 58 | /* |
mluis | 10:7af820d1e1df | 59 | * Global variables declarations |
mluis | 10:7af820d1e1df | 60 | */ |
GregCr | 13:edb9b443c1dd | 61 | SX1272MB2xAS Radio( NULL ); |
GregCr | 0:1ed39951ab7b | 62 | |
GregCr | 0:1ed39951ab7b | 63 | uint16_t BufferSize = BUFFER_SIZE; |
GregCr | 0:1ed39951ab7b | 64 | uint8_t Buffer[BUFFER_SIZE]; |
GregCr | 0:1ed39951ab7b | 65 | |
GregCr | 5:f2431c4fe3bb | 66 | int16_t RssiValue = 0.0; |
GregCr | 5:f2431c4fe3bb | 67 | int8_t SnrValue = 0.0; |
GregCr | 0:1ed39951ab7b | 68 | |
Antoine38 | 14:6c312f8635fe | 69 | int main() |
GregCr | 0:1ed39951ab7b | 70 | { |
GregCr | 0:1ed39951ab7b | 71 | uint8_t i; |
Antoine38 | 14:6c312f8635fe | 72 | |
Antoine38 | 14:6c312f8635fe | 73 | debug( "\n\n\r iGreenhouse Application \n\n\r" ); |
mluis | 10:7af820d1e1df | 74 | |
mluis | 10:7af820d1e1df | 75 | // Initialize Radio driver |
mluis | 10:7af820d1e1df | 76 | RadioEvents.RxDone = OnRxDone; |
mluis | 10:7af820d1e1df | 77 | RadioEvents.RxError = OnRxError; |
mluis | 10:7af820d1e1df | 78 | RadioEvents.RxTimeout = OnRxTimeout; |
mluis | 10:7af820d1e1df | 79 | Radio.Init( &RadioEvents ); |
Antoine38 | 14:6c312f8635fe | 80 | |
GregCr | 7:c1bbd6c56979 | 81 | // verify the connection with the board |
Antoine38 | 14:6c312f8635fe | 82 | while( Radio.Read( REG_VERSION ) == 0x00 ) { |
GregCr | 7:c1bbd6c56979 | 83 | debug( "Radio could not be detected!\n\r", NULL ); |
GregCr | 7:c1bbd6c56979 | 84 | wait( 1 ); |
GregCr | 2:59e108728d71 | 85 | } |
Antoine38 | 14:6c312f8635fe | 86 | |
GregCr | 13:edb9b443c1dd | 87 | debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1272MB2XAS ) ) , "\n\r > Board Type: SX1272MB2xAS < \n\r" ); |
GregCr | 0:1ed39951ab7b | 88 | |
Antoine38 | 14:6c312f8635fe | 89 | Radio.SetChannel( RF_FREQUENCY ); |
Antoine38 | 14:6c312f8635fe | 90 | |
Antoine38 | 14:6c312f8635fe | 91 | |
GregCr | 7:c1bbd6c56979 | 92 | debug_if( LORA_FHSS_ENABLED, "\n\n\r > LORA FHSS Mode < \n\n\r"); |
GregCr | 7:c1bbd6c56979 | 93 | debug_if( !LORA_FHSS_ENABLED, "\n\n\r > LORA Mode < \n\n\r"); |
GregCr | 7:c1bbd6c56979 | 94 | |
Antoine38 | 14:6c312f8635fe | 95 | Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR, |
Antoine38 | 14:6c312f8635fe | 96 | LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH, |
Antoine38 | 14:6c312f8635fe | 97 | LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0, |
Antoine38 | 14:6c312f8635fe | 98 | LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, |
Antoine38 | 14:6c312f8635fe | 99 | LORA_IQ_INVERSION_ON, true ); |
GregCr | 0:1ed39951ab7b | 100 | |
Antoine38 | 14:6c312f8635fe | 101 | |
Antoine38 | 14:6c312f8635fe | 102 | debug_if( DEBUG_MESSAGE, "Starting listening loop\r\n" ); |
GregCr | 0:1ed39951ab7b | 103 | |
GregCr | 3:8b9e2a4df4b5 | 104 | led = 0; |
Antoine38 | 14:6c312f8635fe | 105 | |
GregCr | 0:1ed39951ab7b | 106 | Radio.Rx( RX_TIMEOUT_VALUE ); |
Antoine38 | 14:6c312f8635fe | 107 | |
Antoine38 | 14:6c312f8635fe | 108 | while( 1 ) { |
Antoine38 | 14:6c312f8635fe | 109 | switch( State ) { |
Antoine38 | 14:6c312f8635fe | 110 | case RX: |
Antoine38 | 15:dc867bef95d9 | 111 | if( BufferSize > 0 ) { |
Antoine38 | 15:dc867bef95d9 | 112 | debug("\r\n========\r\nNew Packet\r\n========\r\n"); |
Antoine38 | 15:dc867bef95d9 | 113 | debug("%s \r\n", Buffer); |
Antoine38 | 15:dc867bef95d9 | 114 | /*if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, 4 ) == 0 ) { |
Antoine38 | 15:dc867bef95d9 | 115 | led = !led; |
Antoine38 | 15:dc867bef95d9 | 116 | debug( "...Ping\r\n" ); |
Antoine38 | 15:dc867bef95d9 | 117 | } else { // valid reception but not a PING as expected, let start again |
Antoine38 | 15:dc867bef95d9 | 118 | Radio.Rx( RX_TIMEOUT_VALUE ); |
Antoine38 | 15:dc867bef95d9 | 119 | }*/ |
Antoine38 | 15:dc867bef95d9 | 120 | } |
Antoine38 | 14:6c312f8635fe | 121 | State = LOWPOWER; |
Antoine38 | 14:6c312f8635fe | 122 | break; |
Antoine38 | 15:dc867bef95d9 | 123 | case RX_TIMEOUT: |
Antoine38 | 14:6c312f8635fe | 124 | Radio.Rx( RX_TIMEOUT_VALUE ); |
Antoine38 | 14:6c312f8635fe | 125 | State = LOWPOWER; |
Antoine38 | 14:6c312f8635fe | 126 | break; |
Antoine38 | 14:6c312f8635fe | 127 | case RX_ERROR: |
Antoine38 | 15:dc867bef95d9 | 128 | // We have received a Packet with a CRC error |
Antoine38 | 14:6c312f8635fe | 129 | State = LOWPOWER; |
Antoine38 | 14:6c312f8635fe | 130 | break; |
Antoine38 | 14:6c312f8635fe | 131 | case LOWPOWER: |
Antoine38 | 14:6c312f8635fe | 132 | break; |
Antoine38 | 14:6c312f8635fe | 133 | default: |
Antoine38 | 14:6c312f8635fe | 134 | State = LOWPOWER; |
Antoine38 | 14:6c312f8635fe | 135 | break; |
Antoine38 | 14:6c312f8635fe | 136 | } |
GregCr | 0:1ed39951ab7b | 137 | } |
GregCr | 0:1ed39951ab7b | 138 | } |
GregCr | 0:1ed39951ab7b | 139 | |
GregCr | 4:5ece30264cd9 | 140 | void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr) |
GregCr | 0:1ed39951ab7b | 141 | { |
GregCr | 0:1ed39951ab7b | 142 | Radio.Sleep( ); |
GregCr | 0:1ed39951ab7b | 143 | BufferSize = size; |
GregCr | 0:1ed39951ab7b | 144 | memcpy( Buffer, payload, BufferSize ); |
GregCr | 0:1ed39951ab7b | 145 | RssiValue = rssi; |
GregCr | 0:1ed39951ab7b | 146 | SnrValue = snr; |
GregCr | 0:1ed39951ab7b | 147 | State = RX; |
GregCr | 7:c1bbd6c56979 | 148 | debug_if( DEBUG_MESSAGE, "> OnRxDone\n\r" ); |
GregCr | 0:1ed39951ab7b | 149 | } |
GregCr | 0:1ed39951ab7b | 150 | |
GregCr | 0:1ed39951ab7b | 151 | void OnRxTimeout( void ) |
GregCr | 0:1ed39951ab7b | 152 | { |
GregCr | 0:1ed39951ab7b | 153 | Radio.Sleep( ); |
GregCr | 1:126d70d374f6 | 154 | Buffer[ BufferSize ] = 0; |
GregCr | 0:1ed39951ab7b | 155 | State = RX_TIMEOUT; |
GregCr | 7:c1bbd6c56979 | 156 | debug_if( DEBUG_MESSAGE, "> OnRxTimeout\n\r" ); |
GregCr | 0:1ed39951ab7b | 157 | } |
GregCr | 0:1ed39951ab7b | 158 | |
GregCr | 0:1ed39951ab7b | 159 | void OnRxError( void ) |
GregCr | 0:1ed39951ab7b | 160 | { |
GregCr | 0:1ed39951ab7b | 161 | Radio.Sleep( ); |
GregCr | 0:1ed39951ab7b | 162 | State = RX_ERROR; |
GregCr | 7:c1bbd6c56979 | 163 | debug_if( DEBUG_MESSAGE, "> OnRxError\n\r" ); |
GregCr | 0:1ed39951ab7b | 164 | } |
GregCr | 3:8b9e2a4df4b5 | 165 |