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

Using ECON in a C Program

I have seen data be written to the flash/EE data memory with ECON in assembly language but I have not been able to find any example code in C. I am unfamiliar with assembly so I am trying to learn how to write data to the flash/EE data memory in C. If anyone has any ideas on how to do this it would help greatly. thanks.

- Nathan Necaise


  • You're using an Analog Devices "MicroConverter" of some sort?

    Their ECON register is just a special function register (SFR) that controls flash. There are also some address (ADRL), data (EDATAx), and timing (ETIMx) registers that go with it.

    Keil probably has a header file for your MCU chip (e.g., ADUC812.h) already. If not, then you can declare the SFRs in C with code like this (taken right out of ADUC812.h):

    sfr   ECON      = 0xB9;
    sfr   ETIM1     = 0xBA; //AR
    sfr   ETIM2     = 0xBB;
    sfr   ETIM3     = 0xC4;
    sfr   EDATA1    = 0xBC;
    sfr   EDATA2    = 0xBD;
    sfr   EDATA3    = 0xBE;
    sfr   EDATA4    = 0xBF;
    

    See the data sheet for the appropriate SFR addresses. Once that's done, you can read and write to the SFRs just by using C assignment statements and expressions.

    The actual values you need to program for timing should be in the data sheet for your part. You could also just mimic the values you see in the assembly code.

    There's a nice chapter in the datasheet that explains how the flash/EE memory on the parts work. Translation of the examples from assembly should be pretty straightforward, e.g. the datasheet example of programming:

    This example, coded in 8051 assembly, would appear as:
    MOV EADRL, #03H ; Set Page Address Pointer
    MOV ECON, #01H ; Read Page
    MOV EDATA2, #0F3H ; Write New Byte
    MOV ECON, #05H ; Erase Page
    MOV ECON, #02H ; Write Page (Program
    Flash/EE)
    

    turns literally into C code thus:

    EADRL = 0x03;  // set page address == 3
    ECON = 0x01;   // read page 3 into EDATA1-4
    EDATA2 = 0xf3; // update byte 2
    ECON = 0x05;   // erase page (3)
    ECON = 0x02;   // write page with EDATA1-4
    

    I'd #define or typedef enum some constants for those command values in practice. You might also might want to write routines to encapsulate the operation so that you can just hand it a value and an address from which you calculate the page number and correct EDATAx register.

    Looks like they made it pretty straightforward as flash access goes.