573 lines
12 KiB
C++
573 lines
12 KiB
C++
#include <SPI.h>
|
|
|
|
|
|
|
|
|
|
#define iebusIRQ 2
|
|
#define RAW 0x4
|
|
#define isRAW (READFLG&RAW)
|
|
#define isMARQ (READFLG&0x40)
|
|
|
|
|
|
#define REG_WRITE_CTR bitchange(0x0)
|
|
#define REG_WRITE_CMR bitchange(0x1)
|
|
#define REG_WRITE_UAR1 bitchange(0x2)
|
|
#define REG_WRITE_UAR2 bitchange(0x3)
|
|
#define REG_WRITE_SAR1 bitchange(0x4)
|
|
#define REG_WRITE_SAR2 bitchange(0x5)
|
|
#define REG_WRITE_MCR bitchange(0x6)
|
|
#define REG_WRITE_TBF bitchange(0xe)
|
|
|
|
|
|
#define REG_READ_STR (bitchange(0x0)|READ)
|
|
#define REG_READ_FLG (bitchange(0x1)|READ)
|
|
#define REG_READ_RDR1 (bitchange(0x2)|READ)
|
|
#define REG_READ_RDR2 (bitchange(0x3)|READ)
|
|
#define REG_READ_LOR1 (bitchange(0x4)|READ)
|
|
#define REG_READ_LOR2 (bitchange(0x5)|READ)
|
|
#define REG_READ_DAR1 (bitchange(0x6)|READ)
|
|
#define REG_READ_DAR2 (bitchange(0x7)|READ)
|
|
#define REG_READ_RCR (bitchange(0x8)|READ)
|
|
#define REG_READ_RBF (bitchange(0xe)|READ)
|
|
|
|
|
|
#define LOW_ADDR 0x00
|
|
#define HIGH_ADDR 0x36
|
|
|
|
#define MODE 8
|
|
#define CONTROL 9
|
|
#define READ 0x08
|
|
#define RESET 7
|
|
|
|
#define DATASIZE 20
|
|
|
|
|
|
|
|
|
|
|
|
void iebus_init();
|
|
void flaginit();
|
|
void iebus_transmission();
|
|
void iebus_reception();
|
|
void iebus_mastercomm();
|
|
void iebus_slavetransmission();
|
|
void iebus_command();
|
|
int checkMARC();
|
|
void iebus_irq();
|
|
void iebus_loop();
|
|
void dataWrite(char reg_addr, char *data, int datasize);
|
|
int dataRead(char reg_addr, char *data);
|
|
byte dataRead1byte(char addr);
|
|
void dataWrite1byte(char addr , byte data);
|
|
|
|
byte READFLG;
|
|
char data[DATASIZE] = {0x0,};
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
struct _communicationFlag
|
|
{
|
|
bool RAWF;
|
|
bool TRRQ;
|
|
|
|
byte I;
|
|
bool RERQ;
|
|
byte RECF;
|
|
byte SIZE;
|
|
byte PW;
|
|
byte PR;
|
|
byte J;
|
|
bool MCRQ;
|
|
bool SDRQ;
|
|
bool CORQ;
|
|
bool MTRQF;
|
|
bool MRRQF;
|
|
bool STRQF;
|
|
bool SLREF;
|
|
byte TRCF;
|
|
} commFlag;
|
|
|
|
|
|
|
|
|
|
|
|
byte bitchange(byte b)
|
|
{
|
|
byte result = 0;
|
|
|
|
//
|
|
// // Serial.print("TEST:0b");
|
|
// // Serial.print(b,BIN);
|
|
////
|
|
//// //result=(bitRead(b,3))|(bitRead(b,2)<<1) |(bitRead(b,1)<<2)|(bitRead(b,0)<<3);
|
|
//if(bitRead(b,0))
|
|
//{
|
|
// result|=0x8;
|
|
//}
|
|
//
|
|
//if(bitRead(b,1))
|
|
//{
|
|
// result|=0x4;
|
|
//}
|
|
//
|
|
//if(bitRead(b,2))
|
|
//{
|
|
// result|=0x2;
|
|
//}
|
|
//
|
|
//if(bitRead(b,3))
|
|
//{
|
|
// result|=0x1;
|
|
//}
|
|
// // | (bitRead(b,2) <<1 )| (bitRead(b,3) <<0)
|
|
// return result;
|
|
|
|
return b<<4;
|
|
}
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
void setup (void) {
|
|
|
|
|
|
Serial.begin(9600); //set baud rate to 115200 for usart
|
|
|
|
SPI.begin ();
|
|
SPI.setClockDivider(SPI_CLOCK_DIV128);//divide the clock by 8
|
|
SPI.setBitOrder(MSBFIRST);
|
|
SPI.setDataMode(SPI_MODE0);
|
|
// SPI.beginTransaction(SPISettings(1000, MSBFIRST, SPI_MODE0));
|
|
|
|
|
|
pinMode(MODE, OUTPUT);
|
|
pinMode(CONTROL, OUTPUT);
|
|
|
|
pinMode(RESET, OUTPUT);
|
|
|
|
digitalWrite(MODE, HIGH);
|
|
digitalWrite(CONTROL, HIGH);
|
|
|
|
|
|
digitalWrite(RESET, HIGH);
|
|
digitalWrite(SS, LOW);
|
|
|
|
iebus_init();
|
|
|
|
}
|
|
|
|
|
|
|
|
byte reverse_bits(byte Mitutoyo) {
|
|
byte rtn = 0;
|
|
for (byte i=0; i<4; i++) {
|
|
bitWrite(rtn, 3-i, bitRead(Mitutoyo, i));
|
|
}
|
|
return rtn;
|
|
}
|
|
|
|
|
|
|
|
void iebus_init()
|
|
{
|
|
digitalWrite(RESET, LOW);
|
|
delay(10);
|
|
digitalWrite(RESET, HIGH);
|
|
delay(10);
|
|
|
|
|
|
|
|
// dataWrite1byte(REG_WRITE_UAR1, LOW_ADDR << 4);
|
|
// delay(500);
|
|
//
|
|
// dataWrite1byte(REG_WRITE_UAR2, HIGH_ADDR);
|
|
//
|
|
//
|
|
// delay(500);
|
|
//
|
|
//
|
|
//
|
|
// dataWrite1byte(REG_WRITE_CTR, B00000000);
|
|
// delay(500);
|
|
//
|
|
//
|
|
// dataWrite1byte(REG_WRITE_CMR, B01000000);
|
|
// delay(2000);
|
|
|
|
|
|
|
|
|
|
{
|
|
Serial.print("REG_READ_STR[0b");
|
|
Serial.print(REG_READ_STR,BIN);
|
|
Serial.print("]");
|
|
Serial.print("REG_READ_STR: 0x");
|
|
byte tmp = dataRead1byte(REG_READ_STR);
|
|
Serial.println(tmp,BIN);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
Serial.print("REG_READ_FLG[0b");
|
|
Serial.print(REG_READ_FLG,BIN);
|
|
Serial.print("]");
|
|
Serial.print("REG_READ_FLG: 0x");
|
|
byte tmp = dataRead1byte(REG_READ_FLG);
|
|
Serial.println(tmp,BIN);
|
|
}
|
|
|
|
|
|
{
|
|
Serial.print("REG_READ_LOR[0b");
|
|
Serial.print(REG_READ_LOR2,BIN);
|
|
Serial.print("]");
|
|
Serial.print("REG_READ_LOR2: 0x");
|
|
byte tmp = dataRead1byte(REG_READ_LOR2);
|
|
Serial.println(tmp,BIN);
|
|
}
|
|
|
|
|
|
{
|
|
Serial.print("REG_READ_RCR[0b");
|
|
Serial.print(REG_READ_RCR,BIN);
|
|
Serial.print("]");
|
|
Serial.print("REG_READ_RCR: 0x");
|
|
byte tmp = dataRead1byte(REG_READ_RCR);
|
|
Serial.println(tmp,BIN);
|
|
}
|
|
|
|
Serial.println("=====================================================");
|
|
dataWrite1byte(REG_WRITE_CTR,0b00000001); //Enter stanby mode
|
|
delay(100);
|
|
dataWrite1byte(REG_WRITE_CTR,0b00000000); //Exits from standby mode
|
|
delay(100);
|
|
dataWrite1byte(REG_WRITE_CTR,0b00010000); //set REENs
|
|
|
|
|
|
delay(100);
|
|
dataWrite1byte(REG_WRITE_UAR1,0x37); //UAR1
|
|
|
|
delay(100);
|
|
dataWrite1byte(REG_WRITE_UAR2,0x00); //UAR2
|
|
|
|
|
|
delay(100);
|
|
dataWrite1byte(REG_WRITE_CMR,0b01000000); //Lock
|
|
delay(100);
|
|
{
|
|
Serial.print("REG_READ_STR[0b");
|
|
Serial.print(REG_READ_STR,BIN);
|
|
Serial.print("]");
|
|
Serial.print("REG_READ_STR: 0x");
|
|
byte tmp = dataRead1byte(REG_READ_STR);
|
|
Serial.println(tmp,BIN);
|
|
}
|
|
{
|
|
Serial.print("REG_READ_FLG[0b");
|
|
Serial.print(REG_READ_FLG,BIN);
|
|
Serial.print("]");
|
|
Serial.print("REG_READ_FLG: 0x");
|
|
byte tmp = dataRead1byte(REG_READ_FLG);
|
|
Serial.println(tmp,BIN);
|
|
}
|
|
{
|
|
Serial.print("REG_READ_LOR1[0b");
|
|
Serial.print(REG_READ_LOR1,BIN);
|
|
Serial.print("]");
|
|
Serial.print("REG_READ_LOR1: 0x");
|
|
byte tmp = dataRead1byte(REG_READ_LOR1);
|
|
Serial.println(tmp,BIN);
|
|
}
|
|
{
|
|
Serial.print("REG_READ_LOR2[0b");
|
|
Serial.print(REG_READ_LOR2,BIN);
|
|
Serial.print("]");
|
|
Serial.print("REG_READ_LOR2: 0x");
|
|
byte tmp = dataRead1byte(REG_READ_LOR2);
|
|
Serial.println(tmp,BIN);
|
|
}
|
|
{
|
|
Serial.print("REG_READ_RCR[0b");
|
|
Serial.print(REG_READ_RCR,BIN);
|
|
Serial.print("]");
|
|
Serial.print("REG_READ_RCR: 0x");
|
|
byte tmp = dataRead1byte(REG_READ_RCR);
|
|
Serial.println(tmp,BIN);
|
|
}
|
|
|
|
|
|
//pinMode(iebusIRQ, INPUT_PULLUP);
|
|
// attachInterrupt(digitalPinToInterrupt(iebusIRQ), iebus_irq, RISING);
|
|
|
|
flaginit();
|
|
}
|
|
void flaginit()
|
|
{
|
|
commFlag.RAWF = 0;
|
|
commFlag.TRRQ = 0;
|
|
commFlag.RERQ = 0;
|
|
commFlag.SIZE = 0;
|
|
commFlag.J = 1;
|
|
commFlag.PW = 0;
|
|
commFlag.PR = 0;
|
|
commFlag.MCRQ = 0;
|
|
commFlag.SDRQ = 0;
|
|
commFlag.CORQ = 0;
|
|
commFlag.MTRQF = 0;
|
|
commFlag.MRRQF = 0;
|
|
commFlag.STRQF = 0;
|
|
commFlag.SLREF = 0;
|
|
|
|
|
|
}
|
|
|
|
void iebus_transmission()
|
|
{
|
|
|
|
}
|
|
|
|
|
|
void iebus_reception()
|
|
{
|
|
|
|
}
|
|
void iebus_mastercomm()
|
|
{
|
|
|
|
}
|
|
void iebus_slavetransmission()
|
|
{
|
|
|
|
}
|
|
|
|
|
|
void iebus_command()
|
|
{
|
|
while((READFLG = dataRead1byte(REG_READ_FLG))&0x8);
|
|
dataWrite1byte(REG_WRITE_CMR, 0b00010000);
|
|
|
|
//CMR command code
|
|
|
|
}
|
|
|
|
|
|
|
|
void iebus_irq()
|
|
{
|
|
noInterrupts();
|
|
|
|
|
|
interrupts();
|
|
}
|
|
|
|
|
|
void iebus_irq2()
|
|
{
|
|
byte registerRCR;
|
|
noInterrupts();
|
|
READFLG = dataRead1byte(REG_READ_FLG);
|
|
if (isRAW) // Program Crash?
|
|
{
|
|
commFlag.RAWF = 1;
|
|
}
|
|
else
|
|
{
|
|
//Read RCR
|
|
registerRCR = dataRead1byte(REG_READ_RCR);
|
|
byte MARC = (registerRCR>>4)&0xf;
|
|
byte SLRC = registerRCR&0x0f;
|
|
|
|
//is return MARC Enabled
|
|
if(bitRead(MARC,3)==0&&bitRead(MARC,2)==0)
|
|
{
|
|
//00xx
|
|
commFlag.TRCF=MARC;
|
|
commFlag.TRRQ=true;
|
|
|
|
}
|
|
else if(bitRead(MARC,3)==0&&bitRead(MARC,2)==1 &&bitRead(MARC,1)==0)
|
|
{
|
|
////010x
|
|
commFlag.RERQ=true;
|
|
commFlag.SIZE=dataRead1byte(REG_READ_RDR1);
|
|
commFlag.RECF = MARC;
|
|
|
|
}
|
|
|
|
else if(bitRead(MARC,3)==0&&bitRead(MARC,2)==1 &&bitRead(MARC,1)==1)
|
|
{
|
|
//011x
|
|
commFlag.RERQ=true;
|
|
commFlag.SIZE=dataRead1byte(REG_READ_RDR1);
|
|
commFlag.RECF = MARC;
|
|
commFlag.PW++;
|
|
|
|
commFlag.RERQ=false;
|
|
commFlag.SIZE=0;
|
|
}
|
|
|
|
else
|
|
{
|
|
Serial.print("MARC Disabled");
|
|
|
|
}
|
|
|
|
|
|
//is return SLRC Enabled
|
|
if(bitRead(SLRC,3)==0&&bitRead(SLRC,2)==0)
|
|
{
|
|
//00xx
|
|
commFlag.TRCF=SLRC;
|
|
commFlag.TRRQ=true;
|
|
|
|
}
|
|
|
|
else if((bitRead(SLRC,3)==0&&bitRead(SLRC,2)==1 &&bitRead(SLRC,1)==0)|(bitRead(SLRC,3)==1&&bitRead(SLRC,2)==0 &&bitRead(SLRC,1)==0))
|
|
{
|
|
//010x, 100x
|
|
commFlag.RERQ=true;
|
|
commFlag.SIZE=dataRead1byte(REG_READ_RDR2);
|
|
commFlag.RECF = SLRC;
|
|
|
|
}
|
|
else if((bitRead(SLRC,3)==0&&bitRead(SLRC,2)==1 &&bitRead(SLRC,1)==1)|(bitRead(SLRC,3)==1&&bitRead(SLRC,2)==0 &&bitRead(SLRC,1)==1))
|
|
{
|
|
//011x
|
|
commFlag.RERQ=true;
|
|
commFlag.SIZE=dataRead1byte(REG_READ_RDR2);
|
|
commFlag.RECF = SLRC;
|
|
//REEN 1
|
|
dataWrite1byte(REG_WRITE_CTR, 0b00010000);
|
|
|
|
commFlag.RERQ=false;
|
|
commFlag.SIZE=0;
|
|
}
|
|
|
|
|
|
}
|
|
interrupts();
|
|
}
|
|
|
|
void iebus_loop()
|
|
{
|
|
|
|
if (commFlag.RAWF == 1)
|
|
{
|
|
iebus_init();
|
|
|
|
}
|
|
|
|
if (commFlag.TRRQ == 1)
|
|
{
|
|
iebus_transmission();
|
|
commFlag.TRRQ = 0;
|
|
}
|
|
if (commFlag.RERQ == 1)
|
|
{
|
|
iebus_reception();
|
|
commFlag.RERQ = 0;
|
|
}
|
|
|
|
if (commFlag.MCRQ == 1)
|
|
{
|
|
iebus_mastercomm();
|
|
commFlag.MCRQ = 0;
|
|
}
|
|
|
|
if (commFlag.SDRQ == 1)
|
|
{
|
|
iebus_slavetransmission();
|
|
commFlag.SDRQ = 0;
|
|
}
|
|
|
|
if (commFlag.CORQ == 1)
|
|
{
|
|
iebus_command();
|
|
commFlag.CORQ = 0;
|
|
}
|
|
|
|
}
|
|
void dataWrite(char reg_addr, char *data, int datasize)
|
|
{
|
|
//data
|
|
for (int i = 0; i < datasize; i++)
|
|
{
|
|
|
|
}
|
|
}
|
|
|
|
int dataRead(char reg_addr, char *data)
|
|
{
|
|
int size = 0;
|
|
|
|
return size;
|
|
}
|
|
|
|
byte dataRead1byte(char addr)
|
|
{
|
|
byte data;
|
|
// write the LTC CS pin low to initiate ADC sample and data transmit
|
|
digitalWrite(CONTROL, HIGH);
|
|
|
|
|
|
//Serial.println(addr&0xff,HEX);
|
|
|
|
SPI.transfer(addr&0xf8); // Register Select
|
|
|
|
digitalWrite(CONTROL, LOW);
|
|
|
|
data = SPI.transfer(0xff); // read second 8 bits
|
|
// Serial.println(data); //0x2
|
|
digitalWrite(CONTROL, HIGH);
|
|
return (data);
|
|
}
|
|
|
|
|
|
void dataWrite1byte(char addr , byte data)
|
|
{
|
|
|
|
// write the LTC CS pin low to initiate ADC sample and data transmit
|
|
digitalWrite(CONTROL, HIGH);
|
|
|
|
|
|
//Serial.println(addr&0xff,HEX);
|
|
|
|
SPI.transfer(addr&0xf0); // Register Select
|
|
|
|
digitalWrite(CONTROL, LOW);
|
|
// Serial.print("sending:");
|
|
// Serial.println(data,HEX);
|
|
SPI.transfer(data); // read second 8 bits
|
|
// Serial.println(data); //0x2
|
|
digitalWrite(CONTROL, HIGH);
|
|
}
|
|
|
|
|
|
void loop (void) {
|
|
|
|
|
|
byte addr = 0x0;
|
|
byte data;
|
|
|
|
|
|
// Serial.print("0b");
|
|
// data = dataRead1byte(READ);
|
|
// Serial.println( data,BIN);
|
|
//
|
|
//
|
|
// Serial.print("0b");
|
|
// data = dataRead1byte(READ);
|
|
// Serial.println( data,BIN);
|
|
|
|
// wite LTC CS pin high to stop LTC from transmitting zeros.
|
|
// SPI.endTransaction();
|
|
// close SPI transaction
|
|
delay(2500);
|
|
// Delay that is fast but easy to read.
|
|
// delayMicroseconds(83);
|
|
// Delay that matches 12 khz delay time.
|
|
}
|