2015-02-10 12:11:27 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013, 2014 Damien P. George
|
|
|
|
* Copyright (c) 2015 Daniel Campora
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "py/mpstate.h"
|
|
|
|
#include MICROPY_HAL_H
|
2015-02-21 18:58:43 +00:00
|
|
|
#include "py/runtime.h"
|
2015-02-10 12:11:27 +00:00
|
|
|
#include "bufhelper.h"
|
|
|
|
#include "inc/hw_types.h"
|
|
|
|
#include "inc/hw_i2c.h"
|
|
|
|
#include "inc/hw_ints.h"
|
|
|
|
#include "inc/hw_memmap.h"
|
|
|
|
#include "rom_map.h"
|
|
|
|
#include "pin.h"
|
|
|
|
#include "prcm.h"
|
|
|
|
#include "i2c.h"
|
|
|
|
#include "pybi2c.h"
|
|
|
|
#include "mpexception.h"
|
2015-03-11 20:49:50 +00:00
|
|
|
#include "pybsleep.h"
|
2015-03-18 12:16:09 +00:00
|
|
|
#include "utils.h"
|
2015-02-10 12:11:27 +00:00
|
|
|
|
|
|
|
/// \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() # create
|
2015-03-25 15:56:14 +00:00
|
|
|
/// i2c = I2C(50000) # create and init with a 50KHz baudrate
|
|
|
|
/// i2c.init(100000) # init with a 100KHz baudrate
|
2015-02-10 12:11:27 +00:00
|
|
|
/// 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
|
|
|
|
///
|
|
|
|
/// A master must specify the recipient's address:
|
|
|
|
///
|
2015-03-25 15:56:14 +00:00
|
|
|
/// i2c.init(100000)
|
2015-02-10 12:11:27 +00:00
|
|
|
/// 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:
|
|
|
|
///
|
2015-03-25 15:56:14 +00:00
|
|
|
/// 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) # write 3 bytes to memory of slave 0x42,
|
|
|
|
/// # starting at address 2 in the slave
|
2015-02-10 12:11:27 +00:00
|
|
|
|
2015-02-20 15:31:30 +00:00
|
|
|
typedef struct _pyb_i2c_obj_t {
|
|
|
|
mp_obj_base_t base;
|
2015-03-18 14:58:48 +00:00
|
|
|
uint baudrate;
|
2015-02-20 15:31:30 +00:00
|
|
|
} pyb_i2c_obj_t;
|
|
|
|
|
2015-02-10 12:11:27 +00:00
|
|
|
/******************************************************************************
|
|
|
|
DEFINE CONSTANTS
|
|
|
|
******************************************************************************/
|
2015-03-19 14:17:39 +00:00
|
|
|
#define PYBI2C_MIN_BAUD_RATE_HZ (50000)
|
2015-03-18 14:58:48 +00:00
|
|
|
#define PYBI2C_MAX_BAUD_RATE_HZ (400000)
|
|
|
|
|
2015-03-25 15:56:14 +00:00
|
|
|
#define PYBI2C_TRANSC_TIMEOUT_MS (10)
|
2015-03-18 12:16:09 +00:00
|
|
|
#define PYBI2C_TRANSAC_WAIT_DELAY_US (10)
|
2015-02-10 12:11:27 +00:00
|
|
|
|
2015-03-18 12:16:09 +00:00
|
|
|
#define PYBI2C_TIMEOUT_TO_COUNT(to_us, baud) (((baud) * to_us) / 16000000)
|
2015-02-10 12:11:27 +00:00
|
|
|
|
|
|
|
#define RET_IF_ERR(Func) { \
|
|
|
|
if (!Func) { \
|
|
|
|
return false; \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2015-03-17 10:05:59 +00:00
|
|
|
/******************************************************************************
|
|
|
|
DECLARE PRIVATE DATA
|
|
|
|
******************************************************************************/
|
2015-03-19 14:17:39 +00:00
|
|
|
STATIC pyb_i2c_obj_t pyb_i2c_obj = {.baudrate = 0};
|
2015-03-17 10:05:59 +00:00
|
|
|
|
2015-02-10 12:11:27 +00:00
|
|
|
/******************************************************************************
|
2015-03-11 20:49:50 +00:00
|
|
|
DEFINE PRIVATE FUNCTIONS
|
2015-02-10 12:11:27 +00:00
|
|
|
******************************************************************************/
|
|
|
|
// only master mode is available for the moment
|
2015-03-11 20:49:50 +00:00
|
|
|
STATIC void i2c_init (pyb_i2c_obj_t *self) {
|
2015-02-10 12:11:27 +00:00
|
|
|
// Enable the I2C Peripheral
|
|
|
|
MAP_PRCMPeripheralClkEnable(PRCM_I2CA0, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK);
|
|
|
|
MAP_PRCMPeripheralReset(PRCM_I2CA0);
|
|
|
|
// Configure I2C module with the specified baudrate
|
2015-03-11 20:49:50 +00:00
|
|
|
MAP_I2CMasterInitExpClk(I2CA0_BASE, self->baudrate);
|
2015-02-10 12:11:27 +00:00
|
|
|
}
|
|
|
|
|
2015-03-25 15:56:14 +00:00
|
|
|
STATIC bool pyb_i2c_transaction(uint cmd) {
|
|
|
|
// Convert the timeout to microseconds
|
|
|
|
int32_t timeout = PYBI2C_TRANSC_TIMEOUT_MS * 1000;
|
2015-03-18 12:16:09 +00:00
|
|
|
// Sanity check, t_timeout must be between 1 and 255
|
|
|
|
uint t_timeout = MIN(PYBI2C_TIMEOUT_TO_COUNT(timeout, pyb_i2c_obj.baudrate), 255);
|
2015-02-10 12:11:27 +00:00
|
|
|
// Clear all interrupts
|
|
|
|
MAP_I2CMasterIntClearEx(I2CA0_BASE, MAP_I2CMasterIntStatusEx(I2CA0_BASE, false));
|
2015-03-18 12:16:09 +00:00
|
|
|
// Set the time-out in terms of clock cycles. Not to be used with breakpoints.
|
|
|
|
MAP_I2CMasterTimeoutSet(I2CA0_BASE, t_timeout);
|
2015-02-10 12:11:27 +00:00
|
|
|
// Initiate the transfer.
|
|
|
|
MAP_I2CMasterControl(I2CA0_BASE, cmd);
|
|
|
|
// Wait until the current byte has been transferred.
|
|
|
|
// Poll on the raw interrupt status.
|
2015-03-18 12:16:09 +00:00
|
|
|
while ((MAP_I2CMasterIntStatusEx(I2CA0_BASE, false) & (I2C_MASTER_INT_DATA | I2C_MASTER_INT_TIMEOUT)) == 0) {
|
|
|
|
// wait for a few microseconds
|
|
|
|
UtilsDelay(UTILS_DELAY_US_TO_COUNT(PYBI2C_TRANSAC_WAIT_DELAY_US));
|
|
|
|
timeout -= PYBI2C_TRANSAC_WAIT_DELAY_US;
|
|
|
|
if (timeout < 0) {
|
2015-03-25 15:56:14 +00:00
|
|
|
// the peripheral is not responding, so stop
|
2015-03-18 12:16:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2015-02-10 12:11:27 +00:00
|
|
|
|
2015-03-25 15:56:14 +00:00
|
|
|
// Check for any errors in the transfer
|
2015-02-10 12:11:27 +00:00
|
|
|
if (MAP_I2CMasterErr(I2CA0_BASE) != I2C_MASTER_ERR_NONE) {
|
|
|
|
switch(cmd) {
|
|
|
|
case I2C_MASTER_CMD_BURST_SEND_START:
|
|
|
|
case I2C_MASTER_CMD_BURST_SEND_CONT:
|
|
|
|
case I2C_MASTER_CMD_BURST_SEND_STOP:
|
|
|
|
MAP_I2CMasterControl(I2CA0_BASE, I2C_MASTER_CMD_BURST_SEND_ERROR_STOP);
|
|
|
|
break;
|
|
|
|
case I2C_MASTER_CMD_BURST_RECEIVE_START:
|
|
|
|
case I2C_MASTER_CMD_BURST_RECEIVE_CONT:
|
|
|
|
case I2C_MASTER_CMD_BURST_RECEIVE_FINISH:
|
|
|
|
MAP_I2CMasterControl(I2CA0_BASE, I2C_MASTER_CMD_BURST_RECEIVE_ERROR_STOP);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-25 15:56:14 +00:00
|
|
|
STATIC bool pyb_i2c_write(byte devAddr, byte *data, uint len, bool stop) {
|
2015-02-10 12:11:27 +00:00
|
|
|
// Set I2C codec slave address
|
|
|
|
MAP_I2CMasterSlaveAddrSet(I2CA0_BASE, devAddr, false);
|
|
|
|
// Write the first byte to the controller.
|
|
|
|
MAP_I2CMasterDataPut(I2CA0_BASE, *data++);
|
|
|
|
// Initiate the transfer.
|
2015-03-25 15:56:14 +00:00
|
|
|
RET_IF_ERR(pyb_i2c_transaction(I2C_MASTER_CMD_BURST_SEND_START));
|
2015-02-10 12:11:27 +00:00
|
|
|
|
|
|
|
// Loop until the completion of transfer or error
|
|
|
|
while (--len) {
|
|
|
|
// Write the next byte of data
|
|
|
|
MAP_I2CMasterDataPut(I2CA0_BASE, *data++);
|
|
|
|
// Transact over I2C to send the byte
|
2015-03-25 15:56:14 +00:00
|
|
|
RET_IF_ERR(pyb_i2c_transaction(I2C_MASTER_CMD_BURST_SEND_CONT));
|
2015-02-10 12:11:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If a stop bit is to be sent, send it.
|
|
|
|
if (stop) {
|
2015-03-25 15:56:14 +00:00
|
|
|
RET_IF_ERR(pyb_i2c_transaction(I2C_MASTER_CMD_BURST_SEND_STOP));
|
2015-02-10 12:11:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-25 15:56:14 +00:00
|
|
|
STATIC bool pyb_i2c_read(byte devAddr, byte *data, uint len) {
|
2015-02-10 12:11:27 +00:00
|
|
|
uint cmd;
|
|
|
|
|
|
|
|
// Set I2C codec slave address
|
|
|
|
MAP_I2CMasterSlaveAddrSet(I2CA0_BASE, devAddr, true);
|
|
|
|
|
|
|
|
// Check if its a single receive or burst receive
|
|
|
|
if (len > 1) {
|
|
|
|
// Initiate a burst receive sequence
|
|
|
|
cmd = I2C_MASTER_CMD_BURST_RECEIVE_START;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Configure for a single receive
|
|
|
|
cmd = I2C_MASTER_CMD_SINGLE_RECEIVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initiate the transfer.
|
2015-03-25 15:56:14 +00:00
|
|
|
RET_IF_ERR(pyb_i2c_transaction(cmd));
|
2015-02-10 12:11:27 +00:00
|
|
|
// Decrement the count
|
|
|
|
len--;
|
|
|
|
// Loop until the completion of reception or error
|
|
|
|
while (len) {
|
|
|
|
// Receive the byte over I2C
|
|
|
|
*data++ = MAP_I2CMasterDataGet(I2CA0_BASE);
|
|
|
|
if (--len) {
|
|
|
|
// Continue with reception
|
2015-03-25 15:56:14 +00:00
|
|
|
RET_IF_ERR(pyb_i2c_transaction(I2C_MASTER_CMD_BURST_RECEIVE_CONT));
|
2015-02-10 12:11:27 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Complete the last reception
|
2015-03-25 15:56:14 +00:00
|
|
|
RET_IF_ERR(pyb_i2c_transaction(I2C_MASTER_CMD_BURST_RECEIVE_FINISH));
|
2015-02-10 12:11:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Receive the last byte over I2C
|
|
|
|
*data = MAP_I2CMasterDataGet(I2CA0_BASE);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-25 15:56:14 +00:00
|
|
|
STATIC bool pyb_i2c_scan_device(byte devAddr) {
|
2015-02-10 12:11:27 +00:00
|
|
|
// Set I2C codec slave address
|
|
|
|
MAP_I2CMasterSlaveAddrSet(I2CA0_BASE, devAddr, true);
|
|
|
|
// Initiate the transfer.
|
2015-03-25 15:56:14 +00:00
|
|
|
RET_IF_ERR(pyb_i2c_transaction(I2C_MASTER_CMD_SINGLE_RECEIVE));
|
2015-02-10 12:11:27 +00:00
|
|
|
// Since this is a hack, send the stop bit anyway
|
|
|
|
MAP_I2CMasterControl(I2CA0_BASE, I2C_MASTER_CMD_BURST_SEND_ERROR_STOP);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-18 12:16:09 +00:00
|
|
|
/******************************************************************************/
|
|
|
|
/* Micro Python bindings */
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-03-25 15:56:14 +00:00
|
|
|
/// \method init(100000)
|
2015-02-10 12:11:27 +00:00
|
|
|
///
|
2015-03-25 15:56:14 +00:00
|
|
|
/// Initialise the I2C bus as a master with the given baudrate.
|
2015-02-10 12:11:27 +00:00
|
|
|
///
|
2015-03-25 15:56:14 +00:00
|
|
|
STATIC mp_obj_t pyb_i2c_init_helper(pyb_i2c_obj_t *self_in, mp_obj_t baudrate) {
|
2015-02-10 12:11:27 +00:00
|
|
|
pyb_i2c_obj_t *self = self_in;
|
|
|
|
|
2015-03-19 14:17:39 +00:00
|
|
|
// make sure the baudrate is between the valid range
|
2015-03-25 15:56:14 +00:00
|
|
|
self->baudrate = MIN(MAX(mp_obj_get_int(baudrate), PYBI2C_MIN_BAUD_RATE_HZ), PYBI2C_MAX_BAUD_RATE_HZ);
|
2015-02-10 12:11:27 +00:00
|
|
|
|
|
|
|
// init the I2C bus
|
2015-03-11 20:49:50 +00:00
|
|
|
i2c_init(self);
|
|
|
|
|
|
|
|
// register it with the sleep module
|
|
|
|
pybsleep_add ((const mp_obj_t)self, (WakeUpCB_t)i2c_init);
|
2015-02-10 12:11:27 +00:00
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \classmethod \constructor(bus, ...)
|
|
|
|
///
|
2015-02-20 15:31:30 +00:00
|
|
|
/// Construct an I2C object on the given bus. `bus` can only be 0.
|
2015-02-10 12:11:27 +00:00
|
|
|
/// 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, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
|
|
|
|
// check arguments
|
|
|
|
mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
|
|
|
|
|
2015-03-17 10:05:59 +00:00
|
|
|
// setup the object
|
|
|
|
pyb_i2c_obj_t *self = &pyb_i2c_obj;
|
2015-02-20 15:31:30 +00:00
|
|
|
self->base.type = &pyb_i2c_type;
|
2015-02-10 12:11:27 +00:00
|
|
|
|
2015-03-25 15:56:14 +00:00
|
|
|
if (n_args > 0) {
|
2015-02-10 12:11:27 +00:00
|
|
|
// start the peripheral
|
2015-03-25 15:56:14 +00:00
|
|
|
pyb_i2c_init_helper(self, *args);
|
2015-02-10 12:11:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (mp_obj_t)self;
|
|
|
|
}
|
|
|
|
|
2015-04-09 23:56:15 +01:00
|
|
|
STATIC void pyb_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
2015-03-18 14:58:48 +00:00
|
|
|
pyb_i2c_obj_t *self = self_in;
|
2015-03-19 14:17:39 +00:00
|
|
|
if (self->baudrate > 0) {
|
2015-04-09 23:56:15 +01:00
|
|
|
mp_printf(print, "<I2C0, I2C.MASTER, baudrate=%u>)", self->baudrate);
|
2015-03-19 14:17:39 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-04-09 23:56:15 +01:00
|
|
|
mp_print_str(print, "<I2C0>");
|
2015-03-19 14:17:39 +00:00
|
|
|
}
|
2015-03-18 14:58:48 +00:00
|
|
|
}
|
|
|
|
|
2015-03-25 15:56:14 +00:00
|
|
|
STATIC mp_obj_t pyb_i2c_init(mp_obj_t self_in, mp_obj_t baudrate) {
|
|
|
|
return pyb_i2c_init_helper(self_in, baudrate);
|
2015-02-10 12:11:27 +00:00
|
|
|
}
|
2015-03-25 15:56:14 +00:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_i2c_init_obj, pyb_i2c_init);
|
2015-02-10 12:11:27 +00:00
|
|
|
|
|
|
|
/// \method deinit()
|
|
|
|
/// Turn off the I2C bus.
|
|
|
|
STATIC mp_obj_t pyb_i2c_deinit(mp_obj_t self_in) {
|
2015-03-26 09:25:28 +00:00
|
|
|
// disable the peripheral
|
|
|
|
MAP_I2CMasterDisable(I2CA0_BASE);
|
|
|
|
MAP_PRCMPeripheralClkDisable(PRCM_I2CA0, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK);
|
|
|
|
// invalidate the baudrate
|
|
|
|
pyb_i2c_obj.baudrate = 0;
|
2015-03-16 12:18:29 +00:00
|
|
|
// unregister it with the sleep module
|
|
|
|
pybsleep_remove ((const mp_obj_t)self_in);
|
2015-02-10 12:11:27 +00:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_i2c_deinit_obj, pyb_i2c_deinit);
|
|
|
|
|
|
|
|
/// \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) {
|
|
|
|
mp_uint_t i2c_addr = mp_obj_get_int(i2c_addr_o);
|
2015-03-25 15:56:14 +00:00
|
|
|
for (int i = 0; i < 7; i++) {
|
|
|
|
if (pyb_i2c_scan_device(i2c_addr)) {
|
2015-02-10 12:11:27 +00:00
|
|
|
return mp_const_true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mp_const_false;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_i2c_is_ready_obj, pyb_i2c_is_ready);
|
|
|
|
|
|
|
|
/// \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) {
|
|
|
|
mp_obj_t list = mp_obj_new_list(0, NULL);
|
|
|
|
for (uint addr = 1; addr <= 127; addr++) {
|
2015-03-25 15:56:14 +00:00
|
|
|
for (int i = 0; i < 7; i++) {
|
|
|
|
if (pyb_i2c_scan_device(addr)) {
|
2015-02-10 12:11:27 +00:00
|
|
|
mp_obj_list_append(list, mp_obj_new_int(addr));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_i2c_scan_obj, pyb_i2c_scan);
|
|
|
|
|
|
|
|
/// \method send(send, addr=0x00)
|
|
|
|
/// 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)
|
|
|
|
/// 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 = 0} },
|
|
|
|
};
|
|
|
|
#define PYB_I2C_SEND_NUM_ARGS MP_ARRAY_SIZE(pyb_i2c_send_args)
|
|
|
|
|
|
|
|
STATIC mp_obj_t pyb_i2c_send(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
|
|
|
|
// parse args
|
|
|
|
mp_arg_val_t vals[PYB_I2C_SEND_NUM_ARGS];
|
|
|
|
mp_arg_parse_all(n_args - 1, args + 1, kw_args, PYB_I2C_SEND_NUM_ARGS, pyb_i2c_send_args, vals);
|
|
|
|
|
|
|
|
// get the buffer to send from
|
|
|
|
mp_buffer_info_t bufinfo;
|
|
|
|
uint8_t data[1];
|
|
|
|
pyb_buf_get_for_send(vals[0].u_obj, &bufinfo, data);
|
|
|
|
|
|
|
|
// send the data
|
2015-03-25 15:56:14 +00:00
|
|
|
if (!pyb_i2c_write(vals[1].u_int, bufinfo.buf, bufinfo.len, true)) {
|
2015-03-18 14:58:48 +00:00
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed));
|
2015-02-10 12:11:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_send_obj, 1, pyb_i2c_send);
|
|
|
|
|
2015-03-25 15:56:14 +00:00
|
|
|
/// \method recv(recv, addr=0x00)
|
2015-02-10 12:11:27 +00:00
|
|
|
///
|
|
|
|
/// 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)
|
|
|
|
///
|
|
|
|
/// 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 = 0} },
|
|
|
|
};
|
|
|
|
#define PYB_I2C_RECV_NUM_ARGS MP_ARRAY_SIZE(pyb_i2c_recv_args)
|
|
|
|
|
|
|
|
STATIC mp_obj_t pyb_i2c_recv(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
|
|
|
|
// parse args
|
|
|
|
mp_arg_val_t vals[PYB_I2C_RECV_NUM_ARGS];
|
|
|
|
mp_arg_parse_all(n_args - 1, args + 1, kw_args, PYB_I2C_RECV_NUM_ARGS, pyb_i2c_recv_args, vals);
|
|
|
|
|
|
|
|
// get the buffer to receive into
|
|
|
|
vstr_t vstr;
|
|
|
|
mp_obj_t o_ret = pyb_buf_get_for_recv(vals[0].u_obj, &vstr);
|
|
|
|
|
|
|
|
// receive the data
|
2015-03-25 15:56:14 +00:00
|
|
|
if (!pyb_i2c_read(vals[1].u_int, (byte *)vstr.buf, vstr.len)) {
|
2015-03-18 14:58:48 +00:00
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed));
|
2015-02-10 12:11:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// return the received data
|
|
|
|
if (o_ret != MP_OBJ_NULL) {
|
|
|
|
return o_ret;
|
2015-03-18 14:58:48 +00:00
|
|
|
}
|
|
|
|
else {
|
2015-02-10 12:11:27 +00:00
|
|
|
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_recv_obj, 1, pyb_i2c_recv);
|
|
|
|
|
2015-03-25 15:56:14 +00:00
|
|
|
/// \method mem_read(data, addr, memaddr, addr_size=8)
|
2015-02-10 12:11:27 +00:00
|
|
|
///
|
|
|
|
/// 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
|
2015-03-25 15:56:14 +00:00
|
|
|
/// - `addr_size` selects the width of memaddr: 8 or 16 bits
|
2015-02-10 12:11:27 +00:00
|
|
|
///
|
|
|
|
/// 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} },
|
|
|
|
{ MP_QSTR_memaddr, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 0} },
|
|
|
|
{ MP_QSTR_addr_size, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
|
|
|
|
};
|
|
|
|
#define PYB_I2C_MEM_READ_NUM_ARGS MP_ARRAY_SIZE(pyb_i2c_mem_read_args)
|
|
|
|
|
|
|
|
STATIC mp_obj_t pyb_i2c_mem_read(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
|
|
|
|
// parse args
|
|
|
|
mp_arg_val_t vals[PYB_I2C_MEM_READ_NUM_ARGS];
|
|
|
|
mp_arg_parse_all(n_args - 1, args + 1, kw_args, PYB_I2C_MEM_READ_NUM_ARGS, pyb_i2c_mem_read_args, vals);
|
|
|
|
|
|
|
|
// get the buffer to read into
|
|
|
|
vstr_t vstr;
|
|
|
|
mp_obj_t o_ret = pyb_buf_get_for_recv(vals[0].u_obj, &vstr);
|
|
|
|
|
|
|
|
// get the addresses
|
|
|
|
mp_uint_t i2c_addr = vals[1].u_int;
|
|
|
|
mp_uint_t mem_addr = vals[2].u_int;
|
2015-03-25 15:56:14 +00:00
|
|
|
// determine the width of mem_addr (1 or 2 bytes)
|
|
|
|
mp_uint_t mem_addr_size = vals[3].u_int >> 3;
|
2015-02-10 12:11:27 +00:00
|
|
|
|
|
|
|
// Write the register address to be read from.
|
2015-03-25 15:56:14 +00:00
|
|
|
if (pyb_i2c_write (i2c_addr, (byte *)&mem_addr, mem_addr_size, false)) {
|
2015-02-10 12:11:27 +00:00
|
|
|
// Read the specified length of data
|
2015-03-25 15:56:14 +00:00
|
|
|
if (pyb_i2c_read (i2c_addr, (byte *)vstr.buf, vstr.len)) {
|
2015-02-10 12:11:27 +00:00
|
|
|
// return the read data
|
|
|
|
if (o_ret != MP_OBJ_NULL) {
|
|
|
|
return o_ret;
|
|
|
|
} else {
|
|
|
|
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed));
|
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_mem_read_obj, 1, pyb_i2c_mem_read);
|
|
|
|
|
2015-03-25 15:56:14 +00:00
|
|
|
/// \method mem_write(data, addr, memaddr, addr_size=8)
|
2015-02-10 12:11:27 +00:00
|
|
|
///
|
|
|
|
/// 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
|
2015-03-25 15:56:14 +00:00
|
|
|
/// - `addr_size` selects the width of memaddr: 8 or 16 bits
|
2015-02-10 12:11:27 +00:00
|
|
|
///
|
|
|
|
/// Returns `None`.
|
|
|
|
/// This is only valid in master mode.
|
|
|
|
STATIC mp_obj_t pyb_i2c_mem_write(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
|
|
|
|
// parse args (same as mem_read)
|
|
|
|
mp_arg_val_t vals[PYB_I2C_MEM_READ_NUM_ARGS];
|
|
|
|
mp_arg_parse_all(n_args - 1, args + 1, kw_args, PYB_I2C_MEM_READ_NUM_ARGS, pyb_i2c_mem_read_args, vals);
|
|
|
|
|
|
|
|
// get the buffer to write from
|
|
|
|
mp_buffer_info_t bufinfo;
|
|
|
|
uint8_t data[1];
|
|
|
|
pyb_buf_get_for_send(vals[0].u_obj, &bufinfo, data);
|
|
|
|
|
|
|
|
// get the addresses
|
|
|
|
mp_uint_t i2c_addr = vals[1].u_int;
|
|
|
|
mp_uint_t mem_addr = vals[2].u_int;
|
2015-03-25 15:56:14 +00:00
|
|
|
// determine the width of mem_addr (1 or 2 bytes)
|
|
|
|
mp_uint_t mem_addr_size = vals[3].u_int >> 3;
|
2015-02-10 12:11:27 +00:00
|
|
|
|
2015-03-19 14:17:39 +00:00
|
|
|
// Write the register address to write to.
|
2015-03-25 15:56:14 +00:00
|
|
|
if (pyb_i2c_write (i2c_addr, (byte *)&mem_addr, mem_addr_size, false)) {
|
2015-02-10 12:11:27 +00:00
|
|
|
// Write the specified length of data
|
2015-03-25 15:56:14 +00:00
|
|
|
if (pyb_i2c_write (i2c_addr, bufinfo.buf, bufinfo.len, true)) {
|
2015-02-10 12:11:27 +00:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed));
|
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_mem_write_obj, 1, pyb_i2c_mem_write);
|
|
|
|
|
|
|
|
STATIC const mp_map_elem_t pyb_i2c_locals_dict_table[] = {
|
|
|
|
// instance methods
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_init), (mp_obj_t)&pyb_i2c_init_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_deinit), (mp_obj_t)&pyb_i2c_deinit_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_is_ready), (mp_obj_t)&pyb_i2c_is_ready_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_scan), (mp_obj_t)&pyb_i2c_scan_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_send), (mp_obj_t)&pyb_i2c_send_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_recv), (mp_obj_t)&pyb_i2c_recv_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_mem_read), (mp_obj_t)&pyb_i2c_mem_read_obj },
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_mem_write), (mp_obj_t)&pyb_i2c_mem_write_obj },
|
|
|
|
};
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(pyb_i2c_locals_dict, pyb_i2c_locals_dict_table);
|
|
|
|
|
|
|
|
const mp_obj_type_t pyb_i2c_type = {
|
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_I2C,
|
|
|
|
.print = pyb_i2c_print,
|
|
|
|
.make_new = pyb_i2c_make_new,
|
|
|
|
.locals_dict = (mp_obj_t)&pyb_i2c_locals_dict,
|
|
|
|
};
|