-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneuprog.cpp
executable file
·159 lines (134 loc) · 2.97 KB
/
neuprog.cpp
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
#include <stdint.h>
#include <stdio.h>
#include <pigpio.h>
static const int CS = 5;
static const int CK = 21;
static const int mMOSI = 20;
static const int mMISO = 19;
static const int LOW = 0;
static const int HIGH = 1;
#define PIGPIOFACTOR 100
void setup()
{
gpioInitialise();
gpioSetMode(CS, PI_OUTPUT);
gpioSetMode(CK, PI_OUTPUT);
gpioSetMode(mMOSI, PI_OUTPUT);
gpioSetMode(mMISO, PI_INPUT);
printf("Setup done...\n");
}
// Write out the N low order bits of data as MSB first
// and return the collected data.
uint32_t write_N_MSB_first(uint32_t data, int n)
{
uint32_t ret = 0;
// First shift everything to the 16th bit
data <<= (32 - n);
gpioWrite(mMOSI, LOW); // Make the scope trace look nice
gpioDelay(1 * PIGPIOFACTOR);
for (int i = 0; i < n; i++)
{
gpioWrite(mMOSI, (bool)(data & (uint32_t(1) << 31)));
gpioDelay(1 * PIGPIOFACTOR);
gpioWrite(CK, HIGH);
gpioDelay(1 * PIGPIOFACTOR);
gpioWrite(CK, LOW);
gpioDelay(1 * PIGPIOFACTOR);
int r = gpioRead(mMISO);
ret <<= 1;
ret |= r;
data <<= 1;
}
gpioWrite(mMOSI, LOW); // Make the scope trace look nice
return ret;
}
void ewen()
{
gpioWrite(CS, 1);
write_N_MSB_first(0x260, 10);
gpioWrite(CS, 0);
gpioDelay(1 * PIGPIOFACTOR);
}
void ewds()
{
gpioWrite(CS, 1);
write_N_MSB_first(0x200, 10);
gpioWrite(CS, 0);
gpioDelay(1 * PIGPIOFACTOR);
}
uint8_t read(uint8_t addr)
{
gpioWrite(CS, 1);
write_N_MSB_first(0x300 | (addr & 0x7f), 10);
int v = write_N_MSB_first(0, 8);
gpioWrite(CS, 0);
gpioDelay(1 * PIGPIOFACTOR);
return v;
}
void write(uint8_t addr, uint8_t val)
{
gpioWrite(CS, 1);
write_N_MSB_first(0x280 | (addr & 0x7f), 10);
write_N_MSB_first(val, 8);
gpioWrite(CS, 0);
gpioDelay(15 * PIGPIOFACTOR);
}
void dump_eeprom()
{
printf("dump begin\n");
unsigned char data[128];
for (int i = 0; i < 128; i++)
data[i] = read(i);
for (int i = 0; i < 128; i++)
{
char buf[16];
if (i % 16 == 0)
{
sprintf(buf, "\n0x%02x:", i);
fputs(buf, stdout);
}
sprintf(buf, " %02x", data[i]);
fputs(buf, stdout);
}
puts("");
}
void read_count()
{
printf("read count\n");
int v1 = read(0x55);
int v2 = read(0x50);
printf("0x55, 0x50 := 0x%x 0x%x\n", v1, v2);
}
void write_count(int new_count)
{
printf("write count\n");
ewen();
write(0x55, new_count);
write(0x50, new_count - 1);
ewds();
int v3 = read(0x55);
int v4 = read(0x50);
printf("new 0x55, 0x50 := 0x%x 0x%x\n", v3, v4);
}
void all(int state)
{
gpioWrite(CS, state);
gpioWrite(CK, state);
gpioWrite(mMOSI, state);
}
int main()
{
printf("Simple bitbang SPI EEPROM tool for AT93C64D\n");
do
{
setup();
dump_eeprom();
read_count();
write_count(0x9a);
dump_eeprom();
} while (0);
all(LOW);
gpioTerminate();
printf("the end\n");
return 0;
}