Skip to content

Commit 5d02e71

Browse files
committed
Merge branch 'docs/i2c_ng_docs' into 'master'
docs(I2C): Add new programming guide for new I2C driver See merge request espressif/esp-idf!26752
2 parents c5e52f3 + ef46828 commit 5d02e71

File tree

19 files changed

+540
-714
lines changed

19 files changed

+540
-714
lines changed

components/driver/i2c/i2c_master.c

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -933,13 +933,13 @@ esp_err_t i2c_del_master_bus(i2c_master_bus_handle_t bus_handle)
933933
return ESP_OK;
934934
}
935935

936-
esp_err_t i2c_master_bus_reset(i2c_master_bus_handle_t handle)
936+
esp_err_t i2c_master_bus_reset(i2c_master_bus_handle_t bus_handle)
937937
{
938-
ESP_RETURN_ON_FALSE((handle != NULL), ESP_ERR_INVALID_ARG, TAG, "This bus is not initialized");
938+
ESP_RETURN_ON_FALSE((bus_handle != NULL), ESP_ERR_INVALID_ARG, TAG, "This bus is not initialized");
939939
// Reset I2C master bus
940-
ESP_RETURN_ON_ERROR(s_i2c_hw_fsm_reset(handle), TAG, "I2C master bus reset failed");
940+
ESP_RETURN_ON_ERROR(s_i2c_hw_fsm_reset(bus_handle), TAG, "I2C master bus reset failed");
941941
// Reset I2C status state
942-
atomic_store(&handle->status, I2C_STATUS_IDLE);
942+
atomic_store(&bus_handle->status, I2C_STATUS_IDLE);
943943
return ESP_OK;
944944
}
945945

@@ -1022,24 +1022,24 @@ esp_err_t i2c_master_receive(i2c_master_dev_handle_t i2c_dev, uint8_t *read_buff
10221022
return ESP_OK;
10231023
}
10241024

1025-
esp_err_t i2c_master_probe(i2c_master_bus_handle_t i2c_master, uint16_t address, int xfer_timeout_ms)
1025+
esp_err_t i2c_master_probe(i2c_master_bus_handle_t bus_handle, uint16_t address, int xfer_timeout_ms)
10261026
{
1027-
ESP_RETURN_ON_FALSE(i2c_master != NULL, ESP_ERR_INVALID_ARG, TAG, "i2c handle not initialized");
1027+
ESP_RETURN_ON_FALSE(bus_handle != NULL, ESP_ERR_INVALID_ARG, TAG, "i2c handle not initialized");
10281028
TickType_t ticks_to_wait = (xfer_timeout_ms == -1) ? portMAX_DELAY : pdMS_TO_TICKS(xfer_timeout_ms);
1029-
if (xSemaphoreTake(i2c_master->bus_lock_mux, ticks_to_wait) != pdTRUE) {
1029+
if (xSemaphoreTake(bus_handle->bus_lock_mux, ticks_to_wait) != pdTRUE) {
10301030
return ESP_ERR_TIMEOUT;
10311031
}
10321032

1033-
i2c_master->cmd_idx = 0;
1034-
i2c_master->trans_idx = 0;
1035-
i2c_master->trans_done = false;
1036-
i2c_hal_context_t *hal = &i2c_master->base->hal;
1033+
bus_handle->cmd_idx = 0;
1034+
bus_handle->trans_idx = 0;
1035+
bus_handle->trans_done = false;
1036+
i2c_hal_context_t *hal = &bus_handle->base->hal;
10371037
i2c_operation_t i2c_ops[] = {
10381038
{.hw_cmd = I2C_TRANS_START_COMMAND},
10391039
{.hw_cmd = I2C_TRANS_STOP_COMMAND},
10401040
};
10411041

1042-
i2c_master->i2c_trans = (i2c_transaction_t) {
1042+
bus_handle->i2c_trans = (i2c_transaction_t) {
10431043
.device_address = address,
10441044
.ops = i2c_ops,
10451045
.cmd_count = DIM(i2c_ops),
@@ -1048,20 +1048,20 @@ esp_err_t i2c_master_probe(i2c_master_bus_handle_t i2c_master, uint16_t address,
10481048
// I2C probe does not have i2c device module. So set the clock parameter independently
10491049
// This will not influence device transaction.
10501050
I2C_CLOCK_SRC_ATOMIC() {
1051-
i2c_ll_set_source_clk(hal->dev, i2c_master->base->clk_src);
1052-
i2c_hal_set_bus_timing(hal, 100000, i2c_master->base->clk_src, i2c_master->base->clk_src_freq_hz);
1051+
i2c_ll_set_source_clk(hal->dev, bus_handle->base->clk_src);
1052+
i2c_hal_set_bus_timing(hal, 100000, bus_handle->base->clk_src, bus_handle->base->clk_src_freq_hz);
10531053
}
10541054
i2c_ll_master_set_fractional_divider(hal->dev, 0, 0);
10551055
i2c_ll_update(hal->dev);
10561056

1057-
s_i2c_send_commands(i2c_master, ticks_to_wait);
1058-
if (i2c_master->status == I2C_STATUS_ACK_ERROR) {
1057+
s_i2c_send_commands(bus_handle, ticks_to_wait);
1058+
if (bus_handle->status == I2C_STATUS_ACK_ERROR) {
10591059
// Reset the status to done, in order not influence next time transaction.
1060-
i2c_master->status = I2C_STATUS_DONE;
1061-
xSemaphoreGive(i2c_master->bus_lock_mux);
1060+
bus_handle->status = I2C_STATUS_DONE;
1061+
xSemaphoreGive(bus_handle->bus_lock_mux);
10621062
return ESP_ERR_NOT_FOUND;
10631063
}
1064-
xSemaphoreGive(i2c_master->bus_lock_mux);
1064+
xSemaphoreGive(bus_handle->bus_lock_mux);
10651065
return ESP_OK;
10661066
}
10671067

@@ -1088,19 +1088,19 @@ esp_err_t i2c_master_register_event_callbacks(i2c_master_dev_handle_t i2c_dev, c
10881088
return ESP_OK;
10891089
}
10901090

1091-
esp_err_t i2c_master_wait_all_done(i2c_master_bus_handle_t i2c_master, int timeout_ms)
1091+
esp_err_t i2c_master_bus_wait_all_done(i2c_master_bus_handle_t bus_handle, int timeout_ms)
10921092
{
1093-
ESP_RETURN_ON_FALSE(i2c_master, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
1093+
ESP_RETURN_ON_FALSE(bus_handle, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
10941094
TickType_t wait_ticks = timeout_ms < 0 ? portMAX_DELAY : pdMS_TO_TICKS(timeout_ms);
10951095
i2c_transaction_t t;
10961096

1097-
size_t cnt = i2c_master->num_trans_inflight;
1097+
size_t cnt = bus_handle->num_trans_inflight;
10981098
for (size_t i = 0; i < cnt; i++) {
1099-
ESP_RETURN_ON_FALSE(xQueueReceive(i2c_master->trans_queues[I2C_TRANS_QUEUE_COMPLETE], &t, wait_ticks) == pdTRUE,
1099+
ESP_RETURN_ON_FALSE(xQueueReceive(bus_handle->trans_queues[I2C_TRANS_QUEUE_COMPLETE], &t, wait_ticks) == pdTRUE,
11001100
ESP_ERR_TIMEOUT, TAG, "flush timeout");
1101-
ESP_RETURN_ON_FALSE(xQueueSend(i2c_master->trans_queues[I2C_TRANS_QUEUE_READY], &t, 0) == pdTRUE,
1101+
ESP_RETURN_ON_FALSE(xQueueSend(bus_handle->trans_queues[I2C_TRANS_QUEUE_READY], &t, 0) == pdTRUE,
11021102
ESP_ERR_INVALID_STATE, TAG, "ready queue full");
1103-
i2c_master->num_trans_inflight--;
1103+
bus_handle->num_trans_inflight--;
11041104
}
11051105
return ESP_OK;
11061106

components/driver/i2c/include/driver/i2c_master.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ typedef struct {
2828
size_t trans_queue_depth; /*!< Depth of internal transfer queue, increase this value can support more transfers pending in the background, only valid in asynchronous transaction. (Typically max_device_num * per_transaction)*/
2929
struct {
3030
uint32_t enable_internal_pullup:1; /*!< Enable internal pullups. Note: This is not strong enough to pullup buses under high-speed frequency. Recommend proper external pull-up if possible */
31-
} flags;
31+
} flags; /*!< I2C master config flags */
3232
} i2c_master_bus_config_t;
3333

3434
/**
@@ -157,13 +157,15 @@ esp_err_t i2c_master_receive(i2c_master_dev_handle_t i2c_dev, uint8_t *read_buff
157157
/**
158158
* @brief Probe I2C address, if address is correct and ACK is received, this function will return ESP_OK.
159159
*
160-
* @param[in] i2c_dev I2C master device handle that created by `i2c_master_bus_add_device`.
160+
* @param[in] bus_handle I2C master device handle that created by `i2c_master_bus_add_device`.
161+
* @param[in] address I2C device address that you want to probe.
161162
* @param[in] xfer_timeout_ms Wait timeout, in ms. Note: -1 means wait forever (Not recommended in this function).
162163
* @return
163164
* - ESP_OK: I2C device probe successfully
165+
* - ESP_ERR_NOT_FOUND: I2C probe failed, doesn't find the device with specific address you gave.
164166
* - ESP_ERR_TIMEOUT: Operation timeout(larger than xfer_timeout_ms) because the bus is busy or hardware crash.
165167
*/
166-
esp_err_t i2c_master_probe(i2c_master_bus_handle_t i2c_master, uint16_t address, int xfer_timeout_ms);
168+
esp_err_t i2c_master_probe(i2c_master_bus_handle_t bus_handle, uint16_t address, int xfer_timeout_ms);
167169

168170
/**
169171
* @brief Register I2C transaction callbacks for a master device
@@ -186,26 +188,26 @@ esp_err_t i2c_master_register_event_callbacks(i2c_master_dev_handle_t i2c_dev, c
186188
/**
187189
* @brief Reset the I2C master bus.
188190
*
189-
* @param handle I2C bus handle.
191+
* @param bus_handle I2C bus handle.
190192
* @return
191193
* - ESP_OK: Reset succeed.
192194
* - ESP_ERR_INVALID_ARG: I2C master bus handle is not initialized.
193195
* - Otherwise: Reset failed.
194196
*/
195-
esp_err_t i2c_master_bus_reset(i2c_master_bus_handle_t handle);
197+
esp_err_t i2c_master_bus_reset(i2c_master_bus_handle_t bus_handle);
196198

197199
/**
198200
* @brief Wait for all pending I2C transactions done
199201
*
200-
* @param[in] i2c_master I2C bus handle
202+
* @param[in] bus_handle I2C bus handle
201203
* @param[in] timeout_ms Wait timeout, in ms. Specially, -1 means to wait forever.
202204
* @return
203205
* - ESP_OK: Flush transactions successfully
204206
* - ESP_ERR_INVALID_ARG: Flush transactions failed because of invalid argument
205207
* - ESP_ERR_TIMEOUT: Flush transactions failed because of timeout
206208
* - ESP_FAIL: Flush transactions failed because of other error
207209
*/
208-
esp_err_t i2c_master_wait_all_done(i2c_master_bus_handle_t i2c_master, int timeout_ms);
210+
esp_err_t i2c_master_bus_wait_all_done(i2c_master_bus_handle_t bus_handle, int timeout_ms);
209211

210212
#ifdef __cplusplus
211213
}

components/driver/i2c/include/driver/i2c_slave.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ typedef struct {
4040
#if SOC_I2C_SLAVE_SUPPORT_SLAVE_UNMATCH
4141
uint32_t slave_unmatch_en:1; /*!< Can trigger unmatch interrupt when slave address does not match what master sends*/
4242
#endif
43-
} flags;
43+
} flags; /*!< I2C slave config flags */
4444
} i2c_slave_config_t;
4545

4646
/**

components/driver/i2c/include/driver/i2c_types.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ typedef bool (*i2c_master_callback_t)(i2c_master_dev_handle_t i2c_dev, const i2c
7878
* @brief Event structure used in I2C slave
7979
*/
8080
typedef struct {
81-
uint8_t *buffer;
81+
uint8_t *buffer; /**< Pointer for buffer received in callback. */
8282
} i2c_slave_rx_done_event_data_t;
8383

8484
/**
@@ -98,7 +98,7 @@ typedef bool (*i2c_slave_received_callback_t)(i2c_slave_dev_handle_t i2c_slave,
9898
* @brief Stretch cause event structure used in I2C slave
9999
*/
100100
typedef struct {
101-
i2c_slave_stretch_cause_t stretch_cause;
101+
i2c_slave_stretch_cause_t stretch_cause; /*!< Stretch cause can be got in callback */
102102
} i2c_slave_stretch_event_data_t;
103103

104104
/**
50.7 KB
Loading
30.2 KB
Loading
10.1 KB
Loading
Loading
Loading
Loading
Loading
Loading
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"head": {
3+
"text": "Standard I2C Transaction Timing Diagram"
4+
},
5+
"signal": [
6+
{
7+
"node":"A.....B...................C..D.E...............F...G",
8+
"period": 0.5},
9+
{
10+
"name": "SDA",
11+
"wave": "1.0...3...3.|.3...4...5...1..0.6...6.|.6...7...10..1", "data": "A6 . A0 R/W ACK D7 . D0 ACK",
12+
"period": 0.5},
13+
{
14+
"name": "SCL",
15+
"wave": "1...0..1.0.1|0.1.0.1.0.1.0......1.0.1|0.1.0.1.0..1..",
16+
"period": 0.5}
17+
],
18+
"config":
19+
{
20+
"skin": "narrow"
21+
},
22+
"edge": [
23+
"B<->C Write address",
24+
"E<->F Write data"
25+
]
26+
}

docs/doxygen/Doxyfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ INPUT = \
7979
$(PROJECT_PATH)/components/driver/dac/include/driver/dac_cosine.h \
8080
$(PROJECT_PATH)/components/driver/dac/include/driver/dac_oneshot.h \
8181
$(PROJECT_PATH)/components/driver/dac/include/driver/dac_types.h \
82-
$(PROJECT_PATH)/components/driver/i2c/include/driver/i2c.h \
82+
$(PROJECT_PATH)/components/driver/i2c/include/driver/i2c_master.h \
83+
$(PROJECT_PATH)/components/driver/i2c/include/driver/i2c_slave.h \
84+
$(PROJECT_PATH)/components/driver/i2c/include/driver/i2c_types.h \
8385
$(PROJECT_PATH)/components/driver/i2s/include/driver/i2s_common.h \
8486
$(PROJECT_PATH)/components/driver/i2s/include/driver/i2s_pdm.h \
8587
$(PROJECT_PATH)/components/driver/i2s/include/driver/i2s_std.h \

0 commit comments

Comments
 (0)