SeungheeOh 2 years ago
parent
commit
0dac38e5d2
1 changed files with 148 additions and 36 deletions
  1. 148
    36
      d72042.ino

+ 148
- 36
d72042.ino View File

@@ -1,18 +1,12 @@
1 1
 #include <SPI.h>
2 2
 
3
-#define MODE 8
4
-#define CONTROL 9
5
-#define READ 0x08
6
-#define RESET 7
7 3
 
8
-#define DATASIZE 20
9 4
 
10
-char data[DATASIZE] = {0x0,};
11
-void iebus_init();
12
-byte inline bitchange(byte b)
13
-{
14
-  return b << 4;
15
-}
5
+
6
+#define iebusIRQ 2
7
+#define RAW 0x4
8
+#define isRAW (READFLG&RAW)
9
+#define isMARQ (READFLG&0x40)
16 10
 
17 11
 
18 12
 #define REG_WRITE_CTR bitchange(0x0)
@@ -37,44 +31,46 @@ byte inline bitchange(byte b)
37 31
 #define REG_READ_RBF (bitchange(0xe)|READ)
38 32
 
39 33
 
40
-void setup (void) {
41
-  Serial.begin(9600); //set baud rate to 115200 for usart
42
-
43
-  SPI.begin ();
44
-  SPI.setClockDivider(SPI_CLOCK_DIV128);//divide the clock by 8
45
-
46
-  pinMode(MODE, OUTPUT);
47
-  pinMode(CONTROL, OUTPUT);
48
-
49
-  pinMode(RESET, OUTPUT);
50
-
51
-  digitalWrite(MODE, HIGH);
52
-  digitalWrite(CONTROL, HIGH);
34
+#define LOW_ADDR 0x0
35
+#define HIGH_ADDR 0x36
53 36
 
37
+#define MODE 8
38
+#define CONTROL 9
39
+#define READ 0x08
40
+#define RESET 7
54 41
 
55
-  digitalWrite(RESET, HIGH);
56
-  digitalWrite(SS, LOW);
42
+#define DATASIZE 20
57 43
 
58
-  iebus_init();
59
-}
60 44
 
61
-//      avclan.deviceAddress = 0x360;
62
-//      avclan.broadcast = AVC_MSG_NORMAL;
63
-//      avclan.masterAddress = 0x0360;
64
-//      avclan.slaveAddress  = 0x0140;
65 45
 
66 46
 
67 47
 
68
-#define LOW_ADDR 0x0
69
-#define HIGH_ADDR 0x36
48
+void iebus_init();
49
+void flaginit();
50
+void iebus_transmission();
51
+void iebus_reception();
52
+void iebus_mastercomm();
53
+void iebus_slavetransmission();
54
+void iebus_command();
55
+int checkMARC();
56
+void iebus_irq();
57
+void iebus_loop();
58
+void dataWrite(char reg_addr, char *data, int datasize);
59
+int dataRead(char reg_addr, char *data);
60
+byte dataRead1byte(char addr);
61
+void dataWrite1byte(char addr , char data);
62
+
63
+byte READFLG;
64
+char data[DATASIZE] = {0x0,};
70 65
 
71 66
 
67
+///////////////////////////////////////////////////////////////////////////////////////////////////////////
72 68
 
73 69
 struct _communicationFlag
