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 : task_system.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 "app.h" 49 #include "task_system_attribute.h" 50 #include "task_system_interface.h" 51 #include "task_actuator_attribute.h" 52 #include "task_actuator_interface.h" 53 54 /********************** macros and definitions *******************************/ 55 #define G_TASK_SYS_CNT_INI 0ul 56 #define G_TASK_SYS_TICK_CNT_INI 0ul 57 58 #define DEL_SYS_MIN 0ul 59 #define DEL_SYS_MED 50ul 60 #define DEL_SYS_MAX 500ul 61 62 /********************** internal data declaration ****************************/ 63 task_system_dta_t task_system_dta = 64 {DEL_SYS_MIN, ST_SYS_IDLE, EV_SYS_IDLE, false}; 65 66 #define SYSTEM_DTA_QTY (sizeof(task_system_dta)/sizeof(task_system_dta_t)) 67 68 /********************** internal functions declaration ***********************/ 69 void task_system_statechart(void); 70 71 /********************** internal data definition *****************************/ 72 const char *p_task_system = "Task System (System Statechart)"; 73 const char *p_task_system_ = "Non-Blocking & Update By Time Code"; 74 75 /********************** external data declaration ****************************/ 76 uint32_t g_task_system_cnt; 77 volatile uint32_t g_task_system_tick_cnt; 78 79 /********************** external functions definition ************************/ 80 void task_system_init(void *parameters) 81 { 82 task_system_dta_t *p_task_system_dta; 83 task_system_st_t state; 84 task_system_ev_t event; 85 bool b_event; 86 87 /* Print out: Task Initialized */ 88 LOGGER_INFO(" "); 89 LOGGER_INFO(" %s is running - %s", GET_NAME(task_system_init), p_task_system); 90 LOGGER_INFO(" %s is a %s", GET_NAME(task_system), p_task_system_); 91 92 /* Init & Print out: Task execution counter */ 93 g_task_system_cnt = G_TASK_SYS_CNT_INI; 94 LOGGER_INFO(" %s = %lu", GET_NAME(g_task_system_cnt), g_task_system_cnt); 95 96 init_queue_event_task_system(); 97 98 /* Update Task Actuator Configuration & Data Pointer */ 99 p_task_system_dta = &task_system_dta; 100 101 /* Init & Print out: Task execution FSM */ 102 state = ST_SYS_IDLE; 103 p_task_system_dta->state = state; 104 105 event = EV_SYS_IDLE; 106 p_task_system_dta->event = event; 107 108 b_event = false; 109 p_task_system_dta->flag = b_event; 110 111 LOGGER_INFO(" "); 112 LOGGER_INFO(" %s = %lu %s = %lu %s = %s", 113 GET_NAME(state), (uint32_t)state, 114 GET_NAME(event), (uint32_t)event, 115 GET_NAME(b_event), (b_event ? "true" : "false")); 116 } 117 118 void task_system_update(void *parameters) 119 { 120 bool b_time_update_required = false; 121 122 /* Protect shared resource */ 123 __asm("CPSID i"); /* disable interrupts */ 124 if (G_TASK_SYS_TICK_CNT_INI < g_task_system_tick_cnt) 125 { 126 /* Update Tick Counter */ 127 g_task_system_tick_cnt--; 128 b_time_update_required = true; 129 } 130 __asm("CPSIE i"); /* enable interrupts */ 131 132 while (b_time_update_required) 133 { 134 /* Update Task Counter */ 135 g_task_system_cnt++; 136 137 /* Run Task Statechart */ 138 task_system_statechart(); 139 140 /* Protect shared resource */ 141 __asm("CPSID i"); /* disable interrupts */ 142 if (G_TASK_SYS_TICK_CNT_INI < g_task_system_tick_cnt) 143 { 144 /* Update Tick Counter */ 145 g_task_system_tick_cnt--; 146 b_time_update_required = true; 147 } 148 else 149 { 150 b_time_update_required = false; 151 } 152 __asm("CPSIE i"); /* enable interrupts */ 153 } 154 } 155 156 void task_system_statechart(void) 157 { 158 task_system_dta_t *p_task_system_dta; 159 160 /* Update Task System Data Pointer */ 161 p_task_system_dta = &task_system_dta; 162 163 if (true == any_event_task_system()) 164 { 165 p_task_system_dta->flag = true; 166 p_task_system_dta->event = get_event_task_system(); 167 } 168 169 switch (p_task_system_dta->state) 170 { 171 case ST_SYS_IDLE: 172 173 if ((true == p_task_system_dta->flag) && (EV_SYS_LOOP_DET == p_task_system_dta->event)) 174 { 175 p_task_system_dta->flag = false; 176 put_event_task_actuator(EV_LED_XX_ON, ID_LED_A); 177 p_task_system_dta->state = ST_SYS_ACTIVE_01; 178 } 179 180 break; 181 182 case ST_SYS_ACTIVE_01: 183 184 if ((true == p_task_system_dta->flag) && (EV_SYS_IDLE == p_task_system_dta->event)) 185 { 186 p_task_system_dta->flag = false; 187 put_event_task_actuator(EV_LED_XX_OFF, ID_LED_A); 188 p_task_system_dta->state = ST_SYS_IDLE; 189 } 190 191 break; 192 193 case ST_SYS_ACTIVE_02: 194 195 break; 196 197 case ST_SYS_ACTIVE_03: 198 199 break; 200 201 case ST_SYS_ACTIVE_04: 202 203 break; 204 205 case ST_SYS_ACTIVE_05: 206 207 break; 208 209 case ST_SYS_ACTIVE_06: 210 211 break; 212 213 default: 214 215 p_task_system_dta->tick = DEL_SYS_MIN; 216 p_task_system_dta->state = ST_SYS_IDLE; 217 p_task_system_dta->event = EV_SYS_IDLE; 218 p_task_system_dta->flag = false; 219 220 break; 221 } 222 } 223 224 /********************** end of file ******************************************/
