pub fn init_card(&mut self, freq: ClockFreq) -> Result<(), Error> {
        // Enable power to card
        self.power_card(true);

        // Enable clock
        self.sdio.clkcr.modify(|_, w| w.clken().enabled());
        // Send card to idle state
        self.cmd(cmd::idle())?;

        // Check if cards supports CMD 8 (with pattern)
        self.cmd(cmd::send_if_cond(1, 0xAA))?;
        let cic = CIC::from(self.sdio.resp1.read().bits());

        // If card did't echo back the pattern, we do not have a v2 card
        if cic.pattern() != 0xAA {
            return Err(Error::UnsupportedCardVersion);
        }

        if cic.voltage_accepted() & 1 == 0 {
            return Err(Error::UnsupportedVoltage);
        }

        let ocr = loop {
            // Initialize card

            // 3.2-3.3V
            let voltage_window = 1 << 20;
            match self.app_cmd(cmd::sd_send_op_cond(true, false, true, voltage_window)) {
                Ok(_) => (),
                Err(Error::Crc) => (),
                Err(err) => return Err(err),
            }
            let ocr = OCR::from(self.sdio.resp1.read().bits());
            if ocr.is_busy() {
                // Still powering up
                continue;
            }
            break ocr;
        };

        // True for SDHC and SDXC False for SDSC
        let capacity = if ocr.high_capacity() {
            CardCapacity::SDHC
        } else {
            CardCapacity::SDSC
        };

        // Get CID
        self.cmd(cmd::all_send_cid())?;
        let mut cid = [0; 4];
        cid[3] = self.sdio.resp1.read().bits();
        cid[2] = self.sdio.resp2.read().bits();
        cid[1] = self.sdio.resp3.read().bits();
        cid[0] = self.sdio.resp4.read().bits();
        let cid = CID::from(cid);

        // Get RCA
        self.cmd(cmd::send_relative_address())?;
        let rca = RCA::from(self.sdio.resp1.read().bits());
        let card_addr = rca.address();

        // Get CSD
        self.cmd(cmd::send_csd(card_addr))?;

        let mut csd = [0; 4];
        csd[3] = self.sdio.resp1.read().bits();
        csd[2] = self.sdio.resp2.read().bits();
        csd[1] = self.sdio.resp3.read().bits();
        csd[0] = self.sdio.resp4.read().bits();
        let csd = CSD::from(csd);

        self.select_card(card_addr)?;
        let scr = self.get_scr(card_addr)?;

        let card = Card {
            capacity,
            ocr,
            rca,
            cid,
            csd,
            scr,
        };

        self.card.replace(card);

        // Wait before setting the bus witdth and frequency to avoid timeouts on SDSC cards
        while !self.card_ready()? {}

        self.set_bus(self.bw, freq)?;
        Ok(())
    }
/// Initializes card (if present) and sets the bus at the specified frequency.
Initializes card (if present) and sets the bus at the specified frequency.
Initializes card ( if present ) and sets the bus at the specified frequency .
pub fn init_card(&mut self, freq: ClockFreq) -> Result<(), Error> {
        // Enable power to card
        self.power_card(true);
        // Enable clock
        self.sdio.clkcr.modify(|_, w| w.clken().enabled());
        // Send card to idle state
        self.cmd(cmd::idle())?;
        // Check if cards supports CMD 8 (with pattern)
        self.cmd(cmd::send_if_cond(1, 0xAA))?;
        let cic = CIC::from(self.sdio.resp1.read().bits());
        // If card did't echo back the pattern, we do not have a v2 card
        if cic.pattern() != 0xAA {
            return Err(Error::UnsupportedCardVersion);
        }
        if cic.voltage_accepted() & 1 == 0 {
            return Err(Error::UnsupportedVoltage);
        }
        let ocr = loop {
            // Initialize card
            // 3.2-3.3V
            let voltage_window = 1 << 20;
            match self.app_cmd(cmd::sd_send_op_cond(true, false, true, voltage_window)) {
                Ok(_) => (),
                Err(Error::Crc) => (),
                Err(err) => return Err(err),
            }
            let ocr = OCR::from(self.sdio.resp1.read().bits());
            if ocr.is_busy() {
                // Still powering up
                continue;
            }
            break ocr;
        };
        // True for SDHC and SDXC False for SDSC
        let capacity = if ocr.high_capacity() {
            CardCapacity::SDHC
        } else {
            CardCapacity::SDSC
        };
        // Get CID
        self.cmd(cmd::all_send_cid())?;
        let mut cid = [0; 4];
        cid[3] = self.sdio.resp1.read().bits();
        cid[2] = self.sdio.resp2.read().bits();
        cid[1] = self.sdio.resp3.read().bits();
        cid[0] = self.sdio.resp4.read().bits();
        let cid = CID::from(cid);
        // Get RCA
        self.cmd(cmd::send_relative_address())?;
        let rca = RCA::from(self.sdio.resp1.read().bits());
        let card_addr = rca.address();
        // Get CSD
        self.cmd(cmd::send_csd(card_addr))?;
        let mut csd = [0; 4];
        csd[3] = self.sdio.resp1.read().bits();
        csd[2] = self.sdio.resp2.read().bits();
        csd[1] = self.sdio.resp3.read().bits();
        csd[0] = self.sdio.resp4.read().bits();
        let csd = CSD::from(csd);
        self.select_card(card_addr)?;
        let scr = self.get_scr(card_addr)?;
        let card = Card {
            capacity,
            ocr,
            rca,
            cid,
            csd,
            scr,
        };
        self.card.replace(card);
        // Wait before setting the bus witdth and frequency to avoid timeouts on SDSC cards
        while !self.card_ready()? {}
        self.set_bus(self.bw, freq)?;
        Ok(())
    }
