I2C Exported Functions

group I2C_Exported_Functions

Functions

void I2C_DeInit(I2C_TypeDef *I2Cx)

Deinitializes the I2Cx peripheral registers to their default reset values.

Example usage

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

参数:

I2Cx -- [in] Select the I2C peripheral. I2C Declaration

返回:

None.

void I2C_Init(I2C_TypeDef *I2Cx, I2C_InitTypeDef *I2C_InitStruct)

Initializes 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);
}

参数:
  • I2Cx -- [in] Select the I2C peripheral. I2C Declaration

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

返回:

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);
}

参数:
  • I2Cx -- [in] Select the I2C peripheral. I2C Declaration

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

返回:

None.

I2C_Status I2C_CheckAbortStatus(I2C_TypeDef *I2Cx)

Enable or disable the specified I2C peripheral.

Example usage

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

参数:

I2Cx -- [in] Select the I2C peripheral. I2C Declaration

返回:

I2C_Status: The status of I2Cx. I2C Status

void I2C_StructInit(I2C_InitTypeDef *I2C_InitStruct)

Fills 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);
}

参数:

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

返回:

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,x0x02,0x03,0x04};
    I2C_MasterWrite(I2C0, data, 4);
}

参数:
  • I2Cx -- [in] Select the I2C peripheral. I2C Declaration

  • pBuf -- [in] Byte to be transmitted.

  • len -- [in] Data length to send.

返回:

I2C_Status: The status of I2Cx.

I2C_Status I2C_MasterWriteDevice(I2C_TypeDef *I2Cx, uint8_t *pBuf, uint16_t len, uint8_t *pbuf2, uint32_t len2)

Read data in master mode through the I2Cx peripheral.

rtl876x_i2c.h

Example usage

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

参数:
  • I2Cx -- [in] Select the I2C peripheral. I2C Declaration

  • pBuf -- [in] Byte to be transmitted.

  • len -- [in] Data length to send.

  • pBuf2 -- [in] Byte to be transmitted.

  • len2 -- [in] Data length to send.

返回:

I2C status, please refer 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);
}

参数:
  • I2Cx -- [in] Select the I2C peripheral. I2C Declaration

  • pBuf -- [in] Data buffer to receive data.

  • len -- [in] Read data length.

返回:

I2C_Status: The status of I2Cx.

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

Sends data and read data in master mode through the I2Cx peripheral. Attention:Read data with time out mechanism.

Example usage

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

参数:
  • I2Cx -- [in] Select the I2C peripheral. I2C Declaration

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

  • Writelen -- [in] Send data length.

  • pReadBuf -- [in] Data buffer to receive.

  • Readlen -- [in] Receive data length.

返回:

I2C_Status: The status of I2Cx.

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

Mask 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);
}

参数:
  • I2Cx -- [in] Select the I2C peripheral. I2C Declaration

  • I2C_IT -- [in] This parameter can be one of the following values:

    • I2C_INT_MST_ON_HOLD: Indicates whether a master is holding the bus.

    • I2C_INT_GEN_CALL: Set only when a General Call address is received and it is acknowledged.

    • I2C_INT_START_DET: Indicates whether 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: Indicates whether a STOP condition has occurred on the I2C interface regardless of whether I2C is operating in slave or master mode

    • I2C_INT_ACTIVITY: This bit captures I2C activity and stays set until it is cleared.

    • I2C_INT_RX_DONE: When the I2C is acting as a slave-transmitter, this bit is set to 1 if 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: This bit indicates if I2C as an I2C transmitter, is unable to complete the intended actions on the contents of the transmit FIFO.

    • I2C_INT_RD_REQ: This bit is set to 1 when acting as a slave and another I2C master is attempting to read data.

    • I2C_INT_TX_EMPTY: This bit is set to 1 when the transmit buffer is at or below the threshold value set in the IC_TX_TL register.

    • I2C_INT_TX_OVER: Set during transmit if the transmit buffer is filled to IC_TX_BUFFER_DEPTH and the processor attempts to issue another I2C command.

    • I2C_INT_RX_FULL: Set when the receive buffer reaches or goes above the RX_TL threshold in the IC_RX_TL register

    • I2C_INT_RX_OVER: Set if 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: Set if the processor attempts to read the receive buffer when it is empty by reading.

  • NewState -- [in] Disable or enable I2C interrupt.

