uStepper S-lite
Public Member Functions | Private Member Functions | Private Attributes | List of all members
i2cMaster Class Reference

Prototype of class for accessing the TWI (I2C) interface of the AVR (master mode only). More...

#include <i2cMaster.h>

Public Member Functions

void * operator new (size_t size)
 
 i2cMaster (bool channel)
 Constructor. More...
 
 i2cMaster (void)
 Constructor. More...
 
bool readByte (bool ack, uint8_t *data)
 Reads a byte from the I2C bus. More...
 
bool read (uint8_t slaveAddr, uint8_t regAddr, uint8_t numOfBytes, uint8_t *data)
 sets up I2C connection to device, reads a number of data bytes and closes the connection More...
 
bool start (uint8_t addr, bool RW)
 sets up connection between arduino and I2C device. More...
 
bool restart (uint8_t addr, bool RW)
 Restarts connection between arduino and I2C device. More...
 
bool writeByte (uint8_t data)
 Writes a byte to a device on the I2C bus. More...
 
bool write (uint8_t slaveAddr, uint8_t regAddr, uint8_t numOfBytes, uint8_t *data)
 sets up I2C connection to device, writes a number of data bytes and closes the connection More...
 
bool stop (void)
 Closes the I2C connection. More...
 
uint8_t getStatus (void)
 Get current I2C status. More...
 
void begin (void)
 Setup TWI (I2C) interface. More...
 
void begin (bool channel)
 Setup TWI (I2C) interface. More...
 

Private Member Functions

bool cmd (uint8_t cmd)
 Sends commands over the I2C bus. More...
 

Private Attributes

uint8_t status
 
volatile uint8_t twsr
 
volatile uint8_t twbr
 
volatile uint8_t twdr
 
volatile uint8_t twcr
 

Detailed Description

Prototype of class for accessing the TWI (I2C) interface of the AVR (master mode only).

This class enables the use of the hardware TWI (I2C) interface in the AVR (master mode only), which is used for interfacing with the encoder. This class is needed in this library, as arduino's build in "wire" library uses interrupts to access the TWI interface, and since the uStepper library needs to use the TWI interface within a timer interrupt, this library cannot be used. As a result of this, the "wire" library, cannot be used in sketches using the uStepper library, as this will screw with the setups, and make the sketch hang in the timer interrupt routine. Instead, if the programmer of the sketch needs to interface with external I2C devices, this class should be used. This library contains a predefined object called "I2C", which should be used for these purposes.

The functions "read()" and "write()", should be the only functions needed by most users of this library !

Definition at line 93 of file i2cMaster.h.

Constructor & Destructor Documentation

◆ i2cMaster() [1/2]

i2cMaster::i2cMaster ( bool  channel)

Constructor.

        This is the constructor, used to instantiate an I2C
        object. Under normal circumstances, this should not be
        needed by the programmer of the arduino sketch, since
        this library already has a global object instantiation of
        this class, called "I2C".

Definition at line 256 of file i2cMaster.cpp.

◆ i2cMaster() [2/2]

i2cMaster::i2cMaster ( void  )

Constructor.

        This is the constructor, used to instantiate an I2C
        object. Under normal circumstances, this should not be
        needed by the programmer of the arduino sketch, since
        this library already has a global object instantiation of
        this class, called "I2C".

Definition at line 274 of file i2cMaster.cpp.

Member Function Documentation

◆ begin() [1/2]

void i2cMaster::begin ( bool  channel)

Setup TWI (I2C) interface.

        This function sets up the TWI interface, and is
        automatically called in the instantiation of the uStepper
        encoder object.

Definition at line 229 of file i2cMaster.cpp.

◆ begin() [2/2]

void i2cMaster::begin ( void  )

Setup TWI (I2C) interface.

        This function sets up the TWI interface, and is
        automatically called in the instantiation of the uStepper
        encoder object.

Definition at line 221 of file i2cMaster.cpp.

◆ cmd()

bool i2cMaster::cmd ( uint8_t  cmd)
private

Sends commands over the I2C bus.

        This function is used to send different commands over the
        I2C bus.
Parameters
cmd- Command to be send over the I2C bus.

Definition at line 39 of file i2cMaster.cpp.

◆ getStatus()

