Optimization and Bug Fix within SC16IS740RK

Hello, I am using the SC16IS740RK repo, by @rickkas7, within my project. It took a long time debugging issues and I have found 1 bug in the current implementation in master. I have also found 1 optimization that could further improve this repo.

The bug that is currently in this repo is that in the SC16IS740Base class, the peekByte variable is uint8 and thus means that the value must be between 0-255. This is not an issue, however the read function, which is what peek is using internally returns 0-255 or -1 if nothing is available. This is where the problem lies because if nothing is available it will return a -1 but since peekByte is uint8 casted it gets transformed into 255. This can cause people to incorrectly process bytes when calling peek because extra 0xFF bytes will seem to appear out of nowhere. I currently have a PR for this fix linked here: Fix peekByte casting to uint8_t by josephfemia · Pull Request #10 · rickkas7/SC16IS740RK · GitHub

The optimization that I have found comes in the form of writing. Within the SC16IS740Base class, the write function can be sped up to improve delays between writing chunks. Within the write function that accepts a pointer to the buffer and a size argument, it currently operates in a way where it will write the max amount of bytes for the wire, in my case 64 bytes, then waits until all 64 bytes are done writing before writing the next 64 byte chunk. This can cause delays between 64B chunks which can be problematic, especially when in a situation where a continuous chunk of written data is expected. This problem can be solved by only writing 1/2 of the amount of bytes for the wires internal max. This allows for half of the buffer to be written on the first pass through, then immediately on the second iteration it can write again without waiting because you only wrote half of the bytes. By the time the second iteration has finished, when it goes to check the register for space again the first half of bytes are already done writing and freed up. This optimization significantly improves writing speeds when passing in a buffer to write. I also have a PR for this optimization as well: Faster buffered writing in SC16IS740Base by josephfemia · Pull Request #8 · rickkas7/SC16IS740RK · GitHub. The PR also has images taken via a logic analyzer to further describe what I am talking about above.

Thank you, and I hope this helps people who end up stumbling across this.

2 Likes

Thanks for sharing this Joe! I’ll take a look.

1 Like