This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

word access to bit fields

Other Parts Discussed in Thread: OMAPL138

Hello,

I am using a bit field structure to read/write a 16-bit device on the EMIF bus on an OMAPL138.  This device prohibits byte access to its registers. In other words, all reads and writes to the registers of this device must be 16-bit access on even addresses.

For reasons of code readability and maintenance, I wish to use I bit field struct to access these registers. An example of such a struct is as follows:

typedef struct S_RT_ADC_CONFIG
{
  unsigned short SOURCE        : 2; /* b0:b1 */
  unsigned short SCALE         : 2; /* b2:b3 */
  unsigned short ADC0_PRESENCE : 1; /* b4 */
  unsigned short ADC1_PRESENCE : 1; /* b5 */
  unsigned short ADC2_PRESENCE : 1; /* b6 */
  unsigned short ADC3_PRESENCE : 1; /* b7 */
  unsigned short BITS          : 2; /* b8:b9 */
  unsigned short EN            : 1; /* b10 */
  unsigned short UNUSED        : 5; /* b11:b15 */
} S_RT_ADC_CONFIG;

Once compiled, the assembly to access these fields result in 8-bit accesses to the hardware register. Is there anyway I can ensure that any and all accesses using this strucuture definition is done using 16-bit accesses only?

Thanks,

Matt



  • You must use EABI and you must mark the bit-fields volatile.

    typedef struct S_RT_ADC_CONFIG
    {
      volatile unsigned short SOURCE        : 2; /* b0:b1 */
    
  • Bit-field members of structures representing fields in device registers are risky for several reasons; Matt ran in to one, but there are others, including: field order could be either big- or little- endian; and: register may require that all bits be simultaneously correct, not built by ORing in the fields separately.  It's also debatable how much more "readable" it is compared to RT_ADC_CONFIG |= EN;  Some, if not all, of the TI DSP Chip Support Libraries provide macros for a non-bitfield approach for on-chip device register access, so if you're using the CSL anyway it might actually be less confusing to treat all device registers the same way.