nimita naik
/
SX1262PingPong
ping pong with modifiable parameters
main.cpp
- Committer:
- nimita23
- Date:
- 2019-06-19
- Revision:
- 3:c704345b0c9f
- Parent:
- 2:2bedac9faed3
- Child:
- 4:99bfe3b61a6a
File content as of revision 3:c704345b0c9f:
/* ______ _ / _____) _ | | ( (____ _____ ____ _| |_ _____ ____| |__ \____ \| ___ | (_ _) ___ |/ ___) _ \ _____) ) ____| | | || |_| ____( (___| | | | (______/|_____)_|_|_| \__)_____)\____)_| |_| (C)2017 Semtech Description: Main program Maintainer: Gregory Cristian & Gilbert Menth */ #include "mbed.h" #include "radio.h" #include "sx126x-hal.h" /*! * \brief Used to display firmware version on RS232 */ #define FIRMWARE_VERSION ( ( char* )"Firmware Version: 170314A" ) /*! * Use the Ping Ping in uncommented Mode */ //#define MODE_BLE #define MODE_LORA //#define MODE_GENERIC //#define MODE_FLRC /*! * \brief Defines the nominal frequency */ #define RF_FREQUENCY 350000000 // Hz /*! * \brief Defines the output power in dBm * * \remark The range of the output power is [-18..+13] dBm */ #define TX_OUTPUT_POWER 10 /*! * \brief Defines the states of the application */ typedef enum { APP_LOWPOWER, APP_RX, APP_RX_TIMEOUT, APP_RX_ERROR, APP_TX, APP_TX_TIMEOUT, }AppStates_t; /*! * \brief Defines the buffer size, i.e. the payload size */ #define BUFFER_SIZE 16 /*! * \brief Define the possible message type for this application */ const uint8_t PingMsg[] = "PING"; const uint8_t PongMsg[] = "PONG"; /*! * \brief Defines the size of the token defining message type in the payload */ #define PINGPONGSIZE 4 /*! * \brief The size of the buffer */ uint8_t BufferSize = BUFFER_SIZE; /*! * \brief The buffer */ uint8_t Buffer[BUFFER_SIZE]; /*! * \brief The State of the application */ AppStates_t AppState = APP_LOWPOWER; int8_t RssiValue = 0; int8_t SnrValue = 0; /*! * \brief Function to be executed on Radio Tx Done event */ void OnTxDone( void ); /*! * \brief Function to be executed on Radio Rx Done event */ void OnRxDone( void ); /*! * \brief Function executed on Radio Tx Timeout event */ void OnTxTimeout( void ); /*! * \brief Function executed on Radio Rx Timeout event */ void OnRxTimeout( void ); /*! * \brief Function executed on Radio Rx Error event */ void OnRxError( IrqErrorCode_t ); RadioCallbacks_t callbacks = { &OnTxDone, // txDone &OnRxDone, // rxDone NULL, // rxPreambleDetect NULL, // rxSyncWordDone NULL, // rxHeaderDone &OnTxTimeout, // txTimeout &OnRxTimeout, // rxTimeout &OnRxError, // rxError NULL, // cadDone }; // mosi, miso, sclk, nss, busy, dio1, dio2, dio3, rst, callbacks... SX126xHal Radio( D11, D12, D13, D7, D3, D5, NC, NC, A0, A1, A2, D8, &callbacks ); DigitalOut ANT_SW( A3 ); DigitalOut TxLed( A4 ); DigitalOut RxLed( A5 ); /*! * \brief Define IO for Unused Pin */ DigitalOut F_CS( D6 ); // MBED description of pin DigitalOut SD_CS( D8 ); // MBED description of pin /*! * \brief Number of tick size steps for tx timeout */ #define TX_TIMEOUT_VALUE 0xFFFF // ms /*! * \brief Number of tick size steps for rx timeout */ #define RX_TIMEOUT_VALUE 0xFFFF // ms /*! * \brief Mask of IRQs to listen to in rx mode */ uint16_t RxIrqMask = IRQ_RX_DONE | IRQ_CRC_ERROR | IRQ_RX_TX_TIMEOUT; /*! * \brief Mask of IRQs to listen to in tx mode */ uint16_t TxIrqMask = IRQ_TX_DONE | IRQ_RX_TX_TIMEOUT; /*! * \brief Locals parameters and status for radio API * NEED TO BE OPTIMIZED, COPY OF STUCTURE ALREADY EXISTING */ PacketParams_t PacketParams; PacketStatus_t PacketStatus; /*! * \brief Specify serial datarate for UART debug output */ void baud( int baudrate ) { Serial s( USBTX, USBRX ); s.baud( baudrate ); } int main( ) { bool isMaster = true; ModulationParams_t modulationParams; baud( 115200 ); F_CS = 1; SD_CS = 1; RxLed = 1; TxLed = 1; ANT_SW = 1; wait_ms( 500 ); // wait for on board DC/DC start-up time Radio.Init( ); Radio.SetRegulatorMode( USE_DCDC ); // Can also be set in LDO mode but consume more power memset( &Buffer, 0x00, BufferSize ); printf( "\n\n\r SX1262 Ping Pong Demo Application (%s)\n\n\r", "nimita" ); printf( "\nPing Pong running in LORA mode\n\r"); modulationParams.PacketType = PACKET_TYPE_LORA; modulationParams.Params.LoRa.SpreadingFactor = LORA_SF7; modulationParams.Params.LoRa.Bandwidth = LORA_BW_500; modulationParams.Params.LoRa.CodingRate = LORA_CR_4_5; PacketParams.PacketType = PACKET_TYPE_LORA; PacketParams.Params.LoRa.PreambleLength = 0x08; PacketParams.Params.LoRa.HeaderType = LORA_PACKET_VARIABLE_LENGTH; PacketParams.Params.LoRa.PayloadLength = 15; PacketParams.Params.LoRa.CrcMode = LORA_CRC_ON; PacketParams.Params.LoRa.InvertIQ = LORA_IQ_INVERTED; Radio.SetStandby( STDBY_RC ); Radio.SetPacketType( modulationParams.PacketType ); Radio.SetModulationParams( &modulationParams ); Radio.SetPacketParams( &PacketParams ); Radio.SetRfFrequency( RF_FREQUENCY ); Radio.SetBufferBaseAddresses( 0x00, 0x00 ); Radio.SetTxParams( TX_OUTPUT_POWER, RADIO_RAMP_20_US ); RxLed = 0; TxLed = 0; Radio.SetDioIrqParams( RxIrqMask, RxIrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE ); Radio.SetRx( RX_TIMEOUT_VALUE ); AppState = APP_LOWPOWER; Radio.ProcessIrqs( ); while( 1 ) { Radio.ProcessIrqs( ); switch( AppState ) { case APP_RX: AppState = APP_LOWPOWER; RxLed = !RxLed; Radio.GetPayload( Buffer, &BufferSize, BUFFER_SIZE ); printf("\n%s\n", Buffer); if( isMaster == true ) { if( BufferSize > 0 ) { if( strncmp( ( const char* )Buffer, ( const char* )PongMsg, PINGPONGSIZE ) == 0 ) { printf( "...Pong\r\n" ); memcpy( Buffer, PingMsg, PINGPONGSIZE ); Radio.SetDioIrqParams( TxIrqMask, TxIrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE ); Radio.SendPayload( Buffer, BufferSize, (TX_TIMEOUT_VALUE)); } else if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, PINGPONGSIZE ) == 0 ) { // A master already exists then become a slave printf( "...Ping\r\n" ); isMaster = false; memcpy( Buffer, PongMsg, PINGPONGSIZE ); Radio.SetDioIrqParams( TxIrqMask, TxIrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE ); Radio.SendPayload( Buffer, BufferSize, TX_TIMEOUT_VALUE ); } else // valid reception but neither a PING or a PONG message { // Set device as master ans start again isMaster = true; Radio.SetDioIrqParams( RxIrqMask, RxIrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE ); Radio.SetRx(RX_TIMEOUT_VALUE ); } } } else { if( BufferSize > 0 ) { if( strncmp( ( const char* )Buffer, ( const char* )PingMsg, PINGPONGSIZE ) == 0 ) { printf( "...Ping\r\n" ); memcpy( Buffer, PongMsg, 4 ); Radio.SetDioIrqParams( TxIrqMask, TxIrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE ); Radio.SendPayload( Buffer, BufferSize, TX_TIMEOUT_VALUE ); } else // valid reception but not a PING as expected { isMaster = true; Radio.SetDioIrqParams( RxIrqMask, RxIrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE ); Radio.SetRx(RX_TIMEOUT_VALUE); } } } break; case APP_TX: AppState = APP_LOWPOWER; TxLed = !TxLed; if( isMaster == true ) { printf( "Ping...\r\n" ); } else { printf( "Pong...\r\n" ); } Radio.SetDioIrqParams( RxIrqMask, RxIrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE ); Radio.SetRx(RX_TIMEOUT_VALUE ); break; case APP_RX_TIMEOUT: AppState = APP_LOWPOWER; if( isMaster == true ) { // Send the next PING frame memcpy( Buffer, PingMsg, PINGPONGSIZE ); Radio.SetDioIrqParams( TxIrqMask, TxIrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE ); Radio.SendPayload( Buffer, BufferSize, TX_TIMEOUT_VALUE ); } else { Radio.SetDioIrqParams( RxIrqMask, RxIrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE ); Radio.SetRx( RX_TIMEOUT_VALUE ); } break; case APP_RX_ERROR: AppState = APP_LOWPOWER; // We have received a Packet with a CRC error, send reply as if packet was correct if( isMaster == true ) { // Send the next PING frame memcpy( Buffer, PingMsg, PINGPONGSIZE ); Radio.SetDioIrqParams( TxIrqMask, TxIrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE ); Radio.SendPayload( Buffer, BufferSize, TX_TIMEOUT_VALUE); } else { // Send the next PONG frame memcpy( Buffer, PongMsg, PINGPONGSIZE ); Radio.SetDioIrqParams( TxIrqMask, TxIrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE ); Radio.SendPayload( Buffer, BufferSize, TX_TIMEOUT_VALUE ); } break; case APP_TX_TIMEOUT: AppState = APP_LOWPOWER; Radio.SetDioIrqParams( RxIrqMask, RxIrqMask, IRQ_RADIO_NONE, IRQ_RADIO_NONE ); Radio.SetRx( RX_TIMEOUT_VALUE ); break; case APP_LOWPOWER: break; default: // Set low power break; } } } void OnTxDone( void ) { AppState = APP_TX; } void OnRxDone( void ) { AppState = APP_RX; PacketStatus_t packetStatus; Radio.GetPacketStatus(&packetStatus); RssiValue = packetStatus.Params.LoRa.RssiPkt; SnrValue = packetStatus.Params.LoRa.SnrPkt; printf("rssi: %d; snr: %d\n\r", RssiValue, SnrValue ); } void OnTxTimeout( void ) { AppState = APP_TX_TIMEOUT; printf( "<>>>>>>>>TXE\r\n" ); } void OnRxTimeout( void ) { AppState = APP_RX_TIMEOUT; } void OnRxError( IrqErrorCode_t errorCode ) { AppState = APP_RX_ERROR; printf( "RXE<>>>>>>>>\r\n" ); } // //void OnRangingDone( IrqRangingCode_t val ) //{ //} void OnCadDone( bool channelActivityDetected ) { }