4747
4848/// \moduleref pyb
4949/// \class I2C - a two-wire serial protocol
50- ///
51- /// I2C is a two-wire protocol for communicating between devices. At the physical
52- /// level it consists of 2 wires: SCL and SDA, the clock and data lines respectively.
53- ///
54- /// I2C objects are created attached to a specific bus. They can be initialised
55- /// when created, or initialised later on:
56- ///
57- /// from pyb import I2C
58- ///
59- /// i2c = I2C(1) # create
60- /// i2c = I2C(1, I2C.MASTER, baudrate=50000) # create and init with a 50KHz baudrate
61- /// i2c.init(I2C.MASTER, baudrate=100000) # init with a 100KHz baudrate
62- /// i2c.deinit() # turn off the peripheral
63- ///
64- /// Printing the i2c object gives you information about its configuration.
65- ///
66- /// Basic methods for slave are send and recv:
67- ///
68- /// i2c.send('abc') # send 3 bytes
69- /// i2c.send(0x42) # send a single byte, given by the number
70- /// data = i2c.recv(3) # receive 3 bytes
71- ///
72- /// To receive inplace, first create a bytearray:
73- ///
74- /// data = bytearray(3) # create a buffer
75- /// i2c.recv(data) # receive 3 bytes, writing them into data
76- ///
77- /// A master must specify the recipient's address:
78- ///
79- /// i2c.send('123', 0x42) # send 3 bytes to slave with address 0x42
80- /// i2c.send(b'456', addr=0x42) # keyword for address
81- ///
82- /// Master also has other methods:
83- ///
84- /// i2c.is_ready(0x42) # check if slave 0x42 is ready
85- /// i2c.scan() # scan for slaves on the bus, returning
86- /// # a list of valid addresses
87- /// i2c.mem_read(3, 0x42, 2) # read 3 bytes from memory of slave 0x42,
88- /// # starting at address 2 in the slave
89- /// i2c.mem_write('abc', 0x42, 2) # write 3 bytes to memory of slave 0x42,
90- /// # starting at address 2 in the slave
9150
9251typedef struct _pyb_i2c_obj_t {
9352 mp_obj_base_t base ;
@@ -262,12 +221,7 @@ STATIC void pyb_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp_print_ki
262221 }
263222}
264223
265- /// \method init(mode, *, baudrate=100000)
266- ///
267- /// Initialise the I2C bus with the given parameters:
268- ///
269- /// - `mode` must be either `I2C.MASTER` or `I2C.SLAVE`
270- /// - `baudrate` is the SCL clock rate (only sensible for a master)
224+ /// \method init()
271225STATIC const mp_arg_t pyb_i2c_init_args [] = {
272226 { MP_QSTR_mode , MP_ARG_REQUIRED | MP_ARG_INT , },
273227 { MP_QSTR_baudrate , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 100000 } },
@@ -297,12 +251,6 @@ STATIC mp_obj_t pyb_i2c_init_helper(pyb_i2c_obj_t *self, mp_uint_t n_args, const
297251}
298252
299253/// \classmethod \constructor(bus, ...)
300- ///
301- /// Construct an I2C object on the given bus. `bus` can only be 1.
302- /// With no additional parameters, the I2C object is created but not
303- /// initialised (it has the settings from the last initialisation of
304- /// the bus, if any). If extra arguments are given, the bus is initialised.
305- /// See `init` for parameters of initialisation.
306254STATIC mp_obj_t pyb_i2c_make_new (mp_obj_t type_in , mp_uint_t n_args , mp_uint_t n_kw , const mp_obj_t * args ) {
307255 // check arguments
308256 mp_arg_check_num (n_args , n_kw , 1 , MP_OBJ_FUN_ARGS_MAX , true);
@@ -311,6 +259,11 @@ STATIC mp_obj_t pyb_i2c_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n
311259 pyb_i2c_obj_t * self = & pyb_i2c_obj ;
312260 self -> base .type = & pyb_i2c_type ;
313261
262+ // check the peripheral id
263+ if (mp_obj_get_int (args [0 ]) != 0 ) {
264+ nlr_raise (mp_obj_new_exception_msg (& mp_type_OSError , mpexception_os_resource_not_avaliable ));
265+ }
266+
314267 if (n_args > 1 || n_kw > 0 ) {
315268 // start the peripheral
316269 mp_map_t kw_args ;
@@ -327,7 +280,6 @@ STATIC mp_obj_t pyb_i2c_init(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *k
327280STATIC MP_DEFINE_CONST_FUN_OBJ_KW (pyb_i2c_init_obj , 1 , pyb_i2c_init );
328281
329282/// \method deinit()
330- /// Turn off the I2C bus.
331283STATIC mp_obj_t pyb_i2c_deinit (mp_obj_t self_in ) {
332284 // disable the peripheral
333285 MAP_I2CMasterDisable (I2CA0_BASE );
@@ -341,7 +293,6 @@ STATIC mp_obj_t pyb_i2c_deinit(mp_obj_t self_in) {
341293STATIC MP_DEFINE_CONST_FUN_OBJ_1 (pyb_i2c_deinit_obj , pyb_i2c_deinit );
342294
343295/// \method is_ready(addr)
344- /// Check if an I2C device responds to the given address. Only valid when in master mode.
345296STATIC mp_obj_t pyb_i2c_is_ready (mp_obj_t self_in , mp_obj_t i2c_addr_o ) {
346297 mp_uint_t i2c_addr = mp_obj_get_int (i2c_addr_o );
347298 for (int i = 0 ; i < 7 ; i ++ ) {
@@ -354,8 +305,6 @@ STATIC mp_obj_t pyb_i2c_is_ready(mp_obj_t self_in, mp_obj_t i2c_addr_o) {
354305STATIC MP_DEFINE_CONST_FUN_OBJ_2 (pyb_i2c_is_ready_obj , pyb_i2c_is_ready );
355306
356307/// \method scan()
357- /// Scan all I2C addresses from 0x01 to 0x7f and return a list of those that respond.
358- /// Only valid when in master mode.
359308STATIC mp_obj_t pyb_i2c_scan (mp_obj_t self_in ) {
360309 mp_obj_t list = mp_obj_new_list (0 , NULL );
361310 for (uint addr = 1 ; addr <= 127 ; addr ++ ) {
@@ -366,17 +315,11 @@ STATIC mp_obj_t pyb_i2c_scan(mp_obj_t self_in) {
366315 }
367316 }
368317 }
369-
370318 return list ;
371319}
372320STATIC MP_DEFINE_CONST_FUN_OBJ_1 (pyb_i2c_scan_obj , pyb_i2c_scan );
373321
374- /// \method send(send, addr=0x00)
375- /// Send data on the bus:
376- ///
377- /// - `send` is the data to send (an integer to send, or a buffer object)
378- /// - `addr` is the address to send to (only required in master mode)
379- /// Return value: `None`.
322+ /// \method send()
380323STATIC const mp_arg_t pyb_i2c_send_args [] = {
381324 { MP_QSTR_send , MP_ARG_REQUIRED | MP_ARG_OBJ , },
382325 { MP_QSTR_addr , MP_ARG_INT , {.u_int = 0 } },
@@ -403,16 +346,7 @@ STATIC mp_obj_t pyb_i2c_send(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *k
403346}
404347STATIC MP_DEFINE_CONST_FUN_OBJ_KW (pyb_i2c_send_obj , 1 , pyb_i2c_send );
405348
406- /// \method recv(recv, addr=0x00)
407- ///
408- /// Receive data on the bus:
409- ///
410- /// - `recv` can be an integer, which is the number of bytes to receive,
411- /// or a mutable buffer, which will be filled with received bytes
412- /// - `addr` is the address to receive from (only required in master mode)
413- ///
414- /// Return value: if `recv` is an integer then a new buffer of the bytes received,
415- /// otherwise the same buffer that was passed in to `recv`.
349+ /// \method recv()
416350STATIC const mp_arg_t pyb_i2c_recv_args [] = {
417351 { MP_QSTR_recv , MP_ARG_REQUIRED | MP_ARG_OBJ , },
418352 { MP_QSTR_addr , MP_ARG_INT , {.u_int = 0 } },
@@ -444,17 +378,7 @@ STATIC mp_obj_t pyb_i2c_recv(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *k
444378}
445379STATIC MP_DEFINE_CONST_FUN_OBJ_KW (pyb_i2c_recv_obj , 1 , pyb_i2c_recv );
446380
447- /// \method mem_read(data, addr, memaddr, addr_size=8)
448- ///
449- /// Read from the memory of an I2C device:
450- ///
451- /// - `data` can be an integer or a buffer to read into
452- /// - `addr` is the I2C device address
453- /// - `memaddr` is the memory location within the I2C device
454- /// - `addr_size` selects the width of memaddr: 8 or 16 bits
455- ///
456- /// Returns the read data.
457- /// This is only valid in master mode.
381+ /// \method mem_read()
458382STATIC const mp_arg_t pyb_i2c_mem_read_args [] = {
459383 { MP_QSTR_data , MP_ARG_REQUIRED | MP_ARG_OBJ , },
460384 { MP_QSTR_addr , MP_ARG_REQUIRED | MP_ARG_INT , {.u_int = 0 } },
@@ -498,17 +422,7 @@ STATIC mp_obj_t pyb_i2c_mem_read(mp_uint_t n_args, const mp_obj_t *args, mp_map_
498422}
499423STATIC MP_DEFINE_CONST_FUN_OBJ_KW (pyb_i2c_mem_read_obj , 1 , pyb_i2c_mem_read );
500424
501- /// \method mem_write(data, addr, memaddr, addr_size=8)
502- ///
503- /// Write to the memory of an I2C device:
504- ///
505- /// - `data` can be an integer or a buffer to write from
506- /// - `addr` is the I2C device address
507- /// - `memaddr` is the memory location within the I2C device
508- /// - `addr_size` selects the width of memaddr: 8 or 16 bits
509- ///
510- /// Returns `None`.
511- /// This is only valid in master mode.
425+ /// \method mem_write()
512426STATIC mp_obj_t pyb_i2c_mem_write (mp_uint_t n_args , const mp_obj_t * args , mp_map_t * kw_args ) {
513427 // parse args (same as mem_read)
514428 mp_arg_val_t vals [PYB_I2C_MEM_READ_NUM_ARGS ];
0 commit comments