/*
 * Copyright (c) 2020 Texas Instruments Incorporated - http://www.ti.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * *  Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * *  Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * *  Neither the name of Texas Instruments Incorporated nor the names of
 *    its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 THE COPYRIGHT OWNER OR
 * CONTRIBUTORS 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.
 *
 */

#include "board.h"

//*****************************************************************************
//
// Board Configurations
// Initializes the rest of the modules. 
// Call this function in your application if you wish to do all module 
// initialization.
// If you wish to not use some of the initializations, instead of the 
// Board_init use the individual Module_inits
//
//*****************************************************************************
void Board_init()
{
	EALLOW;

	PinMux_init();
	DMA_init();
	SPI_init();
	INTERRUPT_init();

	EDIS;
}

//*****************************************************************************
//
// PINMUX Configurations
//
//*****************************************************************************
void PinMux_init()
{
	//
	// PinMux for modules assigned to CPU1
	//
	
	//
	// SPIA -> example_spi Pinmux
	//
	GPIO_setPinConfig(example_spi_SPIPICO_PIN_CONFIG);
	GPIO_setPadConfig(example_spi_SPIPICO_GPIO, GPIO_PIN_TYPE_STD);
	GPIO_setQualificationMode(example_spi_SPIPICO_GPIO, GPIO_QUAL_ASYNC);

	GPIO_setPinConfig(example_spi_SPIPOCI_PIN_CONFIG);
	GPIO_setPadConfig(example_spi_SPIPOCI_GPIO, GPIO_PIN_TYPE_STD);
	GPIO_setQualificationMode(example_spi_SPIPOCI_GPIO, GPIO_QUAL_ASYNC);

	GPIO_setPinConfig(example_spi_SPICLK_PIN_CONFIG);
	GPIO_setPadConfig(example_spi_SPICLK_GPIO, GPIO_PIN_TYPE_STD);
	GPIO_setQualificationMode(example_spi_SPICLK_GPIO, GPIO_QUAL_ASYNC);

	GPIO_setPinConfig(example_spi_SPIPTE_PIN_CONFIG);
	GPIO_setPadConfig(example_spi_SPIPTE_GPIO, GPIO_PIN_TYPE_STD);
	GPIO_setQualificationMode(example_spi_SPIPTE_GPIO, GPIO_QUAL_ASYNC);


}

//*****************************************************************************
//
// DMA Configurations
//
//*****************************************************************************
void DMA_init(){
    DMA_initController();
	example_spi_TX_DMA_init();
}

void example_spi_TX_DMA_init(){
    DMA_setEmulationMode(DMA_EMULATION_FREE_RUN);
    DMA_configAddresses(example_spi_TX_DMA_BASE, example_spi_TX_DMA_ADDRESS, (const void *)0);
    DMA_configBurst(example_spi_TX_DMA_BASE, 8U, 0, 0);
    DMA_configTransfer(example_spi_TX_DMA_BASE, 16U, 0, 0);
    DMA_configWrap(example_spi_TX_DMA_BASE, 65535U, 0, 65535U, 0);
    DMA_configMode(example_spi_TX_DMA_BASE, example_spi_TX_DMA_TRIGGER, DMA_CFG_ONESHOT_DISABLE | DMA_CFG_CONTINUOUS_DISABLE | DMA_CFG_SIZE_16BIT);
    DMA_setInterruptMode(example_spi_TX_DMA_BASE, DMA_INT_AT_END);
    DMA_enableInterrupt(example_spi_TX_DMA_BASE);
    DMA_disableOverrunInterrupt(example_spi_TX_DMA_BASE);
    DMA_enableTrigger(example_spi_TX_DMA_BASE);
    DMA_stopChannel(example_spi_TX_DMA_BASE);
}

//*****************************************************************************
//
// INTERRUPT Configurations
//
//*****************************************************************************
void INTERRUPT_init(){
	
	// Interrupt Settings for INT_example_spi_RX
	// ISR need to be defined for the registered interrupts
	Interrupt_register(INT_example_spi_RX, &INT_example_spi_RX_ISR);
	Interrupt_enable(INT_example_spi_RX);
	
	// Interrupt Settings for INT_example_spi_TX
	// ISR need to be defined for the registered interrupts
	Interrupt_register(INT_example_spi_TX, &INT_example_spi_TX_ISR);
	Interrupt_disable(INT_example_spi_TX);
	
	// Interrupt Settings for INT_example_spi_TX_DMA
	// ISR need to be defined for the registered interrupts
	Interrupt_register(INT_example_spi_TX_DMA, &INT_example_spi_TX_DMA_ISR);
	Interrupt_enable(INT_example_spi_TX_DMA);
}
//*****************************************************************************
//
// SPI Configurations
//
//*****************************************************************************
void SPI_init(){
	example_spi_init();
}

void example_spi_init(){
	SPI_disableModule(example_spi_BASE);
	SPI_setConfig(example_spi_BASE, DEVICE_LSPCLK_FREQ, SPI_PROT_POL1PHA0,
				  SPI_MODE_PERIPHERAL, example_spi_BITRATE, example_spi_DATAWIDTH);
	SPI_setPTESignalPolarity(example_spi_BASE, SPI_PTE_ACTIVE_LOW);
	SPI_enableFIFO(example_spi_BASE);
	SPI_setFIFOInterruptLevel(example_spi_BASE, SPI_FIFO_TX8, SPI_FIFO_RX2);
	SPI_clearInterruptStatus(example_spi_BASE, SPI_INT_RXFF | SPI_INT_TXFF);
	SPI_enableInterrupt(example_spi_BASE, SPI_INT_RXFF | SPI_INT_TXFF);
	SPI_disableLoopback(example_spi_BASE);
	SPI_setEmulationMode(example_spi_BASE, SPI_EMULATION_STOP_MIDWAY);
	SPI_enableModule(example_spi_BASE);
}

