#ifndef _SYSMGR_H_
#define _SYSMGR_H_

#include "nrf_drv_spi.h"
#include "app_util_platform.h"
#include "nrf_gpio.h"
#include "nrf_delay.h"
#include "boards.h"
#include "app_error.h"
#include <string.h>
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"

#include "ble_gap.h"

#include "fds.h"
#include "nrf_fstorage.h"



struct stSystemTimer__ {
	volatile int MS_1;
        volatile int UART_LAST_RECV_TIMER;
        volatile int SEND_TEST_TIMER;
        volatile int LED_TOGGLE_TIMER;
        volatile int TMR_SYS_OFF;
        volatile int SEND_TIMER;
        volatile int BUZZER_TIMER;
        char EMG_SEND_RUN;
		volatile int TIM_KEY;
		volatile int SLEEP_TIMER;
};
typedef struct stSystemTimer__ stSystemTimer;




struct stSystemManager__ {

  char EmgSendButton;
  char TestSendButton;
  char RxEvent;
  char SendRepeat;
  char SendFlag;
  int  BleConnectTime;
  int  SendRepeatTimerLimit;
  char BuzzerType;
  char BuzzerBusy;
  unsigned char Batt;

  char BleSendReady;
  char BleSendStatus;


  char BleConnected;
  char BleSendRetry;
  char PeerAddr[6];
  char StrPeerAddr[15];
  char RegPeerAddr[6];
  char StrRegPeerAddr[15];



  char SysReady;
  char SleepEnter;

  char FWUpdate;
  
  char GpReg;
  char GoSleep;  
  uint32_t ResetReason;


	char AesEncData[16];


};
typedef struct stSystemManager__ stSystemManager;




#define     SC16IS750_ADDRESS_AA     (0X90)
#define     SC16IS750_ADDRESS_AB     (0X92)
#define     SC16IS750_ADDRESS_AC     (0X94)
#define     SC16IS750_ADDRESS_AD     (0X96)
#define     SC16IS750_ADDRESS_BA     (0X98)
#define     SC16IS750_ADDRESS_BB     (0X9A)
#define     SC16IS750_ADDRESS_BC     (0X9C)
#define     SC16IS750_ADDRESS_BD     (0X9E)
#define     SC16IS750_ADDRESS_CA     (0XA0)
#define     SC16IS750_ADDRESS_CB     (0XA2)
#define     SC16IS750_ADDRESS_CC     (0XA4)
#define     SC16IS750_ADDRESS_CD     (0XA6)
#define     SC16IS750_ADDRESS_DA     (0XA8)
#define     SC16IS750_ADDRESS_DB     (0XAA)
#define     SC16IS750_ADDRESS_DC     (0XAC)
#define     SC16IS750_ADDRESS_DD     (0XAE)


//General Registers
#define     SC16IS750_REG_RHR        (0x00)
#define     SC16IS750_REG_THR        (0X00)
#define     SC16IS750_REG_IER        (0X01)
#define     SC16IS750_REG_FCR        (0X02)
#define     SC16IS750_REG_IIR        (0X02)
#define     SC16IS750_REG_LCR        (0X03)
#define     SC16IS750_REG_MCR        (0X04)
#define     SC16IS750_REG_LSR        (0X05)
#define     SC16IS750_REG_MSR        (0X06)
#define     SC16IS750_REG_SPR        (0X07)
#define     SC16IS750_REG_TCR        (0X06)
#define     SC16IS750_REG_TLR        (0X07)
#define     SC16IS750_REG_TXLVL      (0X08)
#define     SC16IS750_REG_RXLVL      (0X09)
#define     SC16IS750_REG_IODIR      (0X0A)
#define     SC16IS750_REG_IOSTATE    (0X0B)
#define     SC16IS750_REG_IOINTENA   (0X0C)
#define     SC16IS750_REG_IOCONTROL  (0X0E)
#define     SC16IS750_REG_EFCR       (0X0F)

//Special Registers
#define     SC16IS750_REG_DLL        (0x00)
#define     SC16IS750_REG_DLH        (0X01)

//Enhanced Registers
#define     SC16IS750_REG_EFR        (0X02)
#define     SC16IS750_REG_XON1       (0X04)
#define     SC16IS750_REG_XON2       (0X05)
#define     SC16IS750_REG_XOFF1      (0X06)
#define     SC16IS750_REG_XOFF2      (0X07)

//
#define     SC16IS750_INT_CTS        (0X80)
#define     SC16IS750_INT_RTS        (0X40)
#define     SC16IS750_INT_XOFF       (0X20)
#define     SC16IS750_INT_SLEEP      (0X10)
#define     SC16IS750_INT_MODEM      (0X08)
#define     SC16IS750_INT_LINE       (0X04)
#define     SC16IS750_INT_THR        (0X02)
#define     SC16IS750_INT_RHR        (0X01)

//Application Related 

#define     SC16IS750_CRYSTCAL_FREQ (14745600UL) 
//#define 	SC16IS750_CRYSTCAL_FREQ (1843200UL)	  
//#define     SC16IS750_CRYSTCAL_FREQ (16000000UL)    
//#define     SC16IS750_DEBUG_PRINT   (0)
#define     SC16IS750_PROTOCOL_I2C  (0)
#define     SC16IS750_PROTOCOL_SPI  (1)






#define AES_ENC_CAR		1
#define AES_ENC_EMG		2
#define AES_ENC_PCA		3
#define AES_ENC_WES_EMG	4
#define AES_ENC_WES_ALV	5
#define AES_ENC_WES_TST	6
#define AES_ENC_WES_REG	7


/* A dummy structure to save in flash. */

#define CONFIG_FILE     (0x8010)
#define CONFIG_REC_KEY  (0x7010)


typedef struct
{
    uint32_t boot_count;
    char     device_name[16];
    bool     config1_on;
    bool     config2_on;
} configuration_t;



typedef struct
{
    char Mac[10][15];	
    int Count;
}RegMacPool_t;




void SC16IS750_ResetDevice(void);
void SC16IS750_WriteRegister(unsigned char reg_addr, unsigned char val);
unsigned char SC16IS750_ReadRegister(unsigned char reg_addr);
void SC16IS750_FIFOEnable(unsigned char fifo_enable);
int16_t SC16IS750_SetBaudrate(uint32_t baudrate);
void SC16IS750_SetLine(uint8_t data_length, uint8_t parity_select, uint8_t stop_length );
void SC16IS750_WriteByte(uint8_t val);
int DBGPrint(const char *fmt, ...);


void ParkSysInit();
void ParkPowerOff();

void SendEmgData(void);
extern stSystemTimer SystemTimer;
extern stSystemManager SystemManager;







#define KEY_SELECT		0
#define KEY_UP			1
#define KEY_DOWN		3
#define KEY_BACK		2




typedef struct tsHSKey{
	unsigned char onFlag;
	unsigned char softRelease;
	unsigned char pressedSec;
	unsigned int  pressTime;
	unsigned int  longPressTimeOne;
	unsigned int  longPressTimeKeep;
	unsigned int  prevTime;


}HSKey;






void ReadKEY(void);
void KeyInit(void);




void KeySelectLongPressedOne(void);


void m_standby_counter_clerar();
void PortInputCfg(void);


void lfclk_config(void);
void rtc_config(void);

void SaveReceiverMac(char* mac);
void LoadReceiverMac();
void FlashTest();





void WesEncryptEcb(uint8_t* in);



extern ble_gap_addr_t old_ble_addr;
extern ble_gap_addr_t new_ble_addr;

extern RegMacPool_t RegMacPool;

#endif
