// *********************************************************************
// *                        E14-CONSUMPTION                            *
// *               Power consumption in various TR modes               *
// *********************************************************************
// Testing of power consumption in various operating modes.
//
// Intended for:
//   HW: - TR-72G + DK-EVAL-04x or TR-76G + DK-EVAL-04A
//
//   OS: - v4.06G
//
// Description:
//   - Consumption can be measured by connecting a meter instead of the jumper.
//   - The consumption of DK-EVAL-04x itself must be subtracted from the measured value
//
// ---------------------------------------------------------------------
// Online IQRF OS Reference Guide: https://doc.iqrf.org/IQRF-OS-Reference-guide
//
// Copyright (c) IQRF Tech s.r.o.
//
// File:    E14-CONSUMPTION.c
// Version: v3.00                                   Revision: 17/06/2021
//
// Revision history:
//   v3.00: 17/06/2021  First release for TR-7xG.
//
// *********************************************************************
// Default IQRF include (modify the path according to your setup)
#include "IQRF.h"

// *********************************************************************
#define DELAY       1000        // 1000 ticks => 10 s
//#define DEEP_SLEEP

// *********************************************************************
void APPLICATION()
{ 
// To minimize the power consumption, no MCU pin must be left as a digital input without defined input level value.
//   So, unused pins in given hardware should be set as outputs:
#if defined TR72G               // Generated by IQRF IDE according to the TR selection in a project
                                // TR pin C1 (MCU pin RA0): should be set as an output
    LATA.0 = 0;                     // Low level
    TRISA.0 = 0;                    // Output
    
                                // TR pin C2 (MCU pin RC2): should be set as an output
    LATC.2 = 0;                     // Low level
    TRISC.2 = 0;                    // Output

                                // TR pin C5 (MCU pins RA5, RB4 and RC6 connected in parallel):
                                //   All MCU pins can be set as an input, but pin RB4 must be configured with internal pull-up (default activated).
    TRISA.5 = 1;                    // Input
    TRISB.4 = 1;                    // Input
    TRISC.6 = 1;                    // Input
    
                                // TR pin C6 (MCU pin RC3): should be set as an output
    LATC.3 = 0;                     // Low level
    TRISC.3 = 0;                    // Output

                                // TR pin C7 (MCU pin RC4): should be set as an output
    LATC.4 = 0;                     // Low level
    TRISC.4 = 0;                    // Output

                                // TR pin C8 (MCU pins RC5 and RC7 connected in parallel):
                                //   Only one MCU pin should be set as an output
    LATC.5 = 0;                     // Low level
    TRISC.5 = 0;                    // Output
    TRISC.7 = 1;                    // Input
    
    pulseLEDR();
#elif defined TR76G
                                // TR pin Q14 (MCU pin RA0): should be set as an output
    LATA.0 = 0;                     // Low level
    TRISA.0 = 0;                    // Output
    
                                // TR pin Q15 (MCU pin RC2): should be set as an output
    LATC.2 = 0;                     // Low level
    TRISC.2 = 0;                    // Output
    
                                // TR pin Q4 (MCU pin RC6): should be set as an output
    LATC.6 = 0;                     // Low level
    TRISC.6 = 0;                    // Output
    
                                // TR pin Q5 (MCU pin RC7): should be set as an output
    LATC.7 = 0;                     // Low level
    TRISC.7 = 0;                    // Output

                                // TR pin Q6 (MCU pin RC3): should be set as an output
    LATC.3 = 0;                     // Low level
    TRISC.3 = 0;                    // Output
    
                                // TR pin Q7 (MCU pin RC4): should be set as an output
    LATC.4 = 0;                     // Low level
    TRISC.4 = 0;                    // Output
    
                                // TR pin Q8 (MCU pin RC5): should be set as an output
    LATC.5 = 0;                     // Low level
    TRISC.5 = 0;                    // Output
    
                                // TR pin Q9 (MCU pin RA5): should be set as an output
    LATA.5 = 0;                     // Low level
    TRISA.5 = 0;                    // Output
                                
                                // TR LED pins Q10 and Q11 (MCU pins RB7 and RA2) are set as outputs by OS.
                                // TR pin Q12 (MCU pin RB4) is set as an input with internal pull-up activated as default.
    pulseLEDG();
#else
    #error Wrong TR module setting.
#endif
// ---------------------------------------------------------------------
    startLongDelay(DELAY);          
    while (isDelay());              // Run mode + RFIC in RF Sleep mode: cca 1.8 mA for 10 s
    
    toutRF = 250;                   // [In ticks]
    startLongDelay(DELAY);
    while (isDelay())
        RFRXpacket();               // STD receiving: cca 13 mA for 10 s
    
    startLongDelay(DELAY);          
    while (isDelay());              // Run mode + RFIC in RF Ready mode: cca 3.3 mA for 10 s
    
    setRFmode(_RX_LP);
    toutRF = 213;                   // [In cycles], 1 cycle is cca 47 ms
    RFRXpacket();                   // LP receiving: cca 190 uA (average) for 10 s
    
    startLongDelay(DELAY);          
    while (isDelay());              // Run mode + RFIC in RF Ready mode: cca 3.3 mA for 10 s
    
    setRFmode(_RX_XLP);
    toutRF = 13;                    // [In cycles], 1 cycle is cca 790 ms
    RFRXpacket();                   // XLP receiving: cca 13 uA (average) for 10 s
    
    startLongDelay(DELAY);          
    while (isDelay());              // Run mode + RFIC in RF Sleep mode: cca 1.8 mA for 10 s
    
    DLEN = 64;
    PIN = 0;
    startLongDelay(DELAY);           
    while (isDelay())
        RFTXpacket();               // Transmiting (full RF power): cca 23 mA for 10 s
    
    startLongDelay(DELAY);          
    while (isDelay());              // Run mode + RFIC in RF Ready mode: cca 3.3 mA for 10 s
    
    setRFsleep();                   
    startLongDelay(DELAY);          
    while (isDelay());              // Run mode + RFIC in RF Sleep mode: cca 1.8 mA for 10 s
    
    setWDTon_16s();
    
#ifndef DEEP_SLEEP
    iqrfSleep();                    // Sleep with wake-up on watchdog timer: < 2.8 uA (TR-72G) or < 1.5 uA (TR-76G) for 16 s
#else    
    iqrfDeepSleep();                // Deep sleep with wake-up on watchdog timer: < 2.1 uA (TR-72G) or < 800 nA (TR-76G) for 16 s
#endif
 
    setWDToff();
    
    startLongDelay(DELAY);
    while (isDelay());              // Run mode + RFIC in RF Sleep mode: cca 1.8 mA for 10 s

#ifndef DEEP_SLEEP
    iqrfSleep();                    // Sleep: < 2.3 uA (TR-72G) or < 1 uA (TR-76G)
#else
    iqrfDeepSleep();                // Deep sleep: < 1.6 uA (TR-72G) or < 300 nA (TR-76G)
#endif

    pulsingLEDG();
    while (1) ;
}

// *********************************************************************
