tdse-tp2_05-model_integration

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