0

This one is going to be short. I have a question regarding manipulations on device registers. As maybe you guys know, device registers addresses are determined by hardware so in order to write code that uses them you have to create variables with these addresses defined, you cannot let the compiler decide on the addresses. This can be done in a few ways, with pointers and references, like this:

using dev_reg = uint32_t volatile;
dev_reg& MYREG = *(reinterpret_cast<dev_reg*>(0x45FF));

So while you can do something like this I am not sure why wouldn't a programming language such as C++ offer some package of features to make life easier for access to such registers, especially as I heard that volatile is slowly being removed.

Clifford
  • 88,407
  • 13
  • 85
  • 165
  • 2
    `volatile` isn't going away, and it's just use-cases like this that makes it impossible to remove it. – Some programmer dude Apr 24 '22 at 10:19
  • I do not understand. `why wouldn't a programming language such as C++ offer some package of features to make life easier` "easier" as compared to? C++ does - C++ offers `volatile`, it's as easy as it can get. – KamilCuk Apr 24 '22 at 17:19
  • Compilers don't "decide" on addresses in any case - that is the job of the linker. `volatile` is not being "removed" (slowly or otherwise). Some uses of `volatile` are being deprecated. That does not include this use case: https://stackoverflow.com/questions/59223814/why-is-volatile-deprecated-in-c20 – Clifford Apr 24 '22 at 17:59
  • Note that compilers do provide extensions for this: for example: https://www.iar.com/knowledge/support/technical-notes/compiler/linker-error-for-absolute-located-variable/ (IAR) and https://stackoverflow.com/questions/4067811/how-to-place-a-variable-at-a-given-absolute-address-in-memory-with-gcc (comparing armcc/RealView with gnu). The gnu solution is particularly cumbersome. None of these solutions are necessary for this use case because the I/O register space is not visible to linker in any case, so do not need to be protected from allocation by the linker. – Clifford Apr 24 '22 at 18:17

1 Answers1

0

Volatility of memory mapped IO is out of question, and one of the very legit and mandatory use cases of keyword volatile. The genius design decisions on early days of C++ allow us to use placement new to initialize them correctly, and explicit destructor call to finalize them. These objects are also practical examples of unavoidable singleton which is normaly considered anti-pattern elsewhere.

Thanks Žarko for his point on singleton.

Red.Wave
  • 2,790
  • 11
  • 17
  • Singleton is anti pattern but that doesnt mean that it doesnt have its uses. – Žarko Tomičić Apr 24 '22 at 15:30
  • The term "often" should `catch` that exception – Red.Wave Apr 24 '22 at 15:40
  • I would just like to point out that my main question was not about volatile but abou support for embedded programming. Because it seems that there is none except what you cook up with stuff C or C++ offer... – Žarko Tomičić Apr 24 '22 at 17:14
  • embedded programming does not define anstractions suitable for the task. Most embedded providers are electronics engineers with some level of C knowledge. While C++ can benefit embedded programming the most, mainstream doesn't step up to ask for features. I wish there was strong community for embedded C++. Software engineers are frightened of C++ by bad advice or old none-familiar university instructor s. The number of C++ progrmmers is even less among electronics engineers. – Red.Wave Apr 24 '22 at 17:25
  • @ŽarkoTomičić It's not an "anti pattern" - on embedded systems it is common that you only have one single hardware resource, such as a hardware peripheral. If you aren't treating it as a singleton in your code, you are doing it wrong, simple as that. Since no amount of software magic is going to create more hardware resources. Generally, when insisting on using C++ in embedded systems, it's important to leave various PC programmer religious arguments behind. I find the various "design patterns" unhelpful overall, since most of them were created with PC programming in mind. – Lundin Apr 25 '22 at 08:33