Skip to content

Commit 2822d4e

Browse files
committed
stmhal: Add I2C functions for pure master read/write.
1 parent ee01411 commit 2822d4e

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

stmhal/i2c.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,48 @@ STATIC mp_obj_t pyb_i2c_scan(mp_obj_t self_in) {
136136

137137
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_i2c_scan_obj, pyb_i2c_scan);
138138

139+
STATIC mp_obj_t pyb_i2c_read(uint n_args, const mp_obj_t *args) {
140+
pyb_i2c_obj_t *self = args[0];
141+
machine_uint_t i2c_addr = mp_obj_get_int(args[1]) << 1;
142+
machine_uint_t n = mp_obj_get_int(args[2]);
143+
144+
byte *data;
145+
mp_obj_t o = mp_obj_str_builder_start(&mp_type_bytes, n, &data);
146+
HAL_StatusTypeDef status = HAL_I2C_Master_Receive(self->i2c_handle, i2c_addr, data, n, 500);
147+
148+
if (status != HAL_OK) {
149+
// TODO really need a HardwareError object, or something
150+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_Exception, "HAL_I2C_Master_Receive failed with code %d", status));
151+
}
152+
153+
return mp_obj_str_builder_end(o);
154+
}
155+
156+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_i2c_read_obj, 3, 3, pyb_i2c_read);
157+
158+
STATIC mp_obj_t pyb_i2c_write(uint n_args, const mp_obj_t *args) {
159+
pyb_i2c_obj_t *self = args[0];
160+
machine_uint_t i2c_addr = mp_obj_get_int(args[1]) << 1;
161+
HAL_StatusTypeDef status;
162+
if (MP_OBJ_IS_INT(args[2])) {
163+
uint8_t data[1] = {mp_obj_get_int(args[2])};
164+
status = HAL_I2C_Master_Transmit(self->i2c_handle, i2c_addr, data, 1, 500);
165+
} else {
166+
buffer_info_t bufinfo;
167+
mp_get_buffer_raise(args[2], &bufinfo);
168+
status = HAL_I2C_Master_Transmit(self->i2c_handle, i2c_addr, bufinfo.buf, bufinfo.len, 500);
169+
}
170+
171+
if (status != HAL_OK) {
172+
// TODO really need a HardwareError object, or something
173+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_Exception, "HAL_I2C_Master_Transmit failed with code %d", status));
174+
}
175+
176+
return mp_const_none;
177+
}
178+
179+
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_i2c_write_obj, 3, 3, pyb_i2c_write);
180+
139181
STATIC mp_obj_t pyb_i2c_mem_read(uint n_args, const mp_obj_t *args) {
140182
pyb_i2c_obj_t *self = args[0];
141183
machine_uint_t i2c_addr = mp_obj_get_int(args[1]) << 1;
@@ -187,6 +229,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_i2c_mem_write_obj, 4, 4, pyb_i2c_
187229
STATIC const mp_map_elem_t pyb_i2c_locals_dict_table[] = {
188230
{ MP_OBJ_NEW_QSTR(MP_QSTR_is_ready), (mp_obj_t)&pyb_i2c_is_ready_obj },
189231
{ MP_OBJ_NEW_QSTR(MP_QSTR_scan), (mp_obj_t)&pyb_i2c_scan_obj },
232+
{ MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&pyb_i2c_read_obj },
233+
{ MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&pyb_i2c_write_obj },
190234
{ MP_OBJ_NEW_QSTR(MP_QSTR_mem_read), (mp_obj_t)&pyb_i2c_mem_read_obj },
191235
{ MP_OBJ_NEW_QSTR(MP_QSTR_mem_write), (mp_obj_t)&pyb_i2c_mem_write_obj },
192236
};

0 commit comments

Comments
 (0)