通信总线组件 (Bus)

[English]

通信总线组件 (Bus) 是建立在 ESP-IDF 外设驱动代码之上的一套应用层代码,包括 i2c_busspi_bus 等,主要用于 ESP 芯片与外置设备之间的总线通信。该组件从应用开发的角度出发,实现了以下功能:

  1. 简化外设初始化步骤

  2. 线程安全的设备操作

  3. 简单灵活的读写操作

该组件对以下概念进行了抽象:

  1. 总线 (Bus):通信时设备之间共同拥有的资源和配置项

  2. 设备 (Device):通信时设备特有的资源和配置项

每个物理外设总线 (Bus) 在电气条件允许的情况下,均可挂载一到多个设备(Device),其中 SPI 总线根据 CS 引脚对设备进行寻址,I2C 总线则根据设备地址进行寻址,进而实现相同总线不同设备之间软件上的独立。

../_images/i2c_bus.png

i2c_bus 连接框图

../_images/spi_bus.png

spi_bus 连接框图

i2c_bus 使用方法

  1. 创建总线:使用 i2c_bus_create() 创建一个总线实例。创建时需要指定 I2C 端口号,以及总线配置项 i2c_config_t。配置项包括 SDA 和 SCL 引脚号、上下拉模式,因为这些配置项在系统设计时已经确定,一般不在运行时切换。总线配置项还包括总线默认的时钟频率,在设备不指定频率时使用。

  2. 创建设备:使用 i2c_bus_device_create() 在已创建的总线实例之上创建设备,创建时需要指定总线句柄、设备的 I2C 地址、设备运行的时钟频率,I2C 传输时将根据设备的配置项动态切换频率。设备时钟速率可配置为 0,表示默认使用当前的总线频率。

  3. 数据读取:使用 i2c_bus_read_byte()i2c_bus_read_bytes() 可直接进行 Byte 的读取操作;使用 i2c_bus_read_bit()i2c_bus_read_bits() 可直接进行 bit 的读取操作。只需要传入设备句柄、设备寄存器地址、用于存放读取数据的 buf 和读取长度的等。寄存器地址可设为 NULL_I2C_MEM_ADDR,用于操作没有内部寄存器的设备。

  4. 数据写入:使用 i2c_bus_write_byte()i2c_bus_write_bytes() 可直接进行 Byte 的写入操作;使用 i2c_bus_write_bit()i2c_bus_write_bits() 可直接进行 bit 的写入操作。只需要传入设备句柄、设备寄存器地址、将要写入的数据位置和写入长度等。寄存器地址可设为 NULL_I2C_MEM_ADDR,用于操作没有内部寄存器的设备。

  5. 删除设备和总线:如果所有的 i2c_bus 通信已经完成,可以通过删除设备和总线实例释放系统资源。可使用 i2c_bus_device_delete() 分别将已创建的设备删除,然后使用 i2c_bus_delete() 将总线资源删除。如果在设备未删除时删除总线,操作将不会被执行。

示例:

i2c_config_t conf = {
    .mode = I2C_MODE_MASTER,
    .sda_io_num = I2C_MASTER_SDA_IO,
    .sda_pullup_en = GPIO_PULLUP_ENABLE,
    .scl_io_num = I2C_MASTER_SCL_IO,
    .scl_pullup_en = GPIO_PULLUP_ENABLE,
    .master.clk_speed = 100000,
}; // i2c_bus configurations

uint8_t data_rd[2] = {0};
uint8_t data_wr[2] = {0x01, 0x21};

i2c_bus_handle_t i2c0_bus = i2c_bus_create(I2C_NUM_0, &conf); // create i2c_bus
i2c_bus_device_handle_t i2c_device1 = i2c_bus_device_create(i2c0_bus, 0x28, 400000); // create device1, address: 0x28 , clk_speed: 400000
i2c_bus_device_handle_t i2c_device2 = i2c_bus_device_create(i2c0_bus, 0x32, 0); // create device2, address: 0x32 , clk_speed: no-specified

i2c_bus_read_bytes(i2c_device1, NULL_I2C_MEM_ADDR, 2, data_rd); // read bytes from device1 with no register address
i2c_bus_write_bytes(i2c_device2, 0x10, 2, data_wr); // write bytes to device2 register 0x10

i2c_bus_device_delete(&i2c_device1); //delete device1
i2c_bus_device_delete(&i2c_device2); //delete device2
i2c_bus_delete(&i2c0_bus);  //delete i2c_bus

注解

对于某些特殊应用场景,例如:

  1. 当寄存器地址为 16 位时,可以使用 i2c_bus_read_reg16()i2c_bus_write_reg16() 进行读写操作;

  2. 对于需要跳过地址阶段或者需要增加命令阶段的设备,可以使用 i2c_bus_cmd_begin() 结合 I2C command link 进行操作。

spi_bus 使用方法

  1. 创建总线:使用 spi_bus_create() 创建一个总线实例,创建时需要指定 SPI 端口号(可选 SPI2_HOSTSPI3_HOST)以及总线配置项 spi_config_t。配置项包括 MOSIMISOSCLK 引脚号,因为这些引脚在系统设计时已经确定,一般不在运行时切换。总线配置项还包括 max_transfer_sz,用于配置一次传输时的最大数据量,设置为 0 将使用默认值 4096。

  2. 创建设备:使用 spi_bus_device_create() 在已创建的总线实例之上创建设备,创建时需要指定总线句柄、设备的 CS 引脚号、设备运行模式、设备运行的时钟频率,SPI 传输时将根据设备的配置项动态切换模式和频率。

  3. 数据传输:使用 spi_bus_transfer_byte()spi_bus_transfer_bytes()spi_bus_transfer_reg16() 以及 spi_bus_transfer_reg32() 可直接进行数据的传输操作。由于 SPI 是全双工通信,因此每次传输发送和接收可以同时进行,只需要传入设备句柄、待发送数据、存放读取数据的 buf 和传输长度。

  4. 删除设备和总线:如果所有的 spi_bus 通信已经完成,可以通过删除设备和总线实例释放系统资源。可使用 spi_bus_device_delete() 分别将已创建的设备删除,然后使用 spi_bus_delete() 将总线资源删除。如果在设备未删除时删除总线,操作将不会被执行。

示例:

spi_bus_handle_t bus_handle = NULL;
spi_bus_device_handle_t device_handle = NULL;
uint8_t data8_in = 0;
uint8_t data8_out = 0xff;
uint16_t data16_in = 0;
uint32_t data32_in = 0;

spi_config_t bus_conf = {
    .miso_io_num = 19,
    .mosi_io_num = 23,
    .sclk_io_num = 18,
}; // spi_bus configurations

spi_device_config_t device_conf = {
    .cs_io_num = 19,
    .mode = 0,
    .clock_speed_hz = 20 * 1000 * 1000,
}; // spi_device configurations

bus_handle = spi_bus_create(SPI2_HOST, &bus_conf); // create spi bus
device_handle = spi_bus_device_create(bus_handle, &device_conf); // create spi device

spi_bus_transfer_bytes(device_handle, &data8_out, &data8_in, 1); // transfer 1 byte with spi device
spi_bus_transfer_bytes(device_handle, NULL, &data8_in, 1); // only read 1 byte with spi device
spi_bus_transfer_bytes(device_handle, &data8_out, NULL, 1); // only write 1 byte with spi device
spi_bus_transfer_reg16(device_handle, 0x1020, &data16_in); // transfer 16-bit value with the device
spi_bus_transfer_reg32(device_handle, 0x10203040, &data32_in); // transfer 32-bit value with the device

spi_bus_device_delete(&device_handle);
spi_bus_delete(&bus_handle);

注解

对于某些特殊应用场景,可以直接使用 spi_bus_transmit_begin() 结合 spi_transaction_t 进行操作。

已适配 IDF 版本

  • ESP-IDF v4.0 及以上版本。

已适配芯片

  • ESP32

  • ESP32-S2

API 参考

i2c_bus API 参考

Header File

Functions

i2c_bus_handle_t i2c_bus_create(i2c_port_t port, const i2c_config_t *conf)

Create an I2C bus instance then return a handle if created successfully. Each I2C bus works in a singleton mode, which means for an i2c port only one group parameter works. When i2c_bus_create is called more than one time for the same i2c port, following parameter will override the previous one.

Return

i2c_bus_handle_t Return the I2C bus handle if created successfully, return NULL if failed.

Parameters
  • port: I2C port number

  • conf: Pointer to I2C bus configuration

esp_err_t i2c_bus_delete(i2c_bus_handle_t *p_bus_handle)

Delete and release the I2C bus resource.

Return

  • ESP_OK Success

  • ESP_FAIL Fail

Parameters
  • p_bus_handle: Point to the I2C bus handle, if delete succeed handle will set to NULL.

uint8_t i2c_bus_scan(i2c_bus_handle_t bus_handle, uint8_t *buf, uint8_t num)

Scan i2c devices attached on i2c bus.

Return

uint8_t Total number of devices found on the I2C bus

Parameters
  • bus_handle: I2C bus handle

  • buf: Pointer to a buffer to save devices’ address, if NULL no address will be saved.

  • num: Maximum number of addresses to save, invalid if buf set to NULL, higer addresses will be discarded if num less-than the total number found on the I2C bus.

uint32_t i2c_bus_get_current_clk_speed(i2c_bus_handle_t bus_handle)

Get current active clock speed.

Return

uint32_t current clock speed

Parameters
  • bus_handle: I2C bus handle

uint8_t i2c_bus_get_created_device_num(i2c_bus_handle_t bus_handle)

Get created device number of the bus.

Return

uint8_t created device number of the bus

Parameters
  • bus_handle: I2C bus handle

i2c_bus_device_handle_t i2c_bus_device_create(i2c_bus_handle_t bus_handle, uint8_t dev_addr, uint32_t clk_speed)

Create an I2C device on specific bus. Dynamic configuration must be enable to achieve multiple devices with different configs on a single bus. menuconfig:Bus Options->I2C Bus Options->enable dynamic configuration.

Return

i2c_bus_device_handle_t return a device handle if created successfully, return NULL if failed.

Parameters
  • bus_handle: Point to the I2C bus handle

  • dev_addr: i2c device address

  • clk_speed: device specified clock frequency the i2c_bus will switch to during each transfer. 0 if use current bus speed.

esp_err_t i2c_bus_device_delete(i2c_bus_device_handle_t *p_dev_handle)

Delete and release the I2C device resource, i2c_bus_device_delete should be used in pairs with i2c_bus_device_create.

Return

  • ESP_OK Success

  • ESP_FAIL Fail

Parameters
  • p_dev_handle: Point to the I2C device handle, if delete succeed handle will set to NULL.

uint8_t i2c_bus_device_get_address(i2c_bus_device_handle_t dev_handle)

Get device’s I2C address.

Return

uint8_t I2C address, return NULL_I2C_DEV_ADDR if dev_handle is invalid.

Parameters
  • dev_handle: I2C device handle

esp_err_t i2c_bus_read_byte(i2c_bus_device_handle_t dev_handle, uint8_t mem_address, uint8_t *data)

Read single byte from i2c device with 8-bit internal register/memory address.

Return

esp_err_t

  • ESP_OK Success

  • ESP_ERR_INVALID_ARG Parameter error

  • ESP_FAIL Sending command error, slave doesn’t ACK the transfer.

  • ESP_ERR_INVALID_STATE I2C driver not installed or not in master mode.

  • ESP_ERR_TIMEOUT Operation timeout because the bus is busy.

Parameters
  • dev_handle: I2C device handle

  • mem_address: The internal reg/mem address to read from, set to NULL_I2C_MEM_ADDR if no internal address.

  • data: Pointer to a buffer to save the data that was read

esp_err_t i2c_bus_read_bytes(i2c_bus_device_handle_t dev_handle, uint8_t mem_address, size_t data_len, uint8_t *data)

Read multiple bytes from i2c device with 8-bit internal register/memory address. If internal reg/mem address is 16-bit, please refer i2c_bus_read_reg16.

Return

esp_err_t

  • ESP_OK Success

  • ESP_ERR_INVALID_ARG Parameter error

  • ESP_FAIL Sending command error, slave doesn’t ACK the transfer.

  • ESP_ERR_INVALID_STATE I2C driver not installed or not in master mode.

  • ESP_ERR_TIMEOUT Operation timeout because the bus is busy.

Parameters
  • dev_handle: I2C device handle

  • mem_address: The internal reg/mem address to read from, set to NULL_I2C_MEM_ADDR if no internal address.

  • data_len: Number of bytes to read

  • data: Pointer to a buffer to save the data that was read

esp_err_t i2c_bus_read_bit(i2c_bus_device_handle_t dev_handle, uint8_t mem_address, uint8_t bit_num, uint8_t *data)

Read single bit of a byte from i2c device with 8-bit internal register/memory address.

Return

esp_err_t

  • ESP_OK Success

  • ESP_ERR_INVALID_ARG Parameter error

  • ESP_FAIL Sending command error, slave doesn’t ACK the transfer.

  • ESP_ERR_INVALID_STATE I2C driver not installed or not in master mode.

  • ESP_ERR_TIMEOUT Operation timeout because the bus is busy.

Parameters
  • dev_handle: I2C device handle

  • mem_address: The internal reg/mem address to read from, set to NULL_I2C_MEM_ADDR if no internal address.

  • bit_num: The bit number 0 - 7 to read

  • data: Pointer to a buffer to save the data that was read. *data == 0 -> bit = 0, *data !=0 -> bit = 1.

esp_err_t i2c_bus_read_bits(i2c_bus_device_handle_t dev_handle, uint8_t mem_address, uint8_t bit_start, uint8_t length, uint8_t *data)

Read multiple bits of a byte from i2c device with 8-bit internal register/memory address.

Return

esp_err_t

  • ESP_OK Success

  • ESP_ERR_INVALID_ARG Parameter error

  • ESP_FAIL Sending command error, slave doesn’t ACK the transfer.

  • ESP_ERR_INVALID_STATE I2C driver not installed or not in master mode.

  • ESP_ERR_TIMEOUT Operation timeout because the bus is busy.

Parameters
  • dev_handle: I2C device handle

  • mem_address: The internal reg/mem address to read from, set to NULL_I2C_MEM_ADDR if no internal address.

  • bit_start: The bit to start from, 0 - 7, MSB at 0

  • length: The number of bits to read, 1 - 8

  • data: Pointer to a buffer to save the data that was read

esp_err_t i2c_bus_write_byte(i2c_bus_device_handle_t dev_handle, uint8_t mem_address, uint8_t data)

Write single byte to i2c device with 8-bit internal register/memory address.

Return

esp_err_t

  • ESP_OK Success

  • ESP_ERR_INVALID_ARG Parameter error

  • ESP_FAIL Sending command error, slave doesn’t ACK the transfer.

  • ESP_ERR_INVALID_STATE I2C driver not installed or not in master mode.

  • ESP_ERR_TIMEOUT Operation timeout because the bus is busy.

Parameters
  • dev_handle: I2C device handle

  • mem_address: The internal reg/mem address to write to, set to NULL_I2C_MEM_ADDR if no internal address.

  • data: The byte to write.

esp_err_t i2c_bus_write_bytes(i2c_bus_device_handle_t dev_handle, uint8_t mem_address, size_t data_len, const uint8_t *data)

Write multiple byte to i2c device with 8-bit internal register/memory address If internal reg/mem address is 16-bit, please refer i2c_bus_write_reg16.

Return

esp_err_t

  • ESP_OK Success

  • ESP_ERR_INVALID_ARG Parameter error

  • ESP_FAIL Sending command error, slave doesn’t ACK the transfer.

  • ESP_ERR_INVALID_STATE I2C driver not installed or not in master mode.

  • ESP_ERR_TIMEOUT Operation timeout because the bus is busy.

Parameters
  • dev_handle: I2C device handle

  • mem_address: The internal reg/mem address to write to, set to NULL_I2C_MEM_ADDR if no internal address.

  • data_len: Number of bytes to write

  • data: Pointer to the bytes to write.

esp_err_t i2c_bus_write_bit(i2c_bus_device_handle_t dev_handle, uint8_t mem_address, uint8_t bit_num, uint8_t data)

Write single bit of a byte to an i2c device with 8-bit internal register/memory address.

Return

esp_err_t

  • ESP_OK Success

  • ESP_ERR_INVALID_ARG Parameter error

  • ESP_FAIL Sending command error, slave doesn’t ACK the transfer.

  • ESP_ERR_INVALID_STATE I2C driver not installed or not in master mode.

  • ESP_ERR_TIMEOUT Operation timeout because the bus is busy.

Parameters
  • dev_handle: I2C device handle

  • mem_address: The internal reg/mem address to write to, set to NULL_I2C_MEM_ADDR if no internal address.

  • bit_num: The bit number 0 - 7 to write

  • data: The bit to write, data == 0 means set bit = 0, data !=0 means set bit = 1.

esp_err_t i2c_bus_write_bits(i2c_bus_device_handle_t dev_handle, uint8_t mem_address, uint8_t bit_start, uint8_t length, uint8_t data)

Write multiple bits of a byte to an i2c device with 8-bit internal register/memory address.

Return

esp_err_t

  • ESP_OK Success

  • ESP_ERR_INVALID_ARG Parameter error

  • ESP_FAIL Sending command error, slave doesn’t ACK the transfer.

  • ESP_ERR_INVALID_STATE I2C driver not installed or not in master mode.

  • ESP_ERR_TIMEOUT Operation timeout because the bus is busy.

