Skip to content

Commit 880ed50

Browse files
committed
Add Flasher sketch
This flashes version 0.06
1 parent a5edb33 commit 880ed50

File tree

2 files changed

+1070
-0
lines changed

2 files changed

+1070
-0
lines changed

examples/Flasher/Flasher.ino

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#include "Wire.h"
2+
#include "fw.h"
3+
4+
#define I2C_ADDRESS 0x09
5+
6+
void setDataRunning(int cmd, uint8_t target, int data) {
7+
Wire.beginTransmission(0x66);
8+
Wire.write((uint8_t)cmd);
9+
Wire.write((uint8_t)target);
10+
Wire.write(data);
11+
Wire.endTransmission();
12+
}
13+
14+
15+
void setup() {
16+
// put your setup code here, to run once:
17+
Wire.begin();
18+
Wire.setClock(1000000);
19+
20+
Serial.begin(115200);
21+
while (!Serial);
22+
pinMode(6, OUTPUT);
23+
digitalWrite(6, HIGH);
24+
25+
int prev_address = -1;
26+
27+
Wire.beginTransmission(0x66);
28+
Wire.write((uint8_t)1);
29+
Wire.write((uint8_t)0);
30+
Wire.endTransmission();
31+
32+
Wire.requestFrom(0x66, 5);
33+
Wire.read();
34+
35+
String version = "";
36+
37+
while (Wire.available()) {
38+
version += (char)Wire.read();
39+
}
40+
41+
if (version.c_str()[0] == '0') {
42+
Serial.println("Reset D11");
43+
setDataRunning(2, 0, 0);
44+
delay(10);
45+
}
46+
47+
// reset running D11
48+
Serial.println("Erase flash");
49+
50+
Wire.beginTransmission(I2C_ADDRESS);
51+
Wire.write('r');
52+
Wire.endTransmission();
53+
54+
delay(500);
55+
56+
Serial.println("Starting flash");
57+
58+
int address = 0;
59+
while (address < (I2C_Motor_bridge_D11_ino_bin_len + 0x1000)) {
60+
int retry = 0;
61+
do {
62+
Wire.requestFrom(I2C_ADDRESS, 4);
63+
uint8_t buf[4];
64+
int k = 0;
65+
while (Wire.available()) {
66+
buf[k++] = Wire.read();
67+
}
68+
address = *(uint32_t*)buf;
69+
delay(10);
70+
} while (prev_address == address && retry++ < 5);
71+
prev_address = address;
72+
Serial.println(address);
73+
74+
uint8_t crc = 0;
75+
for (int j = 0; j < 64; j++) {
76+
crc ^= I2C_Motor_bridge_D11_ino_bin[address - 0x1000 + j];
77+
}
78+
79+
Serial.println(crc, HEX);
80+
81+
Wire.beginTransmission(I2C_ADDRESS);
82+
Wire.write('w');
83+
Wire.write(crc);
84+
Wire.write(&I2C_Motor_bridge_D11_ino_bin[address - 0x1000], 64);
85+
Wire.endTransmission();
86+
}
87+
88+
Serial.println("Booting FW");
89+
Wire.beginTransmission(I2C_ADDRESS);
90+
Wire.write('x');
91+
Wire.endTransmission();
92+
93+
delay(1000);
94+
95+
Wire.beginTransmission(0x66);
96+
Wire.write((uint8_t)1);
97+
Wire.write((uint32_t)0);
98+
Wire.endTransmission();
99+
100+
Wire.requestFrom(0x66, 5);
101+
Wire.read();
102+
103+
Serial.print("New version: ");
104+
while (Wire.available()) {
105+
Serial.print((char)Wire.read());
106+
}
107+
Serial.println();
108+
}
109+
110+
void loop() {
111+
// put your main code here, to run repeatedly:
112+
113+
}

0 commit comments

Comments
 (0)