summaryrefslogtreecommitdiffstats
path: root/reverse_engineering/scani2c-4.pl
blob: 40c9cc1397de103f7eb8c142b772113385717a45 (plain)
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#!/usr/bin/env perl 
use strict;

use IO::Socket::INET;
use Data::Dumper;
#use IO::Socket::Timeout;
#
#

$SIG{INT}=\&quit;
my $prompt= '> ';

sub my_readline_worker($)
{
my $sock=shift;
my $ret="";
my $d="";

while (1) {
return $ret if $sock->read($d,1)!=1;

next if $d eq "\n";

$ret.=$d;

return $ret if $d eq "\r";
return $ret if $ret =~ /> $/;

} 


}

sub my_readline($)
{
my $sock=shift;
my $ret=my_readline_worker($sock);
#print $ret."\n";
return $ret;
}




sub wait_for_prompt($)
{
my $ocd=shift;

1 while (my_readline($ocd) ne $prompt);
}


sub open_ocd($) {
my $addr=shift;
my $sock = IO::Socket::INET->new( $addr);
wait_for_prompt($sock);
    $sock->printf( "reset halt\n");
    wait_for_prompt($sock);
return $sock;
}


sub write_reg($$$)
{
my ($ocd,$r,$v)=@_;

$ocd->printf("mww 0x%08x 0x%08x\n",$r,$v);
wait_for_prompt($ocd);
}

sub read_reg($$)
{
my ($ocd,$r)=@_;
my $ret;

$ocd->printf("mdw 0x%08x\n",$r);

$ret=my_readline($ocd);
$ret=my_readline($ocd);

wait_for_prompt($ocd);


$ret =~ s/[\r\n\s]//g;


if ($ret =~ /0x[0-9A-Fa-f]+:([0-9a-fA-F]+)/) {
	return hex($1);
}

return undef;
}



sub dir_set($$) {
my ($ocd,$v)=@_;
write_reg($ocd,0x50000518,$v);
}


sub dir_clr($$) {
my ($ocd,$v)=@_;
write_reg($ocd,0x5000051c,$v);
}

sub io_set($$) {
my ($ocd,$v)=@_;
write_reg($ocd,0x50000508,$v);
}

sub io_clr($$) {
my ($ocd,$v)=@_;
write_reg($ocd,0x5000050c,$v);
}

sub io_get($) {
my $ocd=shift;
return read_reg($ocd,0x50000510);
}


sub io_cnf($$$) {
    my ( $ocd, $pin, $v ) = @_;
    write_reg( $ocd, 0x50000700 + ($pin * 4), $v );
}


my $SDABIT=1<<18;
my $SCLBIT=1<<17;



sub i2c_set($$$) {
my ($ocd,$scl,$sda) =@_;
my $clr=0;
my $set=0;


if ($scl) {
	$clr|=$SCLBIT;
} else {
	$set|=$SCLBIT;
}

if ($sda) {
	$clr|=$SDABIT;
} else {
	$set|=$SDABIT;
}

dir_set($ocd,$set);
dir_clr($ocd,$clr);
}


sub i2c_get($) {
my $ocd=shift;

return (io_get($ocd) & $SDABIT ) ? 1:0;
}

sub i2c_start($) {
my $ocd=shift;

i2c_set($ocd,1,1);
i2c_set($ocd,1,0);
i2c_set($ocd,0,0);
}

sub i2c_stop($) {
my $ocd=shift;

i2c_set($ocd,0,0);
i2c_set($ocd,1,0);
i2c_set($ocd,1,1);
}


sub i2c_sendbyte($$)
{
my ($ocd,$byte) =@_;

for ( my $c=0x80; $c; $c>>=1)
{
my $v=($c & $byte) ? 1:0;

print $v;

i2c_set($ocd,0,$v);
i2c_set($ocd,1,$v);
i2c_set($ocd,0,$v);
}

}

sub i2c_startaddr($$)
{
my ($ocd,$addr) =@_;
my $ret;

i2c_start($ocd);
i2c_sendbyte($ocd,$addr);
print " ";

i2c_set($ocd,0,1);
i2c_set($ocd,1,1);

$ret=i2c_get($ocd);
i2c_set($ocd,0,1);

print $ret;

return $ret;

}

sub i2c_ping($$)
{
my ($ocd,$addr) =@_;
my $ret;
$ret=i2c_startaddr($ocd,$addr);
i2c_stop($ocd);
return $ret;
}


my $ocd=open_ocd('127.0.0.1:4444' );


io_cnf($ocd,18,0x60c);
io_cnf($ocd,17,0x60c);
io_clr($ocd,$SCLBIT | $SDABIT);

i2c_start($ocd);
i2c_stop($ocd);

for (my $a=0;$a<0x100;$a++) {
printf "a=0x%02x ",$a;
i2c_ping($ocd,$a);
i2c_start($ocd);
i2c_stop($ocd);
i2c_start($ocd);
i2c_stop($ocd);

print "\n";
}