11/*
2- * This file is part of the MicroPython project, http://micropython.org/
2+ * This file is part of the MicroPython project, http://micropython.org/
33 *
44 * The MIT License (MIT)
55 *
6868
6969#define I2C_DEFAULT_TIMEOUT_US (50000) // 50ms
7070
71- // ---------------- 内部数据结构 ----------------
71+ // ---------------- Internal Data Structures ----------------
7272typedef struct _machine_hw_i2c_obj_t {
7373 mp_obj_base_t base ;
7474 i2c_master_bus_handle_t bus_handle ;
@@ -80,21 +80,21 @@ typedef struct _machine_hw_i2c_obj_t {
8080
8181static machine_hw_i2c_obj_t machine_hw_i2c_obj [I2C_NUM_MAX ];
8282
83- // ---------------- 初始化 ----------------
83+ // ---------------- Initialization ----------------
8484static void machine_hw_i2c_init (machine_hw_i2c_obj_t * self ,
8585 uint32_t freq ,
8686 uint32_t timeout_us ,
8787 bool first_init ) {
8888
89- // 1. 若已初始化,先卸载旧驱动
89+ // 1. If already initialized, first remove the old driver
9090 if (!first_init && self -> bus_handle ) {
9191 i2c_master_bus_rm_device (self -> dev_handle );
9292 i2c_del_master_bus (self -> bus_handle );
9393 self -> bus_handle = NULL ;
9494 self -> dev_handle = NULL ;
9595 }
9696
97- // 2. 配置总线
97+ // 2. Configure the bus
9898 i2c_master_bus_config_t bus_cfg = {
9999 .i2c_port = self -> port ,
100100 .scl_io_num = self -> scl ,
@@ -105,10 +105,10 @@ static void machine_hw_i2c_init(machine_hw_i2c_obj_t *self,
105105 };
106106 ESP_ERROR_CHECK (i2c_new_master_bus (& bus_cfg , & self -> bus_handle ));
107107
108- // 3. 添加设备(占位地址,后面真正传输时再动态改)
108+ // 3. Add device (placeholder address, dynamically changed later during actual transmission)
109109 i2c_device_config_t dev_cfg = {
110110 .dev_addr_length = I2C_ADDR_BIT_LEN_7 ,
111- .device_address = 0x00 , // 占位
111+ .device_address = 0x00 , // Placeholder
112112 .scl_speed_hz = freq ,
113113 };
114114 ESP_ERROR_CHECK (i2c_master_bus_add_device (self -> bus_handle , & dev_cfg , & self -> dev_handle ));
@@ -122,16 +122,16 @@ int machine_hw_i2c_transfer(mp_obj_base_t *self_in,
122122{
123123 machine_hw_i2c_obj_t * self = MP_OBJ_TO_PTR (self_in );
124124
125- /* 0. 先探测地址是否有设备回应 */
125+ /* 0. First, probe the address to see if a device responds */
126126 esp_err_t err = i2c_master_probe (self -> bus_handle , addr , 1000 );
127127 if (err != ESP_OK ) {
128- return - MP_ENODEV ; /* 地址无设备,直接返回 */
128+ return - MP_ENODEV ; /* No device at address, return directly */
129129 }
130- /* 1. 为本次事务创建临时 device 句柄 */
130+ /* 1. Create a temporary device handle for this transaction */
131131 i2c_device_config_t dev_cfg = {
132132 .dev_addr_length = I2C_ADDR_BIT_LEN_7 ,
133133 .device_address = addr ,
134- .scl_speed_hz = 100000 , /* 沿用总线频率即可 */
134+ .scl_speed_hz = 100000 , /* Use the bus frequency */
135135 };
136136 i2c_master_dev_handle_t dev_handle ;
137137 err = i2c_master_bus_add_device (self -> bus_handle , & dev_cfg , & dev_handle );
@@ -141,21 +141,21 @@ int machine_hw_i2c_transfer(mp_obj_base_t *self_in,
141141
142142 int data_len = 0 ;
143143
144- /* 2. 若有 WRITE1 段,先写一段 */
144+ /* 2. If there is a WRITE1 segment, write it first */
145145 if (flags & MP_MACHINE_I2C_FLAG_WRITE1 ) {
146146 if (bufs -> len ) {
147147 err = i2c_master_transmit (dev_handle ,
148148 bufs -> buf ,
149149 bufs -> len ,
150- 1000 ); /* 阻塞超时 1 s */
150+ 1000 ); /* Blocking timeout 1 s */
151151 if (err != ESP_OK ) goto cleanup ;
152152 }
153153 data_len += bufs -> len ;
154154 -- n ;
155155 ++ bufs ;
156156 }
157157 if (flags & MP_MACHINE_I2C_FLAG_READ ) {
158- /* 3. 主循环:剩余段 */
158+ /* 3. Main loop: remaining segments */
159159 for (; n -- ; ++ bufs ) {
160160 if (bufs -> len == 0 ) continue ;
161161 err = i2c_master_receive (dev_handle ,
@@ -168,52 +168,52 @@ int machine_hw_i2c_transfer(mp_obj_base_t *self_in,
168168 data_len += bufs -> len ;
169169 }
170170 } else {
171- // 写操作逻辑
171+ // Write operation logic
172172 size_t total_len = 0 ;
173- mp_machine_i2c_buf_t * original_bufs = bufs ; // 保存原始指针
174- size_t yuann = n ;
173+ mp_machine_i2c_buf_t * original_bufs = bufs ; // Save the original pointer
174+ size_t old_n = n ;
175175
176- // 计算总长度
176+ // Calculate total length
177177 for (; n -- ; ++ bufs ) {
178178 total_len += bufs -> len ;
179179 }
180180
181- // 重置指针
181+ // Reset pointer
182182 bufs = original_bufs ;
183- // 重置n
184- n = yuann ;
185- // 动态分配 write_buf
183+ // Reset n
184+ n = old_n ;
185+ // Dynamically allocate write_buf
186186 uint8_t * write_buf = (uint8_t * )malloc (total_len );
187187 if (write_buf == NULL ) return - MP_ENOMEM ;
188188
189- // 复制数据到 write_buf
189+ // Copy data to write_buf
190190 size_t index = 0 ;
191191 for (; n -- ; ++ bufs ) {
192192 memcpy (write_buf + index , bufs -> buf , bufs -> len );
193193 index += bufs -> len ;
194194 }
195195
196- // 发送数据
196+ // Send data
197197 err = i2c_master_transmit (dev_handle , write_buf , total_len , 1000 );
198198 if (err != ESP_OK ) goto cleanup ;
199199
200- // 释放动态分配的内存
200+ // Free dynamically allocated memory
201201 free (write_buf );
202202 }
203203
204204cleanup :
205- /* 4. 立即销毁临时句柄 */
205+ /* 4. Immediately destroy the temporary handle */
206206 i2c_master_bus_rm_device (dev_handle );
207207
208- /* 5. 出错映射 */
208+ /* 5. Error mapping */
209209 if (err == ESP_FAIL ) return - MP_ENODEV ;
210210 if (err == ESP_ERR_TIMEOUT ) return - MP_ETIMEDOUT ;
211211 if (err != ESP_OK ) return - abs (err );
212212
213213 return data_len ;
214214}
215215
216- // ---------------- 打印 ----------------
216+ // ---------------- Printing ----------------
217217static void machine_hw_i2c_print (const mp_print_t * print ,
218218 mp_obj_t self_in ,
219219 mp_print_kind_t kind ) {
@@ -222,7 +222,7 @@ static void machine_hw_i2c_print(const mp_print_t *print,
222222 self -> port , self -> scl , self -> sda );
223223}
224224
225- // ---------------- 构造函数 ----------------
225+ // ---------------- Constructor ----------------
226226mp_obj_t machine_hw_i2c_make_new (const mp_obj_type_t * type ,
227227 size_t n_args , size_t n_kw ,
228228 const mp_obj_t * all_args ) {
@@ -272,7 +272,7 @@ mp_obj_t machine_hw_i2c_make_new(const mp_obj_type_t *type,
272272 return MP_OBJ_FROM_PTR (self );
273273}
274274
275- // ---------------- 协议表 ----------------
275+ // ---------------- Protocol Table ----------------
276276static const mp_machine_i2c_p_t machine_hw_i2c_p = {
277277 .transfer_supports_write1 = true,
278278 .transfer = machine_hw_i2c_transfer ,
0 commit comments