/*
 * Copyright (c) 2018, Linz Center of Mechatronics GmbH (LCM), web: www.lcm.at
 * All rights reserved.
 */
/*
 * This file is licensed according to the BSD 3-clause license as follows:
 * 
 * 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 the "Linz Center of Mechatronics GmbH" and "LCM" 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 "Linz Center of Mechatronics GmbH" 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.
 */
/*
 * This file is part of X2C. web: x2c.lcm.at
 * $LastChangedRevision: 4306 $
 * $LastChangedDate:: 2026-07-02 15:48:24 +0200#$
 */
/* USERCODE-BEGIN:Description                                                                                         */
/* Description: Calculation of fundamental wave.                              */
/* USERCODE-END:Description                                                                                           */
#include <stddef.h>
#include "FundFreq_FiP32.h"

/* all used update functions to ram for c2000                                                                         */
#if defined(__ALL_UPDATE_FUNC_2_RAM_C2000__)
    #pragma CODE_SECTION(FundFreq_FiP32_Update, "ramfuncs")
#endif

/* USERCODE-BEGIN:PreProcessor                                                                                        */
#include "Sin_Data.h"

/* Inputs */
#define IN              (*block->In)
#define PHI             (*block->phi)

/* Outputs */
#define OUT             (block->Out)

/* Variables */
#define PHI_OLD         (block->phi_old)
#define CNT             (block->cnt)
#define SUM_A           (block->sum_a)
#define SUM_B           (block->sum_b)
#define A1              (block->a)
#define B1              (block->b)

/* Constants */
#define HALFPI          0x40000000              /* pi/2 [Q31] */
#define MORE_THAN_PI    ((int64)0xC0000000u)     /* 3/2 * pi [Q31] */

/* USERCODE-END:PreProcessor                                                                                          */

/**********************************************************************************************************************/
/** Declare private functions with the C inline keyword to ensure function inlining for better runtime performance.  **/
/** Since all tested compilers DO NOT perform inlining at the LOWEST optimization level, compiler optimizations may  **/
/** need to be adjusted accordingly.                                                                                 **/
/**********************************************************************************************************************/
/* USERCODE-BEGIN:PrivateFncPrototypeOrDefinition                                                                     */
/* USERCODE-END:PrivateFncPrototypeOrDefinition                                                                       */

/**********************************************************************************************************************/
/** Update                                                                                                           **/
/**********************************************************************************************************************/
void FundFreq_FiP32_Update(FUNDFREQ_FIP32 *block)
{
/* USERCODE-BEGIN:UpdateFnc                                                                                           */
    int64 temp;
    int32 sine, cosine;

    /* Previous: LOOKUP32(Sin_Table32, PHI, sine); */
    sine = lookupTableInt32(Sin_Table32, PHI);

    /* cos(u) = sin(pi/2 - u)    Previous: LOOKUP32(Sin_Table32, (HALFPI-PHI), cosine); */
    cosine = lookupTableInt32(Sin_Table32, (HALFPI-PHI));

    /* check for start of new period (jump from -pi to +pi or vice versa) */
    if ((ABS_I64((int64)PHI - (int64)PHI_OLD)) > MORE_THAN_PI)
    {
        /* calculate coefficients */
        /* RULECHECKER_comment(1:0, 1:0, check_inappropriate_int, "see X2C MISRA Deviations, DEV0004", true_no_defect) */
        A1 = (int32)((SUM_A / (int64)CNT) << 1u);

        /* Data width limitation Previous: LIMIT(A1, INT32_MAX); */
        if (A1 > INT32_MAX)
        {
            A1 = INT32_MAX;
        }
        else
        {
            if (A1 < -INT32_MAX)
            {
                A1 = -INT32_MAX;
            }
        }

        /* RULECHECKER_comment(1:0, 1:0, check_inappropriate_int, "see X2C MISRA Deviations, DEV0004", true_no_defect) */
        B1 = (int32)((SUM_B / (int64)CNT) << 1u);

        /* Data width limitation Previous: LIMIT(B1, INT32_MAX); */
        if (B1 > INT32_MAX)
        {
            B1 = INT32_MAX;
        }
        else
        {
            if (B1 < -INT32_MAX)
            {
                B1 = -INT32_MAX;
            }
        }

        /* reset counter and sum */
        CNT = 0u;
        SUM_A = 0;
        SUM_B = 0;
    }

    /* check for counter limit */
    if (CNT != UINT32_MAX)
    {
        /* counter value hasn't reached its maximum value:
         * it's save to compute sum and to increase counter
         */
        /* RULECHECKER_comment(1:0, 2:0, check_inappropriate_int, "see X2C MISRA Deviations, DEV0002", true_no_defect) */
        SUM_A += (((int64)IN * (int64)cosine) >> 31u);
        SUM_B += (((int64)IN * (int64)sine) >> 31u);
        CNT++;
    }
    else
    {
        /* counter value has reached its maximum value:
         * reset sum to prevent false output on next jump of phi
         */
        SUM_A = 0;
        SUM_B = 0;
    }

    /* calculate first harmonic */
    temp  =  (int64)A1 * (int64)cosine;
    temp += ((int64)B1 * (int64)sine);

    /* RULECHECKER_comment(1:0, 1:0, check_inappropriate_int, "see X2C MISRA Deviations, DEV0002", true_no_defect) */
    temp = temp >> 31u;

    /* Data width limitation Previous: LIMIT(temp, INT32_MAX); */
    if (temp > INT32_MAX)
    {
        temp = INT32_MAX;
    }
    else
    {
        if (temp < -INT32_MAX)
        {
            temp = -INT32_MAX;
        }
    }

    OUT = (int32)temp;

    /* save angle value for next cycle */
    PHI_OLD = PHI;

/* USERCODE-END:UpdateFnc                                                                                             */
}

/**********************************************************************************************************************/
/** Initialization                                                                                                   **/
/**********************************************************************************************************************/
void FundFreq_FiP32_Init(FUNDFREQ_FIP32 *block)
{
    block->identifier = FUNDFREQ_FIP32_ID;
    block->Out = 0;
/* USERCODE-BEGIN:InitFnc                                                                                             */
     PHI_OLD = 0;
     CNT = 0u;
     SUM_A = 0;
     SUM_B = 0;
     A1 = 0;
     B1 = 0;

/* USERCODE-END:InitFnc                                                                                               */
}

/**********************************************************************************************************************/
/** No Load function                                                                                                 **/
/**********************************************************************************************************************/

/**********************************************************************************************************************/
/** No Save function                                                                                                 **/
/**********************************************************************************************************************/

/**********************************************************************************************************************/
/** Get block element address                                                                                        **/
/**********************************************************************************************************************/
#if !defined(FUNDFREQ_FIP32_ISLINKED)
void* FundFreq_FiP32_GetAddress(FUNDFREQ_FIP32* block, uint16 elementId)
{
    void* addr;
    switch (elementId)
    {
        case 1:
            addr = block->In;
            break;
        case 2:
            addr = block->phi;
            break;
        case 3:
            addr = &block->Out;
            break;
        default:
            addr = NULL;
            break;
    }
    return (addr);
}
#endif

/**********************************************************************************************************************/
/** Declare private functions with the C inline keyword to ensure function inlining for better runtime performance.  **/
/** Since all tested compilers DO NOT perform inlining at the LOWEST optimization level, compiler optimizations may  **/
/** need to be adjusted accordingly.                                                                                 **/
/**********************************************************************************************************************/
/* USERCODE-BEGIN:PrivateFncDefinition                                                                                */
/* USERCODE-END:PrivateFncDefinition                                                                                  */
