var Defaults = xdc.useModule('xdc.runtime.Defaults');
var Diags = xdc.useModule('xdc.runtime.Diags');
var Error = xdc.useModule('xdc.runtime.Error');
var Log = xdc.useModule('xdc.runtime.Log');
var LoggerBuf = xdc.useModule('xdc.runtime.LoggerBuf');
var Main = xdc.useModule('xdc.runtime.Main');
var Memory = xdc.useModule('xdc.runtime.Memory')
var SysMin = xdc.useModule('xdc.runtime.SysMin');
var System = xdc.useModule('xdc.runtime.System');
var Text = xdc.useModule('xdc.runtime.Text');

var Hwi = xdc.useModule('ti.sysbios.family.c28.Hwi');
var Clock = xdc.useModule('ti.sysbios.knl.Clock');
var Mailbox = xdc.useModule('ti.sysbios.knl.Mailbox');
var ti_sysbios_hal_Hwi = xdc.useModule('ti.sysbios.hal.Hwi');
var Event = xdc.useModule('ti.sysbios.knl.Event');
var Timer = xdc.useModule('ti.sysbios.hal.Timer');
var ti_sysbios_family_c28_Timer = xdc.useModule('ti.sysbios.family.c28.Timer');
var Boot = xdc.useModule('ti.catalog.c2800.initF2837x.Boot');

/* 
 * Program.argSize sets the size of the .args section. 
 * The examples don't use command line args so argSize is set to 0.
 */
Program.argSize = 0x0;

/*
 * Uncomment this line to globally disable Asserts.
 * All modules inherit the default from the 'Defaults' module.  You
 * can override these defaults on a per-module basis using Module.common$. 
 * Disabling Asserts will save code space and improve runtime performance.
Defaults.common$.diags_ASSERT = Diags.ALWAYS_OFF;
 */

/*
 * Uncomment this line to keep module names from being loaded on the target.
 * The module name strings are placed in the .const section. Setting this
 * parameter to false will save space in the .const section.  Error and
 * Assert messages will contain an "unknown module" prefix instead
 * of the actual module name.
Defaults.common$.namedModule = false;
 */

/*
 * Minimize exit handler array in System.  The System module includes
 * an array of functions that are registered with System_atexit() to be
 * called by System_exit().
 */
System.maxAtexitHandlers = 4;       

/* 
 * Uncomment this line to disable the Error print function.  
 * We lose error information when this is disabled since the errors are
 * not printed.  Disabling the raiseHook will save some code space if
 * your app is not using System_printf() since the Error_print() function
 * calls System_printf().
Error.raiseHook = null;
 */

/* 
 * Uncomment this line to keep Error, Assert, and Log strings from being
 * loaded on the target.  These strings are placed in the .const section.
 * Setting this parameter to false will save space in the .const section.
 * Error, Assert and Log message will print raw ids and args instead of
 * a formatted message.
Text.isLoaded = false;
 */

/*
 * Uncomment this line to disable the output of characters by SysMin
 * when the program exits.  SysMin writes characters to a circular buffer.
 * This buffer can be viewed using the SysMin Output view in ROV.
SysMin.flushAtExit = false;
 */


/*
 * Use SysMin for output (System_printf() and error messages) and
 * minimize the output buffer size to save data space.
 */
System.SupportProxy = SysMin;
/* SysMin.bufSize = 80; */

SysMin.bufSize = 0x200;
/*
 * Create a LoggerBuf and make it the default logger for all modules.
 */
var LoggerBufParams = new LoggerBuf.Params();
LoggerBufParams.numEntries = 32;
var logger0 = LoggerBuf.create(LoggerBufParams);
Defaults.common$.logger = logger0;

Main.common$.diags_INFO = Diags.ALWAYS_ON;

Program.stack = 512;

/*
 * Application specific configuration 
 */
 
/*
 * Disable unused BIOS features to minimize footprint.
 * This example uses Tasks but not Swis or Clocks.
 */
var BIOS = xdc.useModule('ti.sysbios.BIOS'); 
/*
BIOS.swiEnabled = true;
BIOS.taskEnabled = true;
BIOS.clockEnabled = true;
*/
/* Minimize system heap size */
BIOS.heapSize = 0x1000;
/* Create a task with priority 1 */
var Task = xdc.useModule('ti.sysbios.knl.Task');
/*
var taskParams = new Task.Params();
taskParams.priority = 10;
taskParams.instance.name = "ControlTaskHd";
taskParams.stackSize = 1024;
Program.global.ControlTaskHd = Task.create("&Task_Control", taskParams);
*/

/* Inhibit the creation of a task to run idle functions */
/*
Task.enableIdleTask = false;
*/
/* 
 * Create a binary semaphore for the timer ISR to use to 
 * signal the pended task every 10 ticks 
 */
var Semaphore = xdc.useModule('ti.sysbios.knl.Semaphore');
var semParams = new Semaphore.Params();
semParams.mode = Semaphore.Mode_BINARY;
semParams.instance.name = "adcResultSem";
Program.global.adcResultSem = Semaphore.create(0, semParams);

/*
 * Build a custom BIOS library.  The custom library will be smaller than the
 * pre-built "instrumented" (default) and "non-instrumented" libraries.
 *
 * The BIOS.logsEnabled parameter specifies whether the Logging is enabled
 * within BIOS for this custom build.  These logs are used by the RTA and
 * UIA analysis tools.
 *
 * The BIOS.assertsEnabled parameter specifies whether BIOS code will
 * include Assert() checks.  Setting this parameter to 'false' will generate
 * smaller and faster code, but having asserts enabled is recommended for
 * early development as the Assert() checks will catch lots of programming
 * errors (invalid parameters, etc.)
 */
BIOS.libType = BIOS.LibType_Custom;
/*
BIOS.logsEnabled = false;
BIOS.assertsEnabled = true;
*/


/********************* TEMP(SPIA) ************/

var mailbox70Params = new Mailbox.Params();
mailbox70Params.instance.name = "SpiAMbx";
Program.global.SpiAMbx = Mailbox.create(2, 10, mailbox70Params);


var taskTempParams = new Task.Params();
taskTempParams.instance.name = "SpiATaskHandle";
taskTempParams.priority = 3;
taskTempParams.stackSize = 1536;
Program.global.SpiATaskHandle = Task.create("&SpiA_Task", taskTempParams);


/********************* ExtAdc(SPIB) ************/
var mailbox71Params = new Mailbox.Params();
mailbox71Params.instance.name = "SpiBMbx";
var event3Params = new Event.Params();
event3Params.instance.name = "ExtAdcEvent";
Program.global.ExtAdcEvent = Event.create(event3Params);
mailbox71Params.readerEvent = Program.global.ExtAdcEvent;
mailbox71Params.readerEventId = 4;
mailbox71Params.writerEventId = 4;
Program.global.SpiBMbx = Mailbox.create(2, 10, mailbox71Params);


var taskExtAdcParams = new Task.Params();
taskExtAdcParams.instance.name = "SpiBTaskHandle";
taskExtAdcParams.priority = 6;
taskExtAdcParams.stackSize = 512;
Program.global.SpiBTaskHandle = Task.create("&SpiB_Task", taskExtAdcParams);


/********************* ExtDac(SPIC) ************/
var mailbox73Params = new Mailbox.Params();
mailbox73Params.instance.name = "SpiCMbx";
var event39Params = new Event.Params();
event39Params.instance.name = "ExtDacEvent";
Program.global.ExtDacEvent = Event.create(event39Params);
mailbox73Params.readerEvent = Program.global.ExtDacEvent;
mailbox73Params.readerEventId = 4;
mailbox73Params.writerEventId = 4;
Program.global.SpiCMbx = Mailbox.create(12, 2, mailbox73Params);

var taskExtDacParams = new Task.Params();
taskExtDacParams.instance.name = "SpiCTaskHandle";
taskExtDacParams.priority = 6;
taskExtDacParams.stackSize = 512;
Program.global.SpiCTaskHandle = Task.create("&SpiC_Task", taskExtDacParams);


/************************ IntAdc A ~ D ****************************/
var hwi6Params = new Hwi.Params();
hwi6Params.instance.name = "AdcAHwi";
Program.global.AdcAHwi = Hwi.create(32, "&AdcA1_Isr", hwi6Params);
var hwi7Params = new Hwi.Params();
hwi7Params.instance.name = "AdcBHwi";
Program.global.AdcBHwi = Hwi.create(33, "&AdcB1_Isr", hwi7Params);
var hwi8Params = new Hwi.Params();
hwi8Params.instance.name = "AdcCHwi";
Program.global.AdcCHwi = Hwi.create(34, "&AdcC1_Isr", hwi8Params);
var hwi9Params = new Hwi.Params();
hwi9Params.instance.name = "AdcDHwi";
Program.global.AdcDHwi = Hwi.create(37, "&AdcD1_Isr", hwi9Params);

var mailbox61Params = new Mailbox.Params();
mailbox61Params.instance.name = "IntAppMbx";

var event6Params = new Event.Params();
event6Params.instance.name = "IntAppEvent";
Program.global.IntAppEvent = Event.create(event6Params);
mailbox61Params.readerEvent = Program.global.IntAppEvent;
mailbox61Params.readerEventId = 16;
mailbox61Params.writerEvent = null;
mailbox61Params.writerEventId = 16;
Program.global.IntAppMbx = Mailbox.create(2, 3, mailbox61Params);

/* ePWM  Isr ==> Move CPU2 */
var hwi11Params = new Hwi.Params();
hwi11Params.instance.name = "ePwm1Hwi";
Program.global.ePwm1Hwi = Hwi.create(48, "&EPwm1Isr", hwi11Params);

/*
var hwi12Params = new Hwi.Params();
hwi12Params.instance.name = "ePwm2Hwi";
Program.global.ePwm2Hwi = Hwi.create(49, "&EPwm2Isr", hwi12Params);
*/
var hwi14Params = new Hwi.Params();
hwi14Params.instance.name = "ePwm4Hwi";
Program.global.ePwm4Hwi = Hwi.create(51, "&EPwm1Isr", hwi14Params);

/*
var hwi15Params = new Hwi.Params();
hwi15Params.instance.name = "ePwm5Hwi";
Program.global.ePwm5Hwi = Hwi.create(52, "&EPwm5Isr", hwi15Params);
*/
var mailbox11Params = new Mailbox.Params();
mailbox11Params.instance.name = "EpwmMbx";

var event61Params = new Event.Params();
event61Params.instance.name = "ePwmEvent";
Program.global.ePwmEvent = Event.create(event61Params);
mailbox11Params.readerEvent = Program.global.ePwmEvent;
mailbox11Params.readerEventId = 16;
mailbox11Params.writerEvent = null;
mailbox11Params.writerEventId = 16;
Program.global.EpwmMbx = Mailbox.create(2, 3, mailbox11Params);

/* Remove pWM Task 20170511 
var task10Params = new Task.Params();
task10Params.instance.name = "EpwmTask";
task10Params.priority = 7;
Program.global.EpwmTask = Task.create("&ePwm_Task", task10Params);
 */

/********** IPC ***************************/

var hwi30Params = new Hwi.Params();
hwi30Params.instance.name = "Ipc0Hwi";
Program.global.Ipc0Hwi = Hwi.create(132, "&Ipc0_Isr", hwi30Params);


var mailbox30Params = new Mailbox.Params();
mailbox30Params.instance.name = "Ipc0Mbx";
Program.global.Ipc0Mbx = Mailbox.create(12, 3, mailbox30Params);


var task30Params = new Task.Params();
task30Params.instance.name = "IpcRxTask";
task30Params.priority = 10;
task30Params.stackSize = 1536;

/*****************  EXT Interrupt *************/
var hwi87Params = new Hwi.Params();
hwi87Params.instance.name = "Ext1Hwi";
Program.global.Ext1Hwi = Hwi.create(35, "&XINT1_Isr", hwi87Params);

var hwi88Params = new Hwi.Params();
hwi88Params.instance.name = "Ext2Hwi";
Program.global.Ext2Hwi = Hwi.create(36, "&XINT2_Isr", hwi88Params);

var hwi89Params = new Hwi.Params();
hwi89Params.instance.name = "Ext3Hwi";
Program.global.Ext3Hwi = Hwi.create(120, "&XINT3_Isr", hwi89Params);

var event8Params = new Event.Params();
event8Params.instance.name = "ExtIntEvent";
Program.global.ExtIntEvent = Event.create(event8Params);

var task87Params = new Task.Params();
task87Params.instance.name = "GPIOXint_Debug";
task87Params.priority = 8;
task87Params.stackSize = 512;
Program.global.GPIOXint_Debug = Task.create("&GPIOXint_Debug_Task", task87Params);


/********************* EMIF *****************/

var mailbox91Params = new Mailbox.Params();
mailbox91Params.instance.name = "EmifMbx";
var event91Params = new Event.Params();
event91Params.instance.name = "EmifEvent";
Program.global.EmifEvent = Event.create(event91Params);
mailbox91Params.readerEvent = Program.global.EmifEvent;
mailbox91Params.readerEventId = 4;
mailbox91Params.writerEventId = 4;
mailbox91Params.writerEvent = Program.global.EmifEvent;
Program.global.EmifMbx = Mailbox.create(12, 3, mailbox91Params);

var task97Params = new Task.Params();
task97Params.instance.name = "EMIF_Debug";
task97Params.priority = 5;
task97Params.stackSize = 512;
Program.global.EMIF_Debug = Task.create("&EMIF_Debug_Task", task97Params);


/********************* CAN *****************/

var hwi20Params = new Hwi.Params();
hwi20Params.instance.name = "CanAHwi";
Program.global.CanAHwi = Hwi.create(100, "&CANAIntHandler", hwi20Params);

var hwi21Params = new Hwi.Params();
hwi21Params.instance.name = "CanBHwi";
Program.global.CanBHwi = Hwi.create(102, "&CANBIntHandler", hwi21Params);


var mailbox21Params = new Mailbox.Params();
mailbox21Params.instance.name = "CanARxMbx";

var event0Params = new Event.Params();
event0Params.instance.name = "CanAEvent";
Program.global.CanAEvent = Event.create(event0Params);
mailbox21Params.readerEvent = Program.global.CanAEvent;
mailbox21Params.readerEventId = 4;
mailbox21Params.writerEvent = null;
mailbox21Params.writerEventId = 4;
Program.global.CanARxMbx = Mailbox.create(16, 16, mailbox21Params);



var mailbox22Params = new Mailbox.Params();
mailbox22Params.instance.name = "CanBRxMbx";

var event1Params = new Event.Params();
event1Params.instance.name = "CanBEvent";
Program.global.CanBEvent = Event.create(event1Params);
mailbox22Params.readerEvent = Program.global.CanBEvent;
mailbox22Params.readerEventId = 4;
mailbox22Params.writerEventId = 4;
mailbox22Params.writerEvent = null;
Program.global.CanBRxMbx = Mailbox.create(16, 16, mailbox22Params);


var mailbox52Params = new Mailbox.Params();
mailbox52Params.instance.name = "MgmtMbx";

var event52Params = new Event.Params();
event52Params.instance.name = "MgmtEvent";
Program.global.MgmtEvent = Event.create(event52Params);
mailbox52Params.readerEvent = Program.global.MgmtEvent;
mailbox52Params.readerEventId = 4;
mailbox52Params.writerEventId = 4;
mailbox52Params.writerEvent = Program.global.MgmtEvent;
Program.global.MgmtMbx = Mailbox.create(2, 3, mailbox52Params);

var task21Params = new Task.Params();
task21Params.instance.name = "CanATxRxTask";
task21Params.priority = 7;
task21Params.vitalTaskFlag = false;
Program.global.CanATxRxTask = Task.create("&CanA_TxRx_Task", task21Params);

var task22Params = new Task.Params();
task22Params.instance.name = "CanBTxRxTask";
task22Params.priority = 7;
Program.global.CanBTxRxTask = Task.create("&CanB_TxRx_Task", task22Params);


var task30Params = new Task.Params();
task30Params.instance.name = "CmdTask";
task30Params.priority = 8;
task30Params.stackSize = 2048;
Program.global.CMDtaskHd = Task.create("&Cmd_Task", task30Params);

var task50Params = new Task.Params();
task50Params.instance.name = "MgmtTask";
task50Params.priority = 8;
task50Params.stackSize = 256;
Program.global.MgmtTaskHd = Task.create("&Mgmt_Task", task50Params);

var task60Params = new Task.Params();
task60Params.priority = 10;
task60Params.instance.name = "ControlTaskHd";
task60Params.stackSize = 1024;
Program.global.ControlTaskHd = Task.create("&Task_Control", task60Params);

Error.maxDepth = 32;
Task.numPriorities = 16;
Boot.configureClocks = false;
Semaphore.supportsEvents = true;
Boot.OSCCLK = 20;
BIOS.cpuFreq.lo = 200000000;
Boot.SPLLIMULT = 20;
Boot.SYSCLKDIVSEL = 1;
Boot.bootCPU2 = false;

var ti_sysbios_family_c28_Timer0Params = new ti_sysbios_family_c28_Timer.Params();
ti_sysbios_family_c28_Timer0Params.instance.name = "TickforTmr";
ti_sysbios_family_c28_Timer0Params.period = 200000;
ti_sysbios_family_c28_Timer0Params.startMode = xdc.module("ti.sysbios.interfaces.ITimer").StartMode_USER;
ti_sysbios_family_c28_Timer0Params.emulationModeInit.free = 1;
Program.global.TickforTmr = ti_sysbios_family_c28_Timer.create(-1, "&TickTmr_Isr", ti_sysbios_family_c28_Timer0Params);

//SysMin.outputFxn = "&myOutputFunc";
Hwi.dispatcherAutoNestingSupport = false;

Task.checkStackFlag = true;
Task.idleTaskStackSize = 256;

Boot.loadSegment = "MYFLASH PAGE = 0";