pub fn init_card ( & mut self , freq : ClockFreq ) -> Result < ( ) , Error > { // Enable power to card self . power_card ( true ) ; // Enable clock self . sdio . clkcr . modify ( | _ , w | w . clken ( ) . enabled ( ) ) ; // Send card to idle state self . cmd ( cmd :: idle ( ) ) ? ; // Check if cards supports CMD 8 (with pattern) self . cmd ( cmd :: send_if_cond ( 1 , 0xAA ) ) ? ; let cic = CIC :: from ( self . sdio . resp1 . read ( ) . bits ( ) ) ; // If card did't echo back the pattern, we do not have a v2 card if cic . pattern ( ) != 0xAA { return Err ( Error :: UnsupportedCardVersion ) ; } if cic . voltage_accepted ( ) & 1 == 0 { return Err ( Error :: UnsupportedVoltage ) ; } let ocr = loop { // Initialize card // 3.2-3.3V let voltage_window = 1 << 20 ; match self . app_cmd ( cmd :: sd_send_op_cond ( true , false , true , voltage_window ) ) { Ok ( _ ) => ( ) , Err ( Error :: Crc ) => ( ) , Err ( err ) => return Err ( err ) , } let ocr = OCR :: from ( self . sdio . resp1 . read ( ) . bits ( ) ) ; if ocr . is_busy ( ) { // Still powering up continue ; } break ocr ; } ; // True for SDHC and SDXC False for SDSC let capacity = if ocr . high_capacity ( ) { CardCapacity :: SDHC } else { CardCapacity :: SDSC } ; // Get CID self . cmd ( cmd :: all_send_cid ( ) ) ? ; let mut cid = [ 0 ; 4 ] ; cid [ 3 ] = self . sdio . resp1 . read ( ) . bits ( ) ; cid [ 2 ] = self . sdio . resp2 . read ( ) . bits ( ) ; cid [ 1 ] = self . sdio . resp3 . read ( ) . bits ( ) ; cid [ 0 ] = self . sdio . resp4 . read ( ) . bits ( ) ; let cid = CID :: from ( cid ) ; // Get RCA self . cmd ( cmd :: send_relative_address ( ) ) ? ; let rca = RCA :: from ( self . sdio . resp1 . read ( ) . bits ( ) ) ; let card_addr = rca . address ( ) ; // Get CSD self . cmd ( cmd :: send_csd ( card_addr ) ) ? ; let mut csd = [ 0 ; 4 ] ; csd [ 3 ] = self . sdio . resp1 . read ( ) . bits ( ) ; csd [ 2 ] = self . sdio . resp2 . read ( ) . bits ( ) ; csd [ 1 ] = self . sdio . resp3 . read ( ) . bits ( ) ; csd [ 0 ] = self . sdio . resp4 . read ( ) . bits ( ) ; let csd = CSD :: from ( csd ) ; self . select_card ( card_addr ) ? ; let scr = self . get_scr ( card_addr ) ? ; let card = Card { capacity , ocr , rca , cid , csd , scr , } ; self . card . replace ( card ) ; // Wait before setting the bus witdth and frequency to avoid timeouts on SDSC cards while ! self . card_ready ( ) ? { } self . set_bus ( self . bw , freq ) ? ; Ok ( ( ) ) }
Initializes card (if present) and sets the bus at the specified frequency.
Initializes card ( if present ) and sets the bus at the specified frequency .
&mut self
freq
ClockFreq
&mut self
freq
ClockFreq