Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
BitBangI2C< HW > Class Template Reference

Implement a bit-banged I2C master. More...

#include <BitBangI2C.hxx>

Inheritance diagram for BitBangI2C< HW >:
BitBangI2CStates I2C Atomic Node Device FileIO

Public Member Functions

 BitBangI2C (const char *name)
 Constructor.
 
 ~BitBangI2C ()
 Destructor.
 
void tick_interrupt ()
 Called at a periodic tick, when enabled.
 
- Public Member Functions inherited from Device
 Device (const char *name)
 Constructor.
 
virtual ~Device ()
 Destructor.
 

Private Member Functions

bool state_start ()
 Execute state machine for sending start condition.
 
bool state_stop ()
 Execute state machine for sending the stop condition.
 
bool state_tx (uint8_t data)
 Execute data TX state machine.
 
bool state_rx (uint8_t *data, bool nack)
 Execute data RX state machine.
 
bool state_recovery ()
 Execute recovery state machine.
 
void enable () override
 function to enable device
 
void disable () override
 function to disable device
 
int transfer (struct i2c_msg *msg, bool stop) override
 Method to transmit/receive the data.
 
- Private Member Functions inherited from Atomic
void lock ()
 
void unlock ()
 

Private Attributes

struct i2c_msg * msg_
 I2C message to presently act upon

 
OSSem sem_
 semaphore to wakeup task level from ISR
 
int count_
 the count of data bytes transferred, error if < 0
 
State state_
 state machine state
 
union { 
 
   StateStart   stateStart_ 
 I2C start state machine state. More...
 
   StateStop   stateStop_ 
 I2C stop state machine state. More...
 
   StateTx   stateTx_ 
 I2C data TX state machine state. More...
 
   StateRx   stateRx_ 
 I2C data RX state machine state. More...
 
   StateRecovery   stateRecovery_ 
 I2C recovery state machine state. More...
 
};  
 
bool stop_
 if true, issue stop condition at the end of the message
 
bool clockStretchActive_
 true if the slave is clock stretching.
 

Additional Inherited Members

- Static Public Member Functions inherited from Device
static int open (struct _reent *reent, const char *path, int flags, int mode)
 Open a file or device.
 
static int close (struct _reent *reent, int fd)
 Close a file or device.
 
static int stat (struct _reent *reent, const char *path, struct stat *stat)
 Get the status information of a file or device.
 
static int select (int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, long long timeout)
 POSIX select().
 
static void select_clear ()
 Clears the current thread's select bits.
 
- Static Public Member Functions inherited from FileIO
static ssize_t read (struct _reent *reent, int fd, void *buf, size_t count)
 Read from a file or device.
 
static ssize_t write (struct _reent *reent, int fd, const void *buf, size_t count)
 Write to a file or device.
 
static _off_t lseek (struct _reent *reent, int fd, _off_t offset, int whence)
 Change the offset index of a file or device.
 
static int fstat (struct _reent *reent, int fd, struct stat *stat)
 Get the status information of a file or device.
 
static int ioctl (int fd, unsigned long int key, unsigned long data)
 Request and ioctl transaction.
 
static int fcntl (int fd, int cmd, unsigned long data)
 Manipulate a file descriptor.
 
static bool is_device (int fd)
 Test if the file descriptor belongs to a device.
 
- Protected Types inherited from BitBangI2CStates
enum class  State : uint8_t {
  START , ADDRESS , DATA_TX , DATA_RX ,
  RECOVERY , STOP
}
 High level I2C States. More...
 
enum class  StateStart : uint8_t {
  SDA_SET , SCL_SET , SDA_CLR , FIRST = SDA_SET ,
  LAST = SDA_CLR
}
 Low level I2C start states. More...
 
enum class  StateStop : uint8_t {
  SDA_CLR , SCL_SET , SDA_SET , FIRST = SDA_CLR ,
  LAST = SDA_SET
}
 Low level I2C stop states. More...
 
enum class  StateTx : uint8_t {
  DATA_7_SCL_CLR , DATA_7_SCL_SET , DATA_6_SCL_CLR , DATA_6_SCL_SET ,
  DATA_5_SCL_CLR , DATA_5_SCL_SET , DATA_4_SCL_CLR , DATA_4_SCL_SET ,
  DATA_3_SCL_CLR , DATA_3_SCL_SET , DATA_2_SCL_CLR , DATA_2_SCL_SET ,
  DATA_1_SCL_CLR , DATA_1_SCL_SET , DATA_0_SCL_CLR , DATA_0_SCL_SET ,
  ACK_SDA_SET_SCL_CLR , ACK_SCL_SET , ACK_SCL_CLR , FIRST = DATA_7_SCL_CLR ,
  LAST = ACK_SCL_CLR
}
 Low level I2C data TX states. More...
 
enum class  StateRx : uint8_t {
  DATA_7_SCL_SET , DATA_7_SCL_CLR , DATA_6_SCL_SET , DATA_6_SCL_CLR ,
  DATA_5_SCL_SET , DATA_5_SCL_CLR , DATA_4_SCL_SET , DATA_4_SCL_CLR ,
  DATA_3_SCL_SET , DATA_3_SCL_CLR , DATA_2_SCL_SET , DATA_2_SCL_CLR ,
  DATA_1_SCL_SET , DATA_1_SCL_CLR , DATA_0_SCL_SET , DATA_0_SCL_CLR ,
  ACK_SDA_SCL_SET , ACK_SCL_CLR , FIRST = DATA_7_SCL_SET , LAST = ACK_SCL_CLR
}
 Low level I2C data RX states. More...
 
enum class  StateRecovery : uint8_t {
  SDA_SET , DATA_7_SCL_SET , DATA_7_SCL_CLR , DATA_6_SCL_SET ,
  DATA_6_SCL_CLR , DATA_5_SCL_SET , DATA_5_SCL_CLR , DATA_4_SCL_SET ,
  DATA_4_SCL_CLR , DATA_3_SCL_SET , DATA_3_SCL_CLR , DATA_2_SCL_SET ,
  DATA_2_SCL_CLR , DATA_1_SCL_SET , DATA_1_SCL_CLR , DATA_0_SCL_SET ,
  DATA_0_SCL_CLR , ACK_SCL_SET , ACK_SCL_CLR , FIRST = SDA_SET ,
  LAST = ACK_SCL_CLR
}
 Low level I2C data RX states. More...
 
- Protected Member Functions inherited from BitBangI2CStates
 BitBangI2CStates ()
 Default constructor.
 
- Protected Member Functions inherited from I2C
 I2C (const char *name)
 Constructor.
 
 ~I2C ()
 Destructor.
 
- Protected Member Functions inherited from Node
 Node (const char *name)
 Constructor.
 
virtual ~Node ()
 Destructor.
 
int open (File *, const char *, int, int) OVERRIDE
 Open method.
 
int close (File *) OVERRIDE
 Close method.
 
virtual int fstat (File *file, struct stat *stat) override
 Get the status information of a file or device.
 
 Device (const char *name)
 Constructor.
 
virtual ~Device ()
 Destructor.
 
- Protected Member Functions inherited from FileIO
 FileIO (const char *name)
 Constructor.
 
virtual ~FileIO ()
 Destructor.
 
virtual off_t lseek (File *f, off_t offset, int whence)
 Seek method.
 
virtual int fcntl (File *file, int cmd, unsigned long data)
 Manipulate a file descriptor.
 
virtual bool select (File *file, int mode)
 Device select method.
 
- Static Protected Member Functions inherited from Device
static void select_insert (SelectInfo *info)
 Add client to list of clients needing woken.
 
static void select_wakeup (SelectInfo *info)
 Wakeup the list of clients needing woken.
 
static void select_wakeup_from_isr (SelectInfo *info, int *woken)
 Wakeup the list of clients needing woken.
 
static int open (struct _reent *reent, const char *path, int flags, int mode)
 Open a file or device.
 
static int close (struct _reent *reent, int fd)
 Close a file or device.
 
static int stat (struct _reent *reent, const char *path, struct stat *stat)
 Get the status information of a file or device.
 
static int select (int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, long long timeout)
 POSIX select().
 
static void select_clear ()
 Clears the current thread's select bits.
 
- Static Protected Member Functions inherited from FileIO
static int fd_alloc (void)
 Allocate a free file descriptor.
 
static void fd_free (int fd)
 Free up a file descriptor.
 
static Filefile_lookup (int fd)
 Looks up a reference to a File corresponding to a given file descriptor.
 
static int fd_lookup (File *file)
 Looks up a file descriptor corresponding to a given File reference.
 
static ssize_t read (struct _reent *reent, int fd, void *buf, size_t count)
 Read from a file or device.
 
static ssize_t write (struct _reent *reent, int fd, const void *buf, size_t count)
 Write to a file or device.
 
static _off_t lseek (struct _reent *reent, int fd, _off_t offset, int whence)
 Change the offset index of a file or device.
 
static int fstat (struct _reent *reent, int fd, struct stat *stat)
 Get the status information of a file or device.
 
static int ioctl (int fd, unsigned long int key, unsigned long data)
 Request and ioctl transaction.
 
static int fcntl (int fd, int cmd, unsigned long data)
 Manipulate a file descriptor.
 
static bool is_device (int fd)
 Test if the file descriptor belongs to a device.
 
- Protected Attributes inherited from Node
OSMutex lock_
 protects internal structures.
 
mode_t mode_
 File open mode, such as O_NONBLOCK.
 
unsigned int references_
 number of open references
 
- Protected Attributes inherited from FileIO
const char * name
 device name
 
- Static Protected Attributes inherited from FileIO
static const unsigned int numOpenFiles = 20
 
static File files []
 File descriptor pool.
 
static OSMutex mutex
 mutual exclusion for fileio
 

Detailed Description

template<class HW>
class BitBangI2C< HW >

Implement a bit-banged I2C master.

A periodic timer [interrupt] should call the BitBangI2C::tick_interrupt() method at a rate that is one half the desired clock rate. For example, for a 100kHz bus, call once every 5 microseconds. The tick should be enabled to start. The driver will call HW::tick_enable()/HW::tick_disable() to enable/disable the tick as needed to save on spurious interrupts.

Example:

struct I2CHwDefs
{
class SCL_Pin
{
public:
static void set(bool on)
{
...
}
static bool get()
{
...
}
static void hw_init()
{
...
}
};
class SDA_Pin
{
public:
static void set(bool on)
{
...
}
static bool get()
{
...
}
static void hw_init()
{
...
}
};
static void tick_enable()
{
...
}
static void tick_disable()
{
...
}
};
BitBangI2C<I2CHwDefs> bitBangI2C("/dev/i2c0", disable_tick, enable_tick);
Implement a bit-banged I2C master.
Template Parameters
HWhardware interface to the access the SCL and SDA I/O lines

Definition at line 235 of file BitBangI2C.hxx.

Constructor & Destructor Documentation

◆ BitBangI2C()

template<class HW >
BitBangI2C< HW >::BitBangI2C ( const char *  name)
inline

Constructor.

Parameters
namename of this device instance in the file system

Definition at line 242 of file BitBangI2C.hxx.

◆ ~BitBangI2C()

template<class HW >
BitBangI2C< HW >::~BitBangI2C ( )
inline

Destructor.

Definition at line 257 of file BitBangI2C.hxx.

Member Function Documentation

◆ disable()

template<class HW >
void BitBangI2C< HW >::disable ( )
inlineoverrideprivatevirtual

function to disable device

Implements Node.

Definition at line 295 of file BitBangI2C.hxx.

◆ enable()

template<class HW >
void BitBangI2C< HW >::enable ( )
inlineoverrideprivatevirtual

function to enable device

Implements Node.

Definition at line 294 of file BitBangI2C.hxx.

◆ state_recovery()

template<class HW >
bool BitBangI2C< HW >::state_recovery ( )
inlineprivate

Execute recovery state machine.

Returns
true if the sub-state machine is finished, after doing the work required by the current tick.

Definition at line 751 of file BitBangI2C.hxx.

◆ state_rx()

template<class HW >
bool BitBangI2C< HW >::state_rx ( uint8_t *  data,
bool  nack 
)
inlineprivate

Execute data RX state machine.

Parameters
locationto shift data into
nacksend a NACK in the (N)ACK slot
Returns
true if the sub-state machine is finished, after doing the work required by the current tick.

Definition at line 667 of file BitBangI2C.hxx.

◆ state_start()

template<class HW >
bool BitBangI2C< HW >::state_start ( )
inlineprivate

Execute state machine for sending start condition.

Returns
true if the sub-state machine is finished, after doing the work required by the current tick.

Definition at line 523 of file BitBangI2C.hxx.

◆ state_stop()

template<class HW >
bool BitBangI2C< HW >::state_stop ( )
inlineprivate

Execute state machine for sending the stop condition.

Returns
true if the sub-state machine is finished, after doing the work required by the current tick.

Definition at line 548 of file BitBangI2C.hxx.

◆ state_tx()

template<class HW >
bool BitBangI2C< HW >::state_tx ( uint8_t  data)
inlineprivate

Execute data TX state machine.

Parameters
datavalue to send
Returns
true if the sub-state machine is finished, after doing the work required by the current tick. count_ may be negative to indicate an error.

Definition at line 572 of file BitBangI2C.hxx.

◆ tick_interrupt()

template<class HW >
void BitBangI2C< HW >::tick_interrupt ( )
inline

Called at a periodic tick, when enabled.

Todo:
only supporting 7-bit I2C addresses at the moment.

Definition at line 390 of file BitBangI2C.hxx.

◆ transfer()

template<class HW >
int BitBangI2C< HW >::transfer ( struct i2c_msg *  msg,
bool  stop 
)
inlineoverrideprivatevirtual

Method to transmit/receive the data.

Parameters
msgmessage to transact.
stopproduce a stop condition at the end of the transfer
Returns
bytes transfered upon success or -1 with errno set

Implements I2C.

Definition at line 794 of file BitBangI2C.hxx.

Member Data Documentation

◆ clockStretchActive_

template<class HW >
bool BitBangI2C< HW >::clockStretchActive_
private

true if the slave is clock stretching.

Definition at line 316 of file BitBangI2C.hxx.

◆ count_

template<class HW >
int BitBangI2C< HW >::count_
private

the count of data bytes transferred, error if < 0

Definition at line 305 of file BitBangI2C.hxx.

◆ msg_

template<class HW >
struct i2c_msg* BitBangI2C< HW >::msg_
private

I2C message to presently act upon

Definition at line 303 of file BitBangI2C.hxx.

◆ sem_

template<class HW >
OSSem BitBangI2C< HW >::sem_
private

semaphore to wakeup task level from ISR

Definition at line 304 of file BitBangI2C.hxx.

◆ state_

template<class HW >
State BitBangI2C< HW >::state_
private

state machine state

Definition at line 306 of file BitBangI2C.hxx.

◆ stateRecovery_

template<class HW >
StateRecovery BitBangI2C< HW >::stateRecovery_

I2C recovery state machine state.

Definition at line 313 of file BitBangI2C.hxx.

◆ stateRx_

template<class HW >
StateRx BitBangI2C< HW >::stateRx_

I2C data RX state machine state.

Definition at line 312 of file BitBangI2C.hxx.

◆ stateStart_

template<class HW >
StateStart BitBangI2C< HW >::stateStart_

I2C start state machine state.

Definition at line 309 of file BitBangI2C.hxx.

◆ stateStop_

template<class HW >
StateStop BitBangI2C< HW >::stateStop_

I2C stop state machine state.

Definition at line 310 of file BitBangI2C.hxx.

◆ stateTx_

template<class HW >
StateTx BitBangI2C< HW >::stateTx_

I2C data TX state machine state.

Definition at line 311 of file BitBangI2C.hxx.

◆ stop_

template<class HW >
bool BitBangI2C< HW >::stop_
private

if true, issue stop condition at the end of the message

Definition at line 315 of file BitBangI2C.hxx.


The documentation for this class was generated from the following file: