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

/* all used update functions to ram for c2000                                                                         */
#if defined(__ALL_UPDATE_FUNC_2_RAM_C2000__)
    #pragma CODE_SECTION(IIR_Float32_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 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_Float32_Update(IIR_FLOAT32 *block)
{
/* USERCODE-BEGIN:UpdateFnc                                                                                           */
    int8 i;
    float32 sumA = 0.0f, sumB = 0.0f;

    /* 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 += (COEFF_B[i] * BUFFER_IN[(IDX_IN + i) % ORDER_N]);
        sumA += (COEFF_A[i] * BUFFER_OUT[(IDX_OUT + i) % ORDER_D]);
    }
    for (i=ORDER_N; i < ORDER_D; i++)
    {
        sumA += (COEFF_A[i] * BUFFER_OUT[(IDX_OUT + i) % ORDER_D]);
    }

    /* calculate total sum and assign output */
    OUT = sumB - sumA;

/* USERCODE-END:UpdateFnc                                                                                             */
}

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

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

/**********************************************************************************************************************/
/** Save block data                                                                                                  **/
/**********************************************************************************************************************/
uint8 IIR_Float32_Save(IIR_FLOAT32 *block, const uint8 data[], uint16 dataLength)
{
    uint8 error;
    if (dataLength != 254u)
    {
        error = 1u;
    }
    else
    {
        uint16 i;
        uint32 tmp32;
        for (i=0; i<32u; i++)
        {
            tmp32 = (uint32)((uint32)data[(4u*i)] | ((uint32)data[(4u*i) + 1u] << 8) | ((uint32)data[(4u*i) + 2u] << 16) | ((uint32)data[(4u*i) + 3u] << 24));
            (void) memcpy((void*)&block->coeffB[i], (const void*)&tmp32, sizeof(float32));
        }
        for (i=0; i<31u; i++)
        {
            tmp32 = (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));
            (void) memcpy((void*)&block->coeffA[i], (const void*)&tmp32, sizeof(float32));
        }
        block->orderN = castUint8ToInt8(data[252]);
        block->orderD = castUint8ToInt8(data[253]);
        error = 0u;
/* USERCODE-BEGIN:SaveFnc                                                                                             */
/* USERCODE-END:SaveFnc                                                                                               */
    }
    return (error);
}

/**********************************************************************************************************************/
/** Get block element address                                                                                        **/
/**********************************************************************************************************************/
#if !defined(IIR_FLOAT32_ISLINKED)
void* IIR_Float32_GetAddress(IIR_FLOAT32* 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                                                                                  */
