/**
 * \file
 *
 * \brief Declaration of main function used by HID generic example
 *
 * Copyright (c) 2014-2018 Microchip Technology Inc. and its subsidiaries.
 *
 * \asf_license_start
 *
 * \page License
 *
 * Subject to your compliance with these terms, you may use Microchip
 * software and any derivatives exclusively with Microchip products.
 * It is your responsibility to comply with third party license terms applicable
 * to your use of third party software (including open source software) that
 * may accompany Microchip software.
 *
 * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES,
 * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE,
 * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY,
 * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE
 * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL
 * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE
 * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE
 * POSSIBILITY OR THE DAMAGES ARE FORESEEABLE.  TO THE FULLEST EXTENT
 * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY
 * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
 * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
 *
 * \asf_license_stop
 *
 */
/*
 * Support and FAQ: visit <a href="https://www.microchip.com/support/">Microchip Support</a>
 */

#ifndef _MAIN_H_
#define _MAIN_H_

/*! \brief Called by HID interface
 * Callback running when USB Host enable generic interface
 *
 * \retval true if generic startup is ok
 */
bool main_generic_enable(void);

/*! \brief Called by HID interface
 * Callback running when USB Host disable generic interface
 */
void main_generic_disable(void);

/*! \brief Called when a start of frame is received on USB line
 */
void main_sof_action(void);

/*! \brief Enters the application in low power mode
 * Callback called when USB host sets USB line in suspend state
 */
void main_suspend_action(void);

/*! \brief Called by UDD when the USB line exit of suspend state
 */
void main_resume_action(void);

/*! \brief Called by UDC when USB Host request to enable remote wakeup
 */
void main_remotewakeup_enable(void);

/*! \brief Called by UDC when USB Host request to disable remote wakeup
 */
void main_remotewakeup_disable(void);

/*! \brief Called by UDI HID generic when USB Host send a feature request
 */
void main_hid_set_feature(uint8_t* report);


#define CE_1_SET_LOW	ioport_set_pin_level(CE_1_PIN, IOPORT_PIN_LEVEL_LOW)
#define CE_2_SET_LOW	ioport_set_pin_level(CE_2_PIN, IOPORT_PIN_LEVEL_LOW)
#define CE_3_SET_LOW	ioport_set_pin_level(CE_3_PIN, IOPORT_PIN_LEVEL_LOW)
#define CE_4_SET_LOW	ioport_set_pin_level(CE_4_PIN, IOPORT_PIN_LEVEL_LOW)

#define CE_1_SET_HIGH	ioport_set_pin_level(CE_1_PIN, IOPORT_PIN_LEVEL_HIGH)
#define CE_2_SET_HIGH	ioport_set_pin_level(CE_2_PIN, IOPORT_PIN_LEVEL_HIGH)
#define CE_3_SET_HIGH	ioport_set_pin_level(CE_3_PIN, IOPORT_PIN_LEVEL_HIGH)
#define CE_4_SET_HIGH	ioport_set_pin_level(CE_4_PIN, IOPORT_PIN_LEVEL_HIGH)

#define ALL_CHIP_SET_LOW	{CE_1_SET_LOW;CE_2_SET_LOW;CE_3_SET_LOW;CE_4_SET_LOW;}
#define ALL_CHIP_SET_HIGH	{CE_1_SET_HIGH;CE_2_SET_HIGH;CE_3_SET_HIGH;CE_4_SET_HIGH;}

#define TMEN_SET_LOW	ioport_set_pin_level(TMEN_PIN, IOPORT_PIN_LEVEL_LOW)
#define TMEN_SET_HIGH	ioport_set_pin_level(TMEN_PIN, IOPORT_PIN_LEVEL_HIGH)

#define TC_LATCH_SET_LOW	ioport_set_pin_level(TC_LATCH_PIN, IOPORT_PIN_LEVEL_LOW)
#define TC_LATCH_SET_HIGH	ioport_set_pin_level(TC_LATCH_PIN, IOPORT_PIN_LEVEL_HIGH)

#define WE_SET_LOW	ioport_set_pin_level(WE_PIN, IOPORT_PIN_LEVEL_LOW)
#define WE_SET_HIGH	ioport_set_pin_level(WE_PIN, IOPORT_PIN_LEVEL_HIGH)

#define ERASE_SET_LOW	ioport_set_pin_level(ERASE_PIN, IOPORT_PIN_LEVEL_LOW)
#define ERASE_SET_HIGH	ioport_set_pin_level(ERASE_PIN, IOPORT_PIN_LEVEL_HIGH)

#define ERASE_1_SET_LOW		ioport_set_pin_level(ERASE_1_PIN, IOPORT_PIN_LEVEL_LOW)
#define ERASE_1_SET_HIGH	ioport_set_pin_level(ERASE_1_PIN, IOPORT_PIN_LEVEL_HIGH)


#define PLSE_1_PIN_SET_LOW	ioport_set_pin_level(PLSE_1_PIN, IOPORT_PIN_LEVEL_LOW)
#define PLSE_1_PIN_SET_HIGH	ioport_set_pin_level(PLSE_1_PIN, IOPORT_PIN_LEVEL_HIGH)

#define PLSE_2_PIN_SET_LOW	ioport_set_pin_level(PLSE_2_PIN, IOPORT_PIN_LEVEL_LOW)
#define PLSE_2_PIN_SET_HIGH	ioport_set_pin_level(PLSE_2_PIN, IOPORT_PIN_LEVEL_HIGH)

#define IO_1_SET_LOW	ioport_set_pin_level(IO_1_PIN, IOPORT_PIN_LEVEL_LOW)
#define IO_2_SET_LOW	ioport_set_pin_level(IO_2_PIN, IOPORT_PIN_LEVEL_LOW)
#define IO_3_SET_LOW	ioport_set_pin_level(IO_3_PIN, IOPORT_PIN_LEVEL_LOW)
#define IO_4_SET_LOW	ioport_set_pin_level(IO_4_PIN, IOPORT_PIN_LEVEL_LOW)	
#define IO_5_SET_LOW	ioport_set_pin_level(IO_5_PIN, IOPORT_PIN_LEVEL_LOW)
#define IO_6_SET_LOW	ioport_set_pin_level(IO_6_PIN, IOPORT_PIN_LEVEL_LOW)
#define IO_7_SET_LOW	ioport_set_pin_level(IO_7_PIN, IOPORT_PIN_LEVEL_LOW)
#define IO_0_SET_LOW	ioport_set_pin_level(IO_0_PIN, IOPORT_PIN_LEVEL_LOW)

#define IO_1_SET_HIGH	ioport_set_pin_level(IO_1_PIN, IOPORT_PIN_LEVEL_HIGH)
#define IO_2_SET_HIGH	ioport_set_pin_level(IO_2_PIN, IOPORT_PIN_LEVEL_HIGH)
#define IO_3_SET_HIGH	ioport_set_pin_level(IO_3_PIN, IOPORT_PIN_LEVEL_HIGH)
#define IO_4_SET_HIGH	ioport_set_pin_level(IO_4_PIN, IOPORT_PIN_LEVEL_HIGH)
#define IO_5_SET_HIGH	ioport_set_pin_level(IO_5_PIN, IOPORT_PIN_LEVEL_HIGH)
#define IO_6_SET_HIGH	ioport_set_pin_level(IO_6_PIN, IOPORT_PIN_LEVEL_HIGH)
#define IO_7_SET_HIGH	ioport_set_pin_level(IO_7_PIN, IOPORT_PIN_LEVEL_HIGH)
#define IO_0_SET_HIGH	ioport_set_pin_level(IO_0_PIN, IOPORT_PIN_LEVEL_HIGH)

#define CONFEN_SET_LOW	ioport_set_pin_level(CONFEN_PIN, IOPORT_PIN_LEVEL_LOW)
#define CONFEN_SET_HIGH	ioport_set_pin_level(CONFEN_PIN, IOPORT_PIN_LEVEL_HIGH)

#define IP_CE1_SET_HIGH	ioport_set_pin_level(IP_CE1_PIN, IOPORT_PIN_LEVEL_HIGH)
#define IP_CE1_SET_LOW	ioport_set_pin_level(IP_CE1_PIN, IOPORT_PIN_LEVEL_LOW)

#define IP_CE0_SET_HIGH	ioport_set_pin_level(IP_CE0_PIN, IOPORT_PIN_LEVEL_HIGH)
#define IP_CE0_SET_LOW	ioport_set_pin_level(IP_CE0_PIN, IOPORT_PIN_LEVEL_LOW)


#define RDEN_SET_LOW	ioport_set_pin_level(RDEN_PIN, IOPORT_PIN_LEVEL_LOW)
#define RDEN_SET_HIGH	ioport_set_pin_level(RDEN_PIN, IOPORT_PIN_LEVEL_HIGH)
#define CLOCK_SET_LOW	ioport_set_pin_level(CLOCK_PIN, IOPORT_PIN_LEVEL_LOW)
#define CLOCK_SET_HIGH	ioport_set_pin_level(CLOCK_PIN, IOPORT_PIN_LEVEL_HIGH)
#define STROB0_SET_LOW	ioport_set_pin_level(STROB0_PIN, IOPORT_PIN_LEVEL_LOW)
#define STROB0_SET_HIGH	ioport_set_pin_level(STROB0_PIN, IOPORT_PIN_LEVEL_HIGH)
#define STROB1_SET_LOW	ioport_set_pin_level(STROB1_PIN, IOPORT_PIN_LEVEL_LOW)
#define STROB1_SET_HIGH	ioport_set_pin_level(STROB1_PIN, IOPORT_PIN_LEVEL_HIGH)
#define OE_SET_LOW	ioport_set_pin_level(OE_PIN, IOPORT_PIN_LEVEL_LOW)
#define OE_SET_HIGH	ioport_set_pin_level(OE_PIN, IOPORT_PIN_LEVEL_HIGH)

#define PROG_1_SET_LOW	ioport_set_pin_level(PROG_1_PIN, IOPORT_PIN_LEVEL_LOW)
#define PROG_1_SET_HIGH	ioport_set_pin_level(PROG_1_PIN, IOPORT_PIN_LEVEL_HIGH)
#define PROG_2_SET_LOW	ioport_set_pin_level(PROG_2_PIN, IOPORT_PIN_LEVEL_LOW)
#define PROG_2_SET_HIGH	ioport_set_pin_level(PROG_2_PIN, IOPORT_PIN_LEVEL_HIGH)
#define PROG_SET_LOW	ioport_set_pin_level(PROG_PIN, IOPORT_PIN_LEVEL_LOW)
#define PROG_SET_HIGH	ioport_set_pin_level(PROG_PIN, IOPORT_PIN_LEVEL_HIGH)


void PwerOnRst(void);  // FLASH POR process
void TestModeEntry(uint8_t chip);
void TestModeExit(uint8_t chip);


////////////////  offline test variable
#define ERS_HV_VALUE		0x1F   // variable in chip ERASE

#define PRGDIG_HV_VALUE		0x1F   // variable in Dig_Prog
#define ENIP_PRGDIG_VALUE	0x1F   // variable in Dig_Prog
#define ENBIAS_PRGDIG_VALUE 0x1F   // variable in Dig_Prog

#define TEST_ESF_IDX	1
#define TEST_ESF_IP_IDX	1

typedef union
{
	struct
	{
		uint8_t byte[4];
	}b;
	uint32_t u32data;
}u32_union;



#define ESF_CONTRL			20
#define VERSION_INFO_MSB    1
#define VERSION_INFO_LSB    1					
#define YEAR_INFO    20 
#define MONTH_INFO   11  
#define DATE_INFO    14 




//extern u32_union Packet_W[64];
#endif // _MAIN_H_
