summaryrefslogtreecommitdiffstatshomepage
path: root/stmhal/i2c.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-04-29 22:55:34 +0100
committerDamien George <damien.p.george@gmail.com>2014-04-29 22:55:34 +0100
commit8d09640b22714fa9c66a5a7bbf9ab59a6a0c710d (patch)
treef8f196bf58661af9668b14f69a20c15765aa459b /stmhal/i2c.c
parent186e463a9ea5f1f1cabd928ba363fb3f0c0de4dc (diff)
downloadmicropython-8d09640b22714fa9c66a5a7bbf9ab59a6a0c710d.tar.gz
micropython-8d09640b22714fa9c66a5a7bbf9ab59a6a0c710d.zip
stmhal: Add documentation in comments, and script to generate HTML.
Decided to write own script to pull documentation from comments in C code. Style for writing auto generated documentation is: start line with /// and then use standard markdown to write the comment. Keywords recognised by the scraper begin with backslash. See code for examples. Running: python gendoc.py modpyb.c accel.c adc.c dac.c extint.c i2c.c led.c pin.c rng.c servo.c spi.c uart.c usrsw.c, will generate a HTML structure in gendoc-out/. gendoc.py is crude but functional. Needed something quick, and this was it.
Diffstat (limited to 'stmhal/i2c.c')
-rw-r--r--stmhal/i2c.c159
1 files changed, 113 insertions, 46 deletions
diff --git a/stmhal/i2c.c b/stmhal/i2c.c
index 2c804a23d0..ef9da496d0 100644
--- a/stmhal/i2c.c
+++ b/stmhal/i2c.c
@@ -14,50 +14,54 @@
#include "bufhelper.h"
#include "i2c.h"
-// Usage model:
-//
-// I2C objects are created attached to a specific bus. They can be initialised
-// when created, or initialised later on:
-//
-// from pyb import I2C
-//
-// i2c = I2C(1) # create on bus 1
-// i2c = I2C(1, I2C.MASTER) # create and init as a master
-// i2c.deinit() # turn off the peripheral
-// i2c.init(I2C.MASTER, baudrate=20000) # init as a master
-// i2c.init(I2C.SLAVE, addr=0x42) # init as a slave with given address
-//
-// Printing the i2c object gives you information about its configuration.
-//
-// Basic methods for slave are send and recv:
-//
-// i2c.send('abc') # send 3 bytes
-// i2c.send(0x42) # send a single byte, given by the number
-// data = i2c.recv(3) # receive 3 bytes
-//
-// To receive inplace, first create a bytearray:
-//
-// data = bytearray(3) # create a buffer
-// i2c.recv(data) # receive 3 bytes, writing them into data
-//
-// You can specify a timeout (in ms):
-//
-// i2c.send(b'123', timeout=2000) # timout after 2 seconds
-//
-// A master must specify the recipient's address:
-//
-// i2c.init(I2C.MASTER)
-// i2c.send('123', 0x42) # send 3 bytes to slave with address 0x42
-// i2c.send(b'456', addr=0x42) # keyword for address
-//
-// Master also has other methods:
-//
-// i2c.is_ready(0x42) # check if slave 0x42 is ready
-// i2c.scan() # scan for slaves on the bus, returning
-// # a list of valid addresses
-// i2c.mem_read(3, 0x42, 2) # read 3 bytes from memory of slave 0x42,
-// # starting at address 2 in the slave
-// i2c.mem_write('abc', 0x42, 2, timeout=1000)
+/// \moduleref pyb
+/// \class I2C - a two-wire serial protocol
+///
+/// I2C is a two-wire protocol for communicating between devices. At the physical
+/// level it consists of 2 wires: SCL and SDA, the clock and data lines respectively.
+///
+/// I2C objects are created attached to a specific bus. They can be initialised
+/// when created, or initialised later on:
+///
+/// from pyb import I2C
+///
+/// i2c = I2C(1) # create on bus 1
+/// i2c = I2C(1, I2C.MASTER) # create and init as a master
+/// i2c.init(I2C.MASTER, baudrate=20000) # init as a master
+/// i2c.init(I2C.SLAVE, addr=0x42) # init as a slave with given address
+/// i2c.deinit() # turn off the peripheral
+///
+/// Printing the i2c object gives you information about its configuration.
+///
+/// Basic methods for slave are send and recv:
+///
+/// i2c.send('abc') # send 3 bytes
+/// i2c.send(0x42) # send a single byte, given by the number
+/// data = i2c.recv(3) # receive 3 bytes
+///
+/// To receive inplace, first create a bytearray:
+///
+/// data = bytearray(3) # create a buffer
+/// i2c.recv(data) # receive 3 bytes, writing them into data
+///
+/// You can specify a timeout (in ms):
+///
+/// i2c.send(b'123', timeout=2000) # timout after 2 seconds
+///
+/// A master must specify the recipient's address:
+///
+/// i2c.init(I2C.MASTER)
+/// i2c.send('123', 0x42) # send 3 bytes to slave with address 0x42
+/// i2c.send(b'456', addr=0x42) # keyword for address
+///
+/// Master also has other methods:
+///
+/// i2c.is_ready(0x42) # check if slave 0x42 is ready
+/// i2c.scan() # scan for slaves on the bus, returning
+/// # a list of valid addresses
+/// i2c.mem_read(3, 0x42, 2) # read 3 bytes from memory of slave 0x42,
+/// # starting at address 2 in the slave
+/// i2c.mem_write('abc', 0x42, 2, timeout=1000)
#define PYB_I2C_MASTER (0)
#define PYB_I2C_SLAVE (1)
@@ -176,6 +180,14 @@ STATIC void pyb_i2c_print(void (*print)(void *env, const char *fmt, ...), void *
}
}
+/// \method init(mode, *, addr=0x12, baudrate=400000, gencall=False)
+///
+/// Initialise the I2C bus with the given parameters:
+///
+/// - `mode` must be either `I2C.MASTER` or `I2C.SLAVE`
+/// - `addr` is the 7-bit address (only sensible for a slave)
+/// - `baudrate` is the SCL clock rate (only sensible for a master)
+/// - `gencall` is whether to support general call mode
STATIC const mp_arg_t pyb_i2c_init_args[] = {
{ MP_QSTR_mode, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 0} },
{ MP_QSTR_addr, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0x12} },
@@ -213,6 +225,13 @@ STATIC mp_obj_t pyb_i2c_init_helper(const pyb_i2c_obj_t *self, uint n_args, cons
return mp_const_none;
}
+/// \classmethod \constructor(bus, ...)
+///
+/// Construct an I2C object on the given bus. `bus` can be 1 or 2.
+/// With no additional parameters, the I2C object is created but not
+/// initialised (it has the settings from the last initialisation of
+/// the bus, if any). If extra arguments are given, the bus is initialised.
+/// See `init` for parameters of initialisation.
STATIC mp_obj_t pyb_i2c_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
// check arguments
mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
@@ -243,6 +262,8 @@ STATIC mp_obj_t pyb_i2c_init(uint n_args, const mp_obj_t *args, mp_map_t *kw_arg
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_init_obj, 1, pyb_i2c_init);
+/// \method deinit()
+/// Turn off the I2C bus.
STATIC mp_obj_t pyb_i2c_deinit(mp_obj_t self_in) {
pyb_i2c_obj_t *self = self_in;
i2c_deinit(self->i2c);
@@ -250,7 +271,8 @@ STATIC mp_obj_t pyb_i2c_deinit(mp_obj_t self_in) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_i2c_deinit_obj, pyb_i2c_deinit);
-// Check if an I2C device responds to the given address.
+/// \method is_ready(addr)
+/// Check if an I2C device responds to the given address. Only valid when in master mode.
STATIC mp_obj_t pyb_i2c_is_ready(mp_obj_t self_in, mp_obj_t i2c_addr_o) {
pyb_i2c_obj_t *self = self_in;
@@ -271,7 +293,9 @@ STATIC mp_obj_t pyb_i2c_is_ready(mp_obj_t self_in, mp_obj_t i2c_addr_o) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_i2c_is_ready_obj, pyb_i2c_is_ready);
-// Scan all I2C addresses from 0x01 to 0x7f and return a list of those that respond.
+/// \method scan()
+/// Scan all I2C addresses from 0x01 to 0x7f and return a list of those that respond.
+/// Only valid when in master mode.
STATIC mp_obj_t pyb_i2c_scan(mp_obj_t self_in) {
pyb_i2c_obj_t *self = self_in;
@@ -295,6 +319,14 @@ STATIC mp_obj_t pyb_i2c_scan(mp_obj_t self_in) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_i2c_scan_obj, pyb_i2c_scan);
+/// \method send(send, addr=0x00, timeout=5000)
+/// Send data on the bus:
+///
+/// - `send` is the data to send (an integer to send, or a buffer object)
+/// - `addr` is the address to send to (only required in master mode)
+/// - `timeout` is the timeout in milliseconds to wait for the send
+///
+/// Return value: `None`.
STATIC const mp_arg_t pyb_i2c_send_args[] = {
{ MP_QSTR_send, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_addr, MP_ARG_INT, {.u_int = PYB_I2C_MASTER_ADDRESS} },
@@ -335,6 +367,17 @@ STATIC mp_obj_t pyb_i2c_send(uint n_args, const mp_obj_t *args, mp_map_t *kw_arg
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_send_obj, 1, pyb_i2c_send);
+/// \method recv(send, addr=0x00, timeout=5000)
+///
+/// Receive data on the bus:
+///
+/// - `recv` can be an integer, which is the number of bytes to receive,
+/// or a mutable buffer, which will be filled with received bytes
+/// - `addr` is the address to receive from (only required in master mode)
+/// - `timeout` is the timeout in milliseconds to wait for the receive
+///
+/// Return value: if `recv` is an integer then a new buffer of the bytes received,
+/// otherwise the same buffer that was passed in to `recv`.
STATIC const mp_arg_t pyb_i2c_recv_args[] = {
{ MP_QSTR_recv, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_addr, MP_ARG_INT, {.u_int = PYB_I2C_MASTER_ADDRESS} },
@@ -379,6 +422,17 @@ STATIC mp_obj_t pyb_i2c_recv(uint n_args, const mp_obj_t *args, mp_map_t *kw_arg
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_recv_obj, 1, pyb_i2c_recv);
+/// \method mem_read(data, addr, memaddr, timeout=5000)
+///
+/// Read from the memory of an I2C device:
+///
+/// - `data` can be an integer or a buffer to read into
+/// - `addr` is the I2C device address
+/// - `memaddr` is the memory location within the I2C device
+/// - `timeout` is the timeout in milliseconds to wait for the read
+///
+/// Returns the read data.
+/// This is only valid in master mode.
STATIC const mp_arg_t pyb_i2c_mem_read_args[] = {
{ MP_QSTR_data, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_addr, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 0} },
@@ -422,6 +476,17 @@ STATIC mp_obj_t pyb_i2c_mem_read(uint n_args, const mp_obj_t *args, mp_map_t *kw
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_mem_read_obj, 1, pyb_i2c_mem_read);
+/// \method mem_write(data, addr, memaddr, timeout=5000)
+///
+/// Write to the memory of an I2C device:
+///
+/// - `data` can be an integer or a buffer to write from
+/// - `addr` is the I2C device address
+/// - `memaddr` is the memory location within the I2C device
+/// - `timeout` is the timeout in milliseconds to wait for the write
+///
+/// Returns `None`.
+/// This is only valid in master mode.
STATIC mp_obj_t pyb_i2c_mem_write(uint n_args, const mp_obj_t *args, mp_map_t *kw_args) {
pyb_i2c_obj_t *self = args[0];
@@ -465,6 +530,8 @@ STATIC const mp_map_elem_t pyb_i2c_locals_dict_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_mem_write), (mp_obj_t)&pyb_i2c_mem_write_obj },
// class constants
+ /// \constant MASTER - for initialising the bus to master mode
+ /// \constant SLAVE - for initialising the bus to slave mode
{ MP_OBJ_NEW_QSTR(MP_QSTR_MASTER), MP_OBJ_NEW_SMALL_INT(PYB_I2C_MASTER) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_SLAVE), MP_OBJ_NEW_SMALL_INT(PYB_I2C_SLAVE) },
};