📄 ecos-flash-v1.html
字号:
<!-- Copyright (C) 2009 Free Software Foundation, Inc. -->
<!-- This material may be distributed only subject to the terms -->
<!-- and conditions set forth in the Open Publication License, v1.0 -->
<!-- or later (the latest version is presently available at -->
<!-- http://www.opencontent.org/openpub/). -->
<!-- Distribution of the work or derivative of the work in any -->
<!-- standard (paper) book form is prohibited unless prior -->
<!-- permission is obtained from the copyright holder. -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>The legacy Version 1 eCos FLASH API</TITLE
><meta name="MSSmartTagsPreventParsing" content="TRUE">
<META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REL="HOME"
TITLE="eCos Reference Manual"
HREF="ecos-ref.html"><LINK
REL="UP"
TITLE="FLASH Library"
HREF="io-flash.html"><LINK
REL="PREVIOUS"
TITLE="FLASH device API"
HREF="ecos-flash-v2-dev.html"><LINK
REL="NEXT"
TITLE="FLASH device API"
HREF="ecos-flash-v1-dev.html"></HEAD
><BODY
CLASS="CHAPTER"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>eCos Reference Manual</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="ecos-flash-v2-dev.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="ecos-flash-v1-dev.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="CHAPTER"
><H1
><A
NAME="ECOS-FLASH-V1"
></A
>Chapter 28. The legacy Version 1 eCos FLASH API</H1
><DIV
CLASS="TOC"
><DL
><DT
><B
>Table of Contents</B
></DT
><DT
><A
HREF="ecos-flash-v1.html#ECOS-FLASH-V1-API"
>FLASH user API</A
></DT
><DT
><A
HREF="ecos-flash-v1-dev.html"
>FLASH device API</A
></DT
></DL
></DIV
><P
>The library has a number of limitations:</P
><P
></P
><OL
TYPE="1"
><LI
><P
>Only one family of FLASH device may be supported at once.
</P
></LI
><LI
><P
>Multiple devices of one family are supported, but they must
be contiguous in memory.
</P
></LI
><LI
><P
>The library is not thread or interrupt safe under
some conditions.
</P
></LI
><LI
><P
>The library currently does not use the eCos naming
convention for its functions. This may change in the
future but backward compatibility is likely to be kept.
</P
></LI
></OL
><P
> There are two APIs described here. The first is the application
API which programs should use. The second API is that between the
FLASH io library and the device drivers. </P
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="ECOS-FLASH-V1-API"
>FLASH user API</A
></H1
><P
>All of the functions described below are declared in the header
file <TT
CLASS="FILENAME"
><cyg/io/flash.h></TT
> which all users of
the FLASH library should include.</P
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN7947"
>Initializing the FLASH library</A
></H2
><P
>The FLASH library needs to be initialized before other FLASH
operations can be performed. This only needs to be done once. The
following function will only do the initialization once so it's safe
to call multiple times: </P
><TABLE
BORDER="5"
BGCOLOR="#E0E0F0"
WIDTH="70%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>externC int flash_init( _printf *pf );
typedef int _printf(const char *fmt, ...); </PRE
></TD
></TR
></TABLE
><P
>The parameter <CODE
CLASS="PARAMETER"
>pf</CODE
> is a pointer to a function
which is to be used for diagnostic output. Typically the function
<CODE
CLASS="FUNCTION"
>diag_printf()</CODE
> will be passed. Normally this
function is not used by the higher layer of the library unless
<TT
CLASS="LITERAL"
>CYGSEM_IO_FLASH_CHATTER</TT
> is enabled. Passing a
<CODE
CLASS="PARAMETER"
>NULL</CODE
> is not recommended, even when
CYGSEM_IO_FLASH_CHATTER is disabled. The lower layers of the library
may unconditionally call this function, especially when errors occur,
probably resulting in a more serious error/crash!.</P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN7956"
>Retrieving information about the FLASH</A
></H2
><P
>The following four functions return information about the FLASH.</P
><TABLE
BORDER="5"
BGCOLOR="#E0E0F0"
WIDTH="70%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>externC int flash_get_block_info(int *block_size, int *blocks);
externC int flash_get_limits(void *target, void **start, void **end);
externC int flash_verify_addr(void *target);
externC bool flash_code_overlaps(void *start, void *end);</PRE
></TD
></TR
></TABLE
><P
>The function <CODE
CLASS="FUNCTION"
>flash_get_block_info()</CODE
> returns the
size and number of blocks. When the device has a mixture of block
sizes, the size of the "normal" block will be returned. Please read
the source code to determine exactly what this means.
<CODE
CLASS="FUNCTION"
>flash_get_limits()</CODE
> returns the lower and upper
memory address the FLASH occupies. The <CODE
CLASS="PARAMETER"
>target</CODE
>
parameter is current unused. <CODE
CLASS="FUNCTION"
> flash_verify_addr()</CODE
> tests if the target addresses is within the flash,
returning <TT
CLASS="LITERAL"
>FLASH_ERR_OK</TT
> if so. Lastly, <CODE
CLASS="FUNCTION"
>flash_code_overlaps() </CODE
> checks if the executing code is
resident in the section of flash indicated by
<CODE
CLASS="PARAMETER"
>start</CODE
> and <CODE
CLASS="PARAMETER"
> end</CODE
>. If this
function returns true, erase and program operations within this range
are very likely to cause the target to crash and burn horribly. Note
the FLASH library does allow you to shoot yourself in the foot in this
way.</P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN7969"
>Reading from FLASH</A
></H2
><P
>There are two methods for reading from FLASH. The first is to use the
following function. </P
><TABLE
BORDER="5"
BGCOLOR="#E0E0F0"
WIDTH="70%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>externC int flash_read(void *flash_base, void *ram_base, int len, void **err_address);</PRE
></TD
></TR
></TABLE
><P
><CODE
CLASS="PARAMETER"
>flash_base</CODE
> is where in the flash to read
from. <CODE
CLASS="PARAMETER"
>ram_base</CODE
> indicates where the data read
from flash should be placed into RAM. <CODE
CLASS="PARAMETER"
>len</CODE
> is
the number of bytes to be read from the FLASH and
<CODE
CLASS="PARAMETER"
>err_address</CODE
> is used to return the location in
FLASH that any error occurred while reading.</P
><P
>The second method is to simply <CODE
CLASS="FUNCTION"
>memcpy()</CODE
> directly
from the FLASH. This is not recommended since some types of device
cannot be read in this way, eg NAND FLASH. Using the FLASH library
function to read the FLASH will always work so making it easy to port
code from one FLASH device to another.</P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN7980"
>Erasing areas of FLASH</A
></H2
><P
>Blocks of FLASH can be erased using the following function:</P
><TABLE
BORDER="5"
BGCOLOR="#E0E0F0"
WIDTH="70%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>externC int flash_erase(void *flash_base, int len, void **err_address);</PRE
></TD
></TR
></TABLE
><P
><CODE
CLASS="PARAMETER"
>flash_base</CODE
> is where in the flash to erase
from. <CODE
CLASS="PARAMETER"
>len</CODE
> is the minimum number of bytes to
erase in the FLASH and <CODE
CLASS="PARAMETER"
>err_address</CODE
> is used to
return the location in FLASH that any error occurred while erasing. It
should be noted that FLASH devices are block oriented when erasing. It
is not possible to erase a few bytes within a block, the whole block
will be erased. <CODE
CLASS="PARAMETER"
>flash_base</CODE
> may be anywhere
within the first block to be erased and <CODE
CLASS="PARAMETER"
>flash_base+len</CODE
> may be anywhere in the last block to be erased. </P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN7990"
>Programming the FLASH</A
></H2
><P
> Programming of the flash is achieved using the following
function.</P
><TABLE
BORDER="5"
BGCOLOR="#E0E0F0"
WIDTH="70%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>externC int flash_program(void *flash_base, void *ram_base, int len, void **err_address);</PRE
></TD
></TR
></TABLE
><P
><CODE
CLASS="PARAMETER"
>flash_base</CODE
> is where in the flash to program
from. <CODE
CLASS="PARAMETER"
>ram_base</CODE
> indicates where the data to be
programmed into FLASH should be read from in RAM. <CODE
CLASS="PARAMETER"
>len</CODE
> is the number of bytes to be program into the FLASH and
<CODE
CLASS="PARAMETER"
>err_address</CODE
> is used to return the location in
FLASH that any error occurred while programming. </P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN7999"
>Locking and unlocking blocks</A
></H2
><P
>Some flash devices have the ability to lock and unlock blocks. A
locked block cannot be erased or programmed without it first being
unlocked. For devices which support this feature and when <TT
CLASS="LITERAL"
>CYGHWR_IO_FLASH_BLOCK_LOCKING</TT
> is enabled then the following
two functions are available:</P
><TABLE
BORDER="5"
BGCOLOR="#E0E0F0"
WIDTH="70%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>externC int flash_lock(void *flash_base, int len, void **err_address);
externC int flash_unlock(void *flash_base, int len, void **err_address);</PRE
></TD
></TR
></TABLE
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN8004"
>Return values and errors</A
></H2
><P
>All the functions above, except <CODE
CLASS="FUNCTION"
>flash_code_overlaps()</CODE
> return one of the following return values.</P
><TABLE
BORDER="5"
BGCOLOR="#E0E0F0"
WIDTH="70%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>FLASH_ERR_OK No error - operation complete
FLASH_ERR_INVALID Invalid FLASH address
FLASH_ERR_ERASE Error trying to erase
FLASH_ERR_LOCK Error trying to lock/unlock
FLASH_ERR_PROGRAM Error trying to program
FLASH_ERR_PROTOCOL Generic error
FLASH_ERR_PROTECT Device/region is write-protected
FLASH_ERR_NOT_INIT FLASH info not yet initialized
FLASH_ERR_HWR Hardware (configuration?) problem
FLASH_ERR_ERASE_SUSPEND Device is in erase suspend mode
FLASH_ERR_PROGRAM_SUSPEND Device is in program suspend mode
FLASH_ERR_DRV_VERIFY Driver failed to verify data
FLASH_ERR_DRV_TIMEOUT Driver timed out waiting for device
FLASH_ERR_DRV_WRONG_PART Driver does not support device
FLASH_ERR_LOW_VOLTAGE Not enough juice to complete job</PRE
></TD
></TR
></TABLE
><P
>To turn an error code into a human readable string the following
function can be used:</P
><TABLE
BORDER="5"
BGCOLOR="#E0E0F0"
WIDTH="70%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>externC char *flash_errmsg(int err);</PRE
></TD
></TR
></TABLE
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN8011"
>Notes on using the FLASH library</A
></H2
><P
>The FLASH library evolved from the needs and environment of RedBoot
rather than being a general purpose eCos component. This history
explains some of the problems with the library. </P
><P
>The library is not thread safe. Multiple simultaneous calls to
its library functions will likely fail and may cause a crash. It is
the callers responsibility to use the necessary mutex's if needed.</P
></DIV
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="ecos-flash-v2-dev.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="ecos-ref.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="ecos-flash-v1-dev.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>FLASH device API</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="io-flash.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>FLASH device API</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -