Advertisement
Guest User

RecieveTransmit

a guest
Feb 28th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.46 KB | None | 0 0
  1. #include "mbed.h"
  2. #include "main.h"
  3. #include "sx1272-hal.h"
  4. #include "debug.h"
  5. #include "SDFileSystem.h" // include SD filesystem for SDHC slot
  6.  
  7. /* Set this flag to '1' to display debug messages on the console */
  8. #define DEBUG_MESSAGE   1
  9.  
  10.  
  11. #define RF_FREQUENCY                                868300000 // Center EU frequencyHz
  12. #define TX_OUTPUT_POWER                                 14        // 14 dBm
  13.  
  14. #define LORA_BANDWIDTH                              0        
  15. // [0: 125 kHz,
  16. //  1: 250 kHz,
  17. //  2: 500 kHz,
  18. //  3: Reserved]
  19.  
  20. #define LORA_SPREADING_FACTOR                       7         // [SF7..SF12]
  21. #define LORA_CODINGRATE                             1         // [1: 4/5,
  22. //  2: 4/6,
  23. //  3: 4/7,
  24. //  4: 4/8]
  25.  
  26. #define LORA_PREAMBLE_LENGTH                        8
  27. #define LORA_SYMBOL_TIMEOUT                         5         // Symbols
  28. #define LORA_FIX_LENGTH_PAYLOAD_ON                  false
  29. #define LORA_FHSS_ENABLED                           false
  30. #define LORA_NB_SYMB_HOP                            4
  31. #define LORA_IQ_INVERSION_ON                        false
  32. #define LORA_CRC_ENABLED                            true
  33.  
  34. #define RX_TIMEOUT_VALUE                            3500000   // in us
  35. #define BUFFER_SIZE                                 48        // Define the payload size here
  36.  
  37. DigitalOut led(LED1);
  38.  
  39. /*
  40.  *  Global variables declarations
  41.  */
  42. typedef enum {
  43.     LOWPOWER = 0,
  44.     IDLE,
  45.    
  46.     RX,
  47.     RX_TIMEOUT,
  48.     RX_ERROR,
  49.    
  50.     TX,
  51.     TX_TIMEOUT,
  52.  
  53.     CAD,
  54.     CAD_DONE
  55. } AppStates_t;
  56.  
  57. volatile AppStates_t State = LOWPOWER;
  58.  
  59. /*!
  60.  * Radio events function pointer
  61.  */
  62. static RadioEvents_t RadioEvents;
  63.  
  64. /*
  65.  *  Global variables declarations
  66.  */
  67. SX1272MB2xAS Radio( NULL );
  68.  
  69. uint16_t BufferSize = 0;
  70. uint8_t Buffer[BUFFER_SIZE];
  71.  
  72. int16_t RssiValue = 0.0;
  73. int8_t SnrValue = 0.0;
  74.  
  75. SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS PINLAYOUT FOR SD CARD
  76.  
  77. int main()
  78. {
  79.     debug( "\n\n\r W14008354 - LoRa Receiver \n\n\r" );
  80.    
  81.     // Initialize Radio driver
  82.     RadioEvents.TxDone = OnTxDone;
  83.     RadioEvents.RxDone = OnRxDone;
  84.     RadioEvents.RxError = OnRxError;
  85.     RadioEvents.TxTimeout = OnTxTimeout;
  86.     RadioEvents.RxTimeout = OnRxTimeout;
  87.     Radio.Init( &RadioEvents );
  88.  
  89.     // verify the connection with the board
  90.     while( Radio.Read( REG_VERSION ) == 0x00  ) {
  91.         debug( "Radio could not be detected!\n\r", NULL );
  92.         wait( 1 );
  93.     }
  94.  
  95.     debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1272MB2XAS ) ) , "\n\r > Board Type: SX1272MB2xAS < \n\r" );
  96.  
  97.     Radio.SetChannel( RF_FREQUENCY );
  98.  
  99.     debug_if( LORA_FHSS_ENABLED, "\n\n\r             > LORA FHSS Mode < \n\n\r");
  100.     debug_if( !LORA_FHSS_ENABLED, "\n\n\r             > LORA Mode < \n\n\r");
  101.    
  102.     Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
  103.                          LORA_SPREADING_FACTOR, LORA_CODINGRATE,
  104.                          LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
  105.                          LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
  106.                          LORA_IQ_INVERSION_ON, 2000 );
  107.                          
  108.     Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
  109.                        LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
  110.                        LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0,
  111.                        LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
  112.                        LORA_IQ_INVERSION_ON, true );
  113.  
  114.     debug_if( DEBUG_MESSAGE, "Starting listening loop\r\n\r\n" );
  115.  
  116.     led = 0;
  117.     Radio.Rx( RX_TIMEOUT_VALUE );
  118.  
  119.     while( 1 ) {
  120.         switch( State ) {
  121.             case RX: // If RX case is true then the following happens if not then moves onto next case which is RX_Timeout
  122.                 if( BufferSize > 0 ) {
  123.                     debug_if( DEBUG_MESSAGE, "\r\n========\r\nRX Recieved Packet\r\n========\r\n" );
  124.                     for(int i = 0; i < BufferSize; i++) {
  125.                         debug_if( DEBUG_MESSAGE, "%02x", Buffer[i]);     // was missing leading zeros so used %02x to enforce 2 characters for each byte
  126.                     } // for loop which prints buffer until it hits buffersize which is currently 48
  127.                     retrieve_data( Buffer ); // Runs data retrieval function which will later contain packet manipulation code
  128.                 }
  129.                 debug_if( DEBUG_MESSAGE, "\r\n========\r\nTX Radio.Send\r\n========\r\n" );
  130.                 wait_ms( 10 ); // added a 10ms wait before radio send ( seems standard in semtech programs )
  131.                 Radio.Send( Buffer, BufferSize ); // radio send buffer
  132.                 BufferSize = 0;
  133.                 wait_ms( 100 ); // added a 100ms wait to see if RX is affecting TX
  134.                 Radio.Rx( RX_TIMEOUT_VALUE ); // Sets radio in RX mode MAY BE CAUSING ISSUE
  135.                 State = LOWPOWER;
  136.                 break;
  137.             case TX:
  138.                 led = !led;
  139.                 debug( "TX CASE DEBUG\r\n" );
  140.             Radio.Rx( RX_TIMEOUT_VALUE );
  141.             State = LOWPOWER;
  142.             break;
  143.             case RX_TIMEOUT:
  144.                 Radio.Rx( RX_TIMEOUT_VALUE );
  145.                 State = LOWPOWER;
  146.                 break;
  147.             case RX_ERROR:
  148.                 // We have received a Packet with a CRC error
  149.                 Radio.Rx( RX_TIMEOUT_VALUE );
  150.                 State = LOWPOWER;
  151.                 break;
  152.             case LOWPOWER:
  153.                 break;
  154.             default:
  155.                 State = LOWPOWER;
  156.                 break;
  157.         }
  158.     }
  159. }
  160.  
  161. void OnTxDone( void )
  162. {
  163.     Radio.Sleep( );
  164.     State = TX;
  165.     debug_if( DEBUG_MESSAGE, "> OnTxDone\n\r" );
  166. }
  167.  
  168. void OnTxTimeout( void )
  169. {
  170.     Radio.Sleep( );
  171.     State = TX_TIMEOUT;
  172.     debug_if( DEBUG_MESSAGE, "> OnTxTimeout\n\r" );
  173. }
  174.  
  175. void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr)
  176. {
  177.     Radio.Sleep( );
  178.     BufferSize = size;
  179.     memcpy( Buffer, payload, BufferSize );
  180.     RssiValue = rssi;
  181.     SnrValue = snr;
  182.     State = RX;
  183.     debug_if( DEBUG_MESSAGE, "> OnRxDone %d \n\r", RssiValue );
  184. }
  185.  
  186. void OnRxTimeout( void )
  187. {
  188.     Radio.Sleep( );
  189.     BufferSize = 0;
  190.     State = RX_TIMEOUT;
  191.     debug_if( DEBUG_MESSAGE, "> OnRxTimeout\n\r" );
  192. }
  193.  
  194. void OnRxError( void )
  195. {
  196.     Radio.Sleep( );
  197.     State = RX_ERROR;
  198.     debug_if( DEBUG_MESSAGE, "> OnRxError\n\r" );
  199. }
  200.  
  201. void retrieve_data(uint8_t * payload) // Data retrieval fucntion for later manipulation
  202. {
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement