update
This commit is contained in:
@@ -34,40 +34,90 @@ void mcp3550_spi_init()
|
||||
}
|
||||
|
||||
|
||||
// int32_t mcp3550_read(uint8_t cs_pin)
|
||||
// {
|
||||
// uint8_t rx_buf[4] = {0}; // 25 bits fits in 4 bytes
|
||||
|
||||
// int64_t start = esp_timer_get_time(); // in microseconds
|
||||
// while (true) {
|
||||
// mcp23018_set_pin(MCP23018_DEV_HANDLE, cs_pin, 0); // CS LOW
|
||||
// vTaskDelay(pdMS_TO_TICKS(30)); // Wait before retrying
|
||||
// mcp23018_set_pin(MCP23018_DEV_HANDLE, cs_pin, 1); // CS HIGH
|
||||
|
||||
// vTaskDelay(pdMS_TO_TICKS(200)); // Wait before retrying
|
||||
|
||||
// mcp23018_set_pin(MCP23018_DEV_HANDLE, cs_pin, 0); // CS LOW
|
||||
// esp_rom_delay_us(1000); // Wait before retrying
|
||||
|
||||
// spi_transaction_t trans = {
|
||||
// .length = 25,
|
||||
// .rx_buffer = rx_buf,
|
||||
// };
|
||||
|
||||
// esp_err_t err = spi_device_transmit(mcp3550_handle, &trans);
|
||||
|
||||
// mcp23018_set_pin(MCP23018_DEV_HANDLE, cs_pin, 1); // CS HIGH
|
||||
|
||||
// if (err != ESP_OK) {
|
||||
// ESP_LOGE(TAG_MICS, "SPI transmit failed on CS pin %u", cs_pin);
|
||||
// return INT32_MIN;
|
||||
// }
|
||||
|
||||
// bool dr = (rx_buf[0] & 0x80) != 0;
|
||||
// if (!dr) break;
|
||||
|
||||
// if ((esp_timer_get_time() - start) > (MCP3550_TIMEOUT_MS * 1000)) {
|
||||
// ESP_LOGW(TAG_MICS, "Timeout waiting for DR=0 on CS pin %u", cs_pin);
|
||||
// return INT32_MIN;
|
||||
// }
|
||||
|
||||
// vTaskDelay(pdMS_TO_TICKS(10)); // Wait before retrying
|
||||
// }
|
||||
|
||||
// // Combine 22-bit result (drop DR + status bits)
|
||||
// uint32_t raw = ((rx_buf[0] & 0x3F) << 16) | (rx_buf[1] << 8) | rx_buf[2];
|
||||
|
||||
// // Sign-extend 22-bit value
|
||||
// int32_t value = raw;
|
||||
// if (value & (1 << 21)) value |= 0xFFC00000;
|
||||
|
||||
// return value;
|
||||
// }
|
||||
|
||||
int32_t mcp3550_read(uint8_t cs_pin)
|
||||
{
|
||||
uint8_t rx_buf[4] = {0}; // 25 bits fits in 4 bytes
|
||||
uint8_t rx_buf[4] = {0};
|
||||
uint32_t timeout_us = MCP3550_TIMEOUT_MS * 1000;
|
||||
int64_t start = esp_timer_get_time();
|
||||
|
||||
int64_t start = esp_timer_get_time(); // in microseconds
|
||||
while (true) {
|
||||
mcp23018_set_pin(MCP23018_DEV_HANDLE, cs_pin, 0); // CS LOW
|
||||
// Start conversion
|
||||
mcp23018_set_pin(MCP23018_DEV_HANDLE, cs_pin, 0); // CS LOW
|
||||
|
||||
spi_transaction_t trans = {
|
||||
.length = 25,
|
||||
.rx_buffer = rx_buf,
|
||||
};
|
||||
|
||||
esp_err_t err = spi_device_transmit(mcp3550_handle, &trans);
|
||||
|
||||
mcp23018_set_pin(MCP23018_DEV_HANDLE, cs_pin, 1); // CS HIGH
|
||||
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG_MICS, "SPI transmit failed on CS pin %u", cs_pin);
|
||||
// Wait until MISO/SDO goes LOW = DR ready
|
||||
while (gpio_get_level(MCP3550_MISO_GPIO)) {
|
||||
if ((esp_timer_get_time() - start) > timeout_us) {
|
||||
ESP_LOGW(TAG_MICS, "Timeout waiting for MISO=0 on CS %u", cs_pin);
|
||||
mcp23018_set_pin(MCP23018_DEV_HANDLE, cs_pin, 1); // CS HIGH
|
||||
return INT32_MIN;
|
||||
}
|
||||
|
||||
bool dr = (rx_buf[0] & 0x80) != 0;
|
||||
if (!dr) break;
|
||||
|
||||
if ((esp_timer_get_time() - start) > (MCP3550_TIMEOUT_MS * 1000)) {
|
||||
ESP_LOGW(TAG_MICS, "Timeout waiting for DR=0 on CS pin %u", cs_pin);
|
||||
return INT32_MIN;
|
||||
}
|
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(10)); // Wait before retrying
|
||||
esp_rom_delay_us(10); // micro delay
|
||||
}
|
||||
|
||||
// Combine 22-bit result (drop DR + status bits)
|
||||
// Data is ready, do full SPI read
|
||||
spi_transaction_t trans = {
|
||||
.length = 25, // 25 bits
|
||||
.rx_buffer = rx_buf,
|
||||
};
|
||||
esp_err_t err = spi_device_transmit(mcp3550_handle, &trans);
|
||||
|
||||
mcp23018_set_pin(MCP23018_DEV_HANDLE, cs_pin, 1); // CS HIGH to start next conversion
|
||||
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG_MICS, "SPI transmit failed");
|
||||
return INT32_MIN;
|
||||
}
|
||||
|
||||
// Extract 22-bit result
|
||||
uint32_t raw = ((rx_buf[0] & 0x3F) << 16) | (rx_buf[1] << 8) | rx_buf[2];
|
||||
|
||||
// Sign-extend 22-bit value
|
||||
@@ -78,6 +128,7 @@ int32_t mcp3550_read(uint8_t cs_pin)
|
||||
}
|
||||
|
||||
|
||||
|
||||
float mcp3550_to_voltage(int32_t value, float vref)
|
||||
{
|
||||
return ((float)value / (1 << 21)) * vref;
|
||||
@@ -107,10 +158,10 @@ void log_mics_adc_values(mics_adc_data_t *data)
|
||||
{
|
||||
ESP_LOGI(TAG_MICS,
|
||||
"ADC Readings:\n"
|
||||
" NH3 -> Raw: %8d, Voltage: %7.6f V\n"
|
||||
" CO -> Raw: %8d, Voltage: %7.6f V\n"
|
||||
" NO2 -> Raw: %8d, Voltage: %7.6f V\n"
|
||||
" UVC -> Raw: %8d, Voltage: %7.6f V",
|
||||
" NH3 -> Raw: %ld, Voltage: %f V\n"
|
||||
" CO -> Raw: %ld, Voltage: %f V\n"
|
||||
" NO2 -> Raw: %ld, Voltage: %f V\n"
|
||||
" UVC -> Raw: %ld, Voltage: %f V",
|
||||
data->raw_nh3, data->volts_nh3,
|
||||
data->raw_co, data->volts_co,
|
||||
data->raw_no2, data->volts_no2,
|
||||
|
Reference in New Issue
Block a user