📄 at45db642d.c
字号:
////////////////////////////////////////////////////////////////////////////
//// Library for an ATMEL AT45DB021 DataFlash ////
//// ////
//// init_ext_flash() ////
//// Initializes the pins that control the flash device. This must ////
//// be called before any other flash function is used. ////
//// ////
//// ****************************************************************** ////
//// void ext_flash_startContinuousRead(p, i) ////
//// Initiate a continuous read starting with page p at index i ////
//// ////
//// BYTE ext_flash_getByte() ////
//// Gets a byte of data from the flash device ////
//// Use after calling ext_flash_startContinuousRead() ////
//// ////
//// void ext_flash_getBytes(a, n) ////
//// Read n bytes and store in array a ////
//// Use after calling ext_flash_startContinuousRead() ////
//// ////
//// void ext_flash_stopContinuousRead() ////
//// Use to stop continuously reading data from the flash device ////
//// ****************************************************************** ////
//// ////
//// void ext_flash_readPage(p, i, a, n) ////
//// Read n bytes from page p at index i and store in array a ////
//// ////
//// void ext_flash_readBuffer(b, i, a, n) ////
//// Read n bytes from buffer b at index i and store in array a ////
//// ////
//// BYTE ext_flash_readStatus() ____ ////
//// Return the status of the flash device: Rdy/Busy Comp 0101XX ////
//// ////
//// void ext_flash_writeToBuffer(b, i, a, n) ////
//// Write n bytes from array a to buffer b at index i ////
//// ////
//// void ext_flash_BufferToPage(b, p, mode) ////
//// Copy buffer b to page p ////
//// The 2 modes: ////
//// - Use ERASE to use built in erase first functionality ////
//// - Use NO_ERASE to write to a previously erased page ////
//// ////
//// void ext_flash_erasePage(p) ////
//// Erase all bytes in page p to 0xFF ////
//// ////
//// void ext_flash_eraseBlock(b) ////
//// Erase all bytes in block b to 0xFF. A block is 8 pages. ////
//// ////
//// void ext_flash_writePageThroughBuffer(p, b, i, a, n) ////
//// Write n bytes from array a to page p at index i through ////
//// buffer b ////
//// ////
//// void ext_flash_PageToBuffer(p, b) ////
//// Copy the data from page p to buffer b ////
//// ////
//// int1 ext_flash_comparePageToBuffer(p, b) ////
//// Compare the data in page p to buffer b ////
//// Returns 1 if equivalent or 0 if not equivalent ////
//// ////
//// void ext_flash_rewritePage(p, b) ////
//// Rewrite the data in page p using buffer b ////
//// ////
//// void ext_flash_waitUntilReady() ////
//// Waits until the flash device is ready to accept commands ////
//// ////
//// The main program may define FLASH_SELECT, FLASH_CLOCK, ////
//// FLASH_DI, and FLASH_DO to override the defaults below. ////
//// ////
////////////////////////////////////////////////////////////////////////////
//// ////
//// Pin Layout ////
//// --------------------------------------------------- ////
//// | | ////
//// | 1: SI FLASH_DI | 8: SO FLASH_DO | ////
//// | | | ////
//// | 2: SCK FLASH_CLOCK | 7: GND GND | ////
//// | _____ | | ////
//// | 3: RESET +2.7V - +3.6V | 6: VCC +2.7V - +3.6V | ////
//// | __ | __ | ////
//// | 4: CS FLASH_SELECT | 5: WP +2.7V - +3.6V | ////
//// --------------------------------------------------- ////
//// ////
////////////////////////////////////////////////////////////////////////////
//// (C) Copyright 1996, 2003 Custom Computer Services ////
//// This source code may only be used by licensed users of the CCS C ////
//// compiler. This source code may only be distributed to other ////
//// licensed users of the CCS C compiler. No other use, reproduction ////
//// or distribution is permitted without written permission. ////
//// Derivative programs created using this software in object code ////
//// form are not restricted in any way. ////
////////////////////////////////////////////////////////////////////////////
#include<p24hj256GP210.h>
#include"Global.h"
//#ifndef FLASH_SELECT
#define FLASH_SELECT PORTFbits.RF12
#define FLASH_CLOCK PORTFbits.RF6
#define FLASH_DO PORTFbits.RF7
#define FLASH_DI PORTFbits.RF8
//#endif
#define FLASH_SIZE 8650752 // The size of the flash device in bytes
// Used in ext_flash_BufferToPage()
#define ERASE 1 // The flash device will initiate an erase before writing
#define NO_ERASE 0 // The flash device will not initiate an erase before writing
unsigned char bufferNumber;
void ext_flash_sendData(unsigned int data, unsigned char size);
void ext_flash_sendBytes(unsigned char* data, unsigned int size);
void ext_flash_getBytes(unsigned char * data, unsigned int size);
void ext_flash_waitUntilReady();
extern unsigned char BitTest(unsigned char Data,unsigned int bitpos);
extern void BitClear(unsigned char *Data,unsigned int bitpos);
extern void BitSet(unsigned char *Data,unsigned char bitpos);
// Purpose: Initialize the pins that control the flash device.
// This must be called before any other flash function is used.
// Inputs: None
// Outputs: None
// Dependencies: None
void init_ext_flash()
{
FLASH_CLOCK=0;
FLASH_SELECT=1;
}
// Purpose: This function will start reading a continuous stream of
// data from the entire flash device.
// Inputs: 1) A page address
// 2) An index into the page
// Outputs: None
// Dependencies: ext_flash_sendData(), ext_flash_waitUntilReady()
void ext_flash_startContinuousRead(int16 pageAddress, int16 pageIndex)
{
ext_flash_waitUntilReady();
FLASH_SELECT=0; // Enable select line
ext_flash_sendData(0xE8, 8); // Send opcode
ext_flash_sendData(pageAddress, 13); // Send page address
ext_flash_sendData(pageIndex, 11); // Starting Byte with in the page(index)
ext_flash_sendData(0, 16); // Send 16 don't care bits
ext_flash_sendData(0, 16); // Send 16 don't care bits
}
// Purpose: Get a byte of data from the flash device. This function is
// meant to be used after ext_flash_startContinuousRead() has
// been called to initiate a continuous read.
// Inputs: None
// Outputs: 1) A byte of data
// Dependencies: None
BYTE ext_flash_getByte()
{
BYTE flashData;
int i;
for(i=0; i<8; ++i) // Get 8 bits of data
{
FLASH_CLOCK=1;
if(FLASH_DO)
{
flashData<<=1;
BitSet(&flashData,0);
}
else
{
flashData<<=1;
BitClear(&flashData,0);
}
// shift_left(&flashData, 1, input(FLASH_DO));
FLASH_CLOCK=0;
}
return flashData;
}
// Purpose: Get a byte of data from the flash device. This function is
// meant to be used after ext_flash_startContinuousRead() has
// been called to initiate a continuous read. This function is
// also used by ext_flash_readPage() and ext_flash_readBuffer().
// Inputs: 1) A pointer to an array to fill
// 2) The number of bytes of data to read
// Outputs: None
// Dependencies: None
void ext_flash_getBytes(BYTE* data, int16 size)
{
int16 i;
signed char j;
for(i=0; i<size; ++i)
{
for(j=0; j<8; ++j)
{
FLASH_CLOCK=1;
if(FLASH_DO)
{
data[i]<<=1;
BitSet(&data[i],0);
}
else
{
data[i]<<=1;
BitClear(&data[i],0);
}
// shift_left(data+i, 1, input(FLASH_DO));
FLASH_CLOCK=0;
}
}
}
// Purpose: Stop continuously reading data from the flash device.
// Inputs: None
// Outputs: None
// Dependencies: None
void ext_flash_stopContinuousRead()
{
FLASH_SELECT=1; // Disable select line
}
// Purpose: Read data from a memory page.
// Inputs: 1) A page address
// 2) An index into the page to start reading at
// 3) A pointer to a data array to fill
// 4) The number of bytes of data to read
// Outputs: None
// Dependencies: ext_flash_sendData(), ext_flash_waitUntilReady(), ext_flash_getBytes()
void ext_flash_readPage(int16 pageAddress, int16 pageIndex, BYTE* data, int16 size)
{
ext_flash_waitUntilReady(); // Wait until ready
FLASH_SELECT=0; // Enable select line
ext_flash_sendData(0xD2, 8); // Send opcode
ext_flash_sendData(pageAddress, 13); // Send page address
ext_flash_sendData(pageIndex, 11); // Send index
ext_flash_sendData(0, 16); // Send 16 don't care bits
ext_flash_sendData(0, 16); // Send 16 don't care bits
ext_flash_getBytes(data, size); // Get data from the flash device
FLASH_SELECT=1; // Disable select line
}
// Purpose: Read data from a buffer
// Inputs: 1) A buffer number (0 or 1)
// 2) An index into the buffer to start reading at
// 3) A pointer to a data array to be filled
// 4) The number of bytes of data to read
// Outputs: None
// Dependencies: ext_flash_sendData(), ext_flash_waitUntilReady(), ext_flash_getBytes()
void ext_flash_readBuffer(unsigned char bufferNumber, int16 bufferAddress, BYTE* data, int16 size)
{
BYTE opcode;
FLASH_SELECT=0; // Enable select line
if(bufferNumber)
opcode = 0xD6; // Opcode for second buffer
else
opcode = 0xD4; // Opcode for first buffer
ext_flash_sendData(opcode, 8); // Send opcode
ext_flash_sendData(0, 13); // Send 15 don't care bits
ext_flash_sendData(bufferAddress, 11); // Send buffer address
ext_flash_sendData(0, 8); // Send 8 don't care bits
ext_flash_getBytes(data, size); // Get data from the flash device
FLASH_SELECT=1; // Disable select line
}
// Purpose: Return the status of the flash device
// Inputs: None ____
// Outputs: The status: Rdy/Busy Comp 0101XX
// Dependencies: ext_flash_sendData(), ext_flash_getByte()
BYTE ext_flash_readStatus()
{
BYTE status;
FLASH_SELECT=0; // Enable select line
ext_flash_sendData(0xD7, 8); // Send status command
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -