tdse-tp2_01-model_integration

Index Commits Files Refs README
app/src/app.c (6709B)
   1 /*
   2  * Copyright (c) 2023 Juan Manuel Cruz <jcruz@fi.uba.ar> <jcruz@frba.utn.edu.ar>.
   3  * All rights reserved.
   4  *
   5  * Redistribution and use in source and binary forms, with or without
   6  * modification, are permitted provided that the following conditions are met:
   7  *
   8  * 1. Redistributions of source code must retain the above copyright
   9  *    notice, this list of conditions and the following disclaimer.
  10  *
  11  * 2. Redistributions in binary form must reproduce the above copyright
  12  *    notice, this list of conditions and the following disclaimer in the
  13  *    documentation and/or other materials provided with the distribution.
  14  *
  15  * 3. Neither the name of the copyright holder nor the names of its
  16  *    contributors may be used to endorse or promote products derived from
  17  *    this software without specific prior written permission.
  18  *
  19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  25  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  28  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  29  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30  * POSSIBILITY OF SUCH DAMAGE.
  31  *
  32  * @file   : app.c
  33  * @date   : Set 26, 2023
  34  * @author : Juan Manuel Cruz <jcruz@fi.uba.ar> <jcruz@frba.utn.edu.ar>
  35  * @version    v1.0.0
  36  */
  37 
  38 /********************** inclusions *******************************************/
  39 /* Project includes */
  40 #include "main.h"
  41 
  42 /* Demo includes */
  43 #include "logger.h"
  44 #include "dwt.h"
  45 
  46 /* Application & Tasks includes */
  47 #include "board.h"
  48 #include "task_system.h"
  49 #include "task_actuator.h"
  50 #include "task_sensor.h"
  51 
  52 /********************** macros and definitions *******************************/
  53 #define G_APP_CNT_INI       0ul
  54 #define G_APP_TICK_CNT_INI  0ul
  55 #define G_APP_WCET_INI      0ul
  56 
  57 #define TASK_X_WCET_INI     0ul
  58 #define TASK_X_DELAY_MIN    0ul
  59 
  60 typedef struct {
  61     void (*task_init)(void *);
  62     void (*task_update)(void *);
  63     void *parameters;
  64 } task_cfg_t;
  65 
  66 typedef struct {
  67     uint32_t WCET; // Task worst-case execution time (microseconds)
  68 } task_dta_t;
  69 
  70 /********************** internal data declaration ****************************/
  71 const task_cfg_t task_cfg_list[] = {
  72     {task_sensor_init,   task_sensor_update,   NULL},
  73     {task_system_init,   task_system_update,   NULL},
  74     {task_actuator_init, task_actuator_update, NULL}
  75 };
  76 
  77 #define TASK_QTY (sizeof(task_cfg_list)/sizeof(task_cfg_t))
  78 
  79 /********************** internal functions declaration ***********************/
  80 
  81 /********************** internal data definition *****************************/
  82 const char *p_sys = " Bare Metal - Event-Triggered Systems (ETS)";
  83 const char *p_app = " App - Model Integration - C codig";
  84 
  85 /********************** external data declaration ****************************/
  86 uint32_t g_app_cnt;
  87 uint32_t g_app_runtime_us;
  88 uint32_t g_app_WCET_us; // Worst case execution time historically
  89 
  90 volatile uint32_t g_app_tick_cnt;
  91 
  92 task_dta_t task_dta_list[TASK_QTY];
  93 
  94 /********************** external functions definition ************************/
  95 void app_init(void)
  96 {
  97     uint32_t index;
  98 
  99     /* Print out: Application Initialized */
 100     LOGGER_INFO(" ");
 101     LOGGER_INFO("%s is running - Tick [mS] = %lu", GET_NAME(app_init), HAL_GetTick());
 102 
 103     LOGGER_INFO(p_sys);
 104     LOGGER_INFO(p_app);
 105 
 106     /* Init & Print out: Application execution counter */
 107     g_app_cnt = G_APP_CNT_INI;
 108     LOGGER_INFO(" %s = %lu", GET_NAME(g_app_cnt), g_app_cnt);
 109 
 110     /* Init application worst case execution time */
 111     g_app_WCET_us = G_APP_WCET_INI;
 112 
 113     /* Init Cycle Counter */
 114     cycle_counter_init();
 115 
 116     /* Go through the task arrays */
 117     for (index = 0; TASK_QTY > index; index++)
 118     {
 119         /* Run task_x_init */
 120         (*task_cfg_list[index].task_init)(task_cfg_list[index].parameters);
 121 
 122         /* Init variables */
 123         task_dta_list[index].WCET = TASK_X_WCET_INI;
 124     }
 125 
 126     /* Protect shared resource */
 127     __asm("CPSID i");    /* disable interrupts */
 128     /* Init Tick Counter */
 129     g_app_tick_cnt = G_APP_TICK_CNT_INI;
 130 
 131     g_task_sensor_tick_cnt = G_APP_TICK_CNT_INI;
 132     g_task_system_tick_cnt = G_APP_TICK_CNT_INI;
 133     g_task_actuator_tick_cnt = G_APP_TICK_CNT_INI;
 134     __asm("CPSIE i");    /* enable interrupts */
 135 }
 136 
 137 void app_update(void)
 138 {
 139     uint32_t index;
 140     bool b_time_update_required = false;
 141     uint32_t cycle_counter_time_us;
 142 
 143     /* Protect shared resource */
 144     __asm("CPSID i");    /* disable interrupts */
 145     if (G_APP_TICK_CNT_INI < g_app_tick_cnt)
 146     {
 147         /* Update Tick Counter */
 148         g_app_tick_cnt--;
 149         b_time_update_required = true;
 150     }
 151     __asm("CPSIE i");    /* enable interrupts */
 152 
 153     /* Check if it's time to run tasks */
 154     while (b_time_update_required)
 155     {
 156         /* Update App Counter */
 157         g_app_cnt++;
 158         g_app_runtime_us = 0;
 159 
 160         /* Go through the task arrays */
 161         for (index = 0; TASK_QTY > index; index++)
 162         {
 163             cycle_counter_reset();
 164 
 165             /* Run task_x_update */
 166             (*task_cfg_list[index].task_update)(task_cfg_list[index].parameters);
 167 
 168             cycle_counter_time_us = cycle_counter_get_time_us();
 169 
 170             /* Update variables */
 171             g_app_runtime_us += cycle_counter_time_us;
 172 
 173             if (g_app_WCET_us < g_app_runtime_us) {
 174                 g_app_WCET_us = g_app_runtime_us;
 175                 LOGGER_INFO("  App WCET: %lu", g_app_WCET_us);
 176             }
 177 
 178             if (task_dta_list[index].WCET < cycle_counter_time_us)
 179             {
 180                 task_dta_list[index].WCET = cycle_counter_time_us;
 181             }
 182         }
 183 
 184         /* Protect shared resource */
 185         __asm("CPSID i");    /* disable interrupts */
 186         if (G_APP_TICK_CNT_INI < g_app_tick_cnt)
 187         {
 188             /* Update Tick Counter */
 189             g_app_tick_cnt--;
 190             b_time_update_required = true;
 191         }
 192         else
 193         {
 194             b_time_update_required = false;
 195         }
 196         __asm("CPSIE i");    /* enable interrupts */
 197     }
 198 }
 199 
 200 void HAL_SYSTICK_Callback(void)
 201 {
 202     /* Update Tick Counter */
 203     g_app_tick_cnt++;
 204 
 205     g_task_sensor_tick_cnt++;
 206     g_task_system_tick_cnt++;
 207     g_task_actuator_tick_cnt++;
 208 }
 209 
 210 /********************** end of file ******************************************/