/*
 * 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: */
/* USERCODE-END:Description                                                                                           */
#include <stddef.h>
#include "IIR_FiP32.h"

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

/* USERCODE-BEGIN:PreProcessor                                                                                        */
/* Inputs */
#define IN              (*block->In)

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

/* Parameter */
#if !defined(X2C_USE_UNION_FOR_POINTER)
#define COEFF_A         (block->coeffA)       /* Denominator coefficients array in reversed order (an,...,a2,a1) */
#define COEFF_B         (block->coeffB)       /* Numerator coefficients array in reversed order (bn,...,b2,b1,b0) */
#else
#define COEFF_A         (block->coeffA.ptr)   /* Denominator coefficients array in reversed order (an,...,a2,a1) */
#define COEFF_B         (block->coeffB.ptr)   /* Numerator coefficients array in reversed order (bn,...,b2,b1,b0) */
#endif
#define SFRB            (block->sfrB)         /* Shift factor for numerator coefficients */
#define SFRA            (block->sfrA)         /* Shift factor for denominator coefficients */
#define ORDER_N         (block->orderN)       /* Length of numerator coefficients array */
#define ORDER_D         (block->orderD)       /* Length of denominator coefficients array */

/* Variables */
#if !defined(X2C_USE_UNION_FOR_POINTER)
#define BUFFER_IN       (block->inputBuffer)  /* Circular buffer for input signal */
#define BUFFER_OUT      (block->outputBuffer) /* Circular buffer for output signal */
#else
#define BUFFER_IN       (block->inputBuffer.ptr)  /* Circular buffer for input signal */
#define BUFFER_OUT      (block->outputBuffer.ptr) /* Circular buffer for output signal */
#endif
#define IDX_IN          (block->indexIn)      /* Pointer to current position in input buffer */
#define IDX_OUT         (block->indexOut)     /* Pointer to current position in output buffer */

/* Constants */
#define BUFFER_LEN      (32)                        /* Maximum length of circular buffer */

/* 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 IIR_FiP32_Update(IIR_FIP32 *block)
{
/* USERCODE-BEGIN:UpdateFnc                                                                                           */
    int8 i;
    int64 sumA = 0, sumB = 0;

    /* enter In(k) and Out(k-1) in circular buffer */
    BUFFER_IN[IDX_IN]   = IN;
    BUFFER_OUT[IDX_OUT] = OUT;

    /* increase pointers to circular buffers */
    IDX_IN  = (IDX_IN  + 1) % ORDER_N;  /* pointer to element k-N */
    IDX_OUT = (IDX_OUT + 1) % ORDER_D;  /* pointer to element k-D */

    /* calculate sums of numerator and denominator */
    for (i=0; i < ORDER_N; i++)
    {
        sumB += ((int64)COEFF_B[i] * (int64)BUFFER_IN[(IDX_IN + i) % ORDER_N]);
        sumA += ((int64)COEFF_A[i] * (int64)BUFFER_OUT[(IDX_OUT + i) % ORDER_D]);
    }
    for (i=ORDER_N; i < ORDER_D; i++)
    {
        sumA += ((int64)COEFF_A[i] * (int64)BUFFER_OUT[(IDX_OUT + i) % ORDER_D]);
    }

    /* RULECHECKER_comment(1:0, 2:0, check_inappropriate_int, "see X2C MISRA Deviations, DEV0001", true_no_defect) */
    sumA = sumA >> SFRA;
    sumB = sumB >> SFRB;

    /* calculate total sum */
    sumA = sumB - sumA;

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

    /* assign output */
    OUT = (int32)sumA;

/* USERCODE-END:UpdateFnc                                                                                             */
}

/**********************************************************************************************************************/
/** Initialization                                                                                                   **/
/**********************************************************************************************************************/
void IIR_FiP32_Init(IIR_FIP32 *block)
{
    block->identifier = IIR_FIP32_ID;
    block->Out = 0;
/* USERCODE-BEGIN:InitFnc                                                                                             */
    IDX_IN = 0;
    IDX_OUT = 0;
    {
        int8 i;
        for (i=0; i < (BUFFER_LEN-1); i++)
        {
            BUFFER_IN[i]  = 0;
            BUFFER_OUT[i] = 0;
        }
        BUFFER_IN[i+1] = 0;
    }
/* USERCODE-END:InitFnc                                                                                               */
}

