/*==================================================================================================
*   Project              : RTD AUTOSAR 4.4
*   Platform             : CORTEXM
*   Peripheral           : S32K3XX
*   Dependencies         : none
*
*   Autosar Version      : 4.4.0
*   Autosar Revision     : ASR_REL_4_4_REV_0000
*   Autosar Conf.Variant :
*   SW Version           : 0.9.0
*   Build Version        : S32K3_RTD_0_9_0_P02_D2107_ASR_REL_4_4_REV_0000_20210716
*
*   (c) Copyright 2020 - 2021 NXP Semiconductors
*   All Rights Reserved.
*
*   NXP Confidential. This software is owned or controlled by NXP and may only be
*   used strictly in accordance with the applicable license terms. By expressly
*   accepting such terms or by downloading, installing, activating and/or otherwise
*   using the software, you are agreeing that you have read, and that you agree to
*   comply with and are bound by, such license terms. If you do not agree to be
*   bound by the applicable license terms, then you may not retain, install,
*   activate or otherwise use the software.
==================================================================================================*/

/**
*   @file main.c
*
*   @addtogroup main_module main module documentation
*   @{
*/

/* Including necessary configuration files. */
#include "Mcal.h"

/* User includes */
#define __APP_MAIN__
	uint8_t write_flag = 1;


#include "FlexCAN_Ip_Types.h"
#include "Lpuart_Uart_Ip.h"
#include "Lpuart_Uart_Ip_Irq.h"
#include "Clock_Ip.h"
#include "IntCtrl_Ip.h"
#include "Flexio_Mcl_Ip.h"
#include "Adc_Sar_Ip.h"
#include "FlexCAN_Ip.h"
#include "Emios_Pwm_Ip.h"
#include "Emios_Mcl_Ip.h"
#include "Siul2_Port_Ip.h"
#include "Siul2_Dio_Ip.h"
#include "Pit_Ip.h"
#include <string.h>

#include "main.h"
#include "can.h"
#include "board.h"
#include "KATECK_Logic.h"
#include "IG_Layer/Ignition.h"
#include "System_Check.h"
#include <Operation_Layer/Drive_Mode.h>
#include "Global_Variable.h"

	//==========================PYO====
#include "Operation_Layer/Lamp/Lamp.h"

//========================== bsw
#include "def_uds.h"
#include "uds.h"

//========================== bsw

#include "watchdog_hal.h"

uint16_t adc0;

extern uint8 rxBufferSlave[TRANSFER_SIZE];


// VERSION
__attribute__ ((section(".appversion"))) const char AppVersion[]= {
		"GW344 V0.2.5 2023.11.6 12:00\n\r"
};
void (*const appsign[])() __attribute__ ((section(".appsign"))) =
{
    0x55AA1234,			//  0x55AA1234  bootloader
    0x00000000
};

void (*const boot_vector[])() __attribute__ ((section(".boot_vector"))) =
{
    0x5AA55AA5,			//  0x55AA1234  bootloader
    0x00000001,
    0x00000000,
    0x00401000
};

/*!
  \brief The main function for the project.
  \details The startup initialization sequence is the following:
 * - startup asm routine
 * - main()
*/
int main(void)
{
    /* Write your code here */
	StatusType status;
	int	retry, i, port;
	char	ch;
	u64		tick_100ms, tick_10ms,tick_20ms,tick_50ms, tick_2ms, tick_1sec, tick_1ms,tick_500us, tick_5ms, tick_200ms;



	Gvar_init();

	/* Init clock  */
	Clock_Ip_Init(&Mcu_aClockConfigPB[0]);

	/* Initialize all pins */
	Siul2_Port_Ip_Init(NUM_OF_CONFIGURED_PINS0, g_pin_mux_InitConfigArr0);

	/* Initialize IRQs */
	IntCtrl_Ip_Init(&IntCtrlConfig_0);
	IntCtrl_Ip_ConfigIrqRouting(&intRouteConfig);

    // /* Init osif */
    // OsIf_Init(NULL_PTR);

	/* --- init Uart Channels --- */
	Uart_Init();

	/* --- init pwm channels --- */
	pwm_init(gb.pwm_max_tick);

	/* --- ADC init --- */
	Adc_Init();

	/* --- I2C init --- */
	i2c1_init();
	i2c0_init();

	/* --- SPI init --- */
	SPI_init();

	/* --- Lin init --- */
	// Lin_init();

	/*Initial PIT instance 0 - Channel 0*/
	Pit_Ip_Init(PIT_INST_0, &PIT_0_InitConfig_PB);
	/*Initial channel 0 */
	Pit_Ip_InitChannel(PIT_INST_0, PitChannel_0);
	/*Enable channel interrupt PIT_0 - CH_0*/
	Pit_Ip_EnableChannelInterrupt(PIT_INST_0, CH_0);
	/*Start channel CH_0*/
	Pit_Ip_StartChannel(PIT_INST_0, CH_0, PIT_PERIOD);

	/* --- CAN init --- */

	for(ch=0; ch<MAX_CAN_INST; ch++)	Can_Init_ECU3(ch);

	board_init();

	tick_500us = gb.tmr_cnt;
	tick_1ms = gb.tmr_cnt;
	tick_2ms = gb.tmr_cnt;
	tick_5ms = gb.tmr_cnt;
	tick_10ms = gb.tmr_cnt;
	tick_20ms = gb.tmr_cnt;
	tick_50ms = gb.tmr_cnt;
	tick_100ms = gb.tmr_cnt;
	tick_200ms = gb.tmr_cnt;
	tick_1sec = gb.tmr_cnt;
	tmr_delay(1000);

	WATCHDOG_HAL_Init();

	debug_printf("Board Init Finished\n\r");
	// for (i=0;i<=24;i++)
	// {
	// 	Test_Signal_1_[i] = 0xFF;
	// }

	#ifdef EMC_TEST
//	Set_PWM_Duty(PWM_CH0,50);
//	Set_PWM_Duty(PWM_CH1,50);
//	Set_PWM_Duty(PWM_CH2,50);
//	Set_PWM_Duty(PWM_CH3,50);
	#endif

	ECU3_KATECH_Logic_init();

	while(1)
	{

		if(gb.tmr_cnt >= (tick_1sec + 1000))
		{
			tick_1sec = gb.tmr_cnt;
			task_1s();
		}

		if(gb.tmr_cnt >= (tick_200ms + 200))
		{
			tick_200ms = gb.tmr_cnt;
			task_200ms();
		}

		if(gb.tmr_cnt >= (tick_100ms + 100))
		{
			tick_100ms = gb.tmr_cnt;
			task_100ms();
		}

		if(gb.tmr_cnt >= (tick_50ms + 50))
		{
			tick_50ms = gb.tmr_cnt;
			task_50ms();
		}

		if(gb.tmr_cnt >= (tick_20ms + 20))
		{
			tick_20ms = gb.tmr_cnt;
			task_20ms();
		}

		if(gb.tmr_cnt >= (tick_10ms + 10))
		{
			tick_10ms = gb.tmr_cnt;
			task_10ms();
		}

		if(gb.tmr_cnt >= (tick_5ms + 5))
		{
			tick_5ms = gb.tmr_cnt;
			task_5ms();
		}

		if(gb.tmr_cnt >= (tick_2ms + 2))
		{
			tick_2ms = gb.tmr_cnt;
			task_2ms();
		}

		if(gb.tmr_cnt >= (tick_1ms + 1))
		{
			tick_1ms = gb.tmr_cnt;
			task_1ms();
		}

		if(gb.x500us >= tick_500us+1)
		{
			tick_500us = gb.x500us;
			task_500us();

		}
		
		WATCHDOG_HAL_Fed();
		uds_wait();
	}

}



