/*----------------------------------------------------------------------------
 * Name:    Debug_UlinkPlus.ini
 * Purpose: Configuration and template script functions for ULINKplus 
 *----------------------------------------------------------------------------
 * This file is part of the uVision/ARM development tools.
 * Copyright (c) 2017 Arm Germany GmbH. All rights reserved.
 *----------------------------------------------------------------------------*/

//-------- <<< Use Configuration Wizard in Context Menu >>> -----------------
//

/*---
  ConfigureUlinkPlus ()
  Configure I/O pins and shunt resistor for power measurement.  This function is
  called at debugger start when using this file is entered as debug initialization file.
---*/

FUNC void ConfigureUlinkPlus (void) {
  unsigned int ULpin0, ULpin1, ULpin2, ULpin3, ULpin4, ULpin5, ULpin6, ULpin7, ULpin8, ULpin3v3;
  unsigned int ULpinEna, ULpwrEna, UL_Shunt;

// <e>ULINKplus I/O Pin Configuration
// ==================================
// <i> Setup I/O pins for test automoation

  ULpinEna = 0;

//
// <o>Pin 0 
//   <0=> Digital Input 
//   <1=> Digital Output
// <i> Default: Digital Input

  ULpin0 = 0;

// <o>Pin 1 
//   <0=> Digital Input 
//   <1=> Digital Output
// <i> Default: Digital Input

  ULpin1 = 0;

// <o>Pin 2 
//   <0=> Digital Input 
//   <1=> Digital Output
// <i> Default: Digital Input

  ULpin2 = 0;

// <o>Pin 3 
//   <0=> Digital Input 
//   <1=> Digital Output
// <i> Default: Digital Input

  ULpin3 = 0;

// <o>Pin 4 
//   <0=> Digital Input 
//   <1=> Digital Output
//   <2=> Analog Input
// <i> Default: Digital Input

  ULpin4 = 0;

// <o>Pin 5 
//   <0=> Digital Input 
//   <1=> Digital Output
//   <2=> Analog Input
// <i> Default: Digital Input

  ULpin5 = 0;

// <o>Pin 6 
//   <0=> Digital Input 
//   <1=> Digital Output
//   <2=> Analog Input
// <i> Default: Digital Input

  ULpin6 = 0;

// <o>Pin 7 
//   <0=> Digital Input 
//   <1=> Digital Output
//   <2=> Analog Input
//   <3=> Analog Output
// <i> Default: Digital Input

  ULpin7 = 0;

// <o>Pin 8 
//   <0=> Digital Input 
//   <1=> Digital Output
// <i> Default: Digital Input

  ULpin8 = 0;

// <o>+3.3V Out 
//   <0=> Off 
//   <1=> On
// <i> Default: Off

  ULpin3v3 = 0;
// </e>

  // configure ULINK pins according to the assigned functionalities
  if (ULpinEna) {
    ULINK_AMODE = (unsigned int)(((ULpin0>>1)<<0) | ((ULpin1>>1)<<1) | ((ULpin2>>1)<<2) | ((ULpin3>>1)<<3) | ((ULpin4>>1)<<4) | ((ULpin5>>1)<<5) | ((ULpin6>>1)<<6) | ((ULpin7>>1)<<7) | ((ULpin8>>1)<<8));
    ULINK_DIR   = (unsigned int)(((ULpin0&1)<<0)  | ((ULpin1&1)<<1)  | ((ULpin2&1)<<2)  | ((ULpin3&1)<<3)  | ((ULpin4&1)<<4)  | ((ULpin5&1)<<5)  | ((ULpin6&1)<<6)  | ((ULpin7&1)<<7)  | ((ULpin8&1)<<8));
    ULINK_3V3   = ULpin3v3;
  }

// <e>ULINKplus Power Measurement
// ==============================
// <i> Enable power measurement and configure shunt resistor
  ULpwrEna = 0;
  
  if (ULpwrEna)  {
    ULINK_PM    = 1;

// <o>External Shunt and max. Current Range
// <i> Select external shunt with max. current range
//   <     0=>no external Shunt   2.5 mA  
//   <100000=>100.00 Ohm          5.0 mA  
//   < 33200=> 33.20 Ohm         10.0 mA  
//   < 11000=> 11.00 Ohm         25.0 mA  
//   <  5230=>  5.23 Ohm         50.0 mA  
//   <  2550=>  2.55 Ohm        100.0 mA  
//   <  1000=>  1.00 Ohm        250.0 mA  
    UL_Shunt = 0;
    ULINK_SHUNT = UL_Shunt/1000.0;
  }

// </e>

}


/*---------- Debug script examples for control ULINKplus I/O pins -------------*/


/*---
  TogglePin (unsigned int pin, unsigned int interval, unsigned int count)  
  This debug function toggles a digial I/O pin on ULINKplus.
  The parameters specify:
   pin       I/O pin number
   interval  time of each state in millisconds 
   count     number to toggle cycles

  Note: Configure I/O pins of ULINKplus as 'Digital Output' before using it.
---*/

FUNC void TogglePin (unsigned int pin, unsigned int interval, unsigned int count) {
  unsigned int i;
  
  printf ("%d times toggle pin %d @ state time %d mSec  \n", count, pin, interval);

  for (i = 0; i < count; i++)  {
    ULINK_OUT |= (1 << pin);
    _sleep_ (interval);
    ULINK_OUT &= ~(1 << pin);
    _sleep_ (interval);
  }
  printf ("TogglePin done...\n");
}



/*---
  TableOut (unsigned int interval)  
  This debug function generates an digital signal pattern on the ULINKplus 
  I/O pins Parameter:
     interval   time delay of table steps in millisconds 

  Note: Configure I/O pins of ULINKplus as 'Digital Output' before using it.
---*/

FUNC void TableOut (unsigned int interval) {
  printf ("Start digital signal pattern with interval @ %d mSec\n", interval);
  ULINK_OUT = 0x00;      // pins low
  _sleep_ (interval);
  ULINK_OUT = 0x01;      // pin 0 high
  _sleep_ (interval);
  ULINK_OUT = 0x02;      // pin 1 high
  _sleep_ (interval);
  ULINK_OUT = 0x04;      // pin 2 high
  _sleep_ (interval);
  ULINK_OUT = 0x07;      // pin 0, 1, and 2 high
  _sleep_ (interval);
  ULINK_OUT = 0x00;      // pins low
  printf ("Digital pattern done...\n");
}



/*---
  TableSweep (unsigned int interval)  
  This debug function generates an analog sweep signal on ULINKplus I/O pin 
  Aout 7 based on fixed values entered in this script. Parameter:
     interval   time delay table steps in millisconds 

  Note: Configure I/O Pin 7 of ULINKplus as 'Analog Output' before using it.
---*/

FUNC void TableSweep (unsigned int interval) {
  printf ("Start TableSweep signal with interval @ %d mSec\n", interval);

  ULINK_AOUT7 = 0;         // set output voltage
  _sleep_ (interval);      // wait
  ULINK_AOUT7 = 1.0;
  _sleep_ (interval);
  ULINK_AOUT7 = 2.0;
  _sleep_ (interval);
  ULINK_AOUT7 = 2.4;
  _sleep_ (interval);
  ULINK_AOUT7 = 2.5;
  _sleep_ (interval);
  ULINK_AOUT7 = 2.6;
  _sleep_ (interval);
  ULINK_AOUT7 = 3.0;

  printf ("TableSweep signal done...\n");
}



/*---
  StepSweep (float volts, unsigned int interval, unsigned int steps)  
  This debug function generates an analog sweep signal on ULINKplus I/O pin 
  Aout 7 starting from 0 volts with 0.5 V steps. Maximum voltage, interval, 
  and number \of steps are configured with the following function parameters:
     volt       maximum output voltage in volt {0...3.3 V]
     steps      number of generated sweep steps
     interval   time delay for increments in millisconds 

  Note: Configure I/O Pin 7 of ULINKplus as 'Analog Output' before using it.
---*/

FUNC void StepSweep (float volts, unsigned int steps, unsigned int interval) {
  float val;
  unsigned int i, j;
  
  printf ("Generating StepSweep signal on Aout7 (0 .. %f V) %d steps @ %d mSec\n", volts, steps, interval);

  val = 0;
  for (j = 0; j < steps; j++) {
    ULINK_AOUT7 = val;
  val = val + 0.5;
  if (val > volts)  val = 0;
    _sleep_ (interval);
  }
  printf ("StepSweep signal done...\n");
}

/*--- Configure Toolbox buttons ---*/
define button "5x toggle pin #1",  "TogglePin (1, 100, 5)"
define button "TableOut 250 ms",    "TableOut (250)"
define button "TableSweep 500 ms",  "TableSweep (500)"
define button "StepSweep 100 ms",   "StepSweep (3.3, 20, 100)"

ConfigureUlinkPlus()

