projet-lievres / Mbed 2 deprecated lora-project

Dependencies:   SX1272Lib mbed

Committer:
aGoelzer
Date:
Mon Oct 10 13:16:16 2016 +0000
Revision:
4:954ae88b6664
Parent:
1:bddab1c9c632
Child:
5:d28a689b1635
Code pour recevoir et transmettre des messages en Lora

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bLandais 0:b239f8883fcc 1 #include "mbed.h"
aGoelzer 4:954ae88b6664 2 #include "main.h"
aGoelzer 4:954ae88b6664 3 #include "sx1272-hal.h"
aGoelzer 4:954ae88b6664 4 #include "debug.h"
aGoelzer 4:954ae88b6664 5
aGoelzer 4:954ae88b6664 6 /* Set this flag to '1' to display debug messages on the console */
aGoelzer 4:954ae88b6664 7 #define DEBUG_MESSAGE 1
aGoelzer 4:954ae88b6664 8
aGoelzer 4:954ae88b6664 9 /* Set this flag to '1' to use the LoRa modulation or to '0' to use FSK modulation */
aGoelzer 4:954ae88b6664 10 #define USE_MODEM_LORA 1
aGoelzer 4:954ae88b6664 11 #define USE_MODEM_FSK !USE_MODEM_LORA
aGoelzer 4:954ae88b6664 12
aGoelzer 4:954ae88b6664 13 #define RF_FREQUENCY 868000000 // Hz
aGoelzer 4:954ae88b6664 14 #define TX_OUTPUT_POWER 14 // 14 dBm
aGoelzer 4:954ae88b6664 15
aGoelzer 4:954ae88b6664 16 #if USE_MODEM_LORA == 1
aGoelzer 4:954ae88b6664 17
aGoelzer 4:954ae88b6664 18 #define LORA_BANDWIDTH 2 // [0: 125 kHz,
aGoelzer 4:954ae88b6664 19 // 1: 250 kHz,
aGoelzer 4:954ae88b6664 20 // 2: 500 kHz,
aGoelzer 4:954ae88b6664 21 // 3: Reserved]
aGoelzer 4:954ae88b6664 22 #define LORA_SPREADING_FACTOR 7 // [SF7..SF12]
aGoelzer 4:954ae88b6664 23 #define LORA_CODINGRATE 1 // [1: 4/5,
aGoelzer 4:954ae88b6664 24 // 2: 4/6,
aGoelzer 4:954ae88b6664 25 // 3: 4/7,
aGoelzer 4:954ae88b6664 26 // 4: 4/8]
aGoelzer 4:954ae88b6664 27 #define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx
aGoelzer 4:954ae88b6664 28 #define LORA_SYMBOL_TIMEOUT 5 // Symbols
aGoelzer 4:954ae88b6664 29 #define LORA_FIX_LENGTH_PAYLOAD_ON false
aGoelzer 4:954ae88b6664 30 #define LORA_FHSS_ENABLED false
aGoelzer 4:954ae88b6664 31 #define LORA_NB_SYMB_HOP 4
aGoelzer 4:954ae88b6664 32 #define LORA_IQ_INVERSION_ON false
aGoelzer 4:954ae88b6664 33 #define LORA_CRC_ENABLED true
aGoelzer 4:954ae88b6664 34
aGoelzer 4:954ae88b6664 35 #elif USE_MODEM_FSK == 1
aGoelzer 4:954ae88b6664 36
aGoelzer 4:954ae88b6664 37 #define FSK_FDEV 25000 // Hz
aGoelzer 4:954ae88b6664 38 #define FSK_DATARATE 19200 // bps
aGoelzer 4:954ae88b6664 39 #define FSK_BANDWIDTH 50000 // Hz
aGoelzer 4:954ae88b6664 40 #define FSK_AFC_BANDWIDTH 83333 // Hz
aGoelzer 4:954ae88b6664 41 #define FSK_PREAMBLE_LENGTH 5 // Same for Tx and Rx
aGoelzer 4:954ae88b6664 42 #define FSK_FIX_LENGTH_PAYLOAD_ON false
aGoelzer 4:954ae88b6664 43 #define FSK_CRC_ENABLED true
aGoelzer 4:954ae88b6664 44
aGoelzer 4:954ae88b6664 45 #else
aGoelzer 4:954ae88b6664 46 #error "Please define a modem in the compiler options."
aGoelzer 4:954ae88b6664 47 #endif
aGoelzer 4:954ae88b6664 48
aGoelzer 4:954ae88b6664 49 #define RX_TIMEOUT_VALUE 3500000 // in us
aGoelzer 4:954ae88b6664 50 #define BUFFER_SIZE 32 // Define the payload size here
bLandais 0:b239f8883fcc 51
aGoelzer 4:954ae88b6664 52 #if( defined ( TARGET_KL25Z ) || defined ( TARGET_LPC11U6X ) )
aGoelzer 4:954ae88b6664 53 DigitalOut led(LED2);
aGoelzer 4:954ae88b6664 54 #else
aGoelzer 4:954ae88b6664 55 DigitalOut led(LED1);
aGoelzer 4:954ae88b6664 56 #endif
aGoelzer 4:954ae88b6664 57
aGoelzer 4:954ae88b6664 58 /*
aGoelzer 4:954ae88b6664 59 * Global variables declarations
aGoelzer 4:954ae88b6664 60 */
aGoelzer 4:954ae88b6664 61 typedef enum {
aGoelzer 4:954ae88b6664 62 LOWPOWER = 0,
aGoelzer 4:954ae88b6664 63 IDLE,
aGoelzer 4:954ae88b6664 64
aGoelzer 4:954ae88b6664 65 RX,
aGoelzer 4:954ae88b6664 66 RX_TIMEOUT,
aGoelzer 4:954ae88b6664 67 RX_ERROR,
aGoelzer 4:954ae88b6664 68
aGoelzer 4:954ae88b6664 69 TX,
aGoelzer 4:954ae88b6664 70 TX_TIMEOUT,
aGoelzer 4:954ae88b6664 71
aGoelzer 4:954ae88b6664 72 CAD,
aGoelzer 4:954ae88b6664 73 CAD_DONE
aGoelzer 4:954ae88b6664 74 } AppStates_t;
aGoelzer 4:954ae88b6664 75
aGoelzer 4:954ae88b6664 76 volatile AppStates_t State = LOWPOWER;
aGoelzer 4:954ae88b6664 77
aGoelzer 4:954ae88b6664 78 /*!
aGoelzer 4:954ae88b6664 79 * Radio events function pointer
aGoelzer 4:954ae88b6664 80 */
aGoelzer 4:954ae88b6664 81 static RadioEvents_t RadioEvents;
aGoelzer 4:954ae88b6664 82
aGoelzer 4:954ae88b6664 83 /*
aGoelzer 4:954ae88b6664 84 * Global variables declarations
aGoelzer 4:954ae88b6664 85 */
aGoelzer 4:954ae88b6664 86 SX1272MB2xAS Radio( NULL );
aGoelzer 4:954ae88b6664 87
aGoelzer 4:954ae88b6664 88 uint16_t BufferSize = BUFFER_SIZE;
aGoelzer 4:954ae88b6664 89 uint8_t Buffer[BUFFER_SIZE];
aGoelzer 4:954ae88b6664 90
aGoelzer 4:954ae88b6664 91 int16_t RssiValue = 0.0;
aGoelzer 4:954ae88b6664 92 int8_t SnrValue = 0.0;
aGoelzer 4:954ae88b6664 93
aGoelzer 4:954ae88b6664 94 int main()
aGoelzer 4:954ae88b6664 95 {
aGoelzer 4:954ae88b6664 96 // Initialize Radio driver
aGoelzer 4:954ae88b6664 97 RadioEvents.TxDone = OnTxDone;
aGoelzer 4:954ae88b6664 98 RadioEvents.RxDone = OnRxDone;
aGoelzer 4:954ae88b6664 99 RadioEvents.RxError = OnRxError;
aGoelzer 4:954ae88b6664 100 RadioEvents.TxTimeout = OnTxTimeout;
aGoelzer 4:954ae88b6664 101 RadioEvents.RxTimeout = OnRxTimeout;
aGoelzer 4:954ae88b6664 102 Radio.Init( &RadioEvents );
bLandais 0:b239f8883fcc 103
aGoelzer 4:954ae88b6664 104 // verify the connection with the board
aGoelzer 4:954ae88b6664 105 while( Radio.Read( REG_VERSION ) == 0x00 ) {
aGoelzer 4:954ae88b6664 106 debug( "Radio could not be detected!\n\r", NULL );
aGoelzer 4:954ae88b6664 107 wait( 1 );
aGoelzer 4:954ae88b6664 108 }
aGoelzer 4:954ae88b6664 109
aGoelzer 4:954ae88b6664 110 debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1272MB2XAS ) ) , "\n\r > Board Type: SX1272MB2xAS < \n\r" );
aGoelzer 4:954ae88b6664 111
aGoelzer 4:954ae88b6664 112 Radio.SetChannel( RF_FREQUENCY );
aGoelzer 4:954ae88b6664 113
aGoelzer 4:954ae88b6664 114 #if USE_MODEM_LORA == 1
aGoelzer 4:954ae88b6664 115
aGoelzer 4:954ae88b6664 116 debug_if( LORA_FHSS_ENABLED, "\n\n\r > LORA FHSS Mode < \n\n\r");
aGoelzer 4:954ae88b6664 117 debug_if( !LORA_FHSS_ENABLED, "\n\n\r > LORA Mode < \n\n\r");
aGoelzer 4:954ae88b6664 118
aGoelzer 4:954ae88b6664 119 Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
aGoelzer 4:954ae88b6664 120 LORA_SPREADING_FACTOR, LORA_CODINGRATE,
aGoelzer 4:954ae88b6664 121 LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
aGoelzer 4:954ae88b6664 122 LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
aGoelzer 4:954ae88b6664 123 LORA_IQ_INVERSION_ON, 2000000 );
aGoelzer 4:954ae88b6664 124
aGoelzer 4:954ae88b6664 125 Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
aGoelzer 4:954ae88b6664 126 LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
aGoelzer 4:954ae88b6664 127 LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0,
aGoelzer 4:954ae88b6664 128 LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
aGoelzer 4:954ae88b6664 129 LORA_IQ_INVERSION_ON, true );
aGoelzer 4:954ae88b6664 130
aGoelzer 4:954ae88b6664 131 #elif USE_MODEM_FSK == 1
aGoelzer 4:954ae88b6664 132
aGoelzer 4:954ae88b6664 133 debug("\n\n\r > FSK Mode < \n\n\r");
aGoelzer 4:954ae88b6664 134 Radio.SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0,
aGoelzer 4:954ae88b6664 135 FSK_DATARATE, 0,
aGoelzer 4:954ae88b6664 136 FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON,
aGoelzer 4:954ae88b6664 137 FSK_CRC_ENABLED, 0, 0, 0, 2000000 );
aGoelzer 4:954ae88b6664 138
aGoelzer 4:954ae88b6664 139 Radio.SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE,
aGoelzer 4:954ae88b6664 140 0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH,
aGoelzer 4:954ae88b6664 141 0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, FSK_CRC_ENABLED,
aGoelzer 4:954ae88b6664 142 0, 0, false, true );
aGoelzer 4:954ae88b6664 143
aGoelzer 4:954ae88b6664 144 #else
aGoelzer 4:954ae88b6664 145
aGoelzer 4:954ae88b6664 146 #error "Please define a modem in the compiler options."
aGoelzer 4:954ae88b6664 147
aGoelzer 4:954ae88b6664 148 #endif
aGoelzer 4:954ae88b6664 149
aGoelzer 4:954ae88b6664 150 Radio.Rx( 10*RX_TIMEOUT_VALUE );
aGoelzer 4:954ae88b6664 151
aGoelzer 4:954ae88b6664 152 while( 1 ) {
aGoelzer 4:954ae88b6664 153 }
aGoelzer 4:954ae88b6664 154
aGoelzer 4:954ae88b6664 155 }
aGoelzer 4:954ae88b6664 156
aGoelzer 4:954ae88b6664 157 void SendStr(char* str)
aGoelzer 4:954ae88b6664 158 {
aGoelzer 4:954ae88b6664 159 int len = strlen(str);
aGoelzer 4:954ae88b6664 160 uint8_t MyBuffer[len];
aGoelzer 4:954ae88b6664 161 strcpy( ( char* )MyBuffer, str );
aGoelzer 4:954ae88b6664 162 debug(str);
aGoelzer 4:954ae88b6664 163 Radio.Send(MyBuffer, len);
aGoelzer 4:954ae88b6664 164 }
aGoelzer 4:954ae88b6664 165
aGoelzer 4:954ae88b6664 166 void OnTxDone( void )
aGoelzer 4:954ae88b6664 167 {
aGoelzer 4:954ae88b6664 168 Radio.Sleep();
aGoelzer 4:954ae88b6664 169 State = TX;
aGoelzer 4:954ae88b6664 170 debug_if( DEBUG_MESSAGE, "\n\r> Message transmis\n\r" );
aGoelzer 4:954ae88b6664 171 }
aGoelzer 4:954ae88b6664 172
aGoelzer 4:954ae88b6664 173 void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr)
aGoelzer 4:954ae88b6664 174 {
aGoelzer 4:954ae88b6664 175 Radio.Sleep();
aGoelzer 4:954ae88b6664 176 BufferSize = size;
aGoelzer 4:954ae88b6664 177 memcpy( Buffer, payload, BufferSize );
aGoelzer 4:954ae88b6664 178 RssiValue = rssi;
aGoelzer 4:954ae88b6664 179 SnrValue = snr;
aGoelzer 4:954ae88b6664 180 State = RX;
aGoelzer 4:954ae88b6664 181 int i = 0;
aGoelzer 4:954ae88b6664 182 for(i = 0; i<BufferSize ; i++) {
aGoelzer 4:954ae88b6664 183 debug("%c", Buffer[i]);
aGoelzer 4:954ae88b6664 184 }
aGoelzer 4:954ae88b6664 185 debug_if( DEBUG_MESSAGE, "\n\r> Message recu\n\r" );
aGoelzer 4:954ae88b6664 186
aGoelzer 4:954ae88b6664 187 Radio.Rx( 10*RX_TIMEOUT_VALUE );
aGoelzer 4:954ae88b6664 188 }
aGoelzer 4:954ae88b6664 189
aGoelzer 4:954ae88b6664 190 void OnTxTimeout( void )
aGoelzer 4:954ae88b6664 191 {
aGoelzer 4:954ae88b6664 192 Radio.Sleep( );
aGoelzer 4:954ae88b6664 193 State = TX_TIMEOUT;
aGoelzer 4:954ae88b6664 194 debug_if( DEBUG_MESSAGE, "\n\r> Delai d'attente depasse\n\r" );
aGoelzer 4:954ae88b6664 195 }
aGoelzer 4:954ae88b6664 196
aGoelzer 4:954ae88b6664 197 void OnRxTimeout( void )
aGoelzer 4:954ae88b6664 198 {
aGoelzer 4:954ae88b6664 199 Radio.Sleep( );
aGoelzer 4:954ae88b6664 200 Buffer[ BufferSize ] = 0;
aGoelzer 4:954ae88b6664 201 State = RX_TIMEOUT;
aGoelzer 4:954ae88b6664 202 debug_if( DEBUG_MESSAGE, "> OnRxTimeout\n\r" );
aGoelzer 4:954ae88b6664 203 }
aGoelzer 4:954ae88b6664 204
aGoelzer 4:954ae88b6664 205 void OnRxError( void )
aGoelzer 4:954ae88b6664 206 {
aGoelzer 4:954ae88b6664 207 Radio.Sleep( );
aGoelzer 4:954ae88b6664 208 State = RX_ERROR;
aGoelzer 4:954ae88b6664 209 debug_if( DEBUG_MESSAGE, "> OnRxError\n\r" );
aGoelzer 4:954ae88b6664 210 }
aGoelzer 4:954ae88b6664 211