
#include "SPI_ADI.h"

// defines

#define RTCSEC 0x00
#define RTCMIN 0x01
#define RTCHOUR 0x02
#define RTCWKDAY 0x03
#define RTCDATE 0x04
#define RTCMTH 0x05
#define RTCYEAR 0x06
#define CONTROL 0x07
#define OSCTRIM 0x08
#define ALM0SEC 0x0A
#define ALM0MIN 0x0B
#define ALM0HOUR 0x0C
#define ALM0WKDAY 0x0D
#define ALM0DATE 0x0E
#define ALM0MTH 0x0F
#define ALM1SEC 0x11
#define ALM1MIN 0x12
#define ALM1HOUR 0x13
#define ALM1WKDAY 0x14
#define ALM1DATE 0x15
#define ALM1MTH 0x16
#define PWRDNMIN 0x18
#define PWRDNHOUR 0x19
#define PWRDNDATE 0x1A
#define PWRDNMTH 0x1B
#define PWRUPMIN 0x1C
#define PWRUPHOUR 0x1D
#define PWRUPDATE 0x1E
#define PWRUPMTH 0x1F

// variables used here

void spiInit()
{ // SPI Initialize
  int ir;
  // HSPI
  // uninitalised pointers to SPI objects
  // SPIClass * hspi = NULL;
  // static const int spiClk = 1000000; // 1 MHz

  hspi = new SPIClass(HSPI);

  hspi->begin();

  // pinMode(RTC_CS, OUTPUT); digitalWrite(RTC_CS, 1);
  //  pinMode(RLY_CS, OUTPUT); digitalWrite(RLY_CS, 1);
  //  pinMode(INPUT1_CS, OUTPUT); digitalWrite(INPUT1_CS, 1);
  //  pinMode(OUTPUT1_CS, OUTPUT); digitalWrite(OUTPUT1_CS, 1);
  // pinMode(ADC_CS, OUTPUT); digitalWrite(ADC_CS, 1);
  pinMode(IOEXP_CS, OUTPUT);
  digitalWrite(IOEXP_CS, 1);
  pinMode(RF_CS, OUTPUT);
  digitalWrite(RF_CS, 1);
  //	pinMode(CAN_CS, OUTPUT); digitalWrite(CAN_CS, 1);

  Serial.println("\r\n\r\nSPI Init\r\n\r\n");

  // rtcGetTime(); //get time
  Serial.println("SPI Init Time Complete");

  // MCP23S08 Outputs
  hspi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
  digitalWrite(IOEXP_CS, LOW); // pull SS low to prep other end for transfer
  hspi->transfer(0x40);
  hspi->transfer(0);
  hspi->transfer(IOEXP_TRIS);
  // for (ir = 0; ir < 9; ir++) hspi->transfer(0);
  // hspi->transfer(0); //all relays off
  digitalWrite(IOEXP_CS, HIGH); // pull ss high to signify end of data transfer
  hspi->endTransaction();

  /* hspi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
   digitalWrite(OUTPUT1_CS, LOW); //pull SS low to prep other end for transfer
   hspi->transfer(0x40);
   hspi->transfer(0);
   for (ir = 0; ir < 9; ir++) hspi->transfer(0);
   hspi->transfer(0); //all relays off
   digitalWrite(OUTPUT1_CS, HIGH); //pull ss high to signify end of data transfer
   hspi->endTransaction();
   Serial.println("SPI Init MCP23S08 Outputs Complete");


   //MCP23S08 Inputs
   hspi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
   digitalWrite(INPUT1_CS, LOW); //pull SS low to prep other end for transfer
   hspi->transfer(0x40);
   hspi->transfer(0);
   hspi->transfer(0xff); //set as inputs
   for (ir = 1; ir < 8; ir++) hspi->transfer(0);
   digitalWrite(INPUT1_CS, HIGH); //pull ss high to signify end of data transfer
   hspi->endTransaction();
 */
  Serial.println("SPI Init MCP2S08 Init Complete");
}

/*
long rtcGetTime(void) //sets dt struct and returns epoch
{
  int i;

  hspi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));

  digitalWrite(RTC_CS, LOW); //pull SS low to prep other end for transfer
  hspi->transfer(19); //command
  hspi->transfer(1); //address

  i = hspi->transfer(0xff) & 0x7f;
  dt.Second = i & 0x0f;
  dt.Second = ((i >> 4) * 10) + dt.Second;

  i = hspi->transfer(0xff) & 0x7f;
  dt.Minute = i & 0x0f;
  dt.Minute = ((i >> 4) * 10) + dt.Minute;

  i = hspi->transfer(0xff) & 0x3f;
  dt.Hour = i & 0x0f;
  dt.Hour = ((i >> 4) * 10) + dt.Hour;

  i = hspi->transfer(0xff) & 0x07;
  dt.Wday = i;
  //Serial.println(i & 32); //OCS running?

  //dt.Wday = ((i >> 4) * 10) + dt.Wday;

  i = hspi->transfer(0xff) & 0x3f;
  dt.Day = i & 0x0f;
  dt.Day = ((i >> 4) * 10) + dt.Day;

  i = hspi->transfer(0xff) & 0x1f;
  dt.Month = i & 0x0f;
  dt.Month = ((i >> 4) * 10) + dt.Month;

  i = hspi->transfer(0xff);
  dt.Year = i & 0x0f;
  dt.Year = ((i >> 4) * 10) + dt.Year;

  digitalWrite(RTC_CS, HIGH); //pull ss high to signify end of data transfer
  hspi->endTransaction();

  return (makeTime(dt)); //return epoch
}


void setEpochRtc(long ep) //from epoch
{
  // breakTime off by 30 years
  breakTime(ep, dt);
  //dt.Year -= 30;

  hspi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
  digitalWrite(RTC_CS, LOW); //pull SS low to prep other end for transfer

  hspi->transfer(18);
  hspi->transfer(1);
  hspi->transfer(0);
  hspi->transfer(((dt.Minute / 10) << 4)  + (dt.Minute % 10));
  hspi->transfer(((dt.Hour / 10) << 4)  + (dt.Hour % 10));  // 24 Hour format
  hspi->transfer(dt.Wday + 8); //Enable Coin Cell
  hspi->transfer(((dt.Day / 10) << 4)  + (dt.Day % 10));
  hspi->transfer(((dt.Month / 10) << 4)  + (dt.Month % 10));
  hspi->transfer(((dt.Year / 10) << 4)  + (dt.Year % 10));// two digit year
  hspi->transfer(64);  // turn on external square wave

  digitalWrite(RTC_CS, HIGH); //pull ss high to signify end of data transfer
  hspi->endTransaction();

  hspi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
  digitalWrite(RTC_CS, LOW); //pull SS low to prep other end for transfer

  hspi->transfer(18);
  hspi->transfer(1);
  hspi->transfer(128 + ((dt.Second / 10) << 4)  + (dt.Second % 10));
  digitalWrite(RTC_CS, HIGH); //pull ss high to signify end of data transfer
  hspi->endTransaction();

  if (VB)Serial.println("--RTC set ok");
}


*/
uint8_t spi_readwrite(uint8_t reg)
{
  uint8_t data;
  hspi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
  data = hspi->transfer(reg);
  hspi->endTransaction();
  return data;
}

void setMCP23S08(uint8_t cs, uint8_t reg, uint8_t dta)
{
  hspi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
  digitalWrite(cs, LOW); // pull SS low to prep other end for transfer
  hspi->transfer(0x40);  // write
  hspi->transfer(reg);
  hspi->transfer(dta);

  digitalWrite(cs, HIGH); // pull ss high to signify end of data transfer
  hspi->endTransaction();
}

uint8_t readMCP23S08(uint8_t cs, uint8_t reg)
{
  uint8_t dta;

  hspi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
  digitalWrite(cs, LOW); // pull SS low to prep other end for transfer
  hspi->transfer(0x41);  // read
  hspi->transfer(reg);
  dta = hspi->transfer(0xff);

  digitalWrite(cs, HIGH); // pull ss high to signify end of data transfer
  hspi->endTransaction();
  return dta;
}

/*
void initCC1101()
{
  hspi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
  digitalWrite(RF_CS, LOW); //pull SS low to prep other end for transfer
  hspi->transfer(2); //write
  hspi->transfer(0x2E);	// tristate GD0


  digitalWrite(RF_CS, HIGH); //pull ss high to signify end of data transfer
  hspi->endTransaction();
}
*/
