TI MCU Memory Allocation Issue

Thread Starter

johndeaton

Joined Sep 23, 2015
63
Hi All,

I'm working on a project using a Texas Instruments MSP430G2432IPW20R. One of my main challenges with this project will be low data memory. One way I'm trying to save memory is by declaring bit fields in my structures. However, if I compile it with or without the bit field definition, the memory allocation comes out the same (see code below). I tried turning on optimization, but that didn't help. Any ideas?

Thanks in advance,
John


Code:
typedef struct {
    timer_base_e time_base;
    uint8_t is_running : 1;
    uint8_t expired : 1;
    uint16_t ticks_remaining;
    uint16_t reset_value;
} timer_s;
or

Code:
typedef struct {
    timer_base_e time_base;
    uint8_t is_running;
    uint8_t expired;
    uint16_t ticks_remaining;
    uint16_t reset_value;
} timer_s;
 

Thread Starter

johndeaton

Joined Sep 23, 2015
63
Hi Papa,

Thanks for the reply. I learned some things from the link you posted. Unfortunately, the suggestion in that thread did not work. However, I was able to get the compiler to pack the bits by also making the enumeration a bit field. The total size of the structure dropped from 8 bytes to 6 bytes.

Code:
typedef struct {
    timer_base_e time_base : 3;
    uint8_t is_running : 1;
    uint8_t expired : 1;
    uint16_t ticks_remaining;
    uint16_t reset_value;
} timer_s ;
Not sure why this worked, but thought I'd share in case it helps someone else.

Thanks
 

Papabravo

Joined Feb 24, 2006
21,225
It kinda looks like it is allocating the smallest possible storage unit, a byte, for each unique type, with a bit field, in the structure.
There is 1 byte for time_base,
there is 1 byte for is_running and expired,
there are 2 bytes for ticks_remaining,
and there are 2 bytes for reset_value.
 

Thread Starter

johndeaton

Joined Sep 23, 2015
63
It kinda looks like it is allocating the smallest possible storage unit, a byte, for each unique type, with a bit field, in the structure.
There is 1 byte for time_base,
there is 1 byte for is_running and expired,
there are 2 bytes for ticks_remaining,
and there are 2 bytes for reset_value.
You're right. But, the weird thing is, if I remove the bit field declaration from the enumeration, the size of the structure changes to 8 bytes instead of 7 like I would expect. Seems that is_running and expired no longer share a byte.
 

Papabravo

Joined Feb 24, 2006
21,225
You're right. But, the weird thing is, if I remove the bit field declaration from the enumeration, the size of the structure changes to 8 bytes instead of 7 like I would expect. Seems that is_running and expired no longer share a byte.
It would depend on the typedef for timer_base_e. Restricting it to 3 bits may have allowed it to occupy a byte instead of what would be implied by its base type.
 
Top