/* This file is part of X2C. web: x2c.lcm.at                                                                          */

/* Model: DemoApplication                                                                                             */
/* Date:  2025-11-27 13:58                                                                                            */

/* X2C-Version: 6.5.3948                                                                                              */
/* X2C-Edition: Development                                                                                           */

/* Common includes                                                                                                    */
#include <stddef.h>
#include "TableStruct.h"
/* Generated file includes                                                                                            */
#include "FlashTable.h"
#include "RamTable.h"
#include "X2C.h"

/**********************************************************************************************************************/
/**                                                       Model                                                      **/
/**********************************************************************************************************************/
struct x2cModel x2cModel;

/* Model identifier                                                                                                   */
const uint32 x2cModelIdentifier = 0xC9D0907Fu;

/**********************************************************************************************************************/
/**                                                       Scope                                                      **/
/**********************************************************************************************************************/
SCOPE_MAIN x2cScope;

/* Block function table                                                                                               */
const tBlockFunctions blockFunctionTable[] = {
    FUNCTIONS,
    { 0u, NULL, NULL, NULL, NULL, NULL } /* End of table entry */
};

/* Parameter identifier table                                                                                         */
const tParameterTable parameterIdTable[] = {
    PARAMETER_TABLE,
    { 0u, NULL } /* End of table entry */
};

/* Inport Parameter identifier table                                                                                  */
const tIoParamIdEntry inportParamIdTable[] = {
    INPORT_PARAMID_TABLE, 
    { 0u, 0u, NULL } /* End of table entry */
};

/* Outport Parameter identifier table                                                                                 */
const tIoParamIdEntry outportParamIdTable[] = {
    OUTPORT_PARAMID_TABLE, 
    { 0u, 0u, NULL } /* End of table entry */
};

/* Mask Parameter identifier table                                                                                    */
const tMaskParameterEntry maskParamIdTable[] = {
    { 0u, NULL, NULL, NULL, NULL } /* End of table entry */
};

/* Extended Mask Parameter identifier table (used by Model API)                                                       */
const tMaskParamExtRecord maskParamExtTable[] = {
    { 0u, 0u, NULL, NULL, NULL, NULL, NULL } /* End of table entry */
};

/* Mask Parameter data table (used by Model API)                                                                      */
const tMaskParamDataRecord maskParamDataTable[] = {
    { 0u, 0u, NULL, 0u, 0u } /* End of table entry */
};

/**********************************************************************************************************************/
/** Model Sample Time                                                                                                **/
/**********************************************************************************************************************/
float_CoT getModelSampleTime()
{
    return ((float_CoT)1.0E-4);
}

