![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
asd
main_pingpong.cpp@2:6554e96e4596, 2021-07-05 (annotated)
- Committer:
- Madhouse226
- Date:
- Mon Jul 05 14:49:53 2021 +0000
- Revision:
- 2:6554e96e4596
- Parent:
- 1:ecd6c7ea3545
asd
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
SemBen | 0:ea5a948dfc97 | 1 | /*! |
SemBen | 0:ea5a948dfc97 | 2 | * \file main_pingpong.cpp |
SemBen | 0:ea5a948dfc97 | 3 | * |
SemBen | 0:ea5a948dfc97 | 4 | * \brief Ping-Pong implementation |
SemBen | 0:ea5a948dfc97 | 5 | * |
SemBen | 0:ea5a948dfc97 | 6 | * \copyright Revised BSD License, see section \ref LICENSE. |
SemBen | 0:ea5a948dfc97 | 7 | * |
SemBen | 0:ea5a948dfc97 | 8 | * \code |
SemBen | 0:ea5a948dfc97 | 9 | * ______ _ |
SemBen | 0:ea5a948dfc97 | 10 | * / _____) _ | | |
SemBen | 0:ea5a948dfc97 | 11 | * ( (____ _____ ____ _| |_ _____ ____| |__ |
SemBen | 0:ea5a948dfc97 | 12 | * \____ \| ___ | (_ _) ___ |/ ___) _ \ |
SemBen | 0:ea5a948dfc97 | 13 | * _____) ) ____| | | || |_| ____( (___| | | | |
SemBen | 0:ea5a948dfc97 | 14 | * (______/|_____)_|_|_| \__)_____)\____)_| |_| |
mverdy | 1:ecd6c7ea3545 | 15 | * (C)2013-2018 Semtech |
SemBen | 0:ea5a948dfc97 | 16 | * |
SemBen | 0:ea5a948dfc97 | 17 | * \endcode |
SemBen | 0:ea5a948dfc97 | 18 | * |
SemBen | 0:ea5a948dfc97 | 19 | */ |
SemBen | 0:ea5a948dfc97 | 20 | |
SemBen | 0:ea5a948dfc97 | 21 | #include "mbed.h" |
SemBen | 0:ea5a948dfc97 | 22 | #include "sx126x-hal.h" |
SemBen | 0:ea5a948dfc97 | 23 | #include "sx126x.h" |
mverdy | 1:ecd6c7ea3545 | 24 | //#include "debug.h" |
SemBen | 0:ea5a948dfc97 | 25 | |
mverdy | 1:ecd6c7ea3545 | 26 | #define SEND_PING_BEAT_US 200000 |
mverdy | 1:ecd6c7ea3545 | 27 | #define RX_TIMEOUT_US 200000 |
mverdy | 1:ecd6c7ea3545 | 28 | |
SemBen | 0:ea5a948dfc97 | 29 | /* Set this flag to '1' to display debug messages on the console */ |
SemBen | 0:ea5a948dfc97 | 30 | #define DEBUG_MESSAGE 1 |
SemBen | 0:ea5a948dfc97 | 31 | |
SemBen | 0:ea5a948dfc97 | 32 | /* Set this flag to '1' to use the LoRa modulation or to '0' to use FSK modulation */ |
SemBen | 0:ea5a948dfc97 | 33 | #define USE_MODEM_LORA 1 |
SemBen | 0:ea5a948dfc97 | 34 | #define USE_MODEM_FSK !USE_MODEM_LORA |
SemBen | 0:ea5a948dfc97 | 35 | |
Madhouse226 | 2:6554e96e4596 | 36 | #define RF_FREQUENCY 869524963 // hz // 868000000 // Hz |
SemBen | 0:ea5a948dfc97 | 37 | #define TX_OUTPUT_POWER 14 // 14 dBm |
SemBen | 0:ea5a948dfc97 | 38 | |
SemBen | 0:ea5a948dfc97 | 39 | #if USE_MODEM_LORA == 1 |
SemBen | 0:ea5a948dfc97 | 40 | |
Madhouse226 | 2:6554e96e4596 | 41 | #define LORA_BANDWIDTH LORA_BW_125 // [0: 125 kHz, |
SemBen | 0:ea5a948dfc97 | 42 | // 1: 250 kHz, |
SemBen | 0:ea5a948dfc97 | 43 | // 2: 500 kHz, |
SemBen | 0:ea5a948dfc97 | 44 | // 3: Reserved] |
mverdy | 1:ecd6c7ea3545 | 45 | #define LORA_SPREADING_FACTOR LORA_SF7 // [SF7..SF12] |
mverdy | 1:ecd6c7ea3545 | 46 | #define LORA_LOWDATARATEOPTIMIZE 0 |
Madhouse226 | 2:6554e96e4596 | 47 | #define LORA_CODINGRATE LORA_CR_4_6 // [1: 4/5, |
SemBen | 0:ea5a948dfc97 | 48 | // 2: 4/6, |
SemBen | 0:ea5a948dfc97 | 49 | // 3: 4/7, |
SemBen | 0:ea5a948dfc97 | 50 | // 4: 4/8] |
SemBen | 0:ea5a948dfc97 | 51 | #define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx |
SemBen | 0:ea5a948dfc97 | 52 | #define LORA_SYMBOL_TIMEOUT 5 // Symbols |
mverdy | 1:ecd6c7ea3545 | 53 | #define LORA_HEADER_TYPE LORA_PACKET_VARIABLE_LENGTH |
SemBen | 0:ea5a948dfc97 | 54 | #define LORA_FHSS_ENABLED false |
SemBen | 0:ea5a948dfc97 | 55 | #define LORA_NB_SYMB_HOP 4 |
mverdy | 1:ecd6c7ea3545 | 56 | #define LORA_IQ LORA_IQ_NORMAL |
mverdy | 1:ecd6c7ea3545 | 57 | #define LORA_CRC_MODE LORA_CRC_OFF |
SemBen | 0:ea5a948dfc97 | 58 | |
SemBen | 0:ea5a948dfc97 | 59 | #elif USE_MODEM_FSK == 1 |
SemBen | 0:ea5a948dfc97 | 60 | |
SemBen | 0:ea5a948dfc97 | 61 | #define FSK_FDEV 25000 // Hz |
SemBen | 0:ea5a948dfc97 | 62 | #define FSK_DATARATE 19200 // bps |
mverdy | 1:ecd6c7ea3545 | 63 | #define FSK_BANDWIDTH RX_BW_93800 // Hz |
mverdy | 1:ecd6c7ea3545 | 64 | #define FSK_MODULATION_SHAPPING MOD_SHAPING_G_BT_05 |
SemBen | 0:ea5a948dfc97 | 65 | #define FSK_PREAMBLE_LENGTH 5 // Same for Tx and Rx |
mverdy | 1:ecd6c7ea3545 | 66 | #define FSK_HEADER_TYPE RADIO_PACKET_VARIABLE_LENGTH |
mverdy | 1:ecd6c7ea3545 | 67 | #define FSK_CRC_MODE RADIO_CRC_2_BYTES_CCIT |
mverdy | 1:ecd6c7ea3545 | 68 | #define FSK_ADDR_FILTERING RADIO_ADDRESSCOMP_FILT_NODE; |
mverdy | 1:ecd6c7ea3545 | 69 | #define FSK_WHITENING_MODE RADIO_DC_FREE_OFF |
mverdy | 1:ecd6c7ea3545 | 70 | #define FSK_PREAMBLE_DETECTOR_MODE RADIO_PREAMBLE_DETECTOR_OFF |
mverdy | 1:ecd6c7ea3545 | 71 | #define FSK_SYNCWORD_LENGTH 8 |
SemBen | 0:ea5a948dfc97 | 72 | #else |
SemBen | 0:ea5a948dfc97 | 73 | #error "Please define a modem in the compiler options." |
SemBen | 0:ea5a948dfc97 | 74 | #endif |
SemBen | 0:ea5a948dfc97 | 75 | |
SemBen | 0:ea5a948dfc97 | 76 | #define RX_TIMEOUT_VALUE 3500 // in ms |
Madhouse226 | 2:6554e96e4596 | 77 | #define BUFFER_SIZE 64 // Define the payload size here |
SemBen | 0:ea5a948dfc97 | 78 | |
SemBen | 0:ea5a948dfc97 | 79 | #if( defined ( TARGET_KL25Z ) || defined ( TARGET_LPC11U6X ) ) |
SemBen | 0:ea5a948dfc97 | 80 | DigitalOut led( LED2 ); |
SemBen | 0:ea5a948dfc97 | 81 | #else |
SemBen | 0:ea5a948dfc97 | 82 | DigitalOut led( LED1 ); |
SemBen | 0:ea5a948dfc97 | 83 | #endif |
SemBen | 0:ea5a948dfc97 | 84 | |
SemBen | 0:ea5a948dfc97 | 85 | /* |
SemBen | 0:ea5a948dfc97 | 86 | * Callback functions prototypes |
SemBen | 0:ea5a948dfc97 | 87 | */ |
SemBen | 0:ea5a948dfc97 | 88 | /*! |
SemBen | 0:ea5a948dfc97 | 89 | * @brief Function to be executed on Radio Tx Done event |
SemBen | 0:ea5a948dfc97 | 90 | */ |
SemBen | 0:ea5a948dfc97 | 91 | void OnTxDone( void ); |
SemBen | 0:ea5a948dfc97 | 92 | |
SemBen | 0:ea5a948dfc97 | 93 | /*! |
SemBen | 0:ea5a948dfc97 | 94 | * @brief Function to be executed on Radio Rx Done event |
SemBen | 0:ea5a948dfc97 | 95 | */ |
mverdy | 1:ecd6c7ea3545 | 96 | void OnRxDone( void ); |
SemBen | 0:ea5a948dfc97 | 97 | |
SemBen | 0:ea5a948dfc97 | 98 | /*! |
SemBen | 0:ea5a948dfc97 | 99 | * @brief Function executed on Radio Tx Timeout event |
SemBen | 0:ea5a948dfc97 | 100 | */ |
SemBen | 0:ea5a948dfc97 | 101 | void OnTxTimeout( void ); |
SemBen | 0:ea5a948dfc97 | 102 | |
SemBen | 0:ea5a948dfc97 | 103 | /*! |
SemBen | 0:ea5a948dfc97 | 104 | * @brief Function executed on Radio Rx Timeout event |
SemBen | 0:ea5a948dfc97 | 105 | */ |
SemBen | 0:ea5a948dfc97 | 106 | void OnRxTimeout( void ); |
SemBen | 0:ea5a948dfc97 | 107 | |
SemBen | 0:ea5a948dfc97 | 108 | /*! |
SemBen | 0:ea5a948dfc97 | 109 | * @brief Function executed on Radio Rx Error event |
SemBen | 0:ea5a948dfc97 | 110 | */ |
mverdy | 1:ecd6c7ea3545 | 111 | void OnRxError( IrqErrorCode_t errCode ); |
SemBen | 0:ea5a948dfc97 | 112 | |
SemBen | 0:ea5a948dfc97 | 113 | /*! |
SemBen | 0:ea5a948dfc97 | 114 | * @brief Function executed on Radio Fhss Change Channel event |
SemBen | 0:ea5a948dfc97 | 115 | */ |
SemBen | 0:ea5a948dfc97 | 116 | void OnFhssChangeChannel( uint8_t channelIndex ); |
mverdy | 1:ecd6c7ea3545 | 117 | |
mverdy | 1:ecd6c7ea3545 | 118 | typedef struct{ |
mverdy | 1:ecd6c7ea3545 | 119 | RadioPacketTypes_t packetType; |
mverdy | 1:ecd6c7ea3545 | 120 | int8_t txPower; |
mverdy | 1:ecd6c7ea3545 | 121 | RadioRampTimes_t txRampTime; |
mverdy | 1:ecd6c7ea3545 | 122 | ModulationParams_t modParams; |
mverdy | 1:ecd6c7ea3545 | 123 | PacketParams_t packetParams; |
mverdy | 1:ecd6c7ea3545 | 124 | uint32_t rfFrequency; |
mverdy | 1:ecd6c7ea3545 | 125 | uint16_t irqTx; |
mverdy | 1:ecd6c7ea3545 | 126 | uint16_t irqRx; |
mverdy | 1:ecd6c7ea3545 | 127 | uint32_t txTimeout; |
mverdy | 1:ecd6c7ea3545 | 128 | uint32_t rxTimeout; |
mverdy | 1:ecd6c7ea3545 | 129 | }RadioConfigurations_t; |
mverdy | 1:ecd6c7ea3545 | 130 | RadioConfigurations_t radioConfiguration; |
SemBen | 0:ea5a948dfc97 | 131 | |
SemBen | 0:ea5a948dfc97 | 132 | /* |
SemBen | 0:ea5a948dfc97 | 133 | * Global variables declarations |
SemBen | 0:ea5a948dfc97 | 134 | */ |
SemBen | 0:ea5a948dfc97 | 135 | typedef enum |
SemBen | 0:ea5a948dfc97 | 136 | { |
mverdy | 1:ecd6c7ea3545 | 137 | SEND_PACKET, |
mverdy | 1:ecd6c7ea3545 | 138 | WAIT_SEND_DONE, |
mverdy | 1:ecd6c7ea3545 | 139 | RECEIVE_PACKET, |
mverdy | 1:ecd6c7ea3545 | 140 | WAIT_RECEIVE_DONE, |
mverdy | 1:ecd6c7ea3545 | 141 | PACKET_RECEIVED, |
SemBen | 0:ea5a948dfc97 | 142 | }AppStates_t; |
mverdy | 1:ecd6c7ea3545 | 143 | volatile AppStates_t State = SEND_PACKET; |
mverdy | 1:ecd6c7ea3545 | 144 | |
mverdy | 1:ecd6c7ea3545 | 145 | typedef struct{ |
mverdy | 1:ecd6c7ea3545 | 146 | bool rxDone; |
mverdy | 1:ecd6c7ea3545 | 147 | bool rxError; |
mverdy | 1:ecd6c7ea3545 | 148 | bool txDone; |
mverdy | 1:ecd6c7ea3545 | 149 | bool rxTimeout; |
mverdy | 1:ecd6c7ea3545 | 150 | bool txTimeout; |
mverdy | 1:ecd6c7ea3545 | 151 | }RadioFlags_t; |
mverdy | 1:ecd6c7ea3545 | 152 | RadioFlags_t radioFlags = { |
mverdy | 1:ecd6c7ea3545 | 153 | .txDone = false, |
mverdy | 1:ecd6c7ea3545 | 154 | .rxDone = false, |
mverdy | 1:ecd6c7ea3545 | 155 | .rxError = false, |
mverdy | 1:ecd6c7ea3545 | 156 | .rxTimeout = false, |
mverdy | 1:ecd6c7ea3545 | 157 | .txTimeout = false, |
mverdy | 1:ecd6c7ea3545 | 158 | }; |
SemBen | 0:ea5a948dfc97 | 159 | |
SemBen | 0:ea5a948dfc97 | 160 | /*! |
SemBen | 0:ea5a948dfc97 | 161 | * Radio events function pointer |
SemBen | 0:ea5a948dfc97 | 162 | */ |
mverdy | 1:ecd6c7ea3545 | 163 | static RadioCallbacks_t RadioEvents = { |
mverdy | 1:ecd6c7ea3545 | 164 | .txDone = &OnTxDone, |
mverdy | 1:ecd6c7ea3545 | 165 | .txTimeout = &OnTxTimeout, |
mverdy | 1:ecd6c7ea3545 | 166 | .rxDone = &OnRxDone, |
mverdy | 1:ecd6c7ea3545 | 167 | .rxPreambleDetect = NULL, |
mverdy | 1:ecd6c7ea3545 | 168 | .rxHeaderDone = NULL, |
mverdy | 1:ecd6c7ea3545 | 169 | .rxTimeout = &OnRxTimeout, |
mverdy | 1:ecd6c7ea3545 | 170 | .rxError = &OnRxError, |
mverdy | 1:ecd6c7ea3545 | 171 | .cadDone = NULL, |
mverdy | 1:ecd6c7ea3545 | 172 | }; |
mverdy | 1:ecd6c7ea3545 | 173 | |
SemBen | 0:ea5a948dfc97 | 174 | /* |
SemBen | 0:ea5a948dfc97 | 175 | * Global variables declarations |
SemBen | 0:ea5a948dfc97 | 176 | */ |
SemBen | 0:ea5a948dfc97 | 177 | //Radio Radio( NULL ); |
mverdy | 1:ecd6c7ea3545 | 178 | #define MESSAGE_SIZE 4 |
mverdy | 1:ecd6c7ea3545 | 179 | typedef uint8_t Messages_t[MESSAGE_SIZE]; |
mverdy | 1:ecd6c7ea3545 | 180 | const Messages_t PingMsg = {'P', 'I', 'N', 'G'}; |
mverdy | 1:ecd6c7ea3545 | 181 | const Messages_t PongMsg = {'P', 'O', 'N', 'G'}; |
mverdy | 1:ecd6c7ea3545 | 182 | const Messages_t *messageToReceive = &PongMsg; |
mverdy | 1:ecd6c7ea3545 | 183 | const Messages_t *messageToSend = &PingMsg; |
SemBen | 0:ea5a948dfc97 | 184 | |
mverdy | 1:ecd6c7ea3545 | 185 | uint8_t BufferSize = BUFFER_SIZE; |
SemBen | 0:ea5a948dfc97 | 186 | uint8_t Buffer[BUFFER_SIZE]; |
SemBen | 0:ea5a948dfc97 | 187 | |
mverdy | 1:ecd6c7ea3545 | 188 | int8_t RssiValue = 0; |
mverdy | 1:ecd6c7ea3545 | 189 | int8_t SnrValue = 0; |
mverdy | 1:ecd6c7ea3545 | 190 | |
mverdy | 1:ecd6c7ea3545 | 191 | void GetRssiSnr(int8_t *rssi, int8_t *snr); |
mverdy | 1:ecd6c7ea3545 | 192 | SX126xHal Radio( D11, D12, D13, D7, D3, D5, NC, NC, A0, A1, A2, D8, &RadioEvents ); |
mverdy | 1:ecd6c7ea3545 | 193 | |
mverdy | 1:ecd6c7ea3545 | 194 | void SetToMaster(void); |
mverdy | 1:ecd6c7ea3545 | 195 | void SetToSlave(void); |
mverdy | 1:ecd6c7ea3545 | 196 | void RunMasterStateMachine(); |
mverdy | 1:ecd6c7ea3545 | 197 | void RunSlaveStateMachine(); |
mverdy | 1:ecd6c7ea3545 | 198 | void SetConfiguration(RadioConfigurations_t *config); |
mverdy | 1:ecd6c7ea3545 | 199 | void ConfigureGeneralRadio(SX126xHal *radio, RadioConfigurations_t *config); |
mverdy | 1:ecd6c7ea3545 | 200 | void ConfigureRadioTx(SX126xHal *radio, RadioConfigurations_t *config); |
mverdy | 1:ecd6c7ea3545 | 201 | void ConfigureRadioRx(SX126xHal *radio, RadioConfigurations_t *config); |
mverdy | 1:ecd6c7ea3545 | 202 | void PrepareBuffer(SX126xHal *radio, const Messages_t *messageToSend); |
mverdy | 1:ecd6c7ea3545 | 203 | bool isMaster = true; |
mverdy | 1:ecd6c7ea3545 | 204 | bool masterCanSend = true; |
mverdy | 1:ecd6c7ea3545 | 205 | void MasterSendNextEvent(void){masterCanSend = true;} |
mverdy | 1:ecd6c7ea3545 | 206 | bool slaveCanListen = false; |
mverdy | 1:ecd6c7ea3545 | 207 | void SlaveListenNextEvent(void){slaveCanListen = true;} |
mverdy | 1:ecd6c7ea3545 | 208 | Ticker masterSendNextTicker; |
mverdy | 1:ecd6c7ea3545 | 209 | Ticker slaveListenNextTicker; |
mverdy | 1:ecd6c7ea3545 | 210 | |
Madhouse226 | 2:6554e96e4596 | 211 | int counter = 0; |
Madhouse226 | 2:6554e96e4596 | 212 | |
mverdy | 1:ecd6c7ea3545 | 213 | Serial serial(USBTX, USBRX); |
mverdy | 1:ecd6c7ea3545 | 214 | |
SemBen | 0:ea5a948dfc97 | 215 | int main( void ) |
SemBen | 0:ea5a948dfc97 | 216 | { |
mverdy | 1:ecd6c7ea3545 | 217 | Radio.Reset(); |
mverdy | 1:ecd6c7ea3545 | 218 | Radio.Init(); |
mverdy | 1:ecd6c7ea3545 | 219 | serial.baud(115200); |
Madhouse226 | 2:6554e96e4596 | 220 | // SetToMaster(); |
Madhouse226 | 2:6554e96e4596 | 221 | SetToSlave(); |
mverdy | 1:ecd6c7ea3545 | 222 | SetConfiguration(&radioConfiguration); |
mverdy | 1:ecd6c7ea3545 | 223 | ConfigureGeneralRadio(&Radio, &radioConfiguration); |
mverdy | 1:ecd6c7ea3545 | 224 | while(true){ |
mverdy | 1:ecd6c7ea3545 | 225 | if(isMaster){ |
mverdy | 1:ecd6c7ea3545 | 226 | RunMasterStateMachine(); |
mverdy | 1:ecd6c7ea3545 | 227 | } |
mverdy | 1:ecd6c7ea3545 | 228 | else{ |
mverdy | 1:ecd6c7ea3545 | 229 | RunSlaveStateMachine(); |
mverdy | 1:ecd6c7ea3545 | 230 | } |
SemBen | 0:ea5a948dfc97 | 231 | } |
mverdy | 1:ecd6c7ea3545 | 232 | } |
mverdy | 1:ecd6c7ea3545 | 233 | |
mverdy | 1:ecd6c7ea3545 | 234 | void RunMasterStateMachine(){ |
mverdy | 1:ecd6c7ea3545 | 235 | switch(State){ |
mverdy | 1:ecd6c7ea3545 | 236 | case SEND_PACKET:{ |
mverdy | 1:ecd6c7ea3545 | 237 | if(masterCanSend == true){ |
mverdy | 1:ecd6c7ea3545 | 238 | masterCanSend = false; |
mverdy | 1:ecd6c7ea3545 | 239 | masterSendNextTicker.attach_us(&MasterSendNextEvent, SEND_PING_BEAT_US); |
mverdy | 1:ecd6c7ea3545 | 240 | PrepareBuffer(&Radio, messageToSend); |
mverdy | 1:ecd6c7ea3545 | 241 | ConfigureRadioTx(&Radio, &radioConfiguration); |
mverdy | 1:ecd6c7ea3545 | 242 | Radio.SetTx(radioConfiguration.txTimeout); |
Madhouse226 | 2:6554e96e4596 | 243 | printf("Ping... %d\n", ++counter); |
mverdy | 1:ecd6c7ea3545 | 244 | State = WAIT_SEND_DONE; |
mverdy | 1:ecd6c7ea3545 | 245 | } |
mverdy | 1:ecd6c7ea3545 | 246 | break; |
mverdy | 1:ecd6c7ea3545 | 247 | } |
mverdy | 1:ecd6c7ea3545 | 248 | |
mverdy | 1:ecd6c7ea3545 | 249 | case WAIT_SEND_DONE:{ |
mverdy | 1:ecd6c7ea3545 | 250 | if(radioFlags.txDone){ |
mverdy | 1:ecd6c7ea3545 | 251 | radioFlags.txDone = false; |
mverdy | 1:ecd6c7ea3545 | 252 | State = RECEIVE_PACKET; |
mverdy | 1:ecd6c7ea3545 | 253 | } |
mverdy | 1:ecd6c7ea3545 | 254 | if(radioFlags.txTimeout){ |
mverdy | 1:ecd6c7ea3545 | 255 | radioFlags.txTimeout = false; |
mverdy | 1:ecd6c7ea3545 | 256 | State = SEND_PACKET; |
SemBen | 0:ea5a948dfc97 | 257 | } |
SemBen | 0:ea5a948dfc97 | 258 | break; |
mverdy | 1:ecd6c7ea3545 | 259 | } |
mverdy | 1:ecd6c7ea3545 | 260 | |
mverdy | 1:ecd6c7ea3545 | 261 | case RECEIVE_PACKET:{ |
mverdy | 1:ecd6c7ea3545 | 262 | ConfigureRadioRx(&Radio, &radioConfiguration); |
mverdy | 1:ecd6c7ea3545 | 263 | Radio.SetRx(radioConfiguration.rxTimeout); |
mverdy | 1:ecd6c7ea3545 | 264 | State = WAIT_RECEIVE_DONE; |
SemBen | 0:ea5a948dfc97 | 265 | break; |
mverdy | 1:ecd6c7ea3545 | 266 | } |
mverdy | 1:ecd6c7ea3545 | 267 | |
mverdy | 1:ecd6c7ea3545 | 268 | case WAIT_RECEIVE_DONE:{ |
mverdy | 1:ecd6c7ea3545 | 269 | if(radioFlags.rxDone == true){ |
mverdy | 1:ecd6c7ea3545 | 270 | radioFlags.rxDone = false; |
mverdy | 1:ecd6c7ea3545 | 271 | State = PACKET_RECEIVED; |
SemBen | 0:ea5a948dfc97 | 272 | } |
mverdy | 1:ecd6c7ea3545 | 273 | if(radioFlags.rxTimeout == true){ |
mverdy | 1:ecd6c7ea3545 | 274 | radioFlags.rxTimeout = false; |
mverdy | 1:ecd6c7ea3545 | 275 | State = SEND_PACKET; |
SemBen | 0:ea5a948dfc97 | 276 | } |
SemBen | 0:ea5a948dfc97 | 277 | break; |
mverdy | 1:ecd6c7ea3545 | 278 | } |
mverdy | 1:ecd6c7ea3545 | 279 | |
mverdy | 1:ecd6c7ea3545 | 280 | case PACKET_RECEIVED:{ |
mverdy | 1:ecd6c7ea3545 | 281 | Radio.GetPayload( Buffer, &BufferSize, BUFFER_SIZE ); |
mverdy | 1:ecd6c7ea3545 | 282 | RssiValue = Radio.GetRssiInst(); |
mverdy | 1:ecd6c7ea3545 | 283 | GetRssiSnr(&RssiValue, &SnrValue); |
mverdy | 1:ecd6c7ea3545 | 284 | if( strncmp(( const char* )Buffer, (const char*)messageToReceive, MESSAGE_SIZE ) == 0 ){ |
mverdy | 1:ecd6c7ea3545 | 285 | printf("...Pong\n"); |
mverdy | 1:ecd6c7ea3545 | 286 | State = SEND_PACKET; |
SemBen | 0:ea5a948dfc97 | 287 | } |
mverdy | 1:ecd6c7ea3545 | 288 | else if( strncmp(( const char* )Buffer, (const char*)messageToSend, MESSAGE_SIZE ) == 0 ){ |
mverdy | 1:ecd6c7ea3545 | 289 | // Another Master is in the air, swith to slave |
mverdy | 1:ecd6c7ea3545 | 290 | SetToSlave(); |
SemBen | 0:ea5a948dfc97 | 291 | } |
mverdy | 1:ecd6c7ea3545 | 292 | else{ |
mverdy | 1:ecd6c7ea3545 | 293 | printf("WRONG PAYLOAD\n"); |
mverdy | 1:ecd6c7ea3545 | 294 | SetToMaster(); |
mverdy | 1:ecd6c7ea3545 | 295 | } |
SemBen | 0:ea5a948dfc97 | 296 | break; |
SemBen | 0:ea5a948dfc97 | 297 | } |
SemBen | 0:ea5a948dfc97 | 298 | } |
SemBen | 0:ea5a948dfc97 | 299 | } |
mverdy | 1:ecd6c7ea3545 | 300 | |
mverdy | 1:ecd6c7ea3545 | 301 | void RunSlaveStateMachine(){ |
mverdy | 1:ecd6c7ea3545 | 302 | switch(State){ |
mverdy | 1:ecd6c7ea3545 | 303 | case RECEIVE_PACKET:{ |
mverdy | 1:ecd6c7ea3545 | 304 | if(slaveCanListen == true){ |
mverdy | 1:ecd6c7ea3545 | 305 | slaveCanListen = false; |
mverdy | 1:ecd6c7ea3545 | 306 | ConfigureRadioRx(&Radio, &radioConfiguration); |
mverdy | 1:ecd6c7ea3545 | 307 | Radio.SetRx(radioConfiguration.rxTimeout); |
mverdy | 1:ecd6c7ea3545 | 308 | slaveListenNextTicker.attach_us(&SlaveListenNextEvent, SEND_PING_BEAT_US - ( RX_TIMEOUT_US>>1 )); |
mverdy | 1:ecd6c7ea3545 | 309 | State = WAIT_RECEIVE_DONE; |
mverdy | 1:ecd6c7ea3545 | 310 | } |
mverdy | 1:ecd6c7ea3545 | 311 | break; |
mverdy | 1:ecd6c7ea3545 | 312 | } |
mverdy | 1:ecd6c7ea3545 | 313 | |
mverdy | 1:ecd6c7ea3545 | 314 | case WAIT_RECEIVE_DONE:{ |
mverdy | 1:ecd6c7ea3545 | 315 | if(radioFlags.rxDone == true){ |
mverdy | 1:ecd6c7ea3545 | 316 | radioFlags.rxDone = false; |
mverdy | 1:ecd6c7ea3545 | 317 | State = PACKET_RECEIVED; |
mverdy | 1:ecd6c7ea3545 | 318 | } |
mverdy | 1:ecd6c7ea3545 | 319 | if(radioFlags.rxTimeout == true){ |
mverdy | 1:ecd6c7ea3545 | 320 | radioFlags.rxTimeout = false; |
mverdy | 1:ecd6c7ea3545 | 321 | SetToMaster(); |
mverdy | 1:ecd6c7ea3545 | 322 | } |
mverdy | 1:ecd6c7ea3545 | 323 | break; |
mverdy | 1:ecd6c7ea3545 | 324 | } |
mverdy | 1:ecd6c7ea3545 | 325 | |
mverdy | 1:ecd6c7ea3545 | 326 | case PACKET_RECEIVED:{ |
mverdy | 1:ecd6c7ea3545 | 327 | Radio.GetPayload( Buffer, &BufferSize, BUFFER_SIZE ); |
mverdy | 1:ecd6c7ea3545 | 328 | RssiValue = Radio.GetRssiInst(); |
mverdy | 1:ecd6c7ea3545 | 329 | GetRssiSnr(&RssiValue, &SnrValue); |
mverdy | 1:ecd6c7ea3545 | 330 | if( strncmp(( const char* )Buffer, (const char*)messageToReceive, MESSAGE_SIZE ) == 0 ){ |
mverdy | 1:ecd6c7ea3545 | 331 | printf("...Ping\n"); |
mverdy | 1:ecd6c7ea3545 | 332 | State = SEND_PACKET; |
mverdy | 1:ecd6c7ea3545 | 333 | } |
mverdy | 1:ecd6c7ea3545 | 334 | else{ |
mverdy | 1:ecd6c7ea3545 | 335 | SetToMaster(); |
mverdy | 1:ecd6c7ea3545 | 336 | } |
mverdy | 1:ecd6c7ea3545 | 337 | break; |
mverdy | 1:ecd6c7ea3545 | 338 | } |
mverdy | 1:ecd6c7ea3545 | 339 | |
mverdy | 1:ecd6c7ea3545 | 340 | case SEND_PACKET:{ |
mverdy | 1:ecd6c7ea3545 | 341 | PrepareBuffer(&Radio, messageToSend); |
mverdy | 1:ecd6c7ea3545 | 342 | ConfigureRadioTx(&Radio, &radioConfiguration); |
mverdy | 1:ecd6c7ea3545 | 343 | printf("Pong...\n"); |
mverdy | 1:ecd6c7ea3545 | 344 | Radio.SetTx(radioConfiguration.txTimeout); |
mverdy | 1:ecd6c7ea3545 | 345 | State = WAIT_SEND_DONE; |
mverdy | 1:ecd6c7ea3545 | 346 | break; |
mverdy | 1:ecd6c7ea3545 | 347 | } |
mverdy | 1:ecd6c7ea3545 | 348 | |
mverdy | 1:ecd6c7ea3545 | 349 | case WAIT_SEND_DONE:{ |
mverdy | 1:ecd6c7ea3545 | 350 | if(radioFlags.txDone){ |
mverdy | 1:ecd6c7ea3545 | 351 | radioFlags.txDone = false; |
mverdy | 1:ecd6c7ea3545 | 352 | State = RECEIVE_PACKET; |
mverdy | 1:ecd6c7ea3545 | 353 | } |
mverdy | 1:ecd6c7ea3545 | 354 | if(radioFlags.txTimeout){ |
mverdy | 1:ecd6c7ea3545 | 355 | radioFlags.txTimeout = false; |
mverdy | 1:ecd6c7ea3545 | 356 | SetToMaster(); |
mverdy | 1:ecd6c7ea3545 | 357 | } |
mverdy | 1:ecd6c7ea3545 | 358 | break; |
mverdy | 1:ecd6c7ea3545 | 359 | } |
mverdy | 1:ecd6c7ea3545 | 360 | } |
mverdy | 1:ecd6c7ea3545 | 361 | } |
mverdy | 1:ecd6c7ea3545 | 362 | |
mverdy | 1:ecd6c7ea3545 | 363 | void SetToMaster(){ |
mverdy | 1:ecd6c7ea3545 | 364 | printf("-->Master\n"); |
mverdy | 1:ecd6c7ea3545 | 365 | isMaster = true; |
mverdy | 1:ecd6c7ea3545 | 366 | masterCanSend = true; |
mverdy | 1:ecd6c7ea3545 | 367 | State = SEND_PACKET; |
mverdy | 1:ecd6c7ea3545 | 368 | messageToReceive = &PongMsg; |
mverdy | 1:ecd6c7ea3545 | 369 | messageToSend = &PingMsg; |
mverdy | 1:ecd6c7ea3545 | 370 | } |
mverdy | 1:ecd6c7ea3545 | 371 | |
mverdy | 1:ecd6c7ea3545 | 372 | void SetToSlave(){ |
mverdy | 1:ecd6c7ea3545 | 373 | slaveCanListen = false; |
mverdy | 1:ecd6c7ea3545 | 374 | slaveListenNextTicker.attach_us(&SlaveListenNextEvent, SEND_PING_BEAT_US - ( RX_TIMEOUT_US>>1 )); |
mverdy | 1:ecd6c7ea3545 | 375 | printf("--> Slave\n"); |
mverdy | 1:ecd6c7ea3545 | 376 | isMaster = false; |
mverdy | 1:ecd6c7ea3545 | 377 | State = RECEIVE_PACKET; |
mverdy | 1:ecd6c7ea3545 | 378 | messageToReceive = &PingMsg; |
mverdy | 1:ecd6c7ea3545 | 379 | messageToSend = &PongMsg; |
mverdy | 1:ecd6c7ea3545 | 380 | } |
SemBen | 0:ea5a948dfc97 | 381 | |
SemBen | 0:ea5a948dfc97 | 382 | void OnTxDone( void ) |
SemBen | 0:ea5a948dfc97 | 383 | { |
mverdy | 1:ecd6c7ea3545 | 384 | radioFlags.txDone = true; |
SemBen | 0:ea5a948dfc97 | 385 | } |
SemBen | 0:ea5a948dfc97 | 386 | |
mverdy | 1:ecd6c7ea3545 | 387 | void OnRxDone( void ) |
SemBen | 0:ea5a948dfc97 | 388 | { |
mverdy | 1:ecd6c7ea3545 | 389 | radioFlags.rxDone = true; |
SemBen | 0:ea5a948dfc97 | 390 | } |
SemBen | 0:ea5a948dfc97 | 391 | |
SemBen | 0:ea5a948dfc97 | 392 | void OnTxTimeout( void ) |
SemBen | 0:ea5a948dfc97 | 393 | { |
mverdy | 1:ecd6c7ea3545 | 394 | radioFlags.txTimeout = true; |
SemBen | 0:ea5a948dfc97 | 395 | debug_if( DEBUG_MESSAGE, "> OnTxTimeout\n\r" ); |
SemBen | 0:ea5a948dfc97 | 396 | } |
SemBen | 0:ea5a948dfc97 | 397 | |
SemBen | 0:ea5a948dfc97 | 398 | void OnRxTimeout( void ) |
SemBen | 0:ea5a948dfc97 | 399 | { |
mverdy | 1:ecd6c7ea3545 | 400 | radioFlags.rxTimeout = true; |
SemBen | 0:ea5a948dfc97 | 401 | debug_if( DEBUG_MESSAGE, "> OnRxTimeout\n\r" ); |
SemBen | 0:ea5a948dfc97 | 402 | } |
SemBen | 0:ea5a948dfc97 | 403 | |
mverdy | 1:ecd6c7ea3545 | 404 | void OnRxError( IrqErrorCode_t errCode ) |
SemBen | 0:ea5a948dfc97 | 405 | { |
mverdy | 1:ecd6c7ea3545 | 406 | radioFlags.rxError = true; |
SemBen | 0:ea5a948dfc97 | 407 | debug_if( DEBUG_MESSAGE, "> OnRxError\n\r" ); |
mverdy | 1:ecd6c7ea3545 | 408 | } |
mverdy | 1:ecd6c7ea3545 | 409 | |
mverdy | 1:ecd6c7ea3545 | 410 | void SetConfiguration(RadioConfigurations_t *config){ |
mverdy | 1:ecd6c7ea3545 | 411 | config->irqRx = IRQ_RX_DONE | IRQ_RX_TX_TIMEOUT; |
mverdy | 1:ecd6c7ea3545 | 412 | config->irqTx = IRQ_TX_DONE | IRQ_RX_TX_TIMEOUT; |
mverdy | 1:ecd6c7ea3545 | 413 | config->rfFrequency = RF_FREQUENCY; |
mverdy | 1:ecd6c7ea3545 | 414 | config->txTimeout = 0; |
mverdy | 1:ecd6c7ea3545 | 415 | config->rxTimeout = (uint32_t)(RX_TIMEOUT_US / 15.625); |
mverdy | 1:ecd6c7ea3545 | 416 | config->txPower = TX_OUTPUT_POWER; |
mverdy | 1:ecd6c7ea3545 | 417 | config->txRampTime = RADIO_RAMP_200_US; |
mverdy | 1:ecd6c7ea3545 | 418 | #if USE_MODEM_LORA == 1 |
mverdy | 1:ecd6c7ea3545 | 419 | config->packetType = PACKET_TYPE_LORA; |
mverdy | 1:ecd6c7ea3545 | 420 | config->modParams.PacketType = PACKET_TYPE_LORA; |
mverdy | 1:ecd6c7ea3545 | 421 | config->modParams.Params.LoRa.Bandwidth = LORA_BANDWIDTH; |
mverdy | 1:ecd6c7ea3545 | 422 | config->modParams.Params.LoRa.CodingRate = LORA_CODINGRATE; |
mverdy | 1:ecd6c7ea3545 | 423 | config->modParams.Params.LoRa.LowDatarateOptimize = LORA_LOWDATARATEOPTIMIZE; |
mverdy | 1:ecd6c7ea3545 | 424 | config->modParams.Params.LoRa.SpreadingFactor = LORA_SPREADING_FACTOR; |
mverdy | 1:ecd6c7ea3545 | 425 | config->packetParams.PacketType = PACKET_TYPE_LORA; |
mverdy | 1:ecd6c7ea3545 | 426 | config->packetParams.Params.LoRa.CrcMode = LORA_CRC_MODE; |
mverdy | 1:ecd6c7ea3545 | 427 | config->packetParams.Params.LoRa.HeaderType = LORA_HEADER_TYPE; |
mverdy | 1:ecd6c7ea3545 | 428 | config->packetParams.Params.LoRa.InvertIQ = LORA_IQ; |
mverdy | 1:ecd6c7ea3545 | 429 | config->packetParams.Params.LoRa.PayloadLength = BUFFER_SIZE; |
mverdy | 1:ecd6c7ea3545 | 430 | config->packetParams.Params.LoRa.PreambleLength = LORA_PREAMBLE_LENGTH; |
mverdy | 1:ecd6c7ea3545 | 431 | #elif USE_MODEM_FSK == 1 |
mverdy | 1:ecd6c7ea3545 | 432 | config->packetType = PACKET_TYPE_GFSK; |
mverdy | 1:ecd6c7ea3545 | 433 | config->modParams.PacketType = PACKET_TYPE_GFSK; |
mverdy | 1:ecd6c7ea3545 | 434 | config->modParams.Params.Gfsk.Bandwidth = FSK_BANDWIDTH; |
mverdy | 1:ecd6c7ea3545 | 435 | config->modParams.Params.Gfsk.BitRate = 1024000000 / FSK_DATARATE; |
mverdy | 1:ecd6c7ea3545 | 436 | config->modParams.Params.Gfsk.Fdev = FSK_FDEV * 1.048576; |
mverdy | 1:ecd6c7ea3545 | 437 | config->modParams.Params.Gfsk.ModulationShaping = FSK_MODULATION_SHAPPING; |
mverdy | 1:ecd6c7ea3545 | 438 | config->packetParams.PacketType = PACKET_TYPE_GFSK; |
mverdy | 1:ecd6c7ea3545 | 439 | config->packetParams.Params.Gfsk.AddrComp = FSK_ADDR_FILTERING; |
mverdy | 1:ecd6c7ea3545 | 440 | config->packetParams.Params.Gfsk.CrcLength = FSK_CRC_MODE; |
mverdy | 1:ecd6c7ea3545 | 441 | config->packetParams.Params.Gfsk.DcFree = FSK_WHITENING_MODE; |
mverdy | 1:ecd6c7ea3545 | 442 | config->packetParams.Params.Gfsk.HeaderType = FSK_HEADER_TYPE; |
mverdy | 1:ecd6c7ea3545 | 443 | config->packetParams.Params.Gfsk.PayloadLength = BUFFER_SIZE; |
mverdy | 1:ecd6c7ea3545 | 444 | config->packetParams.Params.Gfsk.PreambleLength = FSK_PREAMBLE_LENGTH; |
mverdy | 1:ecd6c7ea3545 | 445 | config->packetParams.Params.Gfsk.PreambleMinDetect = FSK_PREAMBLE_DETECTOR_MODE; |
mverdy | 1:ecd6c7ea3545 | 446 | config->packetParams.Params.Gfsk.SyncWordLength = FSK_SYNCWORD_LENGTH; |
mverdy | 1:ecd6c7ea3545 | 447 | #endif |
mverdy | 1:ecd6c7ea3545 | 448 | } |
mverdy | 1:ecd6c7ea3545 | 449 | |
mverdy | 1:ecd6c7ea3545 | 450 | void ConfigureGeneralRadio(SX126xHal *radio, RadioConfigurations_t *config){ |
mverdy | 1:ecd6c7ea3545 | 451 | radio->SetPacketType(config->packetType); |
mverdy | 1:ecd6c7ea3545 | 452 | radio->SetPacketParams(&config->packetParams); |
mverdy | 1:ecd6c7ea3545 | 453 | radio->SetModulationParams(&config->modParams); |
mverdy | 1:ecd6c7ea3545 | 454 | radio->SetRfFrequency(config->rfFrequency); |
mverdy | 1:ecd6c7ea3545 | 455 | radio->SetTxParams(config->txPower, config->txRampTime); |
mverdy | 1:ecd6c7ea3545 | 456 | radio->SetInterruptMode(); |
mverdy | 1:ecd6c7ea3545 | 457 | if(config->packetType == PACKET_TYPE_GFSK){ |
mverdy | 1:ecd6c7ea3545 | 458 | uint8_t syncword[8] = {0xF0, 0x0F, 0x55, 0xAA, 0xF0, 0x0F, 0x55, 0xAA}; |
mverdy | 1:ecd6c7ea3545 | 459 | radio->SetSyncWord(syncword); |
mverdy | 1:ecd6c7ea3545 | 460 | } |
mverdy | 1:ecd6c7ea3545 | 461 | } |
mverdy | 1:ecd6c7ea3545 | 462 | |
mverdy | 1:ecd6c7ea3545 | 463 | void ConfigureRadioTx(SX126xHal *radio, RadioConfigurations_t *config){ |
mverdy | 1:ecd6c7ea3545 | 464 | radio->SetDioIrqParams(config->irqTx, config->irqTx, IRQ_RADIO_NONE, IRQ_RADIO_NONE); |
mverdy | 1:ecd6c7ea3545 | 465 | } |
mverdy | 1:ecd6c7ea3545 | 466 | |
mverdy | 1:ecd6c7ea3545 | 467 | void ConfigureRadioRx(SX126xHal *radio, RadioConfigurations_t *config){ |
mverdy | 1:ecd6c7ea3545 | 468 | radio->SetDioIrqParams(config->irqRx, config->irqRx, IRQ_RADIO_NONE, IRQ_RADIO_NONE); |
mverdy | 1:ecd6c7ea3545 | 469 | } |
mverdy | 1:ecd6c7ea3545 | 470 | |
mverdy | 1:ecd6c7ea3545 | 471 | void PrepareBuffer(SX126xHal *radio, const Messages_t *messageToSend){ |
mverdy | 1:ecd6c7ea3545 | 472 | radio->SetPayload((uint8_t*)messageToSend, MESSAGE_SIZE); |
mverdy | 1:ecd6c7ea3545 | 473 | } |
mverdy | 1:ecd6c7ea3545 | 474 | |
mverdy | 1:ecd6c7ea3545 | 475 | void GetRssiSnr(int8_t *rssi, int8_t *snr) |
mverdy | 1:ecd6c7ea3545 | 476 | { |
mverdy | 1:ecd6c7ea3545 | 477 | PacketStatus_t pkt_stat; |
mverdy | 1:ecd6c7ea3545 | 478 | Radio.GetPacketStatus(&pkt_stat); |
mverdy | 1:ecd6c7ea3545 | 479 | #if USE_MODEM_LORA == 1 |
mverdy | 1:ecd6c7ea3545 | 480 | *rssi = pkt_stat.Params.LoRa.RssiPkt; |
mverdy | 1:ecd6c7ea3545 | 481 | *snr = pkt_stat.Params.LoRa.SnrPkt; |
mverdy | 1:ecd6c7ea3545 | 482 | #else |
mverdy | 1:ecd6c7ea3545 | 483 | *rssi = pkt_stat.Params.Gfsk.RssiSync; |
mverdy | 1:ecd6c7ea3545 | 484 | #endif |
mverdy | 1:ecd6c7ea3545 | 485 | } |