#include <dsp.h>
#include "config.h"
#include "can.h"
#include "types.h"
#include "imu.h"
#include "nortek.h"
#include "Maxon.h"

volatile unsigned int time_ctr = 0;

volatile unsigned int WindingCurrent = DEFAULT_CURR;

volatile int outSamples[FILTERSAMPLES];

volatile int temp_inSamples[FILTERSAMPLES];
volatile int temp_outSamples[FILTERSAMPLES];


int raw = 0;  //indicates if output should be raw counts or engineering units

volatile int BattVoltage_ADC[FILTERSAMPLES];
volatile int TetherVoltage_ADC[FILTERSAMPLES];
volatile int Pressure_ADC[FILTERSAMPLES];

volatile int BattVoltage_Buf;
volatile int TetherVoltage_Buf;
volatile int Pressure_Buf;

volatile long TrefoilPosition;

volatile int MeasuredHeading_Buf;

volatile unsigned long PowerLossTimout_ctr = 0;

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


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


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

volatile unsigned int power_loss_timeout =  POWERLOSS_TIMEOUT;
volatile unsigned int comms_loss_timeout =  COMMSLOSS_TIMEOUT;

volatile STATUS Status;

IMU_Packet IMU_DataA,IMU_DataB;
volatile int IMU_Buff;

volatile Maxon_Frame Maxon_FrameA;
volatile int Maxon_CommError;
volatile unsigned long CAN_PacketSend_ctr = 0;  //Counts tenths of a second in CAN ISR for RS-232 timeout purposes.  Rolls over in ~14 years.

volatile unsigned int StatusWord;
volatile signed long MotorCurrentActualValue;
volatile signed long TreFoilPositionActualValue;

volatile int CurrentPositionSet = 0;

volatile int MAXON_CmdFromCAN = 0; //Start with zero which means no command.

volatile char StateMachine = DEFAULT_STATE_MACHINE;

volatile char HomeDirection = 0;

volatile unsigned int i_ctr = 0;  //This is the character counter in the UART recieve interrupt for the Maxon, made global so it can be forced to zero before requests, not sure why it's neccesary...



#ifdef DEBUG
volatile char test_str[255];
unsigned int uiTemp1;

int iTemp1;
int iTemp2;
int iTemp3;
int iTemp4;

float  fTemp1;
float  fTemp2;

long lTemp1;


#endif
