Skip to content
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

[architecture] Require setting SPI config in SpiDevice based drivers #820

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/nucleo_f429zi/adc_ads816x/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ main()

SpiMaster::initialize<Board::SystemClock, 10_MHz>();
SpiMaster::connect<Sck::Sck, Mosi::Mosi, Miso::Miso>();
adc.configureBus();

Rst::setOutput();
Cs::setOutput(true);
Expand Down
10 changes: 10 additions & 0 deletions src/modm/architecture/interface/spi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#ifndef MODM_INTERFACE_SPI_HPP
#define MODM_INTERFACE_SPI_HPP

#include <compare>
#include <modm/architecture/interface/peripheral.hpp>

namespace modm
Expand Down Expand Up @@ -45,6 +46,15 @@ struct Spi
};
};

template <typename SpiMaster>
struct SpiConfiguration
{
SpiMaster::DataMode dataMode{};
SpiMaster::DataOrder dataOrder{};

constexpr auto operator<=>(const SpiConfiguration&) const = default;
};

} // namespace modm

#endif // MODM_INTERFACE_SPI_HPP
8 changes: 7 additions & 1 deletion src/modm/architecture/interface/spi_device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace modm
* @author Niklas Hauser
* @ingroup modm_architecture_spi_device
*/
template < class SpiMaster >
template < class SpiMaster, SpiConfiguration<SpiMaster> config >
class SpiDevice
{
Spi::ConfigurationHandler configuration;
Expand All @@ -44,6 +44,12 @@ class SpiDevice
configuration = handler;
}

void inline
configureBus()
{
SpiMaster::setDataMode(config.dataMode);
SpiMaster::setDataOrder(config.dataOrder);
}
protected:
bool inline
acquireMaster()
Expand Down
7 changes: 6 additions & 1 deletion src/modm/driver/adc/ads816x.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ struct ads816x
};
};

template<typename SpiMaster>
constexpr auto Ads816xSpiConfig = SpiConfiguration<SpiMaster>{SpiMaster::DataMode::Mode0, SpiMaster::DataOrder::MsbFirst};

/**
* @tparam SpiMaster SpiMaster interface
* @tparam Cs Chip-select pin
Expand All @@ -95,7 +98,9 @@ struct ads816x
* @ingroup modm_driver_ads816x
*/
template <typename SpiMaster, typename Cs>
class Ads816x : public ads816x, public modm::SpiDevice<SpiMaster>, protected modm::NestedResumable<3>
class Ads816x : public ads816x,
public modm::SpiDevice<SpiMaster, Ads816xSpiConfig<SpiMaster>>,
protected modm::NestedResumable<3>
{
public:
Ads816x() = default;
Expand Down