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_actuator.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_actuator_attribute.h" 50 #include "task_actuator_interface.h" 51 52 /********************** macros and definitions *******************************/ 53 #define G_TASK_ACT_CNT_INIT 0ul 54 #define G_TASK_ACT_TICK_CNT_INI 0ul 55 56 #define DEL_LED_XX_PUL 250ul 57 #define DEL_LED_XX_BLI 500ul 58 #define DEL_LED_XX_MIN 0ul 59 60 #define LED_ON GPIO_PIN_SET 61 #define LED_OFF GPIO_PIN_RESET 62 63 /********************** internal data declaration ****************************/ 64 const task_actuator_cfg_t task_actuator_cfg_list[] = { 65 {ID_LED_A, LED_A_GPIO_Port, LED_A_Pin, LED_ON, LED_OFF, DEL_LED_XX_BLI, DEL_LED_XX_PUL}, 66 {ID_LED_B, LED_B_GPIO_Port, LED_B_Pin, LED_ON, LED_OFF, DEL_LED_XX_BLI, DEL_LED_XX_PUL} 67 }; 68 69 #define ACTUATOR_CFG_QTY (sizeof(task_actuator_cfg_list)/sizeof(task_actuator_cfg_t)) 70 71 task_actuator_dta_t task_actuator_dta_list[] = { 72 {DEL_LED_XX_MIN, ST_LED_XX_OFF, EV_LED_XX_NOT_BLINK, false}, 73 {DEL_LED_XX_MIN, ST_LED_XX_OFF, EV_LED_XX_NOT_BLINK, false} 74 }; 75 76 #define ACTUATOR_DTA_QTY (sizeof(task_actuator_dta_list)/sizeof(task_actuator_dta_t)) 77 78 /********************** internal functions declaration ***********************/ 79 void task_actuator_statechart(void); 80 81 /********************** internal data definition *****************************/ 82 const char *p_task_actuator = "Task Actuator (Actuator Statechart)"; 83 const char *p_task_actuator_ = "Non-Blocking & Update By Time Code"; 84 85 /********************** external data declaration ****************************/ 86 uint32_t g_task_actuator_cnt; 87 volatile uint32_t g_task_actuator_tick_cnt; 88 89 /********************** external functions definition ************************/ 90 void task_actuator_init(void *parameters) 91 { 92 uint32_t index; 93 const task_actuator_cfg_t *p_task_actuator_cfg; 94 task_actuator_dta_t *p_task_actuator_dta; 95 task_actuator_st_t state; 96 task_actuator_ev_t event; 97 bool b_event; 98 99 /* Print out: Task Initialized */ 100 LOGGER_INFO(" "); 101 LOGGER_INFO(" %s is running - %s", GET_NAME(task_actuator_init), p_task_actuator); 102 LOGGER_INFO(" %s is a %s", GET_NAME(task_actuator), p_task_actuator_); 103 104 /* Init & Print out: Task execution counter */ 105 g_task_actuator_cnt = G_TASK_ACT_CNT_INIT; 106 LOGGER_INFO(" %s = %lu", GET_NAME(g_task_actuator_cnt), g_task_actuator_cnt); 107 108 for (index = 0; ACTUATOR_DTA_QTY > index; index++) 109 { 110 /* Update Task Actuator Configuration & Data Pointer */ 111 p_task_actuator_cfg = &task_actuator_cfg_list[index]; 112 p_task_actuator_dta = &task_actuator_dta_list[index]; 113 114 /* Init & Print out: Index & Task execution FSM */ 115 state = ST_LED_XX_OFF; 116 p_task_actuator_dta->state = state; 117 118 event = EV_LED_XX_OFF; 119 p_task_actuator_dta->event = event; 120 121 b_event = false; 122 p_task_actuator_dta->flag = b_event; 123 124 LOGGER_INFO(" "); 125 LOGGER_INFO(" %s = %lu %s = %lu %s = %lu %s = %s", 126 GET_NAME(index), index, 127 GET_NAME(state), (uint32_t)state, 128 GET_NAME(event), (uint32_t)event, 129 GET_NAME(b_event), (b_event ? "true" : "false")); 130 131 HAL_GPIO_WritePin(p_task_actuator_cfg->gpio_port, 132 p_task_actuator_cfg->pin, 133 p_task_actuator_cfg->led_off); 134 } 135 } 136 137 void task_actuator_update(void *parameters) 138 { 139 bool b_time_update_required = false; 140 141 /* Protect shared resource */ 142 __asm("CPSID i"); /* disable interrupts*/ 143 if (G_TASK_ACT_TICK_CNT_INI < g_task_actuator_tick_cnt) 144 { 145 /* Update Tick Counter */ 146 g_task_actuator_tick_cnt--; 147 b_time_update_required = true; 148 } 149 __asm("CPSIE i"); /* enable interrupts */ 150 151 while (b_time_update_required) 152 { 153 /* Update Task Counter */ 154 g_task_actuator_cnt++; 155 156 /* Run Task Statechart */ 157 task_actuator_statechart(); 158 159 /* Protect shared resource */ 160 __asm("CPSID i"); /* disable interrupts */ 161 if (G_TASK_ACT_TICK_CNT_INI < g_task_actuator_tick_cnt) 162 { 163 /* Update Tick Counter */ 164 g_task_actuator_tick_cnt--; 165 b_time_update_required = true; 166 } 167 else 168 { 169 b_time_update_required = false; 170 } 171 __asm("CPSIE i"); /* enable interrupts */ 172 } 173 } 174 175 void task_actuator_statechart(void) 176 { 177 uint32_t index; 178 const task_actuator_cfg_t *p_task_actuator_cfg; 179 task_actuator_dta_t *p_task_actuator_dta; 180 181 for (index = 0; ACTUATOR_DTA_QTY > index; index++) 182 { 183 /* Update Task Actuator Configuration & Data Pointer */ 184 p_task_actuator_cfg = &task_actuator_cfg_list[index]; 185 p_task_actuator_dta = &task_actuator_dta_list[index]; 186 187 switch (p_task_actuator_dta->state) 188 { 189 case ST_LED_XX_OFF: 190 if (true == p_task_actuator_dta->flag) 191 { 192 p_task_actuator_dta->flag = false; 193 switch(p_task_actuator_dta->event) 194 { 195 case EV_LED_XX_ON: 196 p_task_actuator_dta->state = ST_LED_XX_ON; 197 HAL_GPIO_WritePin( 198 p_task_actuator_cfg->gpio_port, 199 p_task_actuator_cfg->pin, 200 p_task_actuator_cfg->led_on); 201 break; 202 203 case EV_LED_XX_BLINK: 204 p_task_actuator_dta->tick = DEL_LED_XX_BLI; 205 p_task_actuator_dta->state = ST_LED_XX_BLINK_ON; 206 HAL_GPIO_WritePin( 207 p_task_actuator_cfg->gpio_port, 208 p_task_actuator_cfg->pin, 209 p_task_actuator_cfg->led_on); 210 break; 211 212 case EV_LED_XX_PULSE: 213 p_task_actuator_dta->tick = DEL_LED_XX_PUL; 214 p_task_actuator_dta->state = ST_LED_XX_PULSE; 215 HAL_GPIO_WritePin( 216 p_task_actuator_cfg->gpio_port, 217 p_task_actuator_cfg->pin, 218 p_task_actuator_cfg->led_on); 219 default: 220 break; 221 } 222 } 223 break; 224 225 case ST_LED_XX_ON: 226 if ((true == p_task_actuator_dta->flag) 227 && (EV_LED_XX_OFF == p_task_actuator_dta->event)) 228 { 229 p_task_actuator_dta->flag = false; 230 p_task_actuator_dta->state = ST_LED_XX_OFF; 231 HAL_GPIO_WritePin( 232 p_task_actuator_cfg->gpio_port, 233 p_task_actuator_cfg->pin, 234 p_task_actuator_cfg->led_off); 235 } 236 237 break; 238 239 case ST_LED_XX_BLINK_ON: 240 if (true == p_task_actuator_dta->flag) 241 { 242 p_task_actuator_dta->flag = false; 243 switch(p_task_actuator_dta->event) 244 { 245 case EV_LED_XX_OFF: 246 p_task_actuator_dta->state = ST_LED_XX_OFF; 247 HAL_GPIO_WritePin( 248 p_task_actuator_cfg->gpio_port, 249 p_task_actuator_cfg->pin, 250 p_task_actuator_cfg->led_off); 251 break; 252 case EV_LED_XX_ON: 253 p_task_actuator_dta->state = ST_LED_XX_ON; 254 HAL_GPIO_WritePin( 255 p_task_actuator_cfg->gpio_port, 256 p_task_actuator_cfg->pin, 257 p_task_actuator_cfg->led_on); 258 default: 259 break; 260 } 261 } 262 263 if (DEL_LED_XX_MIN == p_task_actuator_dta->tick) 264 { 265 p_task_actuator_dta->state = ST_LED_XX_BLINK_OFF; 266 p_task_actuator_dta->tick = DEL_LED_XX_BLI; 267 HAL_GPIO_WritePin( 268 p_task_actuator_cfg->gpio_port, 269 p_task_actuator_cfg->pin, 270 p_task_actuator_cfg->led_off); 271 } 272 273 if (DEL_LED_XX_MIN < p_task_actuator_dta->tick) 274 { 275 p_task_actuator_dta->tick--; 276 } 277 break; 278 279 case ST_LED_XX_BLINK_OFF: 280 if (true == p_task_actuator_dta->flag) 281 { 282 p_task_actuator_dta->flag = false; 283 switch(p_task_actuator_dta->event) 284 { 285 case EV_LED_XX_OFF: 286 p_task_actuator_dta->state = ST_LED_XX_OFF; 287 HAL_GPIO_WritePin( 288 p_task_actuator_cfg->gpio_port, 289 p_task_actuator_cfg->pin, 290 p_task_actuator_cfg->led_off); 291 break; 292 case EV_LED_XX_ON: 293 p_task_actuator_dta->state = ST_LED_XX_ON; 294 HAL_GPIO_WritePin( 295 p_task_actuator_cfg->gpio_port, 296 p_task_actuator_cfg->pin, 297 p_task_actuator_cfg->led_on); 298 default: 299 break; 300 } 301 } 302 303 if (DEL_LED_XX_MIN == p_task_actuator_dta->tick) 304 { 305 p_task_actuator_dta->state = ST_LED_XX_BLINK_ON; 306 p_task_actuator_dta->tick = DEL_LED_XX_BLI; 307 HAL_GPIO_WritePin( 308 p_task_actuator_cfg->gpio_port, 309 p_task_actuator_cfg->pin, 310 p_task_actuator_cfg->led_on); 311 } 312 313 if (DEL_LED_XX_MIN < p_task_actuator_dta->tick) 314 { 315 p_task_actuator_dta->tick--; 316 } 317 break; 318 319 case ST_LED_XX_PULSE: 320 if (DEL_LED_XX_MIN == p_task_actuator_dta->tick) 321 { 322 p_task_actuator_dta->state = ST_LED_XX_OFF; 323 HAL_GPIO_WritePin( 324 p_task_actuator_cfg->gpio_port, 325 p_task_actuator_cfg->pin, 326 p_task_actuator_cfg->led_off); 327 } 328 329 if (DEL_LED_XX_MIN < p_task_actuator_dta->tick) 330 { 331 p_task_actuator_dta->tick--; 332 } 333 break; 334 335 default: 336 p_task_actuator_dta->tick = DEL_LED_XX_MIN; 337 p_task_actuator_dta->state = ST_LED_XX_OFF; 338 p_task_actuator_dta->event = EV_LED_XX_OFF; 339 p_task_actuator_dta->flag = false; 340 break; 341 } 342 } 343 } 344 345 /********************** end of file ******************************************/
