Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
You NEVER use C bitfields for peripheral register access because the C standard doesn't specify in which order the bits are implemented.
-
@7400 and that's awful legacy code - tied to a single compiler and even compiler version without any good reason.
-
740027806y@Fast-Nop: Yes, I like to live dangerously. Bit ordering in bitfield is not that likely to change between compiler versions, though (especially since the architecture is fixed and it's a single byte every time), and it would undeniably have been nice to use in code. Microchip even uses bitfields in the “official” headers for PICs, I think.
But, yeah, it still was a nice exercise in writing C macros. -
740027806yI'm tempted to extend the macros a bit and use them for my ARM example projects. Right now they use ST's headers with their not open source compatible licence. Some also use a fair amount of GCC-specific attributes. If the bit fields aren't used, the usage would be completely compatible. Comments?
-
@7400 ST's code sucks anyway; I only use it for getting a quick overview how something should work and then implement it myself.
But if you are into macros, why don't you drop the bitfields and make set/reset bit macros using bitmasks?
Related Rants
I felt like being the cause for “that dreaded legacy code“ and wrote 250 lines of C preprocessor macros for generating bitfields in a large header file automatically, with the goal of simplifying and clarifying register access for all peripherals in the end. Then, I found out that SDCC's optimisation for bitfields is absolutely awful (if existent at all), and I don't really want to use these abstractions if they have a performance impact.
Did I deserve that?
rant
🔌
sdcc
c
embedded