Build time packaging

I am writing C for PIC32MX compiled using the Microchip PIC32 C compiler (based on GCC 3.4).

Added . The standard that I follow is GNU99 (C99 with GNU extensions, compiler flag -std=gnu99)

My problem is this: I have some reprogrammable numerical data that is stored either on the EEPROM or in the software flash of the chip. This means that when I want to save a float, I have to execute some type of punning:

typedef union
{
    int intval;
    float floatval;
} IntFloat;

unsigned int float_as_int(float fval)
{
    IntFloat intf;
    intf.floatval = fval;
    return intf.intval;
}

// Stores an int of data in whatever storage we're using
void StoreInt(unsigned int data, unsigned int address);

void StoreFPVal(float data, unsigned int address)
{
    StoreInt(float_as_int(data), address);
}

. ( ) , . float Python, , :

import struct
hex(struct.unpack("I", struct.pack("f", float_value))[0])

... , :

const unsigned int DEFAULTS[] =
{
    0x00000001, // Some default integer value, 1
    0x3C83126F, // Some default float value, 0.005
}

( X macro, .) , ? , - :

const unsigned int DEFAULTS[] =
{
    0x00000001, // Some default integer value, 1
    COMPILE_TIME_CONVERT(0.005), // Some default float value, 0.005
}

... , , .

  • , ", " , .
  • , , , , undefined ( IDB, ).
  • , , DEFAULTS . , , .
+3
1

DEFAULTS IntFloat?

, C99, :

const IntFloat DEFAULTS[] =
{
    { .intval = 0x00000001 }, // Some default integer value, 1
    { .floatval = 0.005 }, // Some default float value, 0.005
};
+9

Source: https://habr.com/ru/post/1747924/


All Articles