2015-02-10 12:11:27 +00:00
|
|
|
/*
|
2017-06-30 08:22:17 +01:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2015-02-10 12:11:27 +00:00
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
|
2015-02-21 18:58:43 +00:00
|
|
|
#include "py/runtime.h"
|
2017-02-22 01:36:23 +00:00
|
|
|
#include "py/mperrno.h"
|
2015-10-30 23:03:58 +00:00
|
|
|
#include "py/mphal.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-09-08 14:08:33 +01:00
|
|
|
#include "pybpin.h"
|
|
|
|
#include "pins.h"
|
2015-02-10 12:11:27 +00:00
|
|
|
|
|
|
|
/// \moduleref pyb
|
|
|
|
/// \class I2C - a two-wire serial protocol
|
|
|
|
|
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)
|
|
|
|
|
2016-02-12 19:17:15 +00:00
|
|
|
#define PYBI2C_TRANSC_TIMEOUT_MS (20)
|
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-09-08 14:08:33 +01:00
|
|
|
|
2016-02-12 19:17:15 +00:00
|
|
|
/******************************************************************************
|
|
|
|
DECLARE PRIVATE FUNCTIONS
|
|
|
|
******************************************************************************/
|
|
|
|
STATIC bool pyb_i2c_write(byte addr, byte *data, uint len, bool stop);
|
|
|
|
|
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) {
|
|
|
|
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;
|
|
|
|
}
|
2016-02-12 19:17:15 +00:00
|
|
|
// wait for a few microseconds
|
|
|
|
UtilsDelay(UTILS_DELAY_US_TO_COUNT(PYBI2C_TRANSAC_WAIT_DELAY_US));
|
|
|
|
timeout -= PYBI2C_TRANSAC_WAIT_DELAY_US;
|
2015-03-18 12:16:09 +00:00
|
|
|
}
|
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-09-09 21:42:42 +01:00
|
|
|
STATIC void pyb_i2c_check_init(pyb_i2c_obj_t *self) {
|
|
|
|
// not initialized
|
|
|
|
if (!self->baudrate) {
|
2017-02-22 01:36:23 +00:00
|
|
|
mp_raise_OSError(MP_EPERM);
|
2015-09-09 21:42:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC bool pyb_i2c_scan_device(byte devAddr) {
|
2016-02-12 19:17:15 +00:00
|
|
|
bool ret = false;
|
|
|
|
// Set the I2C slave address
|
2015-09-09 21:42:42 +01:00
|
|
|
MAP_I2CMasterSlaveAddrSet(I2CA0_BASE, devAddr, true);
|
|
|
|
// Initiate the transfer.
|
2016-02-12 19:17:15 +00:00
|
|
|
if (pyb_i2c_transaction(I2C_MASTER_CMD_SINGLE_RECEIVE)) {
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
// Send the stop bit to cancel the read transaction
|
2015-09-09 21:42:42 +01:00
|
|
|
MAP_I2CMasterControl(I2CA0_BASE, I2C_MASTER_CMD_BURST_SEND_ERROR_STOP);
|
2016-02-12 19:17:15 +00:00
|
|
|
if (!ret) {
|
|
|
|
uint8_t data = 0;
|
|
|
|
if (pyb_i2c_write(devAddr, &data, sizeof(data), true)) {
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
2015-09-09 21:42:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
STATIC bool pyb_i2c_mem_addr_write (byte addr, byte *mem_addr, uint mem_addr_len) {
|
|
|
|
// Set I2C codec slave address
|
|
|
|
MAP_I2CMasterSlaveAddrSet(I2CA0_BASE, addr, false);
|
|
|
|
// Write the first byte to the controller.
|
|
|
|
MAP_I2CMasterDataPut(I2CA0_BASE, *mem_addr++);
|
|
|
|
// Initiate the transfer.
|
|
|
|
RET_IF_ERR(pyb_i2c_transaction(I2C_MASTER_CMD_BURST_SEND_START));
|
|
|
|
|
|
|
|
// Loop until the completion of transfer or error
|
|
|
|
while (--mem_addr_len) {
|
|
|
|
// Write the next byte of data
|
|
|
|
MAP_I2CMasterDataPut(I2CA0_BASE, *mem_addr++);
|
|
|
|
// Transact over I2C to send the next byte
|
|
|
|
RET_IF_ERR(pyb_i2c_transaction(I2C_MASTER_CMD_BURST_SEND_CONT));
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC bool pyb_i2c_mem_write (byte addr, byte *mem_addr, uint mem_addr_len, byte *data, uint data_len) {
|
|
|
|
if (pyb_i2c_mem_addr_write (addr, mem_addr, mem_addr_len)) {
|
|
|
|
// Loop until the completion of transfer or error
|
|
|
|
while (data_len--) {
|
|
|
|
// Write the next byte of data
|
|
|
|
MAP_I2CMasterDataPut(I2CA0_BASE, *data++);
|
|
|
|
// Transact over I2C to send the byte
|
|
|
|
RET_IF_ERR(pyb_i2c_transaction(I2C_MASTER_CMD_BURST_SEND_CONT));
|
|
|
|
}
|
|
|
|
// send the stop bit
|
|
|
|
RET_IF_ERR(pyb_i2c_transaction(I2C_MASTER_CMD_BURST_SEND_STOP));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-09-08 14:08:33 +01:00
|
|
|
STATIC bool pyb_i2c_write(byte addr, byte *data, uint len, bool stop) {
|
2015-02-10 12:11:27 +00:00
|
|
|
// Set I2C codec slave address
|
2015-09-08 14:08:33 +01:00
|
|
|
MAP_I2CMasterSlaveAddrSet(I2CA0_BASE, addr, false);
|
2015-02-10 12:11:27 +00:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2015-09-09 21:42:42 +01:00
|
|
|
// If a stop bit is to be sent, do it.
|
2015-02-10 12:11:27 +00:00
|
|
|
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-09-08 14:08:33 +01:00
|
|
|
STATIC bool pyb_i2c_read(byte addr, byte *data, uint len) {
|
2015-09-09 21:42:42 +01:00
|
|
|
// Initiate a burst or single receive sequence
|
|
|
|
uint cmd = --len > 0 ? I2C_MASTER_CMD_BURST_RECEIVE_START : I2C_MASTER_CMD_SINGLE_RECEIVE;
|
2015-02-10 12:11:27 +00:00
|
|
|
// Set I2C codec slave address
|
2015-09-08 14:08:33 +01:00
|
|
|
MAP_I2CMasterSlaveAddrSet(I2CA0_BASE, addr, true);
|
2015-02-10 12:11:27 +00:00
|
|
|
// 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
|
|
|
// 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-09-09 21:42:42 +01:00
|
|
|
} else {
|
2015-02-10 12:11:27 +00:00
|
|
|
// 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-09-08 14:08:33 +01:00
|
|
|
STATIC void pyb_i2c_read_into (mp_arg_val_t *args, vstr_t *vstr) {
|
2015-09-09 21:42:42 +01:00
|
|
|
pyb_i2c_check_init(&pyb_i2c_obj);
|
2015-09-08 14:08:33 +01:00
|
|
|
// get the buffer to receive into
|
|
|
|
pyb_buf_get_for_recv(args[1].u_obj, vstr);
|
|
|
|
|
|
|
|
// receive the data
|
|
|
|
if (!pyb_i2c_read(args[0].u_int, (byte *)vstr->buf, vstr->len)) {
|
2017-02-22 01:36:23 +00:00
|
|
|
mp_raise_OSError(MP_EIO);
|
2015-09-08 14:08:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void pyb_i2c_readmem_into (mp_arg_val_t *args, vstr_t *vstr) {
|
2015-09-09 21:42:42 +01:00
|
|
|
pyb_i2c_check_init(&pyb_i2c_obj);
|
2015-09-08 14:08:33 +01:00
|
|
|
// get the buffer to receive into
|
|
|
|
pyb_buf_get_for_recv(args[2].u_obj, vstr);
|
|
|
|
|
|
|
|
// get the addresses
|
|
|
|
mp_uint_t i2c_addr = args[0].u_int;
|
|
|
|
mp_uint_t mem_addr = args[1].u_int;
|
|
|
|
// determine the width of mem_addr (1 or 2 bytes)
|
|
|
|
mp_uint_t mem_addr_size = args[3].u_int >> 3;
|
|
|
|
|
2015-09-09 21:42:42 +01:00
|
|
|
// write the register address to be read from
|
|
|
|
if (pyb_i2c_mem_addr_write (i2c_addr, (byte *)&mem_addr, mem_addr_size)) {
|
2015-09-08 14:08:33 +01:00
|
|
|
// Read the specified length of data
|
|
|
|
if (!pyb_i2c_read (i2c_addr, (byte *)vstr->buf, vstr->len)) {
|
2017-02-22 01:36:23 +00:00
|
|
|
mp_raise_OSError(MP_EIO);
|
2015-09-08 14:08:33 +01:00
|
|
|
}
|
2017-03-29 06:09:46 +01:00
|
|
|
} else {
|
|
|
|
mp_raise_OSError(MP_EIO);
|
2015-09-08 14:08:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-18 12:16:09 +00:00
|
|
|
/******************************************************************************/
|
2017-06-30 08:22:17 +01:00
|
|
|
/* MicroPython bindings */
|
2015-03-18 12:16:09 +00:00
|
|
|
/******************************************************************************/
|
2015-05-25 20:06:45 +01:00
|
|
|
STATIC void pyb_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
|
|
|
pyb_i2c_obj_t *self = self_in;
|
|
|
|
if (self->baudrate > 0) {
|
2017-04-18 04:16:05 +01:00
|
|
|
mp_printf(print, "I2C(0, baudrate=%u)", self->baudrate);
|
2015-09-08 14:08:33 +01:00
|
|
|
} else {
|
|
|
|
mp_print_str(print, "I2C(0)");
|
2015-05-25 20:06:45 +01:00
|
|
|
}
|
|
|
|
}
|
2015-03-18 12:16:09 +00:00
|
|
|
|
2017-04-18 04:16:05 +01:00
|
|
|
STATIC mp_obj_t pyb_i2c_init_helper(pyb_i2c_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
|
|
|
enum { ARG_scl, ARG_sda, ARG_freq };
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_scl, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
|
|
|
|
{ MP_QSTR_sda, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
|
|
|
|
{ MP_QSTR_freq, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 100000} },
|
|
|
|
};
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
|
|
|
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
2015-05-26 11:27:13 +01:00
|
|
|
|
2015-03-19 14:17:39 +00:00
|
|
|
// make sure the baudrate is between the valid range
|
2017-04-18 04:16:05 +01:00
|
|
|
self->baudrate = MIN(MAX(args[ARG_freq].u_int, PYBI2C_MIN_BAUD_RATE_HZ), PYBI2C_MAX_BAUD_RATE_HZ);
|
2015-09-08 14:08:33 +01:00
|
|
|
|
|
|
|
// assign the pins
|
2017-04-18 04:16:05 +01:00
|
|
|
mp_obj_t pins[2] = {&pin_GP13, &pin_GP23}; // default (SDA, SCL) pins
|
|
|
|
if (args[ARG_scl].u_obj != MP_OBJ_NULL) {
|
|
|
|
pins[1] = args[ARG_scl].u_obj;
|
|
|
|
}
|
|
|
|
if (args[ARG_sda].u_obj != MP_OBJ_NULL) {
|
|
|
|
pins[0] = args[ARG_sda].u_obj;
|
2015-09-08 14:08:33 +01:00
|
|
|
}
|
2017-04-18 04:16:05 +01:00
|
|
|
pin_assign_pins_af(pins, 2, PIN_TYPE_STD_PU, PIN_FN_I2C, 0);
|
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
|
2015-09-27 12:45:48 +01:00
|
|
|
pyb_sleep_add ((const mp_obj_t)self, (WakeUpCB_t)i2c_init);
|
2015-02-10 12:11:27 +00:00
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
2017-01-04 13:10:42 +00:00
|
|
|
STATIC mp_obj_t pyb_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
|
2017-04-18 04:16:05 +01:00
|
|
|
// check the id argument, if given
|
|
|
|
if (n_args > 0) {
|
|
|
|
if (all_args[0] != MP_OBJ_NEW_SMALL_INT(0)) {
|
|
|
|
mp_raise_OSError(MP_ENODEV);
|
|
|
|
}
|
|
|
|
--n_args;
|
|
|
|
++all_args;
|
|
|
|
}
|
|
|
|
|
2015-09-11 10:05:58 +01:00
|
|
|
// parse args
|
|
|
|
mp_map_t kw_args;
|
|
|
|
mp_map_init_fixed_table(&kw_args, n_kw, all_args + n_args);
|
2015-09-07 20:09:09 +01:00
|
|
|
|
2015-09-11 10:05:58 +01:00
|
|
|
// setup the object
|
|
|
|
pyb_i2c_obj_t *self = &pyb_i2c_obj;
|
|
|
|
self->base.type = &pyb_i2c_type;
|
|
|
|
|
|
|
|
// start the peripheral
|
2017-04-18 04:16:05 +01:00
|
|
|
pyb_i2c_init_helper(self, n_args, all_args, &kw_args);
|
2015-02-10 12:11:27 +00:00
|
|
|
|
|
|
|
return (mp_obj_t)self;
|
|
|
|
}
|
|
|
|
|
2017-04-18 04:16:05 +01:00
|
|
|
STATIC mp_obj_t pyb_i2c_init(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
|
|
|
return pyb_i2c_init_helper(pos_args[0], n_args - 1, pos_args + 1, kw_args);
|
2015-03-18 14:58:48 +00:00
|
|
|
}
|
2015-05-25 20:06:45 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_init_obj, 1, pyb_i2c_init);
|
2015-02-10 12:11:27 +00:00
|
|
|
|
|
|
|
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
|
2015-09-27 12:45:48 +01:00
|
|
|
pyb_sleep_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);
|
|
|
|
|
|
|
|
STATIC mp_obj_t pyb_i2c_scan(mp_obj_t self_in) {
|
2015-09-09 21:42:42 +01:00
|
|
|
pyb_i2c_check_init(&pyb_i2c_obj);
|
2015-02-10 12:11:27 +00:00
|
|
|
mp_obj_t list = mp_obj_new_list(0, NULL);
|
2016-05-02 11:15:36 +01:00
|
|
|
for (uint addr = 0x08; addr <= 0x77; addr++) {
|
2016-02-12 19:17:15 +00:00
|
|
|
for (int i = 0; i < 3; i++) {
|
2015-03-25 15:56:14 +00:00
|
|
|
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);
|
|
|
|
|
2017-08-30 01:59:58 +01:00
|
|
|
STATIC mp_obj_t pyb_i2c_readfrom(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
2015-09-08 14:08:33 +01:00
|
|
|
STATIC const mp_arg_t pyb_i2c_readfrom_args[] = {
|
2015-09-09 21:42:42 +01:00
|
|
|
{ MP_QSTR_addr, MP_ARG_REQUIRED | MP_ARG_INT, },
|
|
|
|
{ MP_QSTR_nbytes, MP_ARG_REQUIRED | MP_ARG_OBJ, },
|
2015-09-08 14:08:33 +01:00
|
|
|
};
|
|
|
|
|
2015-09-09 21:42:42 +01:00
|
|
|
// parse args
|
2015-09-08 14:08:33 +01:00
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(pyb_i2c_readfrom_args)];
|
2015-09-14 18:38:28 +01:00
|
|
|
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(args), pyb_i2c_readfrom_args, args);
|
2015-09-08 14:08:33 +01:00
|
|
|
|
|
|
|
vstr_t vstr;
|
|
|
|
pyb_i2c_read_into(args, &vstr);
|
|
|
|
|
|
|
|
// return the received data
|
|
|
|
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
|
|
|
}
|
2015-09-09 21:42:42 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_readfrom_obj, 3, pyb_i2c_readfrom);
|
2015-09-08 14:08:33 +01:00
|
|
|
|
2017-08-30 01:59:58 +01:00
|
|
|
STATIC mp_obj_t pyb_i2c_readfrom_into(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
2015-09-08 14:08:33 +01:00
|
|
|
STATIC const mp_arg_t pyb_i2c_readfrom_into_args[] = {
|
|
|
|
{ MP_QSTR_addr, MP_ARG_REQUIRED | MP_ARG_INT, },
|
|
|
|
{ MP_QSTR_buf, MP_ARG_REQUIRED | MP_ARG_OBJ, },
|
|
|
|
};
|
|
|
|
|
|
|
|
// parse args
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(pyb_i2c_readfrom_into_args)];
|
2015-09-14 18:38:28 +01:00
|
|
|
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(args), pyb_i2c_readfrom_into_args, args);
|
2015-09-08 14:08:33 +01:00
|
|
|
|
|
|
|
vstr_t vstr;
|
|
|
|
pyb_i2c_read_into(args, &vstr);
|
|
|
|
|
|
|
|
// return the number of bytes received
|
|
|
|
return mp_obj_new_int(vstr.len);
|
|
|
|
}
|
2015-09-26 21:55:24 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_readfrom_into_obj, 1, pyb_i2c_readfrom_into);
|
2015-09-08 14:08:33 +01:00
|
|
|
|
2017-08-30 01:59:58 +01:00
|
|
|
STATIC mp_obj_t pyb_i2c_writeto(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
2015-09-08 14:08:33 +01:00
|
|
|
STATIC const mp_arg_t pyb_i2c_writeto_args[] = {
|
|
|
|
{ MP_QSTR_addr, MP_ARG_REQUIRED | MP_ARG_INT, },
|
|
|
|
{ MP_QSTR_buf, MP_ARG_REQUIRED | MP_ARG_OBJ, },
|
|
|
|
{ MP_QSTR_stop, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} },
|
|
|
|
};
|
2015-02-10 12:11:27 +00:00
|
|
|
|
|
|
|
// parse args
|
2015-09-08 14:08:33 +01:00
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(pyb_i2c_writeto_args)];
|
2015-09-14 18:38:28 +01:00
|
|
|
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(args), pyb_i2c_writeto_args, args);
|
2015-02-10 12:11:27 +00:00
|
|
|
|
2015-09-09 21:42:42 +01:00
|
|
|
pyb_i2c_check_init(&pyb_i2c_obj);
|
|
|
|
|
2015-02-10 12:11:27 +00:00
|
|
|
// get the buffer to send from
|
|
|
|
mp_buffer_info_t bufinfo;
|
|
|
|
uint8_t data[1];
|
2015-09-08 14:08:33 +01:00
|
|
|
pyb_buf_get_for_send(args[1].u_obj, &bufinfo, data);
|
2015-02-10 12:11:27 +00:00
|
|
|
|
|
|
|
// send the data
|
2015-09-08 14:08:33 +01:00
|
|
|
if (!pyb_i2c_write(args[0].u_int, bufinfo.buf, bufinfo.len, args[2].u_bool)) {
|
2017-02-22 01:36:23 +00:00
|
|
|
mp_raise_OSError(MP_EIO);
|
2015-02-10 12:11:27 +00:00
|
|
|
}
|
|
|
|
|
2015-09-08 14:08:33 +01:00
|
|
|
// return the number of bytes written
|
|
|
|
return mp_obj_new_int(bufinfo.len);
|
2015-02-10 12:11:27 +00:00
|
|
|
}
|
2015-09-26 21:55:24 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_writeto_obj, 1, pyb_i2c_writeto);
|
2015-02-10 12:11:27 +00:00
|
|
|
|
2017-08-30 01:59:58 +01:00
|
|
|
STATIC mp_obj_t pyb_i2c_readfrom_mem(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
2015-09-08 14:08:33 +01:00
|
|
|
STATIC const mp_arg_t pyb_i2c_readfrom_mem_args[] = {
|
2015-09-09 21:42:42 +01:00
|
|
|
{ MP_QSTR_addr, MP_ARG_REQUIRED | MP_ARG_INT, },
|
2015-09-08 14:08:33 +01:00
|
|
|
{ MP_QSTR_memaddr, MP_ARG_REQUIRED | MP_ARG_INT, },
|
2015-09-09 21:42:42 +01:00
|
|
|
{ MP_QSTR_nbytes, MP_ARG_REQUIRED | MP_ARG_OBJ, },
|
2015-09-08 14:08:33 +01:00
|
|
|
{ MP_QSTR_addrsize, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
|
|
|
|
};
|
2015-02-10 12:11:27 +00:00
|
|
|
|
|
|
|
// parse args
|
2015-09-08 14:08:33 +01:00
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(pyb_i2c_readfrom_mem_args)];
|
2015-09-14 18:38:28 +01:00
|
|
|
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(args), pyb_i2c_readfrom_mem_args, args);
|
2015-02-10 12:11:27 +00:00
|
|
|
|
|
|
|
vstr_t vstr;
|
2015-09-08 14:08:33 +01:00
|
|
|
pyb_i2c_readmem_into (args, &vstr);
|
|
|
|
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
2015-02-10 12:11:27 +00:00
|
|
|
}
|
2015-09-26 21:55:24 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_readfrom_mem_obj, 1, pyb_i2c_readfrom_mem);
|
2015-09-08 14:08:33 +01:00
|
|
|
|
|
|
|
STATIC const mp_arg_t pyb_i2c_readfrom_mem_into_args[] = {
|
2015-09-09 21:42:42 +01:00
|
|
|
{ MP_QSTR_addr, MP_ARG_REQUIRED | MP_ARG_INT, },
|
2015-09-08 14:08:33 +01:00
|
|
|
{ MP_QSTR_memaddr, MP_ARG_REQUIRED | MP_ARG_INT, },
|
|
|
|
{ MP_QSTR_buf, MP_ARG_REQUIRED | MP_ARG_OBJ, },
|
|
|
|
{ MP_QSTR_addrsize, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
|
2015-02-10 12:11:27 +00:00
|
|
|
};
|
|
|
|
|
2017-08-30 01:59:58 +01:00
|
|
|
STATIC mp_obj_t pyb_i2c_readfrom_mem_into(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
2015-02-10 12:11:27 +00:00
|
|
|
// parse args
|
2015-09-08 14:08:33 +01:00
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(pyb_i2c_readfrom_mem_into_args)];
|
2015-09-14 18:38:28 +01:00
|
|
|
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(args), pyb_i2c_readfrom_mem_into_args, args);
|
2015-02-10 12:11:27 +00:00
|
|
|
|
|
|
|
// get the buffer to read into
|
|
|
|
vstr_t vstr;
|
2015-09-08 14:08:33 +01:00
|
|
|
pyb_i2c_readmem_into (args, &vstr);
|
2017-04-18 06:31:08 +01:00
|
|
|
return mp_const_none;
|
2015-02-10 12:11:27 +00:00
|
|
|
}
|
2015-09-26 21:55:24 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_readfrom_mem_into_obj, 1, pyb_i2c_readfrom_mem_into);
|
2015-02-10 12:11:27 +00:00
|
|
|
|
2017-08-30 01:59:58 +01:00
|
|
|
STATIC mp_obj_t pyb_i2c_writeto_mem(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
2015-09-08 14:08:33 +01:00
|
|
|
// parse args
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(pyb_i2c_readfrom_mem_into_args)];
|
|
|
|
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(pyb_i2c_readfrom_mem_into_args), pyb_i2c_readfrom_mem_into_args, args);
|
2015-02-10 12:11:27 +00:00
|
|
|
|
2015-09-09 21:42:42 +01:00
|
|
|
pyb_i2c_check_init(&pyb_i2c_obj);
|
|
|
|
|
2015-02-10 12:11:27 +00:00
|
|
|
// get the buffer to write from
|
|
|
|
mp_buffer_info_t bufinfo;
|
|
|
|
uint8_t data[1];
|
2015-09-08 14:08:33 +01:00
|
|
|
pyb_buf_get_for_send(args[2].u_obj, &bufinfo, data);
|
2015-02-10 12:11:27 +00:00
|
|
|
|
|
|
|
// get the addresses
|
2015-09-08 14:08:33 +01:00
|
|
|
mp_uint_t i2c_addr = args[0].u_int;
|
|
|
|
mp_uint_t mem_addr = args[1].u_int;
|
2015-03-25 15:56:14 +00:00
|
|
|
// determine the width of mem_addr (1 or 2 bytes)
|
2015-09-08 14:08:33 +01:00
|
|
|
mp_uint_t mem_addr_size = args[3].u_int >> 3;
|
2015-02-10 12:11:27 +00:00
|
|
|
|
2015-09-08 14:08:33 +01:00
|
|
|
// write the register address to write to.
|
2015-09-09 21:42:42 +01:00
|
|
|
if (pyb_i2c_mem_write (i2c_addr, (byte *)&mem_addr, mem_addr_size, bufinfo.buf, bufinfo.len)) {
|
2017-04-18 06:31:08 +01:00
|
|
|
return mp_const_none;
|
2015-02-10 12:11:27 +00:00
|
|
|
}
|
|
|
|
|
2017-02-22 01:36:23 +00:00
|
|
|
mp_raise_OSError(MP_EIO);
|
2015-02-10 12:11:27 +00:00
|
|
|
}
|
2015-09-26 21:55:24 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_writeto_mem_obj, 1, pyb_i2c_writeto_mem);
|
2015-02-10 12:11:27 +00:00
|
|
|
|
2017-08-21 12:34:23 +01:00
|
|
|
STATIC const mp_rom_map_elem_t pyb_i2c_locals_dict_table[] = {
|
2015-02-10 12:11:27 +00:00
|
|
|
// instance methods
|
2017-08-21 12:34:23 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&pyb_i2c_init_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&pyb_i2c_deinit_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_scan), MP_ROM_PTR(&pyb_i2c_scan_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_readfrom), MP_ROM_PTR(&pyb_i2c_readfrom_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_readfrom_into), MP_ROM_PTR(&pyb_i2c_readfrom_into_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_writeto), MP_ROM_PTR(&pyb_i2c_writeto_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_readfrom_mem), MP_ROM_PTR(&pyb_i2c_readfrom_mem_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_readfrom_mem_into), MP_ROM_PTR(&pyb_i2c_readfrom_mem_into_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_writeto_mem), MP_ROM_PTR(&pyb_i2c_writeto_mem_obj) },
|
2015-02-10 12:11:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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,
|
|
|
|
};
|