STM32F4 I2C API

group STM32F4_HAL_I2C

Defines

FLAG_CHECK_TIMEOUT 200

Time out value used when checking if bit flags are set.

Functions

static hal_i2c_handle *hal_i2c_get_handle(KI2CNum num)

Fetches I2C bus data structure.

Return
hal_i2c_handle* pointer to data structure
Parameters
  • num: I2C bus num to fetch

static hal_i2c_handle *hal_i2c_device_init(KI2C *i2c)

Initializes I2C bus structure with data needed to setup hardware.

Return
hal_i2c_handle* NULL if bad bus num, otherwise data ready for dev setup
Parameters
  • i2c: higher level HAL I2C data

static KI2CStatus hal_i2c_hw_init(hal_i2c_handle *handle)

Initializes the I2C according to the specified parameters in the I2C_InitTypeDef and create the associated handle.

Note
Derived from STM32CubeF4’s HAL_I2C_INIT
Return
KI2CStatus I2C_OK if success, otherwise a specific error flag
Parameters

static void hal_i2c_msp_init(hal_i2c_handle *handle)

Low level I2C hardware setup.

Note
Derived from STM32CubeF4’s HAL_I2C_MspInit
Parameters

static void hal_i2c_terminate(hal_i2c_handle *handle)

I2C hardware cleanup and disabling.

Parameters

static KI2CStatus hal_i2c_check_addr_timeout(I2C_HandleTypeDef *handle, uint32_t flag)

Checks for the addr flag to be set, with builtin timeout.

Note
Derived from STM32CubeF4’s I2C_WaitOnMasterAddressFlagUntilTimeout
Return
KI2CStatus I2C_OK if success, otherwise a specific error flag
Parameters
  • handle: Pointer to STM32CubeF4 HAL defined structure for I2C data
  • flag: I2C flag to check

static KI2CStatus hal_i2c_check_flag_special(I2C_HandleTypeDef *handle, uint32_t flag)

Checks for special conditions based on the flag.

Return
KI2CStatus I2C_OK if no special conditions found, specific error otherwise
Parameters
  • handle: Pointer to STM32CubeF4 HAL defined structure for I2C data
  • flag: I2C Flag that should be checked

static KI2CStatus hal_i2c_check_flag_timeout(I2C_HandleTypeDef *handle, uint32_t flag, uint16_t status)

Checks specified flag for desired state, with builtin timeout.

Note
Derived from STM32CubeF4’s I2C_WaitOnFlagUntilTimeout
Return
KI2CStatus I2C_OK if flag is set to desired value within timout, otherwise I2C_TIMEOUT
Parameters
  • handle: Pointer to STM32CubeF4 HAL defined structure for I2C data
  • flag: I2C Flag that should be checked
  • status: Indicates whether to check for flag state of SET or RESET

static KI2CStatus hal_i2c_check_btf_timeout(I2C_HandleTypeDef *handle)

Checks for btf flag to reset, with builtin timeout.

Note
Derived from STM32CubeF4’s I2C_WaitOnBTFFlagUntilTimeout
Return
KI2CStatus I2C_OK if btf is reset within timeout, otherwise specific error
Parameters
  • handle: Pointer to STM32CubeF4 HAL defined structure for I2C data

static KI2CStatus hal_i2c_check_txe_timeout(I2C_HandleTypeDef *handle)

Checks for txe flag to reset, with builtin timeout.

Note
Derived from STM32CubeF4’s I2C_WaitOnBTFFlagUntilTimeout
Return
KI2CStatus I2C_OK if txe is reset within timeout, otherwise specific error
Parameters
  • handle: Pointer to STM32CubeF4 HAL defined structure for I2C data

static KI2CStatus hal_i2c_master_request_read(I2C_HandleTypeDef *hal_handle, uint16_t addr)

Master sends slave address for read request.

Note
Derived from STM32CubeF4’s I2C_MasterRequestRead
Return
KI2CStatus I2C_OK if success, otherwise specific error
Parameters
  • hal_handle: Pointer to STM32CubeF4 HAL defined structure for I2C data
  • addr: target slave address

static KI2CStatus hal_i2c_master_setup_read(I2C_HandleTypeDef *hal_handle, uint16_t addr, int len)

Sends initial receive sequence based on length of data expected.

Note
Partly derived from STM32CubeF4’s HAL_I2C_Master_Receive
Return
KI2CStatus I2C_OK if success, otherwise specific error
Parameters
  • hal_handle: Pointer to STM32CubeF4 HAL defined structure for I2C data
  • addr: target slave address
  • len: length of data expected to be received

static KI2CStatus hal_i2c_master_request_write(I2C_HandleTypeDef *hal_handle, uint16_t addr)

Master sends slave address for write request.

Note
Derived from STM32CubeF4’s I2C_MasterRequestWrite
Return
KI2CStatus I2C_OK if success, otherwise specific error
Parameters
  • hal_handle: Pointer to STM32CubeF4 HAL defined structure for I2C data
  • addr: target slave address

static KI2CStatus hal_i2c_master_setup_write(I2C_HandleTypeDef *hal_handle, uint16_t addr)

Sends initial transmit sequence.

Note
Derived from STM32CubeF4’s HAL_I2C_Master_Transmit
Return
KI2CStatus I2C_OK if success, otherwise specific error
Parameters
  • hal_handle: Pointer to STM32CubeF4 HAL defined structure for I2C data
  • addr: target slave address

KI2CStatus kprv_i2c_dev_init(KI2CNum i2c_num)

Setup and enable I2C bus.

Return
KI2CStatus I2C_OK if success, otherwise a specific error flag
Parameters
  • i2c_num: I2C bus to initialize

KI2CStatus kprv_i2c_dev_terminate(KI2CNum i2c)

I2C hardware cleanup and disabling.

Return
KI2CStatus I2C_OK if success, otherwise a specific error flag
Parameters
  • i2c: bus num to terminate

KI2CStatus kprv_i2c_master_write(KI2CNum i2c, uint16_t addr, uint8_t *ptr, int len)

Write data over I2C bus as master.

Return
KI2CStatus I2C_OK on success, otherwise failure
Parameters
  • i2c: I2C bus to write to
  • addr: I2C address to write to
  • ptr: pointer to data buffer
  • len: length of data to write

KI2CStatus kprv_i2c_master_read(KI2CNum i2c, uint16_t addr, uint8_t *ptr, int len)

Read data over I2C bus as master.

Return
KI2CStatus I2C_OK on success, otherwise failure
Parameters
  • i2c: I2C bus to read from
  • addr: I2C address to write to
  • ptr: pointer to data buffer
  • len: length of data to read

Variables

hal_i2c_handle hal_i2c_bus[K_NUM_I2CS]

Static array of I2C bus handles.

struct hal_i2c_pins
#include <i2c.h>

Structure defining pins + config for I2C device.

Public Members

uint16_t scl

SCL pin number.

uint16_t scl_mode

SCL pin mode.

uint16_t scl_pullup

SCL pin pullup setting.

uint16_t scl_speed

SCL pin speed.

uint16_t sda

SDA pin number.

uint16_t sda_mode

SDA pin mode.

uint16_t sda_pullup

SDA pin pullup setting.

uint16_t sda_speed

SDA pin speed.

uint16_t alt

Alternate pin number.

uint16_t ev_irqn

EV IRQ number.

uint16_t er_irqn

ER IRQ number.

GPIO_TypeDef *gpio_port

STM32CubeF4 defined GPIO port.

struct hal_i2c_handle
#include <i2c.h>

HAL structure for I2C device definition/configuration.

Public Members

KI2C *ki2c

Kubos-HAL structure.

I2C_HandleTypeDef hal_handle

STM32CubeF4 HAL structure.

hal_i2c_pins pins

GPIO config pins/ports.