Skip to content

Commit 512768b

Browse files
committed
Fixed bug where back-to-back invocations of blisp would fail
1 parent 380fc7e commit 512768b

File tree

3 files changed

+45
-19
lines changed

3 files changed

+45
-19
lines changed

include/blisp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ struct blisp_segment_header {
1616
struct blisp_device {
1717
struct blisp_chip* chip;
1818
void* serial_port;
19+
uint32_t serial_timeout; // in ms
1920
bool is_usb;
2021
uint32_t current_baud_rate;
2122
uint8_t rx_buffer[5000]; // TODO:

lib/blisp.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ blisp_return_t blisp_device_init(struct blisp_device* device,
2626
device->chip = chip;
2727
device->is_usb = false;
2828
fill_crcs(&bl808_header);
29+
30+
if (device->chip->type == BLISP_CHIP_BL808) {
31+
// TODO: For some reason the BL808 does not send pending ('PD') responses
32+
// during long (i.e. erase) operations, so we must disable response
33+
// timeouts. Further investigation is necessary.
34+
device->serial_timeout = 0;
35+
} else {
36+
device->serial_timeout = 1000;
37+
}
38+
2939
return BLISP_OK;
3040
}
3141

@@ -145,16 +155,9 @@ blisp_return_t blisp_receive_response(struct blisp_device* device,
145155
bool expect_payload) {
146156
// TODO: Check checksum
147157
int ret;
148-
uint32_t read_timeout = 1000;
149-
if (device->chip->type == BLISP_CHIP_BL808) {
150-
// TODO: For some reason the BL808 does not send pending ('PD') responses
151-
// during long (i.e. erase) operations, so we must disable response
152-
// timeouts. Further investigation is necessary.
153-
read_timeout = 0;
154-
}
155158

156159
struct sp_port* serial_port = device->serial_port;
157-
ret = sp_blocking_read(serial_port, &device->rx_buffer[0], 2, read_timeout);
160+
ret = sp_blocking_read(serial_port, &device->rx_buffer[0], 2, device->serial_timeout);
158161
if (ret < 2) {
159162
blisp_dlog("Failed to receive response, ret: %d", ret);
160163
return BLISP_ERR_NO_RESPONSE;

tools/blisp/src/common.c

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,41 @@ blisp_return_t blisp_common_init_device(struct blisp_device* device,
6767
*/
6868
blisp_return_t blisp_common_prepare_flash(struct blisp_device* device) {
6969
blisp_return_t ret = 0;
70-
71-
printf("Sending a handshake...\n");
72-
ret = blisp_device_handshake(device, false);
73-
if (ret != BLISP_OK) {
74-
fprintf(stderr, "Failed to handshake with device, ret: %d\n", ret);
75-
return ret;
76-
}
77-
printf("Handshake successful!\nGetting chip info...\n");
70+
uint32_t previous_timeout;
7871
struct blisp_boot_info boot_info;
72+
73+
// We may already be in communication with the chip from a previous
74+
// invocation of this command. In that case, it will not respond to our
75+
// handshake. We detect this by trying to send a command to it, using a
76+
// (relatively) short timeout.
77+
//
78+
// NOTE: This appears to be how BouffaloLab software does it as well.
79+
//
80+
// NOTE: Modifying the timeout is mandatory for BL808;
81+
// see blisp.c:blisp_device_init()
82+
previous_timeout = device->serial_timeout;
83+
device->serial_timeout = 500;
84+
printf("Testing if we can skip the handshake...\n");
7985
ret = blisp_device_get_boot_info(device, &boot_info);
80-
if (ret != BLISP_OK) {
81-
fprintf(stderr, "Failed to get boot info, ret: %d\n", ret);
82-
return ret;
86+
device->serial_timeout = previous_timeout;
87+
88+
if (ret == BLISP_OK) {
89+
printf("Skipping handshake!\n");
90+
} else {
91+
printf("We can't; ignore the previous error.\n");
92+
printf("Sending a handshake...\n");
93+
ret = blisp_device_handshake(device, false);
94+
if (ret != BLISP_OK) {
95+
fprintf(stderr, "Failed to handshake with device, ret: %d\n", ret);
96+
return ret;
97+
}
98+
99+
printf("Handshake successful!\nGetting chip info...\n");
100+
ret = blisp_device_get_boot_info(device, &boot_info);
101+
if (ret != BLISP_OK) {
102+
fprintf(stderr, "Failed to get boot info, ret: %d\n", ret);
103+
return ret;
104+
}
83105
}
84106

85107
// TODO: Do we want this to print in big endian to match the output

0 commit comments

Comments
 (0)