From 630abd209ca90302ab7933f34063a6a146784ab5 Mon Sep 17 00:00:00 2001 From: SeungheeOh Date: Wed, 23 Oct 2019 05:36:57 +0900 Subject: [PATCH] 0536 --- d72042.ino | 598 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 396 insertions(+), 202 deletions(-) diff --git a/d72042.ino b/d72042.ino index 8ee17f9..0fae0dc 100644 --- a/d72042.ino +++ b/d72042.ino @@ -23,15 +23,15 @@ #define REG_READ_FLG (bitchange(0x1)|READ) #define REG_READ_RDR1 (bitchange(0x2)|READ) #define REG_READ_RDR2 (bitchange(0x3)|READ) -#define REG_READ_LDR1 (bitchange(0x4)|READ) -#define REG_READ_LDR2 (bitchange(0x5)|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 0x0 +#define LOW_ADDR 0x00 #define HIGH_ADDR 0x36 #define MODE 8 @@ -58,7 +58,7 @@ 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 , char data); +void dataWrite1byte(char addr , byte data); byte READFLG; char data[DATASIZE] = {0x0,}; @@ -68,96 +68,250 @@ char data[DATASIZE] = {0x0,}; struct _communicationFlag { - bool RAWF; - bool TRRQ; + 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; + 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 inline bitchange(byte b) + + + +byte bitchange(byte b) { - return b << 4; + 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 - - pinMode(MODE, OUTPUT); - pinMode(CONTROL, OUTPUT); - - pinMode(RESET, OUTPUT); - - digitalWrite(MODE, HIGH); - digitalWrite(CONTROL, HIGH); - digitalWrite(RESET, HIGH); - digitalWrite(SS, LOW); + 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(); - 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); + digitalWrite(RESET, LOW); + delay(10); + digitalWrite(RESET, HIGH); + delay(10); - dataWrite1byte(REG_WRITE_UAR1, LOW_ADDR << 4); - dataWrite1byte(REG_WRITE_UAR2, HIGH_ADDR); - dataWrite1byte(REG_WRITE_CMR, B10000000); //CMR 100000 b1 b0 - flaginit(); - pinMode(iebusIRQ, INPUT_PULLUP); - attachInterrupt(digitalPinToInterrupt(iebusIRQ), iebus_irq, RISING); + // 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_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_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); + } + + + //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; + 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; } @@ -180,191 +334,231 @@ void iebus_slavetransmission() { } + + void iebus_command() { + while((READFLG = dataRead1byte(REG_READ_FLG))&0x8); + dataWrite1byte(REG_WRITE_CMR, 0b00010000); -} + //CMR command code - -int checkMARC() -{ - byte rcrdata = 0xf0 & dataRead1byte(REG_READ_RCR); - int isResult = 0; - if (bitRead(rcrdata, 7) == 0 && bitRead(rcrdata, 6) == 0) - { - isResult = 1; - } - else if (bitRead(rcrdata, 7) == 0 && bitRead(rcrdata, 6) == 1 && bitRead(rcrdata, 5) == 0) - { - isResult = 2; - } - else if (bitRead(rcrdata, 7) == 0 && bitRead(rcrdata, 6) == 1 && bitRead(rcrdata, 5) == 1) - { - isResult = 3; - } - else - { - isResult = 0; - } - - return isResult; } void iebus_irq() { - byte marcValue; - noInterrupts(); - READFLG = dataRead1byte(REG_READ_FLG); - if (isRAW) - { - commFlag.RAWF = 1; - } - else - { - marcValue = checkMARC(); - if ((commFlag.MTRQF == 1 && isMARQ == 0) || (commFlag.MRRQF == 1 && isMARQ == 0) || (marcValue > 0)) + noInterrupts(); + + + interrupts(); +} + + +void iebus_irq2() +{ + byte registerRCR; + noInterrupts(); + READFLG = dataRead1byte(REG_READ_FLG); + if (isRAW) // Program Crash? { - Serial.println("MARC"); - if (marcValue == 1) - { - //00xx - commFlag.TRCF = marcValue>>4; - commFlag.TRRQ = 1; - } - else if (marcValue == 2) - { - //010x - commFlag.RERQ = 1; - commFlag.SIZE = dataRead1byte(REG_READ_RDR1); - commFlag.RECF = marcValue>>4; - } - else if (isMarc == 3) - { - //011x - commFlag.RERQ = 1; - commFlag.SIZE = dataRead1byte(REG_READ_RDR1); - commFlag.RECF = marcValue>>4; - - //increment PW - commFlag.RERQ = 0; - commFlag.SIZE = 0; - - } - - - - //is Return code in slrc + commFlag.RAWF = 1; } - } - interrupts(); + 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(); - return; - } - if (commFlag.TRRQ == 1) - { - iebus_transmission(); - commFlag.TRRQ = 0; - } - if (commFlag.RERQ == 1) - { - iebus_reception(); - commFlag.RERQ = 0; - } + if (commFlag.RAWF == 1) + { + iebus_init(); - if (commFlag.MCRQ == 1) - { - iebus_mastercomm(); - commFlag.MCRQ = 0; - } + } - if (commFlag.SDRQ == 1) - { - iebus_slavetransmission(); - commFlag.SDRQ = 0; - } + if (commFlag.TRRQ == 1) + { + iebus_transmission(); + commFlag.TRRQ = 0; + } + if (commFlag.RERQ == 1) + { + iebus_reception(); + commFlag.RERQ = 0; + } - if (commFlag.CORQ == 1) - { - iebus_command(); - commFlag.CORQ = 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++) - { + //data + for (int i = 0; i < datasize; i++) + { - } + } } int dataRead(char reg_addr, char *data) { - int size = 0; + int size = 0; - return size; + return size; } byte dataRead1byte(char addr) { - byte data; - // write the LTC CS pin low to initiate ADC sample and data transmit - digitalWrite(CONTROL, HIGH); - delay(5); - SPI.transfer(REG_READ_FLG); // Register Select + byte data; + // write the LTC CS pin low to initiate ADC sample and data transmit + digitalWrite(CONTROL, HIGH); - digitalWrite(CONTROL, LOW); - delay(5); - data = SPI.transfer(0x0); // read second 8 bits - // Serial.println(data); //0x2 - return data; + //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 , char data) +void dataWrite1byte(char addr , byte data) { - digitalWrite(CONTROL, HIGH); - delay(5); - SPI.transfer(addr); // Register Select - digitalWrite(CONTROL, LOW); - delay(5); - SPI.transfer(data); // read second 8 bits - delay(5); + // 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) { - iebus_loop(); + byte addr = 0x0; + byte data; - 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); - // SPI.beginTransaction(SPISettings(1000, MSBFIRST, SPI_MODE0)); - // set speed bit format and clock/data polarity while starting SPI transaction - - Serial.println(dataRead1byte(REG_READ_FLG)); - - // wite LTC CS pin high to stop LTC from transmitting zeros. - // SPI.endTransaction(); - // close SPI transaction - delay(250); - // Delay that is fast but easy to read. - // delayMicroseconds(83); - // Delay that matches 12 khz delay time. + // 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. }