Shop OBEX P1 Docs P2 Docs Learn Events
[Solved] How to use persistant vars in EEPROM — Parallax Forums

[Solved] How to use persistant vars in EEPROM

DavidZemonDavidZemon Posts: 2,973
edited 2014-09-23 17:31 in Propeller 1
Hello all,

I could pick a random large number for the variable address and hardcode it to a pointer... but I'd like to know if there is a better way to persistently read from and write to a variable in EEPROM. I only need to 16 bits, so a full filesystem on an SD card seems like a bit of overkill. Any ideas?

Thanks,
David

Comments

  • ElectrodudeElectrodude Posts: 1,658
    edited 2014-09-22 19:13
    Write the variable to its RAM address in EEPROM - next boot, its new value will get loaded and not the 0 that used to be in the EEPROM.

    Using Chris Gadd's "I2C driver.spin" as an example:
    I2C.write_page(I2C#EEPROM,@val,val, 4)  ' long
    I2C.write_page(I2C#EEPROM,@val,val, 2)  ' word
    I2C.write(I2C#EEPROM,@val,val)  ' byte
    

    If you're using C, it should be pretty similar, but I would probably make the variable volatile.

    Volatile just means don't optimize this out, right?
  • DavidZemonDavidZemon Posts: 2,973
    edited 2014-09-22 19:57
    That looks great! I'll look into this :)
  • pmrobertpmrobert Posts: 673
    edited 2014-09-23 11:20
    This is what I use.
    /* Serial get/send integer routines */
        void send_int(int32_t z) {
            char b;
            b = z>>24;
            fdserial_txChar(serial,b);
            b = z>>16;
            fdserial_txChar(serial,b);
            b = z>>8;
            fdserial_txChar(serial,b);
            b = z;
            fdserial_txChar(serial,b);
        }
    
        int32_t get_int(void) {
            char b;
            int32_t result;
            result = 0;
            b = fdserial_rxChar(serial);
            result = b<<24;
            b = fdserial_rxChar(serial);
            result = result + (b<<16);
            b = fdserial_rxChar(serial);
            result = result + (b<<8);
            b = fdserial_rxChar(serial);
            result = result + b;
            return result;
        }
    
        /* target: 1=fuel, 2=ign, 3=afr, 4 = CLTCOMP, 5 = MATCOMP, more to come */
        /* op: 1 = EEPROM to mem, 2 = mem to EEPROM, 3 = mem to app, 4 = app to mem */
        /* 5 = get PIFlow/SIFlow 6 = send PIFlow/SIFlow */
        void DataShuttle(int target, int op) {
            int row,col;
            switch (target) {
    
            case 1 :
                for (row = 0; row < FUELBASEYSIZE; row++ )
                    for (col = 0; col < FUELBASEXSIZE; col++ )
                        if (op == 1) { //EE2Mem
                            FuelBaseTable[col][row]=ee_getInt(FuelBaseTableOrg+(row*FUELBASEXSIZE*4)+(col*4));
                        } else if (op == 2) { //Mem2EE
                            ee_putInt(FuelBaseTable[col][row],FuelBaseTableOrg+(row*FUELBASEXSIZE*4)+(col*4));
                        } else if (op == 3) {
                            send_int(FuelBaseTable[col][row]);
                        } else if(op == 4) {
                            FuelBaseTable[col][row] = get_int();
                        }
    
                for (col = 0; col < FUELBASEXSIZE; col++ )
                    if (op == 1) {
                        FuelBaseXAxis[col]=ee_getInt(FuelBaseXAxisOrg+(col*4));
                    } else if (op == 2) {
                        ee_putInt(FuelBaseXAxis[col],FuelBaseXAxisOrg+(col*4));
                    } else if (op == 3) {
                        send_int(FuelBaseXAxis[col]);
                    } else if (op == 4) {
                        FuelBaseXAxis[col] = get_int();
                    }
    
                for (row = 0; row < FUELBASEYSIZE; row++ )
                    if (op == 1) {
                        FuelBaseYAxis[row]=ee_getInt(FuelBaseYAxisOrg+(row*4));
                    } else if (op == 2) {
                        ee_putInt(FuelBaseYAxis[row],FuelBaseYAxisOrg+(row*4));
                    } else if (op == 3) {
                        send_int(FuelBaseYAxis[row]);
                    } else if (op == 4) {
                        FuelBaseYAxis[row] = get_int();
                    }
    
                break;
    
    /* Fuel Base Table */
    #define FUELBASEXSIZE 16
    #define FUELBASEYSIZE 16
    volatile int32_t FuelBaseTable[FUELBASEXSIZE][FUELBASEYSIZE]; // 1024 bytes
    volatile int32_t FuelBaseTableOrg = 32768; // 0x8000
    volatile int32_t FuelBaseXAxis[FUELBASEXSIZE];
    volatile int32_t FuelBaseXAxisOrg = 33792;
    volatile int32_t FuelBaseYAxis[FUELBASEYSIZE];
    volatile int32_t FuelBaseYAxisOrg = 33856;
    volatile int32_t FuelCellVal;
    
  • DavidZemonDavidZemon Posts: 2,973
    edited 2014-09-23 17:31
    OH! ee_put* functions are in Simple! That's great :D

    For anyone else stumbling across this thread, there are lots of ee_put* functions - there's one for byte, another for strings, etc.

    All can be found in the doxygen documentation of Simple. Here's a link to ee_putInt() though:
    http://david.zemon.name/PropWare/simpletools_8h.html#adf44a20755432a25076e28e94b809264
Sign In or Register to comment.