i2c.h
Go to the documentation of this file.
#ifndef _utl_com_i2c_h__
#define _utl_com_i2c_h__
#include <utl/core/impl.h>
#include <utl/core/crtp.h>
#include <utl/meta/meta.h>
namespace utl {
template <typename impl_t>
class i2c_i {
_CRTP_IMPL(impl_t);
public:
using type = i2c_i<impl_t>;
enum class Sequence {
BYTE =0,
ACK,
BYTEnACK
};
protected:
i2c_i () = default;
~i2c_i () = default;
i2c_i (const type&) = delete;
type& operator= (const type&) = delete;
private:
uint32_t _clock () const { return impl()._clock (); }
void _clock (uint32_t c) { impl()._clock (c); }
void _start() { impl()._start (); }
void _stop() { impl()._stop (); }
byte_t _rx_data (bool ack, Sequence seq) { return impl()._rx_data (ack, seq); }
bool _tx_data (byte_t byte, Sequence seq) { return impl()._tx_data (byte, seq); }
public:
uint32_t clock () const { return _clock (); }
void clock (uint32_t f) { _clock (f); }
public:
void start() { _start (); }
void stop () { _stop (); }
byte_t rx_data (bool ack, Sequence seq =Sequence::BYTEnACK) {
return _rx_data (ack, seq);
}
bool tx_data (byte_t byte, Sequence seq =Sequence::BYTEnACK) {
return _tx_data (byte, seq);
}
};
template<>
class i2c_i <virtual_tag> {
public:
using type = i2c_i <virtual_tag>;
enum class Sequence {
BYTE=0,
ACK,
BYTEnACK
};
protected:
i2c_i () = default;
i2c_i (const type&) = delete;
type& operator= (const type&) = delete;
public:
virtual ~i2c_i () = default;
private:
virtual uint32_t _clock () const = 0;
virtual void _clock (uint32_t) = 0;
virtual void _start() = 0;
virtual void _stop() = 0;
virtual byte_t _rx_data (bool ack, Sequence seq) = 0;
virtual bool _tx_data (byte_t byte, Sequence seq) = 0;
public:
uint32_t clock () const { return _clock(); }
void clock (uint32_t c) { _clock(c); }
public:
void start () { _start(); }
void stop () { _stop(); }
byte_t rx_data (bool ack, Sequence seq =Sequence::BYTEnACK) {
return _rx_data (ack, seq);
}
bool tx_data (byte_t byte, Sequence seq =Sequence::BYTEnACK) {
return _tx_data (byte, seq);
}
};
#if defined _utl_have_concepts
template <typename T>
concept bool I2c_i = requires (T t, const T ct, typename T::Sequence s) {
// Object type
requires not_<std::is_copy_constructible<T>::value>::value;
requires not_<std::is_copy_assignable<T>::value>::value;
// Methods
{ct.clock()} -> uint32_t;
{t.clock(0)} -> void;
{t.start()} -> void;
{t.stop()} -> void;
{t.rx_data (1, s)} -> byte_t;
{t.tx_data (0, s)} -> bool;
};
#else
namespace i2c_i_details {
using std::declval;
template <class _Tp> using try_cclk_t = decltype (declval<const _Tp>().clock());
template <class _Tp> using try_clk_t = decltype (declval<_Tp>().clock(declval<uint32_t>()));
template <class _Tp> using try_start_t = decltype (declval<_Tp>().start());
template <class _Tp> using try_stop_t = decltype (declval<_Tp>().stop());
template <class _Tp> using try_rx_data_t
= decltype (declval<_Tp>().rx_data (declval<bool>(), declval<typename _Tp::Sequence>()));
template <class _Tp> using try_tx_data_t
= decltype (declval<_Tp>().tx_data (declval<byte_t>(), declval<typename _Tp::Sequence>()));
template <typename _Tp, typename =void>
struct is_i2c_ : meta::false_ { };
template <typename _Tp>
struct is_i2c_ <_Tp,
meta::void_t <
typename _Tp::Sequence,
// meta::use_if_same_t <uint32_t,try_cclk_t <_Tp>>,
// meta::use_if_same_t <void, try_clk_t <_Tp>>,
// meta::use_if_same_t <void, try_start_t <_Tp>>,
// meta::use_if_same_t <void, try_stop_t <_Tp>>,
// meta::use_if_same_t <byte_t, try_rx_data_t <_Tp>>,
// meta::use_if_same_t <bool, try_tx_data_t <_Tp>>
void
>
> : meta::true_ { };
}
// template <typename _Tp>
// constexpr bool I2c_i = i2c_i_details::is_i2c_<_Tp>::value;
#endif
} // namespace utl
#endif // _utl_com_i2c_h__
meta.h
Include all meta library.
utl::meta::void_t
void void_t
void_t type alias
Definition: detection.h:55
utl::i2c_i_details::is_i2c_
Primary template to catch any non I2C interface types.
Definition: i2c.h:257
utl::i2c_i::clock
void clock(uint32_t f)
set clock frequency of the bus
Definition: i2c.h:88
utl::i2c_i
Abstract base class for i2c bus.
Definition: i2c.h:43
utl::i2c_i_details::try_stop_t
decltype(declval< _Tp >().stop()) try_stop_t
Definition: i2c.h:249
utl::i2c_i< virtual_tag >
A virtual base class specialization.
Definition: i2c.h:140
utl::meta::true_
bool_< true > true_
The type used as a compile-time boolean with true value.
Definition: integral.h:68
crtp.h
utl::i2c_i_details::try_tx_data_t
decltype(declval< _Tp >().tx_data(declval< byte_t >(), declval< typename _Tp::Sequence >())) try_tx_data_t
Definition: i2c.h:253
utl::i2c_i_details::try_cclk_t
decltype(declval< const _Tp >().clock()) try_cclk_t
Definition: i2c.h:246
utl::i2c_i_details::try_rx_data_t
decltype(declval< _Tp >().rx_data(declval< bool >(), declval< typename _Tp::Sequence >())) try_rx_data_t
Definition: i2c.h:251
utl::i2c_i::i2c_i
i2c_i()=default
Allow constructor from derived only.
utl::i2c_i::_stop
void _stop()
Send stop functionality.
Definition: i2c.h:77
utl::i2c_i::~i2c_i
~i2c_i()=default
Allow destructor from derived only.
utl::i2c_i_details::try_clk_t
decltype(declval< _Tp >().clock(declval< uint32_t >())) try_clk_t
Definition: i2c.h:247
utl::meta::false_
bool_< false > false_
The type used as a compile-time boolean with false value.
Definition: integral.h:69
utl::i2c_i::_clock
void _clock(uint32_t c)
set clock frequency of the bus [Hz]
Definition: i2c.h:75
utl
STL&#39;s core language concepts.
Definition: _1wire.h:30
utl::i2c_i< virtual_tag >::clock
uint32_t clock() const
Definition: i2c.h:180
utl::i2c_i< virtual_tag >::start
void start()
Definition: i2c.h:189
utl::i2c_i::stop
void stop()
Send stop functionality.
Definition: i2c.h:97
utl::i2c_i::start
void start()
Send start functionality.
Definition: i2c.h:96
utl::i2c_i< i2c_bb_i< impl_t > >::Sequence
Sequence
I2C transmit/receive sequence.
Definition: i2c.h:49
utl::i2c_i::type
i2c_i< impl_t > type
Definition: i2c.h:47
utl::i2c_i::Sequence::ACK
Only send/receive ack [1 clock].
utl::i2c_i::_tx_data
bool _tx_data(byte_t byte, Sequence seq)
Definition: i2c.h:79
utl::i2c_i< virtual_tag >::rx_data
byte_t rx_data(bool ack, Sequence seq=Sequence::BYTEnACK)
Receive a byte from the i2c bus.
Definition: i2c.h:203
utl::i2c_i::operator=
type & operator=(const type &)=delete
utl::i2c_i::clock
uint32_t clock() const
Definition: i2c.h:87
utl::i2c_i< virtual_tag >::clock
void clock(uint32_t c)
set clock frequency of the bus [Hz]
Definition: i2c.h:181
utl::i2c_i::rx_data
byte_t rx_data(bool ack, Sequence seq=Sequence::BYTEnACK)
Receive a byte from the i2c bus.
Definition: i2c.h:111
utl::i2c_i::_CRTP_IMPL
_CRTP_IMPL(impl_t)
utl::i2c_i::tx_data
bool tx_data(byte_t byte, Sequence seq=Sequence::BYTEnACK)
Transmit a byte to the i2c bus.
Definition: i2c.h:127
utl::byte_t
uint8_t byte_t
8 bits wide
Definition: types.h:31
utl::virtual_tag
virtual support tag type
Definition: crtp.h:40
utl::i2c_i< virtual_tag >::tx_data
bool tx_data(byte_t byte, Sequence seq=Sequence::BYTEnACK)
Transmit a byte to the i2c bus.
Definition: i2c.h:218
utl::i2c_i::Sequence::BYTEnACK
Read/Write byte and ack [9 clocks].
utl::i2c_i< virtual_tag >::stop
void stop()
Definition: i2c.h:190
utl::i2c_i::_clock
uint32_t _clock() const
clock frequency of the bus [Hz]
Definition: i2c.h:74
utl::i2c_i::Sequence::BYTE
Only read/write byte [8 clocks].
utl::i2c_i::_start
void _start()
Send start functionality.
Definition: i2c.h:76
utl::i2c_i< virtual_tag >::Sequence
Sequence
I2C transmit/receive sequence.
Definition: i2c.h:145
utl::i2c_i::_rx_data
byte_t _rx_data(bool ack, Sequence seq)
Definition: i2c.h:78
utl::i2c_i_details::try_start_t
decltype(declval< _Tp >().start()) try_start_t
Definition: i2c.h:248
impl.h
Implementation detail main forward header.