STM32-移植FATFS的NANDFLASH驱动(3)
- UID
- 1029342
- 性别
- 男
|
STM32-移植FATFS的NANDFLASH驱动(3)
2,然后对媒介格式化,创建读写文件:
[cpp] view plaincopy
- void test_fatfs(void)
- {
- FATFS fs;
- FIL fl;
- FATFS *pfs;
- DWORD clust;
- unsigned int r,w,i;
- FRESULT res;
- // NF_CHKDSK(0,1024);
- display_page(0,0);
- // for mount
- res=f_mount(0,&fs);
- printf("f_mount=%x \n\r",res);
- // for format
- //res=f_mkfs(0,1,2048); //MUST Format for New NANDFLASH !!!
- //printf("f_mkfs=%x \n\r",res);
- // for
- pfs=&fs;
- res = f_getfree("/", &clust, &pfs);
- printf("f_getfree=%x \n\r",res);
- printf("\n\r%lu MB total drive space."
- "\n\r%lu MB available.\n\r",
- (DWORD)(pfs->max_clust - 2) * pfs->csize /2/1024,
- clust * pfs->csize /2/1024);
- // for read
- res=f_open(&fl,"/test2.dat",FA_OPEN_EXISTING | FA_READ);
- printf("f_open=%x \n\r",res);
- for(i=0;i<2;i++)
- {
- for(r = 0; r < NAND_PAGE_SIZE; r++)
- {
- RxBuffer[r]= 0xff;
- }
- res=f_read(&fl,RxBuffer,NAND_PAGE_SIZE,&r);
- printf("f_read=%x \n\r",res);
- if(res || r == 0)break;
- for(r = 0; r < NAND_PAGE_SIZE; r++)
- {
- printf("D[%08x]=%02x ",(i*NAND_PAGE_SIZE+r),RxBuffer[r]);
- if((r%8)==7)
- {printf("\n\r");}
- }
- }
- f_close(&fl);
- // for write
- res=f_open(&fl,"/test2.dat",FA_CREATE_ALWAYS | FA_WRITE);
- printf("f_open=%x \n\r",res);
- for(i=0;i<2;i++)
- {
- for(w = 0; w < NAND_PAGE_SIZE; w++)
- {
- TxBuffer[w]=((w<<0)&0xff);
- }
- res=f_write(&fl,TxBuffer,NAND_PAGE_SIZE,&w);
- printf("f_write=%x \n\r",res);
- if(res || w
- }
- f_close(&fl);
- // for umount
- f_mount(0,NULL);
- }
六,编写NANDFLASH接口
1,fsmc_nand.c文件:
[cpp] view plaincopy
- /* Includes ------------------------------------------------------------------*/
- #include "fsmc_nand.h"
- #include "stm32f10x_conf.h"
- /** @addtogroup StdPeriph_Examples
- * @{
- */
- /** @addtogroup FSMC_NAND
- * @{
- */
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- #define FSMC_Bank_NAND FSMC_Bank2_NAND
- #define Bank_NAND_ADDR Bank2_NAND_ADDR
- #define Bank2_NAND_ADDR ((uint32_t)0x70000000)
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- /* Private function prototypes -----------------------------------------------*/
- /* Private functions ---------------------------------------------------------*/
- /**
- * @brief Configures the FSMC and GPIOs to interface with the NAND memory.
- * This function must be called before any write/read operation
- * on the NAND.
- * @param None
- * @retval : None
- */
- void FSMC_NAND_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- FSMC_NANDInitTypeDef FSMC_NANDInitStructure;
- FSMC_NAND_PCCARDTimingInitTypeDef p;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE |
- RCC_APB2Periph_GPIOF | RCC_APB2Periph_GPIOG, ENABLE);
- /*-- GPIO Configuration ------------------------------------------------------*/
- /* CLE, ALE, D0->D3, NOE, NWE and NCE2 NAND pin configuration */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_14 | GPIO_Pin_15 |
- GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_4 | GPIO_Pin_5 |
- GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- /* D4->D7 NAND pin configuration */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10;
- GPIO_Init(GPIOE, &GPIO_InitStructure);
- /* NWAIT NAND pin configuration */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- /* INT2 NAND pin configuration */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
- GPIO_Init(GPIOG, &GPIO_InitStructure);
- /*-- FSMC Configuration ------------------------------------------------------*/
- p.FSMC_SetupTime = 0x1;
- p.FSMC_WaitSetupTime = 0x3;
- p.FSMC_HoldSetupTime = 0x2;
- p.FSMC_HiZSetupTime = 0x1;
- FSMC_NANDInitStructure.FSMC_Bank = FSMC_Bank2_NAND;
- FSMC_NANDInitStructure.FSMC_Waitfeature = FSMC_Waitfeature_Enable;
- FSMC_NANDInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_8b;
- FSMC_NANDInitStructure.FSMC_ECC = FSMC_ECC_Enable;
- FSMC_NANDInitStructure.FSMC_ECCPageSize = FSMC_ECCPageSize_512Bytes;
- FSMC_NANDInitStructure.FSMC_TCLRSetupTime = 0x00;
- FSMC_NANDInitStructure.FSMC_TARSetupTime = 0x00;
- FSMC_NANDInitStructure.FSMC_CommonSpaceTimingStruct = &p;
- FSMC_NANDInitStructure.FSMC_AttributeSpaceTimingStruct = &p;
- FSMC_NANDInit(&FSMC_NANDInitStructure);
- /* FSMC NAND Bank Cmd Test */
- FSMC_NANDCmd(FSMC_Bank2_NAND, ENABLE);
- }
|
|
|
|
|
|