59 #define debug(fmt, args ...) do { hal.console->printf("IOMCU: " fmt "\n", ## args); } while(0) 66 bool AP_IOMCU::upload_fw(
const char *filename)
72 uart.begin(115200, 256, 256);
77 for (uint8_t i = 0; i < 15; i++) {
86 debug(
"IO update failed sync");
91 ret = get_info(INFO_BL_REV, bl_rev);
94 debug(
"Err: failed to contact bootloader");
97 if (bl_rev > BL_REV) {
98 debug(
"Err: unsupported bootloader revision %u",
unsigned(bl_rev));
101 debug(
"found bootloader revision: %u",
unsigned(bl_rev));
105 debug(
"erase failed");
109 ret = program(fw_size);
111 debug(
"program failed");
116 ret = verify_rev2(fw_size);
118 ret = verify_rev3(fw_size);
122 debug(
"verify failed");
129 debug(
"reboot failed");
133 debug(
"update complete");
144 bool AP_IOMCU::recv_byte_with_timeout(uint8_t *c, uint32_t timeout_ms)
148 int16_t
v = uart.read();
162 bool AP_IOMCU::recv_bytes(uint8_t *p, uint32_t
count)
167 ret = recv_byte_with_timeout(p++, 5000);
179 void AP_IOMCU::drain(
void)
185 ret = recv_byte_with_timeout(&c, 40);
192 bool AP_IOMCU::send(uint8_t c)
194 if (uart.write(c) != 1) {
203 bool AP_IOMCU::send(
const uint8_t *p, uint32_t count)
220 bool AP_IOMCU::get_sync(uint32_t timeout_ms)
225 ret = recv_byte_with_timeout(c, timeout_ms);
230 ret = recv_byte_with_timeout(c + 1, timeout_ms);
235 if ((c[0] != PROTO_INSYNC) || (c[1] != PROTO_OK)) {
236 debug(
"bad sync 0x%02x,0x%02x", c[0], c[1]);
251 for (uint32_t i = 0; i < (PROG_MULTI_MAX + 6); i++) {
255 send(PROTO_GET_SYNC);
263 bool AP_IOMCU::get_info(uint8_t param, uint32_t &val)
267 send(PROTO_GET_DEVICE);
271 ret = recv_bytes((uint8_t *)&val,
sizeof(val));
282 bool AP_IOMCU::erase()
285 send(PROTO_CHIP_ERASE);
287 return get_sync(10000);
293 bool AP_IOMCU::program(uint32_t fw_size)
302 debug(
"programming %u bytes...", (
unsigned)fw_size);
304 while (sent < fw_size) {
306 uint32_t n = fw_size - sent;
307 if (n > PROG_MULTI_MAX) {
311 send(PROTO_PROG_MULTI);
316 ret = get_sync(1000);
318 debug(
"Failed at %u", (
unsigned)sent);
331 bool AP_IOMCU::verify_rev2(uint32_t fw_size)
339 send(PROTO_CHIP_VERIFY);
346 while (sent < fw_size) {
348 uint32_t n = fw_size - sent;
353 send(PROTO_READ_MULTI);
358 for (uint8_t i = 0; i<n; i++) {
360 ret = recv_byte_with_timeout(&c, 5000);
362 debug(
"%d: got %d waiting for bytes", sent + i, ret);
365 if (c != fw[sent+i]) {
366 debug(
"%d: got 0x%02x expected 0x%02x", sent + i, c, fw[sent+i]);
375 debug(
"timeout waiting for post-verify sync");
386 bool AP_IOMCU::verify_rev3(uint32_t fw_size_local)
390 uint32_t bytes_read = 0;
392 uint32_t fw_size_remote;
393 uint8_t fill_blank = 0xff;
397 ret = get_info(INFO_FLASH_SIZE, fw_size_remote);
401 debug(
"could not read firmware size");
406 while (bytes_read < fw_size_local) {
407 uint32_t n = fw_size_local - bytes_read;
413 sum =
crc_crc32(sum, &fw[bytes_read], n);
419 while (bytes_read < fw_size_remote) {
428 ret = recv_bytes((uint8_t *)(&crc),
sizeof(crc));
430 debug(
"did not receive CRC checksum");
436 debug(
"CRC wrong: received: 0x%x, expected: 0x%x", (
unsigned)crc, (
unsigned)sum);
448 bool AP_IOMCU::reboot()
457 #endif // HAL_WITH_IO_MCU uint32_t crc_crc32(uint32_t crc, const uint8_t *buf, uint32_t size)
#define debug(fmt, args ...)
virtual void delay(uint16_t ms)=0
void sync(void)
POSIX Sync all pending file changes and metadata on ALL files.
virtual void delay_microseconds(uint16_t us)=0
static const uint8_t * find_file(const char *name, uint32_t &size)
AP_HAL::Scheduler * scheduler