48
48
#define nrfx_twi_config_t nrfx_twim_config_t
49
49
50
50
#define nrfx_twi_init nrfx_twim_init
51
+ #define nrfx_twi_uninit nrfx_twim_uninit
51
52
#define nrfx_twi_enable nrfx_twim_enable
52
53
#define nrfx_twi_xfer nrfx_twim_xfer
53
54
#define nrfx_twi_disable nrfx_twim_disable
59
60
60
61
#define NRFX_TWI_INSTANCE NRFX_TWIM_INSTANCE
61
62
63
+ #define NRF_TWI_FREQ_100K NRF_TWIM_FREQ_100K
64
+ #define NRF_TWI_FREQ_250K NRF_TWIM_FREQ_250K
62
65
#define NRF_TWI_FREQ_400K NRF_TWIM_FREQ_400K
63
66
64
67
#endif
@@ -96,11 +99,12 @@ STATIC void machine_hard_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp
96
99
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 ) {
97
100
MP_MACHINE_I2C_CHECK_FOR_LEGACY_SOFTI2C_CONSTRUCTION (n_args , n_kw , all_args );
98
101
99
- enum { ARG_id , ARG_scl , ARG_sda };
102
+ enum { ARG_id , ARG_scl , ARG_sda , ARG_freq };
100
103
static const mp_arg_t allowed_args [] = {
101
104
{ MP_QSTR_id , MP_ARG_REQUIRED | MP_ARG_OBJ },
102
105
{ MP_QSTR_scl , MP_ARG_REQUIRED | MP_ARG_OBJ },
103
106
{ MP_QSTR_sda , MP_ARG_REQUIRED | MP_ARG_OBJ },
107
+ { MP_QSTR_freq , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = -1 } },
104
108
};
105
109
106
110
// 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
115
119
config .scl = mp_hal_get_pin_obj (args [ARG_scl ].u_obj )-> pin ;
116
120
config .sda = mp_hal_get_pin_obj (args [ARG_sda ].u_obj )-> pin ;
117
121
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 ;
119
131
120
132
config .hold_bus_uninit = false;
121
133
134
+ // First reset the TWI
135
+ nrfx_twi_uninit (& self -> p_twi );
136
+
122
137
// Set context to this object.
123
138
nrfx_twi_init (& self -> p_twi , & config , NULL , (void * )self );
124
139
0 commit comments