📄 stm32_eval_spi_sd.c
字号:
/**
******************************************************************************
* @file stm32_eval_spi_sd.c
* @author MCD Application Team
* @version V4.5.0
* @date 07-March-2011
* @brief This file provides a set of functions needed to manage the SPI SD
* Card memory mounted on STM32xx-EVAL board (refer to stm32_eval.h
* to know about the boards supporting this memory).
* It implements a high level communication layer for read and write
* from/to this memory. The needed STM32 hardware resources (SPI and
* GPIO) are defined in stm32xx_eval.h file, and the initialization is
* performed in SD_LowLevel_Init() function declared in stm32xx_eval.c
* file.
* You can easily tailor this driver to any other development board,
* by just adapting the defines for hardware resources and
* SD_LowLevel_Init() function.
*
* +-------------------------------------------------------+
* | Pin assignment |
* +-------------------------+---------------+-------------+
* | STM32 SPI Pins | SD | Pin |
* +-------------------------+---------------+-------------+
* | SD_SPI_CS_PIN | ChipSelect | 1 |
* | SD_SPI_MOSI_PIN / MOSI | DataIn | 2 |
* | | GND | 3 (0 V) |
* | | VDD | 4 (3.3 V)|
* | SD_SPI_SCK_PIN / SCLK | Clock | 5 |
* | | GND | 6 (0 V) |
* | SD_SPI_MISO_PIN / MISO | DataOut | 7 |
* +-------------------------+---------------+-------------+
******************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32_eval_spi_sd.h"
/** @addtogroup Utilities
* @{
*/
/** @addtogroup STM32_EVAL
* @{
*/
/** @addtogroup Common
* @{
*/
/** @addtogroup STM32_EVAL_SPI_SD
* @brief This file includes the SD card driver of STM32-EVAL boards.
* @{
*/
/** @defgroup STM32_EVAL_SPI_SD_Private_Types
* @{
*/
/**
* @}
*/
/** @defgroup STM32_EVAL_SPI_SD_Private_Defines
* @{
*/
/**
* @}
*/
/** @defgroup STM32_EVAL_SPI_SD_Private_Macros
* @{
*/
/**
* @}
*/
/** @defgroup STM32_EVAL_SPI_SD_Private_Variables
* @{
*/
/**
* @}
*/
/** @defgroup STM32_EVAL_SPI_SD_Private_Function_Prototypes
* @{
*/
/**
* @}
*/
/** @defgroup STM32_EVAL_SPI_SD_Private_Functions
* @{
*/
/**
* @brief DeInitializes the SD/SD communication.
* @param None
* @retval None
*/
void SD_DeInit(void)
{
SD_LowLevel_DeInit();
}
/**
* @brief Initializes the SD/SD communication.
* @param None
* @retval The SD Response:
* - SD_RESPONSE_FAILURE: Sequence failed
* - SD_RESPONSE_NO_ERROR: Sequence succeed
*/
SD_Error SD_Init(void)
{
uint32_t i = 0;
/*!< Initialize SD_SPI */
SD_LowLevel_Init();
/*!< SD chip select high */
SD_CS_HIGH();
/*!< Send dummy byte 0xFF, 10 times with CS high */
/*!< Rise CS and MOSI for 80 clocks cycles */
for (i = 0; i <= 9; i++)
{
/*!< Send dummy byte 0xFF */
SD_WriteByte(SD_DUMMY_BYTE);
}
/*------------Put SD in SPI mode--------------*/
/*!< SD initialized and set to SPI mode properly */
return (SD_GoIdleState());
}
/**
* @brief Detect if SD card is correctly plugged in the memory slot.
* @param None
* @retval Return if SD is detected or not
*/
uint8_t SD_Detect(void)
{
__IO uint8_t status = SD_PRESENT;
/*!< Check GPIO to detect SD */
if (GPIO_ReadInputData(SD_DETECT_GPIO_PORT) & SD_DETECT_PIN)
{
status = SD_NOT_PRESENT;
}
return status;
}
/**
* @brief Returns information about specific card.
* @param cardinfo: pointer to a SD_CardInfo structure that contains all SD
* card information.
* @retval The SD Response:
* - SD_RESPONSE_FAILURE: Sequence failed
* - SD_RESPONSE_NO_ERROR: Sequence succeed
*/
SD_Error SD_GetCardInfo(SD_CardInfo *cardinfo)
{
SD_Error status = SD_RESPONSE_FAILURE;
status = SD_GetCSDRegister(&(cardinfo->SD_csd));
status = SD_GetCIDRegister(&(cardinfo->SD_cid));
cardinfo->CardCapacity = (cardinfo->SD_csd.DeviceSize + 1) ;
cardinfo->CardCapacity *= (1 << (cardinfo->SD_csd.DeviceSizeMul + 2));
cardinfo->CardBlockSize = 1 << (cardinfo->SD_csd.RdBlockLen);
cardinfo->CardCapacity *= cardinfo->CardBlockSize;
/*!< Returns the reponse */
return status;
}
/**
* @brief Reads a block of data from the SD.
* @param pBuffer: pointer to the buffer that receives the data read from the
* SD.
* @param ReadAddr: SD's internal address to read from.
* @param BlockSize: the SD card Data block size.
* @retval The SD Response:
* - SD_RESPONSE_FAILURE: Sequence failed
* - SD_RESPONSE_NO_ERROR: Sequence succeed
*/
SD_Error SD_ReadBlock(uint8_t* pBuffer, uint32_t ReadAddr, uint16_t BlockSize)
{
uint32_t i = 0;
SD_Error rvalue = SD_RESPONSE_FAILURE;
/*!< SD chip select low */
SD_CS_LOW();
/*!< Send CMD17 (SD_CMD_READ_SINGLE_BLOCK) to read one block */
SD_SendCmd(SD_CMD_READ_SINGLE_BLOCK, ReadAddr, 0xFF);
/*!< Check if the SD acknowledged the read block command: R1 response (0x00: no errors) */
if (!SD_GetResponse(SD_RESPONSE_NO_ERROR))
{
/*!< Now look for the data token to signify the start of the data */
if (!SD_GetResponse(SD_START_DATA_SINGLE_BLOCK_READ))
{
/*!< Read the SD block data : read NumByteToRead data */
for (i = 0; i < BlockSize; i++)
{
/*!< Save the received data */
*pBuffer = SD_ReadByte();
/*!< Point to the next location where the byte read will be saved */
pBuffer++;
}
/*!< Get CRC bytes (not really needed by us, but required by SD) */
SD_ReadByte();
SD_ReadByte();
/*!< Set response value to success */
rvalue = SD_RESPONSE_NO_ERROR;
}
}
/*!< SD chip select high */
SD_CS_HIGH();
/*!< Send dummy byte: 8 Clock pulses of delay */
SD_WriteByte(SD_DUMMY_BYTE);
/*!< Returns the reponse */
return rvalue;
}
/**
* @brief Reads multiple block of data from the SD.
* @param pBuffer: pointer to the buffer that receives the data read from the
* SD.
* @param ReadAddr: SD's internal address to read from.
* @param BlockSize: the SD card Data block size.
* @param NumberOfBlocks: number of blocks to be read.
* @retval The SD Response:
* - SD_RESPONSE_FAILURE: Sequence failed
* - SD_RESPONSE_NO_ERROR: Sequence succeed
*/
SD_Error SD_ReadMultiBlocks(uint8_t* pBuffer, uint32_t ReadAddr, uint16_t BlockSize, uint32_t NumberOfBlocks)
{
uint32_t i = 0, Offset = 0;
SD_Error rvalue = SD_RESPONSE_FAILURE;
/*!< SD chip select low */
SD_CS_LOW();
/*!< Data transfer */
while (NumberOfBlocks--)
{
/*!< Send CMD17 (SD_CMD_READ_SINGLE_BLOCK) to read one block */
SD_SendCmd (SD_CMD_READ_SINGLE_BLOCK, ReadAddr + Offset, 0xFF);
/*!< Check if the SD acknowledged the read block command: R1 response (0x00: no errors) */
if (SD_GetResponse(SD_RESPONSE_NO_ERROR))
{
return SD_RESPONSE_FAILURE;
}
/*!< Now look for the data token to signify the start of the data */
if (!SD_GetResponse(SD_START_DATA_SINGLE_BLOCK_READ))
{
/*!< Read the SD block data : read NumByteToRead data */
for (i = 0; i < BlockSize; i++)
{
/*!< Read the pointed data */
*pBuffer = SD_ReadByte();
/*!< Point to the next location where the byte read will be saved */
pBuffer++;
}
/*!< Set next read address*/
Offset += 512;
/*!< get CRC bytes (not really needed by us, but required by SD) */
SD_ReadByte();
SD_ReadByte();
/*!< Set response value to success */
rvalue = SD_RESPONSE_NO_ERROR;
}
else
{
/*!< Set response value to failure */
rvalue = SD_RESPONSE_FAILURE;
}
}
/*!< SD chip select high */
SD_CS_HIGH();
/*!< Send dummy byte: 8 Clock pulses of delay */
SD_WriteByte(SD_DUMMY_BYTE);
/*!< Returns the reponse */
return rvalue;
}
/**
* @brief Writes a block on the SD
* @param pBuffer: pointer to the buffer containing the data to be written on
* the SD.
* @param WriteAddr: address to write on.
* @param BlockSize: the SD card Data block size.
* @retval The SD Response:
* - SD_RESPONSE_FAILURE: Sequence failed
* - SD_RESPONSE_NO_ERROR: Sequence succeed
*/
SD_Error SD_WriteBlock(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t BlockSize)
{
uint32_t i = 0;
SD_Error rvalue = SD_RESPONSE_FAILURE;
/*!< SD chip select low */
SD_CS_LOW();
/*!< Send CMD24 (SD_CMD_WRITE_SINGLE_BLOCK) to write multiple block */
SD_SendCmd(SD_CMD_WRITE_SINGLE_BLOCK, WriteAddr, 0xFF);
/*!< Check if the SD acknowledged the write block command: R1 response (0x00: no errors) */
if (!SD_GetResponse(SD_RESPONSE_NO_ERROR))
{
/*!< Send a dummy byte */
SD_WriteByte(SD_DUMMY_BYTE);
/*!< Send the data token to signify the start of the data */
SD_WriteByte(0xFE);
/*!< Write the block data to SD : write count data by block */
for (i = 0; i < BlockSize; i++)
{
/*!< Send the pointed byte */
SD_WriteByte(*pBuffer);
/*!< Point to the next location where the byte read will be saved */
pBuffer++;
}
/*!< Put CRC bytes (not really needed by us, but required by SD) */
SD_ReadByte();
SD_ReadByte();
/*!< Read data response */
if (SD_GetDataResponse() == SD_DATA_OK)
{
rvalue = SD_RESPONSE_NO_ERROR;
}
}
/*!< SD chip select high */
SD_CS_HIGH();
/*!< Send dummy byte: 8 Clock pulses of delay */
SD_WriteByte(SD_DUMMY_BYTE);
/*!< Returns the reponse */
return rvalue;
}
/**
* @brief Writes many blocks on the SD
* @param pBuffer: pointer to the buffer containing the data to be written on
* the SD.
* @param WriteAddr: address to write on.
* @param BlockSize: the SD card Data block size.
* @param NumberOfBlocks: number of blocks to be written.
* @retval The SD Response:
* - SD_RESPONSE_FAILURE: Sequence failed
* - SD_RESPONSE_NO_ERROR: Sequence succeed
*/
SD_Error SD_WriteMultiBlocks(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t BlockSize, uint32_t NumberOfBlocks)
{
uint32_t i = 0, Offset = 0;
SD_Error rvalue = SD_RESPONSE_FAILURE;
/*!< SD chip select low */
SD_CS_LOW();
/*!< Data transfer */
while (NumberOfBlocks--)
{
/*!< Send CMD24 (SD_CMD_WRITE_SINGLE_BLOCK) to write blocks */
SD_SendCmd(SD_CMD_WRITE_SINGLE_BLOCK, WriteAddr + Offset, 0xFF);
/*!< Check if the SD acknowledged the write block command: R1 response (0x00: no errors) */
if (SD_GetResponse(SD_RESPONSE_NO_ERROR))
{
return SD_RESPONSE_FAILURE;
}
/*!< Send dummy byte */
SD_WriteByte(SD_DUMMY_BYTE);
/*!< Send the data token to signify the start of the data */
SD_WriteByte(SD_START_DATA_SINGLE_BLOCK_WRITE);
/*!< Write the block data to SD : write count data by block */
for (i = 0; i < BlockSize; i++)
{
/*!< Send the pointed byte */
SD_WriteByte(*pBuffer);
/*!< Point to the next location where the byte read will be saved */
pBuffer++;
}
/*!< Set next write address */
Offset += 512;
/*!< Put CRC bytes (not really needed by us, but required by SD) */
SD_ReadByte();
SD_ReadByte();
/*!< Read data response */
if (SD_GetDataResponse() == SD_DATA_OK)
{
/*!< Set response value to success */
rvalue = SD_RESPONSE_NO_ERROR;
}
else
{
/*!< Set response value to failure */
rvalue = SD_RESPONSE_FAILURE;
}
}
/*!< SD chip select high */
SD_CS_HIGH();
/*!< Send dummy byte: 8 Clock pulses of delay */
SD_WriteByte(SD_DUMMY_BYTE);
/*!< Returns the reponse */
return rvalue;
}
/**
* @brief Read the CSD card register.
* Reading the contents of the CSD register in SPI mode is a simple
* read-block transaction.
* @param SD_csd: pointer on an SCD register structure
* @retval The SD Response:
* - SD_RESPONSE_FAILURE: Sequence failed
* - SD_RESPONSE_NO_ERROR: Sequence succeed
*/
SD_Error SD_GetCSDRegister(SD_CSD* SD_csd)
{
uint32_t i = 0;
SD_Error rvalue = SD_RESPONSE_FAILURE;
uint8_t CSD_Tab[16];
/*!< SD chip select low */
SD_CS_LOW();
/*!< Send CMD9 (CSD register) or CMD10(CSD register) */
SD_SendCmd(SD_CMD_SEND_CSD, 0, 0xFF);
/*!< Wait for response in the R1 format (0x00 is no errors) */
if (!SD_GetResponse(SD_RESPONSE_NO_ERROR))
{
if (!SD_GetResponse(SD_START_DATA_SINGLE_BLOCK_READ))
{
for (i = 0; i < 16; i++)
{
/*!< Store CSD register value on CSD_Tab */
CSD_Tab[i] = SD_ReadByte();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -