9797/// i2c.mem_write('abc', 0x42, 2, timeout=10)
9898
9999
100+ typedef struct _pyb_i2c_obj_t {
101+ mp_obj_base_t base ;
102+ int mode ;
103+ union {
104+ uint baudrate ;
105+ byte slvaddr ;
106+ };
107+ } pyb_i2c_obj_t ;
108+
100109/******************************************************************************
101110 DEFINE CONSTANTS
102111 ******************************************************************************/
117126/******************************************************************************
118127 DEFINE PUBLIC FUNCTIONS
119128 ******************************************************************************/
120- void i2c_init0 (void ) {
121- MP_STATE_PORT (pyb_i2c_obj ) = NULL ;
122- }
123-
124129// only master mode is available for the moment
125130void i2c_init (uint mode , uint slvaddr , uint baudrate ) {
126131 // Enable the I2C Peripheral
@@ -139,14 +144,6 @@ void i2c_deinit(void) {
139144/******************************************************************************/
140145/* Micro Python bindings */
141146/******************************************************************************/
142- typedef struct _pyb_i2c_obj_t {
143- mp_obj_base_t base ;
144- int mode ;
145- union {
146- uint baudrate ;
147- byte slvaddr ;
148- };
149- } pyb_i2c_obj_t ;
150147
151148STATIC bool pybI2C_transaction (uint cmd , uint timeout ) {
152149 // Clear all interrupts
@@ -260,11 +257,15 @@ STATIC bool pybI2C_ScanDevice(byte devAddr, uint timeout) {
260257STATIC void pyb_i2c_print (void (* print )(void * env , const char * fmt , ...), void * env , mp_obj_t self_in , mp_print_kind_t kind ) {
261258 pyb_i2c_obj_t * self = self_in ;
262259
263- print (env , "I2C0" );
260+ print (env , "< I2C0" );
264261 if (self -> mode == PYBI2C_MODE_MASTER ) {
265- print (env , ", I2C.MASTER, baudrate=%u)" , self -> baudrate );
266- } else if (self -> mode == PYBI2C_MODE_SLAVE ) {
267- print (env , ", I2C.SLAVE, addr=0x%02x)" , self -> slvaddr );
262+ print (env , ", I2C.MASTER, baudrate=%u>)" , self -> baudrate );
263+ }
264+ else if (self -> mode == PYBI2C_MODE_SLAVE ) {
265+ print (env , ", I2C.SLAVE, addr=0x%02x>)" , self -> slvaddr );
266+ }
267+ else {
268+ print (env , ">" );
268269 }
269270}
270271
@@ -310,7 +311,7 @@ STATIC mp_obj_t pyb_i2c_init_helper(pyb_i2c_obj_t *self_in, mp_uint_t n_args, co
310311
311312/// \classmethod \constructor(bus, ...)
312313///
313- /// Construct an I2C object on the given bus. `bus` can be 1 .
314+ /// Construct an I2C object on the given bus. `bus` can only be 0 .
314315/// With no additional parameters, the I2C object is created but not
315316/// initialised (it has the settings from the last initialisation of
316317/// the bus, if any). If extra arguments are given, the bus is initialised.
@@ -320,25 +321,17 @@ STATIC mp_obj_t pyb_i2c_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n
320321 mp_arg_check_num (n_args , n_kw , 1 , MP_OBJ_FUN_ARGS_MAX , true);
321322
322323 // get i2c number
323- mp_int_t i2c_id = mp_obj_get_int (args [0 ]) - 1 ;
324+ mp_int_t i2c_id = mp_obj_get_int (args [0 ]);
324325
325- // check i2c number
326+ // check the i2c number
326327 if (i2c_id != 0 ) {
327328 nlr_raise (mp_obj_new_exception_msg (& mp_type_ValueError , mpexception_value_invalid_arguments ));
328329 }
329330
330- // setup the object
331- pyb_i2c_obj_t * self ;
332- if (MP_STATE_PORT (pyb_i2c_obj ) == NULL ) {
333- // create a new I2C object
334- self = m_new_obj (pyb_i2c_obj_t );
335- self -> base .type = & pyb_i2c_type ;
336- self -> mode = PYBI2C_MODE_DISABLED ;
337- MP_STATE_PORT (pyb_i2c_obj ) = self ;
338- } else {
339- // reference the existing I2C object
340- self = MP_STATE_PORT (pyb_i2c_obj );
341- }
331+ // create and setup the object
332+ pyb_i2c_obj_t * self = m_new_obj (pyb_i2c_obj_t );
333+ self -> base .type = & pyb_i2c_type ;
334+ self -> mode = PYBI2C_MODE_DISABLED ;
342335
343336 if (n_args > 1 || n_kw > 0 ) {
344337 // start the peripheral
@@ -610,6 +603,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_mem_write_obj, 1, pyb_i2c_mem_write);
610603
611604STATIC const mp_map_elem_t pyb_i2c_locals_dict_table [] = {
612605 // instance methods
606+ { MP_OBJ_NEW_QSTR (MP_QSTR___del__ ), (mp_obj_t )& pyb_i2c_deinit_obj },
613607 { MP_OBJ_NEW_QSTR (MP_QSTR_init ), (mp_obj_t )& pyb_i2c_init_obj },
614608 { MP_OBJ_NEW_QSTR (MP_QSTR_deinit ), (mp_obj_t )& pyb_i2c_deinit_obj },
615609 { MP_OBJ_NEW_QSTR (MP_QSTR_is_ready ), (mp_obj_t )& pyb_i2c_is_ready_obj },
0 commit comments