Browse Source

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

doc
Christos Houtouridis 6 years ago
parent
commit
49b219ef25
2 changed files with 79 additions and 82 deletions
  1. +52
    -51
      include/utl/com/i2c.h
  2. +27
    -31
      include/utl/com/i2c_bb.h

+ 52
- 51
include/utl/com/i2c.h View File

@@ -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_<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;
{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 <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_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_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>()));
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>()));
//! Primary template to catch any non I2C interface types
template <typename _Tp, typename =void>
@@ -260,13 +261,13 @@ namespace utl {
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 <uint32_t,try_cclk_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_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
use_if_same_t <byte_t, try_rx_data_t <_Tp>>,
use_if_same_t <bool, try_tx_data_t <_Tp>>
>
> : true_ { };
}
/*!
@@ -275,10 +276,10 @@ namespace utl {
* \return True if _Tp is a i2c interface
*/
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
//!@}
//!@}
} // namespace utl


+ 27
- 31
include/utl/com/i2c_bb.h View File

@@ -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 <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};
//Initial conditions
SCL (0);
@@ -193,7 +191,7 @@ namespace utl {
* \arg true Slave did ACK
*/
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};
//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<virtual_tag>::_read (bool ack, Sequence seq) {
byte_t i2c_bb_i<virtual_tag>::_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<virtual_tag>::_write (byte_t byte, Sequence seq) {
bool i2c_bb_i<virtual_tag>::_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__

Loading…
Cancel
Save