Added backup ADC registers procedure. Fixed bug in the restore ADC registers procedure. Restore registers was added in previous commit.
This commit is contained in:
parent
a43256ac5e
commit
3ae00bdddd
@ -85,6 +85,12 @@ void ADS131M08Hub::setup() {
|
|||||||
this->adc_init_ = 0;
|
this->adc_init_ = 0;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (!adc_backup_registers()) {
|
||||||
|
ESP_LOGE(TAG, "Backing up ADC registers failed!");
|
||||||
|
this->mark_failed(LOG_STR("ADC register backup failed."));
|
||||||
|
this->adc_init_ = 0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
this->drdy_pin_->attach_interrupt(&ADS131M08Hub::isr_handler, this, gpio::INTERRUPT_FALLING_EDGE);
|
this->drdy_pin_->attach_interrupt(&ADS131M08Hub::isr_handler, this, gpio::INTERRUPT_FALLING_EDGE);
|
||||||
// cs_ctr_=1000;
|
// cs_ctr_=1000;
|
||||||
// SPIDevice::enable(); // leave CS low
|
// SPIDevice::enable(); // leave CS low
|
||||||
@ -199,24 +205,38 @@ bool ADS131M08Hub::adc_initialize(uint8_t word_length) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool ADS131M08Hub::adc_backup_registers() {
|
||||||
|
bool success = true; // we will employ this later, for now we just return success
|
||||||
|
int j = 0;
|
||||||
|
int store_nwords = sizeof(settings.data) / sizeof(settings.data[0]);
|
||||||
|
while(success && (j < store_nwords)) {
|
||||||
|
auto reg_data = adc_register_read(j + REG_MODE, MAX_FRAME_SIZE - 2);
|
||||||
|
int nregs_read = reg_data.size();
|
||||||
|
int i = 0;
|
||||||
|
while((i < nregs_read) && (j < store_nwords)) {
|
||||||
|
settings.data[j++] = reg_data[i++];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
bool ADS131M08Hub::adc_restore_registers() {
|
bool ADS131M08Hub::adc_restore_registers() {
|
||||||
uint8_t start_addr = REG_MODE;
|
|
||||||
uint16_str stored_regs(MAX_FRAME_SIZE - 2, 0);
|
uint16_str stored_regs(MAX_FRAME_SIZE - 2, 0);
|
||||||
bool success = true;
|
bool success = true;
|
||||||
int j = 0;
|
int j = 0;
|
||||||
success = adc_register_write(start_addr, settings.data[j], __LINE__, false);
|
int store_nwords = sizeof(settings.data) / sizeof(settings.data[0]);
|
||||||
|
success = adc_register_write(j + REG_MODE, settings.data[j], __LINE__, false);
|
||||||
j++;
|
j++;
|
||||||
start_addr++;
|
while(success && (j < store_nwords)) {
|
||||||
while(success && (j < sizeof(settings.data))) {
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
stored_regs.resize(MAX_FRAME_SIZE - 2);
|
stored_regs.resize(MAX_FRAME_SIZE - 2);
|
||||||
while((i < MAX_FRAME_SIZE - 2) && (j < sizeof(settings.data))) {
|
while((i < MAX_FRAME_SIZE - 2) && (j < store_nwords)) {
|
||||||
|
ESP_LOGD(TAG, "restoring %s reg: %04X", reg_addr_to_string(j + REG_MODE).c_str(), stored_regs[i]);
|
||||||
stored_regs[i++] = settings.data[j++];
|
stored_regs[i++] = settings.data[j++];
|
||||||
}
|
}
|
||||||
stored_regs.resize(i);
|
stored_regs.resize(i);
|
||||||
success = adc_register_write(start_addr, stored_regs, __LINE__, false);
|
success = adc_register_write(j + REG_MODE - i, stored_regs, __LINE__, false);
|
||||||
ESP_LOGD(TAG, "start_addr: %02X i: %02X", start_addr, i);
|
//ESP_LOGD(TAG, "start_addr: %02X i: %02X", j + REG_MODE - , i);
|
||||||
start_addr += i;
|
|
||||||
}
|
}
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
@ -559,7 +579,13 @@ bool ADS131M08Hub::adc_reset_retry() {
|
|||||||
}
|
}
|
||||||
if (!reset_ok) {
|
if (!reset_ok) {
|
||||||
adc_hard_reset();
|
adc_hard_reset();
|
||||||
delay_microseconds_safe(10000);
|
for(int i = 0; i < 4; i++) {
|
||||||
|
//zero_fill(rx_frame);
|
||||||
|
setup_frame(rx_frame, numFrameWords);
|
||||||
|
transfer_array(rx_frame);
|
||||||
|
delay_microseconds_safe(T_REGACQ);
|
||||||
|
}
|
||||||
|
//delay_microseconds_safe(10000);
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < sizeof(retry_wordlengths) && !reset_ok) {
|
while (i < sizeof(retry_wordlengths) && !reset_ok) {
|
||||||
adc_word_length_ = retry_wordlengths[i];
|
adc_word_length_ = retry_wordlengths[i];
|
||||||
@ -774,9 +800,14 @@ float_str ADS131M08Hub::read_multi() {
|
|||||||
crc_ok = check_crc(rx_frame);
|
crc_ok = check_crc(rx_frame);
|
||||||
if (crc_ok) {
|
if (crc_ok) {
|
||||||
if(status & MASK_STATUS_RESET) {
|
if(status & MASK_STATUS_RESET) {
|
||||||
adc_reset_retry();
|
if(adc_reset_retry()) {
|
||||||
adc_restore_registers();
|
if(adc_restore_registers()) {
|
||||||
ESP_LOGW(TAG, "Mid data-read reset detected; restored registers");
|
ESP_LOGW(TAG, "Mid data-read reset detected; restored registers");
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ESP_LOGE(TAG, "ADC reset detected. Unable to recover");
|
||||||
|
this->mark_failed(LOG_STR("ADC reset detected. Recovery failed"));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -1021,7 +1052,7 @@ bool ADS131M08Hub::adc_register_write_masked(uint8_t start_address, const uint16
|
|||||||
}
|
}
|
||||||
uint16_str reg_contents = adc_register_read(start_address, nregs);
|
uint16_str reg_contents = adc_register_read(start_address, nregs);
|
||||||
if (reg_contents.empty()) {
|
if (reg_contents.empty()) {
|
||||||
ESP_LOGW(TAG, "No contents read from adc. Expected %d values. Register write aborted.", nregs);
|
//ESP_LOGW(TAG, "No contents read from adc. Expected %d values. Register write aborted.", nregs);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
bool update_required = false;
|
bool update_required = false;
|
||||||
@ -1035,12 +1066,12 @@ bool ADS131M08Hub::adc_register_write_masked(uint8_t start_address, const uint16
|
|||||||
//ESP_LOGW(TAG, "After applying mask - reg contents: 0x%04X, caller: %d", reg_contents[i], line);
|
//ESP_LOGW(TAG, "After applying mask - reg contents: 0x%04X, caller: %d", reg_contents[i], line);
|
||||||
}
|
}
|
||||||
if(!update_required) {
|
if(!update_required) {
|
||||||
ESP_LOGW(TAG, "2: No update needed. start reg: %02X first data: %04X Line: %d", start_address, values[0], line);
|
//ESP_LOGW(TAG, "2: No update needed. start reg: %02X first data: %04X Line: %d", start_address, values[0], line);
|
||||||
// we backup data irrespective
|
// we backup data irrespective
|
||||||
int bup_index = start_address - REG_MODE;
|
int bup_index = start_address - REG_MODE;
|
||||||
for(auto i = values.begin(); i != values.end(); i++) {
|
for(auto i = values.begin(); i != values.end(); i++) {
|
||||||
ESP_LOGW(TAG, "Storing: %-13s: data %04X stored val: %04X Line: %d", reg_addr_to_string(bup_index + REG_MODE).c_str(), *i, settings.data[bup_index], line);
|
//ESP_LOGW(TAG, "Storing: %-13s: data %04X stored val: %04X Line: %d", reg_addr_to_string(bup_index + REG_MODE).c_str(), *i, settings.data[bup_index], line);
|
||||||
settings.data[bup_index++] = *i;
|
settings.data[bup_index++] = *i;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -1132,10 +1163,10 @@ uint16_str ADS131M08Hub::adc_register_read(uint8_t address, uint8_t nregs) {
|
|||||||
setup_frame(rx_frame, rreg_nwords);
|
setup_frame(rx_frame, rreg_nwords);
|
||||||
set_frame_word(rx_frame, 0, rreg_addr_opcode);
|
set_frame_word(rx_frame, 0, rreg_addr_opcode);
|
||||||
add_crc(rx_frame);
|
add_crc(rx_frame);
|
||||||
|
ESP_LOGVV(TAG, "%s\n", rwreg_command_frame_to_string(rx_frame).c_str());
|
||||||
|
ESP_LOGVV(TAG, "Sending Frame: %s", frame_to_string(rx_frame).c_str());
|
||||||
transfer_array(rx_frame); // send RREG
|
transfer_array(rx_frame); // send RREG
|
||||||
}
|
}
|
||||||
ESP_LOGVV(TAG, "%s\n", rwreg_command_frame_to_string(rx_frame).c_str());
|
|
||||||
ESP_LOGVV(TAG, "Sent Frame: %s", frame_to_string(rx_frame).c_str());
|
|
||||||
delay_microseconds_safe(T_REGACQ);
|
delay_microseconds_safe(T_REGACQ);
|
||||||
{
|
{
|
||||||
CHIP_SELECT
|
CHIP_SELECT
|
||||||
@ -1146,7 +1177,7 @@ uint16_str ADS131M08Hub::adc_register_read(uint8_t address, uint8_t nregs) {
|
|||||||
if (nregs > 1) {
|
if (nregs > 1) {
|
||||||
bool crc_ok = check_crc(rx_frame);
|
bool crc_ok = check_crc(rx_frame);
|
||||||
if (!crc_ok) {
|
if (!crc_ok) {
|
||||||
ESP_LOGD(TAG, "Recv Frame: %s", frame_to_string(rx_frame).c_str());
|
ESP_LOGV(TAG, "Recv Frame: %s", frame_to_string(rx_frame).c_str());
|
||||||
ESP_LOGW(TAG, "CRC failed reading ADC registers!");
|
ESP_LOGW(TAG, "CRC failed reading ADC registers!");
|
||||||
result.clear();
|
result.clear();
|
||||||
return result; // invalid
|
return result; // invalid
|
||||||
@ -1162,6 +1193,7 @@ uint16_str ADS131M08Hub::adc_register_read(uint8_t address, uint8_t nregs) {
|
|||||||
}
|
}
|
||||||
// uint16_t rreg_ack = (nregs == 1) ? rreg_addr_opcode : get_unsigned_frame_word(rx_frame, 0, true);
|
// uint16_t rreg_ack = (nregs == 1) ? rreg_addr_opcode : get_unsigned_frame_word(rx_frame, 0, true);
|
||||||
nregs = (rreg_ack & MASK_CMD_RW_REG_COUNT) + 1;
|
nregs = (rreg_ack & MASK_CMD_RW_REG_COUNT) + 1;
|
||||||
|
|
||||||
uint16_t start_addr = (rreg_ack & MASK_CMD_RW_REG_ADDRESS) >> 7;
|
uint16_t start_addr = (rreg_ack & MASK_CMD_RW_REG_ADDRESS) >> 7;
|
||||||
for (int i = 0; i < nregs; i++) {
|
for (int i = 0; i < nregs; i++) {
|
||||||
result[i] = get_unsigned_frame_word(rx_frame, i + offset, true);
|
result[i] = get_unsigned_frame_word(rx_frame, i + offset, true);
|
||||||
|
|||||||
@ -97,6 +97,7 @@ class ADS131M08Hub : public Component,
|
|||||||
void txf_init();
|
void txf_init();
|
||||||
bool adc_initialize(uint8_t word_length);
|
bool adc_initialize(uint8_t word_length);
|
||||||
bool adc_restore_registers();
|
bool adc_restore_registers();
|
||||||
|
bool adc_backup_registers();
|
||||||
bool adc_set_word_length(uint8_t word_length);
|
bool adc_set_word_length(uint8_t word_length);
|
||||||
|
|
||||||
void setup() override;
|
void setup() override;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user