Start Back Next End
  
ZBasic System Library
ZBasic Microcontrollers
55
SPI Flash Allocation
An ESP8266 application usually consists of two binary image files.  The first image file, stored at address
0x0000 of the Flash chip contains the application boot code (instruction RAM code, and RAM data) and is
limited to 64KB in size.  The second image file contains the ROM code (instruction ROM code and read-
only data).  The primary difference between the handling of the content of the two images is that the
content of the first image gets loaded into RAM at boot time while the routines in the second image are
loaded into a RAM instruction cache as they are needed or (for read-only data) read directly from Flash
when needed.
The ZBasic compiler produces a combined image file containing both of the images described in the
previous paragraph.  The name of the combined image file is based on the project name but it has the
extension .esp.  The ZBasic compiler structures the application to be compatible with the Flash memory
map below.
ESP8266 Memory Map for 512K Flash Chip
Address
Size
Description
0x0000000
0x010000 (64K)
Application boot image
0x0010000
0x001000 (4K)
ZBasic Persistent memory
0x0011000
0x06b000 (428K)
Application ROM code/data
0x007c000
0x004000 (16K)
ESP8266 system parameters
Although most ESP8266 devices come equipped with a 512KB Flash chip, some are available with larger
capacity chips, e.g. 1MB, 2MB, 4MB or 8MB.  The Flash map for the larger Flash chips is similar to that
depicted above.  The first three entries have the same start address but the start address for the fourth
(the sytem parameter area) is always 16KB from the end of the Flash chip.
The area between the ZBasic Persistent memory and the ESP8266 system parameters is available for
use by the application.  Any space beyond the end of the application code/data can be used for any
purpose.  One example of how this "empty" space might be used is a Flash-based file system like
SPIFFS (SPI Flash File System).  More discussion about SPIFFS is found elsewhere in this document.
Using an SPI Flash File System (SPIFFS)
The ZBasic System Library incorporates a version of Peter Andersson's open source SPIFFS code
(available at github.com), slightly modifed and targeted to the ESP8266.  The SPIFFS code allows an
application to use part of the Flash memory as a single-level (i.e. non-hierarchical) file system.  The
ZBasic System Library for the ESP8266 contains new routines (e.g. File.Open) to allow interaction with
the file system.
In order to use SPIFFS you must designate the starting address and size of the area of Flash that you
want to use as the file system.  In general, it is advisable to choose the starting address so that the last
byte of Flash used in file system is just before the ESP8266 system parameter area which is always
located 16KB from the declared end of Flash.  So if you wanted a file system 32KB in size the best place
to put it is 48KB from the end of Flash.  For a 512KB flash that would be at hexadecimal address 0x80000
- 0x4000 - 0x8000 = 0x7c000.  Due to the design of the Flash chips used on ESP8266 devices, the file
system must begin on a 4KB boundary and be an integral multiple of 4KB in size.
It is important that the Flash memory to be used for SPIFFS be properly initialized.  An empty file system
can be initialized by erasing the Flash memory, doing so sets all bytes to 0xff.  Another useful way to
initialize the SPIFFS is to load a prepared SPIFFS image.  The ZBasic distribution contains a utility
named spiffy.exe that can be used to create and populate a SPIFFS image.
The syntax for invoking spiffy is:
spiffy [<options>]
where the available options are as follows.
Previous page Top Next page