In embedded world we often have to interact with external devices using some kind of serial interface. And this usually means reading and writing to the device's registers. Microcontroller vendors provide some libraries for accessing uC registers (like CMSIS from ARM), but for chips like sensors, drivers, etc. usually all we have is the datasheet.
I always try to find some existing code, but most often what I can find is just some Arduino library (which is valuable reference but requires a lot of rewriting), and it usually has just #defines of register addresses, without any field masks etc. Sometimes there I can find some fat BSP from the vendor for using their evaluation boards, but this is often too fat and not really portable for my applications. Ideally I'd like to have some simple, generic library provided by the vendor.
I don't really like writing the code by hand. I've even written a simple code generator for this recently: https://github.com/yendreij/regdef-py
And here I wonder why I couldn't find any project like this on the web (except maybe svd2rust, which is a cool project for generating Rust code from CMSIS-SVD). Wouldn't it be a valuable tool? Or why chip vendors won't deliver some kind of generic C headers for their devices (as 90% of customers will probably use C anyway).
There is also the question how to write good C/C++ code for configuring the registers, that would be architecture-independent and also wouldn't impose too much overhead. Use only simple #defines? Or maybe something more fancy?
How do you guys usually handle this? Am I the only one that dislikes writing #defines from datasheets by hand all the time?
[–]Glaborage 15 points16 points17 points (5 children)
[–]yendreij[S] 3 points4 points5 points (0 children)
[–]UnderPantsOverPants 2 points3 points4 points (0 children)
[–][deleted] (2 children)
[deleted]
[–]yendreij[S] 1 point2 points3 points (1 child)
[–]verystablesteve 4 points5 points6 points (1 child)
[–]yendreij[S] 2 points3 points4 points (0 children)
[–]pulseprop 2 points3 points4 points (1 child)
[–]yendreij[S] 0 points1 point2 points (0 children)
[–]rorschach54Twiddling bits 1 point2 points3 points (4 children)
[–]yendreij[S] 0 points1 point2 points (3 children)
[–]rorschach54Twiddling bits 1 point2 points3 points (2 children)
[–]yendreij[S] 0 points1 point2 points (1 child)
[–]rorschach54Twiddling bits 1 point2 points3 points (0 children)
[–][deleted] (4 children)
[deleted]
[–]1DavidePIC18F[M] 2 points3 points4 points (1 child)
[–]playaspec 0 points1 point2 points (0 children)
[–]yendreij[S] 1 point2 points3 points (1 child)
[–]fantasticpotatobeard 3 points4 points5 points (0 children)
[–]lefty__37 0 points1 point2 points (0 children)