STM32 H743 轮询SD模式

注意SD不擦除也可以写,如果SD擦除 需要查询SD卡状态,等待其擦除完毕才可以继续读写

还有一点在H7中的坑,如果采用DMA传输数据,建议关闭L1缓冲,不然调试会有很多问题。

PS:H7的DMA不需要单独配置DMA通道
datasheet中有介绍
The SDMMC host interface embeds a dedicated DMA controller allowing high-speed
transfers between the interface and the SRAM.

f_mkfs(“0:”,FM_ANY,512,work,512);
参数:path
当给定0时,
首先在驱动器上的第一个扇区创建一个分区表,
然后文件系统被创建在分区上。
这被称为FDISK格式化,用于硬盘和存储卡。
当给定1时,文件系统从第一个扇区开始创建,
而没有分区表。这被称为超级软盘(SFD)格式化,
用于软盘和可移动磁盘。

参数:au
指定每簇中以字节为单位的分配单元大小。
数值必须是0或从512到32K之间2的幂。
当指定0时,簇大小取决于卷大小。

/* USER CODE BEGIN Header */
	/**
	******************************************************************************
	* @file           : main.c
	* @brief          : Main program body
	******************************************************************************
	* @attention
	*
	* <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
	* All rights reserved.</center></h2>
	*
	* This software component is licensed by ST under BSD 3-Clause license,
	* the "License"; You may not use this file except in compliance with the
	* License. You may obtain a copy of the License at:
	*                        opensource.org/licenses/BSD-3-Clause
	*
	******************************************************************************
	*/
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "dma.h"
#include "sdmmc.h"
#include "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
	#include "stdio.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
	#define  BUFFER_SIZE  (255)

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
	uint8_t receive_buff[255];   
	uint8_t read_buf[512];
	uint8_t write_buf[512];
	int sdcard_status = 0,i;
	extern SD_HandleTypeDef hsd1;
	HAL_SD_CardCIDTypedef sdcard_cid;
/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */
  

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_USART1_UART_Init();
  MX_SDMMC1_SD_Init();
  /* USER CODE BEGIN 2 */
  __HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);
  HAL_UART_Receive_DMA(&huart1, (uint8_t*)receive_buff, 255); 
  sdcard_status = HAL_SD_GetCardState(&hsd1);

	if(sdcard_status == HAL_SD_CARD_TRANSFER)
	{
	printf("SD card init ok!\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);


	HAL_SD_GetCardCID(&hsd1,&sdcard_cid);
	printf("ManufacturerID: %d \r\n",sdcard_cid.ManufacturerID);
	}
	else
	{
	printf("SD card init fail!\r\n" );

	}
	/* ²Á³ýSD¿¨¿é */
	printf("------------------- Block Erase -------------------------------\r\n");
	sdcard_status = HAL_SD_Erase(&hsd1, 0, 511);
	if (sdcard_status == 0)
	{
	printf("Erase block ok\r\n");
	}
	else
	{
	printf("Erase block fail\r\n");
	}
	while( HAL_SD_GetCardState(&hsd1)==HAL_SD_CARD_PROGRAMMING);
	
	//HAL_Delay(100);
	/* ¶Áȡδ²Ù×÷֮ǰµÄÊý¾Ý */
	printf("------------------- Read SD card block data Test ------------------\r\n");
	sdcard_status = HAL_SD_ReadBlocks(&hsd1,(uint8_t *)read_buf,0,1,0xffff);
	if(sdcard_status == 0)
	{ 
	printf("Read block data ok \r\n" );
	for(i = 0; i < 512; i++)
	{
	printf("0x%02x ", read_buf[i]);
	if((i+1)%16 == 0)
	{
	printf("\r\n");
	}
	}
	}
	else
	{
	printf("Read block data fail!\r\n " );
	}
	for(i = 0; i < 512; i++)
	{
	//write_buf[i] = i % 256;
	write_buf[i] = 0x02;
	}
	/* ÏòSD¿¨¿éдÈëÊý¾Ý */
	printf("------------------- Write SD card block data Test ------------------\r\n");
	sdcard_status = HAL_SD_WriteBlocks(&hsd1,(uint8_t *)write_buf,0,1,0xffff);
	if(sdcard_status == 0)
	{ 
	printf("Write block data ok \r\n" );
	}
	else
	{
	printf("Write block data fail!\r\n " );
	}
	/* ¶ÁÈ¡²Ù×÷Ö®ºóµÄÊý¾Ý */
	printf("------------------- Read SD card block data after Write ------------------\r\n");
	sdcard_status = HAL_SD_ReadBlocks(&hsd1,(uint8_t *)read_buf,0,1,0xffff);
	if(sdcard_status == 0)
	{ 
	printf("Read block data ok \r\n" );
	for(i = 0; i < 512; i++)
	{
	printf("0x%02x ", read_buf[i]);
	if((i+1)%16 == 0)
	{
	printf("\r\n");
	}
	}
	}



  /* USER CODE END 2 */

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

    /* USER CODE BEGIN 3 */




	//printf("123\r\n");
	}
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};

  /** Supply configuration update enable 
  */
  HAL_PWREx_ConfigSupply(PWR_LDO_SUPPLY);
  /** Configure the main internal regulator output voltage 
  */
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);

  while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}
  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_DIV1;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLM = 4;
  RCC_OscInitStruct.PLL.PLLN = 10;
  RCC_OscInitStruct.PLL.PLLP = 2;
  RCC_OscInitStruct.PLL.PLLQ = 5;
  RCC_OscInitStruct.PLL.PLLR = 2;
  RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_3;
  RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOMEDIUM;
  RCC_OscInitStruct.PLL.PLLFRACN = 0;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
                              |RCC_CLOCKTYPE_D3PCLK1|RCC_CLOCKTYPE_D1PCLK1;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV1;
  RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  {
    Error_Handler();
  }
  PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_SDMMC;
  PeriphClkInitStruct.SdmmcClockSelection = RCC_SDMMCCLKSOURCE_PLL;
  PeriphClkInitStruct.Usart16ClockSelection = RCC_USART16CLKSOURCE_D2PCLK2;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */
	#ifdef __GNUC__
	#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
	PUTCHAR_PROTOTYPE
	{

	HAL_UART_Transmit(&huart1, (uint8_t*)&ch, 1, HAL_MAX_DELAY);

	return ch;
	}
	int _write(int file, char *ptr, int len)
	{

	int DataIdx;

	for (DataIdx = 0; DataIdx < len;DataIdx++)

	{

	__io_putchar(*ptr++);

	}

	return len;

	}

	#else
	#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
	PUTCHAR_PROTOTYPE
	{
	HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);
	return ch;

	}
	#endif	
/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
	/* User can add his own implementation to report the HAL error return state */

  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{ 
  /* USER CODE BEGIN 6 */
	/* User can add his own implementation to report the file name and line number,
	tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值