MCUXpresso SDK API Reference Manual  Rev 2.16.000
NXP Semiconductors
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
LPI2C Peripheral and Driver Overview

Content including 1) peripheral features, work logic and work method; 2) driver design logic and use method; 3) typical use case. More...

Peripheral feature and how this peripheral works

The LPI2C is a low power Inter-Integrated Circuit(I2C) module that supports an efficient interface to an I2C bus as a master and/or as a slave.

Features

How this driver is designed to make this peripheral works

On abstraction level, the LPI2C driver provides two parallel layers for various cases with different requirements on flexibility: Functional layer and Transactional layer. Do not mix the usage of the data transfer APIs in these 2 layers, check the transaction APIs introduction in 'How to use this driver' part. To distinguish Transactional layer from Functional layer easily, all Transactional APIs take 'Transfer' in API name.

Functional Layer

It's provided with highly optimized implementation and highly flexible usage of the hardware details. User needs to know how to organize these functional APIs together to meet application requirement. The LPI2C driver provides master and slave feature enabling APIs named as LPI2C_MasterXxx and LPI2C_SlaveXxx, user can choose which needed in their application and use related interfaces. These APIs implement to configure basic features of LPI2C, they're highly separated according to different features. Application just needs to combine them on demand. For data transfer, there're also multiple APIs to finish the whole I2C transfer: START/REPEAT_START, SEND/RECEIVE and STOP.

Transactional Layer

It's provided with high abstraction, limited flexibility/optimization, and not all features are covered. It aims to let user implement LPI2C transaction with least knowledge requirement of this specific I2C peripheral and least coding effort. It achieves this goal by hiding the setup of interrupt processing inside driver and implementing software state machine for LPI2C transaction.

How to use this driver

General Control Macro

Configuration Items Before Calling LPI2C Driver APIs

The General Steps To Use LPI2C Driver

Common:
Function LPI2C_Init initialises I2C master and slave operations together, user can call LPI2C_GetDefaultConfig to get default setting and give this to LPI2C_Init to enable basic features for master and slave. If user only wants to use one of master/slave, please call LPI2C_MasterInit or LPI2C_SlaveInit to finish specified initialization work. Two similar LPI2C_xxxGetDefaultConfig APIs are also ready for master and slave, more information is in the following comments. If user wants to disable master or/and slave, there're LPI2C_Deinit, LPI2C_MasterDeinit and LPI2C_SlaveDeinit to do it.

Note
If FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL is not enabled, LPI2C clock will be disabled in Master/Slave Deinit function.

Master Operation:

  1. Set suitable value in structure lpi2c_master_config_t to configure basic feature of LPI2C master mode, please check the comments in this structure to get more details. There's an API LPI2C_MasterGetDefaultConfig to get default setting covering conventional demand. User should set it according to specific application.
  2. Call LPI2C_MasterInit to initialise LPI2C peripheral which should enable features based on lpi2c_master_config_t.
  3. Driver provides more APIs to configure special features except what LPI2C_MasterInit has done. Such as LPI2C_MasterSetDataMatch and LPI2C_MasterSetBaudRate, please read separate function description to know more. The benefit is these APIs can be used to enable or disable related features after LPI2C has been initialised.
    Note
    Just be careful these APIs will disable LPI2C master before setting peripheral register and enable after that, should call them when LPI2C master is idle.
  4. For transaction, LPI2C driver provides three ways in two layers to transmit/receive I2C data with master.

    • Functional layer
      A group of APIs LPI2C_MasterStart, LPI2C_MasterRepeatedStart, LPI2C_MasterReceive, LPI2C_MasterSend and LPI2C_MasterStop to implement each procedure of I2C transfer.
      • LPI2C_MasterStart It will check whether the I2C bus is busy(SCL low level) and wait for enough space in LPI2C Tx FIFO. If all transfer conditions are OK, SDA line will pull low to start I2C transfer and send out 7-bit slave address + 1-bit read/write direct.
      • LPI2C_MasterRepeatedStart It just encapsulates LPI2C_MasterStart with a new name. It's better to use this API when starting a new transfer without stopping last one, this name is easier to understand.
      • LPI2C_MasterReceive, LPI2C_MasterSend They will check FIFO space situation, and start to send or receive a bundle of data with I2C.
      • LPI2C_MasterStop It releases SCL to high level to stop the I2C transfer.
    • Transactional layer Blocking Transfer
      • LPI2C_MasterTransferBlocking It sends/receives data according the parameters in lpi2c_master_transfer_t, it returns after the transfer is over or getting errors.
        • lpi2c_master_transfer_t
          Member Comment
          u8ControlFlagMask_lpi2c_master_transfer_control_flags
          u8SlaveAddress The 7-bit slave address
          eDirection Read or write lpi2c_data_direction_t
          pu8Command Pointer to command code
          u8CommandSize Length of command buffer to send in bytes
          pData I2C transfer data buffer address
          u16DataSize Number of bytes to transfer
          Uses u8ControlFlagMask, control to add/remove start/stop operation in this transfer. The eDirection indicates transfer is reading or writing. They make this API flexible to be used for sending or receiving. The u8SlaveAddress is I2C slave device address following the start flag. The pu8Command is usually used when user reads/writes I2C data from/to an external device such as EEPROM. If use Functional layer API to send this external device address, need to populate it into data buffer. With this API user can store it in pu8Command, it's more intuitive and easier to use. It can also take other commands for various I2C device. The u8CommandSize indicates byte size in pu8Command address. The pData and u16DataSize are the most parameters needed by any transfer without more mention.
    • Transactional layer NonBlocking Transfer
    Note
    If system enters debug mode when I2C transfer is on-going, this transfer will abort. If want to debug LPI2C and not stop normal transfer, need to set #bDebugEnable as true.

    Slave Operation:

  1. Set suitable value in structure lpi2c_slave_config_t to configure basic feature of LPI2C slave mode, please check the comments in this structure to get more details. There's an API LPI2C_SlaveGetDefaultConfig to get default setting covering conventional demand. User should set it according to specific application.
  2. Call LPI2C_SlaveInit to initialise LPI2C peripheral which should enable features based on lpi2c_master_config_t.
  3. For transaction, this LPI2C driver provides two ways in two layers to transmit/receive I2C data with slave.

Typical Use Case

Master Operation:

Slave Operation:

EXCEPTION: SMBus is not supported in this driver.