Functions for Erasing and Writing Flash Memory

These functions support the erasing and writing of Flash memory for devices that have this type of memory.

Examples of Use

Example 1: 16-Bit MCUs

#include "libpic30.h" 
#include "p24Fxxxx.h" 

int __attribute__((space(prog),aligned(_FLASH_PAGE*2))) dat[_FLASH_PAGE]; 

int main() { 
  int i; 
  int source1[_FLASH_ROW]; 
  long source2[_FLASH_ROW]; 
  _prog_addressT p; 

  for (i = 0; i < _FLASH_ROW; ) {
    source1[i] = i; 
    source2[i] = i++; 
  } /* initialize some data */ 

  _init_prog_address(p, dat); /* get address in program space */ 
  _erase_flash(p); /* erase a page */ 
  _write_flash16(p, source1); /* write first row with 16-bit data */ 

  #if defined (__dsPIC30F__) 
    _erase_flash(p); /* on dsPIC30F, only 1 row per page */ 
  #else 
    p += (_FLASH_ROW * 2); /* advance to next row */ 
  #endif 

  _write_flash24(p, source2); /* write second row with 24-bit data */ 
}

Example 2: PIC24FXXKA MCUs

#include "libpic30.h" /* should use <> here */ 
#include "p24Fxxxx.h" 

int __attribute__((space(prog),aligned(_FLASH_2ROWS*2))) dat[_FLASH_2ROWS]; 

int main() { 
  int i; 
  int source1[_FLASH_ROW]; 
  long source2[_FLASH_ROW]; 
  _prog_addressT p; 

  for (i = 0; i < _FLASH_ROW; ) {
    source1[i] = i; 
    source2[i] = i++; 
  } /* initialize some data */  

  _init_prog_address(p, dat); /* get address in program space */ 
  _erase_flash(p, _FLASH_2ROWS); /* erase two rows */ 
  _write_flash16(p, source1); /* write first row with 16-bit data */ 

  p += (_FLASH_ROW * 2); /* advance to next row */ 
  _write_flash24(p, source2); /* write second row with 24-bit data */ 
}