tdse-tp2_02-model_integration

Index Commits Files Refs README
app/src/task_actuator.c (7515B)
   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,
  63      DEL_LED_XX_BLI, DEL_LED_XX_PUL}
  64 };
  65 
  66 #define ACTUATOR_CFG_QTY    (sizeof(task_actuator_cfg_list)/sizeof(task_actuator_cfg_t))
  67 
  68 task_actuator_dta_t task_actuator_dta_list[] = {
  69     {DEL_LED_XX_MIN, ST_LED_XX_OFF, EV_LED_XX_NOT_BLINK, false}
  70 };
  71 
  72 #define ACTUATOR_DTA_QTY    (sizeof(task_actuator_dta_list)/sizeof(task_actuator_dta_t))
  73 
  74 /********************** internal functions declaration ***********************/
  75 void task_actuator_statechart(void);
  76 
  77 /********************** internal data definition *****************************/
  78 const char *p_task_actuator         = "Task Actuator (Actuator Statechart)";
  79 const char *p_task_actuator_         = "Non-Blocking & Update By Time Code";
  80 
  81 /********************** external data declaration ****************************/
  82 uint32_t g_task_actuator_cnt;
  83 volatile uint32_t g_task_actuator_tick_cnt;
  84 
  85 /********************** external functions definition ************************/
  86 void task_actuator_init(void *parameters)
  87 {
  88     uint32_t index;
  89     const task_actuator_cfg_t *p_task_actuator_cfg;
  90     task_actuator_dta_t *p_task_actuator_dta;
  91     task_actuator_st_t state;
  92     task_actuator_ev_t event;
  93     bool b_event;
  94 
  95     /* Print out: Task Initialized */
  96     LOGGER_INFO(" ");
  97     LOGGER_INFO("  %s is running - %s", GET_NAME(task_actuator_init), p_task_actuator);
  98     LOGGER_INFO("  %s is a %s", GET_NAME(task_actuator), p_task_actuator_);
  99 
 100     /* Init & Print out: Task execution counter */
 101     g_task_actuator_cnt = G_TASK_ACT_CNT_INIT;
 102     LOGGER_INFO("   %s = %lu", GET_NAME(g_task_actuator_cnt), g_task_actuator_cnt);
 103 
 104     for (index = 0; ACTUATOR_DTA_QTY > index; index++)
 105     {
 106         /* Update Task Actuator Configuration & Data Pointer */
 107         p_task_actuator_cfg = &task_actuator_cfg_list[index];
 108         p_task_actuator_dta = &task_actuator_dta_list[index];
 109 
 110         /* Init & Print out: Index & Task execution FSM */
 111         state = ST_LED_XX_OFF;
 112         p_task_actuator_dta->state = state;
 113 
 114         event = EV_LED_XX_OFF;
 115         p_task_actuator_dta->event = event;
 116 
 117         b_event = false;
 118         p_task_actuator_dta->flag = b_event;
 119 
 120         LOGGER_INFO(" ");
 121         LOGGER_INFO("   %s = %lu   %s = %lu   %s = %lu   %s = %s",
 122                      GET_NAME(index), index,
 123                      GET_NAME(state), (uint32_t)state,
 124                      GET_NAME(event), (uint32_t)event,
 125                      GET_NAME(b_event), (b_event ? "true" : "false"));
 126 
 127         HAL_GPIO_WritePin(p_task_actuator_cfg->gpio_port, p_task_actuator_cfg->pin, p_task_actuator_cfg->led_off);
 128     }
 129 }
 130 
 131 void task_actuator_update(void *parameters)
 132 {
 133     bool b_time_update_required = false;
 134 
 135     /* Protect shared resource */
 136     __asm("CPSID i");    /* disable interrupts*/
 137     if (G_TASK_ACT_TICK_CNT_INI < g_task_actuator_tick_cnt)
 138     {
 139         /* Update Tick Counter */
 140         g_task_actuator_tick_cnt--;
 141         b_time_update_required = true;
 142     }
 143     __asm("CPSIE i");    /* enable interrupts */
 144 
 145     while (b_time_update_required)
 146     {
 147         /* Update Task Counter */
 148         g_task_actuator_cnt++;
 149 
 150         /* Run Task Statechart */
 151         task_actuator_statechart();
 152 
 153         /* Protect shared resource */
 154         __asm("CPSID i");    /* disable interrupts */
 155         if (G_TASK_ACT_TICK_CNT_INI < g_task_actuator_tick_cnt)
 156         {
 157             /* Update Tick Counter */
 158             g_task_actuator_tick_cnt--;
 159             b_time_update_required = true;
 160         }
 161         else
 162         {
 163             b_time_update_required = false;
 164         }
 165         __asm("CPSIE i");    /* enable interrupts */
 166     }
 167 }
 168 
 169 void task_actuator_statechart(void)
 170 {
 171     uint32_t index;
 172     const task_actuator_cfg_t *p_task_actuator_cfg;
 173     task_actuator_dta_t *p_task_actuator_dta;
 174 
 175     for (index = 0; ACTUATOR_DTA_QTY > index; index++)
 176     {
 177         /* Update Task Actuator Configuration & Data Pointer */
 178         p_task_actuator_cfg = &task_actuator_cfg_list[index];
 179         p_task_actuator_dta = &task_actuator_dta_list[index];
 180 
 181         switch (p_task_actuator_dta->state)
 182         {
 183             case ST_LED_XX_OFF:
 184 
 185                 if ((true == p_task_actuator_dta->flag) && (EV_LED_XX_ON == p_task_actuator_dta->event))
 186                 {
 187                     p_task_actuator_dta->flag = false;
 188                     HAL_GPIO_WritePin(p_task_actuator_cfg->gpio_port, p_task_actuator_cfg->pin, p_task_actuator_cfg->led_on);
 189                     p_task_actuator_dta->state = ST_LED_XX_ON;
 190                 }
 191 
 192                 break;
 193 
 194             case ST_LED_XX_ON:
 195 
 196                 if ((true == p_task_actuator_dta->flag) && (EV_LED_XX_OFF == p_task_actuator_dta->event))
 197                 {
 198                     p_task_actuator_dta->flag = false;
 199                     HAL_GPIO_WritePin(p_task_actuator_cfg->gpio_port, p_task_actuator_cfg->pin, p_task_actuator_cfg->led_off);
 200                     p_task_actuator_dta->state = ST_LED_XX_OFF;
 201                 }
 202 
 203                 break;
 204 
 205             case ST_LED_XX_BLINK_ON:
 206 
 207                 break;
 208 
 209             case ST_LED_XX_BLINK_OFF:
 210 
 211                 break;
 212 
 213             case ST_LED_XX_PULSE:
 214 
 215                 break;
 216 
 217             default:
 218 
 219                 p_task_actuator_dta->tick  = DEL_LED_XX_MIN;
 220                 p_task_actuator_dta->state = ST_LED_XX_OFF;
 221                 p_task_actuator_dta->event = EV_LED_XX_OFF;
 222                 p_task_actuator_dta->flag = false;
 223 
 224                 break;
 225         }
 226     }
 227 }
 228 
 229 /********************** end of file ******************************************/