/**
 * \file       Fusa_Cpu.c
 * \author     Yang Zuqiao
 * \brief      Source file for fusa CAN Loop Back
 * \copyright  (c) 2024 Preh GmbH, all rights reserved.
 */

/*----------------------------------------------------------------------------*/
/*                                  Includes                                  */
/*----------------------------------------------------------------------------*/
#include "Fusa_Cpu.h"
#include "hw_memmap.h"
#include "hw_pie.h"
#include "stdlib.h"
#include "gpio.h"
#include "pin_map.h"
#include "interrupt.h"

/*----------------------------------------------------------------------------*/
/*                                Micro Define                                */
/*----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------*/
/*                      Definition of RAM-Variables                           */
/*----------------------------------------------------------------------------*/
uint16 numPEST = 0;

uint16 test_1 = 0;
uint16 test_2 = 0;
/*----------------------------------------------------------------------------*/
/*                             Local data at RAM                              */
/*----------------------------------------------------------------------------*/
uint16 STA_Tests_report[STL_CLA_POST_NUM_VECTS] = {0};
uint16 STA_Tests_report1[STL_CLA_PEST_NUM_VECTS] = {0};

/*----------------------------------------------------------------------------*/
/*                     Declaration of local functions                         */
/*----------------------------------------------------------------------------*/
static uint16 STA_Tests_CyclictestDevice(void);
static uint16 STA_Tests_StartuptestDevice(void);

/*----------------------------------------------------------------------------*/
/*                     Implementation of global functions                     */
/*----------------------------------------------------------------------------*/
uint8 FusaStl_StartupCheck(void)
{
    uint16 returnVal = E_NOT_OK;

    test_1 = STA_Tests_StartuptestDevice();

    returnVal = test_1;
    return returnVal;
}

uint8 FusaStl_CyclicCheck(void)
{
    uint16 returnVal = E_NOT_OK;

    test_2 = STA_Tests_CyclictestDevice();

    returnVal = test_2;
    return returnVal;
}
/*----------------------------------------------------------------------------*/
/*                     Implementation of local functions                      */
/*---------------------------------------static-------------------------------------*/
static uint16 STA_Tests_CyclictestDevice(void)
{
    uint16 returnVal = STL_CLA_PASS;

    uint16 delay = 50U;
    //
    // Clear task complete flag in PIE.
    //
    HWREGH(PIECTRL_BASE + PIE_O_IFR11) = 0x0000U;

    //
    // Copy test segment to RAMLS0 for CLA execution.
    //
    STL_CLA_bootVector((uint32 *)STL_CLA_pestHandleArray[numPEST]->vectorAddress, (size_t)STL_CLA_pestHandleArray[numPEST]->vectorLength);

    //
    // Run a micro-run of CLASWBIST.
    //
    returnVal = STL_CLA_runPESTMicro((STL_CLA_PESTVector)numPEST, delay);
    STA_Tests_report1[numPEST] = returnVal;

    numPEST++;
    numPEST = numPEST % STL_CLA_PEST_NUM_VECTS;

    return returnVal;
}

static uint16 STA_Tests_StartuptestDevice(void)
{
    uint16 returnVal = STL_CLA_PASS;

    uint16 delay = 50U;
    uint32 claBase = CLA1_BASE;
    sint16 numPOSTAll;

    STL_CLA_configMemory();

    returnVal |= STL_CLA_testRegisters1(claBase);
    returnVal |= STL_CLA_testRegisters2(claBase);
    returnVal |= STL_CLA_testRegisters3(claBase, delay);

    for(numPOSTAll = 0; numPOSTAll < STL_CLA_POST_NUM_VECTS; numPOSTAll++)
    {
        STL_CLA_bootVector((uint32 *)STL_CLA_postHandleArray[numPOSTAll]->vectorAddress, (size_t)STL_CLA_postHandleArray[numPOSTAll]->vectorLength);

        if (STL_CLA_POST_VECTOR_11 == numPOSTAll)
        {
            delay = STL_CLA_FPU_TEST_DELAY;  // POST vector 11 - FPU tests - POST only cannot qualify for PEST
        }

        STA_Tests_report[numPOSTAll] = STL_CLA_runPOSTMicro((STL_CLA_POSTVector)numPOSTAll, delay);
    }

    return returnVal;
}
