I2C Exported Functions

group I2C_Exported_Functions

Functions

void I2C_DeInit(I2C_TypeDef *I2Cx)

Deinitialize the I2Cx peripheral registers to their default reset values.

Example usage

void driver_i2c0_init(void)
{
    I2C_DeInit(I2C0);
}

Parameters:

I2Cx[in] Where x can be 0 or 1 to select the I2C peripheral.

Returns:

None.

void I2C_Init(I2C_TypeDef *I2Cx, I2C_InitTypeDef *I2C_InitStruct)

Initialize the I2Cx peripheral according to the specified parameters in the I2C_InitStruct.

Example usage

void driver_i2c0_init(void)
{
    RCC_PeriphClockCmd(APBPeriph_I2C0, APBPeriph_I2C0_CLOCK, ENABLE);

    I2C_InitTypeDef  I2C_InitStruct;
    I2C_StructInit(&I2C_InitStruct);

    I2C_InitStruct.I2C_ClockSpeed    = 100000;
    I2C_InitStruct.I2C_DeviveMode    = I2C_DeviveMode_Master;
    I2C_InitStruct.I2C_AddressMode   = I2C_AddressMode_7BIT;
    I2C_InitStruct.I2C_SlaveAddress  = STK8321_ADDRESS;
    I2C_InitStruct.I2C_Ack           = I2C_Ack_Enable;

    I2C_Init(I2C0, &I2C_InitStruct);
    I2C_Cmd(I2C0, ENABLE);
}

Parameters:
  • I2Cx[in] Where x can be 0 or 1 to select the I2C peripheral.

  • I2C_InitStruct[in] Pointer to an I2C_InitTypeDef structure that contains the configuration information for the specified I2C peripheral.

Returns:

None.

void I2C_Cmd(I2C_TypeDef *I2Cx, FunctionalState NewState)

Enable or disable the specified I2C peripheral.

Example usage

void driver_i2c0_init(void)
{
    RCC_PeriphClockCmd(APBPeriph_I2C0, APBPeriph_I2C0_CLOCK, ENABLE);

    I2C_InitTypeDef  I2C_InitStruct;
    I2C_StructInit(&I2C_InitStruct);

    I2C_InitStruct.I2C_ClockSpeed    = 100000;
    I2C_InitStruct.I2C_DeviveMode    = I2C_DeviveMode_Master;
    I2C_InitStruct.I2C_AddressMode   = I2C_AddressMode_7BIT;
    I2C_InitStruct.I2C_SlaveAddress  = STK8321_ADDRESS;
    I2C_InitStruct.I2C_Ack           = I2C_Ack_Enable;

    I2C_Init(I2C0, &I2C_InitStruct);
    I2C_Cmd(I2C0, ENABLE);
}

Parameters:
  • I2Cx[in] Where x can be 0 or 1 to select the I2C peripheral.

  • NewState[in] New state of the I2Cx peripheral. This parameter can be: ENABLE or DISABLE.

Returns:

None.

void I2C_StructInit(I2C_InitTypeDef *I2C_InitStruct)

Fill each I2C_InitStruct member with its default value.

Example usage

void driver_i2c0_init(void)
{
    RCC_PeriphClockCmd(APBPeriph_I2C0, APBPeriph_I2C0_CLOCK, ENABLE);

    I2C_InitTypeDef  I2C_InitStruct;
    I2C_StructInit(&I2C_InitStruct);

    I2C_InitStruct.I2C_ClockSpeed    = 100000;
    I2C_InitStruct.I2C_DeviveMode    = I2C_DeviveMode_Master;
    I2C_InitStruct.I2C_AddressMode   = I2C_AddressMode_7BIT;
    I2C_InitStruct.I2C_SlaveAddress  = STK8321_ADDRESS;
    I2C_InitStruct.I2C_Ack           = I2C_Ack_Enable;

    I2C_Init(I2C0, &I2C_InitStruct);
    I2C_Cmd(I2C0, ENABLE);
}

Parameters:

I2C_InitStruct[in] Pointer to an I2C_InitTypeDef structure which will be initialized.

Returns:

None.

I2C_Status I2C_MasterWrite(I2C_TypeDef *I2Cx, uint8_t *pBuf, uint16_t len)

Send data in master mode through the I2Cx peripheral.

Example usage

void i2c0_demo(void)
{
    uint8_t data[10] = {0x01,0x02,0x03,0x04};
    I2C_MasterWrite(I2C0, data, 4);
}

Parameters:
  • I2Cx[in] Where x can be 0 or 1 to select the I2C peripheral.

  • pBuf[in] Bytes to be transmitted.

  • len[in] Data length to send.

Returns:

The status of I2Cx, which refers to I2C Status.

I2C_Status I2C_MasterRead(I2C_TypeDef *I2Cx, uint8_t *pBuf, uint16_t len)

Read data in master mode through the I2Cx peripheral.

Example usage

void i2c0_demo(void)
{
    uint8_t data[10] = {0};
    I2C_MasterRead(I2C0, data, 10);
}

Parameters:
  • I2Cx[in] Where x can be 0 or 1 to select the I2C peripheral.

  • pBuf[in] Data buffer to receive data.

  • len[in] Read data length.

Returns:

The status of I2Cx, which refers to I2C Status.

I2C_Status I2C_RepeatRead(I2C_TypeDef *I2Cx, uint8_t *pWriteBuf, uint16_t Writelen, uint8_t *pReadBuf, uint16_t Readlen)

Send data and read data in master mode through the I2Cx peripheral.

Example usage

void i2c0_demo(void)
{
    uint8_t tx_data[10] = {0x01,0x02,0x03,0x04};
    uint8_t rx_data[10] = {0};
    I2C_RepeatRead(I2C0, tx_data, 4, rx_data, 10);
}

Note

Attention: Read data with time-out mechanism.

Parameters:
  • I2Cx[in] Where x can be 0 or 1 to select the I2C peripheral.

  • pWriteBuf[in] Data buffer to send before read.

  • Writelen[in] Send data length.

  • pReadBuf[out] Data buffer to receive.

  • Readlen[in] Receive data length.

Returns:

The status of I2Cx, which refers to I2C Status.

void I2C_INTConfig(I2C_TypeDef *I2Cx, uint16_t I2C_IT, FunctionalState NewState)

Enable or disable the specified I2C interrupt.

Example usage

void i2c0_demo(void)
{
    I2C_ClearINTPendingBit(I2C0, I2C_INT_STOP_DET);
    I2C_INTConfig(I2C0, I2C_INT_STOP_DET, ENABLE);

    NVIC_InitTypeDef NVIC_InitStruct;
    NVIC_InitStruct.NVIC_IRQChannel = I2C0_IRQn;
    NVIC_InitStruct.NVIC_IRQChannelPriority = 3;
    NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStruct);
}

Parameters:
  • I2Cx[in] Where x can be 0 or 1.

  • I2C_IT[in] This parameter can be one of the following values, which refer to I2C Interrupts Definition.

    • I2C_INT_GEN_CALL: When a General Call address is received and it is acknowledged.

    • I2C_INT_START_DET: When a START or RESTART condition has occurred on the I2C interface regardless of whether I2C is operating in slave or master mode.

    • I2C_INT_STOP_DET: When a STOP condition has occurred on the I2C interface regardless of whether I2C is operating in slave or master mode.

    • I2C_INT_ACTIVITY: When I2C is active on the bus. Stays set until it is cleared.

    • I2C_INT_RX_DONE: When the I2C is acting as a slave-transmitter and the master does not acknowledge a transmitted byte. This occurs on the last byte of the transmission, indicating that the transmission is done.

    • I2C_INT_TX_ABRT: When an I2C transmitter is unable to complete the intended actions on the contents of the transmit FIFO.

    • I2C_INT_RD_REQ: When I2C is acting as a slave and another I2C master is attempting to read data from I2C.

    • I2C_INT_TX_EMPTY: When the transmit buffer is at or below the threshold value set in the REG_IC_TXFLR register.

    • I2C_INT_TX_OVER: When transmit buffer is filled to IC_TX_BUFFER_DEPTH and the processor attempts to issue another I2C command by writing to the REG_IC_DATA_CMD register.

    • I2C_INT_RX_FULL: When the receive buffer reaches or goes above the RX_TL threshold in the REG_IC_RX_TL register. A value of 0 sets the threshold for 1 entry, and a value of 255 sets the threshold for 256 entries.

    • I2C_INT_RX_OVER: When the receive buffer is completely filled to IC_RX_BUFFER_DEPTH and an additional byte is received from an external I2C device.

    • I2C_INT_RX_UNDER: When the processor attempts to read the receive buffer when it is empty by reading from the REG_IC_DATA_CMD register.

  • NewState[in] New state of the I2C interrupt. This parameter can be: ENABLE or DISABLE.

Returns:

None.

void I2C_ClearINTPendingBit(I2C_TypeDef *I2Cx, uint16_t I2C_IT)

Clear the specified I2C interrupt pending bit.

Example usage

void I2C0_Handler(void)
{
    if (I2C_GetINTStatus(I2C0, I2C_INT_STOP_DET) == SET)
    {
        //Add user code here.
        I2C_ClearINTPendingBit(I2C0, I2C_INT_STOP_DET);
    }
}

Parameters:
  • I2Cx[in] Where x can be 0 or 1.

  • I2C_IT[in] This parameter can be one of the following values, which refer to I2C Interrupts Definition.

    • I2C_INT_GEN_CALL: When a General Call address is received and it is acknowledged.

    • I2C_INT_START_DET: When a START or RESTART condition has occurred on the I2C interface regardless of whether I2C is operating in slave or master mode.

    • I2C_INT_STOP_DET: When a STOP condition has occurred on the I2C interface regardless of whether I2C is operating in slave or master mode.

    • I2C_INT_ACTIVITY: When I2C is active on the bus. Stays set until it is cleared.

    • I2C_INT_RX_DONE: When the I2C is acting as a slave-transmitter and the master does not acknowledge a transmitted byte. This occurs on the last byte of the transmission, indicating that the transmission is done.

    • I2C_INT_TX_ABRT: When an I2C transmitter is unable to complete the intended actions on the contents of the transmit FIFO.

    • I2C_INT_RD_REQ: When I2C is acting as a slave and another I2C master is attempting to read data from I2C.

    • I2C_INT_TX_EMPTY: When the transmit buffer is at or below the threshold value set in the REG_IC_TXFLR register.

    • I2C_INT_TX_OVER: When transmit buffer is filled to IC_TX_BUFFER_DEPTH and the processor attempts to issue another I2C command by writing to the REG_IC_DATA_CMD register.

    • I2C_INT_RX_FULL: When the receive buffer reaches or goes above the RX_TL threshold in the REG_IC_RX_TL register. A value of 0 sets the threshold for 1 entry, and a value of 255 sets the threshold for 256 entries.

    • I2C_INT_RX_OVER: When the receive buffer is completely filled to IC_RX_BUFFER_DEPTH and an additional byte is received from an external I2C device.

    • I2C_INT_RX_UNDER: When the processor attempts to read the receive buffer when it is empty by reading from the REG_IC_DATA_CMD register.

Returns:

None.

void I2C_SetSlaveAddress(I2C_TypeDef *I2Cx, uint16_t Address)

Set slave device address.

Example usage

void i2c0_demo(void)
{
    I2C_SetSlaveAddress(I2C0, 0x55);
}

Parameters:
  • I2Cx[in] Where x can be 0 or 1 to select the I2C peripheral.

  • Address[in] Specify the slave address which will be transmitted.

Returns:

None.

void I2C_SendCmd(I2C_TypeDef *I2Cx, uint16_t command, uint8_t data, uint16_t StopState)

Write command through the I2Cx peripheral.

Example usage

void i2c0_demo(void)
{
    I2C_SendCmd(I2C0, I2C_READ_CMD, 0xAA, I2C_STOP_ENABLE);
}

Parameters:
  • I2Cx[in] where x can be 0 or 1 to select the I2C peripheral.

  • command[in] command of write or read.

    • I2C_READ_CMD: means this is a read command.

    • I2C_WRITE_CMD: means this is a write command.

  • data[in] Data which is to be transmitted.

  • StopState[in] send stop signal or not.

    • I2C_STOP_ENABLE: Send stop signal.

    • I2C_STOP_DISABLE: Do not send stop signal.

Returns:

None.

uint8_t I2C_ReceiveData(I2C_TypeDef *I2Cx)

Received data by the I2Cx peripheral.

Example usage

void i2c0_demo(void)
{
    uint8_t data = I2C_ReceiveData(I2C0);
}

Parameters:

I2Cx[in] where x can be 0 or 1 to select the I2C peripheral.

Returns:

The most recent received data.

uint8_t I2C_GetRxFIFOLen(I2C_TypeDef *I2Cx)

Get data length in Rx FIFO of the I2Cx peripheral.

Example usage

void i2c0_demo(void)
{
    uint8_t data_len = I2C_GetRxFIFOLen(I2C0);
}

Parameters:

I2Cx[in] where x can be 0 or 1 to select the I2C peripheral.

Returns:

The data length in Rx FIFO.

uint8_t I2C_GetTxFIFOLen(I2C_TypeDef *I2Cx)

Get data length in Tx FIFO of the I2Cx peripheral.

Example usage

void i2c0_demo(void)
{
    uint8_t data_len = I2C_GetTxFIFOLen(I2C0);
}

Parameters:

I2Cx[in] where x can be 0 or 1 to select the I2C peripheral.

Returns:

The data length in Tx FIFO.

void I2C_ClearAllINT(I2C_TypeDef *I2Cx)

Clear all I2C interrupts.

Example usage

void i2c0_demo(void)
{
    I2C_ClearAllINT(I2C0);
}

Parameters:

I2Cx[in] where x can be 0 or 1 to select the I2C peripheral.

Returns:

None.

FlagStatus I2C_GetFlagState(I2C_TypeDef *I2Cx, uint32_t I2C_FLAG)

Check whether the specified I2C flag is set.

Example usage

void i2c0_demo(void)
{
    FlagStatus flag_status = I2C_GetFlagState(I2C0, I2C_FLAG_RFF);
}

Parameters:
  • I2Cx[in] Where x can be 0 or 1 to select the I2C peripheral.

  • I2C_FLAG[in] Specify the flag to check. This parameter can be one of the following values, which refer to I2C Flags Definition.

    • I2C_FLAG_SLV_ACTIVITY: Slave FSM activity status.

    • I2C_FLAG_MST_ACTIVITY: Master FSM activity status.

    • I2C_FLAG_RFF: Receive FIFO completely full.

    • I2C_FLAG_RFNE: Receive FIFO not empty.

    • I2C_FLAG_TFE: Transmit FIFO completely empty.

    • I2C_FLAG_TFNF: Transmit FIFO not full.

    • I2C_FLAG_ACTIVITY: I2C activity status.

Returns:

The new state of I2C_FLAG (SET or RESET).

FlagStatus I2C_CheckEvent(I2C_TypeDef *I2Cx, uint32_t I2C_EVENT)

Check whether the last I2Cx event is equal to the passed parameter.

Example usage

void i2c0_demo(void)
{
    FlagStatus flag_status = I2C_CheckEvent(I2C0, ABRT_SLVRD_INTX);
}

Parameters:
  • I2Cx[in] Where x can be 0 or 1 to select the I2C peripheral.

  • I2C_EVENT[in] Specify the event to be checked about I2C Transmit Abort Status Register. This parameter can be one of the following values, which refer to I2C Transmit Abort Source.

    • ABRT_SLVRD_INTX: When the processor side responds to a slave mode request for data to be transmitted to a remote master and user send read command.

    • ABRT_SLV_ARBLOST: Slave lost the bus while transmitting data to a remote master.

    • ABRT_SLVFLUSH_TXFIFO: Slave has received a read command and some data exists in the TX FIFO so the slave issues a TX_ABRT interrupt to flush old data in TX FIFO.

    • ARB_LOST: Master has lost arbitration or the slave transmitter has lost arbitration.

    • ABRT_MASTER_DIS: User tries to initiate a Master operation with the Master mode disabled

    • ABRT_10B_RD_NORSTRT: The restart is disabled and the master sends a read command in 10-bit addressing mode.

    • ABRT_SBYTE_NORSTRT: The restart is disabled and the user is trying to send a START Byte.

    • ABRT_HS_NORSTRT: The restart is disabled and the user is trying to use the master to transfer data in High Speed mode.

    • ABRT_SBYTE_ACKDET: Master has sent a START Byte and the START Byte was acknowledged (wrong behavior).

    • ABRT_HS_ACKDET: Master is in High Speed mode and the High Speed Master code was acknowledged (wrong behavior).

    • ABRT_GCALL_READ: Sent a General Call but the user programmed the byte following the General Call to be a read from the bus.

    • ABRT_GCALL_NOACK: Sent a General Call and no slave on the bus acknowledged the General Call.

    • ABRT_TXDATA_NOACK: Master sent data byte(s) following the address, it did not receive an acknowledge from the remote slave.

    • ABRT_10ADDR2_NOACK: Master is in 10-bit address mode and the second address byte of the 10-bit address was not acknowledged by any slave.

    • ABRT_10ADDR1_NOACK: Master is in 10-bit address mode and the first 10-bit address byte was not acknowledged by any slave.

    • ABRT_7B_ADDR_NOACK: Master is in 7-bit addressing mode and the address sent was not acknowledged by any slave.

Returns:

An ErrorStatus enumeration value, SET(Last event is equal to the I2C_EVENT) or RESET(Last event is different from the I2C_EVENT).

ITStatus I2C_GetINTStatus(I2C_TypeDef *I2Cx, uint32_t I2C_IT)

Get the specified I2C interrupt status.

Example usage

void i2c0_demo(void)
{
    ITStatus int_status = I2C_GetINTStatus(I2C0, I2C_INT_RD_REQ);
}

Parameters:
  • I2Cx[in] Where x can be 0 or 1 to select the I2C peripheral.

  • I2C_IT[in] This parameter can be one of the following values, which refer to I2C Interrupts Definition.

    • I2C_INT_GEN_CALL: When a General Call address is received and it is acknowledged.

    • I2C_INT_START_DET: When a START or RESTART condition has occurred on the I2C interface regardless of whether I2C is operating in slave or master mode.

    • I2C_INT_STOP_DET: When a STOP condition has occurred on the I2C interface regardless of whether I2C is operating in slave or master mode.

    • I2C_INT_ACTIVITY: When I2C is active on the bus. Stays set until it is cleared.

    • I2C_INT_RX_DONE: When the I2C is acting as a slave-transmitter and the master does not acknowledge a transmitted byte. This occurs on the last byte of the transmission, indicating that the transmission is done.

    • I2C_INT_TX_ABRT: When an I2C transmitter is unable to complete the intended actions on the contents of the transmit FIFO.

    • I2C_INT_RD_REQ: When I2C is acting as a slave and another I2C master is attempting to read data from I2C.

    • I2C_INT_TX_EMPTY: When the transmit buffer is at or below the threshold value set in the REG_IC_TXFLR register.

    • I2C_INT_TX_OVER: When the transmit buffer is filled to IC_TX_BUFFER_DEPTH and the processor attempts to issue another I2C command by writing to the REG_IC_DATA_CMD register.

    • I2C_INT_RX_FULL: When the receive buffer reaches or goes above the RX_TL threshold in the REG_IC_RX_TL register. A value of 0 sets the threshold for 1 entry, and a value of 255 sets the threshold for 256 entries.

    • I2C_INT_RX_OVER: When the receive buffer is completely filled to IC_RX_BUFFER_DEPTH and an additional byte is received from an external I2C device.

    • I2C_INT_RX_UNDER: When the processor attempts to read the receive buffer when it is empty by reading from the REG_IC_DATA_CMD register.

Returns:

The new state of I2C_IT (SET or RESET).

void I2C_GDMACmd(I2C_TypeDef *I2Cx, uint16_t I2C_GDMAReq, FunctionalState NewState)

Enable or disable the I2Cx GDMA interface.

Example usage

void i2c0_demo(void)
{
    I2C_GDMACmd(I2C0, I2C_GDMAReq_Tx, ENABLE);
}

Parameters:
  • I2Cx[in] Where x can be 0 or 1 to select the I2C peripheral.

  • I2C_GDMAReq[in] Specify the I2C GDMA transfer request to be enabled or disabled. This parameter can be one of the following values:

    • I2C_GDMAReq_Tx: Tx buffer GDMA transfer request.

    • I2C_GDMAReq_Rx: Rx buffer GDMA transfer request.

  • NewState[in] New state of the selected I2C GDMA transfer request. This parameter can be: ENABLE or DISABLE.

Returns:

None.