返回:

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);
    }
}

参数:
  • I2Cx -- [in] Select the I2C peripheral. I2C Declaration

  • I2C_IT -- [in] This parameter can be one of the following values:

    • I2C_INT_GEN_CALL: Set only when a General Call address is received and it is acknowledged.

    • I2C_INT_START_DET: Indicates whether 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: Indicates whether a STOP condition has occurred on the I2C interface regardless of whether I2C is operating in slave or master mode

    • I2C_INT_ACTIVITY: This bit captures I2C activity and stays set until it is cleared.

    • I2C_INT_RX_DONE: When the I2C is acting as a slave-transmitter, this bit is set to 1 if 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: This bit indicates if I2C as an I2C transmitter, is unable to complete the intended actions on the contents of the transmit FIFO.

    • I2C_INT_RD_REQ: This bit is set to 1 when acting as a slave and another I2C master is attempting to read data.

    • I2C_INT_TX_EMPTY: This bit is set to 1 when the transmit buffer is at or below the threshold value set in the IC_TX_TL register.

    • I2C_INT_TX_OVER: Set during transmit if the transmit buffer is filled to IC_TX_BUFFER_DEPTH and the processor attempts to issue another I2C command.

    • I2C_INT_RX_FULL: Set when the receive buffer reaches or goes above the RX_TL threshold in the IC_RX_TL register

    • I2C_INT_RX_OVER: Set if 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: Set if the processor attempts to read the receive buffer when it is empty by reading.

返回:

None.

void I2C_SetSlaveAddress(I2C_TypeDef *I2Cx, uint16_t Address)

Set slave device address.

Example usage

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

参数:
  • I2Cx -- [in] Select the I2C peripheral. I2C Declaration

  • Address -- [in] Specifies the slave address which will be transmitte.

返回:

None.

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

Write command through the I2Cx peripheral.

Example usage

void i2c0_demo(void)
{
    I2C_SendCmd(I2C0, 0x01, 0xAA, ENABLE);
}

参数:
  • I2Cx -- [in] Select the I2C peripheral. I2C Declaration

  • command -- [in] Command of write or read.

    • I2C_READ_CMD: Read command. Data which want to transmit can be 0 in this situation.

    • I2C_WRITE_CMD: Write command.

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

  • StopState -- [in] Whether send stop signal.

    • ENABLE: Send stop signal.

    • DISABLE: Do not send stop signal.

返回:

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);
}

参数:

I2Cx -- [in] Select the I2C peripheral. I2C Declaration

返回:

Return 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);
}

参数:

I2Cx -- [in] Select the I2C peripheral. I2C Declaration

返回:

Current data number 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);
}

参数:

I2Cx -- [in] Select the I2C peripheral. I2C Declaration

返回:

Current data number in Tx FIFO.

void I2C_ClearAllINT(I2C_TypeDef *I2Cx)

Clear all I2C interrupt.

Example usage

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

参数:

I2Cx -- [in] Select the I2C peripheral. I2C Declaration

返回:

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);
}

参数:
  • I2Cx -- [in] Select the I2C peripheral. I2C Declaration

  • I2C_FLAG -- [in] Specifies the flag to check. This parameter can be one of the following values:

    • I2C_FLAG_SLV_HOLD_RX_FIFO_FULL: The BUS Hold in Slave mode due to the Rx FIFO being Full and an additional byte being received.

    • I2C_FLAG_SLV_HOLD_TX_FIFO_EMPTY: The BUS Hold in Slave mode for the Read request when the Tx FIFO is empty.

    • I2C_FLAG_MST_HOLD_RX_FIFO_FULL: The BUS Hold in Master mode due to Rx FIFO is full and additional byte has been received.

    • I2C_FLAG_MST_HOLD_TX_FIFO_EMPTY: The BUS hold when the master holds the bus because of the Tx FIFO being empty.

    • 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.

返回:

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 one passed as parameter.

Example usage

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

参数:
  • I2Cx -- [in] Select the I2C peripheral. I2C Declaration

  • I2C_EVENT -- [in] specifies the event to be checked about I2C Transmit Abort Status Register. This parameter can be one of the following values:

    • 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 th address sent was not acknowledged by any slave.

返回值:
  • SUCCESS -- Last event is equal to the I2C_EVENT.

  • ERROR -- Last event is different from the I2C_EVENT.

返回:

An ErrorStatus enumeration value.

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);
}

参数:
  • I2Cx -- [in] Select the I2C peripheral. I2C Declaration

  • I2C_IT -- [in] This parameter can be one of the following values:

    • I2C_INT_MST_ON_HOLD: Indicates whether a master is holding the bus.

    • I2C_INT_GEN_CALL: Set only when a General Call address is received and it is acknowledged.

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

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

    • I2C_INT_ACTIVITY: This bit captures DW_apb_i2c activity and stays set until it is cleared.

    • I2C_INT_RX_DONE: When the DW_apb_i2c is acting as a slave-transmitter, this bit is set to 1 if 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: This bit indicates if DW_apb_i2c, as an I2C transmitter, is unable to complete the intended actions on the contents of the transmit FIFO.

    • I2C_INT_RD_REQ: This bit is set to 1 when acting as a slave and another I2C master is attempting to read data.

    • I2C_INT_TX_EMPTY: This bit is set to 1 when the transmit buffer is at or below the threshold value set in the IC_TX_TL register.

    • I2C_INT_TX_OVER: Set during transmit if the transmit buffer is filled to IC_TX_BUFFER_DEPTH and the processor attempts to issue another I2C command.

    • I2C_INT_RX_FULL: Set when the receive buffer reaches or goes above the RX_TL threshold in the IC_RX_TL register

    • I2C_INT_RX_OVER: Set if 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: Set if the processor attempts to read the receive buffer when it is empty by reading.

返回:

The new state of I2C_IT (SET or RESET).

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

Enable or disable the I2Cx GDMA interface.

Example usage

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

参数:
  • I2Cx -- [in] Select the I2C peripheral. I2C Declaration

  • I2C_GDMAReq -- [in] Specifies 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.

返回:

None.

void I2C_SetClockSpeed(I2C_TypeDef *I2Cx, uint32_t I2C_ClockSpeed)

Set the I2C clock speed, the function need to be called when I2C disabled.

Example usage

void i2c_demo(void)
{
    I2C_SetClockSpeed(I2C0, 400000);
}

参数:
  • I2Cx -- [in] Select the I2C peripheral. I2C Declaration

  • I2C_ClockSpeed -- [in] Secified the I2C interrupt sources.

返回:

None.

void I2C_ClkDivConfig(I2C_TypeDef *I2Cx, uint16_t ClockDiv)

I2C clock divider config.

Example usage

void driver_i2c_init(void)
{
    I2C_ClkDivConfig(I2C0, CLOCK_DIV_1);
}

参数:
  • I2Cx -- [in] Select the I2C peripheral. I2C Declaration

  • ClockDiv -- [in] Specifies the I2C clock divider. This parameter can be one of the following values:

    • CLOCK_DIV_x: where x can refer to CLock Divider to select the specified clock divider

  • I2Cx -- Select the I2C peripheral. I2C Declaration

  • ClockDiv -- specifies the APB peripheral to gates its clock. Clock Divider

返回:

None.

返回:

None