Skip to content

Conversation

@kyrias
Copy link
Contributor

@kyrias kyrias commented Nov 13, 2025

No description provided.

@dimpolo
Copy link

dimpolo commented Nov 17, 2025

Sorry I won't have time to review this but here's a test case for you to try.
Currently your code panics at let bit_count_second = field.bit_count - bit_count_first;

use espflash::target::Chip;
use espflash::target::efuse::EfuseField;
use espflash::target::efuse::esp32s3::{ADC1_INIT_CODE_ATTEN0, ADC1_INIT_CODE_ATTEN1};

fn main() {
    assert_eq!(TestCase.read_efuse(&mut Connection, ADC1_INIT_CODE_ATTEN0).unwrap(), 121);
    assert_eq!(TestCase.read_efuse(&mut Connection, ADC1_INIT_CODE_ATTEN1).unwrap(), 38);
}

struct TestCase;
struct Connection;
#[derive(Debug)]
struct Error;

impl TestCase {
    pub fn read_efuse(&self, connection: &mut Connection, field: EfuseField) -> Result<u32, Error> {
        // TODO: Figure out how to fix this.  (esp-rs/espflash#960)
        if field.bit_count > 32 {
            panic!("Tried to read an eFuse field of more than 32 bits");
        }

        // Calculate the number of bits per word in case the field spans two eFuse words.
        let bit_count_first = 32 - (field.bit_start % 32);
        let bit_count_second = field.bit_count - bit_count_first;

        let value = {
            let mask = ((1u32 << bit_count_first) - 1) << bit_count_second;
            let shift = (field.bit_start % 32) - bit_count_second;

            let value = self.read_efuse_raw(connection, field.block, field.word)?;
            let value = (value >> shift) & mask;
            value
        };

        let value = if bit_count_second == 0 {
            value
        } else {
            // The second half doesn't need to be shifted since it will always start at bit 0.
            let mask = (1u32 << bit_count_second) - 1;

            // Potentially wrap around to the next eFuse block.
            let (block, word) = if (field.word + 1) > self.block_size(field.block as usize) {
                (field.block + 1, 0)
            } else {
                (field.block, field.word + 1)
            };

            let second_value = self.read_efuse_raw(connection, block, word)?;

            value | (second_value & mask)
        };

        Ok(value)
    }

    fn block_size(&self, block: usize) -> u32 {
        Chip::Esp32s3.block_size(block)
    }

    fn read_efuse_raw(&self, _: &mut Connection, block:u32,field:u32)->Result<u32, Error>{
        assert_eq!(block, 2);
        match field {
            4 => Ok(3475674417),
            5 => Ok(1414776004),
            _ => unreachable!("{field}"),
        }
    }
}

@bjoernQ bjoernQ mentioned this pull request Nov 19, 2025
@kyrias kyrias closed this Nov 19, 2025
@kyrias kyrias deleted the push-nrkkykloqpmz branch November 19, 2025 17:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants