-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
INA237 Sensor Bug Fixes and Enhancements (with unit tests) #60717
Changes from all commits
864cb9a
e2eaa61
db7f60a
4a34ccd
f39fca1
50ed5a3
35524b0
d5201e1
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 |
---|---|---|
|
@@ -17,11 +17,13 @@ | |
#include <zephyr/drivers/sensor.h> | ||
|
||
#define INA237_REG_CONFIG 0x00 | ||
#define INA237_CFG_HIGH_PRECISION BIT(4) | ||
|
||
#define INA237_REG_ADC_CONFIG 0x01 | ||
#define INA237_REG_CALIB 0x02 | ||
#define INA237_REG_SHUNT_VOLT 0x04 | ||
#define INA237_REG_BUS_VOLT 0x05 | ||
#define INA237_REG_MASK 0x06 | ||
#define INA237_REG_DIETEMP 0x06 | ||
#define INA237_REG_CURRENT 0x07 | ||
#define INA237_REG_POWER 0x08 | ||
#define INA237_REG_ALERT 0x0B | ||
|
@@ -37,9 +39,13 @@ | |
|
||
struct ina237_data { | ||
const struct device *dev; | ||
uint16_t current; | ||
int16_t current; | ||
uint16_t bus_voltage; | ||
uint32_t power; | ||
int16_t die_temp; | ||
#ifdef CONFIG_INA237_VSHUNT | ||
int16_t shunt_voltage; | ||
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. This can be ifdef protected, so it doesn't take space in the struct unless enabled. 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. Good point, done and pushed. |
||
#endif | ||
enum sensor_channel chan; | ||
struct ina23x_trigger trigger; | ||
}; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,59 @@ compatible: "ti,ina230" | |
include: ti,ina23x-common.yaml | ||
|
||
properties: | ||
config: | ||
type: int | ||
deprecated: true | ||
default: 0x0000 | ||
description: | | ||
Value of the configuration register | ||
e.g shunt voltage and bus voltage ADC conversion | ||
times, ADC and averaging, and ADC operating mode. | ||
|
||
alert-config: | ||
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. Please add 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. Fixed in the latest changes that I've pushed. |
||
type: int | ||
description: Diag alert register, default matches the power-on reset value | ||
|
||
adc-mode: | ||
type: string | ||
description: | | ||
ADC channel conversion configuration. | ||
Default is the power-on reset value. | ||
default: "Bus and shunt voltage continuous" | ||
enum: | ||
- "Shutdown single shot" | ||
- "Shunt Voltage single shot" | ||
- "Bus Voltage single shot" | ||
- "Bus and Shunt Voltage single shot" | ||
- "Shutdown continuous" | ||
- "Shunt voltage continuous" | ||
- "Bus voltage continuous" | ||
- "Bus and shunt voltage continuous" | ||
|
||
vbus-conversion-time-us: | ||
type: int | ||
description: | | ||
Vbus conversion time in microseconds. | ||
Default is the power-on reset value. | ||
default: 1100 | ||
enum: [140, 204, 332, 588, 1100, 2116, 4156, 8244] | ||
|
||
vshunt-conversion-time-us: | ||
type: int | ||
description: | | ||
Vshunt conversion time in microseconds. | ||
Default is the power-on reset value. | ||
default: 1100 | ||
enum: [140, 204, 332, 588, 1100, 2116, 4156, 8244] | ||
|
||
avg-count: | ||
type: int | ||
description: | | ||
Number of samples to average (applies to all inputs). | ||
Default is the power-on reset value. | ||
default: 1 | ||
enum: [1, 4, 16, 64, 128, 256, 512, 1024] | ||
|
||
mask: | ||
type: int | ||
default: 0 | ||
|
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.
Doesn't ina230 have this same problem?
INA23X_CURRENT_SIGN_BIT
looks to be unused after this change.The existing code is broken not just on ESP32-S3, but on all processors. The flaw is not understanding the integer promotions concept in the C standard. While
data->current
is 16-bit, the expression~data->current
does NOT mean to invert bits of the 16-bit value! The first step is promotedata->current
from unsigned 16-bit to a signed 32-bit integer. Then this 32-bit integer (the upper 16-bits are of course zero) will be inverted. That's not what was desired. What would produce the current result would be to invert the 16-bit value, add 1 to the still 16-bit value, and then take the 16-bit result and promote it to 32-bits at the end. But 16->32 conversion happens first, not last.Anyway, this code is somewhat inefficient, actually performing the multiply to invert the sign. Better code to take absolute value:
You can see the results of different methods: original, your method, and the above examples
https://godbolt.org/z/qz3vsG4M6
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.
Thanks for the Compiler Explorer link. Probably just go with abs() in the negative case given the results.
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.
Yes, it does have the same issue code issue, but I do not have INA230 hardware to test, so I didn't want to change the code and release it untested. Seems like a relatively safe thing to change and verify by inspection and/or find a volunteer to test it.
Update: I have modified the INA230 code as well and requested testing on Discord, but no takers so far
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.
FYI, I have added a unit test for the INA230 and tested the change, so should be fixed.
Changes have been included in the latest push to the PR. PTAL when you have a chance.