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 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 "app.h" 48 #include "board.h" 49 #include "task_adc.h" 50 #include "task_pwm.h" 51 52 53 /********************** macros and definitions *******************************/ 54 55 56 #define G_APP_CNT_INI 0ul 57 #define G_APP_TICK_CNT_INI 0ul 58 59 #define TASK_X_WCET_INI 0ul 60 61 typedef struct { 62 void (*task_init)(void *); // Pointer to task (must be a 63 // 'void (void *)' function) 64 void (*task_update)(void *); // Pointer to task (must be a 65 // 'void (void *)' function) 66 void *parameters; // Pointer to parameters 67 } task_cfg_t; 68 69 typedef struct { 70 uint32_t WCET; // Worst-case execution time (microseconds) 71 } task_dta_t; 72 73 /********************** internal data declaration ****************************/ 74 75 76 shared_data_type shared_data; 77 78 const task_cfg_t task_cfg_list[] = { 79 {task_adc_init, task_adc_update, &shared_data}, 80 {task_pwm_init, task_pwm_update, &shared_data}, 81 }; 82 83 #define TASK_QTY (sizeof(task_cfg_list)/sizeof(task_cfg_t)) 84 85 /********************** internal functions declaration ***********************/ 86 87 /********************** internal data definition *****************************/ 88 89 90 const char *p_sys = " Bare Metal - Event-Triggered Systems (ETS)\n"; 91 const char *p_app = " ADC + PWM\n"; 92 93 94 /********************** external data declaration *****************************/ 95 96 97 uint32_t g_app_cnt; 98 uint32_t g_app_time_us; 99 100 volatile uint32_t g_app_tick_cnt; 101 task_dta_t task_dta_list[TASK_QTY]; 102 103 /********************** external functions definition ************************/ 104 105 void app_init(void) 106 { 107 uint32_t index; 108 109 /* Print out: Application Initialized */ 110 LOGGER_LOG("\n"); 111 LOGGER_LOG("%s is running - Tick [mS] = %lu\r\n", GET_NAME(app_init), HAL_GetTick()); 112 113 LOGGER_LOG(p_sys); 114 LOGGER_LOG(p_app); 115 116 g_app_cnt = G_APP_CNT_INI; 117 118 /* Print out: Application execution counter */ 119 LOGGER_LOG(" %s = %lu\n", GET_NAME(g_app_cnt), g_app_cnt); 120 121 /* Go through the task arrays */ 122 for (index = 0; TASK_QTY > index; index++) 123 { 124 125 /* Run task_x_init */ 126 (*task_cfg_list[index].task_init)(task_cfg_list[index].parameters); 127 128 /* Init variables */ 129 task_dta_list[index].WCET = TASK_X_WCET_INI; 130 } 131 132 cycle_counter_init(); 133 } 134 135 void app_update(void) 136 { 137 138 uint32_t index; 139 //uint32_t cycle_counter; 140 uint32_t cycle_counter_time_us; 141 142 /* Check if it's time to run tasks */ 143 if (G_APP_TICK_CNT_INI < g_app_tick_cnt) 144 { 145 146 g_app_tick_cnt--; 147 148 /* Update App Counter */ 149 g_app_cnt++; 150 g_app_time_us = 0; 151 152 /* Print out: Application execution counter */ 153 //LOGGER_LOG(" %s = %lu\r\n", GET_NAME(g_app_cnt), g_app_cnt); 154 155 /* Go through the task arrays */ 156 for (index = 0; TASK_QTY > index; index++) 157 { 158 159 cycle_counter_reset(); 160 161 /* Run task_x_update */ 162 (*task_cfg_list[index].task_update)(task_cfg_list[index].parameters); 163 164 165 //cycle_counter = cycle_counter_get(); 166 cycle_counter_time_us = cycle_counter_time_us(); 167 168 /* Update variables */ 169 g_app_time_us += cycle_counter_time_us; 170 171 if (task_dta_list[index].WCET < cycle_counter_time_us) 172 { 173 task_dta_list[index].WCET = cycle_counter_time_us; 174 } 175 176 177 /* Print out: Cycle Counter */ 178 //LOGGER_LOG(" %s: %lu - %s: %lu uS\r\n", GET_NAME(cycle_counter), cycle_counter, GET_NAME(cycle_counter_time_us), cycle_counter_time_us); 179 //LOGGER_LOG(" %s: %lu uS\r\n", GET_NAME(g_app_time_us), g_app_time_us); 180 } 181 182 LOGGER_LOG("%d,%d\n", shared_data.pwm_active, shared_data.adc_value); 183 } 184 } 185 186 /* Callbacks in C (https://www.geeksforgeeks.org/) */ 187 /* 188 * A callback is any executable code that is passed as an argument to another 189 * code, which is expected to call back (execute) the argument at a given time. 190 * In simple language, If a reference of a function is passed to another 191 * function as an argument to call it, then it will be called a Callback 192 * function. 193 */ 194 195 void HAL_SYSTICK_Callback(void) 196 { 197 g_app_tick_cnt++; 198 199 200 201 //HAL_GPIO_TogglePin(LED_A_PORT, LED_A_PIN); 202 } 203 204 /********************** end of file ******************************************/
