|
14 | 14 | #include "bufhelper.h" |
15 | 15 | #include "i2c.h" |
16 | 16 |
|
| 17 | +// Usage model: |
| 18 | +// |
| 19 | +// I2C objects are created attached to a specific bus. They can be initialised |
| 20 | +// when created, or initialised later on: |
| 21 | +// |
| 22 | +// from pyb import I2C |
| 23 | +// |
| 24 | +// i2c = I2C(1) # create on bus 1 |
| 25 | +// i2c = I2C(1, I2C.MASTER) # create and init as a master |
| 26 | +// i2c.deinit() # turn off the peripheral |
| 27 | +// i2c.init(I2C.MASTER, baudrate=20000) # init as a master |
| 28 | +// i2c.init(I2C.SLAVE, addr=0x42) # init as a slave with given address |
| 29 | +// |
| 30 | +// Printing the i2c object gives you information about its configuration. |
| 31 | +// |
| 32 | +// Basic methods for slave are send and recv: |
| 33 | +// |
| 34 | +// i2c.send('abc') # send 3 bytes |
| 35 | +// i2c.send(0x42) # send a single byte, given by the number |
| 36 | +// data = i2c.recv(3) # receive 3 bytes |
| 37 | +// |
| 38 | +// To receive inplace, first create a bytearray: |
| 39 | +// |
| 40 | +// data = bytearray(3) # create a buffer |
| 41 | +// i2c.recv(data) # receive 3 bytes, writing them into data |
| 42 | +// |
| 43 | +// You can specify a timeout (in ms): |
| 44 | +// |
| 45 | +// i2c.send(b'123', timeout=2000) # timout after 2 seconds |
| 46 | +// |
| 47 | +// A master must specify the recipient's address: |
| 48 | +// |
| 49 | +// i2c.init(I2C.MASTER) |
| 50 | +// i2c.send('123', 0x42) # send 3 bytes to slave with address 0x42 |
| 51 | +// i2c.send(b'456', addr=0x42) # keyword for address |
| 52 | +// |
| 53 | +// Master also has other methods: |
| 54 | +// |
| 55 | +// i2c.is_ready(0x42) # check if slave 0x42 is ready |
| 56 | +// i2c.scan() # scan for slaves on the bus, returning |
| 57 | +// # a list of valid addresses |
| 58 | +// i2c.mem_read(3, 0x42, 2) # read 3 bytes from memory of slave 0x42, |
| 59 | +// # starting at address 2 in the slave |
| 60 | +// i2c.mem_write('abc', 0x42, 2, timeout=1000) |
| 61 | + |
17 | 62 | #define PYB_I2C_MASTER (0) |
18 | 63 | #define PYB_I2C_SLAVE (1) |
19 | 64 |
|
|
0 commit comments