@@ -105,7 +105,7 @@ typedef struct _pyb_i2c_obj_t {
105105#define PYBI2C_MODE_MASTER (0)
106106#define PYBI2C_MODE_SLAVE (1)
107107
108- #define PYBI2C_DEF_BAUD_RATE_HZ (100000 )
108+ #define PYBI2C_MIN_BAUD_RATE_HZ (50000 )
109109#define PYBI2C_DEF_BAUD_RATE_HZ (100000)
110110#define PYBI2C_MAX_BAUD_RATE_HZ (400000)
111111
@@ -123,7 +123,7 @@ typedef struct _pyb_i2c_obj_t {
123123/******************************************************************************
124124 DECLARE PRIVATE DATA
125125 ******************************************************************************/
126- STATIC pyb_i2c_obj_t pyb_i2c_obj ;
126+ STATIC pyb_i2c_obj_t pyb_i2c_obj = {. baudrate = 0 } ;
127127
128128/******************************************************************************
129129 DEFINE PRIVATE FUNCTIONS
@@ -140,6 +140,8 @@ STATIC void i2c_init (pyb_i2c_obj_t *self) {
140140STATIC void i2c_deinit (void ) {
141141 MAP_I2CMasterDisable (I2CA0_BASE );
142142 MAP_PRCMPeripheralClkDisable (PRCM_I2CA0 , PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK );
143+ // invalidate the baudrate
144+ pyb_i2c_obj .baudrate = 0 ;
143145}
144146
145147STATIC bool pyb_i2c_transaction (uint cmd , int timeout ) {
@@ -287,11 +289,12 @@ STATIC mp_obj_t pyb_i2c_init_helper(pyb_i2c_obj_t *self_in, mp_uint_t n_args, co
287289 mp_arg_parse_all (n_args , args , kw_args , PYB_I2C_INIT_NUM_ARGS , pyb_i2c_init_args , vals );
288290
289291 if (vals [0 ].u_int != PYBI2C_MODE_MASTER ) {
290- // thrown an exception since only master mode is supported for now
292+ // thrown an exception since only master mode is supported
291293 nlr_raise (mp_obj_new_exception_msg (& mp_type_ValueError , mpexception_value_invalid_arguments ));
292294 }
293295
294- self -> baudrate = MIN (vals [2 ].u_int , PYBI2C_MAX_BAUD_RATE_HZ );
296+ // make sure the baudrate is between the valid range
297+ self -> baudrate = MIN (MAX (vals [2 ].u_int , PYBI2C_MIN_BAUD_RATE_HZ ), PYBI2C_MAX_BAUD_RATE_HZ );
295298
296299 // init the I2C bus
297300 i2c_init (self );
@@ -337,7 +340,12 @@ STATIC mp_obj_t pyb_i2c_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n
337340
338341STATIC void pyb_i2c_print (void (* print )(void * env , const char * fmt , ...), void * env , mp_obj_t self_in , mp_print_kind_t kind ) {
339342 pyb_i2c_obj_t * self = self_in ;
340- print (env , "<I2C0, I2C.MASTER, baudrate=%u>)" , self -> baudrate );
343+ if (self -> baudrate > 0 ) {
344+ print (env , "<I2C0, I2C.MASTER, baudrate=%u>)" , self -> baudrate );
345+ }
346+ else {
347+ print (env , "<I2C0>" );
348+ }
341349}
342350
343351STATIC mp_obj_t pyb_i2c_init (mp_uint_t n_args , const mp_obj_t * args , mp_map_t * kw_args ) {
@@ -545,7 +553,7 @@ STATIC mp_obj_t pyb_i2c_mem_write(mp_uint_t n_args, const mp_obj_t *args, mp_map
545553 // determine width of mem_addr (1 or 2 bytes)
546554 mp_uint_t mem_addr_size = vals [4 ].u_int >> 3 ;
547555
548- // Write the register address to be write to.
556+ // Write the register address to write to.
549557 if (pyb_i2c_write (i2c_addr , (byte * )& mem_addr , mem_addr_size , false, vals [3 ].u_int )) {
550558 // Write the specified length of data
551559 if (pyb_i2c_write (i2c_addr , bufinfo .buf , bufinfo .len , true, vals [3 ].u_int )) {
0 commit comments