HAL Library of STM32 learning digging - SD card reading and writing (bare machine program)

Continue to dig the pit and run 8GB SD card for reading and writing.

PS: it's impossible to run the system. It's impossible to run the system in this life (funny)

Implementation function:

1. Successfully identify SD card and print SD card configuration information through serial port

2. Read the SD card data and print the read SD card data through the serial port

3. Write SD card data, print the SD card data read after writing through serial port

 

Because there is no JLINK, you can only burn files through dfu, so first configure the serial port through STM32CubeMX, asynchronous receiving and sending, the serial port baud rate is 115200, and other options are default.

 

Set SDIO related data, SDIO clock frequency = APB2 / frequency division coefficient, because MCU clock frequency is 84Mhz, at this time, SDIO clock frequency = 84/84=1M

 

Set SDIO interrupt (it seems useless, but it has not been tested specifically)

 

Set pin mode of SDIO!!! Set pin mode of SDIO!!! Set pin mode of SDIO!!!

Important thing to say three times, because the hardware does not pull up the resistance, resulting in the call function sd_findscrfailed, the program crashed.

 

Then generate the code directly, and add the following code before while(1)

  /* USER CODE BEGIN 2 */
	
	printf( "CardType is : %d\r\n", hsd.SdCard.CardType);
	printf( "CardCapacity is : %d\r\n", hsd.SdCard.BlockNbr);
	printf( "CardBlockSize is : %d\r\n", hsd.SdCard.BlockSize);
	printf( "RCA is : %d\r\n", hsd.SdCard.RelCardAdd);
	printf( "CardVersion is : %d\r\n", hsd.SdCard.CardVersion);
	printf( "Class is : %d\r\n", hsd.SdCard.Class);
	printf( "LogBlockNbr is : %d\r\n", hsd.SdCard.LogBlockNbr);
	printf( "LogBlockSize is : %d\r\n", hsd.SdCard.LogBlockSize);	
	
	SD_Test();
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)

Other definitions and function prototypes

#Define block? Size 512 / / SD card block size     
#Define number of blocks 8 / / number of test blocks (less than 15)
#Define write? Read? Address 0x00000000 / / test read / write address
uint8_t Buffer_Block_Tx[BLOCK_SIZE*NUMBER_OF_BLOCKS]; // Write data cache
uint8_t Buffer_Block_Rx[BLOCK_SIZE*NUMBER_OF_BLOCKS]; // Read data cache

void SD_Test(void)
{ 
  uint32_t index = 0;	
	HAL_StatusTypeDef sd_status;	
	
	//Data before erasure
	HAL_SD_ReadBlocks(&hsd, Buffer_Block_Rx, WRITE_READ_ADDRESS, NUMBER_OF_BLOCKS, 0xffff);
	HAL_Delay(5000);
	printf("Data before erasure\r\n" ); 	
	for (index = 0; index < BLOCK_SIZE*NUMBER_OF_BLOCKS; index++)
		printf("%02x",Buffer_Block_Rx[index]);
	printf("\r\n" ); 
	/*No need to erase data SD card type is HC-8GB*/
	printf("Write data\r\n" ); 			
	for (index = 0; index < BLOCK_SIZE*NUMBER_OF_BLOCKS; index++)
	{
		Buffer_Block_Tx[index] = (index&0xff);
		printf("%02x",Buffer_Block_Tx[index]);
	}
	printf("\r\n" ); 	
	HAL_SD_WriteBlocks(&hsd, Buffer_Block_Tx, WRITE_READ_ADDRESS, NUMBER_OF_BLOCKS, 0xffff);	
	printf("\r\n" ); 		
  HAL_Delay(5000);	
	
	/* Read the area just erased */
	HAL_SD_ReadBlocks(&hsd, Buffer_Block_Rx, WRITE_READ_ADDRESS, NUMBER_OF_BLOCKS, 0xffff);
	HAL_Delay(5000);
	printf("Read write data\r\n" ); 				
	for (index = 0; index < BLOCK_SIZE*NUMBER_OF_BLOCKS; index++)
		printf("%02x",Buffer_Block_Rx[index]);
	 printf("\r\n" ); 
}

Finally compile and burn, ~ OK

In addition, it should be noted that some SD cards can be read and written directly without erasure. Erasure may lead to failure of data writing

09K
Published 2 original articles, won praise 4, visited 10000+
Private letter follow

Tags: less

Posted on Thu, 16 Jan 2020 09:06:28 -0500 by Wykster