/*
 * AutoTest.c
 *
 *  Created on: Jun 13, 2024
 *      Author: Administrator
 */
#include "TouchInclude.h"

#if AUTO_TEST
TEST_STATE TestState;
/*********************************************************************
 * @fn      EXTI0_INT_INIT
 *
 * @brief   Initializes EXTI0 collection.
 *
 * @return  none
 */
void EXTI0_INT_INIT(void)
{
    EXTI_InitTypeDef EXTI_InitStructure = {0};
    //NVIC_InitTypeDef NVIC_InitStructure = {0};

    GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource5);
    EXTI_InitStructure.EXTI_Line = EXTI_Line5;
    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Event;//EXTI_Mode_Interrupt;
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;
    EXTI_Init(&EXTI_InitStructure);
    EXTI->INTENR |= EXTI_INTENR_MR5;
//    NVIC_InitStructure.NVIC_IRQChannel = EXTI7_0_IRQn;
//    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
//    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
//    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
//    NVIC_Init(&NVIC_InitStructure);
}
AT(.com_text.gpio_isr)
void gpio_isr_callback(void)
{
    gpio_edge_pending_clear();

    printf(info);
}

void io_interrupt_init(void)
{
    gpio_edge_cap_typedef config;

    config.edge = GPIO_EDGE_FALLING;
    config.gpiox = IOKEY_PORT;
    config.gpio_pin = IOKEY_PIN;

    gpio_edge_capture_config(&config);

    gpio_edge_pic_config(gpio_isr_callback, 0);
}

void AutoTestInit(void)
{
    GPIO_InitTypeDef GPIO_InitStructure = {0};
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_InitStructure.GPIO_Pin = TEST_START_PIN;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    GPIO_SetBits(GPIOB, TEST_BUSY_PIN);
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Pin = TEST_BUSY_PIN;
    GPIO_Init(GPIOB, &GPIO_InitStructure);

    GPIO_SetBits(GPIOA, TEST_OK_PIN);
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Pin = TEST_OK_PIN;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    GPIO_SetBits(GPIOA, TEST_NG_PIN);
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Pin = TEST_NG_PIN;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    TestState = TEST_INIT;

    EXTI0_INT_INIT();
}



TEST_STATE GetTestState(void)
{
    return TestState;
}


void ReturnTestResult(BOOL rst)
{
    if(rst)//OK
    {
        GPIO_ResetBits(GPIOA, TEST_OK_PIN);
        GPIO_SetBits(GPIOA, TEST_NG_PIN);
    }
    else //NG
    {
        GPIO_SetBits(GPIOA, TEST_OK_PIN);
        GPIO_ResetBits(GPIOA, TEST_NG_PIN);
    }
    TestState = TEST_FINISH;
    GPIO_SetBits(GPIOB, TEST_BUSY_PIN);
}

void AutoTestHandle(void)
{
    if(EXTI_GetITStatus(EXTI_Line5)!=RESET)
    {
        if(TestState==TEST_INIT || TestState==TEST_FINISH)
        {
            GPIO_ResetBits(GPIOB, TEST_BUSY_PIN);
            TestState = TEST_READY;
        }

        EXTI_ClearITPendingBit(EXTI_Line5);     /* Clear Flag */
    }
}

//void EXTI7_0_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
//
///*********************************************************************
// * @fn      EXTI0_IRQHandler
// *
// * @brief   This function handles EXTI0 Handler.
// *
// * @return  none
// */
//void EXTI7_0_IRQHandler(void)
//{
//  if(EXTI_GetITStatus(EXTI_Line5)!=RESET)
//  {
//     GPIO_ResetBits(GPIOB, TEST_BUSY_PIN);
//
//     TestState = TEST_READY;
//     EXTI_ClearITPendingBit(EXTI_Line5);     /* Clear Flag */
//     //GPIO_SetBits(GPIOB, TEST_BUSY_PIN);
//  }
//}

#endif
