Skip to content

Possible bug in arduino_read function of "MKRWANFWUpdate_standalone" example sketch #134

@CFTechno

Description

@CFTechno

The arduino_read function uses the available() routine but it could happen that the STM32 did not return a reply yet.

This problem is what happened when using it on a ESP32-S3 connected to a STM32F4.

When looking at

https://github.com/facchinm/stm32flash/blob/1d155f293cac0f825c73c39b4d5344113bb67052/serial_posix.c#L270C1-L292C2

static port_err_t serial_posix_read(struct port_interface *port, void *buf,
				     size_t nbyte)
{
	serial_t *h;
	ssize_t r;
	uint8_t *pos = (uint8_t *)buf;

	h = (serial_t *)port->private;
	if (h == NULL)
		return PORT_ERR_UNKNOWN;

	while (nbyte) {
		**r = read(h->fd, pos, nbyte);
		if (r == 0)
			return PORT_ERR_TIMEDOUT;**
		if (r < 0)
			return PORT_ERR_UNKNOWN;

		nbyte -= r;
		pos += r;
	}
	return PORT_ERR_OK;
}

This code does two things: it reads as many bytes as available directly into a buffer and checks if the total amount read is the same as the expected amount and if not continues to do so. It will also return the timeout error value if the amount read is 0.

I would therefore suggest to replace available() with readBytes() or more precise use the above quoted while loop and use readBytes().

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: documentationRelated to documentation for the projecttype: imperfectionPerceived defect in any part of project

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions