tdse-tp2_05-model_integration

Index Commits Files Refs README
app/src/task_system.c (8785B)
   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_system.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_system_attribute.h"
  50 #include "task_system_interface.h"
  51 #include "task_actuator_attribute.h"
  52 #include "task_actuator_interface.h"
  53 
  54 /********************** macros and definitions *******************************/
  55 #define G_TASK_SYS_CNT_INI      0ul
  56 #define G_TASK_SYS_TICK_CNT_INI 0ul
  57 
  58 #define DEL_SYS_MIN             0ul
  59 #define DEL_SYS_MED             50ul
  60 #define DEL_SYS_MAX             500ul
  61 
  62 /********************** internal data declaration ****************************/
  63 task_system_dta_t task_system_dta = {
  64     DEL_SYS_MIN, ST_SYS_IDLE, EV_SYS_IDLE, false
  65 };
  66 
  67 #define SYSTEM_DTA_QTY (sizeof(task_system_dta)/sizeof(task_system_dta_t))
  68 
  69 /********************** internal functions declaration ***********************/
  70 void task_system_statechart(void);
  71 
  72 /********************** internal data definition *****************************/
  73 const char *p_task_system  = "Task System (System Statechart)";
  74 const char *p_task_system_ = "Non-Blocking & Update By Time Code";
  75 
  76 /********************** external data declaration ****************************/
  77 uint32_t g_task_system_cnt;
  78 volatile uint32_t g_task_system_tick_cnt;
  79 
  80 /********************** external functions definition ************************/
  81 void task_system_init(void *parameters)
  82 {
  83     task_system_dta_t *p_task_system_dta;
  84     task_system_st_t state;
  85     task_system_ev_t event;
  86     bool b_event;
  87 
  88     /* Print out: Task Initialized */
  89     LOGGER_INFO(" ");
  90     LOGGER_INFO("  %s is running - %s", GET_NAME(task_system_init), p_task_system);
  91     LOGGER_INFO("  %s is a %s", GET_NAME(task_system), p_task_system_);
  92 
  93     /* Init & Print out: Task execution counter */
  94     g_task_system_cnt = G_TASK_SYS_CNT_INI;
  95     LOGGER_INFO("   %s = %lu", GET_NAME(g_task_system_cnt), g_task_system_cnt);
  96 
  97     init_queue_event_task_system();
  98 
  99     /* Update Task Actuator Configuration & Data Pointer */
 100     p_task_system_dta = &task_system_dta;
 101 
 102     /* Init & Print out: Task execution FSM */
 103     state = ST_SYS_IDLE;
 104     p_task_system_dta->state = state;
 105 
 106     event = EV_SYS_IDLE;
 107     p_task_system_dta->event = event;
 108 
 109     b_event = false;
 110     p_task_system_dta->flag = b_event;
 111 
 112     LOGGER_INFO(" ");
 113     LOGGER_INFO("   %s = %lu   %s = %lu   %s = %s",
 114             GET_NAME(state), (uint32_t)state,
 115             GET_NAME(event), (uint32_t)event,
 116             GET_NAME(b_event), (b_event ? "true" : "false"));
 117 }
 118 
 119 void task_system_update(void *parameters)
 120 {
 121     bool b_time_update_required = false;
 122 
 123     /* Protect shared resource */
 124     __asm("CPSID i"); /* disable interrupts */
 125     if (G_TASK_SYS_TICK_CNT_INI < g_task_system_tick_cnt)
 126     {
 127         /* Update Tick Counter */
 128         g_task_system_tick_cnt--;
 129         b_time_update_required = true;
 130     }
 131     __asm("CPSIE i"); /* enable interrupts */
 132 
 133     while (b_time_update_required)
 134     {
 135         /* Update Task Counter */
 136         g_task_system_cnt++;
 137 
 138         /* Run Task Statechart */
 139         task_system_statechart();
 140 
 141         /* Protect shared resource */
 142         __asm("CPSID i"); /* disable interrupts */
 143         if (G_TASK_SYS_TICK_CNT_INI < g_task_system_tick_cnt)
 144         {
 145             /* Update Tick Counter */
 146             g_task_system_tick_cnt--;
 147             b_time_update_required = true;
 148         }
 149         else
 150         {
 151             b_time_update_required = false;
 152         }
 153         __asm("CPSIE i"); /* enable interrupts */
 154     }
 155 }
 156 
 157 void task_system_statechart(void)
 158 {
 159     task_system_dta_t *p_task_system_dta;
 160 
 161     /* Update Task System Data Pointer */
 162     p_task_system_dta = &task_system_dta;
 163 
 164     if (true == any_event_task_system())
 165     {
 166         p_task_system_dta->flag = true;
 167         p_task_system_dta->event = get_event_task_system();
 168     }
 169 
 170     switch (p_task_system_dta->state)
 171     {
 172         case ST_SYS_IDLE:
 173 
 174             if ((true == p_task_system_dta->flag)
 175                     && (EV_SYS_LOOP_DET == p_task_system_dta->event))
 176             {
 177                 p_task_system_dta->flag = false;
 178                 p_task_system_dta->state = ST_SYS_ACTIVE_01;
 179             }
 180 
 181             break;
 182 
 183         case ST_SYS_ACTIVE_01:
 184 
 185             if ((true == p_task_system_dta->flag)
 186                     && (EV_SYS_MANUAL_BTN == p_task_system_dta->event))
 187             {
 188                 p_task_system_dta->flag = false;
 189                 p_task_system_dta->state = ST_SYS_ACTIVE_02;
 190 
 191                 p_task_system_dta->tick = DEL_SYS_MAX;
 192                 put_event_task_actuator(EV_LED_XX_BLINK, ID_LED_A);
 193             }
 194 
 195             break;
 196 
 197         case ST_SYS_ACTIVE_02:
 198 
 199             if (DEL_SYS_MIN == p_task_system_dta->tick)
 200             {
 201                 p_task_system_dta->state = ST_SYS_ACTIVE_03;
 202                 put_event_task_actuator(EV_LED_XX_ON, ID_LED_A);
 203                 put_event_task_actuator(EV_LED_XX_ON, ID_LED_B);
 204             }
 205 
 206             if (DEL_SYS_MIN < p_task_system_dta->tick) {
 207                 p_task_system_dta->tick--;
 208             }
 209 
 210             break;
 211 
 212         case ST_SYS_ACTIVE_03:
 213 
 214             if ((true == p_task_system_dta->flag)
 215                     && (EV_SYS_NOT_LOOP_DET == p_task_system_dta->event))
 216             {
 217                 p_task_system_dta->flag = false;
 218                 p_task_system_dta->state = ST_SYS_ACTIVE_04;
 219             }
 220 
 221             break;
 222 
 223         case ST_SYS_ACTIVE_04:
 224 
 225             if ((true == p_task_system_dta->flag)
 226                     && (EV_SYS_IR_PHO_CELL == p_task_system_dta->event))
 227             {
 228                 p_task_system_dta->flag = false;
 229                 p_task_system_dta->state = ST_SYS_ACTIVE_05;
 230             }
 231 
 232             break;
 233 
 234         case ST_SYS_ACTIVE_05:
 235 
 236             if ((true == p_task_system_dta->flag)
 237                     && (EV_SYS_NOT_IR_PHO_CELL == p_task_system_dta->event))
 238             {
 239                 p_task_system_dta->flag = false;
 240                 p_task_system_dta->state = ST_SYS_ACTIVE_06;
 241 
 242                 p_task_system_dta->tick = DEL_SYS_MAX;
 243                 put_event_task_actuator(EV_LED_XX_BLINK, ID_LED_A);
 244             }
 245 
 246             break;
 247 
 248         case ST_SYS_ACTIVE_06:
 249 
 250             if (DEL_SYS_MIN == p_task_system_dta->tick)
 251             {
 252                 p_task_system_dta->state = ST_SYS_IDLE;
 253                 put_event_task_actuator(EV_LED_XX_OFF, ID_LED_A);
 254                 put_event_task_actuator(EV_LED_XX_OFF, ID_LED_B);
 255             }
 256 
 257             if (DEL_SYS_MIN < p_task_system_dta->tick) {
 258                 p_task_system_dta->tick--;
 259             }
 260 
 261             break;
 262 
 263         default:
 264 
 265             p_task_system_dta->tick  = DEL_SYS_MIN;
 266             p_task_system_dta->state = ST_SYS_IDLE;
 267             p_task_system_dta->event = EV_SYS_IDLE;
 268             p_task_system_dta->flag = false;
 269 
 270             break;
 271     }
 272 }
 273 
 274 /********************** end of file ******************************************/