Added SX1276 getTemp() method.

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SX1272_LoRaRadio.h Source File

SX1272_LoRaRadio.h

00001 /**
00002  / _____)             _              | |
00003 ( (____  _____ ____ _| |_ _____  ____| |__
00004  \____ \| ___ |    (_   _) ___ |/ ___)  _ \
00005  _____) ) ____| | | || |_| ____( (___| | | |
00006 (______/|_____)_|_|_| \__)_____)\____)_| |_|
00007     (C)2013 Semtech
00008  ___ _____ _   ___ _  _____ ___  ___  ___ ___
00009 / __|_   _/_\ / __| |/ / __/ _ \| _ \/ __| __|
00010 \__ \ | |/ _ \ (__| ' <| _| (_) |   / (__| _|
00011 |___/ |_/_/ \_\___|_|\_\_| \___/|_|_\\___|___|
00012 embedded.connectivity.solutions===============
00013 
00014 Description: Radio driver for Semtech SX1272 radio. Implements LoRaRadio class.
00015 
00016 License: Revised BSD License, see LICENSE.TXT file include in the project
00017 
00018 Maintainer: Miguel Luis ( Semtech ), Gregory Cristian ( Semtech ) and Daniel Jaeckle ( STACKFORCE )
00019 
00020 
00021 Copyright (c) 2017, Arm Limited and affiliates.
00022 
00023 SPDX-License-Identifier: BSD-3-Clause
00024 */
00025 
00026 #ifndef SX1272_LORARADIO_H_
00027 #define SX1272_LORARADIO_H_
00028 
00029 #include "PinNames.h"
00030 #include "InterruptIn.h"
00031 #include "DigitalOut.h"
00032 #include "DigitalInOut.h"
00033 #include "SPI.h"
00034 #include "Timeout.h"
00035 #include "platform/PlatformMutex.h"
00036 #ifdef MBED_CONF_RTOS_PRESENT
00037  #include "rtos/Thread.h"
00038 #endif
00039 
00040 #include "lorawan/LoRaRadio.h"
00041 
00042 #ifdef MBED_CONF_SX1272_LORA_DRIVER_BUFFER_SIZE
00043 #define MAX_DATA_BUFFER_SIZE_SX172                        MBED_CONF_SX1272_LORA_DRIVER_BUFFER_SIZE
00044 #else
00045 #define MAX_DATA_BUFFER_SIZE_SX172                        255
00046 #endif
00047 
00048 /**
00049  * Radio driver implementation for Semtech SX1272 plus variants.
00050  * Supports only SPI at the moment. Implements pure virtual LoRaRadio class.
00051  */
00052 class SX1272_LoRaRadio: public LoRaRadio {
00053 public:
00054     /**
00055      * Use this constructor if pin definitions are provided manually.
00056      * The pins that are marked NC are optional. It is assumed that these
00057      * pins are not connected until/unless configured otherwise.
00058      */
00059     SX1272_LoRaRadio(PinName mosi,
00060                      PinName miso,
00061                      PinName sclk,
00062                      PinName nss,
00063                      PinName reset,
00064                      PinName dio0,
00065                      PinName dio1,
00066                      PinName dio2,
00067                      PinName dio3,
00068                      PinName dio4,
00069                      PinName dio5,
00070                      PinName rf_switch_ctl1 = NC,
00071                      PinName rf_switch_ctl2 = NC,
00072                      PinName txctl = NC,
00073                      PinName rxctl = NC,
00074                      PinName ant_switch = NC,
00075                      PinName pwr_amp_ctl = NC,
00076                      PinName tcxo = NC);
00077 
00078     /**
00079      * Destructor
00080      */
00081     virtual ~SX1272_LoRaRadio();
00082 
00083     /**
00084      * Registers radio events with the Mbed LoRaWAN stack and
00085      * undergoes initialization steps if any
00086      *
00087      *  @param events Structure containing the driver callback functions
00088      */
00089     virtual void init_radio(radio_events_t *events);
00090 
00091     /**
00092      * Resets the radio module
00093      */
00094     virtual void radio_reset();
00095 
00096     /**
00097      *  Put the RF module in sleep mode
00098      */
00099     virtual void sleep(void);
00100 
00101     /**
00102      *  Sets the radio in standby mode
00103      */
00104     virtual void standby(void);
00105 
00106     /**
00107      *  Sets the reception parameters
00108      *
00109      *  @param modem         Radio modem to be used [0: FSK, 1: LoRa]
00110      *  @param bandwidth     Sets the bandwidth
00111      *                          FSK : >= 2600 and <= 250000 Hz
00112      *                          LoRa: [0: 125 kHz, 1: 250 kHz,
00113      *                                 2: 500 kHz, 3: Reserved]
00114      *  @param datarate      Sets the Datarate
00115      *                          FSK : 600..300000 bits/s
00116      *                          LoRa: [6: 64, 7: 128, 8: 256, 9: 512,
00117      *                                10: 1024, 11: 2048, 12: 4096  chips]
00118      *  @param coderate      Sets the coding rate ( LoRa only )
00119      *                          FSK : N/A ( set to 0 )
00120      *                          LoRa: [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
00121      *  @param bandwidth_afc Sets the AFC Bandwidth ( FSK only )
00122      *                          FSK : >= 2600 and <= 250000 Hz
00123      *                          LoRa: N/A ( set to 0 )
00124      *  @param preamble_len  Sets the Preamble length ( LoRa only )
00125      *                          FSK : N/A ( set to 0 )
00126      *                          LoRa: Length in symbols ( the hardware adds 4 more symbols )
00127      *  @param symb_timeout  Sets the RxSingle timeout value
00128      *                          FSK : timeout number of bytes
00129      *                          LoRa: timeout in symbols
00130      *  @param fixLen        Fixed length packets [0: variable, 1: fixed]
00131      *  @param payload_len   Sets payload length when fixed lenght is used
00132      *  @param crc_on        Enables/Disables the CRC [0: OFF, 1: ON]
00133      *  @param freq_hop_on   Enables disables the intra-packet frequency hopping  [0: OFF, 1: ON] (LoRa only)
00134      *  @param hop_period    Number of symbols bewteen each hop (LoRa only)
00135      *  @param iq_inverted   Inverts IQ signals ( LoRa only )
00136      *                          FSK : N/A ( set to 0 )
00137      *                          LoRa: [0: not inverted, 1: inverted]
00138      *  @param rx_continuous Sets the reception in continuous mode
00139      *                          [false: single mode, true: continuous mode]
00140      */
00141     virtual void set_rx_config (radio_modems_t modem, uint32_t bandwidth,
00142                                uint32_t datarate, uint8_t coderate,
00143                                uint32_t bandwidth_afc, uint16_t preamble_len,
00144                                uint16_t symb_timeout, bool fix_len,
00145                                uint8_t payload_len,
00146                                bool crc_on, bool freq_hop_on, uint8_t hop_period,
00147                                bool iq_inverted, bool rx_continuous);
00148 
00149     /**
00150      *  Sets the transmission parameters
00151      *
00152      *  @param modem         Radio modem to be used [0: FSK, 1: LoRa]
00153      *  @param power         Sets the output power [dBm]
00154      *  @param fdev          Sets the frequency deviation ( FSK only )
00155      *                          FSK : [Hz]
00156      *                          LoRa: 0
00157      *  @param bandwidth     Sets the bandwidth ( LoRa only )
00158      *                          FSK : 0
00159      *                          LoRa: [0: 125 kHz, 1: 250 kHz,
00160      *                                 2: 500 kHz, 3: Reserved]
00161      *  @param datarate      Sets the Datarate
00162      *                          FSK : 600..300000 bits/s
00163      *                          LoRa: [6: 64, 7: 128, 8: 256, 9: 512,
00164      *                                10: 1024, 11: 2048, 12: 4096  chips]
00165      *  @param coderate      Sets the coding rate ( LoRa only )
00166      *                          FSK : N/A ( set to 0 )
00167      *                          LoRa: [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
00168      *  @param preamble_len  Sets the preamble length
00169      *  @param fix_len       Fixed length packets [0: variable, 1: fixed]
00170      *  @param crc_on        Enables disables the CRC [0: OFF, 1: ON]
00171      *  @param freq_hop_on   Enables disables the intra-packet frequency hopping  [0: OFF, 1: ON] (LoRa only)
00172      *  @param hop_period    Number of symbols bewteen each hop (LoRa only)
00173      *  @param iq_inverted   Inverts IQ signals ( LoRa only )
00174      *                          FSK : N/A ( set to 0 )
00175      *                          LoRa: [0: not inverted, 1: inverted]
00176      *  @param timeout       Transmission timeout [ms]
00177      */
00178     virtual void set_tx_config(radio_modems_t modem, int8_t power, uint32_t fdev,
00179                               uint32_t bandwidth, uint32_t datarate,
00180                               uint8_t coderate, uint16_t preamble_len,
00181                               bool fix_len, bool crc_on, bool freq_hop_on,
00182                               uint8_t hop_period, bool iq_inverted, uint32_t timeout);
00183 
00184     /**
00185      *  Sends the buffer of size
00186      *
00187      *  Prepares the packet to be sent and sets the radio in transmission
00188      *
00189      *  @param buffer        Buffer pointer
00190      *  @param size          Buffer size
00191      */
00192     virtual void send(uint8_t *buffer, uint8_t size);
00193 
00194     /**
00195      * For backwards compatibility
00196      */
00197     virtual void receive(uint32_t timeout)
00198     {
00199         (void) timeout;
00200         receive();
00201     }
00202 
00203     /**
00204      * Sets the radio to receive
00205      *
00206      * All necessary configuration options for receptions are set in
00207      * 'set_rx_config(parameters)' API.
00208      */
00209     virtual void receive(void);
00210 
00211     /**
00212      *  Sets the carrier frequency
00213      *
00214      *  @param freq          Channel RF frequency
00215      */
00216     virtual void set_channel(uint32_t freq);
00217 
00218     /**
00219      *  Generates a 32 bits random value based on the RSSI readings
00220      *
00221      *  Remark this function sets the radio in LoRa modem mode and disables
00222      *         all interrupts.
00223      *         After calling this function either Radio.SetRxConfig or
00224      *         Radio.SetTxConfig functions must be called.
00225      *
00226      *  @return             32 bits random value
00227      */
00228     virtual uint32_t random(void);
00229 
00230     /**
00231      *  Get radio status
00232      *
00233      *  @param status        Radio status [RF_IDLE, RF_RX_RUNNING, RF_TX_RUNNING]
00234      *  @return              Return current radio status
00235      */
00236     virtual uint8_t get_status(void);
00237 
00238     /**
00239      *  Sets the maximum payload length
00240      *
00241      *  @param modem         Radio modem to be used [0: FSK, 1: LoRa]
00242      *  @param max           Maximum payload length in bytes
00243      */
00244     virtual void set_max_payload_length(radio_modems_t modem, uint8_t max);
00245 
00246     /**
00247      *  Sets the network to public or private
00248      *
00249      *  Updates the sync byte. Applies to LoRa modem only
00250      *
00251      *  @param enable        if true, it enables a public network
00252      */
00253     virtual void set_public_network(bool enable);
00254 
00255     /**
00256      *  Computes the packet time on air for the given payload
00257      *
00258      *  Remark can only be called once SetRxConfig or SetTxConfig have been called
00259      *
00260      *  @param modem         Radio modem to be used [0: FSK, 1: LoRa]
00261      *  @param pkt_len       Packet payload length
00262      *  @return              Computed airTime for the given packet payload length
00263      */
00264     virtual uint32_t time_on_air(radio_modems_t modem, uint8_t pkt_len);
00265 
00266     /**
00267      * Perform carrier sensing
00268      *
00269      * Checks for a certain time if the RSSI is above a given threshold.
00270      * This threshold determines if there is already a transmission going on
00271      * in the channel or not.
00272      *
00273      * @param modem                     Type of the radio modem
00274      * @param freq                      Carrier frequency
00275      * @param rssi_threshold            Threshold value of RSSI
00276      * @param max_carrier_sense_time    time to sense the channel
00277      *
00278      * @return                          true if there is no active transmission
00279      *                                  in the channel, false otherwise
00280      */
00281     virtual bool perform_carrier_sense(radio_modems_t modem,
00282                                        uint32_t freq,
00283                                        int16_t rssi_threshold,
00284                                        uint32_t max_carrier_sense_time);
00285 
00286     /**
00287      *  Sets the radio in CAD mode
00288      *
00289      */
00290     virtual void start_cad(void);
00291 
00292     /**
00293      *  Check if the given RF is in range
00294      *
00295      *  @param frequency       frequency needed to be checked
00296      */
00297     virtual bool check_rf_frequency(uint32_t frequency);
00298 
00299     /** Sets the radio in continuous wave transmission mode
00300      *
00301      *  @param freq          Channel RF frequency
00302      *  @param power         Sets the output power [dBm]
00303      *  @param time          Transmission mode timeout [s]
00304      */
00305     virtual void set_tx_continuous_wave(uint32_t freq, int8_t power, uint16_t time);
00306 
00307     /**
00308      * Acquire exclusive access
00309      */
00310     virtual void lock(void);
00311 
00312     /**
00313      * Release exclusive access
00314      */
00315     virtual void unlock(void);
00316 
00317 private:
00318 
00319     // SPI and chip select control
00320     mbed::SPI _spi;
00321     mbed::DigitalOut _chip_select;
00322 
00323     // module rest control
00324     mbed::DigitalInOut _reset_ctl;
00325 
00326     // Interrupt controls
00327     mbed::InterruptIn _dio0_ctl;
00328     mbed::InterruptIn _dio1_ctl;
00329     mbed::InterruptIn _dio2_ctl;
00330     mbed::InterruptIn _dio3_ctl;
00331     mbed::InterruptIn _dio4_ctl;
00332     mbed::InterruptIn _dio5_ctl;
00333 
00334     // Radio specific controls
00335     mbed::DigitalOut _rf_switch_ctl1;
00336     mbed::DigitalOut _rf_switch_ctl2;
00337     mbed::DigitalOut _txctl;
00338     mbed::DigitalOut _rxctl;
00339     mbed::DigitalInOut _ant_switch;
00340     mbed::DigitalOut _pwr_amp_ctl;
00341     mbed::DigitalOut _tcxo;
00342 
00343     // Contains all RF control pin names
00344     // This storage is needed even after assigning the
00345     // pins to corresponding object, as the driver needs to know
00346     // which control pins are connected and which are not. This
00347     // variation is inherent to driver because of target configuration.
00348     rf_ctrls _rf_ctrls;
00349 
00350     // We need these PinNames as not all modules have those connected
00351     PinName _dio4_pin;
00352     PinName _dio5_pin;
00353 
00354     // Structure containing all user and network specified settings
00355     // for radio module
00356     radio_settings_t _rf_settings;
00357 
00358     // Structure containing function pointers to the stack callbacks
00359     radio_events_t *_radio_events;
00360 
00361     // Data buffer used for both TX and RX
00362     // Size of this buffer is configurable via Mbed config system
00363     // Default is 256 bytes
00364     uint8_t _data_buffer[MAX_DATA_BUFFER_SIZE_SX172];
00365 
00366     // TX timer in ms. This timer is used as a fail safe for TX.
00367     // If the chip fails to transmit, its a fatal error, reflecting
00368     // some catastrophic bus failure etc. We wish to have the control
00369     // back from the driver in such a case.
00370     mbed::Timeout tx_timeout_timer;
00371 
00372 #ifdef MBED_CONF_RTOS_PRESENT
00373     // Thread to handle interrupts
00374     rtos::Thread irq_thread;
00375 #endif
00376 
00377     // Access protection
00378     PlatformMutex mutex;
00379 
00380     uint8_t radio_variant;
00381 
00382     /**
00383      * Flag used to set the RF switch control pins in low power mode when the radio is not active.
00384      */
00385     bool radio_is_active;
00386 
00387     // helper functions
00388     void setup_registers();
00389     void default_antenna_switch_ctrls();
00390     void set_antenna_switch(uint8_t operation_mode);
00391     void setup_spi();
00392     void gpio_init();
00393     void gpio_deinit();
00394     void setup_interrupts();
00395     void set_modem(uint8_t modem);
00396     void set_operation_mode(uint8_t mode);
00397     void set_low_power_mode(bool status);
00398     void set_sx1272_variant_type();
00399     uint8_t get_pa_conf_reg();
00400     void set_rf_tx_power(int8_t power);
00401     int16_t get_rssi(radio_modems_t modem);
00402     uint8_t get_fsk_bw_reg_val(uint32_t bandwidth);
00403     void write_to_register(uint8_t addr, uint8_t data);
00404     void write_to_register(uint8_t addr, uint8_t *data, uint8_t size);
00405     uint8_t read_register(uint8_t addr);
00406     void read_register(uint8_t addr, uint8_t *buffer, uint8_t size);
00407     void write_fifo(uint8_t *buffer, uint8_t size);
00408     void read_fifo(uint8_t *buffer, uint8_t size);
00409     void transmit(uint32_t timeout);
00410     void rf_irq_task(void);
00411 
00412     // ISRs
00413     void  dio0_irq_isr();
00414     void  dio1_irq_isr();
00415     void  dio2_irq_isr();
00416     void  dio3_irq_isr();
00417     void  dio4_irq_isr();
00418     void  dio5_irq_isr();
00419     void  timeout_irq_isr();
00420 
00421     // Handlers called by thread in response to signal
00422     void handle_dio0_irq();
00423     void handle_dio1_irq();
00424     void handle_dio2_irq();
00425     void handle_dio3_irq();
00426     void handle_dio4_irq();
00427     void handle_dio5_irq();
00428     void handle_timeout_irq();
00429 };
00430 
00431 #endif /* SX1272_LORARADIO_H_ */