/* USER CODE BEGIN Header */
/**
 ******************************************************************************
 * @file           : main.c
 * @brief          : Main program body
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2024 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 "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "main.h"
#include <stdio.h>
#include <stdlib.h>
#include "io_brd.h"
#include "es_brd.h"
#include "ps_brd.h"
#include "ssd_brd.h"
#include "pwr_brd.h"
#include "bp_brd.h"
#include "max14830_task.h"

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

temp_struct es_temp = {0};
ssd_temp_info ssd_temp = {0};
hsc_sns_info_s hsc_sns_info = {0};
eeprom_id_info_s eprm_id_adrr = {0};

/* USER CODE END PTD */

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

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

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
CAN_HandleTypeDef hcan3;
I2C_HandleTypeDef hi2c1;
SPI_HandleTypeDef hspi2;
UART_HandleTypeDef huart1;
UART_HandleTypeDef huart6;

/* Definitions for defaultTask */
osThreadId_t defaultTaskHandle;
const osThreadAttr_t defaultTask_attributes = {
        .name = "defaultTask",
        .stack_size = 128 * 4,
        .priority = (osPriority_t) osPriorityNormal,
};
/* USER CODE BEGIN PV */

uint8_t board_id ,test_id = 0;
uint8_t sts, ssd_id, on_off = 0;
float io_temp = 0;
uint8_t uart_test_id[2] = {0};

UART_HandleTypeDef huart1;
UART_HandleTypeDef huart6;
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_I2C1_Init(void);
static void MX_SPI2_Init(void);
static void MX_USART1_UART_Init(void);
static void MX_USART6_UART_Init(void);
static void MX_CAN3_Init(void);
void StartDefaultTask(void *argument);

/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_I2C1_Init(void);
static void MX_SPI2_Init(void);
static void MX_USART1_UART_Init(void);
static void MX_USART6_UART_Init(void);
/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
 * @brief  The application entry point.
 * @retval int
 */
int main(void)
{
    /* USER CODE BEGIN 1 */

    /* USER CODE END 1 */

    /* MCU Configuration--------------------------------------------------------*/

    /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
    HAL_Init();

    /* USER CODE BEGIN Init */

    /* USER CODE END Init */

    /* Configure the system clock */
    SystemClock_Config();

    /* USER CODE BEGIN SysInit */

    /* USER CODE END SysInit */

    /* Initialize all configured peripherals */
    MX_GPIO_Init();
    MX_I2C1_Init();
    MX_SPI2_Init();
    MX_USART1_UART_Init();
    MX_USART6_UART_Init();
    MX_CAN3_Init();
    /* USER CODE BEGIN 2 */
    /* Initialize all configured peripherals */
    MX_GPIO_Init();
    MX_I2C1_Init();
    MX_SPI2_Init();
    MX_USART1_UART_Init();
    MX_USART6_UART_Init();
    //max14830_start();
    /* USER CODE END 2 */

    /* Init scheduler */
    osKernelInitialize();

    /* 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 */
    /* add queues, ... */
    /* USER CODE END RTOS_QUEUES */

    /* Create the thread(s) */
    /* creation of defaultTask */
    defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);

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

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

    /* Start scheduler */
    osKernelStart();

    /* We should never get here as control is now taken by the scheduler */
    /* Infinite loop */
    /* USER CODE BEGIN WHILE */
    while (1)
    {

        /* USER CODE END WHILE */

        /* USER CODE BEGIN 3 */
    }
    /* USER CODE END 3 */
}

/**
 * @brief System Clock Configuration
 * @retval None
 */
void SystemClock_Config(void)
{
    RCC_OscInitTypeDef RCC_OscInitStruct = {0};
    RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

    /** Configure the main internal regulator output voltage
     */
    __HAL_RCC_PWR_CLK_ENABLE();
    __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

    /** Initializes the RCC Oscillators according to the specified parameters
     * in the RCC_OscInitTypeDef structure.
     */
    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
    RCC_OscInitStruct.HSIState = RCC_HSI_ON;
    RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
    RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
    RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
    RCC_OscInitStruct.PLL.PLLM = 8;
    RCC_OscInitStruct.PLL.PLLN = 200;
    RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
    RCC_OscInitStruct.PLL.PLLQ = 2;
    RCC_OscInitStruct.PLL.PLLR = 2;
    if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
    {
        Error_Handler();
    }

    /** Activate the Over-Drive mode
     */
    if (HAL_PWREx_EnableOverDrive() != HAL_OK)
    {
        Error_Handler();
    }

    /** Initializes the CPU, AHB and APB buses clocks
     */
    RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
            |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
    RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
    RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
    RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
    RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

    if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_6) != HAL_OK)
    {
        Error_Handler();
    }
}

