|
40 | 40 | #if MICROPY_PY_MACHINE_I2C || MICROPY_PY_MACHINE_SOFTI2C
|
41 | 41 | #include "../../../../micropy_updates/common/mp_i2c_common.h"
|
42 | 42 |
|
43 |
| - #ifndef MICROPY_HW_I2C0_SCL |
44 |
| - #define MICROPY_HW_I2C0_SCL (GPIO_NUM_18) |
45 |
| - #define MICROPY_HW_I2C0_SDA (GPIO_NUM_19) |
46 |
| - #endif |
47 |
| - |
48 |
| - #ifndef MICROPY_HW_I2C1_SCL |
49 |
| - #if CONFIG_IDF_TARGET_ESP32 |
50 |
| - #define MICROPY_HW_I2C1_SCL (GPIO_NUM_25) |
51 |
| - #define MICROPY_HW_I2C1_SDA (GPIO_NUM_26) |
52 |
| - #else |
53 |
| - #define MICROPY_HW_I2C1_SCL (GPIO_NUM_9) |
54 |
| - #define MICROPY_HW_I2C1_SDA (GPIO_NUM_8) |
55 |
| - #endif |
56 |
| - #endif |
57 |
| - |
58 | 43 | #if CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32C6 || CONFIG_IDF_TARGET_ESP32S3
|
59 |
| - #define I2C_SCLK_FREQ XTAL_CLK_FREQ |
| 44 | + #define SCLK_I2C_FREQ XTAL_CLK_FREQ |
60 | 45 | #elif CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
|
61 |
| - #define I2C_SCLK_FREQ APB_CLK_FREQ |
| 46 | + #define SCLK_I2C_FREQ APB_CLK_FREQ |
62 | 47 | #else
|
63 | 48 | #error "unsupported I2C for ESP32 SoC variant"
|
64 | 49 | #endif
|
65 | 50 |
|
66 |
| - #define I2C_DEFAULT_TIMEOUT_US (50000) // 50ms |
| 51 | + #define DEFAULT_I2C_TIMEOUT_US (50000) // 50ms |
67 | 52 |
|
68 | 53 |
|
69 | 54 | static void device_deinit_internal(mp_machine_hw_i2c_device_obj_t *self);
|
|
136 | 121 |
|
137 | 122 | i2c_param_config(self->port, &conf);
|
138 | 123 |
|
139 |
| - int timeout = I2C_SCLK_FREQ / 1000000 * timeout_us; |
| 124 | + int timeout = SCLK_I2C_FREQ / 1000000 * timeout_us; |
140 | 125 |
|
141 | 126 | i2c_set_timeout(self->port, (timeout > I2C_LL_MAX_TIMEOUT) ? I2C_LL_MAX_TIMEOUT : timeout);
|
142 | 127 | i2c_driver_install(self->port, I2C_MODE_MASTER, 0, 0, 0);
|
|
210 | 195 | int h, l;
|
211 | 196 | i2c_get_period(self->port, &h, &l);
|
212 | 197 | mp_printf(print, "I2C(%u, scl=%u, sda=%u, freq=%u)",
|
213 |
| - self->port, self->scl, self->sda, I2C_SCLK_FREQ / (h + l)); |
| 198 | + self->port, self->scl, self->sda, SCLK_I2C_FREQ / (h + l)); |
214 | 199 | }
|
215 | 200 |
|
216 | 201 |
|
|
226 | 211 | { MP_QSTR_freq, MP_ARG_INT, { .u_int = 400000} },
|
227 | 212 | { MP_QSTR_use_locks, MP_ARG_BOOL, { .u_bool = false } },
|
228 | 213 | { MP_QSTR_pullup, MP_ARG_BOOL, { .u_bool = false } },
|
229 |
| - { MP_QSTR_timeout, MP_ARG_INT, { .u_int = I2C_DEFAULT_TIMEOUT_US } }, |
| 214 | + { MP_QSTR_timeout, MP_ARG_INT, { .u_int = DEFAULT_I2C_TIMEOUT_US } }, |
230 | 215 | };
|
231 | 216 | mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
232 | 217 | mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
|
423 | 408 | mp_buffer_info_t read_bufinfo;
|
424 | 409 | mp_get_buffer_raise(args[ARG_write_buf].u_obj, &read_bufinfo, MP_BUFFER_WRITE);
|
425 | 410 |
|
426 |
| - uint32_t memaddr = 0; |
427 |
| - |
428 |
| - for (int i=(int)(self->reg_bits / 8) - 1;i>-1;i--) { |
429 |
| - memaddr |= (uint32_t)(((uint8_t *)write_bufinfo.buf)[i]) << (uint8_t)((~i + (int)(self->reg_bits / 8)) * 8); |
| 411 | + int ret = device_writeto(self, self->device_id, (uint8_t *)write_bufinfo.buf, write_bufinfo.len, false); |
| 412 | + if (ret < 0) { |
| 413 | + mp_raise_OSError(-ret); |
430 | 414 | }
|
431 | 415 |
|
432 |
| - int ret = device_read(self, self->device_id, memaddr, self->reg_bits, (uint8_t *)read_bufinfo.buf, read_bufinfo.len); |
| 416 | + ret = device_readfrom(self, self->device_id, (uint8_t *)read_bufinfo.buf, read_bufinfo.len, true); |
433 | 417 | if (ret < 0) {
|
434 | 418 | mp_raise_OSError(-ret);
|
435 | 419 | }
|
436 |
| - |
| 420 | + |
437 | 421 | return mp_const_none;
|
438 | 422 | }
|
439 | 423 |
|
|
656 | 640 |
|
657 | 641 | static MP_DEFINE_CONST_DICT(i2c_locals_dict, i2c_locals_dict_table);
|
658 | 642 |
|
659 |
| - |
| 643 | + |
660 | 644 | MP_DEFINE_CONST_OBJ_TYPE(
|
661 | 645 | machine_i2c_type,
|
662 | 646 | MP_QSTR_I2C,
|
|
0 commit comments