forked from Mihaylov93/m35080_Read_BitBang
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathm35080_READ_BB.ino
210 lines (195 loc) · 4.43 KB
/
m35080_READ_BB.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
int Chip_Select = SS;
int DATAIN = MISO;
int Sclk = SCK;
int DATAOUT = MOSI;
//Instruction Set
byte WREN = 0b00000110; //Set Write Enable Latch
byte WRDI = 0b00000100; //Reset Write Enable Latch
byte RDSR = 0b00000101; //Read Status Register
byte WRSR = 0b00000001; //Write Status Register
byte READ = 0b00000011; //Read Data from Memory Array
byte WRITE = 0b00000010; //Write Data to Memory Array
byte WRINC = 0b00000111; //Write Data to Secure Array
int i = 0;
unsigned int starting_address;
int h, l;
int fourhigh;
int readings[9];
int retrieve_readings[9];
void setup() {
Serial.println("Boot");
pinMode(Chip_Select, OUTPUT);
pinMode(DATAIN, INPUT);
pinMode(Sclk, OUTPUT);
pinMode(DATAOUT, OUTPUT);
Serial.begin(9600);
Serial.println();
Serial.flush();
Serial.println("Done setting up...");
}
void loop() {
while (Serial.available() <= 0) {
}
byte aux;
Serial.println("INCREMENTAL REGISTERS:");
Serial.print("00: ");
for (byte index = 0x00; index < 0x10; index = index + 0x01) {
aux = read_8(index);
Serial.print(aux, HEX);
Serial.print(" ");
}
Serial.print("\n");
Serial.print("10: ");
for (byte index = 0x10; index < 0x20; index = index + 0x01) {
aux = read_8(index);
Serial.print(aux, HEX);
Serial.print(" ");
}
Serial.println("\n");
Serial.print("NOT INCREMENTAL REGISTERS:");
int cnt = -1 ;
for (int index = 0x20; index <= 0x3FF; index = index + 0x1) {
cnt++;
if (cnt % 8 == 0) {
Serial.print("\n");
Serial.print(index, HEX);
Serial.print(": ");
}
aux = read_8(index);
Serial.print(aux, HEX);
Serial.print(" ");
}
Serial.println("END");
while (1) {
}
}
/*****************************************************/
/*functions*/
void chip_select_low(void) {
digitalWrite(Chip_Select, LOW);
delay(3);
}
void chip_select_high(void) {
digitalWrite(Chip_Select, HIGH);
delay(3);
}
void sclk() {
digitalWrite(Sclk, HIGH);
digitalWrite(Sclk, LOW);
}
/************************************************/
int read_8(int address) {
char bit;
char inbit;
char index;
int value = 0;
chip_select_low();
send_8(READ);
send_address(address);
for (index = 0; index < 8; index++) {
bit = digitalRead(DATAIN);
inbit = bit & 0x01;
if (inbit == 1) {
value = value + (0x80 >> index);
sclk();
} else {
sclk();
}
}
chip_select_high();
return value;
}
/************************************************/
void write_8(int address, char dat) {
char bit;
char inbit;
char index;
int value = 0;
chip_select_low();
send_8(WREN);
chip_select_high();
delay(10);
chip_select_low();
send_8(WRITE);
send_address(address);
// verstehe ich nicht
for (char index = 7; index >= 0; index--) {
bit = ((dat >> index) & 0x01);
if (bit == 1) {
digitalWrite(DATAOUT, HIGH);
sclk();
} else {
digitalWrite(DATAOUT, LOW);
sclk();
}
}
// verstehe ich nicht
chip_select_high();
}
/***********************************************/
void write_secure(int address, char dat1, char dat2) {
char bit;
char inbit;
char index;
int value = 0;
chip_select_low();
send_8(WREN);
chip_select_high();
delay(10);
chip_select_low();
send_8(WRINC);
send_address(address);
// verstehe ich nicht
for (char index = 7; index >= 0; index--) {
bit = ((dat1 >> index) & 0x01);
if (bit == 1) {
digitalWrite(DATAOUT, HIGH);
sclk();
} else {
digitalWrite(DATAOUT, LOW);
sclk();
}
}
for (char index = 7; index >= 0; index--) {
bit = ((dat2 >> index) & 0x01);
if (bit == 1) {
digitalWrite(DATAOUT, HIGH);
sclk();
} else {
digitalWrite(DATAOUT, LOW);
sclk();
}
}
// verstehe ich nicht
chip_select_high();
}
/***********************************************/
void send_8(char dat) {
char bit;
for (char index = 7; index >= 0; index--) {
bit = ((dat >> index) & 0x01);
if (bit == 1) {
digitalWrite(DATAOUT, HIGH);
sclk();
} else {
digitalWrite(DATAOUT, LOW);
sclk();
}
}
}
/******************************************************************/
void send_address(int dat) {
char bit;
char index;
for (index = 15; index >= 0; index--) {
bit = ((dat >> index) & 0x01);
if (bit == 1) {
digitalWrite(DATAOUT, HIGH);
sclk();
} else {
digitalWrite(DATAOUT, LOW);
sclk();
}
}
}
/************************************************************/