| 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 |
| 45 | void 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 | |
| 58 | One 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 | |
| 72 | The 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 = |