From 1a41e40714d5e401cfd4f9fd37fc28d2697057f0 Mon Sep 17 00:00:00 2001 From: Gwendal Date: Wed, 4 Dec 2019 14:55:30 +0100 Subject: [PATCH 1/2] Serial write Linux: patch for segger chip Patch to workaround faulty segger chip of nordic kit. --- lib/platform/linux/serial.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/lib/platform/linux/serial.c b/lib/platform/linux/serial.c index 36639e6..d38623b 100644 --- a/lib/platform/linux/serial.c +++ b/lib/platform/linux/serial.c @@ -21,6 +21,17 @@ static int fd = -1; +// This define modifies the write function to avoid issues with +// Nordic Evaluation Kit connected through USB with a faulty +// segger chip (for the USB to serial conversion). In fact the +// declared USB buffer size seems wrong (512 instead of 64 bytes) +// resulting in truncated packets if the lib send bigger packets than +// 64 bytes. Splitting them before sending seems to workaround the issue +// If not in this situation, the following line can be commented +// Having it all the time even if accessing a real serial device shouldn't +// impact performances +#define PATCH_SEGGER_CHIP + static int set_interface_attribs(int fd, unsigned long bitrate, int parity) { struct termios tty; @@ -198,6 +209,7 @@ int Serial_read(unsigned char * c, unsigned int timeout_ms) } } +#define WRITE_CHUNK_SIZE 64 int Serial_write(const unsigned char * buffer, unsigned int buffer_size) { if (fd < 0) @@ -206,5 +218,25 @@ int Serial_write(const unsigned char * buffer, unsigned int buffer_size) return -1; } +#ifdef PATCH_SEGGER_CHIP + unsigned int written = 0; + // Send buffer in 64 bytes chunks + while (buffer_size > 0) + { + unsigned int size = buffer_size > WRITE_CHUNK_SIZE ? WRITE_CHUNK_SIZE : buffer_size; + unsigned int partial_written; + + partial_written = write(fd, buffer + written, size); + if (partial_written < 0) + { + return partial_written; + } + + written += partial_written; + buffer_size -= partial_written; + } + return written; +#else return write(fd, buffer, buffer_size); +#endif //PATCH_SEGGER_CHIP } From 8886923df535dae93c50c0549923d64580bf7c65 Mon Sep 17 00:00:00 2001 From: Gwendal <47559821+GwendalRaoul@users.noreply.github.com> Date: Wed, 4 Dec 2019 15:28:11 +0100 Subject: [PATCH 2/2] Update wrong type --- lib/platform/linux/serial.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/platform/linux/serial.c b/lib/platform/linux/serial.c index d38623b..175772b 100644 --- a/lib/platform/linux/serial.c +++ b/lib/platform/linux/serial.c @@ -224,9 +224,7 @@ int Serial_write(const unsigned char * buffer, unsigned int buffer_size) while (buffer_size > 0) { unsigned int size = buffer_size > WRITE_CHUNK_SIZE ? WRITE_CHUNK_SIZE : buffer_size; - unsigned int partial_written; - - partial_written = write(fd, buffer + written, size); + int partial_written = write(fd, buffer + written, size); if (partial_written < 0) { return partial_written;