//****************************************************************************
//Copyright (c) 2010-2060 Nader CO,.LTD
//Creation date: 2020.11.11
//Creator: Wanglanxin
//****************************************************************************
#include "spi_flash.h"
//******************************************************************************
//uint8_t SPI1_ReadByte(void)
//******************************************************************************
uint8_t SPI1_ReadByte() //读1字节,下降沿有效
{
uint8_t i,data = 0;
SCK_low;
asm("NOP");
asm("NOP");
for(i=0;i<8;i++)
{
data <<= 1; //左移1位,按从高到底的顺序读取
if(MISO) //输入为高
data |= 1; //BIT0置1
asm("NOP");
asm("NOP");
SCK_high;
asm("NOP");
asm("NOP");
SCK_low;
asm("NOP");
asm("NOP");
}
return data;
}
//******************************************************************************
//void SPI1_WriteByte(uint8_t data)
//******************************************************************************
void SPI1_WriteByte(uint8_t data) //写1字节,上升沿有效
{
uint8_t i;
SCK_low;
asm("NOP");
asm("NOP");
for(i=0;i<8;i++)
{
if((data<<i)&0x80) //按从高到底的顺序发送,发送位为1
MOSI_high; //输出高电平
else
MOSI_low; //输出低电平
asm("NOP");
asm("NOP");
SCK_high;
asm("NOP");
asm("NOP");
SCK_low;
asm("NOP");
asm("NOP");
}
}
//******************************************************************************
// void Flash_Read //读数据
//
// Input: uint32_t address 存储器地址
// uint8_t *data 数据缓冲区
// uint16_t num 字节个数
//
//******************************************************************************
void Flash_Read(uint32_t address, uint8_t *data, uint16_t num)
{
uint8_t addr[3]; //存储器地址
addr[0] = (uint8_t)(address>>16);
addr[1] = (uint8_t)(address>>8);
addr[2] = (uint8_t)address;
WP_high;
CS_low;
SPI1_WriteByte(READ); //读指令
SPI1_WriteByte(addr[0]); //地址:高8位
SPI1_WriteByte(addr[1]); //地址:中8位
SPI1_WriteByte(addr[2]); //地址:低8位
uint16_t i;
for(i=0;i<num;i++)
{
data[i]= SPI1_ReadByte(); //读数据
}
CS_high;
WP_low;
}
//******************************************************************************
// void Flash_Write //写数据
//
// Input: uint32_t address 存储器地址
// uint8_t *data 数据缓冲区
// uint16_t num 字节个数
//
//******************************************************************************
void Flash_Write(uint32_t address, uint8_t *data, uint16_t num)
{
uint8_t addr[3]; //存储器地址
addr[0] = (uint8_t)(address>>16);
addr[1] = (uint8_t)(address>>8);
addr[2] = (uint8_t)address;
WP_high;
CS_low;
SPI1_WriteByte(WREN); //写使能
CS_high;
CS_low;
SPI1_WriteByte(WRITE); //写指令
SPI1_WriteByte(addr[0]); //地址:高8位
SPI1_WriteByte(addr[1]); //地址:中8位
SPI1_WriteByte(addr[2]); //地址:低8位
uint16_t i;
for(i=0;i<num;i++)
{
SPI1_WriteByte(data[i]); //写数据
}
CS_high;
WP_low;
}
//******************************************************************************
// void Flash_Erase //擦除
//
// Input: uint32_t address 存储器地址
//
//******************************************************************************
void Flash_Erase(uint32_t address)
{
uint8_t addr[3]; //存储器地址
addr[0] = (uint8_t)(address>>16);
addr[1] = (uint8_t)(address>>8);
addr[2] = (uint8_t)address;
WP_high;
CS_low;
SPI1_WriteByte(WREN); //写使能
CS_high;
CS_low;
SPI1_WriteByte(ERASE); //擦除
SPI1_WriteByte(addr[0]); //地址:高8位
SPI1_WriteByte(addr[1]); //地址:中8位
SPI1_WriteByte(addr[2]); //地址:低8位
CS_high;
WP_low;
}
//******************************************************************************
// uint8_t Flash_Flag //Flash状态
//******************************************************************************
uint8_t Flash_Status()
{
uint8_t status;
CS_low;
SPI1_WriteByte(RDSR); //读状态寄存器
status = SPI1_ReadByte();
status = SPI1_ReadByte();
CS_high;
return status;
}