/*
 * Copyright (c) 2023 Juan Manuel Cruz <jcruz@fi.uba.ar> <jcruz@frba.utn.edu.ar>.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. 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.
 *
 * 3. Neither the name of the copyright holder 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 THE
 * COPYRIGHT HOLDER OR CONTRIBUTORS 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.
 *
 * @file   : app.c
 * @date   : Set 26, 2023
 * @author : Juan Manuel Cruz <jcruz@fi.uba.ar> <jcruz@frba.utn.edu.ar>
 * @version	v1.0.0
 */

/********************** inclusions *******************************************/
/* Project includes */
#include "main.h"

/* Demo includes */
#include "logger.h"
#include "dwt.h"

/* Application & Tasks includes */
#include "board.h"
#include "task_system.h"
#include "task_actuator.h"
#include "task_sensor.h"

/********************** macros and definitions *******************************/
#define G_APP_CNT_INI       0ul
#define G_APP_TICK_CNT_INI  0ul
#define G_APP_WCET_INI      0ul

#define TASK_X_WCET_INI     0ul
#define TASK_X_DELAY_MIN    0ul

typedef struct {
    void (*task_init)(void *);
    void (*task_update)(void *);
    void *parameters;
} task_cfg_t;

typedef struct {
    uint32_t WCET; // Task worst-case execution time (microseconds)
} task_dta_t;

/********************** internal data declaration ****************************/
const task_cfg_t task_cfg_list[] = {
    {task_sensor_init,   task_sensor_update,   NULL},
    {task_system_init,   task_system_update,   NULL},
    {task_actuator_init, task_actuator_update, NULL}
};

#define TASK_QTY (sizeof(task_cfg_list)/sizeof(task_cfg_t))

/********************** internal functions declaration ***********************/

/********************** internal data definition *****************************/
const char *p_sys = " Bare Metal - Event-Triggered Systems (ETS)";
const char *p_app = " App - Model Integration - C codig";

/********************** external data declaration ****************************/
uint32_t g_app_cnt;
uint32_t g_app_runtime_us;
uint32_t g_app_WCET_us; // Worst case execution time historically

volatile uint32_t g_app_tick_cnt;

task_dta_t task_dta_list[TASK_QTY];

/********************** external functions definition ************************/
void app_init(void)
{
    uint32_t index;

    /* Print out: Application Initialized */
    LOGGER_INFO(" ");
    LOGGER_INFO("%s is running - Tick [mS] = %lu", GET_NAME(app_init), HAL_GetTick());

    LOGGER_INFO(p_sys);
    LOGGER_INFO(p_app);

    /* Init & Print out: Application execution counter */
    g_app_cnt = G_APP_CNT_INI;
    LOGGER_INFO(" %s = %lu", GET_NAME(g_app_cnt), g_app_cnt);

    /* Init application worst case execution time */
    g_app_WCET_us = G_APP_WCET_INI;

    /* Init Cycle Counter */
    cycle_counter_init();

    /* Go through the task arrays */
    for (index = 0; TASK_QTY > index; index++)
    {
        /* Run task_x_init */
        (*task_cfg_list[index].task_init)(task_cfg_list[index].parameters);

        /* Init variables */
        task_dta_list[index].WCET = TASK_X_WCET_INI;
    }

    /* Protect shared resource */
    __asm("CPSID i");	/* disable interrupts */
    /* Init Tick Counter */
    g_app_tick_cnt = G_APP_TICK_CNT_INI;

    g_task_sensor_tick_cnt = G_APP_TICK_CNT_INI;
    g_task_system_tick_cnt = G_APP_TICK_CNT_INI;
    g_task_actuator_tick_cnt = G_APP_TICK_CNT_INI;
    __asm("CPSIE i");	/* enable interrupts */
}

void app_update(void)
{
    uint32_t index;
    bool b_time_update_required = false;
    uint32_t cycle_counter_time_us;

    /* Protect shared resource */
    __asm("CPSID i");	/* disable interrupts */
    if (G_APP_TICK_CNT_INI < g_app_tick_cnt)
    {
        /* Update Tick Counter */
        g_app_tick_cnt--;
        b_time_update_required = true;
    }
    __asm("CPSIE i");	/* enable interrupts */

    /* Check if it's time to run tasks */
    while (b_time_update_required)
    {
        /* Update App Counter */
        g_app_cnt++;
        g_app_runtime_us = 0;

        /* Go through the task arrays */
        for (index = 0; TASK_QTY > index; index++)
        {
            cycle_counter_reset();

            /* Run task_x_update */
            (*task_cfg_list[index].task_update)(task_cfg_list[index].parameters);

            cycle_counter_time_us = cycle_counter_get_time_us();

            /* Update variables */
            g_app_runtime_us += cycle_counter_time_us;

            if (g_app_WCET_us < g_app_runtime_us) {
                g_app_WCET_us = g_app_runtime_us;
                LOGGER_INFO("  App WCET: %lu", g_app_WCET_us);
            }

            if (task_dta_list[index].WCET < cycle_counter_time_us)
            {
                task_dta_list[index].WCET = cycle_counter_time_us;
            }
        }

        /* Protect shared resource */
        __asm("CPSID i");	/* disable interrupts */
        if (G_APP_TICK_CNT_INI < g_app_tick_cnt)
        {
            /* Update Tick Counter */
            g_app_tick_cnt--;
            b_time_update_required = true;
        }
        else
        {
            b_time_update_required = false;
        }
        __asm("CPSIE i");	/* enable interrupts */
    }
}

void HAL_SYSTICK_Callback(void)
{
    /* Update Tick Counter */
    g_app_tick_cnt++;

    g_task_sensor_tick_cnt++;
    g_task_system_tick_cnt++;
    g_task_actuator_tick_cnt++;
}

/********************** end of file ******************************************/
