/***********************************************************************
*                    SEGGER Microcontroller GmbH                       *
*                        The Embedded Experts                          *
************************************************************************
*                                                                      *
*                  (c) SEGGER Microcontroller GmbH                     *
*                        All rights reserved                           *
*                          www.segger.com                              *
*                                                                      *
************************************************************************
*                                                                      *
************************************************************************
*                                                                      *
*                                                                      *
*  Licensing terms                                                     *
*                                                                      *
* This software may be distributed to your customers free of charge.   *
* This grant of redistribution does not entitle YOU or enduser to      *
* receive from SEGGER hard-copy documentation, technical support,      *
* phone assistance, or enhancements or updates to the Software unless  *
* a specific agreement clearly states otherwise.                       *
*                                                                      *
*                                                                      *
* THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDER "AS IS" AND ANY        *
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE    *
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR   *
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER BE        *
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,     *
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,             *
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR   *
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY  *
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT         *
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE    *
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH     *
* DAMAGE.                                                              *
*                                                                      *
************************************************************************

-------------------------- END-OF-HEADER -----------------------------

File    : Example_ResetTimeout.JLinkScript
Purpose : This script shows how to use the JLINK_SYS_ResetTimeout() function to execute long-running scripts.

Notes:
  (1) The script is supposed to be used with the Device Provisioner (DevPro.exe).
      DevPro.exe -operation TestResetTimeout -if SWD -speed 4000 -scriptfile C:\Temp\Example_ResetTimeout.JLinkScript
*/

/*********************************************************************
*
*       Local functions
*
**********************************************************************
*/

/*********************************************************************
*
*       _SimulateCalculations()
*
*  Function description
*    This functions simulates calculations using the JLINK_SYS_Sleep() function.
*/
static void _SimulateCalculations(int Iterations, int Sleep, int Reset) {
  int i;

  i = 0;
  while (i < Iterations) {
    JLINK_SYS_Report1("Simulate calculations .... Run=", i);
    JLINK_SYS_Sleep(Sleep);
    if (Reset) {
      JLINK_SYS_ResetTimeout(); // Can be called anytime.
    }
    i += 1;
  }
}

/*********************************************************************
*
*       Global functions
*
**********************************************************************
*/

/*********************************************************************
*
*       TestDefaultTimeout()
*
*  Function description
*    Demonstrate default timeout without JLINK_SYS_ResetTimeout().
*
*  Return value
*    >= 0:  OK
*     < 0:  Error
*/
int TestDefaultTimeout(void) {
  _SimulateCalculations(15, 5000, 0); // 15 * 5 s = 75 s but will abort after 60 seconds
  return 0;
}

/*********************************************************************
*
*       TestResetTimeout()
*
*  Function description
*    Test JLINK_SYS_ResetTimeout() functionality.
*
*  Return value
*    >= 0:  OK
*     < 0:  Error
*/
int TestResetTimeout(void) {
  _SimulateCalculations(15, 5000, 1); // 15 * 5 s = 75 s with reset
  return 0;
}