-
Notifications
You must be signed in to change notification settings - Fork 69
Wire: improve code, add safety checks, add basic logging #171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
147d04f
6cf79a4
9ebf8ba
939a22d
bba50c7
80c15be
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
| { | ||
| } | ||
|
|
@@ -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); | ||
| 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; | ||
|
|
@@ -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; | ||
| } | ||
|
|
@@ -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"); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah in the last commit you're changing everything to
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
|
@@ -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; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) {} | ||
|
|
||
There was a problem hiding this comment.
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"