This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

IAP

I am writing to flash memory of LPC2136.

Do I need to erase the segment before writing

Because i wrote garbage when i write to Flash after doing segment preparation only.

every thing fine If i do in following way
1.)prepare the segments
2.)erase the segments
3.)prepare the segments
4.)write to segments

Is there any thing wrong or this the only method to write to flash.

my worries is whether each time do i need to erase 32kb segment to write. so i need a copy of this 32kb in the ram.

Regards
murale

  • "Do I need to erase the segment before writing"

    You need to refer to the Datasheet for the answer to that!

    At the physical hardware level within the chip, you cannot write a '1' to a flash memory bit: Erasing sets bits to '1'; you then program '0' into all the bits that need to be '0'.

    However, some chips have clever logic that automatically does an erase-before-write, giving the appearance that you can write '1's to the flash. Again, you need to refer to the Datasheet to find out if this is the case for your particular chip.

    "every thing fine If i do in following way
    1.)prepare the segments
    2.)erase the segments
    3.)prepare the segments
    4.)write to segments"


    This looks very much like you have just answered your own question!

    "this the only method to write to flash."

    Again: see the Datasheet.

    "my worries is whether each time do i need to erase 32kb segment to write. so i need a copy of this 32kb in the ram."

    Yes, that is the usual problem with trying to update data in Flash...!! ;-)

  • No, you do not need to erase the segment before writing unless you want to change a 0 to 1 as already stated.

    What you need to take care about is that you may write only a page of 512/1024/... bytes at a time using Philips IAP functions. If you want to program only one byte you still need to read the whole page to ram, change only one byte and again write this modified page back to flash.
    This might be very time consuming if you are writing on a byte access level. It would be better to keep the whole page in RAM and write it, when the page is full.

    Franc

  • The correct reading is being writen only if i erase before writing.

    I dont know where i made mistake

    Below the code for write. and I always write to 14 th segment of LPC2136.

    _______________________
    unsigned int program (void *flash_addr, void *indata, unsigned int size) {
    struct iap_in iap; // IAP input parameters
    unsigned int result[16]; // IAP results
    // unsigned int save_VicInt; // for saving of interrupt enable register

    save_VicInt = VICIntEnable; // save interrupt enable status
    VICIntEnClr = 0xFFFFFFFF; // disable all interrupts

    //--below three added
    T0IR = 0x8; //clear all timer flags for ADC
    T1IR = 0x8; //clear all timer flags
    EXTINT = 0x0F; //clear all EINT flags

    //#define BYPASS_IAP


    #ifdef BYPASS_IAP
    stop_pll(); // IAP requires to run without PLL
    #endif

    iap.cmd = 50; // IAP Command: Prepare Sectors for Write
    // iap.par[0] = get_secnum (flash_addr); // start sector
    iap.par[0] = 14; // start sector
    // iap.par[1] = iap.par[0]; // end Sektor
    iap.par[1] = 14; // end Sektor
    iap_entry (&iap, result); // call IAP function
    if (result[0]) goto exit; // an error occured?

    iap.cmd = 51; // IAP Command: Copy RAM to Flash
    iap.par[0] = (unsigned int) flash_addr; // destination-addr
    iap.par[1] = (unsigned int) indata; // source-addr
    iap.par[2] = size; // number of bytes
    iap.par[3] = CCLK; // CPU clock
    iap_entry (&iap, result); // call IAP function

    exit:


    VICIntEnable = save_VicInt; // enable interrupts
    return (result[0]);
    }

    ------------------------------
    Is there any thing wrong here pl help.

    Regards

  • What is a flash_adr value? Is it aligned on page boundary?
    For 512 byte pages it should be corrected:

    flash_adr &= ~0x0000001FF;
    

    What is a size value? It should be 512 or a multiple of 512 if you want to program more pages in the same function call or want to use next available page size.

    Parameter iap.par[3] should be a system clock frequency in kHz. You do not need to disable PLL, but specify a correct CPU clock for paramter 3.

    You might be misleaded by ISP function description. Have in mind that ISP function do already implement the above IAP rules.

    Franc

  • Thanks Franc Urbanc for helping me.


    Can i write 1 or 0 as the same way with out considering the previous state(1 or 0) of the flash?

    Because every thing works fine only after i erase the flash before write.

    It looks i need to change all t0 1 before write 0.

    But i cant see any note about this in user manual.

    another point is
    LPC213X flash write size is 256 bytes according to the manual
    and never mention about the start boundary and i think it is fine if i start at word boundary.

    Please parden if any thing wrong and let me clear about this.

    Regards
    Murale

  • Can i write 1 or 0 as the same way with out considering the previous state(1 or 0) of the flash?

    No, not unless this is a really unusual flash technology.

    "Erasing" a flash device means setting all the bits to 1. "Programming" a flash means setting some bits to 0. "Programming" cannot set a bit to 1, and "erasing" cannot set a bit to 0.

    Physically, a bit of flash memory is like a little electron corral, surrounded by a "fence" of impassable potential. Pack a bunch of electrons into the corral, and the bit represents a 1. Open a hole in the fence, and connect the corral to ground, and all the electrons run out. Then the corral has a value of 0.

    "Erasing" a flash means pumping up the internal voltage to the point where you can force electrons into the corral despite the fence around it. Older flash parts used to have an extra high voltage Vcc input used just for this operation.

    Flash has a limited lifetime in terms of erase cycles because this is a fairly brute-force sort of operation, and eventually it wears down the potential barriers around the flash cells to the point where they no longer reliably pen up electrons. Once electrons can wander in and out of the corrals at their own whim, the device is no longer a reliable storage medium.

    For chip layout, density, and cost-per-bit reasons, the circuitry to do the programming isn't done on a per-bit basis. Making each bit independently eraseable would cost too much. Instead, large numbers of bits are grouped together into "pages" or "sectors". All the bits in a sector must be erased at the same time.

    "Programming" the flash means selectively opening up some of those cells and letting the electrons leak out. Whereever there is a 0 in the word to be programmed, the flash device drains electrons from the corresponding cell. Programming does nothing to the cells where there is a 1 in the data to be programmed. Those cells will keep their previous value.

    You can program a flash multiple times without erasing it, but all you can do each time is change 1s to 0s. You can never changes 0s to 1s without using the "erase" operation.

  • Here is what the documentation says about an IAP command Copy RAM to Flash (from LPC213x user manual):

    1. Param 0 (DST) - destination Flash address should be on a 256 byte boundary

    2. Param 1 (SRC) - source RAM address should be a word boundary (means 32-bit aligned)

    3. Param 2 - number of bytes written should be 256 | 512 | 1024 | 2048 | 4096

    4. Param 3 (CLK) - system clock Frequency in KHz.

    As you see you still need to obey the above rules or you will have problems using IAP Flash programming.

    Franc

  • Thank you very much for the detail discription Drew Davis

    best Regards
    murale

  • Thank you very much Franc Urbanc
    Best Regards
    Murale