2016-03-08 01:21 PM
Welcome everybody,
I have made some development on the CMSIS device descriptors: - The bit fields are added for each register mapping C structure, so the bit fields appear as simple variables that can be read and written in the C code (the compiler will insert the necessary masking, shifting, etc. operations). An example of how it looks like:typedef
struct
{ __IOuint32_t
DR; __IOuint8_t
IDR;uint8_t
__RESERVED0;uint16_t
__RESERVED1;union
{struct
{ __IOuint32_t
RESET :1
;uint32_t
__RESERVED0 :2
; __IOuint32_t
POLYSIZE :2
; __IOuint32_t
REV_IN :2
; __IOuint32_t
REV_OUT :1
;uint32_t
__RESERVED1 :24
; } b; __IOuint32_t
w; } CR;uint32_t
__RESERVED2; __IOuint32_t
INIT; __IOuint32_t
POL; } CRC_TypeDef; For example, the user code can read or write theCRC->CR.b.POLYSIZE
field just like any other variable, which makes developing and understanding code much easier. - The other advancement is to create the bit-band alias equivalents for the peripheral structures. This enables accessing single bits with one load or store operation. Let me demonstrate:typedef
struct
{ __IOuint32_t
DR[32
]; __IOuint32_t
IDR[8
];uint32_t
__RESERVED0[8
];uint32_t
__RESERVED1[16
];struct
{ __IOuint32_t
RESET;uint32_t
__RESERVED0[2
]; __IOuint32_t
POLYSIZE[2
]; __IOuint32_t
REV_IN[2
]; __IOuint32_t
REV_OUT;uint32_t
__RESERVED1[24
]; } CR;uint32_t
__RESERVED2[32
]; __IOuint32_t
INIT[32
]; __IOuint32_t
POL[32
]; } CRC_BitBand_TypeDef; ♯define
CRC_BB
((CRC_BitBand_TypeDef *) PERIPH_BB(CRC_BASE))The PERIPH_BB macro converts the peripheral base address to the peripheral alias address. From there, the bit offset is provided by the nature of the C structure (every bit is word-addressed in the alias region). For example, to set the RESET bit of the CRC, one can write CRC_BB->CR.RESET = 1; and this will create a single store CPU operation in the binary. This has only been a short introduction, I have created a GitHub repository where you can find a more detailed description with source code and example code as well: http://github.com/IntergatedCircuits/STM32_XPD There is a readme for the whole project, and a separate bit-banding explainer page. The STM32 device headers are created by a code parser and generator, so I cannot guarantee their correctness (only for the parts that are used by the XPD driver code). Keep in mind that this is an experimental (hobby) project at the moment. Further device headers may be published if requested. If you have any questions or suggestions I'm happy to be of service. Best regards, Ben #bit-banding #stm32 #bit-handling #cmsis2016-03-09 12:58 AM
Using bitfields (this isn't bitband) is not always save.
Headers files with bitfields can be generated by tool aattached to CMSIS