/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

#include <stdio.h>

#include "queue.h"

#include "keys_task.h"
#include "eth_task.h"
#include "mb_slave_task.h"
#include "beeper_task.h"
#include "onewire_task.h"
#include "bme_task.h"

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
typedef StaticTask_t osStaticThreadDef_t;
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

#define KEYS_TASK_STACK_DEPTH_WORDS					(128U)
#define ETH_TASK_STACK_DEPTH_WORDS					(2048U)
#define MB_SLAVE_TASK_STACK_DEPTH_WORDS				(1024U)
#define BEEPER_TASK_STACK_DEPTH_WORDS				(128U)
#define ONEWIRE_TASK_STACK_DEPTH_WORDS				(128U)
#define BME_TASK_STACK_DEPTH_WORDS					(256U)

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */

static StackType_t keysTaskStackBuffer[KEYS_TASK_STACK_DEPTH_WORDS]						__attribute__((section(".DTCM_RAM")));
static StackType_t ethTaskStackBuffer[ETH_TASK_STACK_DEPTH_WORDS]						__attribute__((section(".DTCM_RAM")));
static StackType_t mbSlaveTaskStackBuffer[MB_SLAVE_TASK_STACK_DEPTH_WORDS]				__attribute__((section(".DTCM_RAM")));
static StackType_t beeperTaskStackBuffer[BEEPER_TASK_STACK_DEPTH_WORDS]					__attribute__((section(".DTCM_RAM")));
static StackType_t onewireTaskStackBuffer[ONEWIRE_TASK_STACK_DEPTH_WORDS]				__attribute__((section(".DTCM_RAM")));
static StackType_t bmeTaskStackBuffer[BME_TASK_STACK_DEPTH_WORDS]						__attribute__((section(".DTCM_RAM")));

static uint8_t beeperQueueStorageArea[BEEPER_QUEUE_SIZE * BEEPER_QUEUE_ITEM_SIZE]		__attribute__((section(".DTCM_RAM")));
static uint8_t onewireQueueStorageArea[ONEWIRE_QUEUE_SIZE * ONEWIRE_QUEUE_ITEM_SIZE]	__attribute__((section(".DTCM_RAM")));

static StaticTask_t keysTaskBuffer														__attribute__((section(".DTCM_RAM")));
static StaticTask_t ethTaskBuffer														__attribute__((section(".DTCM_RAM")));
static StaticTask_t mbSlaveTaskBuffer													__attribute__((section(".DTCM_RAM")));
static StaticTask_t beeperTaskBuffer													__attribute__((section(".DTCM_RAM")));
static StaticTask_t onewireTaskBuffer													__attribute__((section(".DTCM_RAM")));
static StaticTask_t bmeTaskBuffer														__attribute__((section(".DTCM_RAM")));

static StaticQueue_t beeperQueueBuffer													__attribute__((section(".DTCM_RAM")));
static StaticQueue_t onewireQueueBuffer													__attribute__((section(".DTCM_RAM")));

static const char* const keysTaskName = "ScanKeysTask";
static const char* const ethTaskName = "EthTask";
static const char* const mbSlaveTaskName = "MBSlaveTask";
static const char* const beeperTaskName = "BeeperTask";
static const char* const onewireTaskName = "1WireTask";
static const char* const bmeTaskName = "BMETask";

/* USER CODE END Variables */
/* Definitions for mainTask */
osThreadId_t mainTaskHandle;
uint32_t mainTaskBuffer[ 512 ];
osStaticThreadDef_t mainTaskControlBlock;
const osThreadAttr_t mainTask_attributes = {
  .name = "mainTask",
  .cb_mem = &mainTaskControlBlock,
  .cb_size = sizeof(mainTaskControlBlock),
  .stack_mem = &mainTaskBuffer[0],
  .stack_size = sizeof(mainTaskBuffer),
  .priority = (osPriority_t) osPriorityNormal,
};

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */

/* USER CODE END FunctionPrototypes */

void mainTaskStart(void *argument);

void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/* Hook prototypes */
void vApplicationStackOverflowHook(TaskHandle_t xTask, signed char *pcTaskName);

/* USER CODE BEGIN 4 */
void vApplicationStackOverflowHook(TaskHandle_t xTask, signed char *pcTaskName)
{
   /* Run time stack overflow checking is performed if
   configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook function is
   called if a stack overflow is detected. */
}
/* USER CODE END 4 */

/* USER CODE BEGIN PREPOSTSLEEP */
__weak void PreSleepProcessing(uint32_t ulExpectedIdleTime)
{
/* place for user code */
}

__weak void PostSleepProcessing(uint32_t ulExpectedIdleTime)
{
/* place for user code */
}
/* USER CODE END PREPOSTSLEEP */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* USER CODE BEGIN RTOS_QUEUES */

  beeperQueue = xQueueCreateStatic(BEEPER_QUEUE_SIZE, BEEPER_QUEUE_ITEM_SIZE, beeperQueueStorageArea, &beeperQueueBuffer);
  if (beeperQueue == NULL) printf("Cannot create Beeper Queue!\n");

  onewireQueue = xQueueCreateStatic(ONEWIRE_QUEUE_SIZE, ONEWIRE_QUEUE_ITEM_SIZE, onewireQueueStorageArea, &onewireQueueBuffer);
  if (onewireQueue == NULL) printf("Cannot create 1-Wire Queue!\n");

  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* creation of mainTask */
  mainTaskHandle = osThreadNew(mainTaskStart, NULL, &mainTask_attributes);

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */

  TaskHandle_t r = xTaskCreateStatic(keysTaskStart, keysTaskName, KEYS_TASK_STACK_DEPTH_WORDS, NULL, 24, keysTaskStackBuffer, &keysTaskBuffer);
  if (r == NULL) printf("Cannot create %s!\n", keysTaskName);

  r = xTaskCreateStatic(ethTaskStart, ethTaskName, ETH_TASK_STACK_DEPTH_WORDS, NULL, 24, ethTaskStackBuffer, &ethTaskBuffer);
  if (r == NULL) printf("Cannot create %s!\n", ethTaskName);

  r = xTaskCreateStatic(mbSlaveTaskStart, mbSlaveTaskName, MB_SLAVE_TASK_STACK_DEPTH_WORDS, NULL, 24, mbSlaveTaskStackBuffer, &mbSlaveTaskBuffer);
  if (r == NULL) printf("Cannot create %s!\n", mbSlaveTaskName);

  r = xTaskCreateStatic(beeperTaskStart, beeperTaskName, BEEPER_TASK_STACK_DEPTH_WORDS, NULL, 24, beeperTaskStackBuffer, &beeperTaskBuffer);
  if (r == NULL) printf("Cannot create %s!\n", beeperTaskName);

  r = xTaskCreateStatic(onewireTaskStart, onewireTaskName, ONEWIRE_TASK_STACK_DEPTH_WORDS, NULL, 24, onewireTaskStackBuffer, &onewireTaskBuffer);
  if (r == NULL) printf("Cannot create %s!\n", onewireTaskName);

  r = xTaskCreateStatic(bmeTaskStart, bmeTaskName, BME_TASK_STACK_DEPTH_WORDS, NULL, 24, bmeTaskStackBuffer, &bmeTaskBuffer);
  if (r == NULL) printf("Cannot create %s!\n", bmeTaskName);

  /* USER CODE END RTOS_THREADS */

  /* USER CODE BEGIN RTOS_EVENTS */
  /* add events, ... */
  /* USER CODE END RTOS_EVENTS */

}

/* USER CODE BEGIN Header_mainTaskStart */
/**
  * @brief  Function implementing the mainTask thread.
  * @param  argument: Not used
  * @retval None
  */
/* USER CODE END Header_mainTaskStart */
__weak void mainTaskStart(void *argument)
{
  /* USER CODE BEGIN mainTaskStart */

  UNUSED(argument);

  /* Infinite loop */
  for(;;)
  {
    osDelay(1);
  }
  /* USER CODE END mainTaskStart */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */

/* USER CODE END Application */

