Changes between Version 2 and Version 3 of howto/bitfields_in_microblaze


Ignore:
Timestamp:
Feb 18, 2015, 9:54:26 AM (9 years ago)
Author:
chunter
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • howto/bitfields_in_microblaze

    v2 v3  
    3434
    3535 * 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__))}}}
     36 * The {{{__attribute__ ((__packed__))}}} struct attribute informs the compiler that that each member of the structure should be placed such that the memory required is minimized. This minimizes the chances that the bit-field will be automatically padded to an unexpected length when it is placed into a larger structure with other elements.
    3737 * 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}}}.
    3838 * In the struct definition, the {{{ :X }}} notation is used to tell the compiler that the element is {{{X}}} bits wide.
    3939
     40{{{bitfield_example_type}}} defines three bit-fields: A, B, and C. B is a 6-bit integer that is surrounding by single-bit values A and C. Let's first define a function that will print this bit-field for us. This function will print the entire byte in hexadecimal and will then print the fields A, B, and C as decimal values. We'll use this function in the coming examples:
     41
     42{{{
     43#!div style="font-size: 90%"
     44  {{{#!C
     45void print_bitfield_example_type(bitfield_example_type my_bitfield){
     46        xil_printf("bitfield_example_type Contents:\n");
     47        xil_printf(".raw_value      = 0x%02x\n", my_bitfield.raw_value);
     48        xil_printf(".A              = %d\n", my_bitfield.A);
     49        xil_printf(".B              = %d\n", my_bitfield.B);
     50        xil_printf(".C              = %d\n", my_bitfield.C);
     51}
     52  }}}
     53}}}
     54
     55
     56== Case 1: Manually Initializing a Bit-field ==
     57
     58One way to create and initialize a bit-field is to manually declare a local variable and assign values to the bit-field's elements.
     59
     60{{{
     61#!div style="font-size: 90%"
     62  {{{#!C
     63        bitfield_example_type my_bitfield;
     64        my_bitfield.raw_value = 0; //clear out the entire bitfield
     65        my_bitfield.A              = 1;
     66        my_bitfield.B              = 60;
     67        my_bitfield.C              = 0;
     68        print_bitfield_example_type(my_bitfield);
     69  }}}
     70}}}
     71
     72The above code creates a {{{bitfield_example_type}}} named {{{my_bitfield}}} and sets the A, B, and C elements to 1, 60, and 0 respectively. It then calls the previously-defined print function to verify the contents of the bit-field. The output of that print function is the following:
     73
     74{{{
     75#!div style="font-size: 90%"
     76
     77
     78}}}
     79
     80
     81== Case 2: Using a Constructor Function to Initialize a Bit-field ==
     82
     83
     84----
     85= Example: Interpretation of Byte Array =
    4086
    4187= Example: Register Access =
    42 
    43 = Example: Interpretation of Byte Array =