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 BIOS = xdc.useModule('ti.sysbios.BIOS');
var Hwi = xdc.useModule('ti.sysbios.hal.Hwi');
var HeapBuf = xdc.useModule('ti.sysbios.heaps.HeapBuf');
var Idle = xdc.useModule('ti.sysbios.knl.Idle');
var Swi = xdc.useModule('ti.sysbios.knl.Swi');
var Clock = xdc.useModule('ti.sysbios.knl.Clock');
var Task = xdc.useModule('ti.sysbios.knl.Task');
var Semaphore = xdc.useModule('ti.sysbios.knl.Semaphore');
var Event = xdc.useModule('ti.sysbios.knl.Event');
var Queue = xdc.useModule('ti.sysbios.knl.Queue');
var Mailbox = xdc.useModule('ti.sysbios.knl.Mailbox');

/* 
 * 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;
 */

/* ======== Setting Program paramters ======== */
/* Program object is available in a cfg file. */
/* Set system stack size */
Program.stack = 0x1000;
/* Set argsSize */
Program.argSize = 0x8;

/* ======== Set default System module ======== */
/* 
 * Configure SysMin for System_printfs because SysStd cannot be used when 
 * calling System_printf from Hwis and Swis 
 */
System.SupportProxy = SysMin;

/* ======== Heaps and Memory ======== */
/* Create a heap using ti.bios.HeapBuf. */
var heapBufParams = new HeapBuf.Params;
heapBufParams.blockSize = 16;
heapBufParams.numBlocks = 16;
heapBufParams.align = 8;
Program.global.heap0 = HeapBuf.create(heapBufParams);
/* Use heap0 as the default heap */
Memory.defaultHeapInstance = Program.global.heap0;

/* ======== Logging ======== */
/* 
 * Create and install logger for the whole system
 */
var loggerBufParams = new LoggerBuf.Params();
loggerBufParams.numEntries = 32;
var logger0 = LoggerBuf.create(loggerBufParams);
Defaults.common$.logger = logger0;
Main.common$.diags_INFO = Diags.ALWAYS_ON;

/* ======== Using the Idle Module ======== */
/* Add idle function to table where idl0Fxn is name of function in C file */
Idle.addFunc('&idl0Fxn');

/* ======== Using the Swi Module ======== */
/* Set number of swi priorities */
Swi.numPriorities = 16;
/* Create a Swi Instance and manipulate Instance parameters. */
var swiParams = new Swi.Params;
swiParams.arg0 = 0;
swiParams.arg1 = 1;
swiParams.priority = 7;
Program.global.swi0 = Swi.create('&swi0Fxn', swiParams);

/* Create another Swi Instance using default Instance parameters */
Program.global.swi1 = Swi.create('&swi1Fxn');

/* ======== Using the Clock Module ======== */
/* Set clock swi priority. 0-15 available because Swi.numPriorities above */
Clock.swiPriority = 15;
Clock.tickPeriod = 5000;
/* Create a periodic clock instance with period and timeout of 5 system ticks */
var clkParams = new Clock.Params;
clkParams.startFlag = true;
clkParams.period = 5;
Program.global.clk0 = Clock.create("&clk0Fxn", 5, clkParams);

/* Create a one-shot clock instance with default parameters */
Program.global.clk1 = Clock.create("&clk1Fxn", 12);

/* ======== Using the Task Module ======== */
/* Set number of Task priority levels */
Task.numPriorities = 16;
Task.idleTaskStackSize = 1024;
/* Create a Task Instance and manipulate Instance parameters */
var tskParams = new Task.Params;
tskParams.arg0 = 1;
tskParams.arg1 = 2;
tskParams.priority = 15;
tskParams.stack = null;
tskParams.stackSize = 1024;
var task0 = Task.create('&task0Fxn', tskParams);

/* Create a Task Instance and use default Instance parameters */
Program.global.task1 = Task.create('&task1Fxn');

/* ======== Using the Semaphore Module ======== */
/* Whether semaphore will support events */
Semaphore.supportsEvents = true;
/* Create a Semaphore Instance with count of 4 which does not use events */
var semParams1 = new Semaphore.Params;
semParams1.mode = Semaphore.Mode_COUNTING;
Program.global.sem0 = Semaphore.create(4, semParams1);

/* ======== Using the Event Module ======== */
/* Create an event */
var eventParams = new Event.Params;
Program.global.event0 = Event.create(eventParams);
/* Create a Semaphore Instance which uses event0 */
var semParams2 = new Semaphore.Params;
semParams2.mode = Semaphore.Mode_COUNTING;
semParams2.event = Program.global.event0;
semParams2.eventId = Event.Id_00;
Program.global.sem1 = Semaphore.create(5, semParams2);

/* ======== Using the Queue Module ======== */
/* Create a Queue Instance */
Program.global.queue0 = Queue.create();

/* ======== Using the Mailbox Module ======== */
/* Create a Mailbox and manipulate Instance parameters */
var mbxParams = new Mailbox.Params;
mbxParams.heap = Program.global.heap0;
mbxParams.readerEvent = null;
mbxParams.writerEvent = Program.global.event0;
mbxParams.writerEventId = Event.Id_01;
Program.global.mbx0 = Mailbox.create(8, 2, mbxParams);
/* Create a Mailbox using default Instance parameters */
Program.global.mbx1 = Mailbox.create(16, 4);
