Thomas MAZIER
/
Read_mes_slave_2
a
main.cpp@15:a99503d744c3, 2019-01-18 (annotated)
- Committer:
- ThomasMazier
- Date:
- Fri Jan 18 10:26:20 2019 +0000
- Revision:
- 15:a99503d744c3
- Parent:
- 14:ab6eecd44b7a
a
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ThomasMazier | 15:a99503d744c3 | 1 | ///////////////////////////////////////////////// BIBLIOTHEQUE /////////////////////////////////////////////////////////////////////////////////////// |
ThomasMazier | 15:a99503d744c3 | 2 | |
GregCr | 0:1ed39951ab7b | 3 | #include "mbed.h" |
GregCr | 4:5ece30264cd9 | 4 | #include "main.h" |
GregCr | 13:edb9b443c1dd | 5 | #include "sx1272-hal.h" |
GregCr | 8:f956dee63a56 | 6 | #include "debug.h" |
ThomasMazier | 15:a99503d744c3 | 7 | #include "sx1272.h" |
ThomasMazier | 15:a99503d744c3 | 8 | |
ThomasMazier | 15:a99503d744c3 | 9 | |
ThomasMazier | 15:a99503d744c3 | 10 | Serial pc(USBTX, USBRX); |
ThomasMazier | 15:a99503d744c3 | 11 | |
ThomasMazier | 15:a99503d744c3 | 12 | #define PCBAUD 9600 |
ThomasMazier | 15:a99503d744c3 | 13 | |
ThomasMazier | 15:a99503d744c3 | 14 | //////////////////////////////////////////////// DEFINITION OUTIL DEBUG /////////////////////////////////////////////////////////////////////////// |
GregCr | 0:1ed39951ab7b | 15 | |
GregCr | 0:1ed39951ab7b | 16 | /* Set this flag to '1' to display debug messages on the console */ |
ThomasMazier | 15:a99503d744c3 | 17 | |
ThomasMazier | 15:a99503d744c3 | 18 | |
ThomasMazier | 15:a99503d744c3 | 19 | #define DEBUG_MESSAGE 1 //permet d'afficher des messages sur le terminal |
ThomasMazier | 15:a99503d744c3 | 20 | |
ThomasMazier | 15:a99503d744c3 | 21 | |
ThomasMazier | 15:a99503d744c3 | 22 | ////////////////////////////////////////////////////////////////////// DEFINITION MODULATION ///////////////////////////////////////////////////// |
GregCr | 0:1ed39951ab7b | 23 | |
GregCr | 0:1ed39951ab7b | 24 | /* Set this flag to '1' to use the LoRa modulation or to '0' to use FSK modulation */ |
ThomasMazier | 15:a99503d744c3 | 25 | |
ThomasMazier | 15:a99503d744c3 | 26 | |
ThomasMazier | 15:a99503d744c3 | 27 | #define USE_MODEM_LORA 1 //On utilise LoRa |
GregCr | 0:1ed39951ab7b | 28 | |
GregCr | 5:f2431c4fe3bb | 29 | #define RF_FREQUENCY 868000000 // Hz |
ThomasMazier | 15:a99503d744c3 | 30 | #define TX_OUTPUT_POWER 14 // dBm |
GregCr | 0:1ed39951ab7b | 31 | |
ThomasMazier | 15:a99503d744c3 | 32 | #if USE_MODEM_LORA == 1 // Definition des parametres de LoRa |
GregCr | 0:1ed39951ab7b | 33 | |
GregCr | 1:126d70d374f6 | 34 | #define LORA_BANDWIDTH 2 // [0: 125 kHz, |
GregCr | 0:1ed39951ab7b | 35 | // 1: 250 kHz, |
GregCr | 0:1ed39951ab7b | 36 | // 2: 500 kHz, |
GregCr | 0:1ed39951ab7b | 37 | // 3: Reserved] |
ThomasMazier | 15:a99503d744c3 | 38 | #define LORA_SPREADING_FACTOR 7 // [SF7..SF12] // ??????????????? |
ThomasMazier | 15:a99503d744c3 | 39 | #define LORA_CODINGRATE 1 // [1: 4/5, // ??????????????? |
ThomasMazier | 15:a99503d744c3 | 40 | // 2: 4/6, // ??????????????? |
GregCr | 0:1ed39951ab7b | 41 | // 3: 4/7, |
GregCr | 0:1ed39951ab7b | 42 | // 4: 4/8] |
ThomasMazier | 15:a99503d744c3 | 43 | #define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx // ??????????????? |
ThomasMazier | 15:a99503d744c3 | 44 | #define LORA_SYMBOL_TIMEOUT 5 // Symbols // ??????????????? |
ThomasMazier | 15:a99503d744c3 | 45 | #define LORA_FIX_LENGTH_PAYLOAD_ON false // ??????????????? |
ThomasMazier | 15:a99503d744c3 | 46 | #define LORA_FHSS_ENABLED false // ??????????????? |
ThomasMazier | 15:a99503d744c3 | 47 | #define LORA_NB_SYMB_HOP 4 // ??????????????? |
ThomasMazier | 15:a99503d744c3 | 48 | #define LORA_IQ_INVERSION_ON false // ??????????????? |
ThomasMazier | 15:a99503d744c3 | 49 | #define LORA_CRC_ENABLED true // ??????????????? |
mluis | 14:ab6eecd44b7a | 50 | |
mluis | 14:ab6eecd44b7a | 51 | |
GregCr | 0:1ed39951ab7b | 52 | #else |
ThomasMazier | 15:a99503d744c3 | 53 | #error "Please define a modem in the compiler options." //ERREUR si ni LoRa ni FSK n'a ete active |
GregCr | 0:1ed39951ab7b | 54 | #endif |
GregCr | 0:1ed39951ab7b | 55 | |
ThomasMazier | 15:a99503d744c3 | 56 | #define RX_TIMEOUT_VALUE 10000 // in ms |
GregCr | 0:1ed39951ab7b | 57 | #define BUFFER_SIZE 32 // Define the payload size here |
GregCr | 0:1ed39951ab7b | 58 | |
ThomasMazier | 15:a99503d744c3 | 59 | |
ThomasMazier | 15:a99503d744c3 | 60 | |
GregCr | 3:8b9e2a4df4b5 | 61 | |
ThomasMazier | 15:a99503d744c3 | 62 | |
ThomasMazier | 15:a99503d744c3 | 63 | //////////////////////////////////////////////////// GLOBAL VARIABLES DECLARATIONS ///////////////////////////////////////////////////////////////////// |
ThomasMazier | 15:a99503d744c3 | 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 |
ThomasMazier | 15:a99503d744c3 | 79 | |
mluis | 10:7af820d1e1df | 80 | }AppStates_t; |
GregCr | 0:1ed39951ab7b | 81 | |
mluis | 10:7af820d1e1df | 82 | volatile AppStates_t State = LOWPOWER; |
mluis | 10:7af820d1e1df | 83 | |
mluis | 10:7af820d1e1df | 84 | /*! |
mluis | 10:7af820d1e1df | 85 | * Radio events function pointer |
mluis | 10:7af820d1e1df | 86 | */ |
ThomasMazier | 15:a99503d744c3 | 87 | |
ThomasMazier | 15:a99503d744c3 | 88 | |
mluis | 10:7af820d1e1df | 89 | static RadioEvents_t RadioEvents; |
mluis | 10:7af820d1e1df | 90 | |
ThomasMazier | 15:a99503d744c3 | 91 | |
ThomasMazier | 15:a99503d744c3 | 92 | |
mluis | 10:7af820d1e1df | 93 | /* |
mluis | 10:7af820d1e1df | 94 | * Global variables declarations |
mluis | 10:7af820d1e1df | 95 | */ |
ThomasMazier | 15:a99503d744c3 | 96 | |
ThomasMazier | 15:a99503d744c3 | 97 | |
GregCr | 13:edb9b443c1dd | 98 | SX1272MB2xAS Radio( NULL ); |
GregCr | 0:1ed39951ab7b | 99 | |
ThomasMazier | 15:a99503d744c3 | 100 | uint16_t BufferSize = BUFFER_SIZE; //Taille de la memoire tampon |
GregCr | 0:1ed39951ab7b | 101 | uint8_t Buffer[BUFFER_SIZE]; |
GregCr | 0:1ed39951ab7b | 102 | |
ThomasMazier | 15:a99503d744c3 | 103 | int16_t RssiValue = 0.0; //Rssi = Received Signal Strentgh Indication |
ThomasMazier | 15:a99503d744c3 | 104 | int8_t SnrValue = 0.0; |
ThomasMazier | 15:a99503d744c3 | 105 | |
GregCr | 0:1ed39951ab7b | 106 | |
ThomasMazier | 15:a99503d744c3 | 107 | ////////////////////////////////////////////////////////////////////// MAIN ///////////////////////////////////////////////////////// |
ThomasMazier | 15:a99503d744c3 | 108 | |
ThomasMazier | 15:a99503d744c3 | 109 | |
ThomasMazier | 15:a99503d744c3 | 110 | int main( void ) |
GregCr | 0:1ed39951ab7b | 111 | { |
ThomasMazier | 15:a99503d744c3 | 112 | pc.baud(PCBAUD); |
ThomasMazier | 15:a99503d744c3 | 113 | |
ThomasMazier | 15:a99503d744c3 | 114 | debug( "\n\n\r SX1272 X Y Z Demo Application \n\n\r" ); |
mluis | 10:7af820d1e1df | 115 | |
mluis | 10:7af820d1e1df | 116 | // Initialize Radio driver |
mluis | 10:7af820d1e1df | 117 | RadioEvents.TxDone = OnTxDone; |
mluis | 10:7af820d1e1df | 118 | RadioEvents.RxDone = OnRxDone; |
mluis | 10:7af820d1e1df | 119 | RadioEvents.RxError = OnRxError; |
mluis | 10:7af820d1e1df | 120 | RadioEvents.TxTimeout = OnTxTimeout; |
mluis | 10:7af820d1e1df | 121 | RadioEvents.RxTimeout = OnRxTimeout; |
mluis | 10:7af820d1e1df | 122 | Radio.Init( &RadioEvents ); |
ThomasMazier | 15:a99503d744c3 | 123 | |
ThomasMazier | 15:a99503d744c3 | 124 | // verify the connection with the board |
GregCr | 7:c1bbd6c56979 | 125 | while( Radio.Read( REG_VERSION ) == 0x00 ) |
GregCr | 2:59e108728d71 | 126 | { |
GregCr | 7:c1bbd6c56979 | 127 | debug( "Radio could not be detected!\n\r", NULL ); |
GregCr | 7:c1bbd6c56979 | 128 | wait( 1 ); |
GregCr | 2:59e108728d71 | 129 | } |
ThomasMazier | 15:a99503d744c3 | 130 | |
mluis | 14:ab6eecd44b7a | 131 | debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1272MB2XAS ) ), "\n\r > Board Type: SX1272MB2xAS < \n\r" ); |
mluis | 14:ab6eecd44b7a | 132 | |
GregCr | 0:1ed39951ab7b | 133 | Radio.SetChannel( RF_FREQUENCY ); |
GregCr | 0:1ed39951ab7b | 134 | |
GregCr | 0:1ed39951ab7b | 135 | #if USE_MODEM_LORA == 1 |
mluis | 14:ab6eecd44b7a | 136 | |
mluis | 14:ab6eecd44b7a | 137 | debug_if( LORA_FHSS_ENABLED, "\n\n\r > LORA FHSS Mode < \n\n\r" ); |
mluis | 14:ab6eecd44b7a | 138 | debug_if( !LORA_FHSS_ENABLED, "\n\n\r > LORA Mode < \n\n\r" ); |
GregCr | 7:c1bbd6c56979 | 139 | |
GregCr | 0:1ed39951ab7b | 140 | Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH, |
GregCr | 0:1ed39951ab7b | 141 | LORA_SPREADING_FACTOR, LORA_CODINGRATE, |
GregCr | 0:1ed39951ab7b | 142 | LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON, |
mluis | 14:ab6eecd44b7a | 143 | LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, |
mluis | 14:ab6eecd44b7a | 144 | LORA_IQ_INVERSION_ON, 2000 ); |
mluis | 14:ab6eecd44b7a | 145 | |
GregCr | 0:1ed39951ab7b | 146 | Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR, |
GregCr | 0:1ed39951ab7b | 147 | LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH, |
mluis | 9:e764990e45df | 148 | LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0, |
mluis | 14:ab6eecd44b7a | 149 | LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP, |
GregCr | 3:8b9e2a4df4b5 | 150 | LORA_IQ_INVERSION_ON, true ); |
mluis | 14:ab6eecd44b7a | 151 | |
mluis | 14:ab6eecd44b7a | 152 | |
GregCr | 0:1ed39951ab7b | 153 | #else |
GregCr | 0:1ed39951ab7b | 154 | |
GregCr | 0:1ed39951ab7b | 155 | #error "Please define a modem in the compiler options." |
GregCr | 0:1ed39951ab7b | 156 | |
GregCr | 0:1ed39951ab7b | 157 | #endif |
mluis | 14:ab6eecd44b7a | 158 | |
ThomasMazier | 15:a99503d744c3 | 159 | Radio.Rx( 3500 ); |
ThomasMazier | 15:a99503d744c3 | 160 | State = RX; |
ThomasMazier | 15:a99503d744c3 | 161 | pc.printf("\n\r ++++++++++++++++++++++++++++++++ \n\r"); |
ThomasMazier | 15:a99503d744c3 | 162 | |
ThomasMazier | 15:a99503d744c3 | 163 | while(1) |
GregCr | 0:1ed39951ab7b | 164 | { |
ThomasMazier | 15:a99503d744c3 | 165 | |
GregCr | 0:1ed39951ab7b | 166 | if( BufferSize > 0 ) |
GregCr | 0:1ed39951ab7b | 167 | { |
ThomasMazier | 15:a99503d744c3 | 168 | if( strncmp( (const char *)Buffer, "B", 1)==0) |
ThomasMazier | 15:a99503d744c3 | 169 | { |
ThomasMazier | 15:a99503d744c3 | 170 | |
ThomasMazier | 15:a99503d744c3 | 171 | pc.printf("%s,\n", Buffer); |
ThomasMazier | 15:a99503d744c3 | 172 | pc.printf("\n\r"); |
ThomasMazier | 15:a99503d744c3 | 173 | |
ThomasMazier | 15:a99503d744c3 | 174 | |
ThomasMazier | 15:a99503d744c3 | 175 | } |
ThomasMazier | 15:a99503d744c3 | 176 | |
ThomasMazier | 15:a99503d744c3 | 177 | BufferSize = 0; |
ThomasMazier | 15:a99503d744c3 | 178 | Radio.Rx( 3500 ); |
GregCr | 0:1ed39951ab7b | 179 | } |
ThomasMazier | 15:a99503d744c3 | 180 | |
ThomasMazier | 15:a99503d744c3 | 181 | |
ThomasMazier | 15:a99503d744c3 | 182 | |
ThomasMazier | 15:a99503d744c3 | 183 | |
GregCr | 0:1ed39951ab7b | 184 | } |
GregCr | 0:1ed39951ab7b | 185 | } |
GregCr | 0:1ed39951ab7b | 186 | |
ThomasMazier | 15:a99503d744c3 | 187 | |
ThomasMazier | 15:a99503d744c3 | 188 | |
ThomasMazier | 15:a99503d744c3 | 189 | |
ThomasMazier | 15:a99503d744c3 | 190 | |
ThomasMazier | 15:a99503d744c3 | 191 | |
ThomasMazier | 15:a99503d744c3 | 192 | |
ThomasMazier | 15:a99503d744c3 | 193 | |
ThomasMazier | 15:a99503d744c3 | 194 | ///////////////////////////////////////// DECLARATION DES FONCTIONS///////////////////////////////////////////////////// |
ThomasMazier | 15:a99503d744c3 | 195 | |
GregCr | 0:1ed39951ab7b | 196 | void OnTxDone( void ) |
GregCr | 0:1ed39951ab7b | 197 | { |
GregCr | 5:f2431c4fe3bb | 198 | Radio.Sleep( ); |
GregCr | 0:1ed39951ab7b | 199 | State = TX; |
GregCr | 7:c1bbd6c56979 | 200 | debug_if( DEBUG_MESSAGE, "> OnTxDone\n\r" ); |
GregCr | 0:1ed39951ab7b | 201 | } |
GregCr | 0:1ed39951ab7b | 202 | |
mluis | 14:ab6eecd44b7a | 203 | void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ) |
GregCr | 0:1ed39951ab7b | 204 | { |
GregCr | 0:1ed39951ab7b | 205 | Radio.Sleep( ); |
GregCr | 0:1ed39951ab7b | 206 | BufferSize = size; |
GregCr | 0:1ed39951ab7b | 207 | memcpy( Buffer, payload, BufferSize ); |
GregCr | 0:1ed39951ab7b | 208 | RssiValue = rssi; |
GregCr | 0:1ed39951ab7b | 209 | SnrValue = snr; |
GregCr | 0:1ed39951ab7b | 210 | State = RX; |
GregCr | 7:c1bbd6c56979 | 211 | debug_if( DEBUG_MESSAGE, "> OnRxDone\n\r" ); |
GregCr | 0:1ed39951ab7b | 212 | } |
GregCr | 0:1ed39951ab7b | 213 | |
GregCr | 0:1ed39951ab7b | 214 | void OnTxTimeout( void ) |
GregCr | 0:1ed39951ab7b | 215 | { |
GregCr | 0:1ed39951ab7b | 216 | Radio.Sleep( ); |
GregCr | 0:1ed39951ab7b | 217 | State = TX_TIMEOUT; |
GregCr | 7:c1bbd6c56979 | 218 | debug_if( DEBUG_MESSAGE, "> OnTxTimeout\n\r" ); |
GregCr | 0:1ed39951ab7b | 219 | } |
GregCr | 0:1ed39951ab7b | 220 | |
GregCr | 0:1ed39951ab7b | 221 | void OnRxTimeout( void ) |
GregCr | 0:1ed39951ab7b | 222 | { |
ThomasMazier | 15:a99503d744c3 | 223 | //Radio.Sleep( ); |
mluis | 14:ab6eecd44b7a | 224 | Buffer[BufferSize] = 0; |
GregCr | 0:1ed39951ab7b | 225 | State = RX_TIMEOUT; |
GregCr | 7:c1bbd6c56979 | 226 | debug_if( DEBUG_MESSAGE, "> OnRxTimeout\n\r" ); |
GregCr | 0:1ed39951ab7b | 227 | } |
GregCr | 0:1ed39951ab7b | 228 | |
GregCr | 0:1ed39951ab7b | 229 | void OnRxError( void ) |
GregCr | 0:1ed39951ab7b | 230 | { |
GregCr | 0:1ed39951ab7b | 231 | State = RX_ERROR; |
GregCr | 7:c1bbd6c56979 | 232 | debug_if( DEBUG_MESSAGE, "> OnRxError\n\r" ); |
ThomasMazier | 15:a99503d744c3 | 233 | } |