3030#include "extmod/modmachine.h"
3131#include "machine_i2c.h"
3232
33- #include "driver/i2c .h"
33+ #include "driver/i2c_master .h"
3434#include "hal/i2c_ll.h"
3535
3636#if MICROPY_PY_MACHINE_I2C || MICROPY_PY_MACHINE_SOFTI2C
4949
5050#define I2C_DEFAULT_TIMEOUT_US (50000) // 50ms
5151
52+ // ---------------- Internal data structures ----------------
5253typedef struct _machine_hw_i2c_obj_t {
5354 mp_obj_base_t base ;
54- i2c_port_t port : 8 ;
55+ i2c_master_bus_handle_t bus_handle ;
56+ i2c_master_dev_handle_t dev_handle ;
57+ uint8_t port : 8 ;
5558 gpio_num_t scl : 8 ;
5659 gpio_num_t sda : 8 ;
5760} machine_hw_i2c_obj_t ;
5861
5962static machine_hw_i2c_obj_t machine_hw_i2c_obj [I2C_NUM_MAX ];
6063
64+ // ---------------- Initialization ----------------
6165static void machine_hw_i2c_init (machine_hw_i2c_obj_t * self , uint32_t freq , uint32_t timeout_us , bool first_init ) {
62- if (!first_init ) {
63- i2c_driver_delete (self -> port );
66+
67+ // 1. If already initialized, uninstall the old driver first
68+ if (!first_init && self -> bus_handle ) {
69+ i2c_master_bus_rm_device (self -> dev_handle );
70+ i2c_del_master_bus (self -> bus_handle );
71+ self -> bus_handle = NULL ;
72+ self -> dev_handle = NULL ;
6473 }
65- i2c_config_t conf = {
66- . mode = I2C_MODE_MASTER ,
67- . sda_io_num = self -> sda ,
68- .sda_pullup_en = GPIO_PULLUP_ENABLE ,
74+
75+ // 2. Configure the bus
76+ i2c_master_bus_config_t bus_cfg = {
77+ .i2c_port = self -> port ,
6978 .scl_io_num = self -> scl ,
70- .scl_pullup_en = GPIO_PULLUP_ENABLE ,
71- .master .clk_speed = freq ,
79+ .sda_io_num = self -> sda ,
80+ .clk_source = I2C_CLK_SRC_DEFAULT ,
81+ .glitch_ignore_cnt = 7 ,
82+ .flags .enable_internal_pullup = true,
7283 };
73- i2c_param_config (self -> port , & conf );
74- int timeout = I2C_SCLK_FREQ / 1000000 * timeout_us ;
75- i2c_set_timeout (self -> port , (timeout > I2C_LL_MAX_TIMEOUT ) ? I2C_LL_MAX_TIMEOUT : timeout );
76- i2c_driver_install (self -> port , I2C_MODE_MASTER , 0 , 0 , 0 );
84+ ESP_ERROR_CHECK (i2c_new_master_bus (& bus_cfg , & self -> bus_handle ));
85+
86+ // 3. Add a device (placeholder address; will be changed dynamically later)
87+ i2c_device_config_t dev_cfg = {
88+ .dev_addr_length = I2C_ADDR_BIT_LEN_7 ,
89+ .device_address = 0x00 , // Placeholder
90+ .scl_speed_hz = freq ,
91+ };
92+ ESP_ERROR_CHECK (i2c_master_bus_add_device (self -> bus_handle , & dev_cfg , & self -> dev_handle ));
7793}
7894
7995int machine_hw_i2c_transfer (mp_obj_base_t * self_in , uint16_t addr , size_t n , mp_machine_i2c_buf_t * bufs , unsigned int flags ) {
8096 machine_hw_i2c_obj_t * self = MP_OBJ_TO_PTR (self_in );
8197
82- i2c_cmd_handle_t cmd = i2c_cmd_link_create ();
98+ /* 0. Probe the address to see if any device responds */
99+ esp_err_t err = i2c_master_probe (self -> bus_handle , addr , 1000 );
100+ if (err != ESP_OK ) {
101+ return - MP_ENODEV ; /* No device at address, return immediately */
102+ }
103+ /* 1. Create a temporary device handle for this transaction */
104+ i2c_device_config_t dev_cfg = {
105+ .dev_addr_length = I2C_ADDR_BIT_LEN_7 ,
106+ .device_address = addr ,
107+ .scl_speed_hz = 100000 , /* Use bus frequency */
108+ };
109+ i2c_master_dev_handle_t dev_handle ;
110+ err = i2c_master_bus_add_device (self -> bus_handle , & dev_cfg , & dev_handle );
111+ if (err != ESP_OK ) {
112+ return - MP_ENODEV ;
113+ }
114+
83115 int data_len = 0 ;
84116
117+ /* 2. If WRITE1 segment exists, perform the write first */
85118 if (flags & MP_MACHINE_I2C_FLAG_WRITE1 ) {
86- i2c_master_start (cmd );
87- i2c_master_write_byte (cmd , addr << 1 , true);
88- i2c_master_write (cmd , bufs -> buf , bufs -> len , true);
119+ if (bufs -> len ) {
120+ err = i2c_master_transmit (dev_handle , bufs -> buf , bufs -> len , 1000 ); /* Block with 1 s timeout */
121+ if (err != ESP_OK ) {
122+ goto cleanup ;
123+ }
124+ }
89125 data_len += bufs -> len ;
90126 -- n ;
91127 ++ bufs ;
92128 }
129+ if (flags & MP_MACHINE_I2C_FLAG_READ ) {
130+ /* 3. Main loop: remaining segments */
131+ for (; n -- ; ++ bufs ) {
132+ if (bufs -> len == 0 ) {
133+ continue ;
134+ }
135+ err = i2c_master_receive (dev_handle , bufs -> buf , bufs -> len , 1000 );
136+ if (err != ESP_OK ) {
137+ break ;
138+ }
139+
140+ data_len += bufs -> len ;
141+ }
142+ } else {
143+ // Write operation logic
144+ size_t total_len = 0 ;
145+ mp_machine_i2c_buf_t * original_bufs = bufs ; // Save original pointer
146+ size_t yuann = n ;
93147
94- i2c_master_start (cmd );
95- i2c_master_write_byte (cmd , addr << 1 | (flags & MP_MACHINE_I2C_FLAG_READ ), true);
148+ // Calculate total length
149+ for (; n -- ; ++ bufs ) {
150+ total_len += bufs -> len ;
151+ }
96152
97- for (; n -- ; ++ bufs ) {
98- if (flags & MP_MACHINE_I2C_FLAG_READ ) {
99- i2c_master_read (cmd , bufs -> buf , bufs -> len , n == 0 ? I2C_MASTER_LAST_NACK : I2C_MASTER_ACK );
100- } else {
101- if (bufs -> len != 0 ) {
102- i2c_master_write (cmd , bufs -> buf , bufs -> len , true);
103- }
153+ // Reset pointer
154+ bufs = original_bufs ;
155+ // Reset n
156+ n = yuann ;
157+ // Dynamically allocate write_buf
158+ uint8_t * write_buf = (uint8_t * )malloc (total_len );
159+ if (write_buf == NULL ) {
160+ return - MP_ENOMEM ;
161+ }
162+
163+ // Copy data into write_buf
164+ size_t index = 0 ;
165+ for (; n -- ; ++ bufs ) {
166+ memcpy (write_buf + index , bufs -> buf , bufs -> len );
167+ index += bufs -> len ;
168+ }
169+
170+ // Transmit data
171+ err = i2c_master_transmit (dev_handle , write_buf , total_len , 1000 );
172+ if (err != ESP_OK ) {
173+ goto cleanup ;
104174 }
105- data_len += bufs -> len ;
106- }
107175
108- if ( flags & MP_MACHINE_I2C_FLAG_STOP ) {
109- i2c_master_stop ( cmd );
176+ // Free dynamically allocated memory
177+ free ( write_buf );
110178 }
111179
112- // TODO proper timeout
113- esp_err_t err = i2c_master_cmd_begin ( self -> port , cmd , 100 * ( 1 + data_len ) / portTICK_PERIOD_MS );
114- i2c_cmd_link_delete ( cmd );
180+ cleanup :
181+ /* 4. Immediately destroy the temporary handle */
182+ i2c_master_bus_rm_device ( dev_handle );
115183
184+ /* 5. Map errors */
116185 if (err == ESP_FAIL ) {
117186 return - MP_ENODEV ;
118- } else if (err == ESP_ERR_TIMEOUT ) {
187+ }
188+ if (err == ESP_ERR_TIMEOUT ) {
119189 return - MP_ETIMEDOUT ;
120- } else if (err != ESP_OK ) {
190+ }
191+ if (err != ESP_OK ) {
121192 return - abs (err );
122193 }
123194
124195 return data_len ;
125196}
126197
127- /******************************************************************************/
128- // MicroPython bindings for machine API
129-
198+ // ---------------- Print ----------------
130199static void machine_hw_i2c_print (const mp_print_t * print , mp_obj_t self_in , mp_print_kind_t kind ) {
131200 machine_hw_i2c_obj_t * self = MP_OBJ_TO_PTR (self_in );
132- int h , l ;
133- i2c_get_period (self -> port , & h , & l );
134- mp_printf (print , "I2C(%u, scl=%u, sda=%u, freq=%u)" ,
135- self -> port , self -> scl , self -> sda , I2C_SCLK_FREQ / (h + l ));
201+ mp_printf (print , "I2C(%u, scl=%u, sda=%u)" , self -> port , self -> scl , self -> sda );
136202}
137203
204+ // ---------------- Constructor ----------------
138205mp_obj_t machine_hw_i2c_make_new (const mp_obj_type_t * type , size_t n_args , size_t n_kw , const mp_obj_t * all_args ) {
139206 // Create a SoftI2C instance if no id is specified (or is -1) but other arguments are given
140207 if (n_args != 0 ) {
@@ -153,46 +220,33 @@ mp_obj_t machine_hw_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_
153220 mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
154221 mp_arg_parse_all_kw_array (n_args , n_kw , all_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
155222
156- // Get I2C bus
157223 mp_int_t i2c_id = args [ARG_id ].u_int ;
158-
159- // Check if the I2C bus is valid
160224 if (!(I2C_NUM_0 <= i2c_id && i2c_id < I2C_NUM_MAX )) {
161225 mp_raise_msg_varg (& mp_type_ValueError , MP_ERROR_TEXT ("I2C(%d) doesn't exist" ), i2c_id );
162226 }
163227
164- // Get static peripheral object
165- machine_hw_i2c_obj_t * self = (machine_hw_i2c_obj_t * )& machine_hw_i2c_obj [i2c_id ];
228+ machine_hw_i2c_obj_t * self = & machine_hw_i2c_obj [i2c_id ];
166229
167- bool first_init = false;
168- if (self -> base .type == NULL ) {
169- // Created for the first time, set default pins
230+ bool first_init = (self -> base .type == NULL );
231+ if (first_init ) {
170232 self -> base .type = & machine_i2c_type ;
171233 self -> port = i2c_id ;
172- if (self -> port == I2C_NUM_0 ) {
173- self -> scl = MICROPY_HW_I2C0_SCL ;
174- self -> sda = MICROPY_HW_I2C0_SDA ;
175- } else {
176- self -> scl = MICROPY_HW_I2C1_SCL ;
177- self -> sda = MICROPY_HW_I2C1_SDA ;
178- }
179- first_init = true;
234+ self -> scl = (i2c_id == I2C_NUM_0 ) ? MICROPY_HW_I2C0_SCL : MICROPY_HW_I2C1_SCL ;
235+ self -> sda = (i2c_id == I2C_NUM_0 ) ? MICROPY_HW_I2C0_SDA : MICROPY_HW_I2C1_SDA ;
180236 }
181237
182- // Set SCL/SDA pins if given
183238 if (args [ARG_scl ].u_obj != MP_OBJ_NULL ) {
184239 self -> scl = machine_pin_get_id (args [ARG_scl ].u_obj );
185240 }
186241 if (args [ARG_sda ].u_obj != MP_OBJ_NULL ) {
187242 self -> sda = machine_pin_get_id (args [ARG_sda ].u_obj );
188243 }
189244
190- // Initialise the I2C peripheral
191245 machine_hw_i2c_init (self , args [ARG_freq ].u_int , args [ARG_timeout ].u_int , first_init );
192-
193246 return MP_OBJ_FROM_PTR (self );
194247}
195248
249+ // ---------------- Protocol table ----------------
196250static const mp_machine_i2c_p_t machine_hw_i2c_p = {
197251 .transfer_supports_write1 = true,
198252 .transfer = machine_hw_i2c_transfer ,
@@ -208,4 +262,4 @@ MP_DEFINE_CONST_OBJ_TYPE(
208262 locals_dict , & mp_machine_i2c_locals_dict
209263 );
210264
211- #endif
265+ #endif // MICROPY_PY_MACHINE_I2C || MICROPY_PY_MACHINE_SOFTI2C
0 commit comments