/**
 * @brief CAN3 Initialization Function
 * @param None
 * @retval None
 */
static void MX_CAN3_Init(void)
{

    /* USER CODE BEGIN CAN3_Init 0 */

    CAN_FilterTypeDef sFilterConfig;
    CAN_RxHeaderTypeDef RxHeader;
    CAN_TxHeaderTypeDef TxHeader;
    uint8_t TxData[8] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88};
    uint8_t RxData[8];
    uint32_t TxMailbox;
    // HAL_StatusTypeDef sts;

    /* USER CODE END CAN3_Init 0 */

    /* USER CODE BEGIN CAN3_Init 1 */

    /* USER CODE END CAN3_Init 1 */
    hcan3.Instance = CAN3;
    hcan3.Init.Prescaler = 2;
    hcan3.Init.Mode = CAN_MODE_LOOPBACK;
    hcan3.Init.SyncJumpWidth = CAN_SJW_1TQ;
    hcan3.Init.TimeSeg1 = CAN_BS1_13TQ;
    hcan3.Init.TimeSeg2 = CAN_BS2_2TQ;
    hcan3.Init.TimeTriggeredMode = DISABLE;
    hcan3.Init.AutoBusOff = DISABLE;
    hcan3.Init.AutoWakeUp = DISABLE;
    hcan3.Init.AutoRetransmission = DISABLE;
    hcan3.Init.ReceiveFifoLocked = DISABLE;
    hcan3.Init.TransmitFifoPriority = DISABLE;
    if (HAL_CAN_Init(&hcan3) != HAL_OK)
    {
        Error_Handler();
    }
    /* USER CODE BEGIN CAN3_Init 2 */

    /* Configure CAN filter */
    sFilterConfig.FilterBank = 0;
    sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;
    sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;
    sFilterConfig.FilterIdHigh = 0x0000;
    sFilterConfig.FilterIdLow = 0x0000;
    sFilterConfig.FilterMaskIdHigh = 0x0000;
    sFilterConfig.FilterMaskIdLow = 0x0000;
    sFilterConfig.FilterFIFOAssignment = CAN_RX_FIFO0;
    sFilterConfig.FilterActivation = ENABLE;

    if (HAL_CAN_ConfigFilter(&hcan3, &sFilterConfig) != HAL_OK)
    {
        Error_Handler();  // Filter configuration failed
    }

    /* Start CAN */
    if (HAL_CAN_Start(&hcan3) != HAL_OK)
    {
        sts = HAL_CAN_STATE_ERROR;
    }

    /* Activate RX notifications */
    if (HAL_CAN_ActivateNotification(&hcan3, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK)
    {
        sts = HAL_CAN_STATE_ERROR;
    }

    /* Configure CAN TxHeader */
    TxHeader.StdId = 0x123;
    TxHeader.ExtId = 0x01;
    TxHeader.IDE = CAN_ID_STD;
    TxHeader.RTR = CAN_RTR_DATA;
    TxHeader.DLC = 8;

    /* Transmit CAN message */
    if (HAL_CAN_AddTxMessage(&hcan3, &TxHeader, TxData, &TxMailbox) != HAL_OK)
    {
        sts = HAL_ERROR;
    }

    /* Wait for the message in RX FIFO */
    if (HAL_CAN_GetRxMessage(&hcan3, CAN_RX_FIFO0, &RxHeader, RxData) == HAL_OK)
    {
        // Compare received message with transmitted data
        if (RxHeader.StdId == TxHeader.StdId && RxHeader.DLC == TxHeader.DLC)
        {
            for (int i = 0; i < 8; i++)
            {
                if (RxData[i] != TxData[i])
                {
                    sts = HAL_CAN_STATE_ERROR;
                }
            }
            sts = HAL_OK;
        }
        else
        {
            sts = HAL_CAN_STATE_ERROR;
        }
    }
    else
    {
        sts = HAL_CAN_STATE_ERROR;
    }
    /* USER CODE BEGIN CAN3_Init 2 */

    /* USER CODE END CAN3_Init 2 */

}

/**
 * @brief I2C1 Initialization Function
 * @param None
 * @retval None
 */
static void MX_I2C1_Init(void)
{

    /* USER CODE BEGIN I2C1_Init 0 */

    /* USER CODE END I2C1_Init 0 */

    /* USER CODE BEGIN I2C1_Init 1 */

    /* USER CODE END I2C1_Init 1 */
    hi2c1.Instance = I2C1;
    hi2c1.Init.Timing = 0x00C0EAFF;
    hi2c1.Init.OwnAddress1 = 0;
    hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
    hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
    hi2c1.Init.OwnAddress2 = 0;
    hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
    hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
    hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
    if (HAL_I2C_Init(&hi2c1) != HAL_OK)
    {
        Error_Handler();
    }

    /** Configure Analogue filter
     */
    if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
    {
        Error_Handler();
    }

    /** Configure Digital filter
     */
    if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK)
    {
        Error_Handler();
    }
    /* USER CODE BEGIN I2C1_Init 2 */

    /* USER CODE END I2C1_Init 2 */

}

/**
 * @brief SPI2 Initialization Function
 * @param None
 * @retval None
 */
static void MX_SPI2_Init(void)
{

    /* USER CODE BEGIN SPI2_Init 0 */

    /* USER CODE END SPI2_Init 0 */

    /* USER CODE BEGIN SPI2_Init 1 */

    /* USER CODE END SPI2_Init 1 */
    /* SPI2 parameter configuration*/
    hspi2.Instance = SPI2;
    hspi2.Init.Mode = SPI_MODE_MASTER;
    hspi2.Init.Direction = SPI_DIRECTION_2LINES;
    hspi2.Init.DataSize = SPI_DATASIZE_8BIT;
    hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;
    hspi2.Init.CLKPhase = SPI_PHASE_1EDGE;
    hspi2.Init.NSS = SPI_NSS_SOFT;
    hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
    hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
    hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
    hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
    hspi2.Init.CRCPolynomial = 7;
    hspi2.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
    hspi2.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
    if (HAL_SPI_Init(&hspi2) != HAL_OK)
    {
        Error_Handler();
    }
    /* USER CODE BEGIN SPI2_Init 2 */

    /* USER CODE END SPI2_Init 2 */

}

/**
 * @brief USART1 Initialization Function
 * @param None
 * @retval None
 */
static void MX_USART1_UART_Init(void)
{

    /* USER CODE BEGIN USART1_Init 0 */

    /* USER CODE END USART1_Init 0 */

    /* USER CODE BEGIN USART1_Init 1 */

    /* USER CODE END USART1_Init 1 */
    huart1.Instance = USART1;
    huart1.Init.BaudRate = 115200;
    huart1.Init.WordLength = UART_WORDLENGTH_8B;
    huart1.Init.StopBits = UART_STOPBITS_1;
    huart1.Init.Parity = UART_PARITY_NONE;
    huart1.Init.Mode = UART_MODE_TX_RX;
    huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
    huart1.Init.OverSampling = UART_OVERSAMPLING_16;
    huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
    huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
    if (HAL_UART_Init(&huart1) != HAL_OK)
    {
        Error_Handler();
    }
    /* USER CODE BEGIN USART1_Init 2 */

    /* USER CODE END USART1_Init 2 */

}

/**
 * @brief USART6 Initialization Function
 * @param None
 * @retval None
 */
static void MX_USART6_UART_Init(void)
{

    /* USER CODE BEGIN USART6_Init 0 */

    /* USER CODE END USART6_Init 0 */

    /* USER CODE BEGIN USART6_Init 1 */

    /* USER CODE END USART6_Init 1 */
    huart6.Instance = USART6;
    huart6.Init.BaudRate = 115200;
    huart6.Init.WordLength = UART_WORDLENGTH_8B;
    huart6.Init.StopBits = UART_STOPBITS_1;
    huart6.Init.Parity = UART_PARITY_NONE;
    huart6.Init.Mode = UART_MODE_TX_RX;
    huart6.Init.HwFlowCtl = UART_HWCONTROL_NONE;
    huart6.Init.OverSampling = UART_OVERSAMPLING_16;
    huart6.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
    huart6.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
    if (HAL_UART_Init(&huart6) != HAL_OK)
    {
        Error_Handler();
    }
    /* USER CODE BEGIN USART6_Init 2 */

    /* USER CODE END USART6_Init 2 */

}

/**
 * @brief GPIO Initialization Function
 * @param None
 * @retval None
 */
static void MX_GPIO_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};

    /* GPIO Ports Clock Enable */
    __HAL_RCC_GPIOB_CLK_ENABLE();
    __HAL_RCC_GPIOA_CLK_ENABLE();
    __HAL_RCC_GPIOH_CLK_ENABLE();
    __HAL_RCC_GPIOC_CLK_ENABLE();
    __HAL_RCC_GPIOJ_CLK_ENABLE();

    /*Configure GPIO pin Output Level */
    HAL_GPIO_WritePin(GPIOJ, GPIO_PIN_3, GPIO_PIN_RESET);

    /*Configure GPIO pin : PJ4 */
    GPIO_InitStruct.Pin = GPIO_PIN_4;
    GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOJ, &GPIO_InitStruct);

    /*Configure GPIO pin : PJ3 */
    GPIO_InitStruct.Pin = GPIO_PIN_3;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    HAL_GPIO_Init(GPIOJ, &GPIO_InitStruct);

}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/* USER CODE BEGIN Header_StartDefaultTask */
/**
 * @brief  Function implementing the defaultTask thread.
 * @param  argument: Not used
 * @retval None
 */
/* USER CODE END Header_StartDefaultTask */
void StartDefaultTask(void *argument)
{
    /* USER CODE BEGIN 5 */
    /* Infinite loop */
    while (1)
    {
        printf("\033[1;35mEnter the Board ID :\033[0m\n\n\r");
        printf("NETWORK IO BOARD   -1\n\r");
        printf("EDGE BOARD         -2\n\r");
        printf("PS BOARD           -3\n\r");
        printf("SSD BOARD          -4\n\r");
        printf("POWER BOARD        -5\n\r");
        printf("BACKPLANE BOARD    -6\n\n\r");

        if(HAL_UART_Receive(&huart1,&board_id, 1, HAL_MAX_DELAY) == HAL_OK)
        {
            board_id = atoi((const char *)&board_id);   ///< Received uart => char value to int convert
            if(board_id > TEST_CASE_0 && board_id < TEST_CASE_7)
            {
                switch (board_id)
                {
                /*
                 * IO BOARD boot function
                 */
                case IO_BRD:
                    printf("\033[1;34mReceived board ID = %d\033[0m\n\n\r",board_id);
                    printf("\033[1;35mEnter the NETWORK IO board Test ID:\033[0m\n\n\r");
                    printf("Network IO board section A test   -1\n\r");
                    printf("Network IO board section B test   -2\n\r");
                    printf("Network IO board Sensor Test      -3\n\r");
                    printf("Network IO board USB Port test    -4\n\n\r");

                    if(HAL_UART_Receive(&huart1,&test_id, 1, HAL_MAX_DELAY) == HAL_OK)
                    {
                        test_id = atoi((const char *)&test_id);
                        printf("\033[1;34mReceived Test ID = %d\033[0m\n\n\r",test_id);
                    }
                    if(test_id == TEST_CASE_1)
                    {
                        sts = io_board_test_sec_a();                  ///< IO_BOARD - A TEST FUNCTION
                        if(sts == HAL_OK)
                        {
                            printf("Network IO Section-A SUCCESS \n\n\r");
                        }
                        else
                        {
                            printf("\033[1;31mI2C COMMUNICATION FAIL\033[0m \n\n\r");
                            printf("Network IO Section-A \033[1;31mFAIL \033[0m\n\n\r");
                        }
                    }
                    else if(test_id == TEST_CASE_2)
                    {
                        sts = io_board_test_sec_b();                 ///< IO_BOARD - B TEST FUNCTION
                        if(sts == HAL_OK)
                        {
                            printf("Network IO Section-B SUCCESS \n\n\r");
                        }
                        else
                        {
                            printf("\033[1;31mI2C COMMUNICATION FAIL\033[0m \n\n\r");
                            printf("Network IO Section-B \033[1;31mFAIL \033[0m \n\n\r");
                        }
                    }
                    else if(test_id == TEST_CASE_3)
                    {
                        sts = io_brd_get_tempreature(&io_temp);     ///< IO_BOARD GET TEMPERATURE FUNCTION
                        printf("IO_BRD Temperature : %d\n\n\r",(int)io_temp);
                    }
                    else if(test_id == TEST_CASE_4)
                    {
                        printf("IO_BRD USB ENABLE  -1\n\r");
                        printf("IO_BRD USB DISABLE -2\n\n\r");
                        if(HAL_UART_Receive(&huart1,&test_id, 1, HAL_MAX_DELAY) == HAL_OK)
                        {
                            test_id = atoi((const char *)&test_id);
                            printf("\033[1;34mReceived Test ID = %d\033[0m\n\n\r",test_id);
                        }
                        sts = io_brd_usb_deduct_func(test_id);     ///< IO_BOARD USB DETECT FUNCTION
                    }
                    break;

                    /*
                     * ES BOARD boot function
                     */
                case ES_BRD:
                    printf("\033[1;35mEnter the Edge board Test ID:\033[0m\n\n\r");
                    printf("EDGE BOARD 1                   -1\n\r");
                    printf("EDGE BOARD 2                   -2\n\r");
                    printf("EDGE BOARD Sensor Test         -3\n\r");
                    printf("EDGE BOARD POWER OFF           -4\n\n\r");
                    if(HAL_UART_Receive(&huart1,&test_id, 1, HAL_MAX_DELAY) == HAL_OK)
                    {
                        test_id = atoi((const char *)&test_id);
                        printf("\033[1;34mReceived Test ID = %d\033[0m\n\n\r",test_id);
                    }
                    if(test_id == TEST_CASE_1 || test_id == TEST_CASE_2)    ///< if select Edge1 or Edge2
                    {
                        printf("\033[1;35mEnter the Edge board SSD Slot Test ID:\033[0m\n\n\r");
                        printf("EDGE_1_SSD_0                   -1\n\r");
                        printf("EDGE_1_SSD_1                   -2\n\r");
                        printf("EDGE_2_SSD_0                   -3\n\r");
                        printf("EDGE_2_SSD_1                   -4\n\n\r");
                        if(HAL_UART_Receive(&huart1,&ssd_id, 1, HAL_MAX_DELAY) == HAL_OK)
                        {
                            ssd_id = atoi((const char *)&ssd_id);
                            printf("\033[1;34mReceived SSD Test ID = %d\033[0m\n\n\r",ssd_id);
                        }
                        if(test_id == TEST_CASE_1)
                        {
                            sts = es_brd_test1();            ///< Edge 1 BOOT FUNCTION
                            sts = edge1_ssd_test(ssd_id);    ///< Edge 1 SSD DETECT FUNCTION
                            if(sts == HAL_OK)
                            {
                                printf("Edge_1 Boot SUCCESS \n\n\r");
                            }
                            else
                            {
                                printf("\033[1;31mI2C COMMUNICATION FAIL\033[0m \n\n\r");
                                printf("Edge_1 Boot \033[1;31mFAIL \033[0m \n\n\r");
                            }
                        }
                        else if(test_id == TEST_CASE_2)
                        {
                            sts = es_brd_test2();            ///< Edge 2 BOOT FUNCTION
                            sts = edge2_ssd_test(ssd_id);    ///< Edge 2 SSD DETECT FUNCTION
                            if(sts == HAL_OK)
                            {
                                printf("Edge_2 Boot SUCCESS \n\n\r");
                            }
                            else
                            {
                                printf("\033[1;31mI2C COMMUNICATION FAIL\033[0m \n\n\r");
                                printf("Edge_2 Boot \033[1;31mFAIL \033[0m \n\n\r");
                            }
                        }
                    }
                    else if(test_id == TEST_CASE_3)        ///< Get temperature value
                    {
                        sts = edge_brd_get_tempreature(&es_temp);
                        printf("ES_BRD U13 Temperature : %d\n\r",(int)es_temp.temp_1);
                        printf("ES_BRD U34 Temperature : %d\n\n\r",(int)es_temp.temp_2);
                    }
                    else if(test_id == TEST_CASE_4)        ///< Shutdown or Turn off the Edge1 or Edge2
                    {
                        printf("\033[1;35mEnter the Edge board OFF Test ID:\033[0m\n\n\r");
                        printf("EDGE 1                   -1\n\r");
                        printf("EDGE 2                   -2\n\n\r");
                        if(HAL_UART_Receive(&huart1,&ssd_id, 1, HAL_MAX_DELAY) == HAL_OK)
                        {
                            ssd_id = atoi((const char *)&ssd_id);
                            printf("\033[1;34mReceived SSD Test ID = %d\033[0m\n\n\r",ssd_id);
                        }
                        sts = edge_brd_pwr_off(&ssd_id);
                    }
                    break;

                    /*
                     * PS BOARD boot function
                     */
                case PS_BRD:
                    printf("\033[1;35mEnter the PS board Test ID:\033[0m\n\n\r");
                    printf("PS BOARD Boot Test                   -1\n\r");
                    printf("PS BOARD SENSOR TEST                 -2\n\r");
                    printf("PS BOARD GATE TEST                   -3\n\r");
                    printf("PS BOARD POWER OFF                   -4\n\n\r");
                    if(HAL_UART_Receive(&huart1,&test_id, 1, HAL_MAX_DELAY) == HAL_OK)
                    {
                        test_id = atoi((const char *)&test_id);
                        printf("\033[1;34mReceived Test ID = %d\033[0m\n\n\r",test_id);
                    }
                    if(test_id == TEST_CASE_1)          ///< Payload Board Boot Test
                    {
                        sts = ps_brd_test1();
                        if(sts == HAL_OK)
                        {
                            printf("Payload Board_1 Boot SUCCESS \n\n\r");
                        }
                        else
                        {
                            printf("\033[1;31mI2C COMMUNICATION FAIL\033[0m \n\n\r");
                            printf("Payload Board_1 Boot \033[1;31mFAIL \033[0m \n\n\r");
                        }
                    }
                    else if(test_id == TEST_CASE_2)     ///< Get Temperature form Payload Board
                    {
                        sts = ps_brd_get_tempreature();
                    }
                    else if(test_id == TEST_CASE_3)     ///< Payload Board gate pin test
                    {
                        printf("\033[1;35mEnter the test ID:\033[0m\n\r");
                        printf("PS_GATE_SUPPLY_ON         -0\n\r");
                        printf("PS_GATE_SUPPLY_OFF        -1\n\n\r");
                        if(HAL_UART_Receive(&huart1,&test_id, 1, HAL_MAX_DELAY) == HAL_OK)
                        {
                            test_id = atoi((const char *)&test_id);
                            printf("\033[1;34mReceived BOARD Test ID = %d\033[0m\n\n\r",test_id);
                            if (test_id % 2 == 0)
                            {
                                on_off = 0;
                                ps_brd_gate_test(&test_id,on_off);
                            }
                            else if (test_id % 2 != 0)
                            {
                                on_off = 1;
                                ps_brd_gate_test(&test_id,on_off);
                            }
                        }
                    }
                    else if(test_id == TEST_CASE_4)     ///< Shutdown or Turn off the Payload Board
                    {
                        sts = ps_brd_power_off();
                    }
                    break;

                    /*
                     * SSD BOARD boot function
                     */
                case SSD_BRD:
                    printf("\033[1;35mEnter the SSD board Test ID:\033[0m\n\n\r");
                    printf("SSD BOARD SSD Slot Test       -1\n\r");
                    printf("SSD BOARD Gpio expander Test  -2\n\r");
                    printf("SSD BOARD Sensor Test         -3\n\n\r");
                    if(HAL_UART_Receive(&huart1,&board_id, 1, HAL_MAX_DELAY) == HAL_OK)
                    {
                        board_id = atoi((const char *)&board_id);
                        printf("\033[1;34mReceived SSD Test ID = %d\033[0m\n\n\r",board_id);
                    }
                    if(board_id == TEST_CASE_1)     ///< SSD Board SSD slot detect Test
                    {
                        printf("\033[1;35mEnter the board ID:\033[0m\n\r");
                        printf("PAYLOAD_A_SSD_0         -1\n\r");
                        printf("PAYLOAD_A_SSD_1         -2\n\r");
                        printf("PAYLOAD_B_SSD_0         -3\n\r");
                        printf("PAYLOAD_B_SSD_1         -4\n\r");
                        printf("SSD_2                   -5\n\r");
                        printf("SSD_3                   -6\n\n\r");
                        if(HAL_UART_Receive(&huart1,&test_id, 1, HAL_MAX_DELAY) == HAL_OK)
                        {
                            test_id = atoi((const char *)&test_id);
                            printf("\033[1;34mReceived Test ID = %d\033[0m\n\n\r",test_id);
                        }
                        sts = ssd_board_id_test(&test_id);
                        if(sts != HAL_OK)
                        {
                            printf("SSD Board detect \033[1;31mFAIL \033[0m \n\n\r");
                        }
                    }

                    if(board_id == TEST_CASE_2)     ///< SSD Board GPIO Expander Test
                    {
                        ssd_board_gpio_exp_test();
                    }
                    if(board_id == TEST_CASE_3)     ///< Get Temperature form SSD Board SSD slots
                    {
                        sts = ssd_brd_get_tempreature(&ssd_temp);
                        printf("SSD_BRD_U23_temperature   : %d\n\r",(int)ssd_temp.ssd_temp1);
                        printf("SSD_BRD_U24_temperature   : %d\n\r",(int)ssd_temp.ssd_temp2);
                        printf("SSD_BRD_U26_temperature   : %d\n\r",(int)ssd_temp.ssd_temp3);
                        printf("SSD_BRD_U27_temperature   : %d\n\n\r",(int)ssd_temp.ssd_temp4);
                    }
                    break;

                    /*
                     * PWR BOARD test function
                     */
                case PWR_BRD:
                    printf("\033[1;35mEnter the PWR board Test ID:\033[0m\n\n\r");
                    printf("PWR BOARD GPIO EXPANDER Test                -1\n\r");
                    printf("PWR BOARD SENSOR SELF Test                  -2\n\r");
                    printf("PWR BOARD POWER SENSOR Test                 -3\n\r");
                    printf("PWR BOARD HOT SWAP CONTROLLER Test          -4\n\r");
                    printf("PWR BOARD POWER ADC7291 Test                -5\n\r");
                    printf("PWR BOARD POWER SUPPLY Test                 -6\n\r");
                    printf("PWR BOARD UART_RS485 TEST                   -7\n\r");
                    printf("PWR BOARD HEAT COIL SUPPLY Test             -8\n\n\r");
                    if(HAL_UART_Receive(&huart1,&board_id, 1, HAL_MAX_DELAY) == HAL_OK)
                    {
                        board_id = atoi((const char *)&board_id);
                        printf("\033[1;34mReceived Test ID = %d\033[0m\n\n\r",board_id);
                    }
                    if(board_id == TEST_CASE_1)         ///< Power Board GPIO Expander Test
                    {
                        pwr_brd_gpio_exp_self_test();
                    }
                    else if(board_id == TEST_CASE_2)    ///< Power Board Sensor Test
                    {
                        printf("\033[1;35mEnter the sensor Test ID:\033[0m\n\n\r");
                        printf("POWER SENSOR MONITOR -1\n\rHOT SWAP CONTROLLER  -2\n\n\r");
                        if(HAL_UART_Receive(&huart1,&test_id, 1, HAL_MAX_DELAY) == HAL_OK)
                        {
                            test_id = atoi((const char *)&test_id);
                            printf("\033[1;34mReceived Test ID = %d\033[0m\n\n\r",test_id);
                            pwr_brd_sensor_self_test(&test_id);
                        }
                    }
                    else if(board_id == TEST_CASE_3)    ///< Get PSM Sensor value
                    {
                        pwr_brd_get_psm_tempreature(&hsc_sns_info);
                    }
                    else if(board_id == TEST_CASE_4)    ///< Get HSC Sensor value
                    {
                        pwr_brd_get_hsc_tempreature(&hsc_sns_info);
                    }
                    else if(board_id == TEST_CASE_5)    ///< ADC7291 Sensor Test
                    {
                        pwr_brd_adc7291_self_test();
                    }

                    if(board_id == TEST_CASE_6)         ///< Shutdown or Turn on the Boards
                    {
                        printf("\033[1;35mEnter the test ID:\033[0m\n\r");
                        printf("PS1_SUPPLY_ON         -0\n\r");
                        printf("PS1_SUPPLY_OFF        -1\n\r");
                        printf("PS2_SUPPLY_ON         -2\n\r");
                        printf("PS2_SUPPLY_OFF        -3\n\r");
                        printf("ES1_SUPPLY_ON         -4\n\r");
                        printf("ES1_SUPPLY_OFF        -5\n\r");
                        printf("ES2_SUPPLY_ON         -6\n\r");
                        printf("ES2_SUPPLY_OFF        -7\n\r");
                        printf("IO_SUPPLY_ON          -8\n\r");
                        printf("IO_SUPPLY_OFF         -9\n\n\r");
                        if(HAL_UART_Receive(&huart1,&test_id, 1, HAL_MAX_DELAY) == HAL_OK)
                        {
                            test_id = atoi((const char *)&test_id);
                            printf("\033[1;34mReceived BOARD Test ID = %d\033[0m\n\n\r",test_id);
                        }
                        if (test_id % 2 == 0)
                        {
                            on_off = 0;
                            power_board_supply_test(&test_id,on_off);
                        }
                        else if (test_id % 2 != 0)
                        {
                            on_off = 1;
                            power_board_supply_test(&test_id,on_off);
                        }
                    }
                    if(board_id == TEST_CASE_7)         ///< RS485 converter test
                    {
                        printf("\033[1;35mEnter the Test ID:\033[0m\n\n\r");
                        printf("RS_485_CORE_BRD_A          -1\n\r");
                        printf("RS_485_CORE_BRD_B          -2\n\n\r");
                        if(HAL_UART_Receive(&huart1,&board_id, 1, HAL_MAX_DELAY) == HAL_OK)
                        {
                            board_id = atoi((const char *)&board_id);
                            printf("\033[1;34mReceived BOARD Test ID = %d\033[0m\n\n\r",board_id);
                        }
                        printf("\033[1;35mEnter the test ID:\033[0m\n\n\r");
                        printf("RS_485_TX_TEST            -1\n\r");
                        printf("RS_485_RX_TEST            -2\n\n\r");
                        if(HAL_UART_Receive(&huart1,&test_id, 1, HAL_MAX_DELAY) == HAL_OK)
                        {
                            test_id = atoi((const char *)&test_id);
                            printf("\033[1;34mReceived BOARD Test ID = %d\033[0m\n\n\r",test_id);
                            uart_test_id[0] = board_id;
                            uart_test_id[1] = test_id;
                            uart_rs485_test((uint8_t *)&uart_test_id);
                        }
                    }
                    if (board_id == TEST_CASE_8)       ///< Turn ON & OFF the Heat Coils supply
                    {
                        printf("\033[1;35mEnter the test ID:\033[0m\n\n\r");
                        printf("HEAT_ENABLE_TOP_1_SUPPLY_ON            -0\n\r");
                        printf("HEAT_ENABLE_TOP_1_SUPPLY_OFF           -1\n\r");
                        printf("HEAT_ENABLE_TOP_2_SUPPLY_ON            -2\n\r");
                        printf("HEAT_ENABLE_TOP_2_SUPPLY_OFF           -3\n\r");
                        printf("HEAT_ENABLE_BOTTOM_1_SUPPLY_ON         -4\n\r");
                        printf("HEAT_ENABLE_BOTTOM_1_SUPPLY_OFF        -5\n\r");
                        printf("HEAT_ENABLE_BOTTOM_2_SUPPLY_ON         -6\n\r");
                        printf("HEAT_ENABLE_BOTTOM_2_SUPPLY_OFF        -7\n\r");
                        printf("HEAT_ENABLE_SSD1_SUPPLY_ON             -8\n\r");
                        printf("HEAT_ENABLE_SSD1_SUPPLY_OFF            -9\n\r");
                        printf("HEAT_ENABLE_SSD2_SUPPLY_ON             -10\n\r");
                        printf("HEAT_ENABLE_SSD2_SUPPLY_OFF            -11\n\n\r");

                        uint8_t buffer[2] = {0};  // Buffer for receiving the test ID as a string
                        if (HAL_UART_Receive(&huart1, buffer, 2, HAL_MAX_DELAY) == HAL_OK)
                        {
                            buffer[3] = '\0';     // Null-terminate the string
                            test_id = atoi((const char *)buffer);
                            printf("\033[1;34mReceived BOARD Test ID = %d\033[0m\n\n\r", test_id);
                            if (test_id % 2 == 0)
                            {
                                on_off = 0;       // Even IDs are ON
                            }
                            else
                            {
                                on_off = 1;  // Odd IDs are OFF
                            }
                            power_board_top_bot_supply_test(&test_id, on_off);
                        }
                    }
                    break;

                    /*
                     * BP BOARD test function
                     */
                case BP_BRD:
                    printf("\033[1;35mEnter the Backplane board Test ID:\033[0m\n\n\r");
                    printf("BP BOARD GPIO EXPANDER TEST  -1\n\r");
                    printf("BP BOARD EEPROM TEST         -2\n\r");
                    printf("BP BOARD SPI - UART TEST     -3\n\n\r");
                    if(HAL_UART_Receive(&huart1,&board_id, 1, HAL_MAX_DELAY) == HAL_OK)
                    {
                        board_id = atoi((const char *)&board_id);
                        printf("\033[1;34mReceived BP Board Test ID = %d\033[0m\n\n\r",board_id);
                    }
                    if(board_id == TEST_CASE_1)         ///< BackPlane Board GPIO Expander test
                    {
                        bp_brd_gpio_exp_self_test();
                    }
                    else if(board_id == TEST_CASE_2)    ///< BackPlane Board EEPROM Test
                    {
                        printf("\033[1;35mEnter the EEPROM Test ID:\033[0m\n\n\r");
                        printf("EEPROM_1        -1\n\r");
                        printf("EEPROM_2        -2\n\n\r");
                        if(HAL_UART_Receive(&huart1,&board_id, 1, HAL_MAX_DELAY) == HAL_OK)
                        {
                            board_id = atoi((const char *)&board_id);
                            printf("\033[1;34mReceived eeprom Test ID = %d\033[0m\n\n\r",board_id);
                            eprm_id_adrr.eeprom_id = board_id;
                        }
                        printf("\033[1;35menter eeprom value\033[0m\n\n\r");
                        printf("0x00 -1\n\r0xAA -2\n\r0x55 -3\n\r0xFF -4\n\r");
                        if(HAL_UART_Receive(&huart1,&test_id, 1, HAL_MAX_DELAY) == HAL_OK)
                        {
                            test_id = atoi((const char *)&test_id);
                            printf("\033[1;34mReceived eeprom Test ID = %x\033[0m\n\n\r",test_id);
                            eprm_id_adrr.eeprom_val = test_id;
                        }
                        sts = bp_eeprom_test(&eprm_id_adrr);
                        if(sts == HAL_OK)
                        {
                            printf("EEPROM Test Success\n\n\r");
                        }
                        else
                        {
                            printf("EEPROM Test \033[1;31mFAIL \033[0m\n\n\r");
                        }
                    }
                    else if(board_id == TEST_CASE_3)
                    {
                        // spi_uart_test_func();
                    }
                    break;

                default:
                    break;
                }
            }

            else
            {
                printf("INVALID board ID \n\n\r");
            }
        }
    }
    /* USER CODE END 5 */
}

/**
 * @brief  This function is executed in case of error occurrence.
 * @retval None
 */
void Error_Handler(void)
{
    /* USER CODE BEGIN Error_Handler_Debug */
    /* User can add his own implementation to report the HAL error return state */
    __disable_irq();
    while (1)
    {
    }
    /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
 * @brief  Reports the name of the source file and the source line number
 *         where the assert_param error has occurred.
 * @param  file: pointer to the source file name
 * @param  line: assert_param error line source number
 * @retval None
 */
void assert_failed(uint8_t *file, uint32_t line)
{
    /* USER CODE BEGIN 6 */
    /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
