emetteur.cpp 4.63 KB
////////////////////////////////////////////////////////////////////////////////
//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 3500      // in ms
#define BUFFER_SIZE 32        // Define the payload size here
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
//Variables globales
typedef enum
{
    LOWPOWER = 0,
    IDLE,
    RX,
    RX_TIMEOUT,
    RX_ERROR,
    TX,
    TX_TIMEOUT,
    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;
const uint8_t TestMsg[] = "TEST";
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
//Fonctions utiles

//Debug pour savoir si le message a été transmis
void OnTxDone( void )
{
    Radio.SetChannel( HoppingFrequencies[0] );
    Radio.Sleep( );
    State = TX;
    debug_if( DEBUG_MESSAGE, "Envoye\n\r" );
}

//Time out de transmission. Échec de l'envoi
void OnTxTimeout( void )
{
    Radio.SetChannel(HoppingFrequencies[0]);
    Radio.Sleep( );
    State = TX_TIMEOUT;
    debug_if( DEBUG_MESSAGE, "Echec de l'envoi\n\r" );
}

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

////////////////////////////////////////////////////////////////////////////////
//Main emission
int main( void )
{
    uint8_t i;
    debug("\n\n\rEmetteur SX1276\n\n\r");

    // Initialize Radio driver
    RadioEvents.TxDone = OnTxDone;
    RadioEvents.TxTimeout = OnTxTimeout;
    RadioEvents.FhssChangeChannel = OnFhssChangeChannel;
    Radio.Init(&RadioEvents);

    //Véification de la connexion avec la carte
    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\rMateriel detecte : SX1276MB1LAS < \n\r" );
    debug_if( ( DEBUG_MESSAGE & ( Radio.DetectBoardType( ) == SX1276MB1MAS ) ), "\n\rMateriel detecte : SX1276MB1MAS < \n\r" );

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

    #if USE_MODEM_LORA == 1

        debug_if( LORA_FHSS_ENABLED, "\n\n\rLORA FHSS Mode\n\n\r" );
        debug_if( !LORA_FHSS_ENABLED, "\n\n\rLORA Mode\n\n\r" );

        //Configuration du modem transmission
        Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
                             LORA_SPREADING_FACTOR, LORA_CODINGRATE,
                             LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
                             LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
                             LORA_IQ_INVERSION_ON, 4000 );
    #else
        #error "Aucun modem defini"
    #endif

    debug_if( DEBUG_MESSAGE, "Procedure d'emission lancee\r\n" );

    //Boucle d'émission infinie
    while(1)
    {
       debug( "Tentative d'envoi :\r\n" );
       strcpy((char*)Buffer, (char*)TestMsg);
        // We fill the buffer with numbers for the payload
       for(i=4; i<BufferSize; i++)
       {
            Buffer[i] = i - 4;
       }
       debug( "Message envoye\r\n" );
       wait_ms(1000);
       Radio.Send(Buffer, BufferSize);
    }
}