Kinetis SDK v.1.3 API Reference Manual  Rev. 0
Freescale Semiconductor, Inc.
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages

Overview

This section describes the programming interface of the LPI2C HAL driver.

LPI2C ICR Table

ICR (hex) SCL divider SDA hold value SCL start hold value SCL stop hold value
00 20 7 6 11
01 22 7 7 12
02 24 8 8 13
03 26 8 9 14
04 28 9 10 15
05 30 9 11 16
06 34 10 13 18
07 40 10 16 21

I2C Clock rate formulas

I2C baud rate = bus_clock_Hz / (mult * SCL_divider)

SDA hold time = bus_clock_period_s * mult * SDA_hold_value

SCL start hold time = bus_clock_period_s * mult * SCL_start_hold_value

SCL stop hold time = bus_clock_period_s * mult * SCL_stop_hold_value

Enumerations

enum  lpi2c_status_t {
  kStatus_LPI2C_Success = 0x0U,
  kStatus_LPI2C_Initialized = 0x1U,
  kStatus_LPI2C_Fail = 0x2U,
  kStatus_LPI2C_Busy = 0x3U,
  kStatus_LPI2C_Timeout = 0x4U,
  kStatus_LPI2C_ReceivedNak = 0x5U,
  kStatus_LPI2C_SlaveTxUnderrun = 0x6U,
  kStatus_LPI2C_SlaveRxOverrun = 0x7U,
  kStatus_LPI2C_AribtrationLost = 0x8U,
  kStatus_LPI2C_StopSignalFail = 0x9U,
  kStatus_LPI2C_Idle = 0xAU,
  kStatus_LPI2C_NoReceiveInProgress = 0xBU,
  kStatus_LPI2C_NoSendInProgress = 0xCU
}
 LPI2C status return codes. More...
 
enum  lpi2c_master_status_t
 LPI2C master status. More...
 
enum  lpi2c_master_interrupt_t
 LPI2C interrupts. More...
 
enum  lpi2c_slave_status_t
 LPI2C slave status. More...
 
enum  lpi2c_slave_interrupt_t
 LPI2C interrupts. More...
 
enum  lpi2c_direction_t { kLPI2CWrite }
 Direction of master and slave transfers. More...
 

Module controls

void LPI2C_HAL_Init (LPI2C_Type *base)
 Restores the LPI2C peripheral to reset state. More...
 
static void LPI2C_HAL_SetMasterCmd (LPI2C_Type *base, bool enable)
 Enables/disables the LPI2C module as a master. More...
 
static void LPI2C_HAL_SetSlaveCmd (LPI2C_Type *base, bool enable)
 Enables/disables the LPI2C module as a slave. More...
 

Status

static bool LPI2C_HAL_MasterGetStatus (LPI2C_Type *base, lpi2c_master_status_t statusFlag)
 Gets the LPI2C status flag state. More...
 
static void LPI2C_HAL_MasterClearStatus (LPI2C_Type *base, lpi2c_master_status_t statusFlag)
 Clears the LPI2C status flag state. More...
 
static bool LPI2C_HAL_SlaveGetStatus (LPI2C_Type *base, lpi2c_slave_status_t statusFlag)
 Gets the LPI2C status flag state. More...
 
static void LPI2C_HAL_SlaveClearStatus (LPI2C_Type *base, lpi2c_slave_status_t statusFlag)
 Clears the LPI2C status flag state. More...
 

Interrupt

void LPI2C_HAL_MasterSetIntCmd (LPI2C_Type *base, lpi2c_master_interrupt_t interrupt, bool enable)
 Enables or disables specific LPI2C master interrupt. More...
 
static bool LPI2C_HAL_MasterGetIntCmd (LPI2C_Type *base, lpi2c_master_interrupt_t interrupt)
 Returns whether the LPI2C master interrupt is enabled. More...
 
