/* 
 * File:   extern.h
 * Author: hamilton
 *
 * Created on January 27, 2014, 5:42 PM
 */

#include <dsp.h>
#include "config.h"
#include "can.h"
#include "park.h"
#include "svgen.h"
#include "types.h"

#ifndef EXTERN_H
#define	EXTERN_H

extern CalibrationConstants Cal;
extern const CalibrationConstants DefaultCal;

extern volatile unsigned char CalibrationMode;

extern volatile unsigned char I2C_State;
extern volatile char I2C_DeviceAddr;
extern volatile int I2C_RegisterAddr;
extern volatile unsigned char I2C_Data[EEPROM_PAGE_SIZE];
extern volatile int I2C_NumDataBytes;
extern volatile int I2C_data_ctr;

extern volatile char U1_inputBuf[U1_InputBufSize];
extern volatile unsigned int U1_BufCtr;
extern volatile unsigned int U1_StringReceived;

extern ReceivedCmdParams CmdParams[NUMBER_OF_CMDS_TO_INTERPRET];

extern volatile unsigned int SC_Range;
extern volatile unsigned int SC_RangeCtr; 
extern volatile signed int SC_Range_Max;
extern volatile signed int SC_Range_Min;
extern volatile signed int StopRange;
extern volatile signed int StopGain;
extern volatile signed int MaxRPMAdjustment;

#ifdef DEBUG
extern tPID *PIDptr;  //Pointer to PID structure for gain adjustments in UARTRxInterrupt.c
extern volatile fractional *kCoeffs;  //Pointer to kCoeffs arrah for gain adjustments in UARTRxInterrupt.c
extern int raw;
#endif
extern volatile unsigned char SPI1_DataOut[4];
extern volatile unsigned char SPI2_DataOut[4];

extern volatile fractional kCoeffs_ShuntReg[];
extern volatile fractional kCoeffs_Vel[];
extern volatile fractional kCoeffs_Id[];  //These are here for tuning purposes
extern volatile fractional kCoeffs_Iq[];  //They can go back to main.c after
extern volatile fractional kCoeffs_BattChargeILim[];
extern volatile fractional kCoeffs_BattDrawILim[];
extern volatile fractional kCoeffs_VqVdLim[];

extern volatile fractional DisableCurrent;
extern volatile fractional SlowRotateCurrent;

extern volatile fractional DistanceFromTargetCircle; //Positive Values means Vq/Vd is inside the target circle radius

extern volatile fractional BridgeDisableCurrent;
extern volatile fractional BridgeDisableRPM;

extern volatile long VoltageConstant;

extern tParkParm ParkParm;
extern tSVGenParm SVGenParm;
extern int qAngleOffset;
extern volatile int SlowRotatePosition;


extern volatile fractional CommandedCurrent;
extern volatile fractional CommandedQuadratureCurrent;
extern volatile fractional TargetQuadratureCurrent;
extern volatile fractional TargetFluxCurrent;
//extern volatile fractional BiasCurrent;

extern volatile fractional RPMRateLimit_ctsperinterpt;

extern volatile unsigned int TorqueCmdTimeout;
extern volatile unsigned int TorqueCmdTimeout_ctr;

extern volatile unsigned int BiasCmdTimeout;
extern volatile unsigned int BiasCmdTimeout_ctr;

extern volatile int ShaftRotations;

extern int TargetCircleRadius;

//The following global variables are defined in extern.c
extern volatile float f_time;

extern unsigned int BufferA[INNERFILTERSAMPLES*NUMAIO] __attribute__((space(dma),aligned(64)));
extern unsigned int BufferB[INNERFILTERSAMPLES*NUMAIO] __attribute__((space(dma),aligned(64)));

extern unsigned int Buffer2A[INNERFILTERSAMPLES*NUMAIO] __attribute__((space(dma),aligned(64)));
extern unsigned int Buffer2B[INNERFILTERSAMPLES*NUMAIO] __attribute__((space(dma),aligned(64)));

extern unsigned int SPIBufferA[2] __attribute__((space(dma)));
extern unsigned int SPIBufferB[2] __attribute__((space(dma)));


extern tPID Id_PID;
extern tPID Iq_PID;
extern tPID Vel_PID;
extern tPID ShuntReg_PID;
extern tPID BattChargeILim_PID;
extern tPID BattDrawILim_PID;
extern tPID VqVdLim_PID;

extern tGSAL Id_GSAL;
extern tGSAL Iq_GSAL;
extern tGSAL Vel_GSAL;
extern tGSAL ShuntReg_GSAL;
extern tGSAL BattChargeILim_GSAL;
extern tGSAL BattDrawILim_GSAL;
extern tGSAL VqVdLim_GSAL;

extern volatile tCRSAL UserCommandedCurrent;
extern volatile tCRSAL BiasCurrent;
extern volatile tCRSAL TargetVoltage;  
extern volatile tCRSAL MaxBattChargeCurrent;
extern volatile tCRSAL MaxBattDrawCurrent;
extern volatile tCRSAL ScaleIntegrationGain;
extern volatile tCRSAL GainScheduleRPMStdDevTarget;
extern volatile tCRSAL scale_factor;
extern volatile tCRSAL retract_factor;
extern volatile tCRSAL CAN_packet_rate;

extern unsigned int RPM_StdDev;

extern volatile fractional AbsoluteMaxTargetVoltage;
extern volatile fractional ShutdownVoltage;
extern volatile fractional MaxWindCurrent;
extern volatile fractional VoltageModification_BattCurrent;
extern volatile fractional VoltageModification_Limiter;  
extern volatile fractional IWindReduction_BattDraw_Mag;
extern volatile fractional IWindReduction_VqVd_Mag;

extern volatile fractional RPMs;
extern volatile int RPMsValid;

extern volatile POWER_CONVERTER_STATUS status;
#define Status status.word
#define StatusBitFields status.bitfields

extern volatile int SPI_OuterSampleCtr;
extern volatile int ADC_OuterSampleCtr;
extern volatile int ADC2_OuterSampleCtr;
extern volatile int ReportingFilterCtr;

//These variables contain the raw samples, possibly several per PWM update rate if INNERFILTERSAMPLES > 1
extern volatile fractional VBus_ADC[INNERFILTERSAMPLES];
extern volatile fractional IBatt_ADC[INNERFILTERSAMPLES];
extern volatile fractional DiffPress_ADC[INNERFILTERSAMPLES];
extern volatile fractional AuxTorque_ADC[INNERFILTERSAMPLES];

//These variables are filtered versions of the samples stored at the PWM rate
extern volatile fractional QuadratureCurrentSamples[OUTERFILTERSAMPLES];
extern volatile fractional FluxCurrentSamples[OUTERFILTERSAMPLES];
extern volatile fractional DiffPressSamples[OUTERFILTERSAMPLES];
extern volatile fractional AuxTorqueSamples[OUTERFILTERSAMPLES];
extern volatile fractional VBusSamples[OUTERFILTERSAMPLES];
extern volatile fractional IBattSamples[OUTERFILTERSAMPLES];
extern volatile fractional LoadDCSamples[OUTERFILTERSAMPLES];
extern volatile fractional LoadDumpCurrentSamples[OUTERFILTERSAMPLES];

//These variables are the results of the 1kHz filtering operation.
extern volatile fractional QuadratureCurrent[REPORTINGFILTERSAMPLES];  // Quadrature Current Filtered to ~1kHz
extern volatile fractional FluxCurrent[REPORTINGFILTERSAMPLES];  // Quadrature Current Filtered to ~1kHz
extern volatile fractional VBus[REPORTINGFILTERSAMPLES];  // VBus Current Filtered to ~1kHz
extern volatile fractional IBatt[REPORTINGFILTERSAMPLES];  // IBatt Current Filtered to ~1kHz
extern volatile fractional Load_DC[REPORTINGFILTERSAMPLES];  // Load Dump Duty Cycle Current Filtered to ~1kHz
extern volatile fractional RPM[REPORTINGFILTERSAMPLES];  // RPM measurement Filtered to ~1kHz (1cnt = 0.5RPM)
extern volatile fractional LoadDumpCurrent[REPORTINGFILTERSAMPLES];
extern volatile fractional DiffPress[REPORTINGFILTERSAMPLES];
extern volatile fractional AuxTorque[REPORTINGFILTERSAMPLES];

extern volatile fractional R_QuadratureCurrent;
extern volatile fractional R_VBus;
extern volatile fractional R_IBatt;
extern volatile fractional R_Load_DC;
extern volatile fractional R_RPM;
extern volatile fractional R_LoadDumpCurrent;

extern volatile fractional R_DiffPress;
extern volatile fractional R_AuxTorque;

extern volatile int halls;

extern unsigned int ECAN1MSGBUF[ECAN1_MSG_BUF_LENGTH][8] __attribute__((space(dma),aligned(ECAN1_MSG_BUF_LENGTH*16)));

extern CANmsg message[ECAN1_NUM_TX_BUFFERS-1];  //One message structure for each buffer


extern int EncoderStatus;

extern FIRStruct Inner_Filter;
extern FIRStruct Outer_Filter;
extern FIRStruct Reporting_Filter;

extern volatile signed int PhaseA_Curr;
extern volatile signed int PhaseB_Curr;
extern volatile int CurrMeasStatus;

extern volatile int iTemp1;
extern volatile int iTemp2;
extern volatile int iTemp3;
extern volatile int iTemp4;
extern volatile float fTemp1;
extern long lTemp1;
extern long lTemp2;
extern long lTemp3;
extern long lTemp4;

extern volatile unsigned long eeprom_timer;

extern ReceivedCmdParams CmdParams[NUMBER_OF_CMDS_TO_INTERPRET];

#endif	/* EXTERN_H */
