/*********************************************************************************************************************
 * Copyright (c) 2019, Infineon Technologies AG
 *
 * 
 * Distributed under the Boost Software License, Version 1.0.
 * 
 * 
 * Boost Software License - Version 1.0 - August 17th, 2003
 * 
 * Permission is hereby granted, free of charge, to any person or organization
 * obtaining a copy of the software and accompanying documentation covered by
 * this license (the "Software") to use, reproduce, display, distribute,
 * execute, and transmit the Software, and to prepare derivative works of the
 * Software, and to permit third-parties to whom the Software is furnished to
 * do so, all subject to the following:
 * 
 * The copyright notices in the Software and this entire statement, including
 * the above license grant, this restriction and the following disclaimer,
 * must be included in all copies of the Software, in whole or in part, and
 * all derivative works of the Software, unless such copies or derivative
 * works are solely in the form of machine-executable object code generated by
 * a source language processor.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 * 
 *********************************************************************************************************************/


/****************************************************************************************************//**
 * @file        TLE94x1_SPI.c
 *
 * @brief       Implementation of all SPI related functions
 *
 * @version     V1.0.0
 * @date        15. April 2019
 * @author      Markus Noll / markus.noll@infineon.com
 * @author      Yannek Micha Rixen / Yannek.Rixen@infineon.com
 *******************************************************************************************************/




/* ================================================================================ */
/* ============================   HEADER FILES     ================================ */
/* ================================================================================ */ 

#include "TLE94x1_SPI.h"
#include "mcu.h"

void SPI_PortInit(void);




/* ================================================================================ */
/* =======================   SPI communication functions     ====================== */
/* ================================================================================ */ 


uint8_t spitxbuf[10],spirxbuf[10];
extern McuType mcu;
uint8_t SBC_SPI_INIT(void) {
    SpiDrv_InitCfgType masterCfg;

    SPI_PortInit();

    SpiDrv_GetDefaultConfig(&masterCfg);
    masterCfg.basicParameters.isMasterNode = true;
    masterCfg.basicParameters.baudRate     = 1000000ul;
    masterCfg.basicParameters.pcsSelection     = SPIDRV_PCS_SEL_PCS0;
    //masterCfg.basicParameters.clockPolarity = SPIDRV_CLK_POLARITY_IDLE_IN_HIGH;
    masterCfg.basicParameters.clockPhase    = SPIDRV_CLK_PHASE_DATA_SAMPLE_ON_TRAILING_EDGE;

    while(ClockDrv_GetFreq(&mcu.clockDrv, CLOCKDRV_SPI0, &masterCfg.basicParameters.busClockFreq) == false)
    {
        ;
    }
    SpiDrv_SetConfig(&mcu.spiDrv0, &masterCfg);

    SpiDrv_SetRxFifoWaterMask(&mcu.spiDrv0, 0);
    //SpiDrv_SetIsrConfig(&mcu.spiDrv0, SPIDRV_ISR_SRC_RX_DATA, true);

    /* Enable the SPI interrupts */
    return 0;
}



uint16_t SBC_SPI_TRANSFER16(uint8_t Upper, uint8_t Lower) {
    uint16_t ret;
    //LPSPI_DRV_SetPcs(LPSPICOM1,LPSPI_PCS0,LPSPI_ACTIVE_LOW);
    spitxbuf[0] = Upper;
    spitxbuf[1] = Lower;

    SpiDrv_MasterSyncTransfer(&mcu.spiDrv0,SPIDRV_FRAME_SIZE_BIT_TYPE_BYTE,2,spitxbuf,spirxbuf);
    //LPSPI_DRV_MasterTransferBlocking(LPSPICOM1,spitxbuf,spirxbuf,2,10);
    ret = spirxbuf[0];
    ret <<= 8;
    ret |= spirxbuf[1];
    //LPSPI_DRV_SetPcs(LPSPICOM1,LPSPI_PCS0,LPSPI_ACTIVE_HIGH);
    return ret;
}


/********************CVA SPI DRV**********************/

void SPI0_Handler(void)
{
    if(SpiDrv_GetStatus(&mcu.spiDrv0, SPIDRV_STATUS_RX_DATA) == true && SpiDrv_GetIsrConfig(&mcu.spiDrv0, SPIDRV_ISR_SRC_RX_DATA) == true)
    {
        //spi0IrqCnt++;
        //uint8_t len = SpiDrv_AsyncReceive(&mcu.spiDrv0, (void *)masterRxBuff, true);
    }
}

void SPI_PortInit(void)
{
    /* SPI CS */
    PinsDrv_SetMuxModeSel(&mcu.pta, 12, PINSDRV_MUX_ALT7);
    /* Strength driver */
    PortReg_SetPcrDrvStr(mcu.pta.port, 12, 1);
    /* fast slew rate */
    PortReg_SetPcrSr(mcu.pta.port, 12, 1);

    /* SPI CLK */
    PinsDrv_SetMuxModeSel(&mcu.pta, 11, PINSDRV_MUX_ALT7);
    /* Strength driver */
    PortReg_SetPcrDrvStr(mcu.pta.port, 11, 1);
    /* fast slew rate */
    PortReg_SetPcrSr(mcu.pta.port, 11, 1);

    /* SPI SIN */
    PinsDrv_SetMuxModeSel(&mcu.pta, 10, PINSDRV_MUX_ALT7);
    /* Strength driver */
    PortReg_SetPcrDrvStr(mcu.pta.port, 10, 1);
    /* fast slew rate */
    PortReg_SetPcrSr(mcu.pta.port, 10, 1);

    /* SPI SOUT */
    PinsDrv_SetMuxModeSel(&mcu.pta, 9, PINSDRV_MUX_ALT7);
    /* Strength driver */
    PortReg_SetPcrDrvStr(mcu.pta.port, 9, 1);
    /* fast slew rate */
    PortReg_SetPcrSr(mcu.pta.port, 9, 1);
}

void SPI_TEST_TASK(void)
{
    
}