TRTOS 操作NandFlash驱动程序

本文档详细介绍了如何在TRTOS操作系统中初始化和操作Nand_Flash,包括设置GPIO、初始化FSMC、配置NAND Flash参数、读写页操作以及擦除块等功能。此外,还提供了设备挂载的实现方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

##include "include.h"
#include "Nand_Flash.h"
void Nand_FSMC_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
FSMC_NAND_PCCARDTimingInitTypeDef p;
FSMC_NANDInitTypeDef FSMC_NANDInitStructure;


RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE |
RCC_APB2Periph_GPIOF | RCC_APB2Periph_GPIOG, ENABLE);


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);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10;
GPIO_Init(GPIOE, &GPIO_InitStructure);

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);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_Init(GPIOG, &GPIO_InitStructure);

p.FSMC_SetupTime = 0x1; //建立时间
p.FSMC_WaitSetupTime = 0x3; //等待时间
p.FSMC_HoldSetupTime = 0x2; //保持时间
p.FSMC_HiZSetupTime = 0x1; //高阻建立时间
FSMC_NANDInitStructure.FSMC_Bank = FSMC_Bank2_NAND; //使用FSMC BANK2
FSMC_NANDInitStructure.FSMC_Waitfeature = FSMC_Waitfeature_Enable; //使能FSMC的等待功能
FSMC_NANDInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_8b; //NAND Flash的数据宽度为8位
FSMC_NANDInitStructure.FSMC_ECC = FSMC_ECC_Enable; //使能ECC特性
FSMC_NANDInitStructure.FSMC_ECCPageSize = FSMC_ECCPageSize_2048Bytes; //ECC页大小2048
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_NANDCmd(FSMC_Bank2_NAND, ENABLE);
}

uint8 Nand_GetState(void)
{
NAND_CMD=0x70;
return NAND_DAT8;
}


void Nand_WriteAddr(uint32 RowAddr,uint16 ColAddr)
{
NAND_ADR16=ColAddr;
NAND_ADR=(uint8)RowAddr;
NAND_ADR16=(RowAddr>>8);
}



uint32 Nand_GetID(void)
{
NAND_CMD=0x90;
NAND_ADR=0x00;
return NAND_DAT32;
}



void Nand_ReadPage(uint32 StartPage,uint8 *rBuffer,uint16 ByteOffset,uint16 Length)
{
uint16 *DP=(uint16 *)rBuffer;
NAND_CMD=0x00;
Nand_WriteAddr(StartPage,ByteOffset);
NAND_CMD=0x30;
Tos_TaskDelay(1);
Length>>=1;
do{
*DP++=NAND_DAT16;
}while(--Length);
}



void Nand_ReadPage_Random(uint16 ByteOffset,uint8 *rBuffer,uint16 Length)
{
NAND_CMD=0x05;
NAND_ADR=(uint8)(ByteOffset);
NAND_ADR=(uint8)(ByteOffset>>8); //写入页内偏移地址
NAND_CMD=0xE0;
while(Length--)*rBuffer++=NAND_DAT8;
}



void Nand_FillPage(uint32 StartPage,uint8 Data,uint16 ByteOffset,uint16 Length)
{
uint32 T;
T=Data;T<<=8;T|=Data;T<<=8;T|=Data;T<<=8;T|=Data; // 合并成32Bit数据
NAND_CMD=0x80;
Nand_WriteAddr(StartPage,ByteOffset);
Length>>=2;
do{NAND_DAT32=T;}while(--Length);
NAND_CMD=0x10;
}



void Nand_WritePage(uint32 StartPage,uint8 *wBuffer,uint16 ByteOffset,uint16 Length)
{
NAND_CMD=0x80;
Nand_WriteAddr(StartPage,ByteOffset);
while(Length--)NAND_DAT8=*wBuffer++;
NAND_CMD=0x10;
}



void Nand_WritePage_WriteStart(uint32 StartPage)
{
NAND_CMD=0x80;
Nand_WriteAddr(StartPage,0); // 按照物理页大小进行读取
}


void Nand_WritePage_WriteRandom(uint16 ByteOffset,uint8 *wBuffer,uint16 Length)
{
NAND_CMD=0x85;
NAND_ADR=(uint8)(ByteOffset);
NAND_ADR=(uint8)(ByteOffset>>8);
while(Length--)NAND_DAT8=*wBuffer++;
}



void Nand_WritePage_WriteStop(void)
{
NAND_CMD=0x10;
}


void Nand_CopyBack(uint32 StartPage,uint32 DestPage)
{
NAND_CMD=0x00;
Nand_WriteAddr(StartPage,0);
NAND_CMD=0x35;
NAND_CMD=0x85;
Nand_WriteAddr(DestPage,0);
NAND_CMD=0x10;
}



void Nand_CopyBack_WriteStart(uint32 StartPage,uint32 DestPage)
{
NAND_CMD=0x00;
Nand_WriteAddr(StartPage,0);
NAND_CMD=0x35;
NAND_CMD=0x85;
Nand_WriteAddr(DestPage,0);
}



void Nand_CopyBack_WriteRandom(uint16 ByteOffset,uint8 *wBuffer,uint16 Length)
{
// uint32 *DP=(uint32 *)wBuffer;
NAND_CMD=0x85;
NAND_ADR=(uint8)(ByteOffset);
NAND_ADR=(uint8)(ByteOffset>>8);
while(Length--)NAND_DAT8=*wBuffer++;
// Length>>=2;
// do{NAND_DAT32=*DP++;}while(--Length);
}


void Nand_CopyBack_WriteStop(void)
{
NAND_CMD=0x10;
}




void Nand_EraseBlock(uint32 BlockAddr)
{
NAND_CMD=0x60;
BlockAddr*=NAND_BLOCK_SIZE; //转换到块地址
NAND_ADR=(uint8)BlockAddr;
NAND_ADR=(uint8)(BlockAddr>>8);
NAND_ADR=(uint8)(BlockAddr>>16);
NAND_CMD=0xD0;
}

void NandFlash_Write(void *Addr,void *Data,void *Length)
{

}
void NandFlash_Read(void *Addr,void *Data,void *Length)
{

}
void DeviceMount_NandFlash()
{
_Tos_Device_Tab[DeviceId_NandFlash].DeviceId=DeviceId_NandFlash;
_Tos_Device_Tab[DeviceId_NandFlash].DeviceName="NandFlash";
_Tos_Device_Tab[DeviceId_NandFlash].DeviceOwnerId=Null;
_Tos_Device_Tab[DeviceId_NandFlash].DeviceVirtue=ReadOnly|WriteOnly|CharDerice;
_Tos_Device_Tab[DeviceId_NandFlash].DeviceState=Ready;
_Tos_Device_Tab[DeviceId_NandFlash].Init=Nand_FSMC_Init;
_Tos_Device_Tab[DeviceId_NandFlash].Write=NandFlash_Write;
_Tos_Device_Tab[DeviceId_NandFlash].Read=NandFlash_Read;
_Tos_Device_Tab[DeviceId_NandFlash].Exit=Null;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值