/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "delay.h"
#include "bsp_lcd.h"
#include "bsp_printf.h"
#include "bsp_sdram.h"
#include "bsp_malloc.h"
#include "bsp_ftl.h"
#include "bsp_nand.h"
#include "bsp_key.h"
#include "bsp_sdmmc.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
uint16_t FBuffer[LCD_WIDTH*LCD_HEIGHT] __attribute__((at(0xC0000000)));
uint32_t POINT_COLOR; //画笔颜色
uint32_t BACK_COLOR; //背景色
uint8_t rx_done;
uint8_t tx_done;
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
DMA2D_HandleTypeDef hdma2d;
LTDC_HandleTypeDef hltdc;
SD_HandleTypeDef hsd1;
UART_HandleTypeDef huart1;
NAND_HandleTypeDef hnand1;
SDRAM_HandleTypeDef hsdram1;
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_FMC_Init(void);
static void MX_LTDC_Init(void);
static void MX_DMA2D_Init(void);
static void MX_USART1_UART_Init(void);
static void MX_SDMMC1_SD_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
//通过串口打印SD卡相关信息
void show_sdcard_info(void)
{
HAL_SD_CardCIDTypeDef cid;
switch(hsd1.SdCard.CardVersion)
{
case CARD_V1_X:printf("Card Version:CARD_V1_X\r\n");break;
case CARD_V2_X:printf("Card Version:CARD_V2_X\r\n");break;
}
switch(hsd1.SdCard.CardType)
{
case CARD_SDSC:printf("Card Type:CARD_SDSC\r\n");break;
case CARD_SDHC_SDXC:printf("Card Type:CARD_SDHC_SDXC\r\n");break;
case CARD_SECURED:printf("Card Type:CARD_SECURED\r\n");break;
}
if(HAL_OK != HAL_SD_GetCardCID(&hsd1, &cid))
{
Error_Handler();
}
printf("Card ManufacturerID:%d\r\n",cid.ManufacturerID); //制造商ID
printf("Card RCA:%d\r\n",hsd1.SdCard.RelCardAdd ); //卡相对地址
printf("Card Capacity:%d MB\r\n",(uint32_t)(((uint64_t)hsd1.SdCard.BlockNbr*hsd1.SdCard.BlockSize)>>20)); //显示容量
printf("Card BlockSize:%d\r\n\r\n",hsd1.SdCard.BlockSize); //显示块大小
printf("Card LogBlockNbr:%d\r\n\r\n",hsd1.SdCard.LogBlockNbr);
printf("Card LogBlockSize:%d\r\n\r\n",hsd1.SdCard.LogBlockSize);
}
/* 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 */
SCB_EnableICache();//使能I-Cache
SCB_EnableDCache();//使能D-Cache
SCB->CACR|=1<<2; //强制D-Cache透写,如不开启,实际使用中可能遇到各种问题
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_FMC_Init();
MX_LTDC_Init();
MX_DMA2D_Init();
MX_USART1_UART_Init();
MX_SDMMC1_SD_Init();
/* USER CODE BEGIN 2 */
//HAL_GPIO_WritePin(GPIOF, GPIO_PIN_6|GPIO_PIN_7, GPIO_PIN_SET);
delay_init(216);
delay_ms(5000);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_SET);
Sdram_Init_Sequence();
my_mem_init(SRAMIN); //初始化内部内存池
my_mem_init(SRAMEX); //初始化外部SDRAM内存池
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
uint8_t key;
uint8_t *buf;
uint32_t sd_size;
uint32_t i;
show_sdcard_info(); //打印SD卡相关信息
POINT_COLOR=BLUE; //设置字体为蓝色
BACK_COLOR = WHITE;
LTDC_Clear(WHITE);
LCD_ShowString(30,150,200,16,16,"SD Card OK ");
LCD_ShowString(30,170,200,16,16,"SD Card Size: MB");
LCD_ShowNum(30+13*8,170,(uint32_t)(((uint64_t)hsd1.SdCard.BlockNbr*hsd1.SdCard.BlockSize)>>20),5,16);//显示SD卡容量
while (1)
{
key=KEY_Scan(0);
if(key==KEY0_PRES)//KEY0按下了
{
buf=mymalloc(SRAMIN,512); //申请内存
if(SD_ReadDisk(buf,0,1)==0) //读取0扇区的内容
{
LCD_ShowString(30,190,200,16,16,"USART1 Sending Data...");
printf("SECTOR 0 DATA:\r\n");
for(sd_size=0;sd_size<512;sd_size++)printf("%x ",buf[sd_size]);//打印0扇区数据
printf("\r\nDATA ENDED\r\n");
LCD_ShowString(30,190,200,16,16,"USART1 Send Data Over!");
}
myfree(SRAMIN,buf);//释放内存
}
else if(key==KEY1_PRES)
{
buf=mymalloc(SRAMIN,512); //申请内存
for(i=0; i<512; i++)
{
buf[i] += 6;
}
if(0 == SD_WriteDisk(buf,0,1))
{
printf("\r\nwrite success!\r\n");
}
else
{
printf("\r\nwrite failed!\r\n");
}
myfree(SRAMIN,buf);//释放内存
}
delay_ms(10);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief SDMMC1 Initialization Function
* @param None
* @retval None
*/
static void MX_SDMMC1_SD_Init(void)
{
/* USER CODE BEGIN SDMMC1_Init 0 */
/* USER CODE END SDMMC1_Init 0 */
/* USER CODE BEGIN SDMMC1_Init 1 */
/* USER CODE END SDMMC1_Init 1 */
hsd1.Instance = SDMMC1;
hsd1.Init.ClockEdge = SDMMC_CLOCK_EDGE_RISING;
hsd1.Init.ClockBypass = SDMMC_CLOCK_BYPASS_DISABLE;
hsd1.Init.ClockPowerSave = SDMMC_CLOCK_POWER_SAVE_DISABLE;
hsd1.Init.BusWide = SDMMC_BUS_WIDE_1B;
hsd1.Init.HardwareFlowControl = SDMMC_HARDWARE_FLOW_CONTROL_DISABLE;
hsd1.Init.ClockDiv = 0;
if (HAL_SD_Init(&hsd1) != HAL_OK)
{
Error_Handler();
}
if (HAL_SD_ConfigWideBusOperation(&hsd1, SDMMC_BUS_WIDE_4B) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN SDMMC1_Init 2 */
/* USER CODE END SDMMC1_Init 2 */
}
#ifndef __BSP_SDMMC_H
#define __BSP_SDMMC_H
#ifdef __cplusplus
extern "C" {
#endif
#include "main.h"
uint8_t SD_ReadDisk(uint8_t* buf,uint32_t sector,uint8_t cnt);
uint8_t SD_WriteDisk(uint8_t *buf,uint32_t sector,uint8_t cnt);
#ifdef __cplusplus
}
#endif
#endif /* __BSP_SDMMC_H */
#include "bsp_sdmmc.h"
#include
#include "delay.h"
//SD_ReadDisk/SD_WriteDisk函数专用buf,当这两个函数的数据缓存区地址不是4字节对齐的时候,
//需要用到该数组,确保数据缓存区地址是4字节对齐的.
__align(4) uint8_t SDIO_DATA_BUFFER[512];
//读SD卡
//buf:读数据缓存区
//sector:扇区地址
//cnt:扇区个数
//返回值:错误状态;0,正常;其他,错误代码;
uint8_t SD_ReadDisk(uint8_t* buf,uint32_t sector,uint8_t cnt)
{
uint8_t sta=HAL_OK;
uint8_t n;
// __set_PRIMASK(0);
if((uint32_t)buf%4!=0)
{
for(n=0;n<cnt;n++)
{
rx_done = 0;
sta=HAL_SD_ReadBlocks_IT(&hsd1, SDIO_DATA_BUFFER, sector+n, 1);//单个sector的读操作
while(!rx_done);
memcpy(buf,SDIO_DATA_BUFFER,BLOCKSIZE);
buf+=512;
}
}else
{
rx_done = 0;
sta=HAL_SD_ReadBlocks_IT(&hsd1, buf, sector, cnt);//单个sector的读操作
while(!rx_done);
}
// __set_PRIMASK(1);
while(1)
{
if(4 == HAL_SD_GetCardState(&hsd1))
{
break;
}
delay_us(100);
}
return sta;
}
//写SD卡
//buf:写数据缓存区
//sector:扇区地址
//cnt:扇区个数
//返回值:错误状态;0,正常;其他,错误代码;
uint8_t SD_WriteDisk(uint8_t *buf,uint32_t sector,uint8_t cnt)
{
uint8_t sta=HAL_OK;
uint8_t n;
// __set_PRIMASK(0);
if((uint32_t)buf%4!=0)
{
for(n=0;n<cnt;n++)
{
memcpy(SDIO_DATA_BUFFER,buf,BLOCKSIZE);
tx_done = 0;
sta=HAL_SD_WriteBlocks_IT(&hsd1, SDIO_DATA_BUFFER, sector+n, 1);
while(!tx_done);
buf+=BLOCKSIZE;
}
}else
{
tx_done = 0;
sta=HAL_SD_WriteBlocks_IT(&hsd1, buf, sector, cnt);
while(!tx_done);
}
// __set_PRIMASK(1);
while(1)
{
if(4 == HAL_SD_GetCardState(&hsd1))
{
break;
}
delay_us(100);
}
return sta;
}
//while(1)
//{
// if(4 == HAL_SD_GetCardState(&hsd1))
// {
// break;
// }
// delay_us(100);
//}
/**
* @brief This function handles SDMMC1 global interrupt.
*/
void SDMMC1_IRQHandler(void)
{
/* USER CODE BEGIN SDMMC1_IRQn 0 */
/* USER CODE END SDMMC1_IRQn 0 */
HAL_SD_IRQHandler(&hsd1);
/* USER CODE BEGIN SDMMC1_IRQn 1 */
/* USER CODE END SDMMC1_IRQn 1 */
}
/* USER CODE BEGIN 1 */
/**
* @brief Rx Transfer completed callbacks
* @param hsd: Pointer SD handle
* @retval None
*/
void HAL_SD_RxCpltCallback(SD_HandleTypeDef *hsd)
{
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_1);
rx_done = 1;
}
/**
* @brief Tx Transfer completed callbacks
* @param hsd: Pointer to SD handle
* @retval None
*/
void HAL_SD_TxCpltCallback(SD_HandleTypeDef *hsd)
{
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_1);
tx_done = 1;
}
/**
* @brief SD error callbacks
* @param hsd: Pointer SD handle
* @retval None
*/
void HAL_SD_ErrorCallback(SD_HandleTypeDef *hsd)
{
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_0);
}
总结:
1、
2、
3、如果不使能ICACHE、DCACHE,程序会运行到void HAL_SD_ErrorCallback(SD_HandleTypeDef *hsd)中
4、读写数据时加不加屏蔽全局中断__set_PRIMASK(0);没有看出有什么区别
5、后来发现__set_PRIMASK(0);并没有屏蔽全局中断,否则就不会进入中断函数,while()循环就出不来,但是试了__disable_irq();、INTX_DISABLE();这两个函数确实能屏蔽中断,这个问题后面再看
6、读写后需要加入一段代码判断SD卡是否就绪
while(1)
{
if(4 == HAL_SD_GetCardState(&hsd1))
{
break;
}
delay_us(100);
}
7、
时钟分频比设为25时,不使能ICACHE、DCACHE程序也是正常的