From faaffd78d952dfb2620fa84e7829d0bfed503277 Mon Sep 17 00:00:00 2001 From: Mateusz Lubecki Date: Mon, 3 Oct 2022 22:51:04 +0200 Subject: [PATCH] max31865 pooling and state machine. warning! it seems that JTAG hangs up after few seconds of debugging --- include/etc/pwr_save_configuration.h | 2 +- src/pwr_save.c | 6 +- system/src/drivers/max31865.c | 90 ++++++++++++++++++++++------ 3 files changed, 76 insertions(+), 22 deletions(-) diff --git a/include/etc/pwr_save_configuration.h b/include/etc/pwr_save_configuration.h index b6ef45e..c5ae29e 100644 --- a/include/etc/pwr_save_configuration.h +++ b/include/etc/pwr_save_configuration.h @@ -35,6 +35,6 @@ /** * Do not uncomment this on production devices */ -//#define INHIBIT_CUTOFF +#define INHIBIT_CUTOFF #endif /* INCLUDE_PWR_SAVE_CONFIGURATION_H_ */ diff --git a/src/pwr_save.c b/src/pwr_save.c index 2ba0f19..c92b6f2 100644 --- a/src/pwr_save.c +++ b/src/pwr_save.c @@ -658,9 +658,9 @@ void pwr_save_pooling_handler(const config_data_mode_t * config, const config_da vbatt = 0xFFFFu; } -// #ifdef INHIBIT_CUTOFF -// vbatt = 0xFFFFu; -// #endif + #ifdef INHIBIT_CUTOFF + vbatt = 0xFFFFu; + #endif if (vbatt > PWR_SAVE_STARTUP_RESTORE_VOLTAGE_DEF) { pwr_save_currently_cutoff = 0; diff --git a/system/src/drivers/max31865.c b/system/src/drivers/max31865.c index 7e4b01e..fef5aea 100644 --- a/system/src/drivers/max31865.c +++ b/system/src/drivers/max31865.c @@ -40,7 +40,7 @@ uint8_t max31865_vbias = 0; uint8_t max31865_conversion_mode = 0; /** - * + * Set to one and send config register to trigger single measurement */ uint8_t max31865_start_singleshot = 0; @@ -73,6 +73,11 @@ uint8_t max31865_buffer[3] = {0u}; */ uint8_t max31865_ok = 0; +/** + * Last raw result read from MAX + */ +uint16_t max31865_raw_result = 0; + /** * Function generates a content of configuration register basing on */ @@ -107,9 +112,13 @@ static void max31865_request_registers(void) { else { max31865_current_state = MAX_ERROR; } + + if (result != SPI_OK) { + max31865_current_state = MAX_ERROR; + } } -static void max31865_send_config_measurement(void) { +static void max31865_send_config_register(void) { uint8_t result = 0; // check if SPI is busy now @@ -123,6 +132,10 @@ static void max31865_send_config_measurement(void) { else { max31865_current_state = MAX_ERROR; } + + if (result != SPI_OK) { + max31865_current_state = MAX_ERROR; + } } void max31865_init(uint8_t rdt_type) { @@ -139,26 +152,14 @@ void max31865_init(uint8_t rdt_type) { // set filter to 50Hz max31865_filter_select = 1; - max31865_vbias = 1; + max31865_vbias = 0; - max31865_conversion_mode = 1; + max31865_conversion_mode = 0; -// max31865_buffer[0] = 0x80; -// max31865_buffer[1] = max31865_get_config_register(); -// -// spi_tx_data(1, SPI_TX_FROM_EXTERNAL, max31865_buffer, 2); - - max31865_send_config_measurement(); + max31865_send_config_register(); spi_wait_for_comms_done(); -// // read adres of configuation register -// max31865_buffer[0] = 0x00; -// max31865_buffer[1] = 0x00; -// -// // read data for verifiaction -// spi_rx_tx_data(1, SPI_TX_FROM_EXTERNAL, SPI_USE_INTERNAL_RX_BUF, max31865_buffer, 30, 1); - max31865_request_registers(); spi_wait_for_comms_done(); @@ -167,6 +168,8 @@ void max31865_init(uint8_t rdt_type) { if (rx_data[0] == max31865_get_config_register()) { max31865_ok = 1; + + max31865_current_state = MAX_INITIALIZED; } else { max31865_ok = 0; @@ -179,6 +182,8 @@ void max31865_init(uint8_t rdt_type) { */ void max31865_pool(void) { + uint8_t * result_ptr; + switch (max31865_current_state) { case MAX_IDLE: // MAX31865 is powered up but not initialized @@ -189,23 +194,72 @@ void max31865_pool(void) { max31865_init(MAX_4WIRE); } - max31865_current_state = MAX_INITIALIZED; + if (max31865_ok == 1) { + max31865_current_state = MAX_INITIALIZED; + } + break; case MAX_INITIALIZED: // initialized and ready to start measurement + max31865_current_state = MAX_MEASUREMENT_STARTED; + max31865_start_singleshot = 1; + max31865_vbias = 1; + + // this function may change 'max31865_current_state' internally due to errors + max31865_send_config_register(); + + // disable VBIAS to reduce power consumption + max31865_vbias = 0; + + max31865_start_singleshot = 0; break; case MAX_ERROR: + // go back to idle in case of any error + max31865_current_state = MAX_IDLE; break; case MAX_MEASUREMENT_STARTED: + // measurement has been started before, so now it's time to request for results + max31865_request_registers(); + + max31865_current_state = MAX_REGISTER_REQUESTED; + break; case MAX_REGISTER_REQUESTED: + // results shall be available + max31865_current_state = MAX_SHUTDOWN; + + // check a SPI status + if (spi_get_rx_state() != SPI_RX_DONE) { + // if SPI is not done + max31865_current_state = MAX_ERROR; + } + else { + // get a pointer to results + result_ptr = spi_get_rx_data(); + + // disable VBIAS to reduce power consumption + max31865_vbias = 0; + + // this function may change 'max31865_current_state' internally due to errors + max31865_send_config_register(); + + max31865_shutdown_ticks = 0; + } + + break; case MAX_SHUTDOWN: // MAX31965 is powered up and initialized but PT bias is disabled // and no measurement is ongoing + if (max31865_shutdown_ticks++ > 9) { + max31865_current_state = MAX_INITIALIZED; + + max31865_shutdown_ticks = 0; + } + break; case MAX_POWER_OFF: // supply voltage for MAX31865 is powered off and no communication