新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > STM32 SD卡_沒有加文件系統

        STM32 SD卡_沒有加文件系統

        作者: 時間:2016-11-13 來源:網絡 收藏

        下圖是萬利板子的SD卡部分

        本文引用地址:http://www.104case.com/article/201611/316408.htm

        其中SD_PWR連接到了 PD10 通過一個控制


        P-Channel 2.5-V (G-S) MOSFET 控制SD卡的電源

        =====================================================================

        SPI模式 讀寫SD卡

        SD卡初始化過程:

        1. 初始化STM32的SPI接口 使用低速模式
        2. 延時至少74clock
        3. 發送CMD0,需要返回0x01,進入Idle狀態
        4. 循環發送CMD55+ACMD41,直到返回0x00,進入Ready狀態
        5. 設置讀寫block大小為512byte
        5. 把STM32的SPI設置為高速模式



        讀一個block塊的過程

        1. 發送CMD17(單塊)或CMD18(多塊)讀命令,返回0x00
        2. 接收數據開始令牌0xfe + 正式數據512Bytes + CRC 校驗2Bytes

        寫一個block塊的過程

        1. 發送CMD24(單塊)或CMD25(多塊)寫命令,返回0x00
        2. 發送數據開始令牌0xfe + 正式數據512Bytes + CRC校驗2Bytes

        /*******************************************************************************
        * Function Name : SD_MMC_SPI_Init
        * Description : SD_MMC_SPI_Init
        * Input : None
        * Output : None
        * Return : zero init success, non-zero init error
        *******************************************************************************/
        u8 SD_MMC_SPI_Init(void)
        {
        GPIO_InitTypeDef GPIO_InitStructure;

        /* Enable SPI1 and GPIO clocks */
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1 | RCC_APB2Periph_GPIOA |
        RCC_APB2Periph_SD_MMC_SPI_CS, ENABLE);

        /* Configure SPI1 pins: SCK, MISO and MOSI */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);

        /* Configure SD_MMC_SPI_CS */
        GPIO_InitStructure.GPIO_Pin = SD_MMC_SPI_CS_Pin_CS;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
        GPIO_Init(SD_MMC_SPI_CS, &GPIO_InitStructure);

        //////////////////////////////////////////////////////////////////////////////
        /* initialize SPI with lowest frequency */
        SD_MMC_Low_Speed();

        /* card needs 74 cycles minimum to start up */
        for(u8 i = 0; i < 10; ++i)
        {
        /* wait 8 clock cycles */
        SD_MMC_ReadWrite_Byte(0x00);
        }

        /* address card */
        SD_MMC_SPI_SELECT();

        /* reset card */
        u8 response;
        for(u16 i = 0; ; ++i)
        {
        response = SD_MMC_Send_Command(CMD_GO_IDLE_STATE , 0 );
        if( response == 0x01 )
        break;

        if(i == 0x1ff)
        {
        SD_MMC_SPI_DESELECT();
        return 1;
        }
        }

        /* wait for card to get ready */
        for(u16 i = 0; ; ++i)
        {
        response = SD_MMC_Send_Command(CMD_SEND_OP_COND, 0);
        if(!(response & (1 << R1_IDLE_STATE)))
        break;

        if(i == 0x7fff)
        {
        SD_MMC_SPI_DESELECT();
        return 1;
        }
        }

        /* set block size to 512 bytes */
        if(SD_MMC_Send_Command(CMD_SET_BLOCKLEN, 512))
        {
        SD_MMC_SPI_DESELECT();
        return 1;
        }

        /* deaddress card */
        SD_MMC_SPI_DESELECT();

        /* switch to highest SPI frequency possible */
        SD_MMC_High_Speed();

        return 0;
        //////////////////////////////////////////////////////////////////////////////
        }

        /*******************************************************************************
        * Function Name : SD_MMC_Read_Single_Block
        * Description : SD_MMC_Read_Single_Block
        * Input : sector number and buffer data point
        * Output : None
        * Return : zero success, non-zero error
        *******************************************************************************/
        u8 SD_MMC_Read_Single_Block(u32 sector, u8* buffer)
        {
        u8 Response;
        u16 i;
        u16 Retry = 0;

        //讀命令 send read command
        Response = SD_MMC_Send_Command(CMD_READ_SINGLE_BLOCK, sector<<9);

        if(Response != 0x00)
        return Response;

        SD_MMC_SPI_SELECT();

        // start byte 0xfe
        while(SD_MMC_ReadWrite_Byte(0xff) != 0xfe)
        {
        if(++Retry > 0xfffe)
        {
        SD_MMC_SPI_DESELECT();
        return 1; //timeout
        }
        }

        for(i = 0; i < 512; ++i)
        {
        //讀512個數據
        *buffer++ = SD_MMC_ReadWrite_Byte(0xff);
        }

        SD_MMC_ReadWrite_Byte(0xff); //偽crc
        SD_MMC_ReadWrite_Byte(0xff); //偽crc

        SD_MMC_SPI_DESELECT();
        SD_MMC_ReadWrite_Byte(0xff); // extra 8 CLK

        return 0;
        }


        /*******************************************************************************
        * Function Name : SD_MMC_Write_Single_Block
        * Description : SD_MMC_Write_Single_Block
        * Input : sector number and buffer data point
        * Output : None
        * Return : zero success, non-zero error.
        *******************************************************************************/
        u8 SD_MMC_Write_Single_Block(u32 sector, u8* buffer)
        {
        u8 Response;
        u16 i;
        u16 retry=0;

        //寫命令 send write command
        Response = SD_MMC_Send_Command(CMD_WRITE_SINGLE_BLOCK, sector<<9);
        if(Response != 0x00)
        return Response;

        SD_MMC_SPI_SELECT();

        SD_MMC_ReadWrite_Byte(0xff);
        SD_MMC_ReadWrite_Byte(0xff);
        SD_MMC_ReadWrite_Byte(0xff);

        //發開始符 start byte 0xfe
        SD_MMC_ReadWrite_Byte(0xfe);

        //送512字節數據 send 512 bytes data
        for(i=0; i<512; i++)
        {
        SD_MMC_ReadWrite_Byte(*buffer++);
        }

        SD_MMC_ReadWrite_Byte(0xff); //dummy crc
        SD_MMC_ReadWrite_Byte(0xff); //dummy crc

        Response = SD_MMC_ReadWrite_Byte(0xff);

        //等待是否成功 judge if it successful
        if( (Response&0x1f) != 0x05)
        {
        SD_MMC_SPI_DESELECT();
        return Response;
        }
        //等待操作完 wait no busy
        while(SD_MMC_ReadWrite_Byte(0xff) != 0x00)
        {
        if(retry++ > 0xfffe)
        {
        SD_MMC_SPI_DESELECT();
        return 1;
        }
        }
        SD_MMC_SPI_DESELECT();
        SD_MMC_ReadWrite_Byte(0xff);// extra 8 CLK

        return 0;
        }

        if( SD_MMC_SPI_Init() ==1)
        printf(" _SD_MMC Initialization ERRORrn");
        else
        {
        printf(" _SD_MMC Initialization OKrn");
        //memset(buffer,0,512);
        //讀取一個扇區的內容 這里讀的是0扇區
        SD_MMC_Read_Single_Block( 0 , buffer );
        Uart1_PutString( buffer , 512 );
        }



        關鍵詞: STM32SD卡文件系

        評論


        技術專區

        關閉
        主站蜘蛛池模板: 顺平县| 伊金霍洛旗| 中阳县| 峡江县| 晋州市| 静宁县| 泰州市| 应城市| 思茅市| 阿拉尔市| 星子县| 汝阳县| 公安县| 武隆县| 仁怀市| 名山县| 武冈市| 高青县| 吉安县| 容城县| 泸定县| 垦利县| 寿宁县| 安国市| 阿拉善左旗| 寿光市| 东乌| 华容县| 涞水县| 乡城县| 长丰县| 清河县| 海丰县| 米泉市| 新泰市| 元朗区| 龙井市| 和平县| 西昌市| 浦县| 丹棱县|