Skip to content

Wire: improve code, add safety checks, add basic logging#171

Open
JZimnol wants to merge 6 commits intozephyrproject-rtos:nextfrom
Hacod-tech:wire_lib_improvements
Open

Wire: improve code, add safety checks, add basic logging#171
JZimnol wants to merge 6 commits intozephyrproject-rtos:nextfrom
Hacod-tech:wire_lib_improvements

Conversation

@JZimnol
Copy link
Copy Markdown

@JZimnol JZimnol commented Mar 11, 2026

This PR fixes and changes a few things related to the wire port of the Arduino library. All changes are done in separate commits for readability. The most important functions changed are the arduino::ZephyrI2C::requestFrom() and arduino::ZephyrI2C::read().

Additionally, the ARDUINO_API log module/levels have been added enabling an user to control the log level of logs in this repository. As an example, printk() calls have been switched with the LOG_* API.

@soburi
Copy link
Copy Markdown
Member

soburi commented Mar 11, 2026

We are considering integrating this part with Arduino, so could you please wait a little while?

@Mierunski
Copy link
Copy Markdown

We are using this repository as our intent is to use Arduino libraries within Zephyr application.
How we understand is that https://github.com/arduino/ArduinoCore-zephyr provides flow for using Zephyr functionalities from Arduino application, we did not manage to integrate it with existing Zephyr application (correct me if I'm wrong)

Therefore we would like to have it in this repository

@soburi
Copy link
Copy Markdown
Member

soburi commented Mar 12, 2026

We are using this repository as our intent is to use Arduino libraries within Zephyr application. How we understand is that https://github.com/arduino/ArduinoCore-zephyr provides flow for using Zephyr functionalities from Arduino application, we did not manage to integrate it with existing Zephyr application (correct me if I'm wrong)

Therefore we would like to have it in this repository

Yes, we are aware of the issue.
The biggest challenge is that the ArduinoCore-API is LGPL2-licensed and incompatible with Zephyr, which needs to be resolved.
We are considering reimplementing it in Rust, but we're still in the early stages.

@DhruvaG2000
Copy link
Copy Markdown
Member

We are considering integrating this part with Arduino, so could you please wait a little while?

@soburi please could you elaborate on what you mean by this part? Are you saying these fixes are already part of the Arduino fork or you're going to send a PR with these fixes there first?
Just trying to understand

return (int)buf[0];

if(!ring_buf_get(&rxRingBuffer.rb, buf, 1)) {
printk("\n\nERR: buff empty\n\n\n");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oo many unnecessary \n's .
Just keep it as printk("ERR: buff empty");

Also, elaborate more on the fix itself in the commit "Wire: fix the behavior of the arduino::ZephyrI2C::read()"

/* Use stack-allocated buffer for reading */
uint8_t readbuff[sizeof(rxRingBuffer.buffer)];
if (len > sizeof(readbuff)) {
printk("\n\nERR: requested length (%zu) exceeds buffer size (%zu) \n\n\n",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, too many unnecessary \n's ...

/* Flush the receive buffer so another read() call returns the correct data */
flush();
ret = ring_buf_put(&rxRingBuffer.rb, rxRingBuffer.buffer, len);
ret = ring_buf_put(&rxRingBuffer.rb, readbuff, len);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you elaborate more on the reason in the commit "Wire: don't use rxRingBuffer.buffer directly for i2c_read() calls"

}
if (!ring_buf_peek(&rxRingBuffer.rb, buf, 1)){
// No data available
return -1;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The commit "Wire: fix the behavior of the arduino::ZephyrI2C::peek()" could also use some more explanation in it's body


if(!ring_buf_get(&rxRingBuffer.rb, buf, 1)) {
printk("\n\nERR: buff empty\n\n\n");
LOG_ERR("buffer is empty");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah in the last commit you're changing everything to LOG_ERR. Fine, then you need to drop the printk changes from all your earlier commits because then it's just noise.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DhruvaG2000 would you like me to squash all those commits into one with a proper name and description then? In the first commits I've been doing some fixes only with the printks being unchanged, that's why it might look misleading. I can squash everything if you want.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JZimnol no, don't squash. I like how you've partitioned all the fixes. I would just like to see an explanation for each in their own commits, and also those commits need to contain only the fix, you can just remove the printk's from those places, or else just use LOG_ERR in those commits itself.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DhruvaG2000 I've added required changes + descriptions to some of the commits.

JZimnol added 6 commits March 25, 2026 11:29
Implemented flush() function to clear the rx buffer. The buffer needs to be flushed so the next read() call returns the proper data.

Signed-off-by: Jakub Zimnol <zimnol.jakub@gmail.com>
Removed unecessary ring buffer peek. Additionally added negative value as a return value in case of an error.

Signed-off-by: Jakub Zimnol <zimnol.jakub@gmail.com>
Removed a bug from requestFrom() function that caused ring buffer corruption by using rxRingBuffer.buffer directly in i2c_read() calls.

Signed-off-by: Jakub Zimnol <zimnol.jakub@gmail.com>
Added a negative return value to peek() in case there's no data to read.

Signed-off-by: Jakub Zimnol <zimnol.jakub@gmail.com>
Signed-off-by: Jakub Zimnol <zimnol.jakub@gmail.com>
Signed-off-by: Jakub Zimnol <zimnol.jakub@gmail.com>
@JZimnol JZimnol force-pushed the wire_lib_improvements branch from 73880fb to 80c15be Compare March 25, 2026 11:22
@JZimnol JZimnol requested a review from DhruvaG2000 March 25, 2026 13:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants