TI MCU Memory Allocation Issue

Discussion in 'Embedded Systems and Microcontrollers' started by johndeaton, Oct 30, 2016.

  1. johndeaton

    Thread Starter Member

    Sep 23, 2015
    63
    4
    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 (Text):
    1. typedef struct {
    2.     timer_base_e time_base;
    3.     uint8_t is_running : 1;
    4.     uint8_t expired : 1;
    5.     uint16_t ticks_remaining;
    6.     uint16_t reset_value;
    7. } timer_s;
    or

    Code (Text):
    1. typedef struct {
    2.     timer_base_e time_base;
    3.     uint8_t is_running;
    4.     uint8_t expired;
    5.     uint16_t ticks_remaining;
    6.     uint16_t reset_value;
    7. } timer_s;
     
  2. Papabravo

    Expert

    Feb 24, 2006
    12,284
    2,723
    johndeaton likes this.
  3. johndeaton

    Thread Starter Member

    Sep 23, 2015
    63
    4
    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 (Text):
    1.  
    2. typedef struct {
    3.     timer_base_e time_base : 3;
    4.     uint8_t is_running : 1;
    5.     uint8_t expired : 1;
    6.     uint16_t ticks_remaining;
    7.     uint16_t reset_value;
    8. } timer_s ;
    9.  
    Not sure why this worked, but thought I'd share in case it helps someone else.

    Thanks
     
  4. Papabravo

    Expert

    Feb 24, 2006
    12,284
    2,723
    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.
     
    johndeaton likes this.
  5. johndeaton

    Thread Starter Member

    Sep 23, 2015
    63
    4
    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.
     
  6. Papabravo

    Expert

    Feb 24, 2006
    12,284
    2,723
    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.
     
    johndeaton likes this.
Loading...