Artículos relacionados
"[SDIO] Índice de artículos relacionados con SDIO, tarjeta SD, sistema de archivos FatFs"
1. Diagrama de bloques funcional STM32 SDIO
El controlador STM32 tiene un SDIO, que consta de dos partes: adaptador SDIO e interfaz APB2 , como se muestra en la figura siguiente. El adaptador SDIO proporciona la función de host SDIO, puede proporcionar reloj SD, enviar comandos y realizar transmisión de datos. El controlador utiliza la interfaz APB2 para acceder a los registros del adaptador SDIO y puede generar interrupciones y señales de solicitud DMA.
SDIO usa dos señales de reloj, un reloj es un adaptador SDIO ( SDIOCLK = 48MHz ), el otro es un reloj de bus APB2 ( PCLK2, típicamente 90MHz ).
El SDIO del controlador STM32 es el dispositivo principal para tarjetas MMC y tarjetas SD, por lo que se reservan 8 líneas de datos y se utilizan hasta cuatro líneas de datos para tarjetas SD.
El adaptador SDIO consta de cinco unidades, a saber :( se muestra a continuación)
- unidad de control
- Unidad de ruta de comando
- Unidad de ruta de datos
- Unidad de registro
- FIFO
2. Diseño de hardware de la tarjeta SD STM32
El pin SDIO del controlador STM32 está diseñado para ser fijo y el diseño de la placa de desarrollo adopta el modo de cuatro líneas de datos. Se debe agregar una resistencia pull-up a la línea de comando y a la línea de datos.
3. Migración de prueba de lectura y escritura de la tarjeta SD
Es relativamente sencillo trasplantar la prueba de lectura y escritura de la tarjeta SD en la plataforma STM32. Podemos consultar directamente la demostración de SDIO en la biblioteca estándar para el trasplante. Tal como:STM32F4xx_DSP_StdPeriph_Lib_V1.8.0\Project\STM32F4xx_StdPeriph_Examples\SDIO\SDIO_uSDCard
- Se
stm324x9i_eval_sdio_sd.c
trasplantará al proyecto actual, proporciona SD_Init () , SD_WriteBlock () y SD_ReadBlock () y otras funciones de operación de la tarjeta SD, que podemos usar directamente.
- Escriba
sd_test.c
archivos para realizar la prueba de borrado de la tarjeta SD , la prueba de operación de escritura de un solo bloque y la prueba de operación de escritura de múltiples bloques .
/**
* @brief Configures SDIO IRQ channel.
* @param None
* @retval None
*/
static void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure the NVIC Preemption Priority Bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = SDIO_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = SD_SDIO_DMA_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_Init(&NVIC_InitStructure);
}
/**
* @brief Tests the SD card erase operation.
* @param None
* @retval None
*/
static void SD_EraseTest(void)
{
printf("\r\n---->Erase test is starting...\r\n");
/*------------------- Block Erase ------------------------------------------*/
if (Status == SD_OK)
{
/* Erase NumberOfBlocks Blocks of WRITE_BL_LEN(512 Bytes) */
Status = SD_Erase(0x00, (BLOCK_SIZE * NUMBER_OF_BLOCKS));
}
if (Status == SD_OK)
{
Status = SD_ReadMultiBlocks(aBuffer_MultiBlock_Rx, 0x00, BLOCK_SIZE, NUMBER_OF_BLOCKS);
/* Check if the Transfer is finished */
Status = SD_WaitReadOperation();
/* Wait until end of DMA transfer */
while(SD_GetStatus() != SD_TRANSFER_OK);
}
/* Check the correctness of erased blocks */
if (Status == SD_OK)
{
EraseStatus = eBuffercmp(aBuffer_MultiBlock_Rx, MULTI_BUFFER_SIZE);
}
if(EraseStatus == PASSED)
{
LED1(ON);
printf("\tThe earase is successful.\r\n");
}
else
{
LED1(OFF);
LED3(ON);
printf("\tThe earase is fail!!!\r\n");
}
printf("<----Erase test is end.\r\n");
}
/**
* @brief Tests the SD card Single Blocks operations.
* @param None
* @retval None
*/
static void SD_SingleBlockTest(void)
{
printf("\r\n---->Single block test is starting...\r\n");
/*------------------- Block Read/Write --------------------------*/
/* Fill the buffer to send */
Fill_Buffer(aBuffer_Block_Tx, BLOCK_SIZE, 0x320F);
if (Status == SD_OK)
{
/* Write block of 512 bytes on address 0 */
Status = SD_WriteBlock(aBuffer_Block_Tx, 0x00, BLOCK_SIZE);
/* Check if the Transfer is finished */
Status = SD_WaitWriteOperation();
while(SD_GetStatus() != SD_TRANSFER_OK);
}
if (Status == SD_OK)
{
/* Read block of 512 bytes from address 0 */
Status = SD_ReadBlock(aBuffer_Block_Rx, 0x00, BLOCK_SIZE);
/* Check if the Transfer is finished */
Status = SD_WaitReadOperation();
while(SD_GetStatus() != SD_TRANSFER_OK);
}
/* Check the correctness of written data */
if (Status == SD_OK)
{
TransferStatus1 = Buffercmp(aBuffer_Block_Tx, aBuffer_Block_Rx, BLOCK_SIZE);
}
if(TransferStatus1 == PASSED)
{
LED1(ON);
printf("\tThe test is successful.\r\n");
}
else
{
LED1(OFF);
LED3(ON);
printf("\tThe test is fail!!!\r\n");
}
printf("<----Single block test is end.\r\n");
}
/**
* @brief Tests the SD card Multiple Blocks operations.
* @param None
* @retval None
*/
static void SD_MultiBlockTest(void)
{
printf("\r\n---->Multi block test is starting...\r\n");
/* Fill the buffer to send */
Fill_Buffer(aBuffer_MultiBlock_Tx, MULTI_BUFFER_SIZE, 0x0);
if (Status == SD_OK)
{
/* Write multiple block of many bytes on address 0 */
Status = SD_WriteMultiBlocks(aBuffer_MultiBlock_Tx, 0, BLOCK_SIZE, NUMBER_OF_BLOCKS);
/* Check if the Transfer is finished */
Status = SD_WaitWriteOperation();
while(SD_GetStatus() != SD_TRANSFER_OK);
}
if (Status == SD_OK)
{
/* Read block of many bytes from address 0 */
Status = SD_ReadMultiBlocks(aBuffer_MultiBlock_Rx, 0, BLOCK_SIZE, NUMBER_OF_BLOCKS);
/* Check if the Transfer is finished */
Status = SD_WaitReadOperation();
while(SD_GetStatus() != SD_TRANSFER_OK);
}
/* Check the correctness of written data */
if (Status == SD_OK)
{
TransferStatus2 = Buffercmp(aBuffer_MultiBlock_Tx, aBuffer_MultiBlock_Rx, MULTI_BUFFER_SIZE);
}
if(TransferStatus2 == PASSED)
{
LED1(ON);
printf("\tThe test is successful.\r\n");
}
else
{
LED1(OFF);
LED3(ON);
printf("\tThe test is fail!!!\r\n");
}
printf("<----Multi block test is end.\r\n");
}
void SD_Test_Start(void)
{
if(Status != SD_OK){
return;
}
/*-------------------------- SD Erase Test ---------------------------- */
SD_EraseTest();
/*-------------------------- SD Single Block Test --------------------- */
SD_SingleBlockTest();
/*-------------------------- SD Multi Blocks Test --------------------- */
SD_MultiBlockTest();
}
void SD_Test_Init(void)
{
/* NVIC Configuration */
NVIC_Configuration();
/*------------------------------ SD Init ---------------------------------- */
if((Status = SD_Init()) == SD_OK)
{
printf("The SD Card is successfully initialized!!!\r\n");
}else{
LED3(ON);
printf("The SD Card is fail initialized!!!\r\n");
}
}
- En la
main.c
convocatoria a pruebas de lectura y escritura de tarjetas SD.
int main(void)
{
// ...
SD_Test_Init();
SD_Test_Start();
while(1){
// Do nothing!!!
}
//return 0;
}
4. Prueba de verificación
Finalmente, la prueba de verificación es exitosa, de la siguiente manera:
5. Dirección de descarga de datos
La dirección de descarga del código completo para un trasplante exitoso es la siguiente:
https://download.csdn.net/download/ZHONGCAI0901/15265756