/**********************************************************************************************************************/
/** Load block data                                                                                                  **/
/**********************************************************************************************************************/
uint8 IIR_FiP32_Load(const IIR_FIP32 *block, uint8 data[], uint16 *dataLength, uint16 maxSize)
{
    uint8 error = 0u;
    if (maxSize < 256u)
    {
        error = 1u;
    }
    else
    {
        uint16 i;
        for (i=0; i<32u; i++)
        {
            data[(4u*i)] = (uint8)((uint32)block->coeffB[i] & 0xFFu);
            data[(4u*i) + 1u] = (uint8)(((uint32)block->coeffB[i] >> 8) & 0xFFu);
            data[(4u*i) + 2u] = (uint8)(((uint32)block->coeffB[i] >> 16) & 0xFFu);
            data[(4u*i) + 3u] = (uint8)(((uint32)block->coeffB[i] >> 24) & 0xFFu);
        }
        for (i=0; i<31u; i++)
        {
            data[128u + (4u*i)] = (uint8)((uint32)block->coeffA[i] & 0xFFu);
            data[128u + (4u*i) + 1u] = (uint8)(((uint32)block->coeffA[i] >> 8) & 0xFFu);
            data[128u + (4u*i) + 2u] = (uint8)(((uint32)block->coeffA[i] >> 16) & 0xFFu);
            data[128u + (4u*i) + 3u] = (uint8)(((uint32)block->coeffA[i] >> 24) & 0xFFu);
        }
        data[252] = (uint8)(block->sfrB & 0xFFu);
        data[253] = (uint8)(block->sfrA & 0xFFu);
        data[254] = (uint8)((uint8)block->orderN & 0xFFu);
        data[255] = (uint8)((uint8)block->orderD & 0xFFu);
        *dataLength = 256u;
/* USERCODE-BEGIN:LoadFnc                                                                                             */
/* USERCODE-END:LoadFnc                                                                                               */
    }
    return (error);
}

/**********************************************************************************************************************/
/** Save block data                                                                                                  **/
/**********************************************************************************************************************/
uint8 IIR_FiP32_Save(IIR_FIP32 *block, const uint8 data[], uint16 dataLength)
{
    uint8 error;
    if (dataLength != 256u)
    {
        error = 1u;
    }
    else
    {
        uint16 i;
        for (i=0; i<32u; i++)
        {
            block->coeffB[i] = castUint32ToInt32((uint32)((uint32)data[(4u*i)] | ((uint32)data[(4u*i) + 1u] << 8) | ((uint32)data[(4u*i) + 2u] << 16) | ((uint32)data[(4u*i) + 3u] << 24)));
        }
        for (i=0; i<31u; i++)
        {
            block->coeffA[i] = castUint32ToInt32((uint32)((uint32)data[128u + (4u*i)] | ((uint32)data[128u + (4u*i) + 1u] << 8) | ((uint32)data[128u + (4u*i) + 2u] << 16) | ((uint32)data[128u + (4u*i) + 3u] << 24)));
        }
        block->sfrB = data[252];
        block->sfrA = data[253];
        block->orderN = castUint8ToInt8(data[254]);
        block->orderD = castUint8ToInt8(data[255]);
        error = 0u;
/* USERCODE-BEGIN:SaveFnc                                                                                             */
/* USERCODE-END:SaveFnc                                                                                               */
    }
    return (error);
}

/**********************************************************************************************************************/
/** Get block element address                                                                                        **/
/**********************************************************************************************************************/
#if !defined(IIR_FIP32_ISLINKED)
void* IIR_FiP32_GetAddress(IIR_FIP32* block, uint16 elementId)
{
    void* addr;
    switch (elementId)
    {
        case 1:
            addr = block->In;
            break;
        case 2:
            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                                                                                  */
