Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of SX1272PingPong by
main.cpp@15:a152246cf3be, 2017-07-21 (annotated)
- Committer:
- franc_unina
- Date:
- Fri Jul 21 17:46:16 2017 +0000
- Revision:
- 15:a152246cf3be
- Parent:
- 14:ab6eecd44b7a
Ricezione
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 | |
| franc_unina | 15:a152246cf3be | 6 | |
| GregCr | 0:1ed39951ab7b | 7 | /* Set this flag to '1' to display debug messages on the console */ |
| GregCr | 13:edb9b443c1dd | 8 | #define DEBUG_MESSAGE 1 |
| GregCr | 0:1ed39951ab7b | 9 | |
| GregCr | 0:1ed39951ab7b | 10 | /* Set this flag to '1' to use the LoRa modulation or to '0' to use FSK modulation */ |
| GregCr | 5:f2431c4fe3bb | 11 | #define USE_MODEM_LORA 1 |
| GregCr | 0:1ed39951ab7b | 12 | #define USE_MODEM_FSK !USE_MODEM_LORA |
| GregCr | 0:1ed39951ab7b | 13 | |
| GregCr | 5:f2431c4fe3bb | 14 | #define RF_FREQUENCY 868000000 // Hz |
| GregCr | 0:1ed39951ab7b | 15 | #define TX_OUTPUT_POWER 14 // 14 dBm |
| GregCr | 0:1ed39951ab7b | 16 | |
| GregCr | 0:1ed39951ab7b | 17 | #if USE_MODEM_LORA == 1 |
| GregCr | 0:1ed39951ab7b | 18 | |
| GregCr | 1:126d70d374f6 | 19 | #define LORA_BANDWIDTH 2 // [0: 125 kHz, |
| GregCr | 0:1ed39951ab7b | 20 | // 1: 250 kHz, |
| GregCr | 0:1ed39951ab7b | 21 | // 2: 500 kHz, |
| GregCr | 0:1ed39951ab7b | 22 | // 3: Reserved] |
| GregCr | 0:1ed39951ab7b | 23 | #define LORA_SPREADING_FACTOR 7 // [SF7..SF12] |
| GregCr | 0:1ed39951ab7b | 24 | #define LORA_CODINGRATE 1 // [1: 4/5, |
| GregCr | 0:1ed39951ab7b | 25 | // 2: 4/6, |
| GregCr | 0:1ed39951ab7b | 26 | // 3: 4/7, |
| GregCr | 0:1ed39951ab7b | 27 | // 4: 4/8] |
| GregCr | 0:1ed39951ab7b | 28 | #define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx |
| GregCr | 0:1ed39951ab7b | 29 | #define LORA_SYMBOL_TIMEOUT 5 // Symbols |
| GregCr | 0:1ed39951ab7b | 30 | #define LORA_FIX_LENGTH_PAYLOAD_ON false |
| GregCr | 3:8b9e2a4df4b5 | 31 | #define LORA_FHSS_ENABLED false |
| GregCr | 3:8b9e2a4df4b5 | 32 | #define LORA_NB_SYMB_HOP 4 |
| GregCr | 0:1ed39951ab7b | 33 | #define LORA_IQ_INVERSION_ON false |
| GregCr | 3:8b9e2a4df4b5 | 34 | #define LORA_CRC_ENABLED true |
| mluis | 14:ab6eecd44b7a | 35 | |
| GregCr | 0:1ed39951ab7b | 36 | #elif USE_MODEM_FSK == 1 |
| GregCr | 0:1ed39951ab7b | 37 | |
| GregCr | 2:59e108728d71 | 38 | #define FSK_FDEV 25000 // Hz |
| GregCr | 2:59e108728d71 | 39 | #define FSK_DATARATE 19200 // bps |
| GregCr | 2:59e108728d71 | 40 | #define FSK_BANDWIDTH 50000 // Hz |
| GregCr | 2:59e108728d71 | 41 | #define FSK_AFC_BANDWIDTH 83333 // Hz |
| GregCr | 0:1ed39951ab7b | 42 | #define FSK_PREAMBLE_LENGTH 5 // Same for Tx and Rx |
| GregCr | 0:1ed39951ab7b | 43 | #define FSK_FIX_LENGTH_PAYLOAD_ON false |
| GregCr | 3:8b9e2a4df4b5 | 44 | #define FSK_CRC_ENABLED true |
| mluis | 14:ab6eecd44b7a | 45 | |
| GregCr | 0:1ed39951ab7b | 46 | #else |
| GregCr | 0:1ed39951ab7b | 47 | #error "Please define a modem in the compiler options." |
| GregCr | 0:1ed39951ab7b | 48 | #endif |
| GregCr | 0:1ed39951ab7b | 49 | |
| mluis | 14:ab6eecd44b7a | 50 | #define RX_TIMEOUT_VALUE 3500 // in ms |
| GregCr | 0:1ed39951ab7b | 51 | #define BUFFER_SIZE 32 // Define the payload size here |
| GregCr | 0:1ed39951ab7b | 52 | |
| GregCr | 8:f956dee63a56 | 53 | #if( defined ( TARGET_KL25Z ) || defined ( TARGET_LPC11U6X ) ) |
| mluis | 14:ab6eecd44b7a | 54 | DigitalOut led( LED2 ); |
| GregCr | 3:8b9e2a4df4b5 | 55 | #else |
| mluis | 14:ab6eecd44b7a | 56 | DigitalOut led( LED1 ); |
| GregCr | 3:8b9e2a4df4b5 | 57 | #endif |
| GregCr | 3:8b9e2a4df4b5 | 58 | |
| franc_unina | 15:a152246cf3be | 59 | Timer timer; |
| franc_unina | 15:a152246cf3be | 60 | |
| franc_unina | 15:a152246cf3be | 61 | |
| GregCr | 0:1ed39951ab7b | 62 | /* |
| GregCr | 0:1ed39951ab7b | 63 | * Global variables declarations |
| GregCr | 0:1ed39951ab7b | 64 | */ |
| mluis | 10:7af820d1e1df | 65 | typedef enum |
| mluis | 10:7af820d1e1df | 66 | { |
| mluis | 10:7af820d1e1df | 67 | LOWPOWER = 0, |
| mluis | 10:7af820d1e1df | 68 | IDLE, |
| mluis | 14:ab6eecd44b7a | 69 | |
| mluis | 10:7af820d1e1df | 70 | RX, |
| mluis | 10:7af820d1e1df | 71 | RX_TIMEOUT, |
| mluis | 10:7af820d1e1df | 72 | RX_ERROR, |
| mluis | 14:ab6eecd44b7a | 73 | |
| mluis | 10:7af820d1e1df | 74 | TX, |
| mluis | 10:7af820d1e1df | 75 | TX_TIMEOUT, |
| mluis | 14:ab6eecd44b7a | 76 | |
| mluis | 10:7af820d1e1df | 77 | CAD, |
| mluis | 10:7af820d1e1df | 78 | CAD_DONE |
| mluis | 10:7af820d1e1df | 79 | }AppStates_t; |
| GregCr | 0:1ed39951ab7b | 80 | |
| mluis | 10:7af820d1e1df | 81 | volatile AppStates_t State = LOWPOWER; |
| mluis | 10:7af820d1e1df | 82 | |
| mluis | 10:7af820d1e1df | 83 | /*! |
| mluis | 10:7af820d1e1df | 84 | * Radio events function pointer |
| mluis | 10:7af820d1e1df | 85 | */ |
| mluis | 10:7af820d1e1df | 86 | static RadioEvents_t RadioEvents; |
| mluis | 10:7af820d1e1df | 87 | |
| mluis | 10:7af820d1e1df | 88 | /* |
| mluis | 10:7af820d1e1df | 89 | * Global variables declarations |
| mluis | 10:7af820d1e1df | 90 | */ |
| GregCr | 13:edb9b443c1dd | 91 | SX1272MB2xAS Radio( NULL ); |
| GregCr | 0:1ed39951ab7b | 92 | |
| franc_unina | 15:a152246cf3be | 93 | const uint8_t PingMsg[] = "LOR3"; |
| franc_unina | 15:a152246cf3be | 94 | const uint8_t PongMs2[] = "LOR1"; |
| franc_unina | 15:a152246cf3be | 95 | const uint8_t PongMs3[] = "LOR2"; |
| GregCr | 0:1ed39951ab7b | 96 | |
| GregCr | 0:1ed39951ab7b | 97 | uint16_t BufferSize = BUFFER_SIZE; |
| GregCr | 0:1ed39951ab7b | 98 | uint8_t Buffer[BUFFER_SIZE]; |
| GregCr | 0:1ed39951ab7b | 99 | |
| GregCr | 5:f2431c4fe3bb | 100 | int16_t RssiValue = 0.0; |
| GregCr | 5:f2431c4fe3bb | 101 | int8_t SnrValue = 0.0; |
| GregCr | 0:1ed39951ab7b | 102 | |
| franc_unina | 15:a152246cf3be | 103 | int32_t TimerValue=0; |
| franc_unina | 15:a152246cf3be | 104 | int32_t TimeStampNew=0; |
| franc_unina | 15:a152246cf3be | 105 | int32_t TimeStampOld=0; |
| franc_unina | 15:a152246cf3be | 106 | |
| mluis | 14:ab6eecd44b7a | 107 | int main( void ) |
| GregCr | 0:1ed39951ab7b | 108 | { |
| GregCr | 0:1ed39951ab7b | 109 | uint8_t i; |
| franc_unina | 15:a152246cf3be | 110 | |
| mluis | 14:ab6eecd44b7a | 111 | |
| GregCr | 13:edb9b443c1dd | 112 | debug( "\n\n\r SX1272 Ping Pong Demo Application \n\n\r" ); |
| mluis | 10:7af820d1e1df | 113 | |
| mluis | 10:7af820d1e1df | 114 | // Initialize Radio driver |
| mluis | 10:7af820d1e1df | 115 | RadioEvents.TxDone = OnTxDone; |
| mluis | 10:7af820d1e1df | 116 | RadioEvents.RxDone = OnRxDone; |
| mluis | 10:7af820d1e1df | 117 | RadioEvents.RxError = OnRxError; |
| mluis | 10:7af820d1e1df | 118 | RadioEvents.TxTimeout = OnTxTimeout; |
| mluis | 10:7af820d1e1df | 119 | RadioEvents.RxTimeout = OnRxTimeout; |
| mluis | 10:7af820d1e1df | 120 | Radio.Init( &RadioEvents ); |
| mluis | 14:ab6eecd44b7a | 121 | |
| GregCr | 7:c1bbd6c56979 | 122 | // verify the connection with the board |
| GregCr | 7:c1bbd6c56979 | 123 | while( Radio.Read( REG_VERSION ) == 0x00 ) |
| GregCr | 2:59e108728d71 | 124 | { |
| GregCr | 7:c1bbd6c56979 | 125 | debug( "Radio could not be detected!\n\r", NULL ); |
| GregCr | 7:c1bbd6c56979 | 126 | wait( 1 ); |
| GregCr | 2:59e108728d71 | 127 | } |
| mluis | 14:ab6eecd44b7a | 128 | |
| mluis | 14:ab6eecd44b7a | 129 | debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1272MB2XAS ) ), "\n\r > Board Type: SX1272MB2xAS < \n\r" ); |
| mluis | 14:ab6eecd44b7a | 130 | |
| GregCr | 0:1ed39951ab7b | 131 | Radio.SetChannel( RF_FREQUENCY ); |
| GregCr | 0:1ed39951ab7b | 132 | |
| GregCr | 0:1ed39951ab7b | 133 | #if USE_MODEM_LORA == 1 |
| mluis | 14:ab6eecd44b7a | 134 | |
| mluis | 14:ab6eecd44b7a | 135 | debug_if( LORA_FHSS_ENABLED, "\n\n\r > LORA FHSS Mode < \n\n\r" ); |
| mluis | 14:ab6eecd44b7a | 136 | debug_if( !LORA_FHSS_ENABLED, "\n\n\r > LORA Mode < \n\n\r" ); |
| GregCr | 7:c1bbd6c56979 | 137 | |
| GregCr | 0:1ed39951ab7b | 138 | Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH, |
| GregCr | 0:1ed39951ab7b | 139 | LORA_SPREADING_FACTOR, LORA_CODINGRATE, |
| GregCr | 0:1ed39951ab7b | 140 | LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON, |
| mluis | 14:ab6eecd44b7a | 141 | LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, |
| mluis | 14:ab6eecd44b7a | 142 | LORA_IQ_INVERSION_ON, 2000 ); |
| mluis | 14:ab6eecd44b7a | 143 | |
| GregCr | 0:1ed39951ab7b | 144 | Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR, |
| GregCr | 0:1ed39951ab7b | 145 | LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH, |
| mluis | 9:e764990e45df | 146 | LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0, |
| mluis | 14:ab6eecd44b7a | 147 | LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, |
| GregCr | 3:8b9e2a4df4b5 | 148 | LORA_IQ_INVERSION_ON, true ); |
| mluis | 14:ab6eecd44b7a | 149 | |
| GregCr | 0:1ed39951ab7b | 150 | #elif USE_MODEM_FSK == 1 |
| GregCr | 0:1ed39951ab7b | 151 | |
| mluis | 14:ab6eecd44b7a | 152 | debug("\n\n\r > FSK Mode < \n\n\r" ); |
| GregCr | 0:1ed39951ab7b | 153 | Radio.SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0, |
| GregCr | 0:1ed39951ab7b | 154 | FSK_DATARATE, 0, |
| GregCr | 0:1ed39951ab7b | 155 | FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON, |
| mluis | 14:ab6eecd44b7a | 156 | FSK_CRC_ENABLED, 0, 0, 0, 2000 ); |
| mluis | 14:ab6eecd44b7a | 157 | |
| GregCr | 0:1ed39951ab7b | 158 | Radio.SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE, |
| GregCr | 0:1ed39951ab7b | 159 | 0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH, |
| mluis | 9:e764990e45df | 160 | 0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, FSK_CRC_ENABLED, |
| GregCr | 3:8b9e2a4df4b5 | 161 | 0, 0, false, true ); |
| mluis | 14:ab6eecd44b7a | 162 | |
| GregCr | 0:1ed39951ab7b | 163 | #else |
| GregCr | 0:1ed39951ab7b | 164 | |
| GregCr | 0:1ed39951ab7b | 165 | #error "Please define a modem in the compiler options." |
| GregCr | 0:1ed39951ab7b | 166 | |
| GregCr | 0:1ed39951ab7b | 167 | #endif |
| mluis | 14:ab6eecd44b7a | 168 | |
| mluis | 14:ab6eecd44b7a | 169 | debug_if( DEBUG_MESSAGE, "Starting Ping-Pong loop\r\n" ); |
| mluis | 14:ab6eecd44b7a | 170 | |
| GregCr | 3:8b9e2a4df4b5 | 171 | led = 0; |
| mluis | 14:ab6eecd44b7a | 172 | |
| GregCr | 0:1ed39951ab7b | 173 | Radio.Rx( RX_TIMEOUT_VALUE ); |
| franc_unina | 15:a152246cf3be | 174 | |
| franc_unina | 15:a152246cf3be | 175 | timer.start(); |
| mluis | 14:ab6eecd44b7a | 176 | |
| GregCr | 0:1ed39951ab7b | 177 | while( 1 ) |
| GregCr | 0:1ed39951ab7b | 178 | { |
| franc_unina | 15:a152246cf3be | 179 | switch(State){ |
| franc_unina | 15:a152246cf3be | 180 | case RX: |
| franc_unina | 15:a152246cf3be | 181 | //timer.stop(); |
| franc_unina | 15:a152246cf3be | 182 | |
| franc_unina | 15:a152246cf3be | 183 | //timer.reset(); |
| franc_unina | 15:a152246cf3be | 184 | //timer.start(); |
| franc_unina | 15:a152246cf3be | 185 | if( strncmp( ( const char* )Buffer, ( const char* )PongMs2, 4 ) == 0 ) |
| GregCr | 0:1ed39951ab7b | 186 | { |
| franc_unina | 15:a152246cf3be | 187 | TimeStampNew=timer.read_ms(); |
| franc_unina | 15:a152246cf3be | 188 | TimerValue=TimeStampNew-TimeStampOld; |
| franc_unina | 15:a152246cf3be | 189 | TimeStampOld=TimeStampNew; |
| franc_unina | 15:a152246cf3be | 190 | |
| franc_unina | 15:a152246cf3be | 191 | led = !led; |
| franc_unina | 15:a152246cf3be | 192 | debug( "I am LOR2\r\n" ); |
| franc_unina | 15:a152246cf3be | 193 | printf("Data received from...%s\r\n",Buffer); |
| franc_unina | 15:a152246cf3be | 194 | printf("TimeStamp %d\n\r",TimeStampNew); |
| franc_unina | 15:a152246cf3be | 195 | printf("Time %d\n\r",TimerValue); |
| franc_unina | 15:a152246cf3be | 196 | Radio.Sleep( ); |
| franc_unina | 15:a152246cf3be | 197 | Buffer[0] = 'A'; |
| franc_unina | 15:a152246cf3be | 198 | }//end if1 |
| franc_unina | 15:a152246cf3be | 199 | wait_ms(10); |
| franc_unina | 15:a152246cf3be | 200 | Radio.Rx( RX_TIMEOUT_VALUE );//SETTA LA RADIO IN MODALITA' RICEZIONE |
| franc_unina | 15:a152246cf3be | 201 | |
| franc_unina | 15:a152246cf3be | 202 | break; |
| franc_unina | 15:a152246cf3be | 203 | |
| franc_unina | 15:a152246cf3be | 204 | default: |
| franc_unina | 15:a152246cf3be | 205 | break; |
| franc_unina | 15:a152246cf3be | 206 | }; |
| franc_unina | 15:a152246cf3be | 207 | }//end while |
| franc_unina | 15:a152246cf3be | 208 | }//end main |
| GregCr | 0:1ed39951ab7b | 209 | |
| GregCr | 0:1ed39951ab7b | 210 | void OnTxDone( void ) |
| GregCr | 0:1ed39951ab7b | 211 | { |
| GregCr | 5:f2431c4fe3bb | 212 | Radio.Sleep( ); |
| GregCr | 0:1ed39951ab7b | 213 | State = TX; |
| GregCr | 7:c1bbd6c56979 | 214 | debug_if( DEBUG_MESSAGE, "> OnTxDone\n\r" ); |
| GregCr | 0:1ed39951ab7b | 215 | } |
| GregCr | 0:1ed39951ab7b | 216 | |
| mluis | 14:ab6eecd44b7a | 217 | void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ) |
| GregCr | 0:1ed39951ab7b | 218 | { |
| GregCr | 0:1ed39951ab7b | 219 | Radio.Sleep( ); |
| GregCr | 0:1ed39951ab7b | 220 | BufferSize = size; |
| GregCr | 0:1ed39951ab7b | 221 | memcpy( Buffer, payload, BufferSize ); |
| GregCr | 0:1ed39951ab7b | 222 | RssiValue = rssi; |
| GregCr | 0:1ed39951ab7b | 223 | SnrValue = snr; |
| GregCr | 0:1ed39951ab7b | 224 | State = RX; |
| GregCr | 7:c1bbd6c56979 | 225 | debug_if( DEBUG_MESSAGE, "> OnRxDone\n\r" ); |
| GregCr | 0:1ed39951ab7b | 226 | } |
| GregCr | 0:1ed39951ab7b | 227 | |
| GregCr | 0:1ed39951ab7b | 228 | void OnTxTimeout( void ) |
| GregCr | 0:1ed39951ab7b | 229 | { |
| GregCr | 0:1ed39951ab7b | 230 | Radio.Sleep( ); |
| GregCr | 0:1ed39951ab7b | 231 | State = TX_TIMEOUT; |
| GregCr | 7:c1bbd6c56979 | 232 | debug_if( DEBUG_MESSAGE, "> OnTxTimeout\n\r" ); |
| GregCr | 0:1ed39951ab7b | 233 | } |
| GregCr | 0:1ed39951ab7b | 234 | |
| GregCr | 0:1ed39951ab7b | 235 | void OnRxTimeout( void ) |
| GregCr | 0:1ed39951ab7b | 236 | { |
| GregCr | 0:1ed39951ab7b | 237 | Radio.Sleep( ); |
| mluis | 14:ab6eecd44b7a | 238 | Buffer[BufferSize] = 0; |
| GregCr | 0:1ed39951ab7b | 239 | State = RX_TIMEOUT; |
| GregCr | 7:c1bbd6c56979 | 240 | debug_if( DEBUG_MESSAGE, "> OnRxTimeout\n\r" ); |
| GregCr | 0:1ed39951ab7b | 241 | } |
| GregCr | 0:1ed39951ab7b | 242 | |
| GregCr | 0:1ed39951ab7b | 243 | void OnRxError( void ) |
| GregCr | 0:1ed39951ab7b | 244 | { |
| GregCr | 0:1ed39951ab7b | 245 | Radio.Sleep( ); |
| GregCr | 0:1ed39951ab7b | 246 | State = RX_ERROR; |
| GregCr | 7:c1bbd6c56979 | 247 | debug_if( DEBUG_MESSAGE, "> OnRxError\n\r" ); |
| GregCr | 0:1ed39951ab7b | 248 | } |
