com: Some rework to i2c to uniform the rest of iface of utl

This commit is contained in:
Christos Houtouridis 2018-10-27 23:19:06 +03:00
parent de4eb2cf09
commit 49b219ef25
2 changed files with 79 additions and 82 deletions

View File

@ -70,12 +70,13 @@ namespace utl {
* it also need to declare this class as friend * it also need to declare this class as friend
*/ */
//!@{ //!@{
//! uint32_t _frequency () const; < clock frequency of the bus [Hz] private:
//! void _frequency (uint32_t); < set clock frequency of the bus [Hz] uint32_t _clock () const { return impl()._clock (); } //!< clock frequency of the bus [Hz]
//! void _start(); < Send start functionality void _clock (uint32_t c) { impl()._clock (c); } //!< set clock frequency of the bus [Hz]
//! void _stop(); < Send stop functionality void _start() { impl()._start (); } //!< Send start functionality
//! byte_t _read (bool ack, Sequence seq); < Receive a byte from the i2c bus. void _stop() { impl()._stop (); } //!< Send stop functionality
//! bool _write (byte_t byte, Sequence seq);< Transmit a byte to the i2c bus. 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); }
//!@} //!@}
/*! /*!
@ -83,8 +84,8 @@ namespace utl {
*/ */
//!@{ //!@{
public: public:
uint32_t frequency () const { return impl()._frequency (); } //!< \return clock frequency of the bus uint32_t clock () const { return _clock (); } //!< \return clock frequency of the bus
void frequency (uint32_t f) { impl()._frequency (f); } //!< \brief set clock frequency of the bus void clock (uint32_t f) { _clock (f); } //!< \brief set clock frequency of the bus
//!@} //!@}
/*! /*!
@ -92,8 +93,8 @@ namespace utl {
*/ */
//!@{ //!@{
public: public:
void start() { impl()._start (); } //!< Send start functionality void start() { _start (); } //!< Send start functionality
void stop () { impl()._stop (); } //!< Send stop functionality void stop () { _stop (); } //!< Send stop functionality
/*! /*!
* \brief * \brief
@ -107,8 +108,8 @@ namespace utl {
* \arg Sequence::BYTEnACK Receive the byte and send the ack bit * \arg Sequence::BYTEnACK Receive the byte and send the ack bit
* \return The byte received. * \return The byte received.
*/ */
byte_t read (bool ack, Sequence seq =Sequence::BYTEnACK) { byte_t rx_data (bool ack, Sequence seq =Sequence::BYTEnACK) {
return impl()._read (ack, seq); return _rx_data (ack, seq);
} }
/*! /*!
@ -123,8 +124,8 @@ namespace utl {
* \arg false Slave didn't ACK * \arg false Slave didn't ACK
* \arg true Slave did ACK * \arg true Slave did ACK
*/ */
bool write (byte_t byte, Sequence seq =Sequence::BYTEnACK) { bool tx_data (byte_t byte, Sequence seq =Sequence::BYTEnACK) {
return impl()._write (byte, seq); return _tx_data (byte, seq);
} }
//!@} //!@}
@ -164,20 +165,20 @@ namespace utl {
*/ */
//!@{ //!@{
private: private:
virtual uint32_t _frequency () const = 0; //!< \return clock frequency of the bus [Hz] virtual uint32_t _clock () const = 0; //!< \return clock frequency of the bus [Hz]
virtual void _frequency (uint32_t) = 0; //!< \brief set clock frequency of the bus [Hz] virtual void _clock (uint32_t) = 0; //!< \brief set clock frequency of the bus [Hz]
virtual void _start() = 0; //!< Send start functionality virtual void _start() = 0; //!< Send start functionality
virtual void _stop() = 0; //!< Send stop functionality virtual void _stop() = 0; //!< Send stop functionality
virtual byte_t _read (bool ack, Sequence seq) = 0; //!< Receive a byte from the i2c bus. virtual byte_t _rx_data (bool ack, Sequence seq) = 0; //!< Receive a byte from the i2c bus.
virtual bool _write (byte_t byte, Sequence seq) = 0; //!< Transmit a byte to the i2c bus. virtual bool _tx_data (byte_t byte, Sequence seq) = 0; //!< Transmit a byte to the i2c bus.
//!@} //!@}
/*! /*!
* \name Get/Set functions * \name Get/Set functions
*/ */
//!@{ //!@{
public: public:
uint32_t frequency () const { return _frequency(); } //!< \return clock frequency of the bus [Hz] uint32_t clock () const { return _clock(); } //!< \return clock frequency of the bus [Hz]
void frequency (uint32_t f) { _frequency(f); } //!< \brief set clock frequency of the bus [Hz] void clock (uint32_t c) { _clock(c); } //!< \brief set clock frequency of the bus [Hz]
//!@} //!@}
/*! /*!
@ -199,8 +200,8 @@ namespace utl {
* \arg Sequence::BYTEnACK Receive the byte and send the ack bit * \arg Sequence::BYTEnACK Receive the byte and send the ack bit
* \return The byte received. * \return The byte received.
*/ */
byte_t read (bool ack, Sequence seq =Sequence::BYTEnACK) { byte_t rx_data (bool ack, Sequence seq =Sequence::BYTEnACK) {
return _read (ack, seq); return _rx_data (ack, seq);
} }
/*! /*!
* \brief * \brief
@ -214,8 +215,8 @@ namespace utl {
* \arg false Slave didn't ACK * \arg false Slave didn't ACK
* \arg true Slave did ACK * \arg true Slave did ACK
*/ */
bool write (byte_t byte, Sequence seq =Sequence::BYTEnACK) { bool tx_data (byte_t byte, Sequence seq =Sequence::BYTEnACK) {
return _write (byte, seq); return _tx_data (byte, seq);
} }
//!@} //!@}
@ -231,25 +232,25 @@ namespace utl {
requires not_<std::is_copy_constructible<T>::value>::value; requires not_<std::is_copy_constructible<T>::value>::value;
requires not_<std::is_copy_assignable<T>::value>::value; requires not_<std::is_copy_assignable<T>::value>::value;
// Methods // Methods
{ct.frequency()} -> uint32_t; {ct.clock()} -> uint32_t;
{t.frequency(0)} -> void; {t.clock(0)} -> void;
{t.start()} -> void; {t.start()} -> void;
{t.stop()} -> void; {t.stop()} -> void;
{t.read (1, s)} -> byte_t; {t.rx_data (1, s)} -> byte_t;
{t.write(0, s)} -> bool; {t.tx_data (0, s)} -> bool;
}; };
#else #else
namespace i2c_i_cnpt { namespace i2c_i_details {
using std::declval; using std::declval;
template <class _Tp> using try_cfreq_t = decltype (declval<const _Tp>().frequency()); template <class _Tp> using try_cclk_t = decltype (declval<const _Tp>().clock());
template <class _Tp> using try_freq_t = decltype (declval<_Tp>().frequency(declval<uint32_t>())); 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_start_t = decltype (declval<_Tp>().start());
template <class _Tp> using try_stop_t = decltype (declval<_Tp>().stop()); template <class _Tp> using try_stop_t = decltype (declval<_Tp>().stop());
template <class _Tp> using try_read_t template <class _Tp> using try_rx_data_t
= decltype (declval<_Tp>().read (declval<bool>(), declval<typename _Tp::Sequence>())); = decltype (declval<_Tp>().rx_data (declval<bool>(), declval<typename _Tp::Sequence>()));
template <class _Tp> using try_write_t template <class _Tp> using try_tx_data_t
= decltype (declval<_Tp>().write (declval<byte_t>(), declval<typename _Tp::Sequence>())); = decltype (declval<_Tp>().tx_data (declval<byte_t>(), declval<typename _Tp::Sequence>()));
//! Primary template to catch any non I2C interface types //! Primary template to catch any non I2C interface types
template <typename _Tp, typename =void> template <typename _Tp, typename =void>
@ -260,13 +261,13 @@ namespace utl {
struct is_i2c_ <_Tp, struct is_i2c_ <_Tp,
void_t < void_t <
typename _Tp::Sequence, typename _Tp::Sequence,
use_if_same_t <uint32_t,try_cfreq_t <_Tp>>, use_if_same_t <uint32_t,try_cclk_t <_Tp>>,
use_if_same_t <void, try_freq_t <_Tp>>, use_if_same_t <void, try_clk_t <_Tp>>,
use_if_same_t <void, try_start_t <_Tp>>, use_if_same_t <void, try_start_t <_Tp>>,
use_if_same_t <void, try_stop_t <_Tp>>, use_if_same_t <void, try_stop_t <_Tp>>,
use_if_same_t <byte_t, try_read_t <_Tp>>, use_if_same_t <byte_t, try_rx_data_t <_Tp>>,
use_if_same_t <bool, try_write_t <_Tp>> use_if_same_t <bool, try_tx_data_t <_Tp>>
> //!^ SFINAE may apply inside if_same_t<> also >
> : true_ { }; > : true_ { };
} }
/*! /*!
@ -275,7 +276,7 @@ namespace utl {
* \return True if _Tp is a i2c interface * \return True if _Tp is a i2c interface
*/ */
template <typename _Tp> template <typename _Tp>
constexpr bool i2c_c = i2c_i_cnpt::is_i2c_<_Tp>::value; constexpr bool i2c_c = i2c_i_details::is_i2c_<_Tp>::value;
#endif #endif
//!@} //!@}

View File

@ -56,6 +56,7 @@ namespace utl {
INPUT =0, INPUT =0,
OUTPUT OUTPUT
}; };
/*! /*!
* \name Object lifetime * \name Object lifetime
*/ */
@ -64,8 +65,8 @@ namespace utl {
//! \brief A default destructor, allow destructor from derived only //! \brief A default destructor, allow destructor from derived only
~i2c_bb_i () noexcept = default; ~i2c_bb_i () noexcept = default;
//! \brief A default constructor //! \brief A default constructor
i2c_bb_i (uint32_t frequency =100000) noexcept i2c_bb_i (uint32_t clk =100000) noexcept
: usec_ {1000000/(2*frequency)} { } : usec_ {1000000/(2*clk)} { }
//!@} //!@}
/*! /*!
@ -73,9 +74,6 @@ namespace utl {
* \note * \note
* In order for the implementation to have the following as private members * In order for the implementation to have the following as private members
* it also need to declare this class as friend * it also need to declare this class as friend
* bool SDA (SDAMode mode, bool st);
* void SCL (uint8_t st);
* void delay (uint32_t usec);
*/ */
//!@{ //!@{
private: private:
@ -94,14 +92,14 @@ namespace utl {
*/ */
//!@{ //!@{
private: private:
uint32_t _frequency () const { return 1000000/(2*usec_); } uint32_t _clock () const { return 1000000/(2*usec_); }
void _frequency (uint32_t f) { usec_ = 1000000/(2*f); } void _clock (uint32_t c) { usec_ = 1000000/(2*c); }
void _start (); //!< Send start functionality void _start (); //!< Send start functionality
void _stop (); //!< Send stop functionality void _stop (); //!< Send stop functionality
byte_t _read (bool ack, Sequence seq); byte_t _rx_data (bool ack, Sequence seq);
bool _write (byte_t byte, Sequence seq); bool _tx_data (byte_t byte, Sequence seq);
uint32_t usec_; //!< half period of I2C nus uint32_t usec_; //!< half period of I2C bus
//!@} //!@}
}; };
@ -153,7 +151,7 @@ namespace utl {
* \return The byte received. * \return The byte received.
*/ */
template <typename impl_t> template <typename impl_t>
byte_t i2c_bb_i<impl_t>::_read (bool ack, Sequence seq) { byte_t i2c_bb_i<impl_t>::_rx_data (bool ack, Sequence seq) {
byte_t byte {0}; byte_t byte {0};
//Initial conditions //Initial conditions
SCL (0); SCL (0);
@ -193,7 +191,7 @@ namespace utl {
* \arg true Slave did ACK * \arg true Slave did ACK
*/ */
template <typename impl_t> template <typename impl_t>
bool i2c_bb_i<impl_t>::_write (byte_t byte, Sequence seq) { bool i2c_bb_i<impl_t>::_tx_data (byte_t byte, Sequence seq) {
bool ack {false}; bool ack {false};
//Initial conditions //Initial conditions
SCL (0); SCL (0);
@ -249,8 +247,8 @@ namespace utl {
//!@{ //!@{
protected: protected:
//! \brief Constructor //! \brief Constructor
i2c_bb_i (uint32_t frequency =100000) noexcept i2c_bb_i (uint32_t clk =100000) noexcept
: usec_ {1000000/(2*frequency)} { } : usec_ {1000000/(2*clk)} { }
//! \brief Virtual destructor //! \brief Virtual destructor
virtual ~i2c_bb_i () noexcept = default; virtual ~i2c_bb_i () noexcept = default;
//!@} //!@}
@ -275,14 +273,14 @@ namespace utl {
*/ */
//!@{ //!@{
private: private:
uint32_t _frequency () const final { return 1000000/(2*usec_); } uint32_t _clock () const final { return 1000000/(2*usec_); }
void _frequency (uint32_t f) final { usec_ = 1000000/(2*f); } void _clock (uint32_t c) final { usec_ = 1000000/(2*c); }
void _start () final; void _start () final;
void _stop () final; void _stop () final;
byte_t _read (bool ack, Sequence seq) final; byte_t _rx_data (bool ack, Sequence seq) final;
bool _write (byte_t byte, Sequence seq) final; bool _tx_data (byte_t byte, Sequence seq) final;
//!< half period of I2C bus //! half period of I2C bus
uint32_t usec_; uint32_t usec_;
//!@} //!@}
}; };
@ -329,7 +327,7 @@ namespace utl {
* \arg Sequence::BYTEnACK Receive the byte and send the ack bit * \arg Sequence::BYTEnACK Receive the byte and send the ack bit
* \return The byte received. * \return The byte received.
*/ */
byte_t i2c_bb_i<virtual_tag>::_read (bool ack, Sequence seq) { byte_t i2c_bb_i<virtual_tag>::_rx_data (bool ack, Sequence seq) {
byte_t byte {0}; byte_t byte {0};
//Initial conditions //Initial conditions
SCL (0); SCL (0);
@ -368,7 +366,7 @@ namespace utl {
* \arg false Slave didn't ACK * \arg false Slave didn't ACK
* \arg true Slave did ACK * \arg true Slave did ACK
*/ */
bool i2c_bb_i<virtual_tag>::_write (byte_t byte, Sequence seq) { bool i2c_bb_i<virtual_tag>::_tx_data (byte_t byte, Sequence seq) {
bool ack {false}; bool ack {false};
//Initial conditions //Initial conditions
SCL (0); SCL (0);
@ -398,10 +396,8 @@ namespace utl {
} }
return ack; return ack;
} }
//!@}
//!@}
} // namspace utl } // namspace utl
#endif // #ifndef __utl_com_i2c_bb_h__ #endif // #ifndef __utl_com_i2c_bb_h__