% var module = system.modules['/driverlib/spi.js'];
% let Common   = system.getScript("/driverlib/Common.js");
%let device_driverlib_peripheral = 
%    system.getScript("/driverlib/device_driverlib_peripherals/" + 
%        Common.getDeviceName().toLowerCase() + "_spi.js");
//*****************************************************************************
//
// SPI Configurations
//
//*****************************************************************************
void `module.peripheralName`_init(){
%   for(var i = 0; i < module.$instances.length; i++) 
%   {
%      var instance = module.$instances[i];
	`instance.$name`_init();
%   }
}

%if (module != null)
%{
	% for(var i = 0; i < module.$instances.length; i++) {
		% var instance = module.$instances[i];
void `instance.$name`_init(){
	SPI_disableModule(`instance.$name`_BASE);
		%
		%var protocol = instance.transferProtocol;
		%var mode = instance.mode;
		%let interruptPrefix = "SPI_INT_";
		%var interrupts = "";
	SPI_setConfig(`instance.$name`_BASE, DEVICE_SYSCLK_FREQ, `protocol`,
				  `mode`, `instance.$name`_BITRATE, `instance.$name`_DATAWIDTH);
		%if(!device_driverlib_peripheral.SPI_PTEPolarity)
		%{
	SPI_setPTESignalPolarity(`instance.$name`_BASE, `instance.ptePolarity`);
		%}
		%else
		%{
	SPI_setPTESignalPolarity(`instance.$name`_BASE, `instance.ptePolarity`);
		%}
		%if (instance.useHSMode)
		%{
	SPI_enableHighSpeedMode(`instance.$name`_BASE);
		%}
		%if (instance.useFifo)
		%{
	SPI_enableFIFO(`instance.$name`_BASE);
			%if (instance.useInterrupts)
			%{
	SPI_setFIFOInterruptLevel(`instance.$name`_BASE, `instance.txFifo`, `instance.rxFifo`);
				% for (var int_index in instance.enabledFIFOInterrupts)
				% {
				%	interrupts += ((interrupts == "")?"":" | ") + instance.enabledFIFOInterrupts[int_index];
				% }
			%}
		%}
		%else{
	SPI_disableFIFO(`instance.$name`_BASE);
			%if (instance.useInterrupts)
			%{
				% for (var int_index in instance.enabledInterrupts)
				% {
				%	interrupts += ((interrupts == "")?"":" | ") + instance.enabledInterrupts[int_index];
				% }
			%}
		%}
		%if (interrupts != "")
		%{
	SPI_clearInterruptStatus(`instance.$name`_BASE, `interrupts`);
	SPI_enableInterrupt(`instance.$name`_BASE, `interrupts`);
		%}
		%if (instance.loopback)
		%{
	SPI_enableLoopback(`instance.$name`_BASE);
		%}
		%else
		%{
	SPI_disableLoopback(`instance.$name`_BASE);
		%}
	SPI_setEmulationMode(`instance.$name`_BASE, `instance.emulationMode`);
	SPI_enableModule(`instance.$name`_BASE);
}
	%}
%}