/*
 * 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_FiP16.h"

/* all used update functions to ram for c2000                                                                         */
#if defined(__ALL_UPDATE_FUNC_2_RAM_C2000__)
    #pragma CODE_SECTION(FundFreq_FiP16_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          0x4000              /* pi/2 [Q15] */
#define MORE_THAN_PI    ((int32)0xC000)     /* 3/2 * pi [Q15] */

/* 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_FiP16_Update(FUNDFREQ_FIP16 *block)
{
/* USERCODE-BEGIN:UpdateFnc                                                                                           */
    int32 temp;
    int16 sine, cosine;

    /* Previous: LOOKUP16(Sin_Table16, PHI, sine); */
    sine = lookupTableInt16(Sin_Table16, PHI);


    /* cos(u) = sin(pi/2 - u)   Previous: LOOKUP16(Sin_Table16, (HALFPI-PHI), cosine); */
    cosine = lookupTableInt16(Sin_Table16, (HALFPI-PHI));

    /* check for start of new period (jump from -pi to +pi or vice versa) */
    if ((ABS_I32((int32)PHI - (int32)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 = (int16)((SUM_A / (int32)CNT) << 1u);

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

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

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

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

    /* check for counter limit */
    if (CNT != (uint16)UINT16_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 += (((int32)IN * (int32)cosine) >> 15u);
        SUM_B += (((int32)IN * (int32)sine) >> 15u);
        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  =  (int32)A1 * (int32)cosine;
    temp += ((int32)B1 * (int32)sine);

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

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

    OUT = (int16)temp;

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

/* USERCODE-END:UpdateFnc                                                                                             */
}

/**********************************************************************************************************************/
/** Initialization                                                                                                   **/
/**********************************************************************************************************************/
void FundFreq_FiP16_Init(FUNDFREQ_FIP16 *block)
{
    block->identifier = FUNDFREQ_FIP16_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_FIP16_ISLINKED)
void* FundFreq_FiP16_GetAddress(FUNDFREQ_FIP16* 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                                                                                  */
