4.9.2 I2C with Dual Mode Operation

I2C (also known as Two-Wire Interface (TWI)) with Dual Mode supporting simultaneous host and client operations

4.9.2.1 Introduction

MCC Melody I2C Driver with Host and Client Interfaces

The Inter-Integrated Circuit (I2C) bus is a multi-host serial data communication bus, supported by a Two Wire Interface (TWI) peripheral on AVR MCUs. Devices communicate in a host/client environment where the host devices initiate the communication. A client device is controlled through addressing.

4.9.2.2 Supported Device Families

AVR® DxAVR® ExATmega

4.9.2.3 Required header files:

For host mode, include the following files in your application to use the I2C abstract driver:

#include <stdbool.h>
#include <stdint.h>
#include "mcc_generated_files/i2c_host/i2c_host_interface.h"

For client mode, include the following files in your application to use the I2C abstract driver:

#include <stdbool.h>
#include <stdint.h>
#include "mcc_generated_files/i2c_client/i2c_client_interface.h"

4.9.2.5 Specific MISRA C:2012 Deviations

Global ISR (TWI0_TWIS_vect)

Advisory: misra-c2012-2.7  

Justification: False positive - ISR is a pre-processor macro defined in interrupt.h and is wrongly treated as a function by the CPP Checker.

Required: misra-c2012-5.5  

Justification: False positive - ISR is a pre-processor macro defined in interrupt.h file provided by XC8 compiler and is redefined in utils/interrupt_avr.h this file is out of scope for the MISRA check here but is considered by CPP Checker.

4.9.2.6 Module Documentation

4.9.2.6.1 I2C_CLIENT

This header file provides the prototypes for the portable firmware interfaces for I2C client.

Module description

This header file provides the prototypes for the portable firmware interfaces for I2C client.

Version: I2C Client Interface Version 1.0.4 TWI0 Driver Version 2.1.3 TWI0 Package Version 8.1.6
Data structures
Enumerations
Functions

Function Documentation

ISR()

ISR (TWI0_TWIS_vect )

Common Interrupt Service Routine (ISR) for all TWI0 interrupts. Initialize TWI0 with TWI0_Initialize() before calling this API.  

This API calls the TWI0_ErrorEventHandler() if an error flag is set, else calls TWI0_EventHandler() for other TWI transmit and receive events.

MISRA C:2012 Deviation Advisory: misra-c2012-2.7  

Justification: False positive - ISR is a pre-processor macro defined in interrupt.h and is wrongly treated as a function by the CPP Checker.

@misradeviation{Required:,8.2,Required:,8.4} False positive - ISR is a pre-processor macro defined in interrupt.h. The CPP Checker wrongly treats ISR as a function and expects a declaration to be available.

MISRA C:2012 Deviation Required: misra-c2012-5.5  

Justification: False positive - ISR is a pre-processor macro defined in interrupt.h file provided by XC8 compiler and is redefined in utils/interrupt_avr.h this file is out of scope for the MISRA check here but is considered by CPP Checker.

Parameters:
None.
Returns:

None.  

TWI0_CallbackRegister()

void TWI0_CallbackRegister (bool(*)(i2c_client_transfer_event_t clientEvent) callback)

Registers a callback function.

Parameters:
callback

- Pointer to custom callback.

Returns:

None.

TWI0_Deinitialize()

void TWI0_Deinitialize (void )

Resets the registers to their default Power-on Reset (POR) values.

Parameters:
None.
Returns:

None.  

TWI0_ErrorGet()

i2c_client_error_t TWI0_ErrorGet (void )

Retrieves the type of error that occurred during I2C communication. Ensure TWI0 is initialized using TWI0_Initialize() before invoking this function.

Parameters:
None.
Returns:

I2C_CLIENT_ERROR_BUS_COLLISION - I2C Bus Collision Error  

I2C_CLIENT_ERROR_WRITE_COLLISION - I2C Write Collision Error  

I2C_CLIENT_ERROR_RECEIVE_OVERFLOW - I2C Receive overflow  

I2C_CLIENT_ERROR_NONE - No Error

TWI0_Initialize()

void TWI0_Initialize (void )

Initializes the TWI0 client module according to the settings specified in the MPLAB® Code Configurator Melody UI.

Parameters:
None.
Returns:

None.  

TWI0_LastByteAckStatusGet()

i2c_client_ack_status_t TWI0_LastByteAckStatusGet (void )

Retrieves the acknowledgement status from host. Ensure TWI0 is initialized using TWI0_Initialize() before invoking this function.

Parameters:
None.
Returns:

I2C_CLIENT_ACK_STATUS_RECEIVED_ACK - An ACK was received for the most recent transaction with the host  

I2C_CLIENT_ACK_STATUS_RECEIVED_NACK - A NACK was received for the most recent transaction with the host  

TWI0_ReadAddr()

uint16_t TWI0_ReadAddr (void )

Retrieves the most recently matched address received from the host. Ensure TWI0 client is initialized using TWI0_Initialize() before invoking this function.

Parameters:
None.
Returns:

address byte - Last Matched address for TWI0 client

TWI0_ReadByte()

uint8_t TWI0_ReadByte (void )

Reads data from a host on the bus. Ensure TWI0 is initialized using TWI0_Initialize() before invoking this function.

Parameters:
None.
Returns:

uint8_t - A byte read by TWI0 client

TWI0_TransferDirGet()

i2c_client_transfer_dir_t TWI0_TransferDirGet (void )

Retrieves the data transfer direction. Ensure TWI0 is initialized using TWI0_Initialize() before invoking this function.

Parameters:
None.
Returns:

I2C_CLIENT_TRANSFER_DIR_WRITE - I2C client writes to host  

I2C_CLIENT_TRANSFER_DIR_READ - I2C client reads from host  

TWI0_WriteByte()

void TWI0_WriteByte (uint8_t data)

Sends data to a host on the bus. Ensure TWI0 is initialized using TWI0_Initialize() before invoking this function.

Parameters:
in data

- A byte that will be written by TWI0

Returns:

None.  

Enumeration Type Documentation

i2c_client_ack_status_t

enum i2c_client_ack_status_t

Enumeration for acknowledgment and non-acknowledgment status.

I2C_CLIENT_ACK_STATUS_RECEIVED_ACK

Host sent an ACK to I2C client

I2C_CLIENT_ACK_STATUS_RECEIVED_NACK

Host sent a NACK to I2C client

i2c_client_error_t

enum i2c_client_error_t

Enumeration that specifies the possible errors in the I2C client driver.

I2C_CLIENT_ERROR_NONE

Indicates no error

I2C_CLIENT_ERROR_BUS_ERROR

Indicates an illegal bus operation

I2C_CLIENT_ERROR_COLLISION

Indicates an error due to bus collision

i2c_client_transfer_dir_t

enum i2c_client_transfer_dir_t

Enumeration to determine the direction of data transfer initiated by the host, as determined by the I2Cx_TransferDirGet function.

I2C_CLIENT_TRANSFER_DIR_WRITE

I2C client is writing to host

I2C_CLIENT_TRANSFER_DIR_READ

I2C client is reading from host

i2c_client_transfer_event_t

enum i2c_client_transfer_event_t

Enumeration defining the various I2C event types.

I2C_CLIENT_TRANSFER_EVENT_NONE

Event indicating that the I2C bus is in an idle state

I2C_CLIENT_TRANSFER_EVENT_ADDR_MATCH

Event indicating that the I2C client has received a matching address

I2C_CLIENT_TRANSFER_EVENT_RX_READY

Event indicating that the I2C client is prepared to receive data from the host

I2C_CLIENT_TRANSFER_EVENT_TX_READY

Event indicating that the I2C client is ready to transmit data to the host

I2C_CLIENT_TRANSFER_EVENT_STOP_BIT_RECEIVED

Event indicating that the I2C client has received a stop bit

I2C_CLIENT_TRANSFER_EVENT_ERROR

Event indicating an error occurred on the I2C bus

4.9.2.6.2 TWI0_CLIENT_INTERRUPT

This header file includes API prototypes and various data types for the TWI0 client driver.

Version: TWI0 Driver Version 2.1.3 TWI0 Package Version 8.1.6

4.9.2.6.3 I2C_HOST

This header file provides the prototypes for the portable firmware interfaces for I2C host.

Module description

This header file provides the prototypes for the portable firmware interfaces for I2C host.

This header file includes API prototypes and various data types for the TWI0 host driver.

Version: I2C Host Interface Version 2.0.5 TWI0 Driver Version 2.1.3 TWI0 Package Version 8.1.6
Functions
  • ISR (TWI0_TWIM_vect)

    Common Interrupt Service Routine (ISR) for all TWI0 interrupts. Initialize TWI0 with TWI0_Initialize() before calling this API.  

    This API calls the TWI0_ErrorEventHandler() if an error flag is set, otherwise it calls TWI0_EventHandler() for other TWI transmit and receive events.

  • void TWI0_ErrorCallbackRegister (void(*handler)(void))

    Setter function for the TWI error interrupt callback.

  • void TWI0_Initialize() (void)

    Initializes the TWI0 host module according to the settings specified in the MPLAB® Code Configurator Melody UI.

  • void TWI0_Deinitialize (void)

    Resets the registers to their default Power-on Reset (POR) values.

  • bool TWI0_Write (uint16_t address, uint8_t *data, size_t dataLength)

    Sends data to a client on the bus. Ensure TWI0 host is initialized using TWI0_Initialize() before invoking this function.

  • bool TWI0_Read (uint16_t address, uint8_t *data, size_t dataLength)

    Reads data from an I2C client on the bus. Ensure TWI0 host is initialized using TWI0_Initialize() before invoking this function.

  • bool TWI0_WriteRead (uint16_t address, uint8_t *writeData, size_t writeLength, uint8_t *readData, size_t readLength)

    Initiates a data transfer by writing data from the writeData function to the bus and subsequently reads data from the Client, storing the received data in the readData parameter. The function generates a Start condition on the bus and transmits the specified number of bytes (writeLength) from the writeData buffer. Following this, a Repeated Start condition is inserted, and the function proceeds to read the specified number of bytes (readLength) from the client, storing them in the readData buffer. A Stop condition is generated after the final byte is received.

  • i2c_host_error_t TWI0_ErrorGet (void)

    Retrieves the error that occurred during I2C transmission and reception. Ensure TWI0 host is initialized using TWI0_Initialize() before invoking this function.  

  • bool TWI0_IsBusy (void)

    Determines if the I2C bus is currently busy. Ensure TWI0 host is initialized using TWI0_Initialize() before invoking this function.  

  • void TWI0_CallbackRegister (void(*callbackHandler)(void))

    Registers a callback function.

  • void TWI0_Tasks (void)

    Task to be polled for I2C host events. Ensure TWI0 host is initialized using TWI0_Initialize() before invoking this function.

Function Documentation

ISR()

ISR (TWI0_TWIM_vect )

Common Interrupt Service Routine (ISR) for all TWI0 interrupts. Initialize TWI0 with TWI0_Initialize() before calling this API.  

This API calls the TWI0_ErrorEventHandler() if an error flag is set, otherwise it calls TWI0_EventHandler() for other TWI transmit and receive events.

MISRA C:2012 Deviation Advisory: misra-c2012-2.7  

Justification: False positive - ISR is a pre-processor macro, defined in interrupt., and is wrongly treated as a function by the CPP Checker.

@misradeviation{Required:,8.2,Required:,8.4} False positive - ISR is a pre-processor macro defined in interrupt.h. The CPP Checker wrongly treats ISR as a function and expects a declaration to be available.

MISRA C:2012 Deviation Required: misra-c2012-5.5  

Justification: False positive - ISR is a pre-processor macro, defined in interrupt.h file provided by XC8 compiler and is redefined in utils/interrupt_avr.h this file is out of scope for the MISRA check here but is considered by CPP Checker.

Parameters:
None.
Returns:

None.  

TWI0_CallbackRegister()

void TWI0_CallbackRegister (void(*)(void) callbackHandler)

Registers a callback function.

Parameters:
handler

- Pointer to custom callback

Returns:

None.

TWI0_Deinitialize()

void TWI0_Deinitialize (void )

Resets the registers to their default Power-on Reset (POR) values.

Parameters:
None.
Returns:

None.  

TWI0_ErrorCallbackRegister()

void TWI0_ErrorCallbackRegister (void(*)(void) handler)

Setter function for the TWI error interrupt callback.

Parameters:
*handler

- Pointer to the custom callback function

Returns:

None.  

TWI0_ErrorGet()

i2c_host_error_t TWI0_ErrorGet (void )

Retrieves the error that occurred during I2C transmission and reception. Ensure TWI0 host is initialized using TWI0_Initialize() before invoking this function.  

Parameters:
None.
Returns:

I2C_CLIENT_ERROR_BUS_COLLISION - I2C Bus Collision Error  

I2C_CLIENT_ERROR_WRITE_COLLISION - I2C Write Collision Error  

I2C_CLIENT_ERROR_RECEIVE_OVERFLOW - I2C Receive overflow  

I2C_CLIENT_ERROR_NONE - No Error

TWI0_Initialize()

void TWI0_Initialize (void )

Initializes the TWI0 host module according to the settings specified in the MPLAB® Code Configurator Melody UI.

Parameters:
None.
Returns:

None.  

TWI0_IsBusy()

bool TWI0_IsBusy (void )

Determines if the I2C bus is currently busy. Ensure TWI0 host is initialized using TWI0_Initialize() before invoking this function.  

Parameters:
None.
Returns:

True - I2C bus busy

False - I2C bus is idle

TWI0_Read()

bool TWI0_Read (uint16_t address, uint8_t * data, size_t dataLength)

Reads data from an I2C client on the bus. Ensure TWI0 host is initialized using TWI0_Initialize() before invoking this function.

This function attempts to read a specified number of bytes received from the I2C client with the given address into the data buffer. The I2C Host initiates a Start condition, reads the data, and then generates a Stop condition. If the Client NAKs the request or a bus error occurs, the transfer is terminated. The application can call the TWI0_ErrorGet() function to determine the cause of the error.

This function is non-blocking; it starts the bus activity and returns immediately. The transfer is completed in the peripheral interrupt. In Polling mode, the user must call TWI0_Tasks within the while loop. A new transfer request cannot be initiated while another transfer is in progress. Calling this function during an ongoing transfer will result in the function returning false.

Parameters:
in address

- 7-bit or 10-bit client address

out data

- Pointer to the destination buffer where the received data will be stored

in dataLength

- The total number of bytes to be received

Returns:

True - The request was successfully placed, and bus activity has started

False - The request failed as another transfer was already in progress when this function was called

TWI0_Tasks()

void TWI0_Tasks (void )

Task to be polled for I2C host events. Ensure TWI0 host is initialized using TWI0_Initialize() before invoking this function.

Parameters:
None.
Returns:

None.  

TWI0_Write()

bool TWI0_Write (uint16_t address, uint8_t * data, size_t dataLength)

Sends data to a client on the bus. Ensure TWI0 host is initialized using TWI0_Initialize() before invoking this function.

This function attempts to write a specified number of bytes from the data buffer to a I2C client, identified by the provided address.

The TWI host will generate a Start condition, transmit the data, and then generate a Stop condition. If the client device responds with a NAK or if a bus error occurs, the transfer will be terminated. The application can call the TWI0_ErrorGet() function to determine the cause of the error. This function is non-blocking, meaning it initiates the bus activity and returns immediately. The transfer is then completed within the peripheral interrupt. In Polling mode, the user must call TWI0_Tasks within the while loop. A new transfer request cannot be initiated while another transfer is in progress. If this function is called while another transfer is ongoing, it will return false.

Parameters:
in address

- 7-bit or 10-bit client address

in data

- Pointer to the buffer holding the data to be sent

in dataLength

- The total number of bytes to be sent

Returns:

True - The request was successfully placed and bus activity has started

False - The request failed as another transfer was already in progress when this function was called

TWI0_WriteRead()

bool TWI0_WriteRead (uint16_t address, uint8_t * writeData, size_t writeLength, uint8_t * readData, size_t readLength)

Initiates a data transfer by writing data from the writeData function to the bus and subsequently reads data from the Client, storing the received data in the readData parameter. The function generates a Start condition on the bus and transmits the specified number of bytes (writeLength) from the writeData buffer. Following this, a Repeated Start condition is inserted, and the function proceeds to read the specified number of bytes (readLength) from the client, storing them in the readData buffer. A Stop condition is generated after the final byte is received.

If the Client responds with a NAK or a bus error occurs, the transfer is terminated. The application can call the TWI0_ErrorGet() function to determine the cause of the error.

This function is non-blocking; it initiates the bus activity and returns immediately. The transfer is then completed via the peripheral interrupt. In Polling mode, the user must call TWI0_Tasks within the while loop. A new transfer request cannot be initiated while another transfer is in progress. Calling this function during an ongoing transfer will result in the function returning false.

Parameters:
in address

- 7-bit or 10-bit client address

in writeData

- Pointer to the buffer holding the data to be sent

in writeLength

- The total number of bytes to be sent

out readData

- Pointer to the destination buffer where the received data will be stored

in readLength

- The total number of bytes to be received

Returns:

True - The request was successfully placed, and bus activity has started

False - The request failed as another transfer was already in progress when this function was called

Enumeration Type Documentation

i2c_event_states_t

enum enum i2c_event_states_t

Enumeration representing the states of host events.

I2C_STATE_IDLE

State indicates that the I2C bus is in an idle state

I2C_STATE_SEND_RD_ADDR

State indicates the transmission of the address for a read operation

I2C_STATE_SEND_WR_ADDR

State indicates the transmission of the address for a write operation

I2C_STATE_TX

State indicates the data transmission

I2C_STATE_RX

State indicates the data reception

I2C_STATE_NACK

State indicates that a negative acknowledgment was received from the I2C client

I2C_STATE_ERROR

State indicates an error

I2C_STATE_STOP

State indicates stop operation

I2C_STATE_RESET

State indicates the reset event

i2c_host_error_t

enum enum i2c_host_error_t

Enumeration for the possible errors in the I2C host driver.

I2C_ERROR_NONE

Indicates no error

I2C_ERROR_ADDR_NACK

Indicates that the I2C client did not acknowledge the address

I2C_ERROR_DATA_NACK

Indicates that the I2C client did not acknowledge the data

I2C_ERROR_BUS_COLLISION

Indicates an error caused due to bus collision

4.9.2.6.4 TWI0_HOST_INTERRUPT

This header file includes API prototypes and various data types for the TWI0 host driver.

Module description

This header file includes API prototypes and various data types for the TWI0 host driver.

Version: TWI0 Driver Version 2.1.3 TWI0 Package Version 8.1.6
Functions
  • void TWI0_Initialize (void)

    Initializes the TWI0 host module according to the settings specified in the MPLAB® Code Configurator Melody UI.

  • void TWI0_Deinitialize (void)

    Resets the registers to their default Power-on Reset (POR) values.

  • bool TWI0_Write (uint16_t address, uint8_t *data, size_t dataLength)

    Sends data to a client on the bus. Ensure TWI0 host is initialized using TWI0_Initialize() before invoking this function.

  • bool TWI0_Read (uint16_t address, uint8_t *data, size_t dataLength)

    Reads data from an I2C client on the bus. Ensure TWI0 host is initialized using TWI0_Initialize() before invoking this function.

  • bool TWI0_WriteRead (uint16_t address, uint8_t *writeData, size_t writeLength, uint8_t *readData, size_t readLength)

    Initiates a data transfer by writing data from the writeData function to the bus and subsequently reads data from the Client, storing the received data in the readData parameter. The function generates a Start condition on the bus and transmits the specified number of bytes (writeLength) from the writeData buffer. Following this, a Repeated Start condition is inserted, and the function proceeds to read the specified number of bytes (readLength) from the client, storing them in the readData buffer. A Stop condition is generated after the final byte is received.

  • i2c_host_error_t TWI0_ErrorGet (void)

    Retrieves the error that occurred during I2C transmission and reception. Ensure TWI0 host is initialized using TWI0_Initialize() before invoking this function.  

  • bool TWI0_IsBusy (void)

    Determines if the I2C bus is currently busy. Ensure TWI0 host is initialized using TWI0_Initialize() before invoking this function.  

  • void TWI0_CallbackRegister (void(*callbackHandler)(void))

    Registers a callback function.

Function Documentation

TWI0_CallbackRegister()

void TWI0_CallbackRegister (void(*)(void) callbackHandler)

Registers a callback function.

Parameters:
handler

- Pointer to custom callback

Returns:

None.

TWI0_Deinitialize()

void TWI0_Deinitialize (void )

Resets the registers to their default Power-on Reset (POR) values.

Parameters:
None.
Returns:

None.  

TWI0_ErrorGet()

i2c_host_error_t TWI0_ErrorGet (void )

Retrieves the error that occurred during I2C transmission and reception. Ensure TWI0 host is initialized using TWI0_Initialize() before invoking this function.  

Parameters:
None.
Returns:

I2C_ERROR_NONE - Indicates no error  

I2C_ERROR_ADDR_NACK - Indicates that the I2C client did not acknowledge the address  

I2C_ERROR_DATA_NACK - Indicates that the I2C client did not acknowledge the data  

I2C_ERROR_BUS_COLLISION - Indicates an error caused due to bus collision  

Retrieves the error that occurred during I2C transmission and reception. Ensure TWI0 host is initialized using TWI0_Initialize() before invoking this function.  

Parameters:
None.
Returns:

I2C_CLIENT_ERROR_BUS_COLLISION - I2C Bus Collision Error  

I2C_CLIENT_ERROR_WRITE_COLLISION - I2C Write Collision Error  

I2C_CLIENT_ERROR_RECEIVE_OVERFLOW - I2C Receive overflow  

I2C_CLIENT_ERROR_NONE - No Error

TWI0_Initialize()

void TWI0_Initialize (void )

Initializes the TWI0 host module according to the settings specified in the MPLAB® Code Configurator Melody UI.

Parameters:
None.
Returns:

None.  

TWI0_IsBusy()

bool TWI0_IsBusy (void )

Determines if the I2C bus is currently busy. Ensure TWI0 host is initialized using TWI0_Initialize() before invoking this function.  

Parameters:
None.
Returns:

True - I2C bus busy

False - I2C bus is idle

TWI0_Read()

bool TWI0_Read (uint16_t address, uint8_t * data, size_t dataLength)

Reads data from an I2C client on the bus. Ensure TWI0 host is initialized using TWI0_Initialize() before invoking this function.

This function attempts to read a specified number of bytes received from the I2C client with the given address into the data buffer. The I2C Host initiates a Start condition, reads the data, and then generates a Stop condition. If the Client NAKs the request or a bus error occurs, the transfer is terminated. The application can call the TWI0_ErrorGet() function to determine the cause of the error.

This function is non-blocking; it starts the bus activity and returns immediately. The transfer is completed in the peripheral interrupt. In Polling mode, the user must call TWI0_Tasks within the while loop. A new transfer request cannot be initiated while another transfer is in progress. Calling this function during an ongoing transfer will result in the function returning false.

Parameters:
in address

- 7-bit or 10-bit client address

out data

- Pointer to the destination buffer where the received data will be stored

in dataLength

- The total number of bytes to be received

Returns:

True - The request was successfully placed, and bus activity has started

False - The request failed as another transfer was already in progress when this function was called

TWI0_Write()

bool TWI0_Write (uint16_t address, uint8_t * data, size_t dataLength)

Sends data to a client on the bus. Ensure TWI0 host is initialized using TWI0_Initialize() before invoking this function.

This function attempts to write a specified number of bytes from the data buffer to a I2C client, identified by the provided address.

The TWI host will generate a Start condition, transmit the data, and then generate a Stop condition. If the client device responds with a NAK or if a bus error occurs, the transfer will be terminated. The application can call the TWI0_ErrorGet() function to determine the cause of the error. This function is non-blocking, meaning it initiates the bus activity and returns immediately. The transfer is then completed within the peripheral interrupt. In Polling mode, the user must call TWI0_Tasks within the while loop. A new transfer request cannot be initiated while another transfer is in progress. If this function is called while another transfer is ongoing, it will return false.

Parameters:
in address

- 7-bit or 10-bit client address

in data

- Pointer to the buffer holding the data to be sent

in dataLength

- The total number of bytes to be sent

Returns:

True - The request was successfully placed and bus activity has started

False - The request failed as another transfer was already in progress when this function was called

TWI0_WriteRead()

bool TWI0_WriteRead (uint16_t address, uint8_t * writeData, size_t writeLength, uint8_t * readData, size_t readLength)

Initiates a data transfer by writing data from the writeData function to the bus and subsequently reads data from the Client, storing the received data in the readData parameter. The function generates a Start condition on the bus and transmits the specified number of bytes (writeLength) from the writeData buffer. Following this, a Repeated Start condition is inserted, and the function proceeds to read the specified number of bytes (readLength) from the client, storing them in the readData buffer. A Stop condition is generated after the final byte is received.

If the Client responds with a NAK or a bus error occurs, the transfer is terminated. The application can call the TWI0_ErrorGet() function to determine the cause of the error.

This function is non-blocking; it initiates the bus activity and returns immediately. The transfer is then completed via the peripheral interrupt. In Polling mode, the user must call TWI0_Tasks within the while loop. A new transfer request cannot be initiated while another transfer is in progress. Calling this function during an ongoing transfer will result in the function returning false.

Parameters:
in address

- 7-bit or 10-bit client address

in writeData

- Pointer to the buffer holding the data to be sent

in writeLength

- The total number of bytes to be sent

out readData

- Pointer to the destination buffer where the received data will be stored

in readLength

- The total number of bytes to be received

Returns:

True - The request was successfully placed, and bus activity has started

False - The request failed as another transfer was already in progress when this function was called

4.9.2.7 Data Structure Documentation

4.9.2.7.1 i2c_client_interface_t Struct Reference

This structure contains the portable interfaces for I2C client.

Detailed Description

This structure contains the portable interfaces for I2C client.

#include <i2c_client_interface.h>

Data Fields
  • void(* Initialize )(void)

    Pointer to the function that initializes the I2C client.

  • void(* Deinitialize )(void)

    Pointer to the function that deinitializes the I2C client.

  • void(* WriteByte )(uint8_t data)

    Pointer to the function that sends data to a host on bus.

  • uint8_t(* ReadByte )(void)

    Pointer to the function that reads data from a host on bus.

  • i2c_client_error_t(* ErrorGet )(void)

    Pointer to the function that returns the last error that occurred on the I2C bus.

  • i2c_client_transfer_dir_t(* TransferDirGet )(void)

    Pointer to the function that returns the direction of data transfer between client and a host on bus.

  • i2c_client_ack_status_t(* LastByteAckStatusGet )(void)

    Pointer to the function that retrieves the acknowledgment status of the last byte transferred.

  • void(* CallbackRegister )(bool(*handler)(i2c_client_transfer_event_t clientEvent))

    Pointer to the function that registers a callback handler for I2C client.

  • void(* Tasks )(void)

    Pointer to the function that is polled for I2C client events.

Field Documentation

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

source/interrupt/i2c_client/

i2c_client_interface.h

CallbackRegister

void(* CallbackRegister

Pointer to the function that registers a callback handler for I2C client.

Deinitialize

void(* Deinitialize

Pointer to the function that deinitializes the I2C client.

ErrorGet

i2c_client_error_t(* ErrorGet

Pointer to the function that returns the last error that occurred on the I2C bus.

Initialize

void(* Initialize

Pointer to the function that initializes the I2C client.

LastByteAckStatusGet

i2c_client_ack_status_t(* LastByteAckStatusGet

Pointer to the function that retrieves the acknowledgment status of the last byte transferred.

ReadByte

uint8_t(* ReadByte

Pointer to the function that reads data from a host on bus.

Tasks

void(* Tasks

Pointer to the function that is polled for I2C client events.

TransferDirGet

i2c_client_transfer_dir_t(* TransferDirGet

Pointer to the function that returns the direction of data transfer between client and a host on bus.

WriteByte

void(* WriteByte

Pointer to the function that sends data to a host on bus.

4.9.2.7.2 i2c_event_status_t Struct Reference

#include <i2c_host_event_types.h>

Field Documentation

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

source/interrupt/i2c_host/

i2c_host_event_types.h

address

uint16_t address

I2C client address

busy

bool busy

A flag that signifies the host is currently busy

errorState

i2c_host_error_t errorState

An instance of i2c_host_error_t

readLength

size_t readLength

Number of bytes to be read from the client

readPtr

uint8_t * readPtr

Pointer to the buffer for received data

state

i2c_event_states_t state

An instance of i2c_host_event_states_t

switchToRead

bool switchToRead

Boolean flag to indicate switching to read mode

writeLength

size_t writeLength

The number of bytes to be written or sent to the client

writePtr

uint8_t * writePtr

Pointer to the buffer used for transmission

4.9.2.7.3 i2c_host_interface_t Struct Reference

This structure contains the portable interfaces for I2C host.

Detailed Description

This structure contains the portable interfaces for I2C host.

#include <i2c_host_interface.h>

Data Fields
  • void(* Initialize )(void)

    Pointer to the function that initializes the I2C host, setting up any necessary hardware or software resources.

  • void(* Deinitialize )(void)

    Pointer to the function that deinitializes the I2C host, by resetting the I2C registers to Power On Default (POR) values.

  • bool(* Write )(uint16_t address, uint8_t *data, size_t dataLength)

    Pointer to the write function of the I2C host.

  • bool(* Read )(uint16_t address, uint8_t *data, size_t dataLength)

    Pointer to the read function of the I2C host.

  • bool(* WriteRead )(uint16_t address, uint8_t *writeData, size_t writeLength, uint8_t *readData, size_t readLength)

    Pointer to the write and read function of the I2C host.

  • bool(* TransferSetup )(struct i2c_transfer_setup *setup, uint32_t srcClkFreq)

    Pointer to the function that sets up the transfer parameters of the I2C host.

  • i2c_host_error_t(* ErrorGet )(void)

    Pointer to the function that returns the last error that occurred on the I2C bus.

  • bool(* IsBusy )(void)

    Pointer to the function that checks if the I2C bus is busy.

  • void(* CallbackRegister )(void(*callback)(void))

    Pointer to the function that Registers a callback function for I2C events.

  • void(* Tasks )(void)

    Pointer to the function that is polled for I2C host events.

Field Documentation

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

source/interrupt/i2c_host/

i2c_host_interface.h

CallbackRegister

void(* CallbackRegister

Pointer to the function that Registers a callback function for I2C events.

Deinitialize

void(* Deinitialize

Pointer to the function that deinitializes the I2C host, by resetting the I2C registers to Power On Default (POR) values.

ErrorGet

i2c_host_error_t(* ErrorGet

Pointer to the function that returns the last error that occurred on the I2C bus.

Initialize

void(* Initialize

Pointer to the function that initializes the I2C host, setting up any necessary hardware or software resources.

IsBusy

bool(* IsBusy

Pointer to the function that checks if the I2C bus is busy.

Read

bool(* Read

Pointer to the read function of the I2C host.

Tasks

void(* Tasks

Pointer to the function that is polled for I2C host events.

TransferSetup

bool(* TransferSetup

Pointer to the function that sets up the transfer parameters of the I2C host.

Write

bool(* Write

Pointer to the write function of the I2C host.

WriteRead

bool(* WriteRead

Pointer to the write and read function of the I2C host.

4.9.2.7.4 i2c_transfer_setup Struct Reference

Structure defining I2C clock configuration.

Detailed Description

Structure defining I2C clock configuration.

@misradeviation{Advisory:,2.4,Advisory:,2.3} These declarations are provided by the Melody-generated drivers for the user’s application. The user has the discretion to implement these declarations in their application.

#include <i2c_host_types.h>

Field Documentation

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

source/interrupt/i2c_host/

i2c_host_types.h

clkSpeed

uint32_t clkSpeed

4.9.2.8 File Documentation

4.9.2.8.1 source/interrupt/i2c_client/i2c_client_interface.h File Reference

#include <stdbool.h>
#include <stdint.h>
#include "i2c_client_types.h"

Data structures

Detailed Description

A header file generated by I2C client driver

4.9.2.8.2 source/polling/i2c_client/i2c_client_interface.h File Reference

#include <stdbool.h>
#include <stdint.h>
#include "i2c_client_types.h"

Data structures

Detailed Description

A header file generated by I2C client driver

4.9.2.8.3 source/interrupt/i2c_client/i2c_client_types.h File Reference

Enumerations

4.9.2.8.4 source/polling/i2c_client/i2c_client_types.h File Reference

Enumerations

Enumeration Type Documentation

i2c_client_ack_status_t

enum i2c_client_ack_status_t

I2C_CLIENT_ACK_STATUS_RECEIVED_ACK

Host sent an ACK to I2C client

I2C_CLIENT_ACK_STATUS_RECEIVED_NACK

Host sent a NACK to I2C client

i2c_client_error_t

enum i2c_client_error_t

I2C_CLIENT_ERROR_NONE

Indicates no error

I2C_CLIENT_ERROR_BUS_ERROR

Indicates an illegal bus operation

I2C_CLIENT_ERROR_COLLISION

Indicates an error due to bus collision

i2c_client_transfer_dir_t

enum i2c_client_transfer_dir_t

I2C_CLIENT_TRANSFER_DIR_WRITE

I2C client is writing to host

I2C_CLIENT_TRANSFER_DIR_READ

I2C client is reading from host

i2c_client_transfer_event_t

enum i2c_client_transfer_event_t

I2C_CLIENT_TRANSFER_EVENT_NONE

Event indicating that the I2C bus is in an idle state

I2C_CLIENT_TRANSFER_EVENT_ADDR_MATCH

Event indicating that the I2C client has received a matching address

I2C_CLIENT_TRANSFER_EVENT_RX_READY

Event indicating that the I2C client is prepared to receive data from the host

I2C_CLIENT_TRANSFER_EVENT_TX_READY

Event indicating that the I2C client is ready to transmit data to the host

I2C_CLIENT_TRANSFER_EVENT_STOP_BIT_RECEIVED

Event indicating that the I2C client has received a stop bit

I2C_CLIENT_TRANSFER_EVENT_ERROR

Event indicating an error occurred on the I2C bus

I2C_CLIENT_TRANSFER_EVENT_NONE

Event indicating that the I2C bus is in an idle state

I2C_CLIENT_TRANSFER_EVENT_ADDR_MATCH

Event indicating that the I2C client has received a matching address

I2C_CLIENT_TRANSFER_EVENT_RX_READY

Event indicating that the I2C client is prepared to receive data from the host

I2C_CLIENT_TRANSFER_EVENT_TX_READY

Event indicating that the I2C client is ready to transmit data to the host

I2C_CLIENT_TRANSFER_EVENT_STOP_BIT_RECEIVED

Event indicating that the I2C client has received a stop bit

I2C_CLIENT_TRANSFER_EVENT_ERROR

Event indicating an error occurred on the I2C bus

4.9.2.8.5 source/interrupt/i2c_client/src/twi0.c File Reference

#include "../twi0.h"
#include <stdbool.h>
#include "../../system/utils/compiler.h"

Functions

Detailed Description

A source file generated by TWI0 I2C client driver.

This file provides API implementation for the TWI0 driver.

Version: TWI0 Driver Version 2.1.3 TWI0 Package Version 8.1.6

Function Documentation

TWI0_ErrorEventHandler()

static void TWI0_ErrorEventHandler (void )[static]

TWI0_EventHandler()

static void TWI0_EventHandler (void )[static]

Variable Documentation

I2C2_Client

const i2c_client_interface_t I2C2_Client

Initial value:

= 
{
    .Initialize = TWI0_Initialize,
    .Deinitialize = TWI0_Deinitialize,
    .WriteByte = TWI0_WriteByte,
    .ReadByte = TWI0_ReadByte,
    .TransferDirGet = TWI0_TransferDirGet,
    .LastByteAckStatusGet = TWI0_LastByteAckStatusGet,
    .ErrorGet = TWI0_ErrorGet,
    .CallbackRegister = TWI0_CallbackRegister,
    .Tasks = NULL
}

Represents an instance of the portable interface structure designed for TWI0 client.

@misradeviation {Advisory:,8.7} False postive - A few MCC generated driver APIs are made available to users through external linkage. This rule ID has been disabled at the project level due to numerous instances across various files. Consequently, in the application project, this rule ID should be disabled in the MPLAB-X IDE by navigating to Tools -> Options -> Embedded -> MISRA Check.

TWI0_InterruptHandler

bool(* TWI0_InterruptHandler) (i2c_client_transfer_event_t clientEvent)[static]

twi0ErrorState

volatile i2c_client_error_t twi0ErrorState[static]

twi0matchedAddress

volatile uint16_t twi0matchedAddress[static]

4.9.2.8.6 source/interrupt/i2c_host/src/twi0.c File Reference

This file contains the API implementation for TWI0 driver.

#include "../twi0.h"
#include <stdbool.h>
#include <stdlib.h>
#include "../../system/utils/compiler.h"

Functions

Detailed Description

This file contains the API implementation for TWI0 driver.

A source file generated by TWI0 I2C host driver.

Version: TWI0 Driver Version 2.1.3 TWI0 Package Version 8.1.6

Function Documentation

I2C_EVENT_ERROR()

static i2c_event_states_t I2C_EVENT_ERROR (void )[static]

I2C_EVENT_IDLE()

static i2c_event_states_t I2C_EVENT_IDLE (void )[static]

I2C_EVENT_NACK()

static i2c_event_states_t I2C_EVENT_NACK (void )[static]

I2C_EVENT_RESET()

static i2c_event_states_t I2C_EVENT_RESET (void )[static]

I2C_EVENT_RX()

static i2c_event_states_t I2C_EVENT_RX (void )[static]

I2C_EVENT_SEND_RD_ADDR()

static i2c_event_states_t I2C_EVENT_SEND_RD_ADDR (void )[static]

I2C_EVENT_SEND_WR_ADDR()

static i2c_event_states_t I2C_EVENT_SEND_WR_ADDR (void )[static]

I2C_EVENT_STOP()

static i2c_event_states_t I2C_EVENT_STOP (void )[static]

I2C_EVENT_TX()

static i2c_event_states_t I2C_EVENT_TX (void )[static]

TWI0_Close()

static void TWI0_Close (void )[static]

@misradeviation {Advisory:,2.5} False positive - A few macros in this file are used as arguments but are not getting recognized by the tool. This rule ID has been disabled at the project level due to numerous instances across various files. Consequently, in the application project, this rule ID must be disabled in the MPLAB-X IDE by navigating to Tools>Options>Embedded>MISRA Check.

TWI0_CommandUpdate()

inline static void TWI0_CommandUpdate (TWI_MCMD_t cmd)

TWI0_ErrorEventHandler()

static void TWI0_ErrorEventHandler (void )[static]

TWI0_EventHandler()

static void TWI0_EventHandler (void )[static]

TWI0_ReadStart()

static void TWI0_ReadStart (void )[static]

TWI0_WriteStart()

static void TWI0_WriteStart (void )[static]

Variable Documentation

TWI0_Callback

void(* TWI0_Callback) (void)[static]

twi0_Status

volatile i2c_event_status_t twi0_Status = {0}

4.9.2.8.7 source/polling/i2c_client/src/twi0.c File Reference

#include "../twi0.h"
#include <stdbool.h>
#include "../../system/utils/compiler.h"

Functions

Detailed Description

A source file generated by TWI0 I2C client driver.

This file provides API implementation for the TWI0 driver.

Version: TWI0 Driver Version 2.1.3 TWI0 Package Version 8.1.6

Function Documentation

TWI0_ErrorEventHandler()

static void TWI0_ErrorEventHandler (void )[static]

TWI0_EventHandler()

static void TWI0_EventHandler (void )[static]

Variable Documentation

I2C1_Client

const i2c_client_interface_t I2C1_Client

Initial value:

= 
{
    .Initialize = TWI0_Initialize,
    .Deinitialize = TWI0_Deinitialize,
    .WriteByte = TWI0_WriteByte,
    .ReadByte = TWI0_ReadByte,
    .TransferDirGet = TWI0_TransferDirGet,
    .LastByteAckStatusGet = TWI0_LastByteAckStatusGet,
    .ErrorGet = TWI0_ErrorGet,
    .CallbackRegister = TWI0_CallbackRegister,
    .Tasks = TWI0_Tasks
}

Represents an instance of the portable interface structure designed for TWI0 client.

@misradeviation {Advisory:,8.7} False postive - A few MCC generated driver APIs are made available to users through external linkage. This rule ID has been disabled at the project level due to numerous instances across various files. Consequently, in the application project, this rule ID should be disabled in the MPLAB-X IDE by navigating to Tools -> Options -> Embedded -> MISRA Check.

TWI0_InterruptHandler

bool(* TWI0_InterruptHandler) (i2c_client_transfer_event_t clientEvent)[static]

twi0ErrorState

volatile i2c_client_error_t twi0ErrorState[static]

twi0matchedAddress

volatile uint16_t twi0matchedAddress[static]

4.9.2.8.8 source/polling/i2c_host/src/twi0.c File Reference

This file contains the API implementation for TWI0 driver.

#include "../twi0.h"
#include <stdbool.h>
#include <stdlib.h>
#include "../../system/utils/compiler.h"

Functions

Detailed Description

This file contains the API implementation for TWI0 driver.

A source file generated by TWI0 I2C host driver.

Version: TWI0 Driver Version 2.1.3 TWI0 Package Version 8.1.6

Function Documentation

I2C_EVENT_ERROR()

static i2c_event_states_t I2C_EVENT_ERROR (void )[static]

I2C_EVENT_IDLE()

static i2c_event_states_t I2C_EVENT_IDLE (void )[static]

I2C_EVENT_NACK()

static i2c_event_states_t I2C_EVENT_NACK (void )[static]

I2C_EVENT_RESET()

static i2c_event_states_t I2C_EVENT_RESET (void )[static]

I2C_EVENT_RX()

static i2c_event_states_t I2C_EVENT_RX (void )[static]

I2C_EVENT_SEND_RD_ADDR()

static i2c_event_states_t I2C_EVENT_SEND_RD_ADDR (void )[static]

I2C_EVENT_SEND_WR_ADDR()

static i2c_event_states_t I2C_EVENT_SEND_WR_ADDR (void )[static]

I2C_EVENT_STOP()

static i2c_event_states_t I2C_EVENT_STOP (void )[static]

I2C_EVENT_TX()

static i2c_event_states_t I2C_EVENT_TX (void )[static]

TWI0_Close()

static void TWI0_Close (void )[static]

@misradeviation {Advisory:,2.5} False positive - A few macros in this file are used as arguments but are not getting recognized by the tool. This rule ID has been disabled at the project level due to numerous instances across various files. Consequently, in the application project, this rule ID must be disabled in the MPLAB-X IDE by navigating to Tools>Options>Embedded>MISRA Check.

TWI0_CommandUpdate()

inline static void TWI0_CommandUpdate (TWI_MCMD_t cmd)

TWI0_ErrorEventHandler()

static void TWI0_ErrorEventHandler (void )[static]

TWI0_EventHandler()

static void TWI0_EventHandler (void )[static]

TWI0_ReadStart()

static void TWI0_ReadStart (void )[static]

TWI0_WriteStart()

static void TWI0_WriteStart (void )[static]

Variable Documentation

TWI0_Callback

void(* TWI0_Callback) (void)[static]

twi0_Status

volatile i2c_event_status_t twi0_Status = {0}

4.9.2.8.9 source/interrupt/i2c_client/twi0.h File Reference

#include <stdbool.h>
#include <stdint.h>
#include "i2c_client_types.h"
#include "i2c_client_interface.h"

Functions

Macros

Detailed Description

A header file generated by TWI0 I2C client driver.

Function Documentation

TWI0_CallbackRegister()

void TWI0_CallbackRegister (bool(*)(i2c_client_transfer_event_t clientEvent) callback)

Registers a callback function.

Parameters:
callback

- Pointer to custom callback.

Returns:

None.

TWI0_Deinitialize()

void TWI0_Deinitialize (void )

Resets the registers to their default Power-on Reset (POR) values.

Parameters:
None.
Returns:

None.  

TWI0_ErrorGet()

i2c_client_error_t TWI0_ErrorGet (void )

Retrieves the type of error that occurred during I2C communication. Ensure TWI0 is initialized using TWI0_Initialize() before invoking this function.

Parameters:
None.
Returns:

I2C_CLIENT_ERROR_BUS_COLLISION - I2C Bus Collision Error  

I2C_CLIENT_ERROR_WRITE_COLLISION - I2C Write Collision Error  

I2C_CLIENT_ERROR_RECEIVE_OVERFLOW - I2C Receive overflow  

I2C_CLIENT_ERROR_NONE - No Error

TWI0_Initialize()

void TWI0_Initialize (void )

Initializes the TWI0 client module according to the settings specified in the MPLAB® Code Configurator Melody UI.

Parameters:
None.
Returns:

None.  

TWI0_LastByteAckStatusGet()

i2c_client_ack_status_t TWI0_LastByteAckStatusGet (void )

Retrieves the acknowledgement status from host. Ensure TWI0 is initialized using TWI0_Initialize() before invoking this function.

Parameters:
None.
Returns:

I2C_CLIENT_ACK_STATUS_RECEIVED_ACK - An ACK was received for the most recent transaction with the host  

I2C_CLIENT_ACK_STATUS_RECEIVED_NACK - A NACK was received for the most recent transaction with the host  

TWI0_ReadAddr()

uint16_t TWI0_ReadAddr (void )

Retrieves the most recently matched address received from the host. Ensure TWI0 client is initialized using TWI0_Initialize() before invoking this function.

Parameters:
None.
Returns:

address byte - Last Matched address for TWI0 client

TWI0_ReadByte()

uint8_t TWI0_ReadByte (void )

Reads data from a host on the bus. Ensure TWI0 is initialized using TWI0_Initialize() before invoking this function.

Parameters:
None.
Returns:

uint8_t - A byte read by TWI0 client

TWI0_TransferDirGet()

i2c_client_transfer_dir_t TWI0_TransferDirGet (void )

Retrieves the data transfer direction. Ensure TWI0 is initialized using TWI0_Initialize() before invoking this function.

Parameters:
None.
Returns:

I2C_CLIENT_TRANSFER_DIR_WRITE - I2C client writes to host  

I2C_CLIENT_TRANSFER_DIR_READ - I2C client reads from host  

TWI0_WriteByte()

void TWI0_WriteByte (uint8_t data)

Sends data to a host on the bus. Ensure TWI0 is initialized using TWI0_Initialize() before invoking this function.

Parameters:
in data

- A byte that will be written by TWI0

Returns:

None.  

Macro Definition Documentation

I2C2_Client_CallbackRegister

#define I2C2_Client_CallbackRegister TWI0_CallbackRegister

i2c2_client_client_interface

#define i2c2_client_client_interface I2C2_Client

@misradeviation {Advisory:,2.5} False positive - A few macros in this file are used as arguments but are not getting recognized by the tool. This rule ID has been disabled at the project level due to numerous instances across various files. Consequently, in the application project, this rule ID must be disabled in the MPLAB-X IDE by navigating to Tools>Options>Embedded>MISRA Check.

I2C2_Client_Deinitialize

#define I2C2_Client_Deinitialize TWI0_Deinitialize

I2C2_Client_ErrorGet

#define I2C2_Client_ErrorGet TWI0_ErrorGet

I2C2_Client_Initialize

#define I2C2_Client_Initialize TWI0_Initialize

I2C2_Client_LastByteAckStatusGet

#define I2C2_Client_LastByteAckStatusGet TWI0_LastByteAckStatusGet

I2C2_Client_ReadAddr

#define I2C2_Client_ReadAddr TWI0_ReadAddr

I2C2_Client_ReadByte

#define I2C2_Client_ReadByte TWI0_ReadByte

I2C2_Client_TransferDirGet

#define I2C2_Client_TransferDirGet TWI0_TransferDirGet

I2C2_Client_WriteByte

#define I2C2_Client_WriteByte TWI0_WriteByte

Variable Documentation

I2C2_Client

const i2c_client_interface_t I2C2_Client

Represents an instance of the portable interface structure designed for TWI0 client.

@misradeviation {Advisory:,8.7} False postive - A few MCC generated driver APIs are made available to users through external linkage. This rule ID has been disabled at the project level due to numerous instances across various files. Consequently, in the application project, this rule ID should be disabled in the MPLAB-X IDE by navigating to Tools -> Options -> Embedded -> MISRA Check.

4.9.2.8.10 source/interrupt/i2c_host/twi0.h File Reference

#include <stdbool.h>
#include <stdint.h>
#include "i2c_host_types.h"
#include "i2c_host_interface.h"
#include "i2c_host_event_types.h"

Functions

  • void TWI0_Initialize (void)

    Initializes the TWI0 host module according to the settings specified in the MPLAB® Code Configurator Melody UI.

  • void TWI0_Deinitialize (void)

    Resets the registers to their default Power-on Reset (POR) values.

  • bool TWI0_Write (uint16_t address, uint8_t *data, size_t dataLength)

    Sends data to a client on the bus. Ensure TWI0 host is initialized using TWI0_Initialize() before invoking this function.

  • bool TWI0_Read (uint16_t address, uint8_t *data, size_t dataLength)

    Reads data from an I2C client on the bus. Ensure TWI0 host is initialized using TWI0_Initialize() before invoking this function.

  • bool TWI0_WriteRead (uint16_t address, uint8_t *writeData, size_t writeLength, uint8_t *readData, size_t readLength)

    Initiates a data transfer by writing data from the writeData function to the bus and subsequently reads data from the Client, storing the received data in the readData parameter. The function generates a Start condition on the bus and transmits the specified number of bytes (writeLength) from the writeData buffer. Following this, a Repeated Start condition is inserted, and the function proceeds to read the specified number of bytes (readLength) from the client, storing them in the readData buffer. A Stop condition is generated after the final byte is received.

  • i2c_host_error_t TWI0_ErrorGet (void)

    Retrieves the error that occurred during I2C transmission and reception. Ensure TWI0 host is initialized using TWI0_Initialize() before invoking this function.  

  • bool TWI0_IsBusy (void)

    Determines if the I2C bus is currently busy. Ensure TWI0 host is initialized using TWI0_Initialize() before invoking this function.  

  • void TWI0_CallbackRegister (void(*callbackHandler)(void))

    Registers a callback function.

  • void TWI0_ErrorCallbackRegister (void(*handler)(void))

    Setter function for the TWI error interrupt callback.

Macros

Detailed Description

A header file generated by TWI0 I2C host driver.

Macro Definition Documentation

I2C2_Host_CallbackRegister

#define I2C2_Host_CallbackRegister TWI0_CallbackRegister

I2C2_Host_Deinitialize

#define I2C2_Host_Deinitialize TWI0_Deinitialize

I2C2_Host_ErrorGet

#define I2C2_Host_ErrorGet TWI0_ErrorGet

i2c2_host_host_interface

#define i2c2_host_host_interface I2C2_Host

@misradeviation {Advisory:,2.5} False positive - A few macros in this file are used as arguments but are not getting recognized by the tool. This rule ID has been disabled at the project level due to numerous instances across various files. Consequently, in the application project, this rule ID must be disabled in the MPLAB-X IDE by navigating to Tools>Options>Embedded>MISRA Check.

I2C2_Host_Initialize

#define I2C2_Host_Initialize TWI0_Initialize

I2C2_Host_IsBusy

#define I2C2_Host_IsBusy TWI0_IsBusy

I2C2_Host_Read

#define I2C2_Host_Read TWI0_Read

I2C2_Host_Write

#define I2C2_Host_Write TWI0_Write

I2C2_Host_WriteRead

#define I2C2_Host_WriteRead TWI0_WriteRead

TWI0_BAUD

#define TWI0_BAUD( F_SCL, T_RISE) ((((((float)3333333 / (float)(F_SCL))) - 10 - ((float)3333333 * (T_RISE) / 1000000))) / 2)

Variable Documentation

I2C2_Host

const i2c_host_interface_t I2C2_Host

Represents an instance of the portable interface structure designed for TWI0 host.

4.9.2.8.11 source/polling/i2c_client/twi0.h File Reference

#include <stdbool.h>
#include <stdint.h>
#include "i2c_client_types.h"
#include "i2c_client_interface.h"

Functions

Macros

Detailed Description

A header file generated by TWI0 I2C client driver.

Function Documentation

TWI0_Tasks()

void TWI0_Tasks (void )

Task to be polled I2C client events. Ensure TWI0 client is initialized using TWI0_Initialize() before invoking this function.

Parameters:
None.
Returns:

None.  

Macro Definition Documentation

I2C1_Client_CallbackRegister

#define I2C1_Client_CallbackRegister TWI0_CallbackRegister

i2c1_client_client_interface

#define i2c1_client_client_interface I2C1_Client

@misradeviation {Advisory:,2.5} False positive - A few macros in this file are used as arguments but are not getting recognized by the tool. This rule ID has been disabled at the project level due to numerous instances across various files. Consequently, in the application project, this rule ID must be disabled in the MPLAB-X IDE by navigating to Tools>Options>Embedded>MISRA Check.

I2C1_Client_Deinitialize

#define I2C1_Client_Deinitialize TWI0_Deinitialize

I2C1_Client_ErrorGet

#define I2C1_Client_ErrorGet TWI0_ErrorGet

I2C1_Client_Initialize

#define I2C1_Client_Initialize TWI0_Initialize

I2C1_Client_LastByteAckStatusGet

#define I2C1_Client_LastByteAckStatusGet TWI0_LastByteAckStatusGet

I2C1_Client_ReadAddr

#define I2C1_Client_ReadAddr TWI0_ReadAddr

I2C1_Client_ReadByte

#define I2C1_Client_ReadByte TWI0_ReadByte

I2C1_Client_Tasks

#define I2C1_Client_Tasks TWI0_Tasks

I2C1_Client_TransferDirGet

#define I2C1_Client_TransferDirGet TWI0_TransferDirGet

I2C1_Client_WriteByte

#define I2C1_Client_WriteByte TWI0_WriteByte

Variable Documentation

I2C1_Client

const i2c_client_interface_t I2C1_Client

Represents an instance of the portable interface structure designed for TWI0 client.

@misradeviation {Advisory:,8.7} False postive - A few MCC generated driver APIs are made available to users through external linkage. This rule ID has been disabled at the project level due to numerous instances across various files. Consequently, in the application project, this rule ID should be disabled in the MPLAB-X IDE by navigating to Tools -> Options -> Embedded -> MISRA Check.

4.9.2.8.12 source/polling/i2c_host/twi0.h File Reference

#include <stdbool.h>
#include <stdint.h>
#include "i2c_host_types.h"
#include "i2c_host_interface.h"
#include "i2c_host_event_types.h"

Functions

  • void TWI0_Initialize (void)

    Initializes the TWI0 host module according to the settings specified in the MPLAB® Code Configurator Melody UI.

  • void TWI0_Deinitialize (void)

    Resets the registers to their default Power-on Reset (POR) values.

  • bool TWI0_Write (uint16_t address, uint8_t *data, size_t dataLength)

    Sends data to a client on the bus. Ensure TWI0 host is initialized using TWI0_Initialize() before invoking this function.

  • bool TWI0_Read (uint16_t address, uint8_t *data, size_t dataLength)

    Reads data from an I2C client on the bus. Ensure TWI0 host is initialized using TWI0_Initialize() before invoking this function.

  • bool TWI0_WriteRead (uint16_t address, uint8_t *writeData, size_t writeLength, uint8_t *readData, size_t readLength)

    Initiates a data transfer by writing data from the writeData function to the bus and subsequently reads data from the Client, storing the received data in the readData parameter. The function generates a Start condition on the bus and transmits the specified number of bytes (writeLength) from the writeData buffer. Following this, a Repeated Start condition is inserted, and the function proceeds to read the specified number of bytes (readLength) from the client, storing them in the readData buffer. A Stop condition is generated after the final byte is received.

  • i2c_host_error_t TWI0_ErrorGet (void)

    Retrieves the error that occurred during I2C transmission and reception. Ensure TWI0 host is initialized using TWI0_Initialize() before invoking this function.  

  • bool TWI0_IsBusy (void)

    Determines if the I2C bus is currently busy. Ensure TWI0 host is initialized using TWI0_Initialize() before invoking this function.  

  • void TWI0_CallbackRegister (void(*callbackHandler)(void))

    Registers a callback function.

  • void TWI0_Tasks (void)

    Task to be polled for I2C host events. Ensure TWI0 host is initialized using TWI0_Initialize() before invoking this function.

Macros

Detailed Description

A header file generated by TWI0 I2C host driver.

Macro Definition Documentation

I2C1_Host_CallbackRegister

#define I2C1_Host_CallbackRegister TWI0_CallbackRegister

I2C1_Host_Deinitialize

#define I2C1_Host_Deinitialize TWI0_Deinitialize

I2C1_Host_ErrorGet

#define I2C1_Host_ErrorGet TWI0_ErrorGet

i2c1_host_host_interface

#define i2c1_host_host_interface I2C1_Host

@misradeviation {Advisory:,2.5} False positive - A few macros in this file are used as arguments but are not getting recognized by the tool. This rule ID has been disabled at the project level due to numerous instances across various files. Consequently, in the application project, this rule ID must be disabled in the MPLAB-X IDE by navigating to Tools>Options>Embedded>MISRA Check.

I2C1_Host_Initialize

#define I2C1_Host_Initialize TWI0_Initialize

I2C1_Host_IsBusy

#define I2C1_Host_IsBusy TWI0_IsBusy

I2C1_Host_Read

#define I2C1_Host_Read TWI0_Read

I2C1_Host_Tasks

#define I2C1_Host_Tasks TWI0_Tasks

I2C1_Host_Write

#define I2C1_Host_Write TWI0_Write

I2C1_Host_WriteRead

#define I2C1_Host_WriteRead TWI0_WriteRead

TWI0_BAUD

#define TWI0_BAUD( F_SCL, T_RISE) ((((((float)4000000 / (float)(F_SCL))) - 10 - ((float)4000000 * (T_RISE) / 1000000))) / 2)

Variable Documentation

I2C1_Host

const i2c_host_interface_t I2C1_Host

Represents an instance of the portable interface structure designed for TWI0 host.

4.9.2.8.13 source/interrupt/i2c_host/i2c_host_event_types.h File Reference

This header file contains helper structures for the implementation of the I2C host driver.

#include "i2c_host_types.h"
#include <stddef.h>

Detailed Description

This header file contains helper structures for the implementation of the I2C host driver.

Header file generated by I2C host driver.

Version: TWI0 Driver Version 2.1.3 TWI0 Package Version 8.1.6

Macro Definition Documentation

I2C_EVENTS

#define I2C_EVENTS (9)

Typedef Documentation

twi0eventHandler

typedef i2c_event_states_t(* twi0eventHandler) (void)

Variable Documentation

twi0_eventTable

const twi0eventHandler twi0_eventTable[I2C_EVENTS]

twi0_Status

volatile i2c_event_status_t twi0_Status

4.9.2.8.14 source/polling/i2c_host/i2c_host_event_types.h File Reference

This header file contains helper structures for the implementation of the I2C host driver.

#include "i2c_host_types.h"
#include <stddef.h>

Detailed Description

This header file contains helper structures for the implementation of the I2C host driver.

Header file generated by I2C host driver.

Version: TWI0 Driver Version 2.1.3 TWI0 Package Version 8.1.6

Macro Definition Documentation

I2C_EVENTS

#define I2C_EVENTS (9)

Typedef Documentation

twi0eventHandler

typedef i2c_event_states_t(* twi0eventHandler) (void)

Enumeration Type Documentation

i2c_event_states_t

enum i2c_event_states_t

I2C_STATE_IDLE

State indicates that the I2C bus is in an idle state

I2C_STATE_SEND_RD_ADDR

State indicates the transmission of the address for a read operation

I2C_STATE_SEND_WR_ADDR

State indicates the transmission of the address for a write operation

I2C_STATE_TX

State indicates the data transmission

I2C_STATE_RX

State indicates the data reception

I2C_STATE_NACK

State indicates that a negative acknowledgment was received from the I2C client

I2C_STATE_ERROR

State indicates an error

I2C_STATE_STOP

State indicates stop operation

I2C_STATE_RESET

State indicates the reset event

I2C_STATE_IDLE

State indicates that the I2C bus is in an idle state

I2C_STATE_SEND_RD_ADDR

State indicates the transmission of the address for a read operation

I2C_STATE_SEND_WR_ADDR

State indicates the transmission of the address for a write operation

I2C_STATE_TX

State indicates the data transmission

I2C_STATE_RX

State indicates the data reception

I2C_STATE_NACK

State indicates that a negative acknowledgment was received from the I2C client

I2C_STATE_ERROR

State indicates an error

I2C_STATE_STOP

State indicates stop operation

I2C_STATE_RESET

State indicates the reset event

Variable Documentation

twi0_eventTable

const twi0eventHandler twi0_eventTable[I2C_EVENTS]

twi0_Status

volatile i2c_event_status_t twi0_Status

4.9.2.8.15 source/interrupt/i2c_host/i2c_host_interface.h File Reference

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include "i2c_host_types.h"

Data structures

Detailed Description

A header file generated by I2C host driver

4.9.2.8.16 source/polling/i2c_host/i2c_host_interface.h File Reference

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include "i2c_host_types.h"

Data structures

Detailed Description

A header file generated by I2C host driver

4.9.2.8.17 source/interrupt/i2c_host/i2c_host_types.h File Reference

This header file provides helper structures for the implementation of the I2C host driver.

Detailed Description

This header file provides helper structures for the implementation of the I2C host driver.

Types Header File generated by I2C host driver

Version: TWI0 Driver Version 2.1.3 TWI0 Package Version 8.1.6

Typedef Documentation

i2c_host_transfer_setup_t

typedef struct i2c_transfer_setup i2c_host_transfer_setup_t

@misradeviation{Advisory:,2.4,Advisory:,2.3} These declarations are provided by the Melody-generated drivers for the user’s application. The user has the discretion to implement these declarations in their application.

4.9.2.8.18 source/polling/i2c_host/i2c_host_types.h File Reference

This header file provides helper structures for the implementation of the I2C host driver.

Detailed Description

This header file provides helper structures for the implementation of the I2C host driver.

Types Header File generated by I2C host driver

Version: TWI0 Driver Version 2.1.3 TWI0 Package Version 8.1.6

Typedef Documentation

i2c_host_transfer_setup_t

typedef struct i2c_transfer_setup i2c_host_transfer_setup_t

@misradeviation{Advisory:,2.4,Advisory:,2.3} These declarations are provided by the Melody-generated drivers for the user’s application. The user has the discretion to implement these declarations in their application.

Enumeration Type Documentation

i2c_host_error_t

enum i2c_host_error_t

I2C_ERROR_NONE

Indicates no error

I2C_ERROR_ADDR_NACK

Indicates that the I2C client did not acknowledge the address

I2C_ERROR_DATA_NACK

Indicates that the I2C client did not acknowledge the data

I2C_ERROR_BUS_COLLISION

Indicates an error caused due to bus collision