/**
  *****************************************************************************
  * 					ûжϴͳһ
  *****************************************************************************
  *
  * @File   : ISR.c
  * @By     : Sun
  * @Version: V0.5
  * @Date   : 2022 / 06 / 01
  * @Shop	: https://xiaomeige.taobao.com/
  *
  *****************************************************************************
**/

#include "ISR.h"
#include "lcd.h"
//ʱ־λ
static uint8_t Flag_20ms = 0;
static uint8_t Flag_30ms = 0;
static uint8_t Flag_50ms = 0;
static uint8_t Flag_100ms = 0;
static uint8_t Flag_200ms = 0;
static uint8_t Flag_500ms = 0;

//̵ı־λ
uint8_t Flag_DrawWave = 0;		//ƲΣ10msһ
uint8_t Flag_DrawGrid = 0;		//20msһ
uint8_t Flag_TouchScan = 0;		//ɨ裬30msһ
uint8_t Flag_Refresh_Val = 0;	//ˢµѹƵֵ500msһ

/**
  *****************************************************
  * @brief	˽жʱжϴ
  * @tag	˽жʱжϣڲû򼴿
  *****************************************************
**/
void ScuTimer_IRQ_Handler(void *CallBackRef)
{
	/* û */
	//ͨʱÿ̵ĴӦı־λ
	Flag_DrawWave = 1;

	if(Flag_20ms >= 1) {
		Flag_20ms = 0;
		Flag_DrawGrid = 1;
	}
	else
		Flag_20ms++;

	if(Flag_30ms >= 2) {
		Flag_30ms = 0;
		Flag_TouchScan = 1;
	}
	else
		Flag_30ms++;

	if(Flag_50ms >= 4) {
		Flag_50ms = 0;

	}
	else
		Flag_50ms++;

	if(Flag_100ms >= 9) {
		Flag_100ms = 0;

	}
	else
		Flag_100ms++;

	if(Flag_200ms >= 19) {
		Flag_200ms = 0;

	}
	else
		Flag_200ms++;

	if(Flag_500ms >= 49) {
		Flag_500ms = 0;
		Flag_Refresh_Val = 1;
	}
	else
		Flag_500ms++;


	/*  */
    XScuTimer_ClearInterruptStatus(&ScuTimer);
}

//־жϴڽ¼֪ͨӦó
volatile int RxDone;
volatile int Error;

//DMARxжϷ
void AXI_DMARx_IRQHandler(void *Callback)
{
	uint32_t IrqStatus;

	//ȡж
	IrqStatus = XAxiDma_IntrGetIrq(&AxiDma0, XAXIDMA_DEVICE_TO_DMA);

	//ȷϹж
	XAxiDma_IntrAckIrq(&AxiDma0, IrqStatus, XAXIDMA_DEVICE_TO_DMA);

	//ûзжϣִκβ
	if (!(IrqStatus & XAXIDMA_IRQ_ALL_MASK)) {
		return;
	}

	//λӲ
	if ((IrqStatus & XAXIDMA_IRQ_ERROR_MASK)) {

		Error = 1;
		//printf("RX Error!\n");

		//λ
		XAxiDma_Reset(&AxiDma0);
		//ȴλ
		while(!XAxiDma_ResetIsDone(&AxiDma0));
		//ʹDMARxж
		XAxiDma_IntrEnable(&AxiDma0, XAXIDMA_IRQ_ALL_MASK,XAXIDMA_DEVICE_TO_DMA);
		//һִ
		XAxiDma_SimpleTransfer(&AxiDma0,(UINTPTR) P_ADC_Data,
				ADC_DATA_LENGTH*2, XAXIDMA_DEVICE_TO_DMA);

		return;
	}

	//жϱԣRxDone1
	if ((IrqStatus & XAXIDMA_IRQ_IOC_MASK)) {

		RxDone = 1;
		//printf("RX Done!\n");
	}

	volatile uint8_t Cnt = 0;
	char Pic_Name[30];
	void PS_GPIO_IntrHandler(void *CallBackRef, uint32_t Bank, uint32_t Status)
	{
		uint32_t Key_State;
		XGpioPs *Gpio = (XGpioPs *)CallBackRef;

		//жBankĸ
		if(Bank == 1) {
			usleep(10000);	//10ms
			Key_State = XGpioPs_ReadPin(Gpio, PS_KEY);
			if(Key_State == 0) {
				//ͼSD
				sprintf(Pic_Name,"Screenshot(%02d).bmp",Cnt);
			    bmp_write(Pic_Name, (char *)&BMODE_800x480, (char *)frame);
			    Cnt++;
			}
		}
	}

}
