Learning diary --SDIO-SDMMC1 (2020.2.13)

SDMMC theoretical basics

• SDMMC refers SD, SDIO, MMC card host interface, providing APB2 peripheral bus multimedia cards (MMCs), secure digital memory card (SD) and SDIO cards.
• MMC stands for "MultiMediaCard" - so it is often called "multimedia card", is a compact high-capacity flash memory cards, especially used in mobile phones and digital video and other mobile terminals.
• SD card, secure digital memory card (Secure Digital Memory Card), are standard memory cards for mobile devices. SD card data transfer and physical specifications developed from MMC, and MMC similar size. The same length and width and MMC, MMC is slightly thicker than a little. SD card compatibility backward compatible Multimedia Card (Multi Media Card)
• SDIO is developed on the basis of the SD memory card interface on the peripheral interface, SDIO interface is compatible with the previous SD memory card and SDIO interface can be connected to the device, currently according to SPEC SDIO protocol, SDIO interface supports the general category of devices with Bluetooth, network cards, TV cards.

Find development board SD card slot

A picture board for developing SD card slot, may be inserted into a SD card 1
Here Insert Picture Description

View pins

1) PD2 is SDMMC1_CMD;
2) is in PC12 SDMMC1_SCK;
. 3) to PC8 SDMMC1_DO;
. 6) PA9 transmission pin UART1;
. 7) is UART1 receive pin P10;
. 8) is an LED PC 13 pins
arranged in accordance with the above information Pin

Pin Configuration

Here Insert Picture Description

Configuring the Clock

Using the internal clock (default)
the system clock up to 80MHZ
SDMMC1 24MHZ clock
Here Insert Picture Description

Configuration parameters SDMMC1

Here Insert Picture Description
SDMMCCLK clock divid also be the default 0

Project generation SDMMC1

1、自定义工程名称。
2、选择英文路径,否则会丢 失启动文件而无法编译通过, 需要手动添加启动文件: startup_stm32l431xx.s
3、选择MDK-ARM V5开发软件, 即KEIL5软件。

代码编写说明

1、初始化microSD
2、读取microSD卡参数
3、写数据到microSD卡
4、读取写入microSD卡数据
调用函数:
1、HAL_SD_CardStateTypedef HAL_SD_GetCardState(SD_HandleTypeDef *hsd);//获取SD状态
2、HAL_SD_GetCardCID(SD_HandleTypeDef *hsd, HAL_SD_CardCIDTypedef *pCID);//获取SD卡ID
3、HAL_SD_WriteBlocks (SD_HandleTypeDef *hsd, uint8_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, int32_t Timeout);
4、HAL_SD_ReadBlocks(SD_HandleTypeDef *hsd, uint8_t pData, uint32_t BlockAdd, uint32_t NumberOfBlocks, uint32_t Timeout)
5、HAL_GPIO_TogglePin(GPIO_TypeDef
GPIOx, uint16_t GPIO_Pin);// 运行LED
6、void HAL_Delay(uint32_t Delay);//延时
7、printf();//打印数据到串口函数 需要重新定义函数fputc();才能正常使用printf();

int fputc(int ch,FILE*f)
{
	uint8_t temp[1]={ch}
	HAL_UART_Transmit(&huert,temp,1,2);
	return HAL_OK;
}

代码编写实现

Add the header file:
#include "stdio.h"
#include "string.h"
main function within the definition of variables
uint8_t temp [] = "Welcome to the club of Things";
uint8_t temp1 [] = "is available for purchase to IoT";
main function outside the defined variables:

HAL_SD_CardCIDTypedef SD_CardCID;
#define BLOCK_SIZE                  512//块大小
#define NUMBER_OF_BLOCKS    8//块数量、
#define WRITE_READ_ADDRESS 0x00002000//读写地址
#define WRITE_READ_ADDRESS2  0x00004000//读写地址
uint32_t Buffer_Block_Tx[BLOCK_SIZE*NUMBER_OF_BLOCKSS];//数据缓存
uint32_t Buffer_Block_Rx[BLOCK_SIZE*NUMBER_OF_BLOCKSS]={1,2};//数据缓存
HAL_StatusTypeDef sd_status;//HAL返回值
void ReadSD(void);
void SD_Write_Read_Test(int32_t addr,uint8_t*temp);

