|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- /*!
- * \file /utl/com/i2c.h
- * \brief An Abstract base class interface for the i2c bus
- *
- * Copyright (C) 2018 Christos Choutouridis
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation, either version 3
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- */
- #ifndef _utl_com_i2c_h__
- #define _utl_com_i2c_h__
-
- #include <utl/impl/impl.h>
- #include <utl/helper/crtp.h>
- #include <utl/meta/sfinae.h>
-
- namespace utl {
-
- /*!
- * \ingroup Communication
- * \brief Abstract base class for i2c bus
- */
- //!@{
-
- /*!
- * \brief
- * this class force a common interface for I2C communication
- * protocol implementations using CRTP
- * \param impl_t The CRTP type (the derived/implementation class typename).
- */
- template <typename impl_t>
- class i2c_i {
- _CRTP_IMPL(impl_t);
-
- public:
- using type = i2c_i<impl_t>; //!< Export type as identity meta-function
- //! I2C transmit/receive sequence
- enum class Sequence {
- BYTE =0, //!< Only read/write byte [8 clocks]
- ACK, //!< Only send/receive ack [1 clock]
- BYTEnACK //!< Read/Write byte and ack [9 clocks]
- };
-
- /*!
- * \name Object lifetime
- */
- //!@{
- protected:
- i2c_i () = default; //!< Allow constructor from derived only
- ~i2c_i () = default; //!< Allow destructor from derived only
- i2c_i (const type&) = delete; //!< No copies
- type& operator= (const type&) = delete;
- //!@}
-
- /*!
- * \name Implementation requirements
- * \note
- * In order for the implementation to have the following as private members
- * 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.
- //!@}
-
- /*!
- * \name Get/Set functions
- */
- //!@{
- 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
- //!@}
-
- /*!
- * \name User functions
- */
- //!@{
- public:
- void start() { impl()._start (); } //!< Send start functionality
- void stop () { impl()._stop (); } //!< Send stop functionality
-
- /*!
- * \brief
- * Receive a byte from the i2c bus.
- * \param ack Optional ack bit.
- * \arg 1 ACK the reception
- * \arg 0 Don't ACK the reception.
- * \param seq The operation sequence to execute
- * \arg Sequence::BYTE Receive only the byte, do not send ack clock
- * \arg Sequence::ACK Send only the ack bit
- * \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);
- }
-
- /*!
- * \brief
- * Transmit a byte to the i2c bus.
- * \param byte The byte to send.
- * \param seq The operation sequence to execute
- * \arg Sequence::BYTE Transmit only the byte, do not read ack bit
- * \arg Sequence::ACK Read only the ack bit
- * \arg Sequence::BYTEnACK Transmit the byte and read the ack bit
- * \return Slave's ACK bit
- * \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);
- }
- //!@}
-
- };
-
- /*!
- * \brief
- * A virtual base class specialization
- * \param impl_t = virtual_tag
- */
- template<>
- class i2c_i <virtual_tag> {
- public:
- using type = i2c_i <virtual_tag>; //!< Export type as identity meta-function
-
- //! I2C transmit/receive sequence
- enum class Sequence {
- BYTE=0, //!< Only read/write byte [8 clocks]
- ACK, //!< Only send/receive ack [1 clock]
- BYTEnACK //!< Read/Write byte and ack [9 clocks]
- };
-
- /*!
- * \name Object lifetime
- */
- //!@{
- protected:
- i2c_i () = default; //!< Allow constructor from derived only
- i2c_i (const type&) = delete; //!< No copies
- type& operator= (const type&) = delete;
- public:
- virtual ~i2c_i () = default; //!< Virtual default destructor
- //!@}
-
- /*!
- * \name Implementation requirements
- */
- //!@{
- 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.
- //!@}
- /*!
- * \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]
- //!@}
-
- /*!
- * \name User functions
- */
- //!@{
- public:
- void start () { _start(); }
- void stop () { _stop(); }
- /*!
- * \brief
- * Receive a byte from the i2c bus.
- * \param ack Optional ack bit.
- * \arg 1 ACK the reception
- * \arg 0 Don't ACK the reception.
- * \param seq The operation sequence to execute
- * \arg Sequence::BYTE Receive only the byte, do not send ack clock
- * \arg Sequence::ACK Send only the ack bit
- * \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);
- }
- /*!
- * \brief
- * Transmit a byte to the i2c bus.
- * \param byte The byte to send.
- * \param seq The operation sequence to execute
- * \arg Sequence::BYTE Transmit only the byte, do not read ack bit
- * \arg Sequence::ACK Read only the ack bit
- * \arg Sequence::BYTEnACK Transmit the byte and read the ack bit
- * \return Slave's ACK bit
- * \arg false Slave didn't ACK
- * \arg true Slave did ACK
- */
- bool write (byte_t byte, Sequence seq =Sequence::BYTEnACK) {
- return _write (byte, seq);
- }
- //!@}
-
- };
-
- #if defined _utl_have_concepts
- /*!
- * i2c interface concept
- */
- template <typename T>
- concept bool i2c_c = 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.frequency()} -> uint32_t;
- {t.frequency(0)} -> void;
- {t.start()} -> void;
- {t.stop()} -> void;
- {t.read (1, s)} -> byte_t;
- {t.write(0, s)} -> bool;
- };
- #else
- namespace i2c_i_cnpt {
- using std::declval;
-
- template <class _Tp> using try_cfreq_t = decltype (declval<const _Tp>().frequency());
- template <class _Tp> using try_freq_t = decltype (declval<_Tp>().frequency(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_read_t
- = decltype (declval<_Tp>().read (declval<bool>(), declval<typename _Tp::Sequence>()));
- template <class _Tp> using try_write_t
- = decltype (declval<_Tp>().write (declval<byte_t>(), declval<typename _Tp::Sequence>()));
-
- //! Primary template to catch any non I2C interface types
- template <typename _Tp, typename =void>
- struct is_i2c_ : false_ { };
-
- //! template to catch a proper I2C interface type
- template <typename _Tp>
- struct is_i2c_ <_Tp,
- void_t <
- typename _Tp::Sequence,
- use_if_same_t <uint32_t,try_cfreq_t <_Tp>>,
- use_if_same_t <void, try_freq_t <_Tp>>,
- use_if_same_t <void, try_start_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 <bool, try_write_t <_Tp>>
- > //!^ SFINAE may apply inside if_same_t<> also
- > : true_ { };
- }
- /*!
- * Value meta-programming function alias for I2C interface checking
- * \param _Tp Type to check
- * \return True if _Tp is a i2c interface
- */
- template <typename _Tp>
- constexpr bool i2c_c = i2c_i_cnpt::is_i2c_<_Tp>::value;
- #endif
-
- //!@}
-
- } // namespace utl
-
- #endif // _utl_com_i2c_h__
|