From 49b219ef25cce8dae825491ab84a4e3382f90c60 Mon Sep 17 00:00:00 2001 From: Christos Houtouridis Date: Sat, 27 Oct 2018 23:19:06 +0300 Subject: [PATCH] com: Some rework to i2c to uniform the rest of iface of utl --- include/utl/com/i2c.h | 103 ++++++++++++++++++++------------------- include/utl/com/i2c_bb.h | 58 ++++++++++------------ 2 files changed, 79 insertions(+), 82 deletions(-) diff --git a/include/utl/com/i2c.h b/include/utl/com/i2c.h index 37b541d..8cdea23 100644 --- a/include/utl/com/i2c.h +++ b/include/utl/com/i2c.h @@ -27,11 +27,11 @@ namespace utl { - /*! - * \ingroup Communication - * \brief Abstract base class for i2c bus - */ - //!@{ +/*! + * \ingroup Communication + * \brief Abstract base class for i2c bus + */ +//!@{ /*! * \brief @@ -70,12 +70,13 @@ namespace utl { * it also need to declare this class as friend */ //!@{ - //! uint32_t _frequency () const; < clock frequency of the bus [Hz] - //! void _frequency (uint32_t); < set clock frequency of the bus [Hz] - //! void _start(); < Send start functionality - //! void _stop(); < Send stop functionality - //! byte_t _read (bool ack, Sequence seq); < Receive a byte from the i2c bus. - //! bool _write (byte_t byte, Sequence seq);< Transmit a byte to the i2c bus. + private: + uint32_t _clock () const { return impl()._clock (); } //!< clock frequency of the bus [Hz] + void _clock (uint32_t c) { impl()._clock (c); } //!< set clock frequency of the bus [Hz] + void _start() { impl()._start (); } //!< Send start functionality + void _stop() { impl()._stop (); } //!< Send stop functionality + 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: - uint32_t frequency () const { return impl()._frequency (); } //!< \return clock frequency of the bus - void frequency (uint32_t f) { impl()._frequency (f); } //!< \brief set clock frequency of the bus + uint32_t clock () const { return _clock (); } //!< \return 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: - void start() { impl()._start (); } //!< Send start functionality - void stop () { impl()._stop (); } //!< Send stop functionality + void start() { _start (); } //!< Send start functionality + void stop () { _stop (); } //!< Send stop functionality /*! * \brief @@ -107,8 +108,8 @@ namespace utl { * \arg Sequence::BYTEnACK Receive the byte and send the ack bit * \return The byte received. */ - byte_t read (bool ack, Sequence seq =Sequence::BYTEnACK) { - return impl()._read (ack, seq); + byte_t rx_data (bool ack, Sequence seq =Sequence::BYTEnACK) { + return _rx_data (ack, seq); } /*! @@ -123,8 +124,8 @@ namespace utl { * \arg false Slave didn't ACK * \arg true Slave did ACK */ - bool write (byte_t byte, Sequence seq =Sequence::BYTEnACK) { - return impl()._write (byte, seq); + bool tx_data (byte_t byte, Sequence seq =Sequence::BYTEnACK) { + return _tx_data (byte, seq); } //!@} @@ -164,20 +165,20 @@ namespace utl { */ //!@{ private: - virtual uint32_t _frequency () 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 _start() = 0; //!< Send start 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 bool _write (byte_t byte, Sequence seq) = 0; //!< Transmit a byte to the i2c bus. + virtual uint32_t _clock () const = 0; //!< \return 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 _stop() = 0; //!< Send stop functionality + virtual byte_t _rx_data (bool ack, Sequence seq) = 0; //!< Receive a byte from the i2c bus. + virtual bool _tx_data (byte_t byte, Sequence seq) = 0; //!< Transmit a byte to the i2c bus. //!@} /*! * \name Get/Set functions */ //!@{ public: - uint32_t frequency () const { return _frequency(); } //!< \return clock frequency of the bus [Hz] - void frequency (uint32_t f) { _frequency(f); } //!< \brief set clock frequency of the bus [Hz] + uint32_t clock () const { return _clock(); } //!< \return 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 * \return The byte received. */ - byte_t read (bool ack, Sequence seq =Sequence::BYTEnACK) { - return _read (ack, seq); + byte_t rx_data (bool ack, Sequence seq =Sequence::BYTEnACK) { + return _rx_data (ack, seq); } /*! * \brief @@ -214,8 +215,8 @@ namespace utl { * \arg false Slave didn't ACK * \arg true Slave did ACK */ - bool write (byte_t byte, Sequence seq =Sequence::BYTEnACK) { - return _write (byte, seq); + bool tx_data (byte_t byte, Sequence seq =Sequence::BYTEnACK) { + return _tx_data (byte, seq); } //!@} @@ -231,25 +232,25 @@ namespace utl { requires not_::value>::value; requires not_::value>::value; // Methods - {ct.frequency()} -> uint32_t; - {t.frequency(0)} -> void; - {t.start()} -> void; - {t.stop()} -> void; - {t.read (1, s)} -> byte_t; - {t.write(0, s)} -> bool; + {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_cnpt { + namespace i2c_i_details { using std::declval; - template using try_cfreq_t = decltype (declval().frequency()); - template using try_freq_t = decltype (declval<_Tp>().frequency(declval())); + template using try_cclk_t = decltype (declval().clock()); + template using try_clk_t = decltype (declval<_Tp>().clock(declval())); template using try_start_t = decltype (declval<_Tp>().start()); template using try_stop_t = decltype (declval<_Tp>().stop()); - template using try_read_t - = decltype (declval<_Tp>().read (declval(), declval())); - template using try_write_t - = decltype (declval<_Tp>().write (declval(), declval())); + template using try_rx_data_t + = decltype (declval<_Tp>().rx_data (declval(), declval())); + template using try_tx_data_t + = decltype (declval<_Tp>().tx_data (declval(), declval())); //! Primary template to catch any non I2C interface types template @@ -260,13 +261,13 @@ namespace utl { struct is_i2c_ <_Tp, void_t < typename _Tp::Sequence, - use_if_same_t >, - use_if_same_t >, + use_if_same_t >, + use_if_same_t >, use_if_same_t >, use_if_same_t >, - use_if_same_t >, - use_if_same_t > - > //!^ SFINAE may apply inside if_same_t<> also + use_if_same_t >, + use_if_same_t > + > > : true_ { }; } /*! @@ -275,10 +276,10 @@ namespace utl { * \return True if _Tp is a i2c interface */ template - constexpr bool i2c_c = i2c_i_cnpt::is_i2c_<_Tp>::value; + constexpr bool i2c_c = i2c_i_details::is_i2c_<_Tp>::value; #endif - //!@} +//!@} } // namespace utl diff --git a/include/utl/com/i2c_bb.h b/include/utl/com/i2c_bb.h index a508e70..4a58910 100644 --- a/include/utl/com/i2c_bb.h +++ b/include/utl/com/i2c_bb.h @@ -29,13 +29,13 @@ namespace utl { - /*! - * \ingroup Communication - * \brief A bit banking implementation of i2c bus - * inherited from \ref i2c_i base class. - * \sa i2c_i - */ - //!@{ +/*! + * \ingroup Communication + * \brief A bit banking implementation of i2c bus + * inherited from \ref i2c_i base class. + * \sa i2c_i + */ +//!@{ /*! * \brief @@ -56,6 +56,7 @@ namespace utl { INPUT =0, OUTPUT }; + /*! * \name Object lifetime */ @@ -64,8 +65,8 @@ namespace utl { //! \brief A default destructor, allow destructor from derived only ~i2c_bb_i () noexcept = default; //! \brief A default constructor - i2c_bb_i (uint32_t frequency =100000) noexcept - : usec_ {1000000/(2*frequency)} { } + i2c_bb_i (uint32_t clk =100000) noexcept + : usec_ {1000000/(2*clk)} { } //!@} /*! @@ -73,9 +74,6 @@ namespace utl { * \note * In order for the implementation to have the following as private members * 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: @@ -94,14 +92,14 @@ namespace utl { */ //!@{ private: - uint32_t _frequency () const { return 1000000/(2*usec_); } - void _frequency (uint32_t f) { usec_ = 1000000/(2*f); } + uint32_t _clock () const { return 1000000/(2*usec_); } + void _clock (uint32_t c) { usec_ = 1000000/(2*c); } void _start (); //!< Send start functionality void _stop (); //!< Send stop functionality - byte_t _read (bool ack, Sequence seq); - bool _write (byte_t byte, Sequence seq); - uint32_t usec_; //!< half period of I2C nus + byte_t _rx_data (bool ack, Sequence seq); + bool _tx_data (byte_t byte, Sequence seq); + uint32_t usec_; //!< half period of I2C bus //!@} }; @@ -153,7 +151,7 @@ namespace utl { * \return The byte received. */ template - byte_t i2c_bb_i::_read (bool ack, Sequence seq) { + byte_t i2c_bb_i::_rx_data (bool ack, Sequence seq) { byte_t byte {0}; //Initial conditions SCL (0); @@ -193,7 +191,7 @@ namespace utl { * \arg true Slave did ACK */ template - bool i2c_bb_i::_write (byte_t byte, Sequence seq) { + bool i2c_bb_i::_tx_data (byte_t byte, Sequence seq) { bool ack {false}; //Initial conditions SCL (0); @@ -249,8 +247,8 @@ namespace utl { //!@{ protected: //! \brief Constructor - i2c_bb_i (uint32_t frequency =100000) noexcept - : usec_ {1000000/(2*frequency)} { } + i2c_bb_i (uint32_t clk =100000) noexcept + : usec_ {1000000/(2*clk)} { } //! \brief Virtual destructor virtual ~i2c_bb_i () noexcept = default; //!@} @@ -275,14 +273,14 @@ namespace utl { */ //!@{ private: - uint32_t _frequency () const final { return 1000000/(2*usec_); } - void _frequency (uint32_t f) final { usec_ = 1000000/(2*f); } + uint32_t _clock () const final { return 1000000/(2*usec_); } + void _clock (uint32_t c) final { usec_ = 1000000/(2*c); } void _start () final; void _stop () final; - byte_t _read (bool ack, Sequence seq) final; - bool _write (byte_t byte, Sequence seq) final; - //!< half period of I2C bus + byte_t _rx_data (bool ack, Sequence seq) final; + bool _tx_data (byte_t byte, Sequence seq) final; + //! half period of I2C bus uint32_t usec_; //!@} }; @@ -329,7 +327,7 @@ namespace utl { * \arg Sequence::BYTEnACK Receive the byte and send the ack bit * \return The byte received. */ - byte_t i2c_bb_i::_read (bool ack, Sequence seq) { + byte_t i2c_bb_i::_rx_data (bool ack, Sequence seq) { byte_t byte {0}; //Initial conditions SCL (0); @@ -368,7 +366,7 @@ namespace utl { * \arg false Slave didn't ACK * \arg true Slave did ACK */ - bool i2c_bb_i::_write (byte_t byte, Sequence seq) { + bool i2c_bb_i::_tx_data (byte_t byte, Sequence seq) { bool ack {false}; //Initial conditions SCL (0); @@ -398,10 +396,8 @@ namespace utl { } return ack; } - //!@} +//!@} } // namspace utl - #endif // #ifndef __utl_com_i2c_bb_h__ -