#include "config.h"
#include "sst25vfxx.h"
/* ========================================================================= */
/* Project Name : SPI读写SST25VF040 flash */
/* File Name : SST25VF040.c */
/* Description : SST25VF040操作函数 */
/* Processor : STC12LE5A16S2 */
/* Tools : KEIL C51 */
/* ========================================================================= */
//=============================================================================
// ----Function: void SST25VFxx_SPIinit(void);
// -Description: Initialization SPMC75F2413A SPI.
// --Parameters: none.
// -----Returns: none.
// -------Notes: Bit11:SCK, Bit12:SDI, Bit13:SDO, Bit14:CS.
//=============================================================================
sbit SPI_CLK = P1^7;
sbit SPI_MOSI = P1^5;
sbit SPI_MISO = P1^6;
sbit SPI_SS = P1^4;
//sbit SPI_WP = P3^4;
#define SPIF 6 //注意该处值
#define PIC_FDT_FILESTARTADDR 0X000000 //图像文件名称属性起始地址
#define PIC_DATA_FILESTARTADDR 0X001000 //图像文件数据的起始地址
//#define WCOL 6
void SST25VFxx_SPIinit(void)
{
//P1M1 |= 0x0;
//P1M0 |= 1;
SPI_CLK = 0;
SPI_MOSI = 1;
SPI_MISO = 1;
SPI_SS = 1;
//SPI_WP = 1;
//SPCTL = 0xd3; // 注意在这里CPHA不能为1,只能为0;SSIG只能为1,不能为0
//SPSTAT = 0xc0;
//=============================================================================
// SPCTL寄存器
// |-------|-------|--------|--------|---------|---------|---------|--------|
// | bit7 | bit6 | bit5 | bit4 | bit3 | bit2 | bit1 | bit0 |
// | SSIG | SPEN | DORD | MSTR | CPOL | CPHA | SPR1 | SPR0 |
// |-------|-------|--------|--------|---------|---------|---------|--------|
/* 1 1 0 1 0 0 1 1
SPIG:使能SPI中断
SPEN:使能SPI.SPE置位将使能SPI,进行任何SPI操作之前必须置位SPE.
DORD:置位时,数据的LSB首先发送;否则数据的MSB首先发送
MSTR:主从选择。置位选择主机模式,否则选择从机模式。
CPOL:时钟极限。置位表示空闲时SCK为高电平;否则空闲时SCK为低电平
CPHA:时钟相位。CPHA决定数据是在SCK的起始沿采样还是在SCK的结束沿采样;
置位时表示在结束沿采样,否则在起始沿采样
SPR1,0:确定主机的SCK速率。采样列表如下:
SPI2X SPR1 SPR0 SCK频率
0 0 0 Fosc/4
0 0 1 Fosc/16
0 1 0 Fosc/64
0 1 1 Fosc/128
1 0 0 Fosc/2
1 0 1 Fosc/8
1 1 0 Fosc/32
1 1 1 Fosc/64
SPI2X:为时钟倍速设置。置位后SCK频率将调高一倍
//=============================================================================
// SPSTAT寄存器设置
// |-------|-------|--------|--------|---------|---------|---------|--------|
// | bit7 | bit6 | bit5 | bit4 | bit3 | bit2 | bit1 | bit0 |
// | SPIF | WCOL | -- | -- | -- | -- | -- | SPI2X |
// |-------|-------|--------|--------|---------|---------|---------|--------|
SPIF:SPI中断标志
SPI2X:倍速。若为主机频率可达到CPU的一半;但是若为从机,最高只能保证Fosc/4
*/
}
//=============================================================================
// ----Function: uint16 SST25VFxx_RDID(void);
// -Description: Read Manufacturer and Product ID
// --Parameters: none
// -----Returns: Manufacturer and Product ID
// -------Notes: Return value for example
// |---------------+---------------|
// | High Byte | Low Byte |
// +---------------+---------------+
// | Manufacturer | Product ID |
// |---------------+---------------|
// High Byte: Manufacturer
// 1FH = ATMEL .....
// Low Byte: Product ID
// 65H = 25F512
// 60H = 25F1024
// 63H = 25F2048
// 64H = 25F4096 .....
//=============================================================================
/*
unsigned int SST25VFxx_RDID(void) //Read Manufacturer and Product ID
{
TYPEWORDDEF HIREBUFFER;
unsigned char buffer[4];
unsigned char i;
SPI_SS = 0; //CS为低,有效
buffer[0] = RDID1;
buffer[1] = 0x00; //高地址
buffer[2] = 0x00; //中间八位地址
buffer[3] = 0x00; //低地址
for(i=0;i<4;i++)
{
//SPDAT = buffer[i]; //SPI发送RDID指令和地址
//while(!(SPSTAT & (3<<SPIF))); //等待SPI发送数据完成
//SPSTAT = 0xC0;
SendData(buffer[i]);
}
// SPDAT = 0; //写任何数据,主机产生时钟接收数据
// while(!(SPSTAT & (3<<SPIF))); //等待SPI数据接收完成
// SPSTAT = 0xc0; //清除标志
// HIREBUFFER.B._byte1 = SPDAT; //读取SST25VFxx送回的厂商信息数据BF
HIREBUFFER.B._byte1 = GetData();
// SPDAT = 0; //写任何数据,主机产生时钟接收数据
// while(!(SPSTAT & (3<<SPIF))); //等待SPI数据接收完成
// SPSTAT = 0xc0; //清除标志
// HIREBUFFER.B._byte0 = SPDAT; //读取SST25VFxx送回的芯片ID数据41
HIREBUFFER.B._byte0 = GetData();
SPI_SS = 1; //CS为高
return(HIREBUFFER.W); //返回厂商和ID数据
}*/
//=============================================================================
// ----Function: uint16 SST25VFxx_RDSR(void);
// -Description: Read status register
// --Parameters: none
// -----Returns: SST25VFxx status
// -------Notes: Return value format
// |------+------+------+------+------+------+------+------|
// | Bit7 | Bit6 | Bit5 | Bit4 | Bit3 | Bit2 | Bit1 | Bit0 |
// +------+------+------+------+------+------+------+------+
// | WPEN | ** | ** | ** | BP1 | BP0 | WEN | /RDY |
// |------+------+------+------+------+------+------+------|
// Bit0=0 :Indicates the device is Ready.
// Bit0=1 :Indicates the write cycle is in progress.
//
// Bit1=0 :Indicates the device is not WRITE ENABLED.
// Bit1=1 :Indicates the device is WRITE ENABLED.
//
// Bit2 and Bit3 : Please see SST25VFxx Data Sheet Table.
//
// Bit7 : Please see SST25VFxx Data Sheet Table. .....
//=============================================================================
unsigned char SST25VFxx_RDSR(void) //Read status register
{
unsigned char status = 0;
SPI_SS = 0;