Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ config ARDUINO_MAX_TONES
Specify the maximum number of tones that can be played simultaneously with tone().
If set to a negative value, the maximum number will be determined from the
system's digital pin configuration.

module = ARDUINO_API
module-str = arduino_api
source "subsys/logging/Kconfig.template.log_config"

endif
51 changes: 34 additions & 17 deletions libraries/Wire/Wire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#include <Wire.h>
#include <zephyr/sys/util_macro.h>

#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(arduino_wire, CONFIG_ARDUINO_API_LOG_LEVEL);

arduino::ZephyrI2C::ZephyrI2C(const struct device *i2c) : i2c_dev(i2c)
{
}
Expand Down Expand Up @@ -42,16 +45,26 @@ uint8_t arduino::ZephyrI2C::endTransmission(void) { // TODO for ADS1115

size_t arduino::ZephyrI2C::requestFrom(uint8_t address, size_t len,
bool stopBit) {
int ret = i2c_read(i2c_dev, rxRingBuffer.buffer, len, address);
/* Use stack-allocated buffer for reading */
uint8_t readbuff[sizeof(rxRingBuffer.buffer)];
if (len > sizeof(readbuff)) {
LOG_ERR("requested read length (%zu) exceeds buffer size (%zu)", len, sizeof(readbuff));
return 0;
}

int ret = i2c_read(i2c_dev, readbuff, len, address);
if (ret != 0)
{
printk("\n\nERR: i2c burst read fails\n\n\n");
LOG_ERR("burst read failed");
return 0;
}
ret = ring_buf_put(&rxRingBuffer.rb, rxRingBuffer.buffer, len);

/* Flush the receive buffer so another read() call returns the correct data */
flush();
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 (ret == 0)
{
printk("\n\nERR: buff put fails\n\n\n");
LOG_ERR("failed to put data in ring buffer");
return 0;
}
return len;
Expand All @@ -62,6 +75,10 @@ size_t arduino::ZephyrI2C::requestFrom(uint8_t address, size_t len) { // TODO fo
}

size_t arduino::ZephyrI2C::write(uint8_t data) { // TODO for ADS1115
if (usedTxBuffer >= sizeof(txBuffer)) {
LOG_ERR("tx buffer is full");
return 0;
}
txBuffer[usedTxBuffer++] = data;
return 1;
}
Expand All @@ -77,15 +94,13 @@ size_t arduino::ZephyrI2C::write(const uint8_t *buffer, size_t size) {

int arduino::ZephyrI2C::read() {
uint8_t buf[1];
if (ring_buf_peek(&rxRingBuffer.rb, buf, 1) > 0) {
int ret = ring_buf_get(&rxRingBuffer.rb, buf, 1);
if (ret == 0) {
printk("\n\nERR: buff empty\n\n\n");
return 0;
}
return (int)buf[0];

if(!ring_buf_get(&rxRingBuffer.rb, buf, 1)) {
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
Contributor 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
Contributor 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.

return -1; // no data available
}
return EXIT_FAILURE;

return (int)buf[0];
}

int arduino::ZephyrI2C::available() { // TODO for ADS1115
Expand All @@ -94,14 +109,16 @@ int arduino::ZephyrI2C::available() { // TODO for ADS1115

int arduino::ZephyrI2C::peek() {
uint8_t buf[1];
int bytes_read = ring_buf_peek(&rxRingBuffer.rb, buf, 1);
if (bytes_read == 0){
return 0;
}
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

}
return (int)buf[0];
}

void arduino::ZephyrI2C::flush() {}
void arduino::ZephyrI2C::flush() {
ring_buf_reset(&rxRingBuffer.rb);
}

void arduino::ZephyrI2C::onReceive(voidFuncPtrParamInt cb) {}
void arduino::ZephyrI2C::onRequest(voidFuncPtr cb) {}
Expand Down
Loading