Create a new Bitfield class, correctly assigning the anonymous
    fields from the Union in order to get the desired behavior.
    
    Parameters::
    
        name
            The name of the class.  This is similar to the namedtuple
            recipe, in that you'll generally pass the same name here as
            a string that you do in defining the class itself.
            
        fields
            A list of fields.  Fields are in order from the LSB of the
            underlying register, and must be 3-tuples of the form::
            
                ('fieldname', fieldtype, bits),
                ('such_as', c_uint, 5),
                ('this', c_int, 3)
                
            fieldtypes should be either c_uint, c_int, or c_bool.  make_bf
            takes care of any sizing requirements appropriately.
                
        basetype
            The type of the underlying register.  This should usually be an
            explicit size variant of c_uint, such as a c_uint32.
            
        doc
            The optional docstring for the newly created class.

def make_bf(name, fields, basetype=c_uint32, doc=None):
    """
    Create a new Bitfield class, correctly assigning the anonymous
    fields from the Union in order to get the desired behavior.
    
    Parameters::
    
        name
            The name of the class.  This is similar to the namedtuple
            recipe, in that you'll generally pass the same name here as
            a string that you do in defining the class itself.
            
        fields
            A list of fields.  Fields are in order from the LSB of the
            underlying register, and must be 3-tuples of the form::
            
                ('fieldname', fieldtype, bits),
                ('such_as', c_uint, 5),
                ('this', c_int, 3)
                
            fieldtypes should be either c_uint, c_int, or c_bool.  make_bf
            takes care of any sizing requirements appropriately.
                
        basetype
            The type of the underlying register.  This should usually be an
            explicit size variant of c_uint, such as a c_uint32.
            
        doc
            The optional docstring for the newly created class.
    
    """
    # We need to hack on the fields array to get our integer sizes correct.
    unsigned_types = [c_uint8, c_uint16, c_uint32, c_uint64]
    signed_types = [c_int8, c_int16, c_int32, c_int64]
    
    unsigned    = next(t for t in unsigned_types if sizeof(t) >= sizeof(basetype))
    signed      = next(t for t in signed_types if sizeof(t) >= sizeof(basetype))
    
    def fixed_type(t):
        if t in unsigned_types:
            return unsigned
        elif t in signed_types:
            return signed
        elif t is c_bool:
            return unsigned
        else:
            try:
                raise TypeError("Field of type {0} not allowed, only integer and boolean types.".format(t.__name__))
            except AttributeError:
                raise TypeError("{0!r} not a class.".format(t))
    
    fields = [ (name, fixed_type(cls), size) for (name, cls, size) in fields ]

    # Define the underlying bitfield type
    bitfield = type(name + '_bitfield', (LittleEndianStructure, ), {})
    bitfield._fields_ = fields
    bitfield._pack_ = 1
    
    # Set up the docstring
    if doc is None:
        doc = _docstring_template.format(
            name = name,
            base = basetype.__name__
        )
        namewidth = max(len(t[0]) for t in fields)
        doc += ''.join(
            _docstring_field_template.format(
                name = t[0],
                nw = namewidth,
                base = t[1].__name__,
                n = t[2]
            ) for t in fields
        )

    # Set up the union
    d = {
        '_fields_' : [('base', basetype), ('_b', bitfield)],
        '__doc__' : doc
    }
    return type(name, (Union, Bitfield), d)