// Status = SD_SelectDeselect((uint32_t) (SDCardInfo.RCA << 16)); // Status = SD_EnableWideBusOperation(SDIO_BusWide_4b); // if ( Status != SD_OK ) // return MAL_FAIL; // Status = SD_SetDeviceMode(SD_DMA_MODE); // if ( Status != SD_OK ) // return MAL_FAIL; // Mass_Memory_Size[0] = Mass_Block_Count[0] * Mass_Block_Size[0]; // LED1 = 0; // if (dis_mem == 0) //显示TF卡的容量 由于是周期性更新状态,所以,加了条件,只显示一次容量值 // { // printf("\r\n Micro SD卡的容量是 %d MBytes\n" ,SDCardInfo.CardCapacity); // dis_mem = 1; // } // return MAL_OK; // } // } // LED1 = 0; // return MAL_FAIL; //} u16 MAL_GetStatus (u8 lun) { uint32_t DeviceSizeMul = 0, NumberOfBlocks = 0; switch (lun) { case 0: if (dis_mem == 0) //显示TF卡的容量 由于是周期性更新状态,所以,加了条件,只显示一次容量值 { if (SD_Init() == SD_OK) { SD_GetCardInfo(&SDCardInfo); SD_SelectDeselect((uint32_t) (SDCardInfo.RCA << 16)); DeviceSizeMul = (SDCardInfo.SD_csd.DeviceSizeMul + 2); if (SDCardInfo.CardType == SDIO_HIGH_CAPACITY_SD_CARD) { Mass_Block_Count[0] = (SDCardInfo.SD_csd.DeviceSize + 1) * 1024; } else { NumberOfBlocks = ((1 << (SDCardInfo.SD_csd.RdBlockLen)) / 512); Mass_Block_Count[0] = ((SDCardInfo.SD_csd.DeviceSize + 1) * (1 << DeviceSizeMul) << (NumberOfBlocks / 2)); } Mass_Block_Size[0] = 512; Status = SD_SelectDeselect((uint32_t) (SDCardInfo.RCA << 16)); Status = SD_EnableWideBusOperation(SDIO_BusWide_4b); if ( Status != SD_OK ) return MAL_FAIL; Status = SD_SetDeviceMode(SD_DMA_MODE); if ( Status != SD_OK ) return MAL_FAIL; Mass_Memory_Size[0] = Mass_Block_Count[0] * Mass_Block_Size[0]; LED1 = 0; // if (dis_mem == 0) //显示TF卡的容量 由于是周期性更新状态,所以,加了条件,只显示一次容量值 // { printf("\r\n Micro SD卡的容量是 %lld MBytes\n" , SDCardInfo.CardCapacity >> 20); dis_mem = 1; // } return MAL_OK; } dis_mem = 1; } return MAL_OK; case 1: return MAL_OK; case 2: return MAL_FAIL; default: return MAL_FAIL; }
/******************************************************************************* * Function Name : MAL_GetStatus * Description : Get status * Input : None * Output : None * Return : None *******************************************************************************/ uint16_t MAL_GetStatus (uint8_t lun) { // NAND_IDTypeDef NAND_ID; uint32_t DeviceSizeMul = 0, NumberOfBlocks = 0; if (lun == 0) { if (SD_Init() == SD_OK) { SD_GetCardInfo(&SDCardInfo); SD_SelectDeselect((uint32_t) (SDCardInfo.RCA << 16)); DeviceSizeMul = (SDCardInfo.SD_csd.DeviceSizeMul + 2); if(SDCardInfo.CardType == SDIO_HIGH_CAPACITY_SD_CARD) { Mass_Block_Count[0] = (SDCardInfo.SD_csd.DeviceSize + 1) * 1024; } else { NumberOfBlocks = ((1 << (SDCardInfo.SD_csd.RdBlockLen)) / 512); Mass_Block_Count[0] = ((SDCardInfo.SD_csd.DeviceSize + 1) * (1 << DeviceSizeMul) << (NumberOfBlocks/2)); } Mass_Block_Size[0] = 512; Status = SD_SelectDeselect((uint32_t) (SDCardInfo.RCA << 16)); Status = SD_EnableWideBusOperation(SDIO_BusWide_4b); if ( Status != SD_OK ) { return MAL_FAIL; } Mass_Memory_Size[0] = Mass_Block_Count[0] * Mass_Block_Size[0]; return MAL_OK; } } // else // { // FSMC_NAND_ReadID(&NAND_ID); // if (NAND_ID.Device_ID != 0 ) // { // /* only one zone is used */ // Mass_Block_Count[1] = NAND_ZONE_SIZE * NAND_BLOCK_SIZE * NAND_MAX_ZONE ; // Mass_Block_Size[1] = NAND_PAGE_SIZE; // Mass_Memory_Size[1] = (Mass_Block_Count[1] * Mass_Block_Size[1]); // return MAL_OK; // } // } return MAL_FAIL; }
/******************************************************************************* * Function Name : MAL_GetStatus * Description : Get status * Input : None * Output : None * Return : None *******************************************************************************/ uint16_t MAL_GetStatus (uint8_t lun) { ; uint32_t DeviceSizeMul = 0, NumberOfBlocks = 0; if (lun == 0) { if (SD_Init() == SD_OK) { SD_GetCardInfo(&SDCardInfo); SD_SelectDeselect((uint32_t) (SDCardInfo.RCA << 16)); DeviceSizeMul = (SDCardInfo.SD_csd.DeviceSizeMul + 2); if(SDCardInfo.CardType == SDIO_HIGH_CAPACITY_SD_CARD) { Mass_Block_Count[0] = (SDCardInfo.SD_csd.DeviceSize + 1) * 1024; } else { NumberOfBlocks = ((1 << (SDCardInfo.SD_csd.RdBlockLen)) / 512); Mass_Block_Count[0] = ((SDCardInfo.SD_csd.DeviceSize + 1) * (1 << DeviceSizeMul) << (NumberOfBlocks/2)); } Mass_Block_Size[0] = 512; Mass_Memory_Size[0] = Mass_Block_Count[0] * Mass_Block_Size[0]; GPIO_SetBits(USB_LED_PORT, GPIO_Pin_7); Status = SD_SelectDeselect((uint32_t) (SDCardInfo.RCA << 16)); Status = SD_EnableWideBusOperation(SDIO_BusWide_4b); if ( Status != SD_OK ) { return MAL_FAIL; } Status = SD_SetDeviceMode(SD_DMA_MODE); if ( Status != SD_OK ) { return MAL_FAIL; } return MAL_OK; } } else { return MAL_OK; } GPIO_ResetBits(USB_LED_PORT, GPIO_Pin_7); return MAL_FAIL; }
/******************************************************************************* * Function Name : MAL_Init * Description : Initializes the Media on the STM32 * Input : None * Output : None * Return : None *******************************************************************************/ uint16_t MAL_Init(uint8_t lun) { uint16_t status = MAL_OK; switch (lun) { case 0: #ifdef USE_STM3210E_EVAL Status = SD_Init(); Status = SD_GetCardInfo(&SDCardInfo); Status = SD_SelectDeselect((uint32_t) (SDCardInfo.RCA << 16)); Status = SD_EnableWideBusOperation(SDIO_BusWide_4b); Status = SD_SetDeviceMode(SD_DMA_MODE); #else MSD_Init(); #endif break; #ifdef USE_FSMC_NAND case 1: status = NAND_Init(); break; #endif default: return MAL_FAIL; } return status; }
void SDCard_Configuration(void) { int i; for( i=0;i<109500;i++);//延时等待电容充电5760,21900//为保证多一点的板子同时脸上的时候有效就设定的大一点21900*5 /*-------------------------- SD Init ----------------------------- */ Status = SD_Init(); //disk_initialize(0);//选择磁盘,这句包括上面的SD_Init();所以只需要这一次 if (Status == SD_OK) { /*----------------- Read CSD/CID MSD registers ------------------*/ Status = SD_GetCardInfo(&SDCardInfo); /*----------------- Select Card --------------------------------*/ Status = SD_SelectDeselect((uint32_t) (SDCardInfo.RCA << 16)); /* Set Device Transfer Mode to 4b */ Status = SD_EnableWideBusOperation(SDIO_BusWide_1b); /* Set Device Transfer Mode to DMA */ Status = SD_SetDeviceMode(SD_DMA_MODE); //Status = SD_SetDeviceMode(SD_POLLING_MODE); //Status = SD_SetDeviceMode(SD_INTERRUPT_MODE); } res = f_mount(0,&fs); if(res==FR_OK) printf("\n\rFile system mounted.\n\r"); else printf("Error mounting file system.\n\r"); }
/******************************************************************************* * Function Name : MAL_Init * Description : Initializes the Media on the STM32 * 功能 : 储存介质初始化 * Input : None * Output : None * Return : None *******************************************************************************/ uint16_t MAL_Init(uint8_t lun) { uint16_t status = MAL_OK; switch (lun) { case 0: Status = SD_Init(); Status = SD_GetCardInfo(&SDCardInfo); Status = SD_SelectDeselect((uint32_t) (SDCardInfo.RCA << 16)); Status = SD_EnableWideBusOperation(SDIO_BusWide_4b); Status = SD_SetDeviceMode(SD_DMA_MODE); if (Status == SD_OK) { // 从地址0开始读取512字节 Status = SD_ReadBlock(Buffer_Block_Rx, 0x00, BlockSize); } if (Status == SD_OK) { // 返回成功的话,串口输出SD卡测试成功信息 DbgPrintf(" \n SD SDIO-4bit模式 TF OK"); } break; default: return MAL_FAIL; } return status; }
/* 注意:從函數的代碼上來看,所謂的drv這個參數,是用一個數字來代表文件硬件的類型,從其switch語句看 drv = ATA = 0:表示是IDE硬盤的一種接口 drv = MMC = 1: 表示的是MMC卡的接口,也就是SD卡 drv = USB = 2: 表示的是USB存儲設備的接口 從這裡看來,我們應該要選擇的是MMC接口,輸入參數必須為1*/ DSTATUS disk_initialize ( BYTE drv /* Physical drive nmuber (0..) */ ) { SD_Error Status; Status = SD_Init(); if (Status == SD_OK) { /*----------------- Read CSD/CID MSD registers ------------------*/ Status = SD_GetCardInfo(&SDCardInfo); } if (Status == SD_OK) { /*----------------- Select Card --------------------------------*/ Status = SD_SelectDeselect((u32) (SDCardInfo.RCA << 16)); } if (Status == SD_OK) { Status = SD_EnableWideBusOperation(SDIO_BusWide_1b); } /* Set Device Transfer Mode to DMA [設置傳輸模式為DMA方式]*/ if (Status == SD_OK) { Status = SD_SetDeviceMode(SD_INTERRUPT_MODE); } if(Status == SD_OK)return 0; else return STA_NOINIT; }
/******************************************************************************* * Function Name : MAL_Init * Description : Initializes the Media on the STM32 * Input : None * Output : None * Return : None *******************************************************************************/ uint16_t MAL_Init(uint8_t lun) { uint16_t status = MAL_OK; switch (lun) { case 0: #ifdef USE_STM3210E_EVAL Status = SD_Init(); Status = SD_GetCardInfo(&SDCardInfo); Status = SD_SelectDeselect((uint32_t) (SDCardInfo.RCA << 16)); Status = SD_EnableWideBusOperation(SDIO_BusWide_4b); Status = SD_SetDeviceMode(SD_DMA_MODE); if (Status == SD_OK) { //Status = SD_ReadMultiBlocks(0x00, Buffer_MultiBlock_Rx, BlockSize, NumberOfBlocks1); // printf("\r\nSD SDIO-4bit模式 测试TF卡读写成功! \n "); } if (Status == SD_OK) { // Read block of 512 bytes from address 0 Status = SD_ReadBlock(0x00, Buffer_Block_Rx, BlockSize); } if (Status == SD_OK) { // Check the corectness of written dada printf("\r\nSD SDIO-4bit模式 测试TF卡读写成功! \n "); } #else MSD_Init(); #endif break; #ifdef USE_STM3210E_EVAL case 1: status = NAND_Init(); break; #endif default: return MAL_FAIL; } return status; }
static int check_sdcard(void) { unsigned int DeviceSizeMul = 0, NumberOfBlocks = 0; SD_Error Status; if (SD_Init() == SD_OK) { SD_GetCardInfo(&SDIO_Info); SD_SelectDeselect((unsigned int) (SDIO_Info.RCA << 16)); DeviceSizeMul = (SDIO_Info.SD_csd.DeviceSizeMul + 2); if(SDIO_Info.CardType == SDIO_HIGH_CAPACITY_SD_CARD) { SDIO_Block_Count = (SDIO_Info.SD_csd.DeviceSize + 1) * 1024; SDIO_Block_Size = 1; } else { NumberOfBlocks = ((1 << (SDIO_Info.SD_csd.RdBlockLen)) / 512); SDIO_Block_Count = ((SDIO_Info.SD_csd.DeviceSize + 1) * (1 << DeviceSizeMul) << (NumberOfBlocks/2)); SDIO_Block_Size = 512; } Status = SD_SelectDeselect((unsigned int) (SDIO_Info.RCA << 16)); Status = SD_EnableWideBusOperation(SDIO_BusWide_4b); if ( Status != SD_OK ) { return -1; } Status = SD_SetDeviceMode(SD_DMA_MODE); if ( Status != SD_OK ) { return -1; } SDIO_Memory_Size = SDIO_Block_Count * SDIO_Block_Size; if( SDIO_Memory_Size >= (2*1024 * 1024 * 1024)) // * 512)) { return 0; // 卡大于2G } else return -1; } return -1; }
uint16_t MAL_GetStatus (uint8_t lun) { uint32_t DeviceSizeMul = 0, NumberOfBlocks = 0; uint64_t dwDevSize; /* nemui */ if (lun == 0) { if (SD_Init() == SD_OK) { SD_GetCardInfo(&SDCardInfo); SD_SelectDeselect((uint32_t) (SDCardInfo.RCA << 16)); DeviceSizeMul = (SDCardInfo.SD_csd.DeviceSizeMul + 2); if(SDCardInfo.CardType == SDIO_HIGH_CAPACITY_SD_CARD) { /* nemui */ dwDevSize = (uint64_t)(SDCardInfo.SD_csd.DeviceSize + 1) * 512 * 1024; /* nemui calculate highest LBA */ Mass_Block_Count[0] = (dwDevSize - 1) / 512; } else { NumberOfBlocks = ((1 << (SDCardInfo.SD_csd.RdBlockLen)) / 512); Mass_Block_Count[0] = ((SDCardInfo.SD_csd.DeviceSize + 1) * (1 << DeviceSizeMul) << (NumberOfBlocks/2)); } Mass_Block_Size[0] = 512; Status = SD_SelectDeselect((uint32_t) (SDCardInfo.RCA << 16)); Status = SD_EnableWideBusOperation(SDIO_BusWide_4b); if ( Status != SD_OK ) { return MAL_FAIL; } Mass_Memory_Size[0] = Mass_Block_Count[0] * Mass_Block_Size[0]; return MAL_OK; } } return MAL_FAIL; }
/******************************************************************************* * Function Name : MAL_GetStatus * Description : Get status * Input : None * Output : None * Return : None *******************************************************************************/ uint16_t MAL_GetStatus (uint8_t lun) { NAND_IDTypeDef NAND_ID; u32 DeviceSizeMul = 0, NumberOfBlocks = 0; if (lun == 0) { if (SD_Init() == SD_OK) { SD_GetCardInfo(&SDCardInfo); SD_SelectDeselect((u32) (SDCardInfo.RCA << 16)); DeviceSizeMul = (SDCardInfo.SD_csd.DeviceSizeMul + 2); if(SDCardInfo.CardType == SDIO_HIGH_CAPACITY_SD_CARD) { Mass_Block_Count[0] = (SDCardInfo.SD_csd.DeviceSize + 1) * 1024; } else { NumberOfBlocks = ((1 << (SDCardInfo.SD_csd.RdBlockLen)) / 512); Mass_Block_Count[0] = ((SDCardInfo.SD_csd.DeviceSize + 1) * (1 << DeviceSizeMul) << (NumberOfBlocks/2)); } Mass_Block_Size[0] = 512; Status = SD_SelectDeselect((u32) (SDCardInfo.RCA << 16)); Status = SD_EnableWideBusOperation(SDIO_BusWide_4b); if ( Status != SD_OK ) return MAL_FAIL; Status = SD_SetDeviceMode(SD_DMA_MODE); if ( Status != SD_OK ) return MAL_FAIL; } } else { FSMC_NAND_ReadID(&NAND_ID); if (NAND_ID.Device_ID != 0 ) { //printf("\r\n Device ID : %02X %02X" ,NAND_ID.Maker_ID,NAND_ID.Device_ID ); /* only one zone is used */ Mass_Block_Count[1] = NAND_ZONE_SIZE * NAND_BLOCK_SIZE * NAND_MAX_ZONE ; Mass_Block_Size[1] = NAND_PAGE_SIZE; Mass_Memory_Size[1] = (Mass_Block_Count[1] * Mass_Block_Size[1]); return MAL_OK; } } return MAL_FAIL; }
//初始化SD卡 //返回值:错误代码;(0,无错误) SD_Error SD_Init(void) { SD_Error errorstatus=SD_OK; u8 clkdiv=0; //SDIO IO口初始化 RCC->AHB1ENR|=1<<2; //使能PORTC时钟 RCC->AHB1ENR|=1<<3; //使能PORTD时钟 RCC->AHB1ENR|=1<<22; //DMA2时钟使能 RCC->APB2ENR|=1<<11; //SDIO时钟使能 RCC->APB2RSTR|=1<<11; //SDIO复位 GPIO_Set(GPIOC,0X1F<<8,GPIO_MODE_AF,GPIO_OTYPE_PP,GPIO_SPEED_50M,GPIO_PUPD_PU); //PC8,9,10,11,12复用功能输出 GPIO_Set(GPIOD,1<<2,GPIO_MODE_AF,GPIO_OTYPE_PP,GPIO_SPEED_50M,GPIO_PUPD_PU); //PD2复用功能输出 GPIO_AF_Set(GPIOC,8,12); //PC8,AF12 GPIO_AF_Set(GPIOC,9,12); //PC9,AF12 GPIO_AF_Set(GPIOC,10,12); //PC10,AF12 GPIO_AF_Set(GPIOC,11,12); //PC11,AF12 GPIO_AF_Set(GPIOC,12,12); //PC12,AF12 GPIO_AF_Set(GPIOD,2,12); //PD2,AF12 RCC->APB2RSTR&=~(1<<11); //SDIO结束复位 //SDIO外设寄存器设置为默认值 SDIO->POWER=0x00000000; SDIO->CLKCR=0x00000000; SDIO->ARG=0x00000000; SDIO->CMD=0x00000000; SDIO->DTIMER=0x00000000; SDIO->DLEN=0x00000000; SDIO->DCTRL=0x00000000; SDIO->ICR=0x00C007FF; SDIO->MASK=0x00000000; MY_NVIC_Init(0,0,SDIO_IRQn,2); //SDIO中断配置 errorstatus=SD_PowerON(); //SD卡上电 if(errorstatus==SD_OK)errorstatus=SD_InitializeCards(); //初始化SD卡 if(errorstatus==SD_OK)errorstatus=SD_GetCardInfo(&SDCardInfo); //获取卡信息 if(errorstatus==SD_OK)errorstatus=SD_SelectDeselect((u32)(SDCardInfo.RCA<<16));//选中SD卡 if(errorstatus==SD_OK)errorstatus=SD_EnableWideBusOperation(1); //4位宽度,如果是MMC卡,则不能用4位模式 if((errorstatus==SD_OK)||(SDIO_MULTIMEDIA_CARD==CardType)) { if(SDCardInfo.CardType==SDIO_STD_CAPACITY_SD_CARD_V1_1||SDCardInfo.CardType==SDIO_STD_CAPACITY_SD_CARD_V2_0) { clkdiv=SDIO_TRANSFER_CLK_DIV+2; //V1.1/V2.0卡,设置最高48/4=12Mhz }else clkdiv=SDIO_TRANSFER_CLK_DIV; //SDHC等其他卡,设置最高48/2=24Mhz SDIO_Clock_Set(clkdiv); //设置时钟频率,SDIO时钟计算公式:SDIO_CK时钟=SDIOCLK/[clkdiv+2];其中,SDIOCLK固定为48Mhz //errorstatus=SD_SetDeviceMode(SD_DMA_MODE); //设置为DMA模式 errorstatus=SD_SetDeviceMode(SD_POLLING_MODE);//设置为查询模式 } return errorstatus; }
/**************************************************************************** * 名 称:void SD_TEST(void) * 功 能:SD卡测试函数 * 入口参数:无 * 出口参数:无 * 说 明: * 调用方法:无 ****************************************************************************/ void SD_TEST(void){ Status = SD_Init(); Status = SD_GetCardInfo(&SDCardInfo); Status = SD_SelectDeselect((uint32_t) (SDCardInfo.RCA << 16)); Status = SD_EnableWideBusOperation(SDIO_BusWide_4b); Status = SD_SetDeviceMode(SD_DMA_MODE); if (Status == SD_OK) { // 从地址0开始读取512字节 Status = SD_ReadBlock(Buffer_Block_Rx, 0x00, 512); } if (Status == SD_OK) { // 返回成功的话,串口输出SD卡测试成功信息 USART_OUT(USART1,"\r\nSD SDIO-4bit模式 测试TF卡成功! \n "); } }
/******************************************************************************* * Function Name : MAL_Init * Description : Initializes the Media on the STM32 * Input : None * Output : None * Return : None *******************************************************************************/ uint16_t MAL_Init(uint8_t lun) { uint16_t status = MAL_OK; switch (lun) { case 0: Status = SD_Init(); Status = SD_GetCardInfo(&SDCardInfo); Status = SD_SelectDeselect((u32) (SDCardInfo.RCA << 16)); Status = SD_EnableWideBusOperation(SDIO_BusWide_4b); Status = SD_SetDeviceMode(SD_DMA_MODE); break; case 1: status = NAND_Init(); break; default: return MAL_FAIL; } return status; }
/******************************************************************************* * Function Name : main * Description : Main program. * Input : None * Output : None * Return : None *******************************************************************************/ int main(void) { #ifdef DEBUG debug(); #endif /* static uint8_t sector[SECTOR_SIZE]; static uint8_t transferbuffer[SECTOR_SIZE]; uint32_t pstart, psize, i; uint8_t pactive, ptype; VOLINFO vi; DIRINFO di; FILEINFO fi; DIRENT de; uint32_t cache; */ // Clock Config: HSE 72 MHz RCC_Configuration(); // Interrupt Config NVIC_Configuration(); // USART Config : 115200,8,n,1 Serial_Init(); ///////////////////////////////////////////////////////////////////// //////// SDCARD Initialisation ////////////////////////////////////// /////////////////Section adapted from ST example///////////////////// /*-------------------------- SD Init ----------------------------- */ Status = SD_Init(); if (Status == SD_OK) { /*----------------- Read CSD/CID MSD registers ------------------*/ Status = SD_GetCardInfo(&SDCardInfo); } if (Status == SD_OK) { /*----------------- Select Card --------------------------------*/ Status = SD_SelectDeselect((u32) (SDCardInfo.RCA << 16)); } if (Status == SD_OK) { //Status = SD_EnableWideBusOperation(SDIO_BusWide_4b); } /* Set Device Transfer Mode to DMA */ if (Status == SD_OK) { // Status = SD_SetDeviceMode(SD_DMA_MODE);//oet // Status = SD_SetDeviceMode(SD_POLLING_MODE); Status = SD_SetDeviceMode(SD_INTERRUPT_MODE); } OutPutFile(); ///////////////////////////////////////////////////////////////////// //////// DOSFS Volume Acess ///////////////////////////////////////// ///////////////////////////////////////////////////////////////////// /* if (DFS_ReadSector(0,sector,0,1)) { printf("Cannot read media!\n"); while(1); return -1; } if ((sector[0x1FE] == 0x55) & (sector[0x1FF] == 0xAA)) { printf("The end of the last read sector contains a FAT or MBR end delimiter\n"); if ((sector[0x36] == 'F') & (sector[0x37] == 'A') & (sector[0x38] == 'T')) { printf("Well, this is not a MBR, but a FAT12/16 header!\n"); pstart = 0; pactive = 0x80; ptype = 0x06; psize = 0xFFFFFFFF; } else if ((sector[0x52] == 'F') & (sector[0x53] == 'A') & (sector[0x54] == 'T')) { printf("Well, this is not a MBR, but a FAT32 header!\n"); pstart = 0; pactive = 0x80; ptype = 0x06; psize = 0xFFFFFFFF; } else { pstart = DFS_GetPtnStart(0, sector, 0, &pactive, &ptype, &psize); if (pstart == 0xffffffff) { printf("Cannot find first partition\n"); return -1; } } } printf("Partition 0 start sector 0x%-08.8lX active %-02.2hX type %-02.2hX size %-08.8lX\n", pstart, pactive, ptype, psize); if (DFS_GetVolInfo(0, sector, pstart, &vi)) { printf("Error getting volume information\n"); return -1; } printf("Volume label '%-11.11s'\n", vi.label); printf("%d sector/s per cluster, %d reserved sector/s, volume total %d sectors.\n", vi.secperclus, vi.reservedsecs, vi.numsecs); printf("%d sectors per FAT, first FAT at sector #%d, root dir at #%d.\n",vi.secperfat,vi.fat1,vi.rootdir); printf("(For FAT32, the root dir is a CLUSTER number, FAT12/16 it is a SECTOR number)\n"); printf("%d root dir entries, data area commences at sector #%d.\n",vi.rootentries,vi.dataarea); printf("%d clusters (%d bytes) in data area, filesystem IDd as ", vi.numclusters, vi.numclusters * vi.secperclus * SECTOR_SIZE); if (vi.filesystem == FAT12) printf("FAT12.\n"); else if (vi.filesystem == FAT16) printf("FAT16.\n"); else if (vi.filesystem == FAT32) printf("FAT32.\n"); else printf("[unknown]\n"); ///////////////////////////////////////////////////////////////////// //////// DOSFS File list Acess ////////////////////////////////////// ///////////////////////////////////////////////////////////////////// // Directory enumeration test di.scratch = sector; if (DFS_OpenDir(&vi, "", &di)) { printf("Error opening root directory\n"); return -1; } ////This section is to list a directory "/MYDIR1" if (DFS_OpenDir(&vi, "MYDIR1", &di)) { printf("error opening subdirectory\n"); return -1; } // while (!DFS_GetNext(&vi, &di, &de)) { if (de.name[0]) printf("file: '%-11.11s'\n", de.name); } //------------------------------------------------------------ // File read test printf("\n=====Read test\n"); if (DFS_OpenFile(&vi, "WVO.TXT", DFS_READ, sector, &fi)) { printf("error opening file\n"); return -1; } DFS_ReadFile(&fi, sector, transferbuffer, &i, 512); printf("read complete %d bytes (expected %d) pointer %d\n", i, fi.filelen, fi.pointer); printf("%s",transferbuffer); //------------------------------------------------------------ // File write test printf("\n=====Write test\n"); if (DFS_OpenFile(&vi, "WRTEST.TXT", DFS_WRITE, sector, &fi)) { printf("error opening file\n"); return -1; } DFS_WriteFile(&fi, sector, transferbuffer, &cache, SECTOR_SIZE/2); */ printf("\nLet's loop for infinity, and beyond!\n"); while (1) {} }
/******************************************************************************* * Function Name : MAL_GetStatus * Description : Get status * Input : None * Output : None * Return : None *******************************************************************************/ uint16_t MAL_GetStatus (uint8_t lun) { #ifdef USE_STM3210E_EVAL NAND_IDTypeDef NAND_ID; uint32_t DeviceSizeMul = 0, NumberOfBlocks = 0; #else //uint32_t temp_block_mul = 0; //SD_CSD SD_csd; //uint32_t DeviceSizeMul = 0; #endif /* USE_STM3210E_EVAL */ if (lun == 0) { #ifdef USE_STM3210E_EVAL if (SD_Init() == SD_OK) { SD_GetCardInfo(&mSDCardInfo); SD_SelectDeselect((uint32_t) (mSDCardInfo.RCA << 16)); DeviceSizeMul = (mSDCardInfo.SD_csd.DeviceSizeMul + 2); if(mSDCardInfo.CardType == SDIO_HIGH_CAPACITY_SD_CARD) { Mass_Block_Count[0] = (mSDCardInfo.SD_csd.DeviceSize + 1) * 1024; } else { NumberOfBlocks = ((1 << (mSDCardInfo.SD_csd.RdBlockLen)) / 512); Mass_Block_Count[0] = ((mSDCardInfo.SD_csd.DeviceSize + 1) * (1 << DeviceSizeMul) << (NumberOfBlocks/2)); } Mass_Block_Size[0] = 512; Status = SD_SelectDeselect((uint32_t) (mSDCardInfo.RCA << 16)); Status = SD_EnableWideBusOperation(SDIO_BusWide_4b); if ( Status != SD_OK ) { return MAL_FAIL; } Status = SD_SetDeviceMode(SD_DMA_MODE); if ( Status != SD_OK ) { return MAL_FAIL; } #else //SD_GetCSDRegister(&SD_csd); //DeviceSizeMul = SD_csd.DeviceSizeMul + 2; //temp_block_mul = (1 << SD_csd.RdBlockLen)/ 512;/* Physical drive number (0) */ while(Sd_Spi_Called_From_USB_MSC){;} if(disk_ioctl (0, GET_SECTOR_COUNT, &Mass_Block_Count[0]))//Sectors are the same as blocks and 512 bytes long? return MAL_FAIL; //Mass_Block_Count[0] = ((SD_csd.DeviceSize + 1) * (1 << (DeviceSizeMul))) * temp_block_mul; Mass_Block_Size[0] = 512; #endif /* USE_STM3210E_EVAL */ Mass_Memory_Size[0] = Mass_Block_Count[0] * Mass_Block_Size[0]; #ifdef CRT GREEN_LED_ON; #else STM_EVAL_LEDOn(LED2); #endif return MAL_OK; #ifdef USE_STM3210E_EVAL } #endif /* USE_STM3210E_EVAL */ } #ifdef USE_STM3210E_EVAL else { FSMC_NAND_ReadID(&NAND_ID); if (NAND_ID.Device_ID != 0 ) { /* only one zone is used */ Mass_Block_Count[1] = NAND_ZONE_SIZE * NAND_BLOCK_SIZE * NAND_MAX_ZONE ; Mass_Block_Size[1] = NAND_PAGE_SIZE; Mass_Memory_Size[1] = (Mass_Block_Count[1] * Mass_Block_Size[1]); return MAL_OK; } } #endif /* USE_STM3210E_EVAL */ #ifdef CRT GREEN_LED_OFF; #else STM_EVAL_LEDOff(LED2); #endif return MAL_FAIL; }
/******************************************************************************* * Function Name : MAL_GetStatus * Description : Get status * Input : None * Output : None * Return : None *******************************************************************************/ uint16_t MAL_GetStatus (uint8_t lun) { #ifdef USE_STM3210E_EVAL NAND_IDTypeDef NAND_ID; uint32_t DeviceSizeMul = 0, NumberOfBlocks = 0; #else SD_CSD SD_csd; uint32_t DeviceSizeMul = 0; #endif /* USE_STM3210E_EVAL */ #ifdef USE_STM32L152D_EVAL uint32_t NumberOfBlocks = 0; #endif if (lun == 0) { #if defined (USE_STM3210E_EVAL) || defined(USE_STM32L152D_EVAL) if (SD_Init() == SD_OK) { SD_GetCardInfo(&mSDCardInfo); SD_SelectDeselect((uint32_t) (mSDCardInfo.RCA << 16)); DeviceSizeMul = (mSDCardInfo.SD_csd.DeviceSizeMul + 2); if(mSDCardInfo.CardType == SDIO_HIGH_CAPACITY_SD_CARD) { Mass_Block_Count[0] = (mSDCardInfo.SD_csd.DeviceSize + 1) * 1024; } else { NumberOfBlocks = ((1 << (mSDCardInfo.SD_csd.RdBlockLen)) / 512); Mass_Block_Count[0] = ((mSDCardInfo.SD_csd.DeviceSize + 1) * (1 << DeviceSizeMul) << (NumberOfBlocks/2)); } Mass_Block_Size[0] = 512; Status = SD_SelectDeselect((uint32_t) (mSDCardInfo.RCA << 16)); Status = SD_EnableWideBusOperation(SDIO_BusWide_4b); if ( Status != SD_OK ) { return MAL_FAIL; } #else uint32_t temp_block_mul = 0; SD_GetCSDRegister(&SD_csd); DeviceSizeMul = SD_csd.DeviceSizeMul + 2; temp_block_mul = (1 << SD_csd.RdBlockLen)/ 512; Mass_Block_Count[0] = ((SD_csd.DeviceSize + 1) * (1 << (DeviceSizeMul))) * temp_block_mul; Mass_Block_Size[0] = 512; Mass_Memory_Size[0] = (Mass_Block_Count[0] * Mass_Block_Size[0]); #endif /* USE_STM3210E_EVAL */ Mass_Memory_Size[0] = Mass_Block_Count[0] * Mass_Block_Size[0]; LED_On1(); return MAL_OK; #if defined (USE_STM3210E_EVAL) || defined(USE_STM32L152D_EVAL) } #endif /* USE_STM3210E_EVAL */ } #ifdef USE_STM3210E_EVAL else { FSMC_NAND_ReadID(&NAND_ID); if (NAND_ID.Device_ID != 0 ) { /* only one zone is used */ Mass_Block_Count[1] = NAND_ZONE_SIZE * NAND_BLOCK_SIZE * NAND_MAX_ZONE ; Mass_Block_Size[1] = NAND_PAGE_SIZE; Mass_Memory_Size[1] = (Mass_Block_Count[1] * Mass_Block_Size[1]); return MAL_OK; } } #endif /* USE_STM3210E_EVAL */ LED_On1(); return MAL_FAIL; }
/******************************************************************************* * Function Name : MAL_GetStatus * Description : Get status * Input : None * Output : None * Return : None *******************************************************************************/ uint16_t MAL_GetStatus (uint8_t lun) { #ifdef USE_STM3210E_EVAL uint32_t DeviceSizeMul = 0, NumberOfBlocks = 0; #else uint32_t temp_block_mul = 0; sMSD_CSD MSD_csd; uint32_t DeviceSizeMul = 0; #endif #ifdef USE_FSMC_NAND NAND_IDTypeDef NAND_ID; #endif if (lun == 0) { #ifdef USE_STM3210E_EVAL if (SD_Init() == SD_OK) { SD_GetCardInfo(&SDCardInfo); SD_SelectDeselect((uint32_t) (SDCardInfo.RCA << 16)); DeviceSizeMul = (SDCardInfo.SD_csd.DeviceSizeMul + 2); if(SDCardInfo.CardType == SDIO_HIGH_CAPACITY_SD_CARD) { Mass_Block_Count[0] = (SDCardInfo.SD_csd.DeviceSize + 1) * 1024; } else { NumberOfBlocks = ((1 << (SDCardInfo.SD_csd.RdBlockLen)) / 512); Mass_Block_Count[0] = ((SDCardInfo.SD_csd.DeviceSize + 1) * (1 << DeviceSizeMul) << (NumberOfBlocks/2)); } Mass_Block_Size[0] = 512; Status = SD_SelectDeselect((uint32_t) (SDCardInfo.RCA << 16)); Status = SD_EnableWideBusOperation(SDIO_BusWide_4b); if ( Status != SD_OK ) { return MAL_FAIL; } Status = SD_SetDeviceMode(SD_DMA_MODE); if ( Status != SD_OK ) { return MAL_FAIL; } #else MSD_GetCSDRegister(&MSD_csd); DeviceSizeMul = MSD_csd.DeviceSizeMul + 2; temp_block_mul = (1 << MSD_csd.RdBlockLen)/ 512; Mass_Block_Count[0] = ((MSD_csd.DeviceSize + 1) * (1 << (DeviceSizeMul))) * temp_block_mul; Mass_Block_Size[0] = 512; Mass_Memory_Size[0] = (Mass_Block_Count[0] * Mass_Block_Size[0]); #endif Mass_Memory_Size[0] = Mass_Block_Count[0] * Mass_Block_Size[0]; GPIO_SetBits(USB_LED_PORT, GPIO_Pin_7); return MAL_OK; #ifdef USE_STM3210E_EVAL } #endif } #ifdef USE_FSMC_NAND else { FSMC_NAND_ReadID(&NAND_ID); if (NAND_ID.Device_ID != 0 ) { //only one zone is used Mass_Block_Count[1] = NAND_ZONE_SIZE * NAND_BLOCK_SIZE * NAND_MAX_ZONE ; Mass_Block_Size[1] = NAND_PAGE_SIZE; Mass_Memory_Size[1] = (Mass_Block_Count[1] * Mass_Block_Size[1]); return MAL_OK; } } #endif GPIO_ResetBits(USB_LED_PORT, GPIO_Pin_7); return MAL_FAIL; }
DSTATUS disk_initialize ( BYTE drv /* Physical drive nmuber (0..) */ ) { SD_Error Status; if(drv==0) { Status = SD_Init(); // printf("SD Init : %d\r\n",Status); if(Status != SD_OK) { return STA_NOINIT; //其他错误:初始化失败 } else { Status = SD_GetCardInfo(&SDCardInfo); //读sd卡信息 // printf("SD_GetCardInfo : %d\r\n",Status); if (Status != SD_OK) { return STA_NOINIT;//RES_NOTRDY; //报NOT READY错误 } // Select Card Status = SD_SelectDeselect((u32) (SDCardInfo.RCA << 16)); if (Status != SD_OK) { return STA_NOINIT;//RES_NOTRDY; //报NOT READY错误 } switch(SD_Mode) { case 0: //dma方式 Status = SD_EnableWideBusOperation(SDIO_BusWide_1b); if (Status != SD_OK) { return RES_NOTRDY; //报NOT READY错误 } Status = SD_SetDeviceMode(SD_DMA_MODE); if (Status != SD_OK) { return RES_NOTRDY; //报NOT READY错误 } break; case 1: //中断方式 Status = SD_EnableWideBusOperation(SDIO_BusWide_1b); if (Status != SD_OK) { return RES_NOTRDY; //报NOT READY错误 } Status = SD_SetDeviceMode(SD_INTERRUPT_MODE); if (Status != SD_OK) { return RES_NOTRDY; //报NOT READY错误 } break; default : return RES_NOTRDY; } return 0; //初始化成功 } } else//仅支持磁盘0的操作 { return STA_NOINIT; } }
/******************************************************************************* * Function Name : MAL_GetStatus * Description : Get status * Input : None * Output : None * Return : None *******************************************************************************/ uint16_t MAL_GetStatus (uint8_t lun) { #ifdef USE_STM3210E_EVAL NAND_IDTypeDef NAND_ID; uint32_t DeviceSizeMul = 0, NumberOfBlocks = 0; #else uint32_t temp_block_mul = 0; sMSD_CSD MSD_csd; uint32_t DeviceSizeMul = 0; #endif if (lun == 0) { #ifdef USE_STM3210E_EVAL if (SD_Init() == SD_OK) { SD_GetCardInfo(&SDCardInfo); SD_SelectDeselect((uint32_t) (SDCardInfo.RCA << 16)); DeviceSizeMul = (SDCardInfo.SD_csd.DeviceSizeMul + 2); if(SDCardInfo.CardType == SDIO_HIGH_CAPACITY_SD_CARD) { Mass_Block_Count[0] = (SDCardInfo.SD_csd.DeviceSize + 1) * 1024; } else { NumberOfBlocks = ((1 << (SDCardInfo.SD_csd.RdBlockLen)) / 512); Mass_Block_Count[0] = ((SDCardInfo.SD_csd.DeviceSize + 1) * (1 << DeviceSizeMul) << (NumberOfBlocks/2)); } Mass_Block_Size[0] = 512; Status = SD_SelectDeselect((uint32_t) (SDCardInfo.RCA << 16)); Status = SD_EnableWideBusOperation(SDIO_BusWide_4b); if ( Status != SD_OK ) { return MAL_FAIL; } Status = SD_SetDeviceMode(SD_DMA_MODE); if ( Status != SD_OK ) { return MAL_FAIL; } #else MSD_GetCSDRegister(&MSD_csd); DeviceSizeMul = MSD_csd.DeviceSizeMul + 2; temp_block_mul = (1 << MSD_csd.RdBlockLen)/ 512; Mass_Block_Count[0] = ((MSD_csd.DeviceSize + 1) * (1 << (DeviceSizeMul))) * temp_block_mul; Mass_Block_Size[0] = 512; Mass_Memory_Size[0] = (Mass_Block_Count[0] * Mass_Block_Size[0]); #endif Mass_Memory_Size[0] = Mass_Block_Count[0] * Mass_Block_Size[0]; //STM_EVAL_LEDOn(LED2); if(dis_mem==0){ //显示TF卡的容量 由于是周期性更新状态,所以,加了条件,只显示一次容量值 //printf("\r\n Micro SD卡的容量是 %d MBytes\n" ,Mass_Memory_Size[0]/1024/1024); dis_mem=1; } return MAL_OK; #ifdef USE_STM3210E_EVAL } #endif } #ifdef USE_STM3210E_EVAL else { FSMC_NAND_ReadID(&NAND_ID); if (NAND_ID.Device_ID != 0 ) { // printf("\r\n Device ID : %02X %02X" ,NAND_ID.Maker_ID,NAND_ID.Device_ID ); /* only one zone is used */ Mass_Block_Count[1] = NAND_ZONE_SIZE * NAND_BLOCK_SIZE * NAND_MAX_ZONE ; Mass_Block_Size[1] = NAND_PAGE_SIZE; Mass_Memory_Size[1] = (Mass_Block_Count[1] * Mass_Block_Size[1]); return MAL_OK; } } #endif //STM_EVAL_LEDOn(LED2); return MAL_FAIL; }
/******************************************************************************* * Function Name : main. * Description : main routine. * Input : None. * Output : None. * Return : None. *******************************************************************************/ int main(void) { GPIO_InitTypeDef GPIO_InitStructure; #ifdef DEBUG debug(); #endif Set_System(); Serial_Init(); ///////////////////////////////////////////////////////////////////// //////// SDCARD Initialisation ////////////////////////////////////// /////////////////Section adapted from ST example///////////////////// /*-------------------------- SD Init ----------------------------- */ Status = SD_Init(); if (Status == SD_OK) { Status = SD_GetCardInfo(&SDCardInfo); } if (Status == SD_OK) { /*----------------- Select Card --------------------------------*/ Status = SD_SelectDeselect((uint32_t) (SDCardInfo.RCA << 16)); } if (Status == SD_OK) { Status = SD_SetDeviceMode(SD_DMA_MODE); //Status = SD_SetDeviceMode(SD_INTERRUPT_MODE); //Status = SD_SetDeviceMode(SD_POLLING_MODE); } if (Status == SD_OK) { //Status = SD_ReadBlock(0x00, Buffer_Block_Rx, BlockSize); //Status = SD_WriteBlock(0x00, Buffer_Block_Tx, BlockSize); Status = SD_ReadMultiBlocks(0x00, Buffer_MultiBlock_Rx, BlockSize, NumberOfBlocks); } if (Status == SD_OK) { /* Read block of 512 bytes from address 0 */ Status = SD_ReadBlock(0x00, Buffer_Block_Rx, BlockSize); } if (Status == SD_OK) { /* Read block of many bytes from address 0 */ Status = SD_ReadMultiBlocks(0x00, Buffer_MultiBlock_Rx, BlockSize, NumberOfBlocks); } if (Status == SD_OK) { /* Check the corectness of written dada */ printf("\r\nSD SDIO-1bit模式 测试TF卡读写成功! \n "); } Get_Medium_Characteristics(); Set_USBClock(); USB_Interrupts_Config(); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD; GPIO_Init(GPIOC, &GPIO_InitStructure); USB_Init(); while (1) { //printf("\nTEST OK!\n"); //if (JoyState() != 0) //{ // Joystick_Send(JoyState()); //} } }
uint8_t BSP_TEST_SDIO(void) { uint8_t t_status = 0; /*-------------------------- SD Init ----------------------------- */ BSP_SDCARD_ENABLE(); Status = SD_Init(); if (Status == SD_OK) { /*----------------- Read CSD/CID MSD registers ------------------*/ Status = SD_GetCardInfo(&SDCardInfo); } if (Status == SD_OK) { /*----------------- Select Card --------------------------------*/ Status = SD_SelectDeselect((uint32_t) (SDCardInfo.RCA << 16)); } if (Status == SD_OK) { Status = SD_EnableWideBusOperation(SDIO_BusWide_4b); } /*------------------- Block Erase -------------------------------*/ if (Status == SD_OK) { /* Erase NumberOfBlocks Blocks of WRITE_BL_LEN(512 Bytes) */ Status = SD_Erase(0x00, (SDIO_BLOCK_SIZE * SDIO_NB_BLOCK)); } /* Set Device Transfer Mode to DMA */ if (Status == SD_OK) { Status = SD_SetDeviceMode(SD_DMA_MODE); } if (Status == SD_OK) { Status = SD_ReadMultiBlocks(0x00, Buffer_MultiBlock_Rx, SDIO_BLOCK_SIZE, SDIO_NB_BLOCK); } if (Status == SD_OK) { t_status = eBuffercmp(Buffer_MultiBlock_Rx, SDIO_MULTIWSIZE); } /*------------------- Block Read/Write --------------------------*/ /* Fill the buffer to send */ Fill_Buffer(Buffer_Block_Tx, SDIO_BUFFERW_SIZE, 0xFFFF); if (Status == SD_OK) { /* Write block of 512 bytes on address 0 */ Status = SD_WriteBlock(0x00, Buffer_Block_Tx, SDIO_BLOCK_SIZE); } if (Status == SD_OK) { /* Read block of 512 bytes from address 0 */ Status = SD_ReadBlock(0x00, Buffer_Block_Rx, SDIO_BLOCK_SIZE); } if (Status == SD_OK) { /* Check the corectness of written dada */ t_status &= Buffercmp(Buffer_Block_Tx, Buffer_Block_Rx, SDIO_BUFFERW_SIZE); } BSP_SDCARD_DISABLE(); return t_status; }
DSTATUS disk_initialize ( BYTE drv /* Physical drive nmuber (0..) */ ) { SD_CardInfo mSDCardInfo; /* 定义SD卡状态信息结构体 */ uint32_t DeviceSizeMul = 0, NumberOfBlocks = 0; uint16_t Status; switch(drv) { case SD_CARD://SD卡 //SD卡初始化-------------------------------------------------------------------------- Status = SD_Init(); if (Status != SD_OK) { fs_printf("SD_Init() fail (%d) : file %s on line %d\r\n", Status, __FILE__, __LINE__); goto retfail; } SD_GetCardInfo(&mSDCardInfo); /* 读取SD卡的信息 */ SD_SelectDeselect((uint32_t) (mSDCardInfo.RCA << 16)); DeviceSizeMul = (mSDCardInfo.SD_csd.DeviceSizeMul + 2); if (mSDCardInfo.CardType == SDIO_HIGH_CAPACITY_SD_CARD) /* 高容量SD卡 SDHC */ { Mass_Block_Count = (mSDCardInfo.SD_csd.DeviceSize + 1) * 1024; } else /* 普通SD卡, 最大4G */ { NumberOfBlocks = ((1 << (mSDCardInfo.SD_csd.RdBlockLen)) / 512); Mass_Block_Count = ((mSDCardInfo.SD_csd.DeviceSize + 1) * (1 << DeviceSizeMul) << (NumberOfBlocks/2)); } Status = SD_SelectDeselect((uint32_t) (mSDCardInfo.RCA << 16)); Status = SD_EnableWideBusOperation(SDIO_BusWide_4b); if (Status != SD_OK) { fs_printf("SD_EnableWideBusOperation(SDIO_BusWide_4b) Fail (%d)\r\n", Status); goto retfail; } Status = SD_SetDeviceMode(SD_DMA_MODE); /* 设置SD卡工作模式为DMA, 其它模式由中断、轮询 */ if (Status != SD_OK) { fs_printf("SD_SetDeviceMode(SD_DMA_MODE) Fail (%d)\r\n", Status); goto retfail; } /* 显示容量 */ Mass_Block_Size = 512; if (mSDCardInfo.CardType == SDIO_HIGH_CAPACITY_SD_CARD) /* 高容量SD卡 SDHC */ { fs_printf("SDHC Card, Memory Size = %uMB\r\n", Mass_Block_Count / (1024 * 4)); } else { fs_printf("Normal Card, Memory Size = %uMB\r\n", (Mass_Block_Count * Mass_Block_Size) /(1024*1024)); } //SD卡初始化结束-------------------------------------------------------------------------- break; case EX_FLASH://外部flash SPI_Flash_Init(); if(SPI_FLASH_TYPE==W25Q64)FLASH_SECTOR_COUNT=2048*6;//W25Q64 else FLASH_SECTOR_COUNT=2048*2; //其他 break; default: goto retfail; } return RES_OK; retfail: return RES_ERROR; }