void LPI2C_HAL_SlaveSetIntCmd (LPI2C_Type *base, lpi2c_slave_interrupt_t interrupt, bool enable)
 Enables or disables a specific LPI2C slave interrupt. More...
 
static bool LPI2C_HAL_SlaveGetIntCmd (LPI2C_Type *base, lpi2c_slave_interrupt_t interrupt)
 Returns whether the LPI2C slave interrupt is enabled. More...
 

Baud rate

void LPI2C_HAL_SetBaudRate (LPI2C_Type *base, uint32_t sourceClockInHz, uint32_t kbps)
 Sets the LPI2C bus frequency for master transactions. More...
 

Bus operations

void LPI2C_HAL_SendStart (LPI2C_Type *base, uint8_t address, lpi2c_direction_t dir)
 Sends a start or a repeated start signal on the LPI2C bus. More...
 
lpi2c_status_t LPI2C_HAL_SendStop (LPI2C_Type *base)
 Sends a stop signal on the LPI2C bus. More...
 
static void LPI2C_HAL_SlaveSendAck (LPI2C_Type *base)
 Causes an ACK to be sent on the bus. More...
 
static void LPI2C_HAL_SlaveSendNak (LPI2C_Type *base)
 Causes a NAK to be sent on the bus. More...
 
static void LPI2C_HAL_MasterSetRxMode (LPI2C_Type *base, uint8_t rxSize)
 Selects either transmit or receive mode. More...
 

Slave address

void LPI2C_HAL_SetAddress7bit (LPI2C_Type *base, uint8_t address)
 Sets the primary 7-bit slave address. More...
 
void LPI2C_HAL_SetAddress10bit (LPI2C_Type *base, uint16_t address)
 Sets the primary slave address and enables 10-bit address mode. More...
 

Data transfer

static uint8_t LPI2C_HAL_MasterReadByte (LPI2C_Type *base)
 Returns the last byte of data read from the bus and initiate another read. More...
 
static void LPI2C_HAL_MasterWriteByte (LPI2C_Type *base, uint8_t byte)
 Writes one byte of data to the LPI2C bus. More...
 
static uint8_t LPI2C_HAL_SlaveReadByte (LPI2C_Type *base)
 Returns the last byte of data read from the slave. More...
 
static void LPI2C_HAL_SlaveWriteByte (LPI2C_Type *base, uint8_t byte)
 Writes one byte of data to the LPI2C slave transmit FIFO. More...
 
bool LPI2C_HAL_SlaveWriteByteBlocking (LPI2C_Type *base, uint8_t byte)
 Writes one byte of data to the LPI2C bus and waits till that byte is transferred successfully. More...
 
uint8_t LPI2C_HAL_MasterReadByteBlocking (LPI2C_Type *base)
 Waits until there is a receive ready flag and reads one byte from the receive FIFO. More...
 
bool LPI2C_HAL_MasterWriteByteBlocking (LPI2C_Type *base, uint8_t byte)
 Waits till a transmit request flag is set and writes one byte of data to the transmit FIFO, then waits till that byte is transferred successfully. More...
 
lpi2c_status_t LPI2C_HAL_MasterReceiveDataPolling (LPI2C_Type *base, uint16_t slaveAddr, const uint8_t *cmdBuff, uint32_t cmdSize, uint8_t *rxBuff, uint32_t rxSize)
 Performs a polling receive transaction on the LPI2C bus. More...
 
lpi2c_status_t LPI2C_HAL_MasterSendDataPolling (LPI2C_Type *base, uint16_t slaveAddr, const uint8_t *cmdBuff, uint32_t cmdSize, const uint8_t *txBuff, uint32_t txSize)
 Performs a polling send transaction on the LPI2C bus. More...
 
lpi2c_status_t LPI2C_HAL_SlaveSendDataPolling (LPI2C_Type *base, const uint8_t *txBuff, uint32_t txSize)
 Sends out multiple bytes of data using the polling method. More...
 
lpi2c_status_t LPI2C_HAL_SlaveReceiveDataPolling (LPI2C_Type *base, uint8_t *rxBuff, uint32_t rxSize)
 Receives multiple bytes of data using the polling method. More...
 

Enumeration Type Documentation

Enumerator
kStatus_LPI2C_Success 

LPI2C operation has no error.

kStatus_LPI2C_Initialized 

Current LPI2C is already initialized by one task.

kStatus_LPI2C_Fail 

LPI2C operation failed.

kStatus_LPI2C_Busy 

The master is already performing a transfer.

kStatus_LPI2C_Timeout 

The transfer timed out.

kStatus_LPI2C_ReceivedNak 

The slave device sent a NAK in response to a byte.

kStatus_LPI2C_SlaveTxUnderrun 

LPI2C Slave TX Underrun error.

kStatus_LPI2C_SlaveRxOverrun 

LPI2C Slave RX Overrun error.

kStatus_LPI2C_AribtrationLost 

LPI2C Arbitration Lost error.

kStatus_LPI2C_StopSignalFail 

LPI2C STOP signal could not release bus.

kStatus_LPI2C_Idle 

LPI2C Slave Bus is Idle.

kStatus_LPI2C_NoReceiveInProgress 

Attempt to abort a receiving when no transfer was in progress.

kStatus_LPI2C_NoSendInProgress 

Attempt to abort a sending when no transfer was in progress.

Enumerator
kLPI2CWrite 

Master transmit.

Master receive.

Function Documentation

void LPI2C_HAL_Init ( LPI2C_Type *  base)
Parameters
baseThe LPI2C peripheral base pointer
static void LPI2C_HAL_SetMasterCmd ( LPI2C_Type *  base,
bool  enable 
)
inlinestatic
Parameters
baseThe LPI2C peripheral base pointer
enableEnable(true) or disable(false) current LPI2C as master.
static void LPI2C_HAL_SetSlaveCmd ( LPI2C_Type *  base,
bool  enable 
)
inlinestatic
Parameters
baseThe LPI2C peripheral base pointer
enableEnable(true) or disable(false) current LPI2C as slave.
static bool LPI2C_HAL_MasterGetStatus ( LPI2C_Type *  base,
lpi2c_master_status_t  statusFlag 
)
inlinestatic
Parameters
baseThe LPI2C peripheral base pointer.
statusFlagThe status flag, defined in type lpi2c_status_flag_t.
Returns
State of the status flag: asserted (true) or not-asserted (false).
  • true: related status flag is being set.
  • false: related status flag is not set.
static void LPI2C_HAL_MasterClearStatus ( LPI2C_Type *  base,
lpi2c_master_status_t  statusFlag 
)
inlinestatic
Parameters
baseThe LPI2C peripheral base pointer.
statusFlagThe status flag, defined in type lpi2c_status_flag_t.
static bool LPI2C_HAL_SlaveGetStatus ( LPI2C_Type *  base,
lpi2c_slave_status_t  statusFlag 
)
inlinestatic
Parameters
baseThe LPI2C peripheral base pointer.
statusFlagThe status flag, defined in type lpi2c_status_flag_t.
Returns
State of the status flag: asserted (true) or not-asserted (false).
  • true: related status flag is being set.
  • false: related status flag is not set.
static void LPI2C_HAL_SlaveClearStatus ( LPI2C_Type *  base,
lpi2c_slave_status_t  statusFlag 
)
inlinestatic
Parameters
baseThe LPI2C peripheral base pointer.
statusFlagThe status flag, defined in type lpi2c_status_flag_t.
void LPI2C_HAL_MasterSetIntCmd ( LPI2C_Type *  base,
lpi2c_master_interrupt_t  interrupt,
bool  enable 
)

This function only enable/disable one specific interrupt. If you need to enable/disable multiple interrupt in one function call, it's better to use the entire register write operation.

Parameters
baseThe LPI2C peripheral base pointer
interruptInterrupt name.
enablePass true to enable interrupt, false to disable.
static bool LPI2C_HAL_MasterGetIntCmd ( LPI2C_Type *  base,
lpi2c_master_interrupt_t  interrupt 
)
inlinestatic
Parameters
baseThe LPI2C peripheral base pointer
Returns
Whethr LPI2C master interrupts are enabled(true) or not(false).
void LPI2C_HAL_SlaveSetIntCmd ( LPI2C_Type *  base,
lpi2c_slave_interrupt_t  interrupt,
bool  enable 
)

This function only enables/disables one specific interrupt. To enable/disable multiple interrupts in one function call, use the entire register write operation.

Parameters
baseThe LPI2C peripheral base pointer
interruptInterrupt name.
enablePass true to enable interrupt, false to disable.
static bool LPI2C_HAL_SlaveGetIntCmd ( LPI2C_Type *  base,
lpi2c_slave_interrupt_t  interrupt 
)
inlinestatic
Parameters
baseThe LPI2C peripheral base pointer
Returns
Whether LPI2C slave interrupts are enabled.
void LPI2C_HAL_SetBaudRate ( LPI2C_Type *  base,
uint32_t  sourceClockInHz,
uint32_t  kbps 
)
Parameters
baseThe LPI2C peripheral base pointer
sourceClockInHzLPI2C source input clock in Hertz
kbpsRequested bus frequency in kilohertz. Common values are either 100 or 400.
void LPI2C_HAL_SendStart ( LPI2C_Type *  base,
uint8_t  address,
lpi2c_direction_t  dir 
)

This function initiates a new master mode transfer by sending the start signal. It is also sends a repeated start signal when a transfer is already in progress.

Parameters
baseThe LPI2C peripheral base pointer
address7-bit slave device address.
dirMaster transfer directions(transmit/receive).
lpi2c_status_t LPI2C_HAL_SendStop ( LPI2C_Type *  base)
Parameters
baseThe LPI2C peripheral base pointer
Returns
Whether the sending of STOP single is success or not.
static void LPI2C_HAL_SlaveSendAck ( LPI2C_Type *  base)
inlinestatic

This function specifies that an ACK signal is sent in response to the next received byte.

Note that the behaviour of this function is changed when the LPI2C peripheral is placed in the fast ACK mode. In this case, this function causes an ACK signal to be sent in response to the current byte, rather than the next received byte.

Parameters
baseThe LPI2C peripheral base pointer
static void LPI2C_HAL_SlaveSendNak ( LPI2C_Type *  base)
inlinestatic

This function specifies that a NAK signal is sent in response to the next received byte.

Note that the behaviour of this function is changed when the LPI2C peripheral is placed in the fast ACK mode. In this case, this function causes an NAK signal to be sent in response to the current byte, rather than the next received byte.

Parameters
baseThe LPI2C peripheral base pointer
static void LPI2C_HAL_MasterSetRxMode ( LPI2C_Type *  base,
uint8_t  rxSize 
)
inlinestatic
Parameters
baseThe LPI2C peripheral base pointer.
rxSizeInitialize receiving "rxSize" of bytes.
void LPI2C_HAL_SetAddress7bit ( LPI2C_Type *  base,
uint8_t  address 
)
Parameters
baseThe LPI2C peripheral base pointer
addressThe slave address is in the upper 7 bits.
void LPI2C_HAL_SetAddress10bit ( LPI2C_Type *  base,
uint16_t  address 
)
Parameters
baseThe LPI2C peripheral base pointer
addressThe 10-bit slave address, in bits [9:0] of the value.
static uint8_t LPI2C_HAL_MasterReadByte ( LPI2C_Type *  base)
inlinestatic

In a master receive mode, calling this function initiates receiving the next byte of data.

Parameters
baseThe LPI2C peripheral base pointer
Returns
This function returns the last byte received while the LPI2C module is configured in master receive or slave receive mode.
static void LPI2C_HAL_MasterWriteByte ( LPI2C_Type *  base,
uint8_t  byte 
)
inlinestatic

When this function is called in the master transmit mode, a data transfer is initiated. In slave mode, the same function is available after an address match occurs.

In a master transmit mode, the first byte of data written following the start bit or repeated start bit is used for the address transfer and must consist of the slave address (in bits 7-1) concatenated with the required R/#W bit (in position bit 0).

Parameters
baseThe LPI2C peripheral base pointer.
byteThe byte of data to transmit.
static uint8_t LPI2C_HAL_SlaveReadByte ( LPI2C_Type *  base)
inlinestatic
Parameters
baseThe LPI2C peripheral base pointer
Returns
This function returns the last byte received while the LPI2C module is configured in slave receive mode.
static void LPI2C_HAL_SlaveWriteByte ( LPI2C_Type *  base,
uint8_t  byte 
)
inlinestatic
Parameters
baseThe LPI2C peripheral base pointer.
byteThe byte of data to transmit.
bool LPI2C_HAL_SlaveWriteByteBlocking ( LPI2C_Type *  base,
uint8_t  byte 
)
Parameters
baseThe LPI2C peripheral base pointer.
byteThe byte of data to transmit.
Returns
Whether ACK is received(TRUE) or not(FALSE).
uint8_t LPI2C_HAL_MasterReadByteBlocking ( LPI2C_Type *  base)
Parameters
baseThe LPI2C peripheral base pointer
Returns
Returns the last byte received
bool LPI2C_HAL_MasterWriteByteBlocking ( LPI2C_Type *  base,
uint8_t  byte 
)
Parameters
baseThe LPI2C peripheral base pointer.
byteThe byte of data to transmit.
Returns
Whether ACK is received(TRUE) or not(FALSE).
lpi2c_status_t LPI2C_HAL_MasterReceiveDataPolling ( LPI2C_Type *  base,
uint16_t  slaveAddr,
const uint8_t *  cmdBuff,
uint32_t  cmdSize,
uint8_t *  rxBuff,
uint32_t  rxSize 
)
Parameters
baseThe LPI2C peripheral base pointer.
salveAddrThe slave address to communicate.
cmdBuffThe pointer to the commands to be transferred.
cmdSizeThe length in bytes of the commands to be transferred.
rxBuffThe pointer to the data to be transferred.
rxSizeThe length in bytes of the data to be transferred.
Returns
Error or success status returned by API.
lpi2c_status_t LPI2C_HAL_MasterSendDataPolling ( LPI2C_Type *  base,
uint16_t  slaveAddr,
const uint8_t *  cmdBuff,
uint32_t  cmdSize,
const uint8_t *  txBuff,
uint32_t  txSize 
)
Parameters
baseThe LPI2C peripheral base pointer.
salveAddrThe slave address to communicate.
cmdBuffThe pointer to the commands to be transferred.
cmdSizeThe length in bytes of the commands to be transferred.
txBuffThe pointer to the data to be transferred.
txSizeThe length in bytes of the data to be transferred.
Returns
Error or success status returned by API.
lpi2c_status_t LPI2C_HAL_SlaveSendDataPolling ( LPI2C_Type *  base,
const uint8_t *  txBuff,
uint32_t  txSize 
)
Parameters
baseLPI2C module base pointer.
txBuffThe buffer pointer which saves the data to be sent.
txSizeSize of data to be sent in unit of byte.
Returns
Error or success status returned by API.
lpi2c_status_t LPI2C_HAL_SlaveReceiveDataPolling ( LPI2C_Type *  base,
uint8_t *  rxBuff,
uint32_t  rxSize 
)
Parameters
baseLPI2C module base pointer.
rxBuffThe buffer pointer which saves the data to be received.
rxSizeSize of data need to be received in unit of byte.
Returns
Error or success status returned by API.