Introduction:
Some Advanced (18F) and some Enhanced Mid-Range (16F) Microchip PIC devices support Storage Area Flash (SAF) memory. These
devices also include EEPROM memory.
SAF memory is not High Endurance, meaning it does not have an endurance of 100K write cyces.
SAF has the same endurance as regular flash memmory, usually specified as 10K write cycles.
SAF memory appears at the top of program memory space and can be used for any purpose, like regular flash program memory.
Storage Area Flash is intended to be used to store data, such a device calibration data, RF device register settings, and
other data. SAFEM can be Read as frequently as necessary. However, it is not intended to be written frequently like EEPROM.
If non-volatile memory need to be written frequenily, it is best to use the EEPROM on these devices.
As with all flash memory, data must be erased before it can be written and writing this memory will stall the device for a
few ms.
Methods to read, write and erase the SAF memory are included in GCBASIC and they are described in this introduction.
The hefsaf.h
library supports SAF operations for GCBASIC.
Note: By default, GCBASIC will use SAF memory for regular executable code unless it is told otherwise.
If you wish to store data here, you should reserve the SAF memory by using the compiler option, as shown below to reserve
128 Words of SAF memory: This equates to 256 bytes on PIC 18F microcontrollers and 128 Bytes on PIC 16F microcontrollers
#option ReserveHighProg 128
SAF memory is a block of memory locations found at the top of the Flash program memory.
Each memory location can be used to hold a variable value, either a byte or a word dependent on the specific device.
The main difference between SAF memory and EEPROM is that EEPROM allows byte-by-byte erase whereas the SAF memory does not.
With SAF memory data must be erased before a write and the erase can only be performed in blocks of memory.
The blocks, also called rows, are a fixed size associated with the specific device.
GCBASIC handles the erase operation automatically.
When a write operation is used by a user the GCBASIC library reads to a buffer, update the buffer, erase the block and finally
write the buffer back to SAFM.
The complexity of using SAF memory is reduced with the automatically handling of these operations.
The library provides a set of methods to support use of SAF memory.
Method |
Parameters |
Usage |
|
a subroutine with the parameters: location, byte value |
SAFWrite ( location, byte_variable ) |
|
a subroutine with the parameters: location, word_value |
SAFWriteWord ( location, word_variable ) |
|
a function with the parameters: location returns a byte value |
byte_variable = SAFRead ( location ) |
|
a subroutine with the paramers: location, byte_value |
SAFRead ( location , out_byte_variable ) |
|
a function with the parameters: location returns a word value |
word_variable = SAFRead ( location ) |
|
a subroutine with the parameters: location, word_value |
SAFRead ( location , word_variable ) |
|
a subroutine with the parameters: block_number |
SAFEraseBlock ( 0 )
|
|
a subroutine with the parameters: block_number, buffer() [,num_blocks ] |
SAFWriteBlock( 0, myMemoryBuffer ) 'where myMemoryBuffer is an Array or a String
|
|
a subroutine with the parameters: block_number, buffer() [, num_blocks ] |
SAFReadBlock( 0, myMemoryBuffer ) 'where myMemoryBuffer is an Array or a String.
|
The library also defines a set constants that are specific to the device.
These may be useful in the user program.
These constants are used by the library.
A user may use these public constants.
Constant |
Type |
Usage |
|
Byte |
Size of an SAFM block in bytes |
|
Word or a Byte |
ChipSAFMemWords parameter from the device .dat file |
|
Word |
Starting address of SAFM |
|
Byte |
Number of block of SAFM |
|
Word |
Device specific constant for the total flash size |
|
Word |
Device specific constant for the number of SAFM words available |
|
Word |
Device specific constant for the number of SAFM in an erase row |
Warning
Whenever you update the hex file of your Microchip PIC micro-controller with your programmer you MAY erase the data that are
stored in SAF memory.
If you want to avoid that you will have to flash your Microchip PIC micro-controller with software that allows memory exclusion
when flashing.
This is the case with Microchip PIC MPLAB IPE (Go to Advanced Mode/Enter password/Select Memory/Tick “Preserve Flash on Program”/
Enter Start and End address
of your SAFM). Or, simply use the PICkitPlus suite of software to preserve SAF memory during programming.
See also
SAFRead,
SAFReadWord,
SAFWrite,
SAFWriteWord,
SAFReadBlock,
SAFWriteBlock,
SAFEraseBlock