Coding to achieve

void SD_Write_Read_Test(int32_t addr,uint8_t* temp)//写入SD卡数据并读出数据 
{
		int i,j = 0; 
		printf("\r\n\r\n*************Write SD card data *************\r\n"); 
		sd_status=HAL_SD_WriteBlocks(&hsd1(uint8_t*)temp,addr,NUMBER_OF_BLOCKS,0xffff); 
		printf("Write data:%s,address:%x\r\n",temp,addr); HAL_Delay(480); 
		if(sd_status == 0) 
		{ 
				printf("Write data test is OK\r\n" ); 
		}
		else
		printf("Write data test is fail!\r\n " ); 
		printf("*************Read SD card data *************\r\n"); 
		sd_status=HAL_SD_ReadBlocks(&hsd1(uint8_t*)Buffer_Block_Rx,addr,NUMBER_OF_BLOCKS,0xffff); printf("ReadSDData:%s,address:%x\r\n",(char*)Buffer_Block_Rx,addr); 
		if(sd_status == 0)
		 { 
		 printf("Read data test is OK\r\n" ); 
		 }
else printf("Read data test is fail!\r\n " ); 
}

void ReadSD(void)//读取SD出厂参数
 { 
	int State=0; 
	State = HAL_SD_GetCardState(&hsd1); 
	if(State == HAL_SD_CARD_TRANSFER) 
	{
	HAL_SD_GetCardCID(&hsd1,&SD_CardCID); 
	printf("\r\n Initialize SD card successfully!\r\n\r\n"); 
	printf(" SD card information! \r\n"); 
	printf(" CardCapacity : %llu \r\n",((unsigned long long)hsd1.SdCard.BlockSize*hsd1.SdCard.BlockNbr)); 
	printf(" CardBlockSize : %d \r\n",hsd1.SdCard.BlockSize); 
	printf(" RCA : %d \r\n",hsd1.SdCard.RelCardAdd);
	printf(" CardType : %d \r\n",hsd1.SdCard.CardType); 
	printf(" ManufacturerID: %d \r\n",SD_CardCID.ManufacturerID); 
	}
	else {printf("SD card init fail!\r\n" ); 
	while(1); // 停机 
	} 
}

while(1)循环外程序: 
ReadSD();//读取SD卡初始化参数 
HAL_SD_ReadBlocks(&hsd1(uint8_t*)Buffer_Block_Rx,WRITE_READ_ADDRESS,NUMBER_OF_BLOCKS,0xffff); 
printf("Read SD old data1:%s,address:%x\r\n",(char*)Buffer_Block_Rx,WRITE_READ_ADDRESS); memset(Buffer_Block_Rx,0x00,sizeof(Buffer_Block_Rx)); 
HAL_SD_ReadBlocks(&hsd1(uint8_t*)Buffer_Block_Rx,WRITE_READ_ADDRESS2,NUMBER_OF_BLOCKS,0xffff); 
printf("Read SD old data2:%s,address:%x\r\n",(char*)Buffer_Block_Rx,WRITE_READ_ADDRESS2); memset(Buffer_Block_Rx,0x00,sizeof(Buffer_Block_Rx)); SD_Write_Read_Test(WRITE_READ_ADDRESS,temp); memset(Buffer_Block_Rx,0x00,sizeof(Buffer_Block_Rx)); SD_Write_Read_Test(WRITE_READ_ADDRESS2,temp1); memset(Buffer_Block_Rx,0x00,sizeof(Buffer_Block_Rx)); 

while(1)循环内程序: 
HAL_GPIO_TogglePin(GPIOC,GPIO_PIN_13); 
HAL_Delay(1000);
Released nine original articles · won praise 0 · Views 291

Guess you like

Origin blog.csdn.net/quanqueen/article/details/104331816