tdse-tp2_01-model_integration

Index Commits Files Refs README
app/src/task_sensor.c (9103B)
   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_sensor.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_sensor_attribute.h"
  50 #include "task_system_attribute.h"
  51 #include "task_system_interface.h"
  52 
  53 /********************** macros and definitions *******************************/
  54 #define G_TASK_SEN_CNT_INIT     0ul
  55 #define G_TASK_SEN_TICK_CNT_INI 0ul
  56 
  57 #define DEL_BTN_XX_MIN          0ul
  58 #define DEL_BTN_XX_MED          25ul
  59 #define DEL_BTN_XX_MAX          50ul
  60 
  61 /********************** internal data declaration ****************************/
  62 const task_sensor_cfg_t task_sensor_cfg_list[] = {
  63     {ID_BTN_A,  BTN_A_PORT,  BTN_A_PIN,  BTN_A_PRESSED, DEL_BTN_XX_MAX,
  64         EV_SYS_IDLE,  EV_SYS_LOOP_DET}
  65 };
  66 
  67 #define SENSOR_CFG_QTY (sizeof(task_sensor_cfg_list)/sizeof(task_sensor_cfg_t))
  68 
  69 task_sensor_dta_t task_sensor_dta_list[] = {
  70     {DEL_BTN_XX_MIN, ST_BTN_XX_UP, EV_BTN_XX_UP}
  71 };
  72 
  73 #define SENSOR_DTA_QTY (sizeof(task_sensor_dta_list)/sizeof(task_sensor_dta_t))
  74 
  75 /********************** internal functions declaration ***********************/
  76 void task_sensor_statechart(void);
  77 
  78 /********************** internal data definition *****************************/
  79 const char *p_task_sensor = "Task Sensor (Sensor Statechart)";
  80 const char *p_task_sensor_ = "Non-Blocking & Update By Time Code";
  81 
  82 /********************** external data declaration ****************************/
  83 uint32_t g_task_sensor_cnt;
  84 volatile uint32_t g_task_sensor_tick_cnt;
  85 
  86 /********************** external functions definition ************************/
  87 void task_sensor_init(void *parameters)
  88 {
  89     uint32_t index;
  90     task_sensor_dta_t *p_task_sensor_dta;
  91     task_sensor_st_t state;
  92     task_sensor_ev_t event;
  93 
  94     /* Print out: Task Initialized */
  95     LOGGER_INFO(" ");
  96     LOGGER_INFO("  %s is running - %s", GET_NAME(task_sensor_init), p_task_sensor);
  97     LOGGER_INFO("  %s is a %s", GET_NAME(task_sensor), p_task_sensor_);
  98 
  99     /* Init & Print out: Task execution counter */
 100     g_task_sensor_cnt = G_TASK_SEN_CNT_INIT;
 101     LOGGER_INFO("   %s = %lu", GET_NAME(g_task_sensor_cnt), g_task_sensor_cnt);
 102 
 103     for (index = 0; SENSOR_DTA_QTY > index; index++)
 104     {
 105         /* Update Task Sensor Data Pointer */
 106         p_task_sensor_dta = &task_sensor_dta_list[index];
 107 
 108         /* Init & Print out: Index & Task execution FSM */
 109         state = ST_BTN_XX_UP;
 110         p_task_sensor_dta->state = state;
 111 
 112         event = EV_BTN_XX_UP;
 113         p_task_sensor_dta->event = event;
 114 
 115         LOGGER_INFO(" ");
 116         LOGGER_INFO("   %s = %lu   %s = %lu   %s = %lu",
 117                 GET_NAME(index), index,
 118                 GET_NAME(state), (uint32_t)state,
 119                 GET_NAME(event), (uint32_t)event);
 120     }
 121 }
 122 
 123 void task_sensor_update(void *parameters)
 124 {
 125     bool b_time_update_required = false;
 126 
 127     /* Protect shared resource */
 128     __asm("CPSID i");    /* disable interrupts */
 129     if (G_TASK_SEN_TICK_CNT_INI < g_task_sensor_tick_cnt)
 130     {
 131         /* Update Tick Counter */
 132         g_task_sensor_tick_cnt--;
 133         b_time_update_required = true;
 134     }
 135     __asm("CPSIE i");    /* enable interrupts */
 136 
 137     while (b_time_update_required)
 138     {
 139         /* Update Task Counter */
 140         g_task_sensor_cnt++;
 141 
 142         /* Run Task Statechart */
 143         task_sensor_statechart();
 144 
 145         /* Protect shared resource */
 146         __asm("CPSID i");    /* disable interrupts */
 147         if (G_TASK_SEN_TICK_CNT_INI < g_task_sensor_tick_cnt)
 148         {
 149             /* Update Tick Counter */
 150             g_task_sensor_tick_cnt--;
 151             b_time_update_required = true;
 152         }
 153         else
 154         {
 155             b_time_update_required = false;
 156         }
 157         __asm("CPSIE i");    /* enable interrupts */
 158     }
 159 }
 160 
 161 void task_sensor_statechart(void)
 162 {
 163     uint32_t index;
 164     uint8_t sensor_pin_status;
 165     const task_sensor_cfg_t *p_task_sensor_cfg;
 166     task_sensor_dta_t *p_task_sensor_dta;
 167 
 168     for (index = 0; SENSOR_DTA_QTY > index; index++)
 169     {
 170         /* Update Task Sensor Configuration & Data Pointer */
 171         p_task_sensor_cfg = &task_sensor_cfg_list[index];
 172         p_task_sensor_dta = &task_sensor_dta_list[index];
 173 
 174         sensor_pin_status = HAL_GPIO_ReadPin(p_task_sensor_cfg->gpio_port, p_task_sensor_cfg->pin);
 175         if (sensor_pin_status == p_task_sensor_cfg->pressed)
 176         {
 177             p_task_sensor_dta->event = EV_BTN_XX_DOWN;
 178         }
 179         else
 180         {
 181             p_task_sensor_dta->event = EV_BTN_XX_UP;
 182         }
 183 
 184         switch (p_task_sensor_dta->state)
 185         {
 186             case ST_BTN_XX_UP:
 187                 if (EV_BTN_XX_DOWN == p_task_sensor_dta->event)
 188                 {
 189                     p_task_sensor_dta->state = ST_BTN_XX_FALLING;
 190                     p_task_sensor_dta->tick  = DEL_BTN_XX_MAX;
 191                 }
 192             break;
 193 
 194             case ST_BTN_XX_FALLING:
 195                 if (EV_BTN_XX_UP == p_task_sensor_dta->event)
 196                 {
 197                     if (0 == p_task_sensor_dta->tick) {
 198                         p_task_sensor_dta->state = ST_BTN_XX_UP;
 199                     }
 200 
 201                     if (p_task_sensor_dta->tick > 0) {
 202                         p_task_sensor_dta->state = ST_BTN_XX_FALLING;
 203                         p_task_sensor_dta->tick--;
 204                     }
 205                 }
 206 
 207                 if (EV_BTN_XX_DOWN == p_task_sensor_dta->event)
 208                 {
 209                     if (0 == p_task_sensor_dta->tick) {
 210                         p_task_sensor_dta->state = ST_BTN_XX_DOWN;
 211                         put_event_task_system(p_task_sensor_cfg->signal_down);
 212                     }
 213 
 214                     if (p_task_sensor_dta->tick > 0) {
 215                         p_task_sensor_dta->tick--;
 216                     }
 217                 }
 218             break;
 219 
 220             case ST_BTN_XX_DOWN:
 221                 if (EV_BTN_XX_UP == p_task_sensor_dta->event)
 222                 {
 223                     p_task_sensor_dta->state = ST_BTN_XX_RISING;
 224                     p_task_sensor_dta->tick  = DEL_BTN_XX_MAX;
 225                 }
 226             break;
 227 
 228             case ST_BTN_XX_RISING:
 229                 if (EV_BTN_XX_UP == p_task_sensor_dta->event)
 230                 {
 231                     if (0 == p_task_sensor_dta->tick) {
 232                         put_event_task_system(p_task_sensor_cfg->signal_up);
 233                         p_task_sensor_dta->state = ST_BTN_XX_UP;
 234                     }
 235 
 236                     if (p_task_sensor_dta->tick > 0) {
 237                         p_task_sensor_dta->tick--;
 238                     }
 239                 }
 240 
 241                 if (EV_BTN_XX_DOWN == p_task_sensor_dta->event)
 242                 {
 243                     if (0 == p_task_sensor_dta->tick) {
 244                         p_task_sensor_dta->state = ST_BTN_XX_DOWN;
 245                     }
 246 
 247                     if (p_task_sensor_dta->tick > 0) {
 248                         p_task_sensor_dta->tick--;
 249                     }
 250                 }
 251             break;
 252 
 253             default:
 254                 p_task_sensor_dta->tick  = DEL_BTN_XX_MIN;
 255                 p_task_sensor_dta->state = ST_BTN_XX_UP;
 256                 p_task_sensor_dta->event = EV_BTN_XX_UP;
 257             break;
 258         }
 259     }
 260 }
 261 
 262 /********************** end of file ******************************************/