白桦林 发表于 2006-6-12 13:12:00
/**********************************************************************************************************
* Copyright : Copyright(C) 1997-2006 ICC 之 北方老松
All rights reserved.
*
* EMail :
zrfzd@yahoo.com.cn * ----------< Document explain >---------------------------------------------------------
* Project Name :
* Compiler : ICC-AVR6.3
* Edit Tools : UltraEdit-32 VER11.1
* Target MCU : ATMaga128
* Crystal : 16MHZ
* PCB P/N :
* File name : SPIFlash.C
* Editor Tabs : 4
* Comments :
-------------------------------------------------------------------
< History >
* Version : 1.0
* Author : zrf
* E-Mail :
* Date : 2006-05-17
**********************************************************************************************************/
#i nclude "..\include\DEFINE.H"
#i nclude "..\include\GloVar.H"
/*******************************************************************************/
/* PROCEDURE: Read_Status_Register */
/* */
/* This procedure reads from Read_Status_Register. */
/* */
/* Input: None */
/* */
/* Returns: status byte */
/* */
/*******************************************************************************/
unsigned char Read_Status_Register(void)
{
unsigned char byte = 0;
Select_Serial_Memory;
SST_MasterIO(0x05); /* send RDSR command */
byte = SST_MasterIO(0x00); /* receive byte */
Deselect_Serial_Memory; /* disable device */
return byte;
}
/*******************************************************************************/
/* PROCEDURE: EWSR */
/* */
/* This procedure enables the Write Status Register. */
/* */
/* Input: None */
/* */
/* Returns: Nothing */
/* */
/*******************************************************************************/
void EWSR(void )
{
Select_Serial_Memory;
SST_MasterIO(0x50); /* enuable writing to the stats register */
Deselect_Serial_Memory; /* disable device */
}
/*******************************************************************************/
/* PROCEDURE: WRSR */
/* */
/* This procedure writes a byte to the Status Register. */
/* */
/* Input: data byte */
/* */
/* Returns: Nothing */
/* */
/*******************************************************************************/
void WRSR(unsigned char byte)
{
Select_Serial_Memory;
SST_MasterIO(0x01); /* select write to status register */
SST_MasterIO(byte); /* data that will change the status of BPx or BPL
(only bits 2,3,7 can be written) */
Deselect_Serial_Memory; /* disable device */
Wait_Busy();
}
/*******************************************************************************/
/* PROCEDURE: WREN */
/* */
/* This procedure enables the Write Enable Latch. */
/* */
/* Input: None */
/* */
/* Returns: Nothing */
/* */
/*******************************************************************************/
void WREN(void)
{
Select_Serial_Memory;
SST_MasterIO(0x06); /* send WREN command */
Deselect_Serial_Memory; /* disable device */
}
/*******************************************************************************/
/* PROCEDURE: WRDI */
/* */
/* This procedure disables the Write Enable Latch. */
/* */
/* Input: None */
/* */
/* Returns: Nothing */
/* */
/*******************************************************************************/
void WRDI(void)
{
Select_Serial_Memory;
SST_MasterIO(0x04); /* send WRDI command */
Deselect_Serial_Memory; /* disable device */
}
/*******************************************************************************/
/* PROCEDURE: Read_ID */
/* */
/* This procedure reads the manufacturer’s ID and device ID. */
/* It will use 90h as the command to read the ID. It is up to */
/* the user to give the last byte ID_addr to determine whether */
/* the device outputs manufacturer’s ID first, or device ID first. */
/* Review the data sheets for details. */
/* Returns ID in variable byte. */
/* */
/* Input: ID_addr */
/* */
/* Returns: byte: ID1 */
/* */
/*******************************************************************************/
unsigned char Read_ID(unsigned char ID_addr)
{
unsigned char byte;
Select_Serial_Memory;
SST_MasterIO(0x90); /* send read ID command */
SST_MasterIO(0x00); /* send address */
SST_MasterIO(0x00); /* send address */
SST_MasterIO(ID_addr); /* send address - either 00H or 01H */
byte = SST_MasterIO(0x00); /* receive byte */
Deselect_Serial_Memory; /* disable device */
return byte;
}
/*******************************************************************************/
/* PROCEDURE: Read */
/* */
/* This procedure reads one address of the device. */
/* It will return the byte read in variable byte. */
/* */
/* Input: Dst: Destination Address 000000H - 07FFFFH */
/* */
/* Returns: byte */
/* */
/*******************************************************************************/
unsigned char Read(unsigned long Dst)
{
unsigned char byte = 0;
Select_Serial_Memory;
SST_MasterIO(0x03); /* read command */
SST_MasterIO(((Dst & 0xFFFFFF) >> 16)); /* send 3 address bytes */
SST_MasterIO(((Dst & 0xFFFF) >> 8));
SST_MasterIO(Dst & 0xFF);
byte = SST_MasterIO(0x00);
Deselect_Serial_Memory; /* disable device */
return byte; /* return one byte read */
}
/*******************************************************************************/
/* PROCEDURE: Read_Cont */
/* */
/* This procedure reads multiple consecutive addresses of */
/* the device and stores the data into DataArray. */
/* */
/* Input: Dst: Destination Address 000000H - 07FFFFH */
/* no_bytes Number of bytes to read */
/* DataArray Array storing read data */
/* */
/* Returns: Nothing */
/* */
/*******************************************************************************/
void Read_Cont(unsigned long Dst, unsigned int no_bytes, unsigned char *DataArray)
{
unsigned int i = 0;
Select_Serial_Memory; /* enable device */
SST_MasterIO(0x03); /* read command */
SST_MasterIO(((Dst) >> 16)); /* send 3 address bytes */
SST_MasterIO(((Dst) >> 8));
SST_MasterIO(Dst&0xFF);
for (i = 0; i < no_bytes; i++) /* read until no_bytes is reached */
{
*(DataArray+i) = SST_MasterIO(0x00); /* receive byte and store in DataArray */
}
Deselect_Serial_Memory; /* disable device */
}
/*******************************************************************************/
/* PROCEDURE: Byte_Program */
/* */
/* This procedure programs one address of the device. */
/* Assumption: Address being programmed is already */
/* erased and is NOT block protected. */
/* */
/* Input: Dst: Destination Address 000000H - 07FFFFH */
/* byte: byte to be programmed */
/* */
/* Returns: Nothing */
/* */
/*******************************************************************************/
void Byte_Program(unsigned long Dst, unsigned char byte)
{
WREN();
Select_Serial_Memory; /* enable device */
SST_MasterIO(0x02); /* send Byte Program command */
SST_MasterIO(((Dst & 0xFFFFFF) >> 16)); /* send 3 address bytes */
SST_MasterIO(((Dst & 0xFFFF) >> 8));
SST_MasterIO(Dst & 0xFF);
SST_MasterIO(byte); /* send byte to be programmed */
Deselect_Serial_Memory; /* disable device */
Wait_Busy();
}
/*******************************************************************************/
/* PROCEDURE: Auto_Add_IncA */
/* */
/* This procedure programs consecutive addresses of */
/* the device. This is used to start the AAI process. */
/* It should be followed by Auto_Add_IncB. */
/* Assumption: Address being programmed is already */
/* erased and is NOT block protected. */
/* */
/* Input: Dst: Destination Address 000000H - 07FFFFH */
/* byte: byte to be programmed */
/* */
/* Returns: Nothing */
/* */
/*******************************************************************************/
void Auto_Add_IncA(unsigned long Dst, unsigned char byte)
{
WREN();
Select_Serial_Memory; /* enable device */
SST_MasterIO(0xAF); /* send AAI command */
SST_MasterIO(((Dst & 0xFFFFFF) >> 16)); /* send 3 address bytes */
SST_MasterIO(((Dst & 0xFFFF) >> 8));
SST_MasterIO(Dst & 0xFF);
SST_MasterIO(byte); /* send byte to be programmed */
Deselect_Serial_Memory; /* disable device */
Wait_Busy();
}
/*******************************************************************************/
/* PROCEDURE: Auto_Add_IncB */
/* */
/* This procedure programs consecutive addresses of */
/* the device. This is used after Auto_Address_IncA. */
/* Assumption: Address being programmed is already */
/* erased and is NOT block protected. */
/* */
/* Input: byte: byte to be programmed */
/* */
/* Returns: Nothing */
/* */
/*******************************************************************************/
void Auto_Add_IncB(unsigned char byte)
{
Select_Serial_Memory; /* enable device */
SST_MasterIO(0xAF); /* send AAI command */
SST_MasterIO(byte); /* send byte to be programmed */
Deselect_Serial_Memory; /* disable device */
Wait_Busy();
}
/*******************************************************************************/
/* PROCEDURE: Chip_Erase */
/* */
/* This procedure erases the entire Chip. */
/* */
/* Input: None */
/* */
/* Returns: Nothing */
/* */
/*******************************************************************************/
void Chip_Erase(void)
{
WREN();
Select_Serial_Memory; /* enable device */
SST_MasterIO(0x60); /* send Chip Erase command */
Deselect_Serial_Memory; /* disable device */
Wait_Busy();
}
/*******************************************************************************/
/* PROCEDURE: Sector_Erase */
/* */
/* This procedure Sector Erases the Chip. */
/* */
/* Input: Dst: Destination Address 000000H - 07FFFFH */
/* */
/* Returns: Nothing */
/* */
/*******************************************************************************/
void Sector_Erase(unsigned long Dst)
{
WREN();
Select_Serial_Memory; /* enable device */
SST_MasterIO(0x20); /* send Sector Erase command */
SST_MasterIO(((Dst & 0xFFFFFF) >> 16)); /* send 3 address bytes */
SST_MasterIO(((Dst & 0xFFFF) >> 8));
SST_MasterIO(Dst & 0xFF);
Deselect_Serial_Memory; /* disable device */
Wait_Busy();
}
/*******************************************************************************/
/* PROCEDURE: Block_Erase */
/* */
/* This procedure Block Erases the Chip. */
/* */
/* Input: Dst: Destination Address 000000H - 07FFFFH */
/* */
/* Returns: Nothing */
/* */
/*******************************************************************************/
void Block_Erase(unsigned long Dst)
{
WREN();
Select_Serial_Memory; /* enable device */
SST_MasterIO(0x52); /* send Block Erase command */
SST_MasterIO(((Dst & 0xFFFFFF) >> 16)); /* send 3 address bytes */
SST_MasterIO(((Dst & 0xFFFF) >> 8));
SST_MasterIO(Dst & 0xFF);
Deselect_Serial_Memory; /* disable device */
Wait_Busy();
}
/*******************************************************************************/
/* PROCEDURE: Wait_Busy */
/* */
/* This procedure waits until device is no longer busy. */
/* */
/* Input: None */
/* */
/* Returns: Nothing */
/* */
/*******************************************************************************/
void Wait_Busy()
{
while ((Read_Status_Register() & 0x03) == 0x03)
Read_Status_Register(); /* waste time until not busy */
}
void SPI_delay(unsigned char n)
{
unsigned char i;
for (i=0;i<n;i++)
;
}
/*******************************************************************************/
/* PROCEDURE: HW_SPI_Init */
/* */
/* This procedure initializes the hardware SPI on the MCU. */
/* */
/* Input: None */
/* */
/* Returns: Nothing */
/* */
/*******************************************************************************/
void SST_SPI_Init(void )
{
cbi(PORTB,PB1); // set SCK hi
sbi(DDRB, PB1); // set SCK as output
cbi(DDRB, PB3); // set MISO as input
sbi(DDRB, PB2); // set MOSI as output
sbi(DDRB, PB0); // SS must be output for Master mode to work
Deselect_Serial_Memory; /* disable device */
SPCR =0;
//SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0); // 1/16
SPCR = (1<<SPE)|(1<<MSTR);
WREN();
EWSR();
WRSR(0x00);
}
/*******************************************************************************/
/* PROCEDURE: SST_MasterIO */
/* */
/* This procedure handles byte transfer to and from */
/* the slave device. */
/* */
/* Input: None */
/* */
/* Returns: Nothing */
/* */
/*******************************************************************************/
/*
unsigned char SST_MasterIO(unsigned char SPI_out)
{
unsigned char i,temp,D_back=0;
temp = SPI_out;
for (i=0;i<8;i++)
{
if (temp & 0x80)
SPI_MO_HIGH ;
else
SPI_MO_LOW ;
D_back = D_back<<1;
D_back = SPI_SO?(D_back | 0x01):(D_back & 0xfe);
SPI_SCK_HIGH ;
SPI_SCK_LOW ;
temp = temp<<1;
}
return D_back;
}
*/
unsigned char SST_MasterIO(unsigned char SPI_out)
{
SPDR = SPI_out ;
while(!(SPSR&(1<<SPIF))){};
SOME_NOP ;
return( SPDR) ;
}
/*********************************************************************************************
PUT CHAR ARRAY
*********************************************************************************************/
/*! \brief Write one or multiple bytes to the Serial SPI memory
*
* This function check the validity of the first byte address location (out of range or write protected area).
* A read access to the serial SPI memory is performed to get the status register value. During this access the interrupt is disabled.
* As from the first SPI write access is started, the function returns.
* The management of the next SPI accesses and the write access completion are performed by the SPI interrupt handler.
*
* \note No page roll-over control is performed.
*
* \param start_add : destination address of the first byte
* \param nb_of_byte : number of bytes to be written decremented by one (this is for compatibility reason for devices with 256 bytes page size)
* \param source : pointer to the write buffer location
* \return AccessStatus.
* \retval TRANSFER_STARTED : the write sequence is started without error.
* \retval OUT_OF_RANGE : the address is out of range of available memory.
* \retval BUSY : the SPI memory or the SPI interface is busy.
* \retval DATA_WR_PROTECTED : the address of the last byte to be written matches a write protected location.
*********************************************************************************************/
void PutCharArray(unsigned long StartAddr, unsigned int SendBytes, unsigned char* SourceAddr)
{
U16 byteread_cnt;
U8 * TempPtr ;
TempPtr= SourceAddr ;
WREN();
Select_Serial_Memory; /* enable device */
SST_MasterIO(SPIFLASH_CMD_AAIP);
SST_MasterIO(((StartAddr ) >> 16)); /* send 3 address bytes */
SST_MasterIO(((StartAddr ) >> 8));
SST_MasterIO(StartAddr & 0xFF);
SST_MasterIO((char)(*(TempPtr))); //write a byte
Deselect_Serial_Memory; // Pull high the chip select line of the SPI serial memory
for(byteread_cnt=1; byteread_cnt < SendBytes; byteread_cnt++)
{
Select_Serial_Memory; // Pull down the chip select line of the SPI serial memory
SST_MasterIO(SPIFLASH_CMD_AAIP);
SST_MasterIO(*(TempPtr+byteread_cnt));
Deselect_Serial_Memory; // Pull high the chip select line of the SPI serial memory
}
WRDI();
}