74 70
 {
75 71
   bool RAWF;
76 72
   bool TRRQ;
77
-  bool TRCF;
73
+
78 74
   byte I;
79 75
   bool RERQ;
80 76
   byte RECF;
@@ -89,7 +85,43 @@ struct _communicationFlag
89 85
   bool MRRQF;
90 86
   bool STRQF;
91 87
   bool SLREF;
92
-}commFlag;
88
+  byte TRCF;
89
+} commFlag;
90
+
91
+
92
+byte inline bitchange(byte b)
93
+{
94
+  return b << 4;
95
+}
96
+
97
+
98
+
99
+
100
+///////////////////////////////////////////////////////////////////////////
101
+
102
+void setup (void) {
103
+  Serial.begin(9600); //set baud rate to 115200 for usart
104
+
105
+  SPI.begin ();
106
+  SPI.setClockDivider(SPI_CLOCK_DIV128);//divide the clock by 8
107
+
108
+  pinMode(MODE, OUTPUT);
109
+  pinMode(CONTROL, OUTPUT);
110
+
111
+  pinMode(RESET, OUTPUT);
112
+
113
+  digitalWrite(MODE, HIGH);
114
+  digitalWrite(CONTROL, HIGH);
115
+
116
+
117
+  digitalWrite(RESET, HIGH);
118
+  digitalWrite(SS, LOW);
119
+
120
+  iebus_init();
121
+}
122
+
123
+
124
+
93 125
 
94 126
 
95 127
 
@@ -106,6 +138,9 @@ void iebus_init()
106 138
   dataWrite1byte(REG_WRITE_CMR, B10000000); //CMR 100000 b1 b0
107 139
 
108 140
   flaginit();
141
+  pinMode(iebusIRQ, INPUT_PULLUP);
142
+
143
+  attachInterrupt(digitalPinToInterrupt(iebusIRQ), iebus_irq, RISING);
109 144
 }
110 145
 void flaginit()
111 146
 {
@@ -149,6 +184,83 @@ void iebus_command()
149 184
 {
150 185
 
151 186
 }
187
+
188
+
189
+int checkMARC()
190
+{
191
+  byte rcrdata = 0xf0 & dataRead1byte(REG_READ_RCR);
192
+  int  isResult = 0;
193
+  if (bitRead(rcrdata, 7) == 0 && bitRead(rcrdata, 6) == 0)
194
+  {
195
+    isResult = 1;
196
+  }
197
+  else if (bitRead(rcrdata, 7) == 0 && bitRead(rcrdata, 6) == 1 && bitRead(rcrdata, 5) == 0)
198
+  {
199
+    isResult = 2;
200
+  }
201
+  else if (bitRead(rcrdata, 7) == 0 && bitRead(rcrdata, 6) == 1 && bitRead(rcrdata, 5) == 1)
202
+  {
203
+    isResult = 3;
204
+  }
205
+  else
206
+  {
207
+    isResult = 0;
208
+  }
209
+
210
+  return isResult;
211
+}
212
+
213
+
214
+
215
+void iebus_irq()
216
+{
217
+  byte marcValue;
218
+  noInterrupts();
219
+  READFLG = dataRead1byte(REG_READ_FLG);
220
+  if (isRAW)
221
+  {
222
+    commFlag.RAWF = 1;
223
+  }
224
+  else
225
+  {
226
+    marcValue = checkMARC();
227
+    if ((commFlag.MTRQF == 1 && isMARQ == 0) || (commFlag.MRRQF == 1 && isMARQ == 0) || (marcValue > 0))
228
+    {
229
+      Serial.println("MARC");
230
+      if (marcValue == 1)
231
+      {
232
+        //00xx
233
+        commFlag.TRCF = marcValue>>4;
234
+        commFlag.TRRQ = 1;
235
+      }
236
+      else if (marcValue == 2)
237
+      {
238
+        //010x
239
+        commFlag.RERQ = 1;
240
+        commFlag.SIZE = dataRead1byte(REG_READ_RDR1);
241
+        commFlag.RECF = marcValue>>4;
242
+      }
243
+      else if (isMarc == 3)
244
+      {
245
+        //011x
246
+        commFlag.RERQ = 1;
247
+        commFlag.SIZE = dataRead1byte(REG_READ_RDR1);
248
+        commFlag.RECF = marcValue>>4;
249
+
250
+        //increment PW
251
+        commFlag.RERQ = 0;
252
+        commFlag.SIZE = 0;
253
+
254
+      }
255
+
256
+
257
+
258
+      //is Return code in slrc
259
+    }
260
+  }
261
+  interrupts();
262
+}
263
+
152 264
 void iebus_loop()
153 265
 {
154 266
   if (commFlag.RAWF == 1)