No Description

d72042.ino 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. #include <SPI.h>
  2. #define iebusIRQ 2
  3. #define RAW 0x4
  4. #define isRAW (READFLG&RAW)
  5. #define isMARQ (READFLG&0x40)
  6. #define REG_WRITE_CTR bitchange(0x0)
  7. #define REG_WRITE_CMR bitchange(0x1)
  8. #define REG_WRITE_UAR1 bitchange(0x2)
  9. #define REG_WRITE_UAR2 bitchange(0x3)
  10. #define REG_WRITE_SAR1 bitchange(0x4)
  11. #define REG_WRITE_SAR2 bitchange(0x5)
  12. #define REG_WRITE_MCR bitchange(0x6)
  13. #define REG_WRITE_TBF bitchange(0xe)
  14. #define REG_READ_STR (bitchange(0x0)|READ)
  15. #define REG_READ_FLG (bitchange(0x1)|READ)
  16. #define REG_READ_RDR1 (bitchange(0x2)|READ)
  17. #define REG_READ_RDR2 (bitchange(0x3)|READ)
  18. #define REG_READ_LOR1 (bitchange(0x4)|READ)
  19. #define REG_READ_LOR2 (bitchange(0x5)|READ)
  20. #define REG_READ_DAR1 (bitchange(0x6)|READ)
  21. #define REG_READ_DAR2 (bitchange(0x7)|READ)
  22. #define REG_READ_RCR (bitchange(0x8)|READ)
  23. #define REG_READ_RBF (bitchange(0xe)|READ)
  24. #define LOW_ADDR 0x00
  25. #define HIGH_ADDR 0x36
  26. #define MODE 8
  27. #define CONTROL 9
  28. #define READ 0x08
  29. #define RESET 7
  30. #define DATASIZE 20
  31. void iebus_init();
  32. void flaginit();
  33. void iebus_transmission();
  34. void iebus_reception();
  35. void iebus_mastercomm();
  36. void iebus_slavetransmission();
  37. void iebus_command();
  38. int checkMARC();
  39. void iebus_irq();
  40. void iebus_loop();
  41. void dataWrite(char reg_addr, char *data, int datasize);
  42. int dataRead(char reg_addr, char *data);
  43. byte dataRead1byte(char addr);
  44. void dataWrite1byte(char addr , byte data);
  45. byte READFLG;
  46. char data[DATASIZE] = {0x0,};
  47. ///////////////////////////////////////////////////////////////////////////////////////////////////////////
  48. struct _communicationFlag
  49. {
  50. bool RAWF;
  51. bool TRRQ;
  52. byte I;
  53. bool RERQ;
  54. byte RECF;
  55. byte SIZE;
  56. byte PW;
  57. byte PR;
  58. byte J;
  59. bool MCRQ;
  60. bool SDRQ;
  61. bool CORQ;
  62. bool MTRQF;
  63. bool MRRQF;
  64. bool STRQF;
  65. bool SLREF;
  66. byte TRCF;
  67. } commFlag;
  68. byte bitchange(byte b)
  69. {
  70. byte result = 0;
  71. //
  72. // // Serial.print("TEST:0b");
  73. // // Serial.print(b,BIN);
  74. ////
  75. //// //result=(bitRead(b,3))|(bitRead(b,2)<<1) |(bitRead(b,1)<<2)|(bitRead(b,0)<<3);
  76. //if(bitRead(b,0))
  77. //{
  78. // result|=0x8;
  79. //}
  80. //
  81. //if(bitRead(b,1))
  82. //{
  83. // result|=0x4;
  84. //}
  85. //
  86. //if(bitRead(b,2))
  87. //{
  88. // result|=0x2;
  89. //}
  90. //
  91. //if(bitRead(b,3))
  92. //{
  93. // result|=0x1;
  94. //}
  95. // // | (bitRead(b,2) <<1 )| (bitRead(b,3) <<0)
  96. // return result;
  97. return b<<4;
  98. }
  99. ///////////////////////////////////////////////////////////////////////////
  100. void setup (void) {
  101. Serial.begin(9600); //set baud rate to 115200 for usart
  102. SPI.begin ();
  103. SPI.setClockDivider(SPI_CLOCK_DIV128);//divide the clock by 8
  104. SPI.setBitOrder(MSBFIRST);
  105. SPI.setDataMode(SPI_MODE0);
  106. // SPI.beginTransaction(SPISettings(1000, MSBFIRST, SPI_MODE0));
  107. pinMode(MODE, OUTPUT);
  108. pinMode(CONTROL, OUTPUT);
  109. pinMode(RESET, OUTPUT);
  110. digitalWrite(MODE, HIGH);
  111. digitalWrite(CONTROL, HIGH);
  112. digitalWrite(RESET, HIGH);
  113. digitalWrite(SS, LOW);
  114. iebus_init();
  115. }
  116. byte reverse_bits(byte Mitutoyo) {
  117. byte rtn = 0;
  118. for (byte i=0; i<4; i++) {
  119. bitWrite(rtn, 3-i, bitRead(Mitutoyo, i));
  120. }
  121. return rtn;
  122. }
  123. void iebus_init()
  124. {
  125. digitalWrite(RESET, LOW);
  126. delay(10);
  127. digitalWrite(RESET, HIGH);
  128. delay(10);
  129. // dataWrite1byte(REG_WRITE_UAR1, LOW_ADDR << 4);
  130. // delay(500);
  131. //
  132. // dataWrite1byte(REG_WRITE_UAR2, HIGH_ADDR);
  133. //
  134. //
  135. // delay(500);
  136. //
  137. //
  138. //
  139. // dataWrite1byte(REG_WRITE_CTR, B00000000);
  140. // delay(500);
  141. //
  142. //
  143. // dataWrite1byte(REG_WRITE_CMR, B01000000);
  144. // delay(2000);
  145. {
  146. Serial.print("REG_READ_STR[0b");
  147. Serial.print(REG_READ_STR,BIN);
  148. Serial.print("]");
  149. Serial.print("REG_READ_STR: 0x");
  150. byte tmp = dataRead1byte(REG_READ_STR);
  151. Serial.println(tmp,BIN);
  152. }
  153. {
  154. Serial.print("REG_READ_FLG[0b");
  155. Serial.print(REG_READ_FLG,BIN);
  156. Serial.print("]");
  157. Serial.print("REG_READ_FLG: 0x");
  158. byte tmp = dataRead1byte(REG_READ_FLG);
  159. Serial.println(tmp,BIN);
  160. }
  161. {
  162. Serial.print("REG_READ_LOR[0b");
  163. Serial.print(REG_READ_LOR2,BIN);
  164. Serial.print("]");
  165. Serial.print("REG_READ_LOR2: 0x");
  166. byte tmp = dataRead1byte(REG_READ_LOR2);
  167. Serial.println(tmp,BIN);
  168. }
  169. {
  170. Serial.print("REG_READ_RCR[0b");
  171. Serial.print(REG_READ_RCR,BIN);
  172. Serial.print("]");
  173. Serial.print("REG_READ_RCR: 0x");
  174. byte tmp = dataRead1byte(REG_READ_RCR);
  175. Serial.println(tmp,BIN);
  176. }
  177. Serial.println("=====================================================");
  178. dataWrite1byte(REG_WRITE_CTR,0b00000001); //Enter stanby mode
  179. delay(100);
  180. dataWrite1byte(REG_WRITE_CTR,0b00000000); //Exits from standby mode
  181. delay(100);
  182. dataWrite1byte(REG_WRITE_CTR,0b00010000); //set REENs
  183. delay(100);
  184. dataWrite1byte(REG_WRITE_UAR1,0x37); //UAR1
  185. delay(100);
  186. dataWrite1byte(REG_WRITE_UAR2,0x00); //UAR2
  187. delay(100);
  188. dataWrite1byte(REG_WRITE_CMR,0b01000000); //Lock
  189. delay(100);
  190. {
  191. Serial.print("REG_READ_STR[0b");
  192. Serial.print(REG_READ_STR,BIN);
  193. Serial.print("]");
  194. Serial.print("REG_READ_STR: 0x");
  195. byte tmp = dataRead1byte(REG_READ_STR);
  196. Serial.println(tmp,BIN);
  197. }
  198. {
  199. Serial.print("REG_READ_FLG[0b");
  200. Serial.print(REG_READ_FLG,BIN);
  201. Serial.print("]");
  202. Serial.print("REG_READ_FLG: 0x");
  203. byte tmp = dataRead1byte(REG_READ_FLG);
  204. Serial.println(tmp,BIN);
  205. }
  206. {
  207. Serial.print("REG_READ_LOR1[0b");
  208. Serial.print(REG_READ_LOR1,BIN);
  209. Serial.print("]");
  210. Serial.print("REG_READ_LOR1: 0x");
  211. byte tmp = dataRead1byte(REG_READ_LOR1);
  212. Serial.println(tmp,BIN);
  213. }
  214. {
  215. Serial.print("REG_READ_LOR2[0b");
  216. Serial.print(REG_READ_LOR2,BIN);
  217. Serial.print("]");
  218. Serial.print("REG_READ_LOR2: 0x");
  219. byte tmp = dataRead1byte(REG_READ_LOR2);
  220. Serial.println(tmp,BIN);
  221. }
  222. {
  223. Serial.print("REG_READ_RCR[0b");
  224. Serial.print(REG_READ_RCR,BIN);
  225. Serial.print("]");
  226. Serial.print("REG_READ_RCR: 0x");
  227. byte tmp = dataRead1byte(REG_READ_RCR);
  228. Serial.println(tmp,BIN);
  229. }
  230. //pinMode(iebusIRQ, INPUT_PULLUP);
  231. // attachInterrupt(digitalPinToInterrupt(iebusIRQ), iebus_irq, RISING);
  232. flaginit();
  233. }
  234. void flaginit()
  235. {
  236. commFlag.RAWF = 0;
  237. commFlag.TRRQ = 0;
  238. commFlag.RERQ = 0;
  239. commFlag.SIZE = 0;
  240. commFlag.J = 1;
  241. commFlag.PW = 0;
  242. commFlag.PR = 0;
  243. commFlag.MCRQ = 0;
  244. commFlag.SDRQ = 0;
  245. commFlag.CORQ = 0;
  246. commFlag.MTRQF = 0;
  247. commFlag.MRRQF = 0;
  248. commFlag.STRQF = 0;
  249. commFlag.SLREF = 0;
  250. }
  251. void iebus_transmission()
  252. {
  253. }
  254. void iebus_reception()
  255. {
  256. }
  257. void iebus_mastercomm()
  258. {
  259. }
  260. void iebus_slavetransmission()
  261. {
  262. }
  263. void iebus_command()
  264. {
  265. while((READFLG = dataRead1byte(REG_READ_FLG))&0x8);
  266. dataWrite1byte(REG_WRITE_CMR, 0b00010000);
  267. //CMR command code
  268. }
  269. void iebus_irq()
  270. {
  271. noInterrupts();
  272. interrupts();
  273. }
  274. void iebus_irq2()
  275. {
  276. byte registerRCR;
  277. noInterrupts();
  278. READFLG = dataRead1byte(REG_READ_FLG);
  279. if (isRAW) // Program Crash?
  280. {
  281. commFlag.RAWF = 1;
  282. }
  283. else
  284. {
  285. //Read RCR
  286. registerRCR = dataRead1byte(REG_READ_RCR);
  287. byte MARC = (registerRCR>>4)&0xf;
  288. byte SLRC = registerRCR&0x0f;
  289. //is return MARC Enabled
  290. if(bitRead(MARC,3)==0&&bitRead(MARC,2)==0)
  291. {
  292. //00xx
  293. commFlag.TRCF=MARC;
  294. commFlag.TRRQ=true;
  295. }
  296. else if(bitRead(MARC,3)==0&&bitRead(MARC,2)==1 &&bitRead(MARC,1)==0)
  297. {
  298. ////010x
  299. commFlag.RERQ=true;
  300. commFlag.SIZE=dataRead1byte(REG_READ_RDR1);
  301. commFlag.RECF = MARC;
  302. }
  303. else if(bitRead(MARC,3)==0&&bitRead(MARC,2)==1 &&bitRead(MARC,1)==1)
  304. {
  305. //011x
  306. commFlag.RERQ=true;
  307. commFlag.SIZE=dataRead1byte(REG_READ_RDR1);
  308. commFlag.RECF = MARC;
  309. commFlag.PW++;
  310. commFlag.RERQ=false;
  311. commFlag.SIZE=0;
  312. }
  313. else
  314. {
  315. Serial.print("MARC Disabled");
  316. }
  317. //is return SLRC Enabled
  318. if(bitRead(SLRC,3)==0&&bitRead(SLRC,2)==0)
  319. {
  320. //00xx
  321. commFlag.TRCF=SLRC;
  322. commFlag.TRRQ=true;
  323. }
  324. 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))
  325. {
  326. //010x, 100x
  327. commFlag.RERQ=true;
  328. commFlag.SIZE=dataRead1byte(REG_READ_RDR2);
  329. commFlag.RECF = SLRC;
  330. }
  331. 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))
  332. {
  333. //011x
  334. commFlag.RERQ=true;
  335. commFlag.SIZE=dataRead1byte(REG_READ_RDR2);
  336. commFlag.RECF = SLRC;
  337. //REEN 1
  338. dataWrite1byte(REG_WRITE_CTR, 0b00010000);
  339. commFlag.RERQ=false;
  340. commFlag.SIZE=0;
  341. }
  342. }
  343. interrupts();
  344. }
  345. void iebus_loop()
  346. {
  347. if (commFlag.RAWF == 1)
  348. {
  349. iebus_init();
  350. }
  351. if (commFlag.TRRQ == 1)
  352. {
  353. iebus_transmission();
  354. commFlag.TRRQ = 0;
  355. }
  356. if (commFlag.RERQ == 1)
  357. {
  358. iebus_reception();
  359. commFlag.RERQ = 0;
  360. }
  361. if (commFlag.MCRQ == 1)
  362. {
  363. iebus_mastercomm();
  364. commFlag.MCRQ = 0;
  365. }
  366. if (commFlag.SDRQ == 1)
  367. {
  368. iebus_slavetransmission();
  369. commFlag.SDRQ = 0;
  370. }
  371. if (commFlag.CORQ == 1)
  372. {
  373. iebus_command();
  374. commFlag.CORQ = 0;
  375. }
  376. }
  377. void dataWrite(char reg_addr, char *data, int datasize)
  378. {
  379. //data
  380. for (int i = 0; i < datasize; i++)
  381. {
  382. }
  383. }
  384. int dataRead(char reg_addr, char *data)
  385. {
  386. int size = 0;
  387. return size;
  388. }
  389. byte dataRead1byte(char addr)
  390. {
  391. byte data;
  392. // write the LTC CS pin low to initiate ADC sample and data transmit
  393. digitalWrite(CONTROL, HIGH);
  394. //Serial.println(addr&0xff,HEX);
  395. SPI.transfer(addr&0xf8); // Register Select
  396. digitalWrite(CONTROL, LOW);
  397. data = SPI.transfer(0xff); // read second 8 bits
  398. // Serial.println(data); //0x2
  399. digitalWrite(CONTROL, HIGH);
  400. return (data);
  401. }
  402. void dataWrite1byte(char addr , byte data)
  403. {
  404. // write the LTC CS pin low to initiate ADC sample and data transmit
  405. digitalWrite(CONTROL, HIGH);
  406. //Serial.println(addr&0xff,HEX);
  407. SPI.transfer(addr&0xf0); // Register Select
  408. digitalWrite(CONTROL, LOW);
  409. // Serial.print("sending:");
  410. // Serial.println(data,HEX);
  411. SPI.transfer(data); // read second 8 bits
  412. // Serial.println(data); //0x2
  413. digitalWrite(CONTROL, HIGH);
  414. }
  415. void loop (void) {
  416. byte addr = 0x0;
  417. byte data;
  418. // Serial.print("0b");
  419. // data = dataRead1byte(READ);
  420. // Serial.println( data,BIN);
  421. //
  422. //
  423. // Serial.print("0b");
  424. // data = dataRead1byte(READ);
  425. // Serial.println( data,BIN);
  426. // wite LTC CS pin high to stop LTC from transmitting zeros.
  427. // SPI.endTransaction();
  428. // close SPI transaction
  429. delay(2500);
  430. // Delay that is fast but easy to read.
  431. // delayMicroseconds(83);
  432. // Delay that matches 12 khz delay time.
  433. }