PropWare  3.0.0.229
C++ objects and CMake build system for Parallax Propeller
Public Member Functions | Static Public Member Functions | Static Public Attributes | List of all members
PropWare::I2CSlave Class Referenceabstract

Basic I2C slave driver. More...

#include <PropWare/serial/i2c/i2cslave.h>

+ Inheritance diagram for PropWare::I2CSlave:
+ Collaboration diagram for PropWare::I2CSlave:

Public Member Functions

template<size_t BUFFER_SIZE, size_t STACK_SIZE>
 I2CSlave (const uint8_t address, uint8_t(&buffer)[BUFFER_SIZE], const uint32_t(&stack)[STACK_SIZE], const Pin::Mask sclMask=DEFAULT_SCL_MASK, const Pin::Mask sdaMask=DEFAULT_SDA_MASK)
 Create an I2CSlave object (requires static allocation of buffer and stack) More...
 
 I2CSlave (const uint8_t address, uint8_t *buffer, const size_t bufferSize, const uint32_t *stack, const size_t stackSize, const Pin::Mask sclMask=DEFAULT_SCL_MASK, const Pin::Mask sdaMask=DEFAULT_SDA_MASK)
 Create an I2C slave object (Allows dynamic allocation of buffer and stack) More...
 
void run ()
 Enter the loop that will watch and operate the bus.
 
size_t available () const
 Get the amount of bytes in the receive buffer. More...
 
int read ()
 Read the next byte from the receiveBuffer. More...
 
void write (const uint8_t data)
 Send the given byte of data on the bus during a request from the bus master. More...
 

Static Public Member Functions

template<class T >
static int8_t invoke (T &runnable)
 Start a new cog running the given object. More...
 

Static Public Attributes

static const Pin::Mask DEFAULT_SCL_MASK = Pin::Mask::P28
 
static const Pin::Mask DEFAULT_SDA_MASK = Pin::Mask::P29
 

Detailed Description

Basic I2C slave driver.

Requires that the SDA and SCL pins have sufficient pull-ups. These should be selected based on the capacitance of the devices on the I2C bus, and the expected clock speed.

The driver supports restarts and only 7-bit addressing. The driver does not support clock stretching.

Warning
If the timeslot between start & restart, restart & restart, or stop and start is too small (depending on the master), a transmission might be completely lost, due to the onReceive callback taking too much time.
Examples
I2CSlave_Demo.cpp.

Definition at line 47 of file i2cslave.h.

Constructor & Destructor Documentation

◆ I2CSlave() [1/2]

template<size_t BUFFER_SIZE, size_t STACK_SIZE>
PropWare::I2CSlave::I2CSlave ( const uint8_t  address,
uint8_t(&)  buffer[BUFFER_SIZE],
const uint32_t(&)  stack[STACK_SIZE],
const Pin::Mask  sclMask = DEFAULT_SCL_MASK,
const Pin::Mask  sdaMask = DEFAULT_SDA_MASK 
)

Create an I2CSlave object (requires static allocation of buffer and stack)

Parameters
[in]addressAddress to join the bus as slave with
[in]sclMaskPin mask for the SCL pin
[in]sdaMaskPin mask for the SDA pin
[in]bufferReceive buffer to store messages as they arrive
[in]stackReserved stack space that can be used for a new cog to execute the run() method
<UserDataType>Type of the userData that can be set and is then passed to all callback functions
Warning
Providing a buffer that is too small will lead to received messages being truncated.
Examples
I2CSlave_Demo.cpp.

Definition at line 67 of file i2cslave.h.

◆ I2CSlave() [2/2]

PropWare::I2CSlave::I2CSlave ( const uint8_t  address,
uint8_t *  buffer,
const size_t  bufferSize,
const uint32_t *  stack,
const size_t  stackSize,
const Pin::Mask  sclMask = DEFAULT_SCL_MASK,
const Pin::Mask  sdaMask = DEFAULT_SDA_MASK 
)

Create an I2C slave object (Allows dynamic allocation of buffer and stack)

Parameters
[in]addressAddress to join the bus as slave with
[in]sclMaskPin mask for the SCL pin
[in]sdaMaskPin mask for the SDA pin
[in]bufferReceive buffer to store messages as they arrive
[in]bufferSizeSize of the receive buffer, that will hold a received message (-> maximal message size)
[in]stackReserved stack space that can be used for a new cog to execute the run() method
[in]stackSizeSize of the reserved stack (in measured in 32-bit words, not bytes)
Warning
Providing a buffer that is too small will lead to received messages being truncated.

Definition at line 92 of file i2cslave.h.

Member Function Documentation

◆ available()

size_t PropWare::I2CSlave::available ( ) const

Get the amount of bytes in the receive buffer.

Returns
The amount of bytes in the receive buffer

Definition at line 144 of file i2cslave.h.

◆ invoke()

template<class T >
static int8_t PropWare::Runnable::invoke ( T &  runnable)
staticinherited

Start a new cog running the given object.

Parameters
[in]runnableObject that should be invoked in a new cog
Returns
If the cog was successfully started, the new cog ID is returned. Otherwise, -1 is returned

Definition at line 85 of file runnable.h.

+ Here is the call graph for this function:

◆ read()

int PropWare::I2CSlave::read ( )

Read the next byte from the receiveBuffer.

Returns
The next byte from the receiveBuffer, -1 when none is available.
Examples
I2CSlave_Demo.cpp.

Definition at line 153 of file i2cslave.h.

◆ write()

void PropWare::I2CSlave::write ( const uint8_t  data)

Send the given byte of data on the bus during a request from the bus master.

Parameters
[in]dataByte to send to the requesting master.
Warning
Calling this method too late may result in a defective state of the i2c state machine.
Examples
I2CSlave_Demo.cpp.

Definition at line 167 of file i2cslave.h.

+ Here is the caller graph for this function:

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