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_a.h" 49 #include "task_b.h" 50 #include "task_c.h" 51 52 /********************** macros and definitions *******************************/ 53 #define G_APP_CNT_INI 0ul 54 #define G_APP_TICK_CNT_INI 0ul 55 56 #define TASK_X_WCET_INI 0ul 57 #define TASK_X_DELAY_MIN 0ul 58 59 typedef struct { 60 void (*task_init)(void*); // Pointer to task (must be a 61 // 'void (void *)' function) 62 void (*task_update)(void*); // Pointer to task (must be a 63 // 'void (void *)' function) 64 void *parameters; // Pointer to parameters 65 } task_cfg_t; 66 67 typedef struct { 68 uint32_t WCET; // Worst-case execution time (microseconds) 69 } task_dta_t; 70 71 /********************** internal data declaration ****************************/ 72 const task_cfg_t task_cfg_list[] = 73 { { task_a_init, task_a_update, NULL }, { task_b_init, task_b_update, 74 NULL }, { task_c_init, task_c_update, NULL } }; 75 76 #define TASK_QTY (sizeof(task_cfg_list)/sizeof(task_cfg_t)) 77 78 /********************** internal functions declaration ***********************/ 79 80 /********************** internal data definition *****************************/ 81 const char *p_sys = " Bare Metal - Event-Triggered Systems (ETS)"; 82 const char *p_app = " App - retarget_printf_to_Console"; 83 84 /********************** external data declaration ****************************/ 85 uint32_t g_app_cnt; 86 uint32_t g_app_runtime_us; 87 88 volatile uint32_t g_app_tick_cnt; 89 90 task_dta_t task_dta_list[TASK_QTY]; 91 92 /********************** external functions definition ************************/ 93 void app_init(void) 94 { 95 uint32_t index; 96 97 /* Print out: Application Initialized */ 98 LOGGER_INFO(" "); 99 LOGGER_INFO("%s is running - Tick [mS] = %lu", GET_NAME(app_init), 100 HAL_GetTick()); 101 102 LOGGER_INFO(p_sys); 103 LOGGER_INFO(p_app); 104 105 /* Init & Print out: Application execution counter */ 106 g_app_cnt = G_APP_CNT_INI; 107 LOGGER_INFO(" %s = %lu", GET_NAME(g_app_cnt), g_app_cnt); 108 109 /* Init Cycle Counter */ 110 cycle_counter_init(); 111 112 /* Go through the task arrays */ 113 for (index = 0; TASK_QTY > index; index++) { 114 /* Run task_x_init */ 115 (*task_cfg_list[index].task_init)(task_cfg_list[index].parameters); 116 117 /* Init variables */ 118 task_dta_list[index].WCET = TASK_X_WCET_INI; 119 } 120 121 /* Protect shared resource */ 122 __asm("CPSID i"); 123 /* disable interrupts */ 124 /* Init Tick Counter */ 125 g_app_tick_cnt = G_APP_TICK_CNT_INI; 126 g_task_c_tick_cnt = G_APP_TICK_CNT_INI; 127 __asm("CPSIE i"); 128 /* enable interrupts */ 129 } 130 131 void app_update(void) 132 { 133 uint32_t index; 134 bool b_time_update_required = false; 135 uint32_t cycle_counter_time_us; 136 137 /* Protect shared resource */ 138 __asm("CPSID i"); 139 /* disable interrupts */ 140 if (G_APP_TICK_CNT_INI < g_app_tick_cnt) { 141 /* Update Tick Counter */ 142 g_app_tick_cnt--; 143 b_time_update_required = true; 144 } 145 __asm("CPSIE i"); 146 /* enable interrupts */ 147 148 /* Check if it's time to run tasks */ 149 while (b_time_update_required) { 150 /* Update App Counter */ 151 g_app_cnt++; 152 g_app_runtime_us = 0; 153 154 /* Go through the task arrays */ 155 for (index = 0; TASK_QTY > index; index++) { 156 cycle_counter_reset(); 157 158 /* Run task_x_update */ 159 (*task_cfg_list[index].task_update)( 160 task_cfg_list[index].parameters); 161 162 cycle_counter_time_us = cycle_counter_get_time_us(); 163 164 /* Update variables */ 165 g_app_runtime_us += cycle_counter_time_us; 166 167 if (task_dta_list[index].WCET < cycle_counter_time_us) { 168 task_dta_list[index].WCET = cycle_counter_time_us; 169 } 170 } 171 172 /* Protect shared resource */ 173 __asm("CPSID i"); 174 /* disable interrupts */ 175 if (G_APP_TICK_CNT_INI < g_app_tick_cnt) { 176 /* Update Tick Counter */ 177 g_app_tick_cnt--; 178 b_time_update_required = true; 179 } else { 180 b_time_update_required = false; 181 } 182 __asm("CPSIE i"); 183 /* enable interrupts */ 184 } 185 } 186 187 void HAL_SYSTICK_Callback(void) 188 { 189 /* Update Tick Counter */ 190 g_app_tick_cnt++; 191 192 g_task_c_tick_cnt++; 193 } 194 195 /********************** end of file ******************************************/
