From 71022210f989046e543773154eef40ba43f76b1f Mon Sep 17 00:00:00 2001 From: Christopher Durand Date: Sat, 5 Feb 2022 03:46:33 +0100 Subject: [PATCH 1/4] [architecture] Require SPI device drivers to specify SPI configuration The SPI configuration is specified as a non-type template parameter to `modm::SpiDevice`. `SpiDevice` will provide a `configureBus()` function to apply the required mode settings for the device. --- src/modm/architecture/interface/spi.hpp | 9 +++++++++ src/modm/architecture/interface/spi_device.hpp | 8 +++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/modm/architecture/interface/spi.hpp b/src/modm/architecture/interface/spi.hpp index 80e2dc85fe..90885ad1d2 100644 --- a/src/modm/architecture/interface/spi.hpp +++ b/src/modm/architecture/interface/spi.hpp @@ -15,6 +15,7 @@ #ifndef MODM_INTERFACE_SPI_HPP #define MODM_INTERFACE_SPI_HPP +#include #include namespace modm @@ -45,6 +46,14 @@ struct Spi }; }; +struct SpiConfiguration +{ + Spi::DataMode dataMode{}; + Spi::DataOrder dataOrder{}; + + constexpr auto operator<=>(const SpiConfiguration&) const = default; +}; + } // namespace modm #endif // MODM_INTERFACE_SPI_HPP diff --git a/src/modm/architecture/interface/spi_device.hpp b/src/modm/architecture/interface/spi_device.hpp index 89ac49b7b4..9d6ec97204 100644 --- a/src/modm/architecture/interface/spi_device.hpp +++ b/src/modm/architecture/interface/spi_device.hpp @@ -27,7 +27,7 @@ namespace modm * @author Niklas Hauser * @ingroup modm_architecture_spi_device */ -template < class SpiMaster > +template < class SpiMaster, SpiConfiguration config > class SpiDevice { Spi::ConfigurationHandler configuration; @@ -44,6 +44,12 @@ class SpiDevice configuration = handler; } + void inline + configureBus() + { + SpiMaster::setDataMode(config.dataMode); + SpiMaster::setDataOrder(config.dataOrder); + } protected: bool inline acquireMaster() From d9cdd1aadc7387c8635fa1694330ef4edd0b3a0e Mon Sep 17 00:00:00 2001 From: Christopher Durand Date: Sat, 5 Feb 2022 03:58:18 +0100 Subject: [PATCH 2/4] WIP: try out SPI mode on ads816x driver --- src/modm/driver/adc/ads816x.hpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/modm/driver/adc/ads816x.hpp b/src/modm/driver/adc/ads816x.hpp index faaf64ce94..0140b3f2b3 100644 --- a/src/modm/driver/adc/ads816x.hpp +++ b/src/modm/driver/adc/ads816x.hpp @@ -85,6 +85,8 @@ struct ads816x ResultChannelStatus = (0b10 << 4), //Reserved = (0b11 << 4), }; + + static constexpr SpiConfiguration spiConfig{Spi::DataMode::Mode0, Spi::DataOrder::MsbFirst}; }; /** @@ -95,7 +97,7 @@ struct ads816x * @ingroup modm_driver_ads816x */ template -class Ads816x : public ads816x, public modm::SpiDevice, protected modm::NestedResumable<3> +class Ads816x : public ads816x, public modm::SpiDevice, protected modm::NestedResumable<3> { public: Ads816x() = default; From 52206dde845e0ba01b12006b646cda17b6d70a1e Mon Sep 17 00:00:00 2001 From: Christopher Durand Date: Sat, 5 Feb 2022 04:18:54 +0100 Subject: [PATCH 3/4] fixup --- examples/nucleo_f429zi/adc_ads816x/main.cpp | 1 + src/modm/architecture/interface/spi.hpp | 5 +++-- src/modm/driver/adc/ads816x.hpp | 9 ++++++--- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/examples/nucleo_f429zi/adc_ads816x/main.cpp b/examples/nucleo_f429zi/adc_ads816x/main.cpp index a338477689..0d231e3d40 100644 --- a/examples/nucleo_f429zi/adc_ads816x/main.cpp +++ b/examples/nucleo_f429zi/adc_ads816x/main.cpp @@ -36,6 +36,7 @@ main() SpiMaster::initialize(); SpiMaster::connect(); + adc.configureBus(); Rst::setOutput(); Cs::setOutput(true); diff --git a/src/modm/architecture/interface/spi.hpp b/src/modm/architecture/interface/spi.hpp index 90885ad1d2..17978cb2d3 100644 --- a/src/modm/architecture/interface/spi.hpp +++ b/src/modm/architecture/interface/spi.hpp @@ -46,10 +46,11 @@ struct Spi }; }; +template struct SpiConfiguration { - Spi::DataMode dataMode{}; - Spi::DataOrder dataOrder{}; + SpiMaster::DataMode dataMode{}; + SpiMaster::DataOrder dataOrder{}; constexpr auto operator<=>(const SpiConfiguration&) const = default; }; diff --git a/src/modm/driver/adc/ads816x.hpp b/src/modm/driver/adc/ads816x.hpp index 0140b3f2b3..a9ccc74992 100644 --- a/src/modm/driver/adc/ads816x.hpp +++ b/src/modm/driver/adc/ads816x.hpp @@ -85,10 +85,11 @@ struct ads816x ResultChannelStatus = (0b10 << 4), //Reserved = (0b11 << 4), }; - - static constexpr SpiConfiguration spiConfig{Spi::DataMode::Mode0, Spi::DataOrder::MsbFirst}; }; +template +constexpr auto Ads816xSpiConfig = SpiConfiguration{SpiMaster::DataMode::Mode0, SpiMaster::DataOrder::MsbFirst}; + /** * @tparam SpiMaster SpiMaster interface * @tparam Cs Chip-select pin @@ -97,7 +98,9 @@ struct ads816x * @ingroup modm_driver_ads816x */ template -class Ads816x : public ads816x, public modm::SpiDevice, protected modm::NestedResumable<3> +class Ads816x : public ads816x, + public modm::SpiDevice>, + protected modm::NestedResumable<3> { public: Ads816x() = default; From 994f60dc35dd668087731c52074e841b05b46244 Mon Sep 17 00:00:00 2001 From: Christopher Durand Date: Sat, 5 Feb 2022 04:32:11 +0100 Subject: [PATCH 4/4] fixup --- src/modm/architecture/interface/spi_device.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modm/architecture/interface/spi_device.hpp b/src/modm/architecture/interface/spi_device.hpp index 9d6ec97204..a5fd910125 100644 --- a/src/modm/architecture/interface/spi_device.hpp +++ b/src/modm/architecture/interface/spi_device.hpp @@ -27,7 +27,7 @@ namespace modm * @author Niklas Hauser * @ingroup modm_architecture_spi_device */ -template < class SpiMaster, SpiConfiguration config > +template < class SpiMaster, SpiConfiguration config > class SpiDevice { Spi::ConfigurationHandler configuration;