Parameters
  • dev_handle: I2C device handle

  • mem_address: The internal reg/mem address to write to, set to NULL_I2C_MEM_ADDR if no internal address.

  • bit_start: The bit to start from, 0 - 7, MSB at 0

  • length: The number of bits to write, 1 - 8

  • data: The bits to write.

esp_err_t i2c_bus_cmd_begin(i2c_bus_device_handle_t dev_handle, i2c_cmd_handle_t cmd)

I2C master send queued commands create by i2c_cmd_link_create . This function will trigger sending all queued commands. The task will be blocked until all the commands have been sent out. If I2C_BUS_DYNAMIC_CONFIG enable, i2c_bus will dynamically check configs and re-install i2c driver before each transfer, hence multiple devices with different configs on a single bus can be supported.

Note

Only call this function when i2c_bus_read/write_xx do not meet the requirements

Return

esp_err_t

  • ESP_OK Success

  • ESP_ERR_INVALID_ARG Parameter error

  • ESP_FAIL Sending command error, slave doesn’t ACK the transfer.

  • ESP_ERR_INVALID_STATE I2C driver not installed or not in master mode.

  • ESP_ERR_TIMEOUT Operation timeout because the bus is busy.

Parameters
  • dev_handle: I2C device handle

  • cmd: I2C command handler

esp_err_t i2c_bus_write_reg16(i2c_bus_device_handle_t dev_handle, uint16_t mem_address, size_t data_len, const uint8_t *data)

Write date to an i2c device with 16-bit internal reg/mem address.

Return

esp_err_t

  • ESP_OK Success

  • ESP_ERR_INVALID_ARG Parameter error

  • ESP_FAIL Sending command error, slave doesn’t ACK the transfer.

  • ESP_ERR_INVALID_STATE I2C driver not installed or not in master mode.

  • ESP_ERR_TIMEOUT Operation timeout because the bus is busy.

Parameters
  • dev_handle: I2C device handle

  • mem_address: The internal 16-bit reg/mem address to write to, set to NULL_I2C_MEM_ADDR if no internal address.

  • data_len: Number of bytes to write

  • data: Pointer to the bytes to write.

esp_err_t i2c_bus_read_reg16(i2c_bus_device_handle_t dev_handle, uint16_t mem_address, size_t data_len, uint8_t *data)

Read date from i2c device with 16-bit internal reg/mem address.

Return

esp_err_t

  • ESP_OK Success

  • ESP_ERR_INVALID_ARG Parameter error

  • ESP_FAIL Sending command error, slave doesn’t ACK the transfer.

  • ESP_ERR_INVALID_STATE I2C driver not installed or not in master mode.

  • ESP_ERR_TIMEOUT Operation timeout because the bus is busy.

Parameters
  • dev_handle: I2C device handle

  • mem_address: The internal 16-bit reg/mem address to read from, set to NULL_I2C_MEM_ADDR if no internal address.

  • data_len: Number of bytes to read

  • data: Pointer to a buffer to save the data that was read

Macros

NULL_I2C_MEM_ADDR

set mem_address to NULL_I2C_MEM_ADDR if i2c device has no internal address during read/write

NULL_I2C_DEV_ADDR

invalid i2c device address

gpio_pad_select_gpio
portTICK_RATE_MS

Type Definitions

typedef void *i2c_bus_handle_t

i2c bus handle

typedef void *i2c_bus_device_handle_t

i2c device handle

spi_bus API 参考

Header File

Functions

spi_bus_handle_t spi_bus_create(spi_host_device_t host_id, const spi_config_t *bus_conf)

Create and initialize a spi bus and return the spi bus handle.

Return

spi_bus_handle_t handle for spi bus operation, NULL if failed.

Parameters
  • host_id: SPI peripheral that controls this bus, SPI2_HOST or SPI3_HOST

  • bus_conf: spi bus configurations details in spi_config_t

esp_err_t spi_bus_delete(spi_bus_handle_t *p_bus_handle)

Deinitialize and delete the spi bus.

Return

esp_err_t

  • ESP_ERR_INVALID_ARG if parameter is invalid

  • ESP_FAIL Fail

  • ESP_OK Success

Parameters
  • p_bus_handle: pointer to spi bus handle, if delete succeed handle will set to NULL.

spi_bus_device_handle_t spi_bus_device_create(spi_bus_handle_t bus_handle, const spi_device_config_t *device_conf)

Create and add a device on the spi bus.

Return

spi_bus_device_handle_t handle for device operation, NULL if failed.

Parameters
  • bus_handle: handle for spi bus operation.

  • device_conf: spi device configurations details in spi_device_config_t

esp_err_t spi_bus_device_delete(spi_bus_device_handle_t *p_dev_handle)

Deinitialize and remove the device from spi bus.

Return

esp_err_t

  • ESP_ERR_INVALID_ARG if parameter is invalid

  • ESP_FAIL Fail

  • ESP_OK Success

Parameters
  • p_dev_handle: pointer to device handle, if delete succeed handle will set to NULL.

esp_err_t spi_bus_transfer_byte(spi_bus_device_handle_t dev_handle, uint8_t data_out, uint8_t *data_in)

Transfer one byte with the device.

Return

esp_err_t

  • ESP_ERR_INVALID_ARG if parameter is invalid

  • ESP_ERR_TIMEOUT if bus is busy

  • ESP_OK on success

Parameters
  • dev_handle: handle for device operation.

  • data_out: data will send to device.

  • data_in: pointer to receive buffer, set NULL to skip receive phase.

esp_err_t spi_bus_transfer_bytes(spi_bus_device_handle_t dev_handle, const uint8_t *data_out, uint8_t *data_in, uint32_t data_len)

Transfer multi-bytes with the device.

Return

esp_err_t

  • ESP_ERR_INVALID_ARG if parameter is invalid

  • ESP_ERR_TIMEOUT if bus is busy

  • ESP_OK on success

Parameters
  • dev_handle: handle for device operation.

  • data_out: pointer to sent buffer, set NULL to skip sent phase.

  • data_in: pointer to receive buffer, set NULL to skip receive phase.

  • data_len: number of bytes will transfer.

esp_err_t spi_bus_transmit_begin(spi_bus_device_handle_t dev_handle, spi_transaction_t *p_trans)

Send a polling transaction, wait for it to complete, and return the result.

Note

Only call this function when spi_bus_transfer_xx do not meet the requirements

Return

esp_err_t

  • ESP_ERR_INVALID_ARG if parameter is invalid

  • ESP_ERR_TIMEOUT if bus is busy

  • ESP_OK on success

Parameters
  • dev_handle: handle for device operation.

  • p_trans: Description of transaction to execute

esp_err_t spi_bus_transfer_reg16(spi_bus_device_handle_t dev_handle, uint16_t data_out, uint16_t *data_in)

Transfer one 16-bit value with the device. using msb by default. For example 0x1234, 0x12 will send first then 0x34.

Return

esp_err_t

  • ESP_ERR_INVALID_ARG if parameter is invalid

  • ESP_ERR_TIMEOUT if bus is busy

  • ESP_OK on success

Parameters
  • dev_handle: handle for device operation.

  • data_out: data will send to device.

  • data_in: pointer to receive buffer, set NULL to skip receive phase.

esp_err_t spi_bus_transfer_reg32(spi_bus_device_handle_t dev_handle, uint32_t data_out, uint32_t *data_in)

Transfer one 32-bit value with the device. using msb by default. For example 0x12345678, 0x12 will send first, 0x78 will send in the end.

Return

esp_err_t

  • ESP_ERR_INVALID_ARG if parameter is invalid

  • ESP_ERR_TIMEOUT if bus is busy

  • ESP_OK on success

Parameters
  • dev_handle: handle for device operation.

  • data_out: data will send to device.

  • data_in: pointer to receive buffer, set NULL to skip receive phase.

Structures

struct spi_config_t

spi bus initialization parameters.

Public Members

gpio_num_t miso_io_num

GPIO pin for Master In Slave Out (=spi_q) signal, or -1 if not used.

gpio_num_t mosi_io_num

GPIO pin for Master Out Slave In (=spi_d) signal, or -1 if not used.

gpio_num_t sclk_io_num

GPIO pin for Spi CLocK signal, or -1 if not used

int max_transfer_sz

<Maximum length of bytes available to send, if < 4096, 4096 will be set

struct spi_device_config_t

spi device initialization parameters.

Public Members

gpio_num_t cs_io_num

GPIO pin to select this device (CS), or -1 if not used

uint8_t mode

modes (0,1,2,3) that correspond to the four possible clocking configurations

int clock_speed_hz

spi clock speed, divisors of 80MHz, in Hz. See `SPI_MASTER_FREQ_*

Macros

NULL_SPI_CS_PIN

set cs_io_num to NULL_SPI_CS_PIN if spi device has no CP pin

Type Definitions

typedef void *spi_bus_handle_t

spi bus handle

typedef void *spi_bus_device_handle_t

spi device handle