SAFM Overview

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

SAFWrite

a subroutine with the parameters: location, byte value

SAFWrite ( location, byte_variable )

SAFWriteWord

a subroutine with the parameters: location, word_value

SAFWriteWord ( location, word_variable )

SAFRead

a function with the parameters: location returns a byte value

byte_variable = SAFRead ( location )

SAFRead

a subroutine with the paramers: location, byte_value

SAFRead ( location , out_byte_variable )

SAFReadWord

a function with the parameters: location returns a word value

word_variable = SAFRead ( location )

SAFReadWord

a subroutine with the parameters: location, word_value

SAFRead ( location , word_variable )

SAFEraseBlock

a subroutine with the parameters: block_number

SAFEraseBlock ( 0 )

A value of 0,1,2,3 etc.

SAFWriteBlock

a subroutine with the parameters: block_number, buffer() [,num_blocks ]

SAFWriteBlock( 0, myMemoryBuffer ) 'where myMemoryBuffer is an Array or a String

The Array or a String will contain the values to be wrttin to the SAFM.

SAFReadBlock

a subroutine with the parameters: block_number, buffer() [, num_blocks ]

SAFReadBlock( 0, myMemoryBuffer ) 'where myMemoryBuffer is an Array or a String.

The Array or a String will contain the values from the SAFM.



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

SAF_ROWSIZE_BYTES

Byte

Size of an SAFM block in bytes

SAF_WORDS and SAF_BYTES

Word or a Byte

ChipSAFMemWords parameter from the device .dat file

SAF_START_ADDR

Word

Starting address of SAFM

SAF_NUM_BLOCKS

Byte

Number of block of SAFM

CHIPWORDS

Word

Device specific constant for the total flash size

CHIPSAFMEMWORDS

Word

Device specific constant for the number of SAFM words available

CHIPERASEROWSIZEWORDS

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