Changes between Version 1 and Version 2 of howto/bitfields_in_microblaze


Ignore:
Timestamp:
Feb 17, 2015, 5:18:26 PM (9 years ago)
Author:
chunter
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • howto/bitfields_in_microblaze

    v1 v2  
    11= Using Bit-Fields in a MicroBlaze C Project =
    22
    3 We use a very useful C programming tool in the [wiki:802.11 802.11 Reference Design] known as [http://en.wikipedia.org/wiki/Bit_field a Bit-Field]. Bits are not individually addressable in C -- when unaligned access is enabled in a processor, the minimum addressable unit of data is a byte. Bit-fields provide a useful way of interpreting the bits that make up large type definitions. This document is a tutorial on how to use bit-fields in MicroBlaze C projects.
     3We use a very useful C programming tool in the [wiki:802.11 802.11 Reference Design] known as [http://en.wikipedia.org/wiki/Bit_field a Bit-Field]. Bits are not individually addressable in C. When unaligned access is enabled in a processor, the minimum addressable unit of data is a byte. Bit-fields provide a useful way of interpreting the bits that make up larger type definitions. They can help abstract away the low level bit shifts and bit masks needed for bit manipulation. This document is a tutorial on how to use bit-fields in MicroBlaze C projects.
    44
     5A typical situation where you might see a bit-field is a definition like the following:
     6
     7{{{
     8#!div style="font-size: 90%"
     9  {{{#!C
     10/*
     11 *      This is an example of a simple 1-byte bitfield with three members: A, B, and C
     12 *
     13 *      Bit mask:
     14 *      MSB     _ _ _ _ _ _ _ _ LSB
     15 *      C|-----B-----|A
     16 *
     17 *      A, C are 1-bit flags.
     18 *      B is a 6-bit integer.
     19 *
     20 */
     21
     22typedef union{
     23        u8 raw_value;
     24        struct __attribute__ ((__packed__)){
     25                unsigned A                      :1;  //b[0]
     26                unsigned B                      :6;  //b[6:1]
     27                unsigned C                      :1;  //b[7]
     28        };
     29} bitfield_example_type;
     30  }}}
     31}}}
     32
     33The above type definition defines a bit-field named {{{bitfield_example_type}}}. There are four important features to notice in the above syntax:
     34
     35 * A [http://en.wikipedia.org/wiki/Union_type union] is used between to designate that {{{raw_value}}} and the proceeding struct occupy the same space.
     36 * The {{{__attribute__ ((__packed__))}}}
     37 * By convention, {{{raw_value}}} is the same size as the proceeding struct (a single byte). If the code does not need to interpret the individual bits in the bit-field, it can instead access the fully byte itself by using {{{raw_value}}}.
     38 * In the struct definition, the {{{ :X }}} notation is used to tell the compiler that the element is {{{X}}} bits wide.
    539
    640