39 #include <px4_platform_common/px4_config.h> 40 #include <px4_platform_common/time.h> 42 #include <sys/types.h> 56 #include <nuttx/arch.h> 62 #include <board_config.h> 78 const char *filename = NULL;
81 #ifndef PX4IO_SERIAL_DEVICE 82 #error Must define PX4IO_SERIAL_DEVICE in board configuration to support firmware upload 86 for (
unsigned i = 0; filenames[i] !=
nullptr; i++) {
87 _fw_fd = open(filenames[i], O_RDONLY);
90 log(
"failed to open %s", filenames[i]);
94 log(
"using firmware from %s", filenames[i]);
95 filename = filenames[i];
99 if (filename == NULL) {
100 log(
"no firmware found");
106 _io_fd = open(PX4IO_SERIAL_DEVICE, O_RDWR);
109 log(
"could not open interface");
114 struct termios t_original;
115 tcgetattr(
_io_fd, &t_original);
120 cfsetspeed(&t, 115200);
121 tcsetattr(
_io_fd, TCSANOW, &t);
124 for (
int i = 0; i < 15; i++) {
137 log(
"bootloader not responding");
138 tcsetattr(
_io_fd, TCSANOW, &t_original);
146 if (stat(filename, &st) != 0) {
147 log(
"Failed to stat %s - %d\n", filename, (
int)errno);
148 tcsetattr(
_io_fd, TCSANOW, &t_original);
154 fw_size = st.st_size;
157 tcsetattr(
_io_fd, TCSANOW, &t_original);
164 for (
unsigned retries = 0; retries < 1; retries++) {
166 log(
"retrying update...");
171 log(
"bootloader not responding");
172 tcsetattr(
_io_fd, TCSANOW, &t_original);
183 log(
"found bootloader revision: %d",
bl_rev);
186 log(
"found unsupported bootloader revision %d, exiting",
bl_rev);
187 tcsetattr(
_io_fd, TCSANOW, &t_original);
204 log(
"program failed");
217 log(
"verify failed");
224 log(
"reboot failed");
225 tcsetattr(
_io_fd, TCSANOW, &t_original);
231 log(
"update complete");
238 tcsetattr(
_io_fd, TCSANOW, &t_original);
246 up_udelay(100 * 1000);
254 struct pollfd fds[1];
257 fds[0].events = POLLIN;
260 int ret = ::poll(&fds[0], 1, timeout);
264 log(
"poll timeout %d", ret);
271 log(
"recv_bytes 0x%02x", c);
307 log(
"discard 0x%02x", c);
318 log(
"send 0x%02x", c);
363 log(
"bad sync 0x%02x,0x%02x", c[0], c[1]);
394 ret =
recv_bytes((uint8_t *)&val,
sizeof(val));
419 ret =
read(fd, buf, n);
420 }
while (ret == -1 && retries++ < 100);
423 printf(
"read of %u bytes needed %u retries\n",
442 log(
"Can't allocate program buffer");
446 ASSERT((fw_size & 3) == 0);
449 log(
"programming %u bytes...", (
unsigned)fw_size);
451 ret = lseek(
_fw_fd, 0, SEEK_SET);
453 while (sent < fw_size) {
455 size_t n = fw_size - sent;
463 if (count != (ssize_t)n) {
464 log(
"firmware read of %u bytes at %u failed -> %d errno %d",
477 send(file_buf, count);
500 lseek(
_fw_fd, 0, SEEK_SET);
510 while (sent < fw_size) {
512 size_t n = fw_size - sent;
514 if (n >
sizeof(file_buf)) {
515 n =
sizeof(file_buf);
520 if (count != (ssize_t)n) {
521 log(
"firmware read of %u bytes at %u failed -> %d errno %d",
538 ASSERT((count % 4) == 0);
544 for (ssize_t i = 0; i < count; i++) {
550 log(
"%d: got %d waiting for bytes", sent + i, ret);
554 if (c != file_buf[i]) {
555 log(
"%d: got 0x%02x expected 0x%02x", sent + i, c, file_buf[i]);
563 log(
"timeout waiting for post-verify sync");
578 uint32_t bytes_read = 0;
580 uint32_t fw_size_remote;
581 uint8_t fill_blank = 0xff;
584 lseek(
_fw_fd, 0, SEEK_SET);
590 log(
"could not read firmware size");
595 while (bytes_read < fw_size_local) {
596 size_t n = fw_size_local - bytes_read;
598 if (n >
sizeof(file_buf)) {
599 n =
sizeof(file_buf);
604 if (count != (ssize_t)n) {
605 log(
"firmware read of %u bytes at %u failed -> %d errno %d",
607 (
unsigned)bytes_read,
623 sum = crc32part((uint8_t *)&file_buf,
sizeof(file_buf), sum);
629 while (bytes_read < fw_size_remote) {
630 sum = crc32part(&fill_blank,
sizeof(fill_blank), sum);
631 bytes_read +=
sizeof(fill_blank);
638 ret =
recv_bytes((uint8_t *)(&crc),
sizeof(crc));
641 log(
"did not receive CRC checksum");
648 log(
"did not receive CRC checksum");
654 log(
"CRC wrong: received: %d, expected: %d", crc, sum);
667 up_udelay(100 * 1000);
673 up_udelay(10 * 1000);
int get_info(int param, uint32_t &val)
int verify_rev2(size_t fw_size)
int verify_rev3(size_t fw_size)
int get_sync(unsigned timeout=40)
supported bootloader protocol
bootloader protocol revision
uint32_t bl_rev
bootloader revision
static void read(bootloader_app_shared_t *pshared)
int program(size_t fw_size)
int recv_byte_with_timeout(uint8_t *c, unsigned timeout)
Firmware uploader definitions for PX4IO.
max firmware size in bytes
protocol max is 255, must be multiple of 4
static void write(bootloader_app_shared_t *pshared)
int recv_bytes(uint8_t *p, unsigned count)
int upload(const char *filenames[])
static int read_with_retry(int fd, void *buf, size_t n)
void log(const char *fmt,...)