5656///
5757/// from pyb import I2C
5858///
59- /// i2c = I2C() # create
60- /// i2c = I2C(50000) # create and init with a 50KHz baudrate
61- /// i2c.init(100000) # init with a 100KHz baudrate
62- /// i2c.deinit() # turn off the peripheral
59+ /// i2c = I2C(1) # create
60+ /// i2c = I2C(1, 50000) # create and init with a 50KHz baudrate
61+ /// i2c.init(100000) # init with a 100KHz baudrate
62+ /// i2c.deinit() # turn off the peripheral
6363///
6464/// Printing the i2c object gives you information about its configuration.
6565///
7676///
7777/// A master must specify the recipient's address:
7878///
79- /// i2c.init(100000)
79+ /// i2c.init(1, 100000)
8080/// i2c.send('123', 0x42) # send 3 bytes to slave with address 0x42
8181/// i2c.send(b'456', addr=0x42) # keyword for address
8282///
@@ -98,6 +98,8 @@ typedef struct _pyb_i2c_obj_t {
9898/******************************************************************************
9999 DEFINE CONSTANTS
100100 ******************************************************************************/
101+ #define PYBI2C_MASTER (0)
102+
101103#define PYBI2C_MIN_BAUD_RATE_HZ (50000)
102104#define PYBI2C_MAX_BAUD_RATE_HZ (400000)
103105
@@ -251,16 +253,35 @@ STATIC bool pyb_i2c_scan_device(byte devAddr) {
251253/******************************************************************************/
252254/* Micro Python bindings */
253255/******************************************************************************/
256+ STATIC void pyb_i2c_print (const mp_print_t * print , mp_obj_t self_in , mp_print_kind_t kind ) {
257+ pyb_i2c_obj_t * self = self_in ;
258+ if (self -> baudrate > 0 ) {
259+ mp_printf (print , "<I2C1, I2C.MASTER, baudrate=%u>)" , self -> baudrate );
260+ }
261+ else {
262+ mp_print_str (print , "<I2C1>" );
263+ }
264+ }
254265
255- /// \method init(100000)
266+ /// \method init(mode, *, baudrate= 100000)
256267///
257- /// Initialise the I2C bus as a master with the given baudrate.
268+ /// Initialise the I2C bus with the given parameters:
258269///
259- STATIC mp_obj_t pyb_i2c_init_helper (pyb_i2c_obj_t * self_in , mp_obj_t baudrate ) {
260- pyb_i2c_obj_t * self = self_in ;
270+ /// - `mode` must be either `I2C.MASTER` or `I2C.SLAVE`
271+ /// - `baudrate` is the SCL clock rate (only sensible for a master)
272+ STATIC const mp_arg_t pyb_i2c_init_args [] = {
273+ { MP_QSTR_mode , MP_ARG_REQUIRED | MP_ARG_INT , },
274+ { MP_QSTR_baudrate , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 100000 } },
275+ };
276+ #define PYB_I2C_INIT_NUM_ARGS MP_ARRAY_SIZE(pyb_i2c_init_args)
277+
278+ STATIC mp_obj_t pyb_i2c_init_helper (pyb_i2c_obj_t * self , mp_uint_t n_args , const mp_obj_t * args , mp_map_t * kw_args ) {
279+ // parse args
280+ mp_arg_val_t vals [PYB_I2C_INIT_NUM_ARGS ];
281+ mp_arg_parse_all (n_args , args , kw_args , PYB_I2C_INIT_NUM_ARGS , pyb_i2c_init_args , vals );
261282
262283 // make sure the baudrate is between the valid range
263- self -> baudrate = MIN (MAX (mp_obj_get_int ( baudrate ) , PYBI2C_MIN_BAUD_RATE_HZ ), PYBI2C_MAX_BAUD_RATE_HZ );
284+ self -> baudrate = MIN (MAX (vals [ 1 ]. u_int , PYBI2C_MIN_BAUD_RATE_HZ ), PYBI2C_MAX_BAUD_RATE_HZ );
264285
265286 // init the I2C bus
266287 i2c_init (self );
@@ -273,7 +294,7 @@ STATIC mp_obj_t pyb_i2c_init_helper(pyb_i2c_obj_t *self_in, mp_obj_t baudrate) {
273294
274295/// \classmethod \constructor(bus, ...)
275296///
276- /// Construct an I2C object on the given bus. `bus` can only be 0 .
297+ /// Construct an I2C object on the given bus. `bus` can only be 1 .
277298/// With no additional parameters, the I2C object is created but not
278299/// initialised (it has the settings from the last initialisation of
279300/// the bus, if any). If extra arguments are given, the bus is initialised.
@@ -282,32 +303,29 @@ STATIC mp_obj_t pyb_i2c_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n
282303 // check arguments
283304 mp_arg_check_num (n_args , n_kw , 1 , MP_OBJ_FUN_ARGS_MAX , true);
284305
306+ // work out the i2c bus id
307+ if (mp_obj_get_int (args [0 ]) != 1 ) {
308+ nlr_raise (mp_obj_new_exception_msg (& mp_type_OSError , mpexception_os_resource_not_avaliable ));
309+ }
310+
285311 // setup the object
286312 pyb_i2c_obj_t * self = & pyb_i2c_obj ;
287313 self -> base .type = & pyb_i2c_type ;
288314
289- if (n_args > 0 ) {
315+ if (n_args > 1 || n_kw > 0 ) {
290316 // start the peripheral
291- pyb_i2c_init_helper (self , * args );
317+ mp_map_t kw_args ;
318+ mp_map_init_fixed_table (& kw_args , n_kw , args + n_args );
319+ pyb_i2c_init_helper (self , n_args - 1 , args + 1 , & kw_args );
292320 }
293321
294322 return (mp_obj_t )self ;
295323}
296324
297- STATIC void pyb_i2c_print (const mp_print_t * print , mp_obj_t self_in , mp_print_kind_t kind ) {
298- pyb_i2c_obj_t * self = self_in ;
299- if (self -> baudrate > 0 ) {
300- mp_printf (print , "<I2C0, I2C.MASTER, baudrate=%u>)" , self -> baudrate );
301- }
302- else {
303- mp_print_str (print , "<I2C0>" );
304- }
325+ STATIC mp_obj_t pyb_i2c_init (mp_uint_t n_args , const mp_obj_t * args , mp_map_t * kw_args ) {
326+ return pyb_i2c_init_helper (args [0 ], n_args - 1 , args + 1 , kw_args );
305327}
306-
307- STATIC mp_obj_t pyb_i2c_init (mp_obj_t self_in , mp_obj_t baudrate ) {
308- return pyb_i2c_init_helper (self_in , baudrate );
309- }
310- STATIC MP_DEFINE_CONST_FUN_OBJ_2 (pyb_i2c_init_obj , pyb_i2c_init );
328+ STATIC MP_DEFINE_CONST_FUN_OBJ_KW (pyb_i2c_init_obj , 1 , pyb_i2c_init );
311329
312330/// \method deinit()
313331/// Turn off the I2C bus.
@@ -529,6 +547,10 @@ STATIC const mp_map_elem_t pyb_i2c_locals_dict_table[] = {
529547 { MP_OBJ_NEW_QSTR (MP_QSTR_recv ), (mp_obj_t )& pyb_i2c_recv_obj },
530548 { MP_OBJ_NEW_QSTR (MP_QSTR_mem_read ), (mp_obj_t )& pyb_i2c_mem_read_obj },
531549 { MP_OBJ_NEW_QSTR (MP_QSTR_mem_write ), (mp_obj_t )& pyb_i2c_mem_write_obj },
550+
551+ // class constants
552+ /// \constant MASTER - for initialising the bus to master mode
553+ { MP_OBJ_NEW_QSTR (MP_QSTR_MASTER ), MP_OBJ_NEW_SMALL_INT (PYBI2C_MASTER ) },
532554};
533555
534556STATIC MP_DEFINE_CONST_DICT (pyb_i2c_locals_dict , pyb_i2c_locals_dict_table );
0 commit comments