8000 nrf/modules/machine: Support the freq=n argument for machine.I2C. · peterwillcn/micropython@a2b31f9 · GitHub
[go: up one dir, main page]

Skip to content

Commit a2b31f9

Browse files
robert-hhdpgeorge
authored andcommitted
nrf/modules/machine: Support the freq=n argument for machine.I2C.
Mostly for compatibility. Effective values are 100000, 250000 and 400000. The supplied values are mapped to these.
1 parent 3bbf2ef commit a2b31f9

File tree

1 file changed

+17
-2
lines changed
  • ports/nrf/modules/machine

1 file changed

+17
-2
lines changed

ports/nrf/modules/machine/i2c.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#define nrfx_twi_config_t nrfx_twim_config_t
4949

5050
#define nrfx_twi_init nrfx_twim_init
51+
#define nrfx_twi_uninit nrfx_twim_uninit
5152
#define nrfx_twi_enable nrfx_twim_enable
5253
#define nrfx_twi_xfer nrfx_twim_xfer
5354
#define nrfx_twi_disable nrfx_twim_disable
@@ -59,6 +60,8 @@
5960

6061
#define NRFX_TWI_INSTANCE NRFX_TWIM_INSTANCE
6162

63+
#define NRF_TWI_FREQ_100K NRF_TWIM_FREQ_100K
64+
#define NRF_TWI_FREQ_250K NRF_TWIM_FREQ_250K
6265
#define NRF_TWI_FREQ_400K NRF_TWIM_FREQ_400K
6366

6467
#endif
@@ -96,11 +99,12 @@ STATIC void machine_hard_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp
9699
mp_obj_t machine_hard_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
97100
MP_MACHINE_I2C_CHECK_FOR_LEGACY_SOFTI2C_CONSTRUCTION(n_args, n_kw, all_args);
98101

99-
enum { ARG_id, ARG_scl, ARG_sda };
102+
enum { ARG_id, ARG_scl, ARG_sda, ARG_freq };
100103
static const mp_arg_t allowed_args[] = {
101104
{ MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_OBJ },
102105
{ MP_QSTR_scl, MP_ARG_REQUIRED | MP_ARG_OBJ },
103106
{ MP_QSTR_sda, MP_ARG_REQUIRED | MP_ARG_OBJ },
107+
{ MP_QSTR_freq, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1} },
104108
};
105109

106110
// parse args
@@ -115,10 +119,21 @@ mp_obj_t machine_hard_i2c_make_new(const mp_obj_type_t *type, size_t n_args, siz
115119
config.scl = mp_hal_get_pin_obj(args[ARG_scl].u_obj)->pin;
116120
config.sda = mp_hal_get_pin_obj(args[ARG_sda].u_obj)->pin;
117121

118-
config.frequency = NRF_TWI_FREQ_400K;
122+
int freq = NRF_TWI_FREQ_400K;
123+
if (args[ARG_freq].u_int != -1) {
124+
if (args[ARG_freq].u_int <= 150000) {
125+
freq = NRF_TWI_FREQ_100K;
126+
} else if (args[ARG_freq].u_int < 320000) {
127+
freq = NRF_TWI_FREQ_250K;
128+
}
129+
}
130+
config.frequency = freq;
119131

120132
config.hold_bus_uninit = false;
121133

134+
// First reset the TWI
135+
nrfx_twi_uninit(&self->p_twi);
136+
122137
// Set context to this object.
123138
nrfx_twi_init(&self->p_twi, &config, NULL, (void *)self);
124139

0 commit comments

Comments
 (0)
0