/*********************************************************************************
 * Copyright(C),Ningbo Joyson Preh Car Connect Co.,Ltd.
 * FileName:    Interrupt.c
 * Author:      Zheng shijie
 * Version :    Ver0.1
 * Date:        2020-10-09
 * Description:
 *
 *********************************************************************************/
#include "Interrupt.h"
#include "Abstraction/Include/OS_Interface.h"
#include "Config/Include/APP_Config.h"
#include "App/include/CanMgrExt.h"
#include "Middleware/CAN/BSW/Ccl/ccl_inc.h"
#include "Abstraction/Driver/GPIO/Gpio_abs.h"
#include "Driver/GPIO/Gpio_drv.h"
#include "App/Power/Power_Os.h"

uint16_t Power_IntSrc;

uint32_t INT_Read_IRQ_Flag(PORT_Type *port_index)
{
	uint32_t IRQValue = port_index->ISFR;
	return IRQValue;
}
uint32_t INT_Clear_IRQ_Flag(PORT_Type *port_index)
{
	port_index->ISFR = ~0U;
	return 0;
}

void INT_WakeUp_Set(PORT_Type *port_index, uint8_t pin_index, uint8_t int_type)
{
	//parameter1:PTC
	//parameter2:PIN_INDEX_9:PTC_9
	//parameter3:PORT_INT_FALLING_EDGE
	uint32_t regValue = port_index->PCR[pin_index];
	regValue &= ~(PORT_PCR_IRQC_MASK);
	regValue |= PORT_PCR_IRQC(int_type);
	port_index->PCR[pin_index] = regValue;
}

void PORTA_IRQHandler(void)
{
	uint32_t IRQflag = 0;

	IRQflag = INT_Read_IRQ_Flag(PORTA);

	/*PTA14:Backup Battery IRQ,Rising Edge:exit,Falling Edge:enter*/
	if((IRQflag&(1 << 14)) != 0)
	{
		/*H:battery exit,L:battery entry*/
		if(GPIO_INPUT_Port_Lev_Read(PTA,PIN_INDEX_14) == PORT_LOW)
		{
			OS_sndMsgFromISR(MSG_ID_POWERMGR,EVENT_MCU_POWER_BATTERY_ENTER_ISR);
		}
		else
		{
			OS_sndMsgFromISR(MSG_ID_POWERMGR,EVENT_MCU_POWER_BATTERY_EXIT_ISR);
		}
		Power_IntSrc |= BACKUP_BATTERY_INT;/*BackUp Battery IRQ,Send ISR Event or Set IRQ Flag*/
	}
	/*Clear IRQ Flag*/
	INT_Clear_IRQ_Flag(PORTA);
}
void PORTB_IRQHandler(void)
{
	;/*No wakeup Src*/
}
void PORTC_IRQHandler(void)
{
	uint32_t IRQflag = 0;

	IRQflag = INT_Read_IRQ_Flag(PORTC);

	/*PTC9:SOS KEY,Falling Edge:Trigger*/
	if((IRQflag&(1 << 9)) != 0)
	{
		Power_IntSrc |= SOS_KEY_INT;/*SOS KEY IRQ,Send ISR Event or Set IRQ Flag*/
	}
	/*PTC11:CAN INH,Rising Edge:Trigger*/
	if((IRQflag&(1 << 11)) != 0)
	{
		Power_IntSrc |= CAN_WAKE_INT;/*CAN INH IRQ,Send ISR Event or Set IRQ Flag*/
		CclCanWakeUpInt();
		//OS_sndMsgFromISR(MSG_ID_CANMGR,CANMGR_TRCV_WAKEUP_INT);
	}
	/*PTC29:BLE,Falling Edge:Trigger*/
	if((IRQflag&(1 << 29)) != 0)
	{
		Power_IntSrc |= BLE_WAKE_INT;/*BLE IRQ,Send ISR Event or Set IRQ Flag*/
	}
	/*Clear IRQ Flag*/
	INT_Clear_IRQ_Flag(PORTC);
}
void PORTD_IRQHandler(void)
{
	uint32_t IRQflag = 0;

	IRQflag = INT_Read_IRQ_Flag(PORTD);
	/*PTD13:Gsensor,Rising Edge:Trigger*/
	if((IRQflag&(1 << 13)) != 0)
	{
		Power_IntSrc |= GSENSOR_INT;/*Gsensor IRQ,Send ISR Event or Set IRQ Flag*/
	}
	/*PTD14:AG550,Falling Edge:Trigger*/
	if((IRQflag&(1 << 14)) != 0)
	{
		Power_IntSrc |= AG550_WAKE_INT;/*AG550 IRQ,Send ISR Event or Set IRQ Flag*/
	}
	/*Clear IRQ Flag*/
	INT_Clear_IRQ_Flag(PORTD);
}
void PORTE_IRQHandler(void)
{
	;/*No wakeup Src*/
}