void task_500us(void)
{
	can_tx_check(); // 원래 아래에 있었는데 위로 올린거임

//	UART0_get_cmd();

	can_main_ECU3(); //STEP1. CAN_Receive, STEP2. CAN_Data_set

	
}

void task_1ms(void)
{

}

void task_2ms(void)
{

	// 속도제어 로직 2ms 단위로 동작
	// 해당 공간에 로직 구동 예정
	ECU3_KATECH_Logic_2ms();

}

void task_5ms(void)
{
	CAN_Task_5ms();
}

void task_10ms(void)
{
//	Adc_Read_ch(ADC_INST_0);

//	Adc_Read_ch(ADC_INST_1);
//	Adc_Read_ch(ADC_INST_2);

	CAN_Task_10ms();
	//debug_printf("Process : %d",IG_Process_Status);

//	RC_Motor_Logic();
}

void task_20ms(void)
{
	// DIN_TEST();


	CAN_Task_20ms();
}
uint8_t test_pwm;
void task_50ms(void)
{

}
uint32_t C_100ms = 0;
void task_100ms(void)
{
	if (test_pwm < 100) test_pwm++;
	else test_pwm = 0;
	C_100ms++;
	// if(C_100ms%10 == 0) debug_printf("C_100ms : %d\r\n",C_100ms);
	CAN_Task_100ms();

//	ALL_BCM_Signal_Check(); // BCM 신호 검사 함수
//	ALL_MCU_Signal_Check(); // MCU 신호 검사 함수

}

void task_200ms(void)
{
	CAN_Task_200ms();
	PWM_for_dirve_mode();
}

uint8_t toggle_PWM;
uint8_t PWM_CH=1;
void task_1s(void)
{

	CAN_Task_1000ms();


	uint16_t adc_mV,din1,din2;

	led_onoff(!gb.led_flag);








//	 Set_PWM_Duty(PWM_CH2,test_pwm);
	// Set_PWM_Duty(PWM_CH3,test_pwm);


//	 adc0 = ADC_Read(ADC_0);
//	 EMEMERGNECY_BUTTEN = Digital_Read(DIN_0);
//
//	 EMEMERGNECY_BUTTEN = din1;



	// din2 = Digital_Read(DIN_2);

//	 Digital_Write(DOUT_1,1);
	// Digital_Write(DOUT_2,write_flag);

	// if(write_flag == 1) write_flag = 0;
	// else if(write_flag == 0) write_flag = 1;

	// i2c_test();
	// SPI_test();
	// Lin_tx_test(LIN_INSTANCE_1);
	



#ifdef DEBUG_TEST
	if(gb.Debug_test_No/10 == 2) 
		debug_printf("Digital Input CH : %d, Value : %d\r\n\n",(gb.Debug_test_No%10),Digital_Read(gb.Debug_test_No%10));
	else if(gb.Debug_test_No/10 == 3) 
		debug_printf("Analog Input CH : %d, Value : %d\r\n\n",(gb.Debug_test_No%10),ADC_Read(gb.Debug_test_No%10));
	else if(gb.Debug_test_No == 6)
	{
		i2c_test();
		for(uint8_t i;i<TRANSFER_SIZE;i++)	debug_printf("%d  ",rxBufferSlave[i]);
		debug_printf("\r\n");
	}

#endif



}
