recepteur.cpp 5.13 KB
////////////////////////////////////////////////////////////////////////////////
/*RECEPTEUR LoRa
par : Robin Cavalieri
      IMA5SC
PFE 2018 */
////////////////////////////////////////////////////////////////////////////////
//Headers
#include "mbed.h"
#include "main.h"
#include "sx1276-hal.h"
#include "debug.h"
////////////////////////////////////////////////////////////////////////////////
#define DEBUG_MESSAGE   1
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
//Modulation LoRa active
#define USE_MODEM_LORA  1
#define USE_MODEM_FSK   !USE_MODEM_LORA
//Paramètres
#define RF_FREQUENCY 868000000 //Hz : Fréquence de travail pour UE
#define TX_OUTPUT_POWER 14     //dBm : puissance de sortie du signal
#define LORA_BANDWIDTH 1         // 0:125 kHz de BP
#define LORA_SPREADING_FACTOR 10 // [SF7..SF12] Portée du débit. grande portée inutile
#define LORA_CODINGRATE 1
#define LORA_PREAMBLE_LENGTH 8   // Same for Tx and Rx
#define LORA_SYMBOL_TIMEOUT 5    // Symbols
#define LORA_FIX_LENGTH_PAYLOAD_ON false
#define LORA_FHSS_ENABLED true
#define LORA_NB_SYMB_HOP 4
#define LORA_IQ_INVERSION_ON false
#define LORA_CRC_ENABLED true
#define RX_TIMEOUT_VALUE 0      // in ms
#define BUFFER_SIZE 32        // Define the payload size here
#define TRAME_SIZE 13
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
//Variables globales
typedef enum
{
    LOWPOWER = 0,
    IDLE,
    RX,
    RX_TIMEOUT,
    RX_ERROR,
    CAD,
    CAD_DONE
}AppStates_t;

volatile AppStates_t State = LOWPOWER;
static RadioEvents_t RadioEvents;
SX1276MB1xAS Radio( NULL );
uint16_t BufferSize = BUFFER_SIZE;
uint8_t Buffer[BUFFER_SIZE];
int16_t RssiValue = 0.0;
int8_t SnrValue = 0.0;
uint8_t receptMsg[13];
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
//Main réception
int main( void )
{
    debug("\nRecepteur SX1276 \n\n\r");

    // Initialize Radio driver
    RadioEvents.RxDone = OnRxDone;
    RadioEvents.RxError = OnRxError;
    RadioEvents.RxTimeout = OnRxTimeout;
    RadioEvents.FhssChangeChannel = OnFhssChangeChannel;
    Radio.Init(&RadioEvents);

    //Véification de la communication avec la board
    while(Radio.Read(REG_VERSION) == 0x00)
    {
        debug("Aucune radio detectee\n\r", NULL);
        wait(1);
    }

    //Checking du matériel connecté
    debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1276MB1LAS ) ), "\n\r Materiel detecte : SX1276MB1LAS \n\r" );
    debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1276MB1MAS ) ), "\n\r Materiel detecte : SX1276MB1MAS \n\r" );

    //Paramétrage du canal
    Radio.SetChannel( HoppingFrequencies[0] );

    #if USE_MODEM_LORA == 1

        debug_if( LORA_FHSS_ENABLED, "\n\n\r             > LORA FHSS Mode < \n\n\r" );
        debug_if( !LORA_FHSS_ENABLED, "\n\n\r             > LORA Mode < \n\n\r" );

        Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
                             LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
                             LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0,
                             LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
                             LORA_IQ_INVERSION_ON, true );

    #else
        #error "Aucun modem defini"
    #endif

        debug_if(DEBUG_MESSAGE, "Reception en cours...\r\n");
        Radio.Rx(RX_TIMEOUT_VALUE); //Reception mode en continu

        while(1)
        {
            if( BufferSize > 0 )
            {
                if( strncmp( ( const char* )Buffer, ( const char* )receptMsg, TRAME_SIZE ) == 0 )
                {
                    debug( "Trame recue : %s\r\n",(char*)Buffer);
                }
                else
                {
                    debug("Attente d'une trame...\n");
                }
                wait_ms(1000);
            }
        }
}
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
//Fonctions utiles
void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr )
{
    Radio.SetChannel( HoppingFrequencies[0] );
    Radio.Sleep( );
    BufferSize = size;
    memcpy( Buffer, payload, BufferSize );
    RssiValue = rssi;
    SnrValue = snr;
    State = RX;
    debug_if( DEBUG_MESSAGE, "> OnRxDone\n\r" );
}

void OnRxTimeout( void )
{
    Radio.SetChannel( HoppingFrequencies[0] );
    Radio.Sleep( );
    Buffer[BufferSize] = 0;
    State = RX_TIMEOUT;
    debug_if( DEBUG_MESSAGE, "> OnRxTimeout\n\r" );
}

void OnRxError( void )
{
    Radio.SetChannel( HoppingFrequencies[0] );
    Radio.Sleep( );
    State = RX_ERROR;
    debug_if( DEBUG_MESSAGE, "> OnRxError\n\r" );
}

void OnFhssChangeChannel( uint8_t channelIndex )
{
    Radio.SetChannel( HoppingFrequencies[channelIndex] );
    debug_if( DEBUG_MESSAGE, "F%d-", channelIndex );
}
////////////////////////////////////////////////////////////////////////////////