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

May functions return an union?

Is it possible for a function to return a union?

Storing data in the Flash/EEData memory of the ADuC845, is done through operating some SFR registers and page write.
I would like to read and write different kind of variables and wounder if its possible to use the same function.

Example:
// EEData page address
#define SERIAL_NO 0x0000
#define DEV_ADR 0x0010

unsigned long serial_no = 123456789;
unsigned int dev_adr = 255;

void main (void) {
// saving data
write(serial_no, SERIAL_NO);
write(dev_adr, DEV_ADR);
// reading data
serial_no = read(SERIAL_NO);
dev_adr = read(DEV_ADR);
}

My idea was something like this:

union u {
unsigned int ival;
unsigned long lval;
}

void write(union u data, long adr) {
// save data in EEData at address long
}

union u read(long adr) {
// read data from EEData
// and return (???)
}

Is it possible to do it this way or perhaps a solution would be the use of pointers but how?

Any suggestions?

Parents
  • "... or perhaps a solution would be the use of pointers but how?"

    A more conventional interface is for the read/write routines to deal with data objects of arbitrary size.

    #define SERIAL_NO  0x0000
    #define DEV_ADR    0x0010
    
    unsigned long  serial_no = 123456789;
    unsigned int   dev_adr   = 255;
    
    void write(unsigned ee_addr, void *p_data, size_t size) {
    }
    
    void read(unsigned ee_addr, void *p_data, size_t size) {
    }
    
    void main (void) {
        write(SERIAL_NO, &serial_no, sizeof serial_no);
        write(DEV_ADR, &dev_adr, sizeof dev_addr);
        read(SERIAL_NO, &serial_no, sizeof serial_no);
        read(DEV_ADR, &dev_adr, sizeof dev_addr);
    }
    

Reply
  • "... or perhaps a solution would be the use of pointers but how?"

    A more conventional interface is for the read/write routines to deal with data objects of arbitrary size.

    #define SERIAL_NO  0x0000
    #define DEV_ADR    0x0010
    
    unsigned long  serial_no = 123456789;
    unsigned int   dev_adr   = 255;
    
    void write(unsigned ee_addr, void *p_data, size_t size) {
    }
    
    void read(unsigned ee_addr, void *p_data, size_t size) {
    }
    
    void main (void) {
        write(SERIAL_NO, &serial_no, sizeof serial_no);
        write(DEV_ADR, &dev_adr, sizeof dev_addr);
        read(SERIAL_NO, &serial_no, sizeof serial_no);
        read(DEV_ADR, &dev_adr, sizeof dev_addr);
    }
    

Children
No data