|
Open Model Railroad Network (OpenMRN)
|
Implement a bit-banged I2C master. More...
#include <BitBangI2C.hxx>
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 File * | file_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 | |
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:
| HW | hardware interface to the access the SCL and SDA I/O lines |
Definition at line 235 of file BitBangI2C.hxx.
|
inline |
Constructor.
| name | name of this device instance in the file system |
Definition at line 242 of file BitBangI2C.hxx.
|
inline |
Destructor.
Definition at line 257 of file BitBangI2C.hxx.
|
inlineoverrideprivatevirtual |
|
inlineoverrideprivatevirtual |
|
inlineprivate |
Execute recovery state machine.
Definition at line 751 of file BitBangI2C.hxx.
|
inlineprivate |
Execute data RX state machine.
| location | to shift data into |
| nack | send a NACK in the (N)ACK slot |
Definition at line 667 of file BitBangI2C.hxx.
|
inlineprivate |
Execute state machine for sending start condition.
Definition at line 523 of file BitBangI2C.hxx.
|
inlineprivate |
Execute state machine for sending the stop condition.
Definition at line 548 of file BitBangI2C.hxx.
|
inlineprivate |
Execute data TX state machine.
| data | value to send |
Definition at line 572 of file BitBangI2C.hxx.
|
inline |
Called at a periodic tick, when enabled.
Definition at line 390 of file BitBangI2C.hxx.
|
inlineoverrideprivatevirtual |
Method to transmit/receive the data.
| msg | message to transact. |
| stop | produce a stop condition at the end of the transfer |
Implements I2C.
Definition at line 794 of file BitBangI2C.hxx.
|
private |
true if the slave is clock stretching.
Definition at line 316 of file BitBangI2C.hxx.
|
private |
the count of data bytes transferred, error if < 0
Definition at line 305 of file BitBangI2C.hxx.
|
private |
I2C message to presently act upon
Definition at line 303 of file BitBangI2C.hxx.
|
private |
semaphore to wakeup task level from ISR
Definition at line 304 of file BitBangI2C.hxx.
|
private |
state machine state
Definition at line 306 of file BitBangI2C.hxx.
| StateRecovery BitBangI2C< HW >::stateRecovery_ |
I2C recovery state machine state.
Definition at line 313 of file BitBangI2C.hxx.
| StateRx BitBangI2C< HW >::stateRx_ |
I2C data RX state machine state.
Definition at line 312 of file BitBangI2C.hxx.
| StateStart BitBangI2C< HW >::stateStart_ |
I2C start state machine state.
Definition at line 309 of file BitBangI2C.hxx.
| StateStop BitBangI2C< HW >::stateStop_ |
I2C stop state machine state.
Definition at line 310 of file BitBangI2C.hxx.
| StateTx BitBangI2C< HW >::stateTx_ |
I2C data TX state machine state.
Definition at line 311 of file BitBangI2C.hxx.
|
private |
if true, issue stop condition at the end of the message
Definition at line 315 of file BitBangI2C.hxx.