// *********************************************************************
// *                        E14-CONSUMPTION                            *
// *               Power consumption in various TR modes               *
// *********************************************************************
// Testing of power consumption in various operating modes.
//
// Intended for:
//   HW: - (DC)TR-72D + DK-EVAL-04x or (DC)TR-76D + DK-EVAL-04A
//
//   OS: - v4.06D
//
// 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: v2.08                                   Revision: 07/01/2022
//
// Revision history:
//   v2.08: 07/01/2022  Tested for OS 4.06D.
//   v2.07: 30/06/2021  Tested for OS 4.05D.
//   v2.06: 26/08/2020  Tested for OS 4.04D.
//   v2.05: 21/09/2018  Tested for OS 4.03D.
//   v2.04: 23/08/2017  Tested for OS 4.02D.
//   v2.02: 03/03/2017  Tested for OS 4.00D.
//                      deepSleep function added.
//                      (DC)TR-76D supported.
//                      Unused MCU pin setting added to minimize the power consumption.
//   v2.01: 22/02/2016  Tested for OS 3.08D.
//   v2.00: 03/07/2015  First release for (DC)TR-7xD.
//
// *********************************************************************
#include "IQRF.h"  // System header files

// *********************************************************************
#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 TR72D               // 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 TR76D
                                // 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.6 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 mA for 10 s
    
    setRFmode(_RX_LP);
    toutRF = 213;                   // [In cycles], 1 cycle is cca 47 ms
    RFRXpacket();                   // LP receiving: cca 235 uA (average) for 10 s
    
    startLongDelay(DELAY);          
    while (isDelay());              // Run mode + RFIC in RF Ready mode: cca 3 mA for 10 s
    
    setRFmode(_RX_XLP);
    toutRF = 13;                    // [In cycles], 1 cycle is cca 790 ms
    RFRXpacket();                   // XLP receiving: cca 16 uA (average) for 10 s
    
    startLongDelay(DELAY);          
    while (isDelay());              // Run mode + RFIC in RF Sleep mode: cca 1.6 mA for 10 s
    
    DLEN = 64;
    PIN = 0;
    startLongDelay(DELAY);           
    while (isDelay())
        RFTXpacket();               // Transmiting (full RF power): cca 19 mA for 10 s
    
    startLongDelay(DELAY);          
    while (isDelay());              // Run mode + RFIC in RF Ready mode: cca 3 mA for 10 s
    
    setRFsleep();                   
    startLongDelay(DELAY);          
    while (isDelay());              // Run mode + RFIC in RF Sleep mode: cca 1.6 mA for 10 s
    
    setWDTon_16s();
    
#ifndef DEEP_SLEEP
    iqrfSleep();                    // Sleep with wake-up on watchdog timer: cca 2.7 uA (TR-72D) or 1.2 uA (TR-76D) for 16 s
#else    
    iqrfDeepSleep();                // Deep sleep with wake-up on watchdog timer: cca 2.2 uA (TR-72D) or 600 nA (TR-76D) for 16 s
#endif
 
    setWDToff();
    
    startLongDelay(DELAY);
    while (isDelay());              // Run mode + RFIC in RF Sleep mode: cca 1.6 mA for 10 s

#ifndef DEEP_SLEEP
    iqrfSleep();                    // Sleep: cca 2.2 uA (TR-72D) or 600 nA (TR-76D)
#else
    iqrfDeepSleep();                // Deep sleep: cca 1.7 uA (TR-72D) or 40 nA (TR-76D)
#endif

    pulsingLEDG();
    while (1) ;
}

// *********************************************************************