/**********************************************************************************************************************/
/**                                                  Initialization                                                  **/
/**********************************************************************************************************************/
void X2C_Init(void)
{
    /******************************************************************************************************************/
    /**                                          Initialize Block Parameters                                         **/
    /******************************************************************************************************************/

    /* Block: Amplitude                                                                                               */
    /* Value = 0.8                                                                                                    */
    x2cModel.blocks.bAmplitude.K = 26214;

    /* Block: D17                                                                                                     */
    /* Value = 0.0                                                                                                    */
    x2cModel.blocks.bD17.K = 0;

    /* Block: D2                                                                                                      */
    /* Value = 0.0                                                                                                    */
    x2cModel.blocks.bD2.K = 0;

    /* Block: Sin3Gen                                                                                                 */
    /* fmax = 1000.0                                                                                                  */
    /* Offset = 0.0                                                                                                   */
    /* ts_fact = 1.0                                                                                                  */
    x2cModel.blocks.bSin3Gen.delta_phi = 6554;
    x2cModel.blocks.bSin3Gen.offset = 0;
    x2cModel.blocks.bSin3Gen.phi = 0;

    /* Block: Start_Stop                                                                                              */
    /* Value = 1.0                                                                                                    */
    x2cModel.blocks.bStart_Stop.K = 1;

    /* Block: TriangleGenerator/AutoSwitch                                                                            */
    /* Thresh_up = 0.03                                                                                               */
    /* Thresh_down = 0.0                                                                                              */
    x2cModel.blocks.sTriangleGenerator.bAutoSwitch.Thresh_up = 983;
    x2cModel.blocks.sTriangleGenerator.bAutoSwitch.Thresh_down = 0;
    x2cModel.blocks.sTriangleGenerator.bAutoSwitch.Status = &RamTable_int16[0];

    /* Block: TriangleGenerator/I                                                                                     */
    /* Ki = 1.0                                                                                                       */
    /* ts_fact = 1.0                                                                                                  */
    x2cModel.blocks.sTriangleGenerator.bI.b0 = 3;
    x2cModel.blocks.sTriangleGenerator.bI.sfr = 15;
    x2cModel.blocks.sTriangleGenerator.bI.i_old = 0;
    x2cModel.blocks.sTriangleGenerator.bI.enable_old = 0;

    /* Block: TriangleGenerator/Init                                                                                  */
    /* Value = 0.0                                                                                                    */
    x2cModel.blocks.sTriangleGenerator.bInit.K = 0;

    /* Block: TriangleGenerator/LoopBreaker                                                                           */

    /* Block: TriangleGenerator/Negation                                                                              */

    /* Block: TriangleGenerator/Slope                                                                                 */
    /* Value = 0.01                                                                                                   */
    x2cModel.blocks.sTriangleGenerator.bSlope.K = 328;


    /* Initialize RAM table content */
    initRamTables();

    /******************************************************************************************************************/
    /**                                              Initialize Inports                                              **/
    /******************************************************************************************************************/
    x2cModel.inports.bHA = false;
    x2cModel.inports.bHB = false;
    x2cModel.inports.bHC = false;
    x2cModel.inports.bHOME = false;
    x2cModel.inports.bI1 = (int16)0;
    x2cModel.inports.bI2 = (int16)0;
    x2cModel.inports.bISum = (int16)0;
    x2cModel.inports.bPOT = (int16)0;
    x2cModel.inports.bS2 = false;
    x2cModel.inports.bS3 = false;
    x2cModel.inports.bVBUS = (int16)0;

    /******************************************************************************************************************/
    /**                                               Link Block Inputs                                              **/
    /******************************************************************************************************************/

    /* Block Amplitude                                                                                                */

    /* Block D17                                                                                                      */

    /* Block D2                                                                                                       */

    /* Block Sin3Gen                                                                                                  */
    x2cModel.blocks.bSin3Gen.A =
        &x2cModel.blocks.bAmplitude.Out;
    x2cModel.blocks.bSin3Gen.f =
        &x2cModel.blocks.sTriangleGenerator.bI.Out;

    /* Block Start_Stop                                                                                               */

    /* Block AutoSwitch                                                                                               */
    x2cModel.blocks.sTriangleGenerator.bAutoSwitch.In1 =
        &x2cModel.blocks.sTriangleGenerator.bNegation.Out;
    x2cModel.blocks.sTriangleGenerator.bAutoSwitch.Switch =
        &x2cModel.blocks.sTriangleGenerator.bLoopBreaker.Out;
    x2cModel.blocks.sTriangleGenerator.bAutoSwitch.In3 =
        &x2cModel.blocks.sTriangleGenerator.bSlope.Out;

    /* Block I                                                                                                        */
    x2cModel.blocks.sTriangleGenerator.bI.In =
        &x2cModel.blocks.sTriangleGenerator.bAutoSwitch.Out;
    x2cModel.blocks.sTriangleGenerator.bI.I0 =
        &x2cModel.blocks.sTriangleGenerator.bInit.Out;
    x2cModel.blocks.sTriangleGenerator.bI.Enable =
        &x2cModel.blocks.bStart_Stop.Out;

    /* Block Init                                                                                                     */

    /* Block LoopBreaker                                                                                              */
    x2cModel.blocks.sTriangleGenerator.bLoopBreaker.In =
        &x2cModel.blocks.sTriangleGenerator.bI.Out;

    /* Block Negation                                                                                                 */
    x2cModel.blocks.sTriangleGenerator.bNegation.In =
        &x2cModel.blocks.sTriangleGenerator.bSlope.Out;

    /* Block Slope                                                                                                    */

    /******************************************************************************************************************/
    /**                                                 Link Outports                                                **/
    /******************************************************************************************************************/
    x2cModel.outports.bLED17 =
        &x2cModel.blocks.bD17.Out;
    x2cModel.outports.bLED2 =
        &x2cModel.blocks.bD2.Out;
    x2cModel.outports.bPWM1 =
        &x2cModel.blocks.bSin3Gen.u;
    x2cModel.outports.bPWM2 =
        &x2cModel.blocks.bSin3Gen.v;
    x2cModel.outports.bPWM3 =
        &x2cModel.blocks.bSin3Gen.w;

    /******************************************************************************************************************/
    /**                                           Run Block Init Functions                                           **/
    /******************************************************************************************************************/
    Constant_FiP16_Init(&x2cModel.blocks.bAmplitude);
    Constant_Bool_Init(&x2cModel.blocks.bD17);
    Constant_Bool_Init(&x2cModel.blocks.bD2);
    Sin3Gen_FiP16_Init(&x2cModel.blocks.bSin3Gen);
    Constant_Bool_Init(&x2cModel.blocks.bStart_Stop);
    AutoSwitch_FiP16_Init(&x2cModel.blocks.sTriangleGenerator.bAutoSwitch);
    I_FiP16_Init(&x2cModel.blocks.sTriangleGenerator.bI);
    Constant_FiP16_Init(&x2cModel.blocks.sTriangleGenerator.bInit);
    LoopBreaker_FiP16_Init(&x2cModel.blocks.sTriangleGenerator.bLoopBreaker);
    Negation_FiP16_Init(&x2cModel.blocks.sTriangleGenerator.bNegation);
    Constant_FiP16_Init(&x2cModel.blocks.sTriangleGenerator.bSlope);
    Scope_Main_Init(&x2cScope);

    /* Initialize TableStruct tables                                                                                  */
    TableStruct->TFncTable = blockFunctionTable;
    TableStruct->TParamTable = parameterIdTable;
    TableStruct->inportParamTable = inportParamIdTable;
    TableStruct->outportParamTable = outportParamIdTable;
    TableStruct->maskParameterTable = maskParamIdTable;
}

/**********************************************************************************************************************/
/**                                            Run Block Update Functions                                            **/
/**********************************************************************************************************************/
void X2C_Update(void)
{
    X2C_Update_1();

}

/* X2C_Update for blocks with 1*Ts                                                                                    */
void X2C_Update_1(void)
{
    LoopBreaker_FiP16_Update(&x2cModel.blocks.sTriangleGenerator.bLoopBreaker);
    Negation_FiP16_Update(&x2cModel.blocks.sTriangleGenerator.bNegation);
    AutoSwitch_FiP16_Update(&x2cModel.blocks.sTriangleGenerator.bAutoSwitch);
    I_FiP16_Update(&x2cModel.blocks.sTriangleGenerator.bI);
    Sin3Gen_FiP16_Update(&x2cModel.blocks.bSin3Gen);
    Scope_Main_Update(&x2cScope);
}

