Data Structures | Macros | Typedefs | Enumerations | Functions
I2CSlave.h File Reference

Detailed Description

Inter-Integrated Circuit (I2C) Slave Driver.


Overview

The I2C Slave driver allows you to send and recieve I2C transfers. This driver complements the I2C.h driver which operates as an I2C master.


Usage

This documentation provides a basic usage summary and a set of examples in the form of commented code fragments. Detailed descriptions of the APIs are provided in subsequent sections.

Synopsis

// Import I2C Slave Driver definitions
// Define name for an index of an I2C Slave
#define MASTER_BUS 0
// One-time init of I2C driver
// initialize optional I2C parameters
// Open I2C Slavefor usage
I2CSlave_Handle i2cHandle = I2CSlave_open(MASTER_BUS, &params);
// Wait for a write from an I2C master device
I2CSlave_read(i2cHandle, buffer, 4);
// Write to the I2C master device
I2CSlave_write(i2cHandle, buffer, 2);
// Close I2C
I2CSlave_close(i2cHandle);

Examples

Opening the driver

// Define name for an index of an I2C Slave
#define MASTER_BUS 0
// One-time init of I2C Slave driver
params.transferCallbackFxn = callbackFxn;
handle = I2CSlave_open(0, &params);
if (handle == NULL) {
// I2C Slave failed to open
while (1) {}
}

Transferring data

status = I2CSlave_read(handle, buffer, 5)
if (status == false) {
//Unsuccessful I2CSlave read
}
status = I2CSlave_write(handle, buffer, 3);
if (status == false) {
//Unsuccessful I2CSlave write
}

Configuration

Refer to the Driver's Configuration section for driver configuration information.


#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
Include dependency graph for I2CSlave.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  I2CSlave_Params
 I2CSlave parameters used with I2CSlave_open(). More...
 
struct  I2CSlave_FxnTable
 The definition of a I2CSlave function table that contains the required set of functions to control a specific I2CSlave driver implementation. More...
 
struct  I2CSlave_Config_
 I2C Slave driver's custom configuration structure. More...
 

Macros

#define I2CSLAVE_CMD_RESERVED   (32)
 
#define I2CSLAVE_STATUS_RESERVED   (-32)
 
#define I2CSLAVE_STATUS_SUCCESS   (0)
 Successful status code returned by I2CSlave_control(). More...
 
#define I2CSLAVE_STATUS_ERROR   (-1)
 Generic error status code returned by I2CSlave_control(). More...
 
#define I2CSLAVE_STATUS_UNDEFINEDCMD   (-2)
 An error status code returned by I2CSlave_control() for undefined command codes. More...
 

Typedefs

typedef struct I2CSlave_Config_I2CSlave_Handle
 A handle that is returned from a I2CSlave_open() call. More...
 
typedef void(* I2CSlave_CallbackFxn) (I2CSlave_Handle handle, bool status)
 The definition of a callback function. More...
 
typedef struct I2CSlave_Config_ I2CSlave_Config
 I2C Slave driver's custom configuration structure. More...
 

Enumerations

enum  I2CSlave_TransferMode { I2CSLAVE_MODE_BLOCKING, I2CSLAVE_MODE_CALLBACK }
 Return behavior of I2CSlave_write() and I2CSlave_read() specified in the I2CSlave_Params. More...
 

Functions

void I2CSlave_close (I2CSlave_Handle handle)
 Function to close an I2CSlave driver instance. More...
 
int_fast16_t I2CSlave_control (I2CSlave_Handle handle, uint_fast16_t cmd, void *arg)
 Function performs implementation specific features on a driver instance. More...
 
void I2CSlave_init (void)
 Function to initialize the I2C Slave driver. More...
 
I2CSlave_Handle I2CSlave_open (uint_least8_t index, I2CSlave_Params *params)
 Function to initialize the I2CSlave peripheral. More...
 
void I2CSlave_Params_init (I2CSlave_Params *params)
 Initialize an I2CSlave_Params structure to its default values. More...
 
bool I2CSlave_read (I2CSlave_Handle handle, void *buffer, size_t size)
 Perform an I2C read from an I2C master. More...
 
bool I2CSlave_write (I2CSlave_Handle handle, const void *buffer, size_t size)
 Perform an I2C write to an I2C master. More...
 

Typedef Documentation

§ I2CSlave_Handle

A handle that is returned from a I2CSlave_open() call.

§ I2CSlave_CallbackFxn

typedef void(* I2CSlave_CallbackFxn) (I2CSlave_Handle handle, bool status)

The definition of a callback function.

When operating in I2CSLAVE_MODE_CALLBACK, the callback function is called when a transfer completes. The application is responsible for declaring an I2CSlave_CallbackFxn function and providing a pointer in I2CSlave_Params.transferCallbackFxn.

Warning
The callback function is called from an interrupt context.
Parameters
[out]handleI2CSlave_Handle used with the initial call to I2CSlave_read() or I2CSlave_write()
[out]statusBoolean indicating if the I2C transfer was successful. If true, the transfer was successful. If false, the transfer failed.

§ I2CSlave_Config

I2C Slave driver's custom configuration structure.

See also
I2CSlave_init()
I2CSlave_open()

Enumeration Type Documentation

§ I2CSlave_TransferMode

Return behavior of I2CSlave_write() and I2CSlave_read() specified in the I2CSlave_Params.

See also
I2CSlave_Params
Enumerator
I2CSLAVE_MODE_BLOCKING 
I2CSLAVE_MODE_CALLBACK 

Function Documentation

§ I2CSlave_close()

void I2CSlave_close ( I2CSlave_Handle  handle)

Function to close an I2CSlave driver instance.

Precondition
I2CSlave_open() has to be called first.
Parameters
[in]handleAn I2CSlave_Handle returned from I2CSlave_open()

§ I2CSlave_control()

int_fast16_t I2CSlave_control ( I2CSlave_Handle  handle,
uint_fast16_t  cmd,
void *  arg 
)

Function performs implementation specific features on a driver instance.

Precondition
I2CSlave_open() has to be called first.
Parameters
[in]handleAn I2CSlave_Handle returned from I2CSlave_open()
[in]cmdA command value defined by the device specific implementation
[in]argAn optional R/W (read/write) argument that is accompanied with cmd
Returns
Implementation specific return codes. Negative values indicate unsuccessful operations.
Return values
I2CSLAVE_STATUS_UNDEFINEDCMDThe cmd value is not supported by the device specific implementation.

§ I2CSlave_init()

void I2CSlave_init ( void  )

Function to initialize the I2C Slave driver.

This function must also be called before any other I2CSlave driver APIs.

§ I2CSlave_open()

I2CSlave_Handle I2CSlave_open ( uint_least8_t  index,
I2CSlave_Params params 
)

Function to initialize the I2CSlave peripheral.

Function to initialize the I2CSlave peripheral specified by the particular index value.

Precondition
I2CSlave_init() has been called
Parameters
[in]indexIndex in the I2CSlave_Config[] array.
[in]paramsPointer to an initialized I2CSlave_Params structure. If NULL, the default I2CSlave_Params values are used.
Returns
An I2CSlave_Handle on success or NULL on error.
See also
I2CSlave_init()
I2CSlave_close()

§ I2CSlave_Params_init()

void I2CSlave_Params_init ( I2CSlave_Params params)

Initialize an I2CSlave_Params structure to its default values.

Parameters
[in]paramsA pointer to an I2CSlave_Params structure.

Defaults values are:

See also
I2CSlave_open()

§ I2CSlave_read()

bool I2CSlave_read ( I2CSlave_Handle  handle,
void *  buffer,
size_t  size 
)

Perform an I2C read from an I2C master.

This function will perform an I2C read transfer.

Note
When using I2CSLAVE_MODE_BLOCKING, this must be called from a thread context.

The data written by the I2CSlave is synchronized with the START and STOP condition from an I2C master.

Parameters
[in]handleAn I2CSlave_Handle returned from I2CSlave_open()
[in,out]bufferA pointer to buffer to store data read
[in]sizeThe number of bytes to read into buffer
Return values
trueon successful read
falseon an error
See also
I2CSlave_read
I2CSlave_TransferMode

§ I2CSlave_write()

bool I2CSlave_write ( I2CSlave_Handle  handle,
const void *  buffer,
size_t  size 
)

Perform an I2C write to an I2C master.

This function will perform an I2C write transfer.

Note
When using I2CSLAVE_MODE_BLOCKING, this must be called from a thread context.

The data written by the I2CSlave is synchronized with the START and STOP conditions from an I2C master.

Parameters
[in]handleAn I2CSlave_Handle
[in]bufferA pointer to buffer containing data to write
[in]sizeThe number of bytes in buffer to write. If the I2C master requests more than size bytes, this driver will transmit 0xFF until a STOP condition is recieved.
Return values
trueon successful write
falseon an error
See also
I2CSlave_write
I2CSlave_TransferMode
© Copyright 1995-2019, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale