📄 df.c
字号:
/*C**************************************************************************
* $RCSfile: df.c,v $
*----------------------------------------------------------------------------
* Copyright (c) 2002 Atmel.
*----------------------------------------------------------------------------
* RELEASE: $Name: DEMO_FAT_1_9_9 $
* REVISION: $Revision: 1.10 $
* FILE_CVSID: $Id: df.c,v 1.10 2002/09/06 13:17:18 njourdan Exp $
*----------------------------------------------------------------------------
* PURPOSE:
* This file contains the low-level dataflash routines
*
* NOTES:
* Global Variables:
* - gl_ptr_mem: long in data space
*****************************************************************************/
/*_____ I N C L U D E S ____________________________________________________*/
#include "config.h" /* system configuration */
#include "c51_drv.h" /* c51 driver definition */
#include "..\..\lib\usb\usb_drv.h" /* usb driver definition */
#include "..\..\lib\spi\spi_drv.h" /* spi driver definition */
#include "df.h" /* dataflash definition */
/*_____ M A C R O S ________________________________________________________*/
/*_____ D E F I N I T I O N ________________________________________________*/
extern data Uint32 gl_ptr_mem; /* memory data pointer */
static bit df_mem_busy; /* memory in busy state */
/*_____ D E C L A R A T I O N ______________________________________________*/
static void df_busy (void);
/*F**************************************************************************
* NAME: df_init
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
* memory number if failed
*----------------------------------------------------------------------------
* PURPOSE:
* Dataflash controller initialization & memory check
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
Byte df_init (void)
{
Spi_set_mode(SPI_MASTER_MODE_3);
Spi_set_rate(SPI_RATE_0);
Spi_disable_ss();
Spi_enable();
DF_CS0 = 0; /* select first memory */
Spi_write_data(DF_RD_STATUS); /* read status */
Spi_ack_cmd();
Spi_write_dummy(); /* dummy write to get status */
Spi_ack_read();
if ((Spi_read_data() & DF_MSK_DENSITY) != DF_DENSITY)
{
return (1);
}
DF_CS0 = 1;
DF_CS1 = 0; /* select second memory */
Spi_write_data(DF_RD_STATUS); /* read status */
Spi_ack_cmd();
Spi_write_dummy(); /* dummy write to get status */
Spi_ack_read();
if ((Spi_read_data() & DF_MSK_DENSITY) != DF_DENSITY)
{
return (2);
}
DF_CS1 = 1;
DF_CS2 = 0; /* select third memory */
Spi_write_data(DF_RD_STATUS); /* read status */
Spi_ack_cmd();
Spi_write_dummy(); /* dummy write to get status */
Spi_ack_read();
if ((Spi_read_data() & DF_MSK_DENSITY) != DF_DENSITY)
{
return (3);
}
DF_CS2 = 1;
DF_CS3 = 0; /* select fourth memory */
Spi_write_data(DF_RD_STATUS); /* read status */
Spi_ack_cmd();
Spi_write_dummy(); /* dummy write to get status */
Spi_ack_read();
if ((Spi_read_data() & DF_MSK_DENSITY) != DF_DENSITY)
{
return (4);
}
DF_CS3 = 1;
df_mem_busy = FALSE; /* memory ready */
return 0;
}
/*F**************************************************************************
* NAME: df_init_mem
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* Dataflash initialization
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void df_init_mem (void)
{
df_mem_busy = FALSE; /* memory ready */
}
/*F**************************************************************************
* NAME: df_busy
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* Wait end of DataFlash busy status
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void df_busy (void)
{
/* select right memory */
if ((((Byte*)&gl_ptr_mem)[DF_MEM_BYTEH] & DF_MEM_MASKH) == 0)
{
if ((((Byte*)&gl_ptr_mem)[DF_MEM_BYTEL] & DF_MEM_MASKL) == 0)
DF_CS0 = 0;
else
DF_CS1 = 0;
}
else
{
if ((((Byte*)&gl_ptr_mem)[DF_MEM_BYTEL] & DF_MEM_MASKL) == 0)
DF_CS2 = 0;
else
DF_CS3 = 0;
}
Spi_write_data(DF_RD_STATUS); /* read status */
Spi_ack_cmd();
Spi_write_dummy(); /* dummy write to get status */
Spi_ack_read();
while ((Spi_read_data() & DF_MSK_BIT_BUSY) == DF_MEM_BUSY)
{
Spi_write_dummy(); /* dummy write to get status */
Spi_ack_read();
}
DF_CS |= DF_DESEL_ALL; /* deselect memory */
}
/*F**************************************************************************
* NAME: df_read_open
*----------------------------------------------------------------------------
* PARAMS:
* pos: next read operation address
*
* return:
* status: TRUE: open done
* FALSE: open not done: memory is still busy
*----------------------------------------------------------------------------
* PURPOSE:
* Open memory in read mode
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
* Address may not be synchronized on the beginning of a page
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
bit df_read_open (Uint32 pos)
{
gl_ptr_mem = pos << 9; /* gl_ptr_mem = pos * 512 */
if (df_mem_busy)
{
df_mem_busy = FALSE;
df_busy(); /* wait end of programming */
}
/* select right memory */
if ((((Byte*)&gl_ptr_mem)[DF_MEM_BYTEH] & DF_MEM_MASKH) == 0)
{
if ((((Byte*)&gl_ptr_mem)[DF_MEM_BYTEL] & DF_MEM_MASKL) == 0)
DF_CS0 = 0;
else
DF_CS1 = 0;
}
else
{
if ((((Byte*)&gl_ptr_mem)[DF_MEM_BYTEL] & DF_MEM_MASKL) == 0)
DF_CS2 = 0;
else
DF_CS3 = 0;
}
Spi_write_data(DF_RD_MAIN); /* send read main command */
Spi_ack_write();
Spi_write_data((((Byte*)&gl_ptr_mem)[1] << DF_SHFT_B1) | (((Byte*)&gl_ptr_mem)[2] >> DF_SHFT_B2));
Spi_ack_write();
Spi_write_data(((((Byte*)&gl_ptr_mem)[2] & ~DF_PAGE_MASK) << DF_SHFT_B1) | (((Byte*)&gl_ptr_mem)[2] & DF_PAGE_MASK));
Spi_ack_write();
Spi_write_data(((Byte*)&gl_ptr_mem)[3]);
Spi_ack_write();
Spi_write_data(0xFF); /* 4 dummy writes for reading delay */
Spi_ack_write();
Spi_write_data(0xFF);
Spi_ack_write();
Spi_write_data(0xFF);
Spi_ack_write();
Spi_write_data(0xFF);
Spi_ack_write();
return OK;
}
/*F**************************************************************************
* NAME: df_read_close
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* Memory read close: release DF memory
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void df_read_close (void)
{
DF_CS |= DF_DESEL_ALL; /* deselect memory */
}
/*F**************************************************************************
* NAME: df_read_byte
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
* Data read from memory
*----------------------------------------------------------------------------
* PURPOSE:
* Memory byte read function
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
* There are no end of page check with this function
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
Byte df_read_byte (void)
{
/* check if end of page */
if ((((Byte*)&gl_ptr_mem)[3] == 0x00) && ((((Byte*)&gl_ptr_mem)[2] & DF_PAGE_MASK) == 0x00))
{
DF_CS |= DF_DESEL_ALL; /* deselect all memories */
/* select right memory */
if ((((Byte*)&gl_ptr_mem)[DF_MEM_BYTEH] & DF_MEM_MASKH) == 0)
{
if ((((Byte*)&gl_ptr_mem)[DF_MEM_BYTEL] & DF_MEM_MASKL) == 0)
DF_CS0 = 0;
else
DF_CS1 = 0;
}
else
{
if ((((Byte*)&gl_ptr_mem)[DF_MEM_BYTEL] & DF_MEM_MASKL) == 0)
DF_CS2 = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -