#ifndef _AIROHA_PWR_CAL_H
#define _AIROHA_PWR_CAL_H

/* 
 * MFG Calibration version number
 * NEVER CHANGE THIS
 */
#define CALIBRATION_VERSION_1    0  //OFDM attenuation 3.5 dBm
#define CALIBRATION_VERSION_2    1  //OFDM attenuation 4.86 dBm
#define CALIBRATION_VERSION_3    2  //New method to calibrate
extern int getPowerCalibrationVersion (void);
extern void setAttenuation( unsigned char value);


/* This is the high and low power point for manufacture calibration */
#define LOW_POINT_AL2236_CCK     10         //dbm
#define HIGH_POINT_AL2236_CCK    17         //dbm
#define LOW_POINT_AL2236_OFDM     LOW_POINT_AL2236_CCK         //dbm
#define HIGH_POINT_AL2236_OFDM   12         //dbm

#if (PLATFORM == connectwiem9210)
#define LOW_POINT_AL7230_BG_CCK      8         //dbm
#define HIGH_POINT_AL7230_BG_CCK    17        //dbm
#define LOW_POINT_AL7230_BG_OFDM     LOW_POINT_AL7230_BG_CCK         //dbm
#define HIGH_POINT_AL7230_BG_OFDM   12        //dbm
#define LOW_POINT_AL7230_A      4         //dbm
#define HIGH_POINT_AL7230_A     10         //dbm
#elif (PLATFORM == connectcorewi9p9215_a || PLATFORM == heavy /* NDS */ || PLATFORM == w9p /* NDS */) 
#define LOW_POINT_AL7230_BG_CCK       8         //dbm
#define HIGH_POINT_AL7230_BG_CCK     17        //dbm
#define LOW_POINT_AL7230_BG_OFDM      LOW_POINT_AL7230_BG_CCK         //dbm
#define HIGH_POINT_AL7230_BG_OFDM    12        //dbm
#define LOW_POINT_AL7230_A      4         //dbm
#define HIGH_POINT_AL7230_A     8         //dbm
#else
/* other possible platforms in the future */
#define LOW_POINT_AL7230_BG_CCK       8         //dbm
#define HIGH_POINT_AL7230_BG_CCK     17        //dbm
#define LOW_POINT_AL7230_BG_OFDM      LOW_POINT_AL7230_BG_CCK         //dbm
#define HIGH_POINT_AL7230_BG_OFDM    12        //dbm
#define LOW_POINT_AL7230_A      4         //dbm
#define HIGH_POINT_AL7230_A     10         //dbm
#endif

/* the calibration line/curve number 
 * NEVER CHANGE THIS
 */
#define CAL_BG_BAND_CCK_ONLY        0
#define CAL_BG_BAND_CCK_OFDM_DIFF   1
#define CAL_A_BAND_SUBBAND1         2 
#define CAL_A_BAND_SUBBAND2         3 
#define CAL_A_BAND_SUBBAND3         4 
#define CAL_A_BAND_SUBBAND4         5 
#define CAL_A_BAND_SUBBAND5         6 
#define CAL_BG_BAND_OFDM_ONLY       7 
#define CAL_BG_BAND_CCK_ONLY_EXT        8          //extension
#define CAL_BG_BAND_OFDM_ONLY_EXT       9          //extension   
#define CAL_A_BAND_SUBBAND1_EXT         10         //extension 
#define CAL_A_BAND_SUBBAND2_EXT         11         //extension 
#define CAL_A_BAND_SUBBAND3_EXT         12         //extension 
#define CAL_A_BAND_SUBBAND4_EXT         13         //extension 
#define CAL_A_BAND_SUBBAND5_EXT         14         //extension 
#define CAL_VERSION                     15         //version  


//#define AIROHA_PWR_CALIBRATION_DEBUG

#define WLN_POWER_CALIBRATION_LENGTH (64)


/*
 * To keep backward compatible with the mfg calibration data
 * (peak reading  - PEAK_ADJUSTMENT)
 */
#define PEAK_ADJUSTMENT_CCK    6
#define PEAK_ADJUSTMENT_OFDM   3



// Note: Bit 10 is set for RF Pwr gain to be set by register
#define AIROHA_PWR_SETTING_AL2236  0x054409  //AL2236
#define AIROHA_PWR_SETTING_AL7230  0x08040B  //Al7230

#define AIROHA_PWR_CONTROL

extern int AIROHA_PWR_SETTING;

// Power control loop gain factor for customer index's 0 - 15
#ifndef _WIFI_PWR_GAIN
#define _WIFI_PWR_GAIN     1.14 // Set for PSK_CCK data transmissions
#endif

// Power control loop Airoha index range for setting initial power level
#ifndef _WIFI_MAX_AIROHA_INDEX
   #ifndef WIFI_ENGINEERING_DEBUG
      #define _WIFI_MAX_AIROHA_INDEX 38   // -9dBm down from Max range of 63
   #else
      #define _WIFI_MAX_AIROHA_INDEX 63
   #endif
#endif

// Cable loss for Debug display
#ifndef _WIFI_CABLE_LOSS
#define _WIFI_CABLE_LOSS  0.5
#endif

// Pwr accept range when initial setting of the RF power
#ifndef _WIFI_PWR_ACCEPT1_LOW
#define _WIFI_PWR_ACCEPT1_LOW  0.55
#endif
#ifndef _WIFI_PWR_ACCEPT1_HIGH
#define _WIFI_PWR_ACCEPT1_HIGH  0.05
#endif

// Pwr accept range after setting the RF power
#ifndef _WIFI_PWR_ACCEPT2_LOW  
#define _WIFI_PWR_ACCEPT2_LOW   0.85
#endif
#ifndef _WIFI_PWR_ACCEPT2_HIGH  
#define _WIFI_PWR_ACCEPT2_HIGH   0.05
#endif

// Typical Airoha Power Resolution for changing of RF_Index
#ifndef _WIFI_PWR_RESOLUTION
#define _WIFI_PWR_RESOLUTION  0.6
#endif

// Power control loop power thresold trigger (dBm)
#define _WIFI_PWR_THRESHOLD   1.0


// ADC value where the PDOut power is readable
#define _WIFI_ADC_THRESHOLD    0x50


// ADC value where the PDOut power is readable
#define _WIFI_ADC_RANGE_LIMIT    4



// ADC averaging cycle control
#ifndef _WIFI_ADC_AVG_COUNT
#define _WIFI_ADC_AVG_COUNT   3
#endif

// Power Control Loop interval trigger, default is set for 15 second
#ifndef _WIFI_PWR_CAL_PERIOD
#define _WIFI_PWR_CAL_PERIOD   15
#endif

// Set timeout for 5 seconds
#ifndef _WIFI_PWR_CAL_TIMEOUT
#define _WIFI_PWR_CAL_TIMEOUT  5
#endif

#ifndef _WIFI_PWR_CAL_DEBOUNCE
#define _WIFI_PWR_CAL_DEBOUNCE  2  /* 2 ms in NETOS */
#endif

// Set inactivity timeout for 1 hour
#ifndef _WIFI_INACTIVITY_PERIOD
#define _WIFI_INACTIVITY_PERIOD   (60*60)
#endif

// TX_START interrupt status bit fields
#define _TX_START_DISABLE   0x00
#define _TX_START_ENABLE    0x01
#define _TX_MODULATION_BIT  0x02
#define _TX_INTERRUPT_BIT   0x04

// Modulation identifier
#define _WIFI_PSK_CCK    0
#define _WIFI_OFDM       1


// ADC discount averaging Alpha value
// fix wireless power recovery from low initial average ADC reading,
// which would never average up if it occurred after the initial average calculation
// because the discounting rate was set way too low; also causes infinite APC
// FSM loop; now uses 33% as the discount rate
#ifndef _WIFI_DISCOUNT_ALPHA
#define _WIFI_DISCOUNT_ALPHA  0.333
#endif

// Spi port settings
#define _WIFI_SF_SPI_CLOCKMODE 3
#define _WIFI_SF_SPI_DIVISOR 0
#define _WIFI_SF_SPI_CONTROL_VALUE 0x0C

// Serial port register offsets
#define _WIFI_SF_SR_OFFSET 3
#define _WIFI_SF_CR_OFFSET 4
#define _WIFI_SF_ER_OFFSET 5

// Serial port register
#define _WIFI_SF_SERPORT_TR 5
#define _WIFI_SF_SERPORT_TR_REG TAT5R
#define _WIFI_SF_SERPORT_TR_SHADOW TAT5RShadow


// Airoha Pwr control states
enum {
        WIFI_APC_START,           // APC state to set timeout and begin APC control
   WIFI_APC_ADC_LOOP,     // APC state to initialize state machine values
   WIFI_APC_ADC_AVG,      // APC state to validate ADC readings
   WIFI_APC_ADJUST_PWR,   // APC Airoha Power Adjustment/Limit check state
   WIFI_APC_DEBOUNCE,     // APC state for debouncing between iterations
   WIFI_APC_COMPLETE,     // APC completed state, sets up next interval period
   WIFI_APC_INTERVAL_TRIGGER, // APC state to check when to kick-off next cycle
};

#ifndef __WIFI_MAC_WEP_LIB
//#use "wifi_mac_wep.lib"       // Needed for CRC routine
#endif

typedef struct
{
   float slope;      // Calibration slope and intercept coefficients
   float intercept;
   long maxIndex;     // Max Airoha Index used for manufacturing calibration
   unsigned long crc;          // CRC
} WiFiPwrCal;

typedef struct {
        int  CpuFullSpeed;
   int  State;
   int  AvgData;
   int  AvgCycles;
   int  AvgHistory;
   int  AirohaIndex;
   float  CustIndex;
   int  Modulation;
   int  Enable;
   int  Interrupt;
   int  TargetIndex;
   int  CalIndex;
   int  ForceADCRead;
   int  TrackUpDown;
   int  NewPwrLevel;
   unsigned char Rawdata;
   unsigned char RawTemp;
   unsigned long Inactivity;
   unsigned long CalTimeout;
   unsigned long Debounce;
#ifdef AIROHA_PWR_CALIBRATION_DEBUG   
   int txStartCount;
   int txStillOn;
   int history[10];
   int ii;
   int ofdmCount;
   int pskCount;
   int avgDone1;
   int avgTry;
   int avgDone2;
   int avgTry2;
   int avgTryTotal;
   int readingPeakTxStart;
   int readingPeakTxDone;
   int goodReading;
#endif
} WifiPwrCntrl;

extern WifiPwrCntrl _wapc;

//extern struct _wifi_freq_divider const _wifi_freqTable[];
typedef struct {
	int dbm;
	float mW;
} dbmToMwTranslationTable_T;



extern int _wifi_init_adc(void);
extern int  _wifi_read_adc(void);
extern void _wifi_PwrAdjust(void);
extern void _wifi_pwrCntrl(void);
extern int _wifi_readadc(void);
extern int  _wifi_read_raw(void);
extern int _wifi_RFCalCRC(void);
extern void __wifi_SetPower (int value);

extern void initPwrCal(void);
extern void WriteRF (uint8 addr, uint32 data);
extern void enableTxStartInt (BOOLEAN on);
extern BOOLEAN isContTXOn (void);
extern BOOLEAN isContOFDM (void);
extern BOOLEAN isBeaconTestOn (void);
extern void _wifi_Airoha (unsigned long value);


#define CAL_TIMEOUT_EVENT       0x01
#define INACTIVE_TIMEOUT_EVENT  0x02
#define DEBOUNCE_TIMEOUT_EVENT  0x04
#define TX_START_EVENT       	0x08
#define TX_DONE_EVENT       	0x10
#define MAC_SEND_FRAME_EVENT    0x20
TX_EVENT_FLAGS_GROUP airoha_pwr_cal_group;

extern int setPowerCalibrationData(WiFiPwrCal*  wcal);
extern int getPowerCalibrationData(WiFiPwrCal*  wcal);
extern int getCalibrationFormattedData(char *buf, int *bufLen);
extern int setCalibrationFormattedData(char *buf, int bufLen);
extern BOOLEAN isRF_AIROHA_7230(void);

#if MANUFACTURING_MODE
extern void suspendPowerControl(void);
extern void resumePowerControl(void);
#endif

#endif //_AIROHA_PWR_CAL_H
