mirror of
https://github.com/esphome/esphome.git
synced 2026-09-28 15:30:21 +00:00
[mcp3204] Read a conversion in one SPI transaction (#19423)
Co-authored-by: Claude Opus 5 <noreply@anthropic.com> Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Co-authored-by: J. Nick Koston <nick@koston.org> Co-authored-by: J. Nick Koston <nick@home-assistant.io>
This commit is contained in:
co-authored by
Claude Opus 5
pre-commit-ci-lite[bot]
J. Nick Koston
J. Nick Koston
parent
16757a84e6
commit
057bf88e4a
@@ -18,19 +18,18 @@ void MCP3204::dump_config() {
|
||||
}
|
||||
|
||||
float MCP3204::read_data(uint8_t pin, bool differential) {
|
||||
uint8_t command, b0, b1;
|
||||
|
||||
command = (1 << 6) | // start bit
|
||||
((differential ? 0 : 1) << 5) | // single or differential bit
|
||||
((pin & 0x07) << 2); // pin
|
||||
const uint8_t command = (1 << 6) | // start bit
|
||||
((differential ? 0 : 1) << 5) | // single or differential bit
|
||||
((pin & 0x07) << 2); // pin
|
||||
// One full-duplex transaction: command out, 12-bit result back in bytes 1 and 2.
|
||||
// Word aligned so ESP-IDF DMA uses the buffer in place; only ESP32-P4 also checks the length and bounces.
|
||||
alignas(4) uint8_t buffer[3] = {command, 0x00, 0x00};
|
||||
|
||||
this->enable();
|
||||
this->transfer_byte(command);
|
||||
b0 = this->transfer_byte(0x00);
|
||||
b1 = this->transfer_byte(0x00);
|
||||
this->transfer_array(buffer, sizeof(buffer));
|
||||
this->disable();
|
||||
|
||||
uint16_t digital_value = encode_uint16(b0, b1) >> 4;
|
||||
uint16_t digital_value = encode_uint16(buffer[1], buffer[2]) >> 4;
|
||||
return float(digital_value) / 4096.000f * this->reference_voltage_; // in V
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user