Skip to content

Commit 8b74048

Browse files
committed
extmod/machine_i2c: Expose soft I2C obj and readfrom/writeto funcs.
For external use by ports if needed.
1 parent 4c90561 commit 8b74048

2 files changed

Lines changed: 17 additions & 11 deletions

File tree

extmod/machine_i2c.c

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,7 @@
3535

3636
#if MICROPY_PY_MACHINE_I2C
3737

38-
typedef struct _machine_i2c_obj_t {
39-
mp_obj_base_t base;
40-
uint32_t us_delay;
41-
uint32_t us_timeout;
42-
mp_hal_pin_obj_t scl;
43-
mp_hal_pin_obj_t sda;
44-
} machine_i2c_obj_t;
38+
typedef mp_machine_soft_i2c_obj_t machine_i2c_obj_t;
4539

4640
STATIC void mp_hal_i2c_delay(machine_i2c_obj_t *self) {
4741
// We need to use an accurate delay to get acceptable I2C
@@ -188,7 +182,7 @@ STATIC int mp_hal_i2c_read_byte(machine_i2c_obj_t *self, uint8_t *val, int nack)
188182
// return value:
189183
// >=0 - number of acks received
190184
// <0 - error, with errno being the negative of the return value
191-
STATIC int mp_machine_soft_i2c_writeto(mp_obj_base_t *self_in, uint16_t addr, const uint8_t *src, size_t len, bool stop) {
185+
int mp_machine_soft_i2c_writeto(mp_obj_base_t *self_in, uint16_t addr, const uint8_t *src, size_t len, bool stop) {
192186
machine_i2c_obj_t *self = (machine_i2c_obj_t*)self_in;
193187

194188
// start the I2C transaction
@@ -234,7 +228,7 @@ STATIC int mp_machine_soft_i2c_writeto(mp_obj_base_t *self_in, uint16_t addr, co
234228
// return value:
235229
// 0 - success
236230
// <0 - error, with errno being the negative of the return value
237-
STATIC int mp_machine_soft_i2c_readfrom(mp_obj_base_t *self_in, uint16_t addr, uint8_t *dest, size_t len, bool stop) {
231+
int mp_machine_soft_i2c_readfrom(mp_obj_base_t *self_in, uint16_t addr, uint8_t *dest, size_t len, bool stop) {
238232
machine_i2c_obj_t *self = (machine_i2c_obj_t*)self_in;
239233

240234
// start the I2C transaction
@@ -582,7 +576,7 @@ STATIC const mp_rom_map_elem_t machine_i2c_locals_dict_table[] = {
582576
{ MP_ROM_QSTR(MP_QSTR_writeto_mem), MP_ROM_PTR(&machine_i2c_writeto_mem_obj) },
583577
};
584578

585-
STATIC MP_DEFINE_CONST_DICT(machine_i2c_locals_dict, machine_i2c_locals_dict_table);
579+
MP_DEFINE_CONST_DICT(mp_machine_soft_i2c_locals_dict, machine_i2c_locals_dict_table);
586580

587581
int mp_machine_soft_i2c_read(mp_obj_base_t *self_in, uint8_t *dest, size_t len, bool nack) {
588582
machine_i2c_obj_t *self = (machine_i2c_obj_t*)self_in;
@@ -625,7 +619,7 @@ const mp_obj_type_t machine_i2c_type = {
625619
.name = MP_QSTR_I2C,
626620
.make_new = machine_i2c_make_new,
627621
.protocol = &mp_machine_soft_i2c_p,
628-
.locals_dict = (mp_obj_dict_t*)&machine_i2c_locals_dict,
622+
.locals_dict = (mp_obj_dict_t*)&mp_machine_soft_i2c_locals_dict,
629623
};
630624

631625
#endif // MICROPY_PY_MACHINE_I2C

extmod/machine_i2c.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ typedef struct _mp_machine_i2c_p_t {
4040
int (*writeto)(mp_obj_base_t *obj, uint16_t addr, const uint8_t *src, size_t len, bool stop);
4141
} mp_machine_i2c_p_t;
4242

43+
typedef struct _mp_machine_soft_i2c_obj_t {
44+
mp_obj_base_t base;
45+
uint32_t us_delay;
46+
uint32_t us_timeout;
47+
mp_hal_pin_obj_t scl;
48+
mp_hal_pin_obj_t sda;
49+
} mp_machine_soft_i2c_obj_t;
50+
4351
extern const mp_obj_type_t machine_i2c_type;
52+
extern const mp_obj_dict_t mp_machine_soft_i2c_locals_dict;
53+
54+
int mp_machine_soft_i2c_readfrom(mp_obj_base_t *self_in, uint16_t addr, uint8_t *dest, size_t len, bool stop);
55+
int mp_machine_soft_i2c_writeto(mp_obj_base_t *self_in, uint16_t addr, const uint8_t *src, size_t len, bool stop);
4456

4557
#endif // __MICROPY_INCLUDED_EXTMOD_MACHINE_I2C_H__

0 commit comments

Comments
 (0)