-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement polymorphism for DigitalIn/Out/InOut #13209
Conversation
ce285f9
to
feea424
Compare
See application for this PR here: https://github.com/AGlass0fMilk/ep-oc-mcu/blob/refine-io-drivers/devices/MCP23008/MCP23008.hpp |
feea424
to
a332084
Compare
@AGlass0fMilk, thank you for your changes. |
This could have a noticeable performance impact for bitbangers. The Although on the other hand, I guess in most cases people accessing I've been involved in quite a bit of "devirtualisation" work - trying to minimise the number of uses of virtual in Mbed OS to get code size down. We haven't yet come up with a totally satisfactory pattern that gives you the best of both worlds - no polymorphism overhead when you have exactly one such type in a system, and pay for it only when you need it. The toolchains would ideally be able to figure that out themselves (LTO?) Maybe you could have a config option for virtualised drivers - either global or per-type? |
Definitely don't want to mess with that.
For software my company writes, I'm trying to make it standard practice to lean towards using references rather than direct objects. This allows drivers written in the past to accept abstracted objects in the future if ever necessary. In my specific case we have an external SPI-based chip and the CS pin is tied to a GPIO on an MCP23008 I/O expander. The driver I originally wrote for the SPI-based chip sometimes does multiple accesses during one function call so controlling the CS pin external to the driver code isn't possible. The best solution for me was to create a subclass of This is one of the benefits of using C++ in embedded. While I accept the increase in latency from the vtable redirection may be unacceptable in some cases, the flexibility provided by polymorphism in C++ is why I like using Mbed in the first place. It's one of the only embedded platforms pushing modern C++ onto micros. Therefore I don't think polymorphism should be completely removed from Mbed's base drivers.
I don't love the idea of yet another obscure configuration option but I don't see another path forward to satisfy both use cases simultaneously. Perhaps we can add a new macro to EDIT: my suggestion above ignores the possibility of enabling/disabling To avoid conflicting macro definitions we could: |
Basically my thoughts too. It would be something a bit more specific like I've been involved a bit too much in micro-optimisation recently, spending days to shave another few hundred bytes off an image, which is somewhat skewing my view :) And that makes me inclined to have per-class virtual options, rather than a single one. There's another significant image size problem in that neither GCC nor ARMC6 can eliminate unused virtual functions (ARMC5 and IAR can). Which means all virtual functions get included into an image. Big effect. |
More and more my problems end up in the compiler bin... |
@AGlass0fMilk I've re-classified this as a feature update as you would be introducing new callable functions... |
This PR cannot be merged due to conflicts. Please rebase to resolve them. |
@donatieng @bulislaw What shall we do with this PR ? Please review |
@AGlass0fMilk Please rebase your branch if you still intend it to be merged. |
I will rebase this week. |
This pull request has automatically been marked as stale because it has had no recent activity. @AGlass0fMilk, please carry out any necessary work to get the changes merged. Thank you for your contributions. |
a332084
to
83ece4a
Compare
Hi @hugueskamba, I have rebased and implemented the discussed configuration options for "virtualizing" each class. |
Another use case for this feature is linked to above: EmbeddedPlanet/ep-oc-mcu#42 @0xc0170 I'd like to know your thoughts on that PR above... It's a simple problem with an infinite number of solutions... |
@0xc0170 Please review |
@0xc0170 @kjbracey-arm I have created a small blinky example showing a simple use case for this polymorphism: https://github.com/EmbeddedPlanet/mbed-os-example-inverted-digitalout It compiles and works as expected on an NRF52840_DK. Let me know if there's anything else to get this pushed forward. |
ce29f89
to
1bc96c9
Compare
1bc96c9
to
78cfad9
Compare
This commit introduces documentation for the experimental driver polymorphism being added to Mbed OS. See related issue there: ARMmbed/mbed-os#13209
Reviewers, is this ready for integration ? The old approvals were cleared after the recent updates, is there anything outstanding? @AGlass0fMilk Thanks for the example and doc PR, I'll check them |
Please move this along! I put quite a bit of work into the documentation and PRs for this... |
CI started |
I'll restart CI, this should be ready once we release 6.9 today |
Jenkins CI Test : ✔️ SUCCESSBuild Number: 3 | 🔒 Jenkins CI Job | 🌐 Logs & ArtifactsCLICK for Detailed Summary
|
Let's get that merge! |
* Introduce section on driver polymorphism This commit introduces documentation for the experimental driver polymorphism being added to Mbed OS. See related issue there: ARMmbed/mbed-os#13209 * Add PlantUML sources for driver polymorphism diagrams * Apply suggestions from code review Co-authored-by: Vincent Coubard <[email protected]> Co-authored-by: George Beckstein <[email protected]> Co-authored-by: George Beckstein <[email protected]> Co-authored-by: Vincent Coubard <[email protected]>
* Introduce section on driver polymorphism This commit introduces documentation for the experimental driver polymorphism being added to Mbed OS. See related issue there: ARMmbed/mbed-os#13209 * Add PlantUML sources for driver polymorphism diagrams * Apply suggestions from code review Co-authored-by: Vincent Coubard <[email protected]> Co-authored-by: Vincent Coubard <[email protected]>
Summary of changes
In some IO-constrained applications, it is necessary to use a GPIO expander like the I2C-based MCP23008. To maintain compatibility with existing libraries that require a
DigitalIn/Out/InOut
handle, it would be beneficial to be able to subclass one of these classes and retarget theread/write, etc
calls. This would enable an MCP23008 driver to provideDigitalOut
-like objects that abstract away the intermediate I2C transfers. These objects can then be passed to older APIs usingDigitalOut
pointers.For this to work properly, some of the
DigitalIn/Out/InOut
class methods must be made virtual. This PR accomplishes this.I would encourage Mbed to consider "virtualizing" many of the other hardware drivers for similar use cases.
See #12387 for a similar discussion concerning retargetting Mbed's
CAN
API to an external SPI-based CAN controller/transceiver.Impact of changes
None. This should not affect current users of these APIs in any functional way. Code size may be slightly increased due to the addition of virtual tables and new functions.
Migration actions required
None
Documentation
None
Pull request type
Test results
Reviewers
@0xc0170 @ithinuel @bulislaw