// *********************************************************************
// *                        E13 - INTERRUPT                            *
// *                        Interrupt usage                            *
// *********************************************************************
// Simple example of interrupt using in application.
//
// Intended for:
//   HW: - TR-72G + DK-EVAL-04x development kit
//
//   OS: - v4.06G
//
// Description:
//   - This example illustrates how to use the interrupt in user application.
//   - Interrupt on change (RB4 pin -> SW1 on DK-EVAL-04x) controls the red LED.
//
// ---------------------------------------------------------------------
// Online IQRF OS Reference Guide: https://doc.iqrf.org/IQRF-OS-Reference-guide
//
// Copyright (c) IQRF Tech s.r.o.
//
// File:    E13-INTERRUPT.c
// Version: v3.00                                   Revision: 11/06/2021
//
// Revision history:
//   v3.00: 11/06/2021  First release for TR-7xG.
//
// *********************************************************************
// Default IQRF include (modify the path according to your setup)
#include "IQRF.h"

// *********************************************************************
void userIntRoutine(void); 

// *********************************************************************
void APPLICATION()
{
    IOCBP.4 = 1;                        // Positive edge active (comment if not required)
    IOCBN.4 = 1;                        // Negative edge active (comment if not required)
                                        
    IOCBF.4 = 0;                        // Clear interrupt on pin change flag

    _enableUserInterrupt = 1;           // User interrupt should be enabled sooner than any individual interrupt sources (e.g. TMR6IE = 1).
                                        //   Possible disabling should be done in reverse order.
    IOCIE = 1;                          // Interrupt on change enabled

    while (1);
}
// ---------------------------------------------------------------------

// This function is invoked whenever any interrupt occurs (even OS)
// and the _enableUserInterrupt flag is set to 1.
// OS generates an interrupt in the period of system ticks.
// The user interrupt routine must start from address 0x4F00 (__USER_INTERRUPT macro). 
// The user interrupt routine must take less than 50 µs. 
// User interrupts must not be called in period shorter then 800 µs.
// IQRF OS functions except of setINDFx and getINDFx are not allowed.
// IQRF OS variables must not be used.
// Only global or local static user variables are allowed.
// For more information see IQRF OS User's Guide.

#pragma origin __USER_INTERRUPT
#pragma library 0
void userIntRoutine(void)
{
    if (IOCBF.4)
    {
        IOCBF.4 = 0;                    // Clear interrupt on pin change flag
        toggleLEDR();
    }    
} 
// *********************************************************************