uint8_t i2cMaster::getStatus ( void  )

Get current I2C status.

        This function returns the status of the I2C bus.
Returns
Status of the I2C bus. Refer to defines for possible status

Definition at line 216 of file i2cMaster.cpp.

◆ read()

bool i2cMaster::read ( uint8_t  slaveAddr,
uint8_t  regAddr,
uint8_t  numOfBytes,
uint8_t *  data 
)

sets up I2C connection to device, reads a number of data bytes and closes the connection

This function is used to perform a read transaction between the arduino and an I2C device. This function will perform everything from setting up the connection, reading the desired number of bytes and tear down the connection.

Parameters
slaveAddr- 7 bit address of the device to read from
regAddr- 8 bit address of the register to read from
numOfBytes- Number of bytes to read from the device
data- Address of the array/string to store the bytes read. Make sure enough space are allocated before calling this function !
Returns
1 - Currently always returns this value. In the future this value will be used to indicate successful transactions.

Definition at line 61 of file i2cMaster.cpp.

◆ readByte()

bool i2cMaster::readByte ( bool  ack,
uint8_t *  data 
)

Reads a byte from the I2C bus.

        This function requests a byte from the device addressed
        during the I2C transaction setup. The parameter "ack" is
        used to determine whether the device should keep sending
        data or not after the reception of the currently
        requested data byte.
Parameters
ack- should be set to "ACK" if more bytes is wanted, and "NACK" if no more bytes should be send (without the quotes)
data- Address of the variable to store the requested data byte
Returns
Always returns 1

Definition at line 128 of file i2cMaster.cpp.

◆ restart()

bool i2cMaster::restart ( uint8_t  addr,
bool  RW 
)

Restarts connection between arduino and I2C device.

        This function restarts the connection between the arduino
        and the I2C device desired to communicate with, by
        sending a start condition on the I2C bus, followed by the
        device address and a read/write bit.
Parameters
addr- Address of the device it is desired to communicate with
RW- Can be set to "READ" to setup a read transaction or "WRITE" for a write transaction (without the quotes)
Returns
1 - Connection properly set up
0 - Connection failed

Definition at line 178 of file i2cMaster.cpp.

◆ start()

bool i2cMaster::start ( uint8_t  addr,
bool  RW 
)

sets up connection between arduino and I2C device.

        This function sets up the connection between the arduino
        and the I2C device desired to communicate with, by
        sending a start condition on the I2C bus, followed by the
        device address and a read/write bit.
Parameters
addr- Address of the device it is desired to communicate with
RW- Can be set to "READ" to setup a read transaction or "WRITE" for a write transaction (without the quotes)
Returns
1 - Connection properly set up
0 - Connection failed

Definition at line 152 of file i2cMaster.cpp.

◆ stop()

bool i2cMaster::stop ( void  )

Closes the I2C connection.

        This function is used to close down the I2C connection,
        by sending a stop condition on the I2C bus.
Returns
1 - This is always returned Currently - in a later version, this should indicate that connection is successfully closed

Definition at line 192 of file i2cMaster.cpp.

◆ write()

bool i2cMaster::write ( uint8_t  slaveAddr,
uint8_t  regAddr,
uint8_t  numOfBytes,
uint8_t *  data 
)

sets up I2C connection to device, writes a number of data bytes and closes the connection

This function is used to perform a write transaction between the arduino and an I2C device. This function will perform everything from setting up the connection, writing the desired number of bytes and tear down the connection.

Parameters
slaveAddr- 7 bit address of the device to write to
regAddr- 8 bit address of the register to write to
numOfBytes- Number of bytes to write to the device
data- Address of the array/string containing data to write.
Returns
1 - Currently always returns this value. In the future this value will be used to indicate successful transactions.

Definition at line 103 of file i2cMaster.cpp.

◆ writeByte()

bool i2cMaster::writeByte ( uint8_t  data)

Writes a byte to a device on the I2C bus.

        This function writes a byte to a device on the I2C bus.
Parameters
data- Byte to be written
Returns
1 - Byte written successfully
0 - transaction failed

Definition at line 183 of file i2cMaster.cpp.

Member Data Documentation

◆ status

uint8_t i2cMaster::status
private

Contains the status of the I2C bus

Definition at line 97 of file i2cMaster.h.


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