Bit manipulations good practices

后端 未结 7 538
情书的邮戳
情书的邮戳 2021-02-03 20:28

As a beginner C programmer, I am wondering, what would be the best easy-to-read and easy-to-understand solution for setting control bits in a device. Are there any standards

相关标签:
7条回答
  • 2021-02-03 20:56

    You should make sure to initialize the bits to a known default value when you declare the variable to store their values. In C, when you declare a variable you are just reserving a block of memory at a address and the size of the block is based its type. If you don't initialize the variable you can encounter undefined / unexpected behavior since the value of the variable will be effected by whatever the value / state of the memory in that block was before you declared it. By initializing the variable to a default value, you are clearing this block of memory of its existing state and putting it in a know state.

    As far as readability, you should use a bit field to store the values of the bit. A bit field enables you to store the values of the bits in a struct. This makes it easier to organize since you can use dot notation. Also, you should make sure to comment the declaration of the bit field to explain what the different fields are used for as a best practice. I hope this answers your question. Good luck with you C programming!

    0 讨论(0)
  • 2021-02-03 20:57

    Other answers have already covered most of the stuff, but it might be worthwhile to mention that even if you can't use the non-standard 0b syntax, you can use shifts to move the 1 bit into position by bit number, i.e.:

    #define DMA_BYTE  (1U << 0)
    #define DMA_HW    (1U << 1)
    #define DMA_WORD  (1U << 2)
    #define DMA_GO    (1U << 3)
    // …
    

    Note how the last number matches the "bit number" column in the documentation.

    The usage for setting and clearing bits doesn't change:

    #define DMA_CONTROL_REG DMA_base_ptr[DMA_CONTROL_OFFS]
    
    DMA_CONTROL_REG |= DMA_HW | DMA_WORD;    // set HW and WORD
    DMA_CONTROL_REG &= ~(DMA_BYTE | DMA_GO); // clear BYTE and GO
    
    0 讨论(0)
  • 2021-02-03 20:57

    Modern C compilers handle trivial inline functions just fine – without overhead. I’d make all of the abstractions functions, so that the user doesn’t need to manipulate any bits or integers, and is unlikely to abuse the implementation details.

    You can of course use constants and not functions for implementation details, but the API should be functions. This also allows using macros instead of functions if you’re using an ancient compiler.

    For example:

    #include <stdbool.h>
    #include <stdint.h>
    
    typedef union DmaBase {
      volatile uint8_t u8[32];
    } DmaBase;
    static inline DmaBase *const dma1__base(void) { return (void*)0x12340000; }
    
    // instead of DMA_CONTROL_OFFS
    static inline volatile uint8_t *dma_CONTROL(DmaBase *base) { return &(base->u8[12]); }
    // instead of constants etc
    static inline uint8_t dma__BYTE(void) { return 0x01; }
    
    inline bool dma_BYTE(DmaBase *base) { return *dma_CONTROL(base) & dma__BYTE(); }
    inline void dma_set_BYTE(DmaBase *base, bool val) {
      if (val) *dma_CONTROL(base) |= dma__BYTE();
      else *dma_CONTROL(base) &= ~dma__BYTE();
    }
    inline bool dma1_BYTE(void) { return dma_BYTE(dma1__base()); }
    inline void dma1_set_BYTE(bool val) { dma_set_BYTE(dma1__base(), val); }
    

    Such code should be machine generated: I use gsl (of 0mq fame) to generate those based on a template and some XML input listing the details of the registers.

    0 讨论(0)
  • 2021-02-03 20:58

    The problem with bit fields is that the C standard does not dictate that the order in which they are defined is the same as the order that they are implemented. So you may not be setting the bits you think you are.

    Section 6.7.2.1p11 of the C standard states:

    An implementation may allocate any addressable storage unit large enough to hold a bit- field. If enough space remains, a bit-field that immediately follows another bit-field in a structure shall be packed into adjacent bits of the same unit. If insufficient space remains, whether a bit-field that does not fit is put into the next unit or overlaps adjacent units is implementation-defined. The order of allocation of bit-fields within a unit (high-order to low-order or low-order to high-order) is implementation-defined. The alignment of the addressable storage unit is unspecified.

    As an example, look at the definition of struct iphdr, which represents an IP header, from the /usr/include/netinet/ip.h file file on Linux:

    struct iphdr
      {
    #if __BYTE_ORDER == __LITTLE_ENDIAN
        unsigned int ihl:4;
        unsigned int version:4;
    #elif __BYTE_ORDER == __BIG_ENDIAN
        unsigned int version:4;
        unsigned int ihl:4;
    #else
    # error "Please fix <bits/endian.h>"
    #endif
        u_int8_t tos;
        ...
    

    You can see here that the bitfields are placed in a different order depending on the implementation. You also shouldn't use this specific check because this behavior is system dependent. It is acceptable for this file because it is part of the system. Other systems may implement this in different ways.

    So don't use a bitfield.

    The best way to do this is to set the required bits. However, it would make sense to define named constants for each bit and to perform a bitwise OR of the constants you want to set. For example:

    const uint8_t BIT_BYTE =     0x1;
    const uint8_t BIT_HW   =     0x2;
    const uint8_t BIT_WORD =     0x4;
    const uint8_t BIT_GO   =     0x8;
    const uint8_t BIT_I_EN =     0x10;
    const uint8_t BIT_REEN =     0x20;
    const uint8_t BIT_WEEN =     0x40;
    const uint8_t BIT_LEEN =     0x80;
    
    DMA_base_ptr[DMA_CONTROL_OFFS] = BIT_LEEN | BIT_GO | BIT_WORD;
    
    0 讨论(0)
  • 2021-02-03 21:08

    The old-school C way is to define a bunch of bits:

    #define WORD  0x04
    #define GO    0x08
    #define I_EN  0x10
    #define LEEN  0x80
    

    Then your initialization becomes

    DMA_base_ptr[DMA_CONTROL_OFFS] = WORD | GO | LEEN;
    

    You can set individual bits using |:

    DMA_base_ptr[DMA_CONTROL_OFFS] |= I_EN;
    

    You can clear individual bits using & and ~:

    DMA_base_ptr[DMA_CONTROL_OFFS] &= ~GO;
    

    You can test individual bits using &:

    if(DMA_base_ptr[DMA_CONTROL_OFFS] & WORD) ...
    

    Definitely don't use bitfields, though. They have their uses, but not when an external specification defines that the bits are in certain places, as I assume is the case here.

    See also questions 20.7 and 2.26 in the C FAQ list.

    0 讨论(0)
  • 2021-02-03 21:08

    You could use bit-fields, despite what all the fear-mongers here have been saying. You would just need to know how the compiler(s) and system ABI(s) you intend your code to work with define the "implementation defined" aspects of bit-fields. Don't be scared off by pedants putting words like "implementation defined" in bold.

    However what others so far seem to have missed out on are the various aspects of how memory-mapped hardware devices might behave that can be counter-intuitive when dealing with a higher-level language like C and the optimization features such languages offer. For example every read or write of a hardware register may have side-effects sometimes even if bits are not changed on the write. Meanwhile the optimizer may make it difficult to tell when the generated code is actually reading or writing to the address of the register, and even when the C object describing the register is carefully qualified as volatile, great care is required to control when I/O occurs.

    Perhaps you will need to use some specific technique defined by your compiler and system in order to properly manipulate memory-mapped hardware devices. This is the case for many embedded systems. In some cases compiler and system vendors will indeed use bit-fields, just as Linux does in some cases. I would suggest reading your compiler manual first.

    The bit description table you quote appears to be for the control register of the the Intel Avalon DMA controller core. The "read/write/clear" column gives a hint as to how a particular bit behaves when it is read or written. The status register for that device has an example of a bit where writing a zero will clear a bit value, but it may not read back the same value as was written -- i.e. writing the register may have a side-effect in the device, depending on the value of the DONE bit. Interestingly they document the SOFTWARERESET bit as "RW", but then describe the procedure as writing a 1 to it twice to trigger the reset, and then they also warn Executing a DMA software reset when a DMA transfer is active may result in permanent bus lockup (until the next system reset). The SOFTWARERESET bit should therefore not be written except as a last resort. Managing a reset in C would take some careful coding no matter how you describe the register.

    As for standards, well ISO/IEC have produced a "technical report" known as "ISO/IEC TR 18037", with the subtitle "Extensions to support embedded processors". It discusses a number of the issues related to using C to manage hardware addressing and device I/O, and specifically for the kinds of bit-mapped registers you mention in your question it documents a number of macros and techniques available through an include file they call <iohw.h>. If your compiler provides such a header file, then you might be able to use these macros.

    There are draft copies of TR 18037 available, the latest being TR 18037(2007), though it provides for rather dry reading. However it does contain an example implementation of <iohw.h>.

    Perhaps a good example of a real-world <iohw.h> implementation is in QNX. The QNX documentation offers a decent overview (and an example, though I would strongly suggest using enums for integer values, never macros): QNX <iohw.h>

    0 讨论(0)
提交回复
热议问题