tdse-tp2_04-model_integration

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