Anda di halaman 1dari 6

//Include this header file in any .

c files within your project from which you wi


sh to use it's functions.
//--------------------------------------//----- SPI PORT SETUP REQUIREMENTS ----//--------------------------------------//Up to 66MHz SPI port (50MHz for 2.5V operation)
//Data latched on rising edge of SCK, data is output on the falling edge of SCK.
/*
Example setup for PIC18 family://----- SETUP SSP1 ----//Used for: Flash memory IC
//CLK idle in low state
//AT45DB081 clocks in data on the rising edge, outputs data on the falli
ng edge
SSPSTAT = 0b01000000;
SSPCON1 = 0b00100000;
//Fosc / 4 = 10MHz
*/
/*
Example setup for DSPIC33FJ family://----- SETUP SPI 1 ----//Used for: Flash memory IC
w_temp = SPI1BUF;
SPI1STAT = 0;
SPI1CON1 = 0x0320;

//SPI in master mode


//Data i
s valid on the rising edge of the clock (Transmit occurs on transition from acti
ve to idle clock state)
//Clock
low in idle bus state
SPI1CON1bits.PPRE1 = 1;
//Prescallers 4:1 1:1 = 10MHz (m
ax possible from this device)
SPI1CON1bits.PPRE0 = 0;
SPI1CON1bits.SPRE2 = 1;
SPI1CON1bits.SPRE1 = 1;
SPI1CON1bits.SPRE0 = 1;
SPI1CON2 = 0;
SPI1STATbits.SPIEN = 1;
//Enable the port
*/
/*
Example setup for PIC24HJ family://----- SETUP SPI 1 ----//Used for: Flash memory IC
w_temp = SPI1BUF;
SPI1STAT = 0;
SPI1CON1 = 0b0000001100100001; //SPI in master mode (SPI1STATbits.SPIEN
must be 0 to write to this register)
//Data i
s valid on the rising edge of the clock (Transmit occurs on transition from acti
ve to idle clock state)
//Clock
low in idle bus state
SPI1CON1bits.PPRE1 = 1;
//Prescallers 4:1 1:1 = 10MHz (m
ax possible from this device)
SPI1CON1bits.PPRE0 = 0;
SPI1CON1bits.SPRE2 = 1;

SPI1CON1bits.SPRE1 = 1;
SPI1CON1bits.SPRE0 = 1;
SPI1CON2 = 0;
SPI1STATbits.SPIEN = 1;

//Enable the port

*/
/*
Example setup for PIC32 family://----- SETUP SPI 1 ----//Used for: Flash Memory
//Idle low, active high
//Transmit on active to idle (falling edge)
SpiChnOpen(SPI_CHANNEL1, (SPI_CON_MSTEN | SPI_OPEN_CKE_REV | SPI_CON_MOD
E8), 2);
//40MHz fpb / 2 = 20MHz
*/

//##### ADDING A FUNCTION TO READ INDVIDIUAL BYTES USING A LOCAL BUFFER FOR A PA
GE OF MEMORY#####
//Add to INTERNAL ONLY MEMORY DEFINITIONS:/*
DWORD flash_buffer_address = 0xffffffff;
BYTE flash_buffer[256];
*/
//Add to fucntion definitions
/*
BYTE flash_read_byte (DWORD address);
extern BYTE flash_read_byte (DWORD address);
*/
//Add this function://*******************************
//*******************************
//********** READ BYTE **********
//*******************************
//*******************************
/*
BYTE flash_read_byte (DWORD address)
{
//LOAD PAGE IF NECESSARY
if ((flash_buffer_address & 0xffffff00) != (address & 0xffffff00))
{
flash_buffer_address = (address & 0xffffff00);
flash_read_page(flash_buffer_address, &flash_buffer[0]);
}
return(flash_buffer[(address & 0x000000ff)]);
}
*/

//*****************************
//*****************************
//********** DEFINES **********
//*****************************
//*****************************
#ifndef FLASH_C_INIT
//Do only once the first time this file is used
#define FLASH_C_INIT
//---------------------------//----- FLASH MEMORY MAP ----//---------------------------//AT45DB081, FLASH MEMORY
//1048576 bytes x 8 (1024KB / 1MB)
//4096 pages of 256 bytes (To avoid non binary complexities this driver only use
s 256 bytes of each page instead of the 264 which are actually available)
//0x000000 - 0x0fffff
//
//4096 pages of 256 bytes
//512 blocks of 2 Kbytes
//16 sectors of 64 Kbytes
//
//The AT45DB081 is shipped with the page size set to 264 bytes. This driver lea
ves it set to 264 bytes and adjusts it's addressing accordingly. When calling i
t's
//functions you use normal binary addressing and can ignore the 264 byte page si
ze issue.

#define FLASH_END_ADDRESS
#define FLASH_BLOCK_SIZE
k is 8 pages
//##################
//##### PIC 18 #####
//##################
/*
#define FLASH_CLR_WDT
watchdog timer
//----- PINS ----#define FLASH_CS(state)

0x0fffff
0x800

ClrWdt()

//A bloc

//Clear

LATCbits.LATC0 = state

//----- SPI BUS READ WRITE DEFINES ----#define FLASH_SPI_BUF_FULL


PIR1bits.SSPIF
//Flag that the SPI receive buffer contains a received byte, also signifying tha
t transmit is complete
#define FLASH_SPI_READ_BUFFER
SSPBUF
//Read byte from SPI bus
#define FLASH_SPI_WRITE_BUFFER(data)
SSPBUF = data
//Write byte to SPI bus
#define DO_CS_HIGH_DELAY
//Delay of at least 50nS
*/

Nop(); Nop()

//#####################
//##### DSPIC33FJ #####
//#####################
/*
#define FLASH_CLR_WDT
watchdog timer
//----- PINS ----#define FLASH_CS(state)

ClrWdt()

//Clear

_LATF5 = state

//----- SPI BUS READ WRITE DEFINES ----#define FLASH_SPI_BUF_FULL


IFS0bits.SPI1IF
//Flag that the SPI receive buffer contains a received byte, also signifying tha
t transmit is complete
#define FLASH_SPI_READ_BUFFER
SPI1BUF
//Read byte from SPI bus
#define FLASH_SPI_WRITE_BUFFER(data)
IFS0bits.SPI1IF = 0; SPI1BUF = data
//Write byte to SPI bus
#define DO_CS_HIGH_DELAY
//Delay of at least 50nS
*/
//###################
//##### PIC24HJ #####
//###################
/*
#define FLASH_CLR_WDT
watchdog timer
//----- PINS ----#define FLASH_CS(state)

Nop(); Nop()

ClrWdt()

//Clear

_LATG15 = state

//----- SPI BUS READ WRITE DEFINES ----#define FLASH_SPI_BUF_FULL


IFS0bits.SPI1IF
//Flag that the SPI receive buffer contains a received byte, also signifying tha
t transmit is complete
#define FLASH_SPI_READ_BUFFER
SPI1BUF
//Read byte from SPI bus
#define FLASH_SPI_WRITE_BUFFER(data)
IFS0bits.SPI1IF = 0; SPI1BUF = data
//Write byte to SPI bus
#define DO_CS_HIGH_DELAY
//Delay of at least 50nS
*/
//#################
//##### PIC32 #####
//#################
#define FLASH_CLR_WDT
watchdog timer
//----- PINS ----#define FLASH_CS(state)
(BIT_14))

Nop(); Nop()

ClearWDT()

//Clear

(state ? mPORTCSetBits(BIT_14) : mPORTCClearBits

//----- SPI BUS READ WRITE DEFINES ----#define FLASH_SPI_BUF_FULL

SpiChnDataRdy(SPI_CHANNE

L1)
//>0 when the SPI receive buffer
contains a received byte, also signifying that transmit is complete
#define FLASH_SPI_READ_BUFFER
(BYTE)SpiChnReadC(SPI_CHANNEL1)
//Register to read last received byte from
#define FLASH_SPI_WRITE_BUFFER(data)
SpiChnWriteC(SPI_CHANNEL1, (unsigned int
)data) //Macro to write a byte and start transmission over the SPI bus
#define DO_CS_HIGH_DELAY
//Delay of at least 50nS
#endif

Nop(); Nop(); Nop(); Nop(); Nop()

//#ifndef FLASH_C_INIT

//*******************************
//*******************************
//********** FUNCTIONS **********
//*******************************
//*******************************
#ifdef FLASH_C
//----------------------------------//----- INTERNAL ONLY FUNCTIONS ----//----------------------------------BYTE flash_read_status (void);
//----------------------------------------//----- INTERNAL & EXTERNAL FUNCTIONS ----//----------------------------------------//(Also defined below as extern)
BYTE flash_check_present (void);
void flash_erase_page (DWORD address);
void flash_erase_block (DWORD address);
void flash_erase_all (void);
void flash_write_page (DWORD address, BYTE *buffer, BYTE erase_first);
void flash_read_page (DWORD address, BYTE *buffer);
BYTE flash_test_memory (void);
#else
//-----------------------------//----- EXTERNAL FUNCTIONS ----//-----------------------------extern BYTE flash_check_present (void);
extern void flash_erase_page (DWORD address);
extern void flash_erase_block (DWORD address);
extern void flash_erase_all (void);
extern void flash_write_page (DWORD address, BYTE *buffer, BYTE erase_first);
extern void flash_read_page (DWORD address, BYTE *buffer);
extern BYTE flash_test_memory (void);
#endif

//****************************
//****************************

//********** MEMORY **********


//****************************
//****************************
#ifdef FLASH_C
//-------------------------------------------//----- INTERNAL ONLY MEMORY DEFINITIONS ----//--------------------------------------------

//-------------------------------------------------//----- INTERNAL & EXTERNAL MEMORY DEFINITIONS ----//-------------------------------------------------//(Also defined below as extern)

#else
//--------------------------------------//----- EXTERNAL MEMORY DEFINITIONS ----//---------------------------------------

#endif

Anda mungkin juga menyukai