📄 sst25vf016b.c
字号:
/**************************************************************************************
软件驱动程序(C8051F020)
SST25VF016B 16 Mbit(2M x 8) 串行Flash存储器
(2007年4月19日)
关于此程序:
此程序为用户使用该FLASH提供了一个完整功能函数库,用户可根据自己的需要使用这些函数
Read_Status_Register 读取状态寄存器
EWSR 使能写状态寄存器
WRSR 改写状态寄存器
WREN 写使能
WRDI 写禁止
EBSY 允许使用MISO 作为AAI模式下RY/BY#的输出脚
DBSY 禁止用MISO输出RY/BY#
HighSpeed_Read 读取一个字节(最大50Mhz时钟频率)并返回byte
HighSpeed_Read_Cont 连续读取(最大50 MHz时钟频率)
Byte_Program 写一个字节数据
Auto_Add_IncA 初始化Auto Address Increment(AAI)
Auto_Add_IncB AAI初始化后进入Auto_Address_Increment(AAI)
Chip_Erase 擦除整个芯片
Block_Erase_32K 擦除一块32 KByte的区域
Block_Erase_64K 擦除一块64 KByte的区域
Wait_Busy 等待空闲(状态寄存器的BUSY位为0)
Wait_Busy_AAI AAI模式下等待空闲
WREN_Check 检查WEL是否被置位
WREN_AAI_Check 检查WEL和AAI模式位被置位
**************************************************************************************/
#include <C8051F020.h>
#include "SST25VF016B.H"
#include "SPI.h"
/*************************************************************************
/*函数名称:SST_SPIInit()
/*函数功能:SST25VF016B SPI总线初始化
/*输入参数:无
/*输出参数:无
*************************************************************************/
void SST_SPIInit(void)
{
SPI_CFG(0,5);
SPI_Init();
}
/*************************************************************************
/*函数名称:SST_Init()
/*函数功能:SST25VF016B 器件初始化
/*输入参数:无
/*输出参数:无
*************************************************************************/
void SST_Init(void)
{
SST_SPIInit();//总线初始化
WREN();//写使能
EWSR();//使能改写状态寄存器操作
WRSR(0x02);//取消所有块保护
DBSY();//禁止MISO在AAI模式下作为输出RY/BY#状态的信号
}
/************************************************************************/
/* 程序名称: Read_Status_Register */
/* 程序功能: 用来读取状态寄存器,并返回状态寄存器的值 */
/* 输入: None */
/* 返回: byte */
/************************************************************************/
unsigned char Read_Status_Register()
{
unsigned char byte = 0;
SST_CE =0; /* 使能设备 */
Send_Byte(0x05); /* 发送读状态寄存器的命令 */
byte = Get_Byte(); /* 读取状态寄存器 */
SST_CE =1; /* 禁止设备 */
return byte;
}
/************************************************************************/
/* 程序名称: EWSR */
/* 程序功能: 使能改写状态寄存器操作 */
/* 输入: None */
/* 返回: Nothing */
/************************************************************************/
void EWSR()
{
SST_CE =0; /* 使能设备*/
Send_Byte(0x50); /* 发送使能写寄存器的命令 */
SST_CE =1; /* 禁止设备*/
}
/************************************************************************/
/* 程序名称: WRSR */
/* 程序功能: 往状态寄存器里写一个字节 */
/* 输入: byte */
/* 返回: Nothing */
/************************************************************************/
void WRSR(unsigned char byte)
{
SST_CE =0; /* 使能设备 */
Send_Byte(0x01); /* 发送写状态寄存器 */
Send_Byte(byte); /* 改变寄存器里BPx或者BPL (只有2,3,4,5,7位可以改写)*/
SST_CE =1; /* 禁止设备 */
}
/************************************************************************/
/* 程序名称: WREN */
/* 程序功能: 写使能,同样可以用于使能写状态寄存器 */
/* 输入: None */
/* 返回: Nothing */
/************************************************************************/
void WREN()
{
SST_CE =0;
Send_Byte(0x06); /* 发送WREN命令 */
SST_CE =1;
}
/************************************************************************/
/* 名称: WRDI */
/* 功能: 写禁止 */
/* 输入: None */
/* 返回: Nothing */
/************************************************************************/
void WRDI()
{
SST_CE =0;
Send_Byte(0x04); /* 发送WRDI命令*/
SST_CE =1;
}
/************************************************************************/
/* 名称: EBSY */
/* 功能: 允许MISO在AAI模式期间输出RY/BY# 状态 */
/* 输入: None */
/* 返回: Nothing */
/************************************************************************/
void EBSY()
{
SST_CE =0;
Send_Byte(0x70); /* 发送EBSY命令*/
SST_CE =1;
}
/************************************************************************/
/* 名称: DBSY */
/* 功能: 禁止MISO在AAI模式下作为输出RY/BY#状态的信号 */
/* 输入: None */
/* 返回: Nothing */
/************************************************************************/
void DBSY()
{
SST_CE =0;
Send_Byte(0x80); /* 发送DBSY命令 */
SST_CE =1;
}
/************************************************************************/
/* 名称: Wait_Busy */
/* 功能: 等待芯片空闲(在执行Byte-Program, Sector-Erase, Block-Erase */
/* Chip-Erase操作后) */
/* 输入: None */
/* 返回: Nothing */
/************************************************************************/
void Wait_Busy()
{
while ((Read_Status_Register())&0x01 == 0x01)/* waste time until not busy */
Read_Status_Register();
}
/************************************************************************/
/* 名称: Wait_Busy_AAI */
/* 功能: 在AAI模式下等待芯片空闲 */
/* 输入: None */
/* 返回: Nothing */
/************************************************************************/
void Wait_Busy_AAI()
{
while (Read_Status_Register() == 0x43) /* 等待空闲 */
Read_Status_Register();
}
/************************************************************************/
/* 名称: WREN_Check */
/* 功能: 检查擦写操作前WEL位是否为1 */
/* 输入: None */
/* 返回: Nothing */
/************************************************************************/
void WREN_Check()
{
unsigned char byte;
byte = Read_Status_Register(); /* 读取状态register */
if ((byte&0x02) != 0x02) /* 检查WEL位置位 */
{
WREN();
//如果未置1进行相应处理,如对其进行写使得操作
}
}
/************************************************************************/
/* 名称: WREN_AAI_Check */
/* 功能: 在AAI模式下检查AAI和WEL位 */
/************************************************************************/
void WREN_AAI_Check()
{
unsigned char byte;
byte = Read_Status_Register(); /*读取状态寄存器*/
if (byte != 0x42) /* 核实AAI 和 WEL位被置1 */
{
while(1);
SST_Init();/*如果发生错误进行相应处理*/
}
}
/************************************************************************/
/* 名称: HighSpeed_Read */
/* 功能: 高速读取一个字节 */
/* 输入: Dst(目标地址 000000H - 1FFFFFH) */
/* 返回: byte */
/************************************************************************/
unsigned char HighSpeed_Read(unsigned long Dst)
{
unsigned char byte = 0;
SST_CE =0; /* 使能芯片 */
Send_Byte(0x0B); /* 发送指令 */
Send_Byte(((Dst & 0xFFFFFF) >> 16));/* 发送3字节的地址*/
Send_Byte(((Dst & 0xFFFF) >> 8));
Send_Byte(Dst & 0xFF);
Send_Byte(0xFF);/*虚拟字节*/
byte = Get_Byte();
SST_CE =1; /* 禁止芯片 */
return byte;/* 返回读取的一个字节 */
}
/************************************************************************/
/* 名称: HighSpeed_Read_Cont */
/* 功能: 高速读取芯片连续地址的内容,最大可读取128字节 */
/* 输入: Dst:(目标地址 000000H - 1FFFFFH) */
/* no_bytes: 读取字节数 (最大128字节) */
/* upper_128[]: 读取数据存放地址指针 */
/* 返回: Nothing */
/************************************************************************/
void HighSpeed_Read_Cont(unsigned long Dst, unsigned char no_bytes,unsigned char upper_128[])
{
unsigned char i = 0;
SST_CE =0; /* 芯片使能 */
Send_Byte(0x0B); /* 发送读取指令 */
Send_Byte(((Dst & 0xFFFFFF) >> 16)); /* 发送3字节的地址 */
Send_Byte(((Dst & 0xFFFF) >> 8));
Send_Byte(Dst & 0xFF);
Send_Byte(0xFF); /*虚拟字节*/
for (i = 0; i < no_bytes; i++) /* 读取no_bytes字节*/
{
upper_128[i] = Get_Byte(); /*接收数据存入 80H - FFH */
}
SST_CE =1; /* 禁止芯片*/
}
/************************************************************************/
/* 名称: Byte_Program */
/* 功能: 写一个字节数据,被写的地址必须被擦除及未被保护 */
/* 输入: */
/* Dst: (目标地址 000000H - 1FFFFFH) */
/* byte: 数据 */
/* 返回: */
/* Nothing */
/************************************************************************/
void Byte_Program(unsigned long Dst, unsigned char byte)
{
WREN();
SST_CE =0; /* 芯片使能 */
Send_Byte(0x02); /* 发送写操作指令 */
Send_Byte(((Dst & 0xFFFFFF) >> 16));/* 发送3字节地址 */
Send_Byte(((Dst & 0xFFFF) >> 8));
Send_Byte(Dst & 0xFF);
Send_Byte(byte); /*发送要写的数据*/
SST_CE =1;
Wait_Busy();
}
/************************************************************************/
/* 名称: Auto_Add_IncA */
/* 功能: 写连续地址 */
/* 输入: */
/* Dst: (目标地址 000000H - 1FFFFFH) */
/* byte1: 第1字节数据 */
/* byte1: 第2字节数据 */
/* 返回: */
/* Nothing */
/************************************************************************/
void Auto_Add_IncA(unsigned long Dst, unsigned char byte1, unsigned char byte2)
{
WREN();
SST_CE =0;
Send_Byte(0xAD); /* 发送AAI命令*/
Send_Byte(((Dst & 0xFFFFFF) >> 16)); /* 发送地址*/
Send_Byte(((Dst & 0xFFFF) >> 8));
Send_Byte(Dst & 0xFF);
Send_Byte(byte1); /* 发送第一个字节 */
Send_Byte(byte2); /* 发送第二个字节 */
SST_CE =1;
Wait_Busy_AAI();
}
/************************************************************************/
/* 名称: 工作Auto_Add_IncB */
/* 功能: 连续写入2字节,使用在Auto_Address_IncA之后 */
/* 进入AAI模式后仅WRDI及AAI操作指令能被执行,因为SO作为RY/BY#状态输出*/
/* 输入: */
/* byte1: 1st byte to be programmed */
/* byte2: 2nd byte to be programmed */
/* state: AAI模式下结束AAI编程模式标志0-->AAI正常编程,1-->最后一次*/
/* 写完数据后退出AAI模式 */
/* 返回: */
/* Nothing */
/************************************************************************/
void Auto_Add_IncB(bit state,unsigned char byte1, unsigned char byte2)
{
WREN();
SST_CE =0;
Send_Byte(0xAD); /* 发送AAI命令*/
Send_Byte(byte1); /* 发送第一个字节 */
Send_Byte(byte2); /* 发送第二个字节*/
SST_CE =1;
Wait_Busy_AAI();
if(state==1)
{
WRDI();//从写禁止中退出AAI模式
}
}
/************************************************************************/
/* 名称: Chip_Erase */
/* 功能: 擦除整个芯片 */
/* 输入: None */
/* 返回: Nothing */
/************************************************************************/
void Chip_Erase()
{
WREN_Check();
SST_CE =0;
Send_Byte(0x60); /* 发送 Chip Erase命令 (60h or C7h) */
SST_CE =1;
Wait_Busy();
}
/************************************************************************/
/* 名称: Block_Erase_32K */
/* 功能: Block Erases 32 KByte of the Chip. */
/* 输入: Dst: 目标地址 000000H - 1FFFFFH */
/* 返回: Nothing */
/************************************************************************/
void Block_Erase_32K(unsigned long Dst)
{
WREN_Check();
SST_CE =0;
Send_Byte(0x52); /* 发送32 KByte Block Erase命令*/
Send_Byte(((Dst & 0xFFFFFF) >> 16)); /* 发送3bytes地址*/
Send_Byte(((Dst & 0xFFFF) >> 8));
Send_Byte(Dst & 0xFF);
SST_CE =1;
Wait_Busy();
}
/************************************************************************/
/* 名称: Block_Erase_64K */
/* 功能: Block Erases 64 KByte */
/* 输入: Dst: 目标地址000000H - 1FFFFFH */
/* 返回: Nothing */
/************************************************************************/
void Block_Erase_64K(unsigned long Dst)
{
WREN_Check();
SST_CE =0;
Send_Byte(0xD8); /* 发送64KByte Block Erase 命令 */
Send_Byte(((Dst & 0xFFFFFF) >> 16)); /* 发送3 bytes地址 */
Send_Byte(((Dst & 0xFFFF) >> 8));
Send_Byte(Dst & 0xFF);
SST_CE =1;
Wait_Busy();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -