remove machine_i2c, machine_spi
This micropython code is not used in circuitpython
This commit is contained in:
parent
4f8b37042d
commit
103ad9966a
@ -1,641 +0,0 @@
|
||||
/*
|
||||
* This file is part of the MicroPython project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2016 Damien P. George
|
||||
*
|
||||
* 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 <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "py/mperrno.h"
|
||||
#include "py/mphal.h"
|
||||
#include "py/runtime.h"
|
||||
#include "extmod/machine_i2c.h"
|
||||
|
||||
#include "supervisor/shared/translate.h"
|
||||
|
||||
#if MICROPY_PY_MACHINE_I2C
|
||||
|
||||
typedef mp_machine_soft_i2c_obj_t machine_i2c_obj_t;
|
||||
|
||||
STATIC void mp_hal_i2c_delay(machine_i2c_obj_t *self) {
|
||||
// We need to use an accurate delay to get acceptable I2C
|
||||
// speeds (eg 1us should be not much more than 1us).
|
||||
mp_hal_delay_us_fast(self->us_delay);
|
||||
}
|
||||
|
||||
STATIC void mp_hal_i2c_scl_low(machine_i2c_obj_t *self) {
|
||||
mp_hal_pin_od_low(self->scl);
|
||||
}
|
||||
|
||||
STATIC int mp_hal_i2c_scl_release(machine_i2c_obj_t *self) {
|
||||
uint32_t count = self->us_timeout;
|
||||
|
||||
mp_hal_pin_od_high(self->scl);
|
||||
mp_hal_i2c_delay(self);
|
||||
// For clock stretching, wait for the SCL pin to be released, with timeout.
|
||||
for (; mp_hal_pin_read(self->scl) == 0 && count; --count) {
|
||||
mp_hal_delay_us_fast(1);
|
||||
}
|
||||
if (count == 0) {
|
||||
return -MP_ETIMEDOUT;
|
||||
}
|
||||
return 0; // success
|
||||
}
|
||||
|
||||
STATIC void mp_hal_i2c_sda_low(machine_i2c_obj_t *self) {
|
||||
mp_hal_pin_od_low(self->sda);
|
||||
}
|
||||
|
||||
STATIC void mp_hal_i2c_sda_release(machine_i2c_obj_t *self) {
|
||||
mp_hal_pin_od_high(self->sda);
|
||||
}
|
||||
|
||||
STATIC int mp_hal_i2c_sda_read(machine_i2c_obj_t *self) {
|
||||
return mp_hal_pin_read(self->sda);
|
||||
}
|
||||
|
||||
STATIC int mp_hal_i2c_start(machine_i2c_obj_t *self) {
|
||||
mp_hal_i2c_sda_release(self);
|
||||
mp_hal_i2c_delay(self);
|
||||
int ret = mp_hal_i2c_scl_release(self);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
mp_hal_i2c_sda_low(self);
|
||||
mp_hal_i2c_delay(self);
|
||||
return 0; // success
|
||||
}
|
||||
|
||||
STATIC int mp_hal_i2c_stop(machine_i2c_obj_t *self) {
|
||||
mp_hal_i2c_delay(self);
|
||||
mp_hal_i2c_sda_low(self);
|
||||
mp_hal_i2c_delay(self);
|
||||
int ret = mp_hal_i2c_scl_release(self);
|
||||
mp_hal_i2c_sda_release(self);
|
||||
mp_hal_i2c_delay(self);
|
||||
return ret;
|
||||
}
|
||||
|
||||
STATIC void mp_hal_i2c_init(machine_i2c_obj_t *self, uint32_t freq) {
|
||||
self->us_delay = 500000 / freq;
|
||||
if (self->us_delay == 0) {
|
||||
self->us_delay = 1;
|
||||
}
|
||||
mp_hal_pin_open_drain(self->scl);
|
||||
mp_hal_pin_open_drain(self->sda);
|
||||
mp_hal_i2c_stop(self); // ignore error
|
||||
}
|
||||
|
||||
// return value:
|
||||
// 0 - byte written and ack received
|
||||
// 1 - byte written and nack received
|
||||
// <0 - error, with errno being the negative of the return value
|
||||
STATIC int mp_hal_i2c_write_byte(machine_i2c_obj_t *self, uint8_t val) {
|
||||
mp_hal_i2c_delay(self);
|
||||
mp_hal_i2c_scl_low(self);
|
||||
|
||||
for (int i = 7; i >= 0; i--) {
|
||||
if ((val >> i) & 1) {
|
||||
mp_hal_i2c_sda_release(self);
|
||||
} else {
|
||||
mp_hal_i2c_sda_low(self);
|
||||
}
|
||||
mp_hal_i2c_delay(self);
|
||||
int ret = mp_hal_i2c_scl_release(self);
|
||||
if (ret != 0) {
|
||||
mp_hal_i2c_sda_release(self);
|
||||
return ret;
|
||||
}
|
||||
mp_hal_i2c_scl_low(self);
|
||||
}
|
||||
|
||||
mp_hal_i2c_sda_release(self);
|
||||
mp_hal_i2c_delay(self);
|
||||
int ret = mp_hal_i2c_scl_release(self);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ack = mp_hal_i2c_sda_read(self);
|
||||
mp_hal_i2c_delay(self);
|
||||
mp_hal_i2c_scl_low(self);
|
||||
|
||||
return ack;
|
||||
}
|
||||
|
||||
// return value:
|
||||
// 0 - success
|
||||
// <0 - error, with errno being the negative of the return value
|
||||
STATIC int mp_hal_i2c_read_byte(machine_i2c_obj_t *self, uint8_t *val, int nack) {
|
||||
mp_hal_i2c_delay(self);
|
||||
mp_hal_i2c_scl_low(self);
|
||||
mp_hal_i2c_delay(self);
|
||||
|
||||
uint8_t data = 0;
|
||||
for (int i = 7; i >= 0; i--) {
|
||||
int ret = mp_hal_i2c_scl_release(self);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
data = (data << 1) | mp_hal_i2c_sda_read(self);
|
||||
mp_hal_i2c_scl_low(self);
|
||||
mp_hal_i2c_delay(self);
|
||||
}
|
||||
*val = data;
|
||||
|
||||
// send ack/nack bit
|
||||
if (!nack) {
|
||||
mp_hal_i2c_sda_low(self);
|
||||
}
|
||||
mp_hal_i2c_delay(self);
|
||||
int ret = mp_hal_i2c_scl_release(self);
|
||||
if (ret != 0) {
|
||||
mp_hal_i2c_sda_release(self);
|
||||
return ret;
|
||||
}
|
||||
mp_hal_i2c_scl_low(self);
|
||||
mp_hal_i2c_sda_release(self);
|
||||
|
||||
return 0; // success
|
||||
}
|
||||
|
||||
// return value:
|
||||
// >=0 - number of acks received
|
||||
// <0 - error, with errno being the negative of the return value
|
||||
int mp_machine_soft_i2c_writeto(mp_obj_base_t *self_in, uint16_t addr, const uint8_t *src, size_t len, bool stop) {
|
||||
machine_i2c_obj_t *self = (machine_i2c_obj_t*)self_in;
|
||||
|
||||
// start the I2C transaction
|
||||
int ret = mp_hal_i2c_start(self);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
// write the slave address
|
||||
ret = mp_hal_i2c_write_byte(self, addr << 1);
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
} else if (ret != 0) {
|
||||
// nack received, release the bus cleanly
|
||||
mp_hal_i2c_stop(self);
|
||||
return -MP_ENODEV;
|
||||
}
|
||||
|
||||
// write the buffer to the I2C memory
|
||||
int num_acks = 0;
|
||||
while (len--) {
|
||||
ret = mp_hal_i2c_write_byte(self, *src++);
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
} else if (ret != 0) {
|
||||
// nack received, stop sending
|
||||
break;
|
||||
}
|
||||
++num_acks;
|
||||
}
|
||||
|
||||
// finish the I2C transaction
|
||||
if (stop) {
|
||||
ret = mp_hal_i2c_stop(self);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
return num_acks;
|
||||
}
|
||||
|
||||
// return value:
|
||||
// 0 - success
|
||||
// <0 - error, with errno being the negative of the return value
|
||||
int mp_machine_soft_i2c_readfrom(mp_obj_base_t *self_in, uint16_t addr, uint8_t *dest, size_t len, bool stop) {
|
||||
machine_i2c_obj_t *self = (machine_i2c_obj_t*)self_in;
|
||||
|
||||
// start the I2C transaction
|
||||
int ret = mp_hal_i2c_start(self);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
// write the slave address
|
||||
ret = mp_hal_i2c_write_byte(self, (addr << 1) | 1);
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
} else if (ret != 0) {
|
||||
// nack received, release the bus cleanly
|
||||
mp_hal_i2c_stop(self);
|
||||
return -MP_ENODEV;
|
||||
}
|
||||
|
||||
// read the bytes from the slave
|
||||
while (len--) {
|
||||
ret = mp_hal_i2c_read_byte(self, dest++, len == 0);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
// finish the I2C transaction
|
||||
if (stop) {
|
||||
ret = mp_hal_i2c_stop(self);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
return 0; // success
|
||||
}
|
||||
|
||||
/******************************************************************************/
|
||||
// MicroPython bindings for I2C
|
||||
|
||||
STATIC void machine_i2c_obj_init_helper(machine_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, ARG_timeout };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_scl, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_sda, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
||||
{ MP_QSTR_freq, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 400000} },
|
||||
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 255} },
|
||||
};
|
||||
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);
|
||||
self->scl = mp_hal_get_pin_obj(args[ARG_scl].u_obj);
|
||||
self->sda = mp_hal_get_pin_obj(args[ARG_sda].u_obj);
|
||||
self->us_timeout = args[ARG_timeout].u_int;
|
||||
mp_hal_i2c_init(self, args[ARG_freq].u_int);
|
||||
}
|
||||
|
||||
STATIC mp_obj_t machine_i2c_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
|
||||
// check the id argument, if given
|
||||
if (n_args > 0) {
|
||||
if (args[0] != MP_OBJ_NEW_SMALL_INT(-1)) {
|
||||
#if defined(MICROPY_PY_MACHINE_I2C_MAKE_NEW)
|
||||
// dispatch to port-specific constructor
|
||||
extern mp_obj_t MICROPY_PY_MACHINE_I2C_MAKE_NEW(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *all_args, mp_map_t *kw_args);
|
||||
return MICROPY_PY_MACHINE_I2C_MAKE_NEW(type, n_args, args, kw_args);
|
||||
#else
|
||||
mp_raise_ValueError(translate("invalid I2C peripheral"));
|
||||
#endif
|
||||
}
|
||||
--n_args;
|
||||
++args;
|
||||
}
|
||||
|
||||
// create new soft I2C object
|
||||
machine_i2c_obj_t *self = m_new_obj(machine_i2c_obj_t);
|
||||
self->base.type = &machine_i2c_type;
|
||||
machine_i2c_obj_init_helper(self, n_args, args, kw_args);
|
||||
return (mp_obj_t)self;
|
||||
}
|
||||
|
||||
STATIC mp_obj_t machine_i2c_obj_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
|
||||
machine_i2c_obj_init_helper(args[0], n_args - 1, args + 1, kw_args);
|
||||
return mp_const_none;
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(machine_i2c_init_obj, 1, machine_i2c_obj_init);
|
||||
|
||||
STATIC mp_obj_t machine_i2c_scan(mp_obj_t self_in) {
|
||||
mp_obj_base_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t*)mp_proto_get(self, QSTR_protocol_i2c);
|
||||
mp_obj_t list = mp_obj_new_list(0, NULL);
|
||||
// 7-bit addresses 0b0000xxx and 0b1111xxx are reserved
|
||||
for (int addr = 0x08; addr < 0x78; ++addr) {
|
||||
int ret = i2c_p->writeto(self, addr, NULL, 0, true);
|
||||
if (ret == 0) {
|
||||
mp_obj_list_append(list, MP_OBJ_NEW_SMALL_INT(addr));
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(machine_i2c_scan_obj, machine_i2c_scan);
|
||||
|
||||
STATIC mp_obj_t machine_i2c_start(mp_obj_t self_in) {
|
||||
mp_obj_base_t *self = (mp_obj_base_t*)MP_OBJ_TO_PTR(self_in);
|
||||
mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t*)mp_proto_get(self, QSTR_protocol_i2c);
|
||||
if (i2c_p->start == NULL) {
|
||||
mp_raise_msg(&mp_type_OSError, translate("I2C operation not supported"));
|
||||
}
|
||||
int ret = i2c_p->start(self);
|
||||
if (ret != 0) {
|
||||
mp_raise_OSError(-ret);
|
||||
}
|
||||
return mp_const_none;
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(machine_i2c_start_obj, machine_i2c_start);
|
||||
|
||||
STATIC mp_obj_t machine_i2c_stop(mp_obj_t self_in) {
|
||||
mp_obj_base_t *self = (mp_obj_base_t*)MP_OBJ_TO_PTR(self_in);
|
||||
mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t*)mp_proto_get(self, QSTR_protocol_i2c);
|
||||
if (i2c_p->stop == NULL) {
|
||||
mp_raise_msg(&mp_type_OSError, translate("I2C operation not supported"));
|
||||
}
|
||||
int ret = i2c_p->stop(self);
|
||||
if (ret != 0) {
|
||||
mp_raise_OSError(-ret);
|
||||
}
|
||||
return mp_const_none;
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(machine_i2c_stop_obj, machine_i2c_stop);
|
||||
|
||||
STATIC mp_obj_t machine_i2c_readinto(size_t n_args, const mp_obj_t *args) {
|
||||
mp_obj_base_t *self = (mp_obj_base_t*)MP_OBJ_TO_PTR(args[0]);
|
||||
mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t*)mp_proto_get(self, QSTR_protocol_i2c);
|
||||
if (i2c_p->read == NULL) {
|
||||
mp_raise_msg(&mp_type_OSError, translate("I2C operation not supported"));
|
||||
}
|
||||
|
||||
// get the buffer to read into
|
||||
mp_buffer_info_t bufinfo;
|
||||
mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_WRITE);
|
||||
|
||||
// work out if we want to send a nack at the end
|
||||
bool nack = (n_args == 2) ? true : mp_obj_is_true(args[2]);
|
||||
|
||||
// do the read
|
||||
int ret = i2c_p->read(self, bufinfo.buf, bufinfo.len, nack);
|
||||
if (ret != 0) {
|
||||
mp_raise_OSError(-ret);
|
||||
}
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_i2c_readinto_obj, 2, 3, machine_i2c_readinto);
|
||||
|
||||
STATIC mp_obj_t machine_i2c_write(mp_obj_t self_in, mp_obj_t buf_in) {
|
||||
mp_obj_base_t *self = (mp_obj_base_t*)MP_OBJ_TO_PTR(self_in);
|
||||
mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t*)mp_proto_get(self, QSTR_protocol_i2c);
|
||||
if (i2c_p->write == NULL) {
|
||||
mp_raise_msg(&mp_type_OSError, translate("I2C operation not supported"));
|
||||
}
|
||||
|
||||
// get the buffer to write from
|
||||
mp_buffer_info_t bufinfo;
|
||||
mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_READ);
|
||||
|
||||
// do the write
|
||||
int ret = i2c_p->write(self, bufinfo.buf, bufinfo.len);
|
||||
if (ret < 0) {
|
||||
mp_raise_OSError(-ret);
|
||||
}
|
||||
|
||||
// return number of acks received
|
||||
return MP_OBJ_NEW_SMALL_INT(ret);
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_2(machine_i2c_write_obj, machine_i2c_write);
|
||||
|
||||
STATIC mp_obj_t machine_i2c_readfrom(size_t n_args, const mp_obj_t *args) {
|
||||
mp_obj_base_t *self = (mp_obj_base_t*)MP_OBJ_TO_PTR(args[0]);
|
||||
mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t*)mp_proto_get(self, QSTR_protocol_i2c);
|
||||
mp_int_t addr = mp_obj_get_int(args[1]);
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, mp_obj_get_int(args[2]));
|
||||
bool stop = (n_args == 3) ? true : mp_obj_is_true(args[3]);
|
||||
int ret = i2c_p->readfrom(self, addr, (uint8_t*)vstr.buf, vstr.len, stop);
|
||||
if (ret < 0) {
|
||||
mp_raise_OSError(-ret);
|
||||
}
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_i2c_readfrom_obj, 3, 4, machine_i2c_readfrom);
|
||||
|
||||
STATIC mp_obj_t machine_i2c_readfrom_into(size_t n_args, const mp_obj_t *args) {
|
||||
mp_obj_base_t *self = (mp_obj_base_t*)MP_OBJ_TO_PTR(args[0]);
|
||||
mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t*)mp_proto_get(self, QSTR_protocol_i2c);
|
||||
mp_int_t addr = mp_obj_get_int(args[1]);
|
||||
mp_buffer_info_t bufinfo;
|
||||
mp_get_buffer_raise(args[2], &bufinfo, MP_BUFFER_WRITE);
|
||||
bool stop = (n_args == 3) ? true : mp_obj_is_true(args[3]);
|
||||
int ret = i2c_p->readfrom(self, addr, bufinfo.buf, bufinfo.len, stop);
|
||||
if (ret < 0) {
|
||||
mp_raise_OSError(-ret);
|
||||
}
|
||||
return mp_const_none;
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_i2c_readfrom_into_obj, 3, 4, machine_i2c_readfrom_into);
|
||||
|
||||
STATIC mp_obj_t machine_i2c_writeto(size_t n_args, const mp_obj_t *args) {
|
||||
mp_obj_base_t *self = (mp_obj_base_t*)MP_OBJ_TO_PTR(args[0]);
|
||||
mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t*)mp_proto_get(self, QSTR_protocol_i2c);
|
||||
mp_int_t addr = mp_obj_get_int(args[1]);
|
||||
mp_buffer_info_t bufinfo;
|
||||
mp_get_buffer_raise(args[2], &bufinfo, MP_BUFFER_READ);
|
||||
bool stop = (n_args == 3) ? true : mp_obj_is_true(args[3]);
|
||||
int ret = i2c_p->writeto(self, addr, bufinfo.buf, bufinfo.len, stop);
|
||||
if (ret < 0) {
|
||||
mp_raise_OSError(-ret);
|
||||
}
|
||||
// return number of acks received
|
||||
return MP_OBJ_NEW_SMALL_INT(ret);
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_i2c_writeto_obj, 3, 4, machine_i2c_writeto);
|
||||
|
||||
STATIC int read_mem(mp_obj_t self_in, uint16_t addr, uint32_t memaddr, uint8_t addrsize, uint8_t *buf, size_t len) {
|
||||
mp_obj_base_t *self = (mp_obj_base_t*)MP_OBJ_TO_PTR(self_in);
|
||||
mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t*)mp_proto_get(self, QSTR_protocol_i2c);
|
||||
uint8_t memaddr_buf[4];
|
||||
size_t memaddr_len = 0;
|
||||
for (int16_t i = addrsize - 8; i >= 0; i -= 8) {
|
||||
memaddr_buf[memaddr_len++] = memaddr >> i;
|
||||
}
|
||||
int ret = i2c_p->writeto(self, addr, memaddr_buf, memaddr_len, false);
|
||||
if (ret != memaddr_len) {
|
||||
// must generate STOP
|
||||
i2c_p->writeto(self, addr, NULL, 0, true);
|
||||
return ret;
|
||||
}
|
||||
return i2c_p->readfrom(self, addr, buf, len, true);
|
||||
}
|
||||
|
||||
#define MAX_MEMADDR_SIZE (4)
|
||||
#define BUF_STACK_SIZE (12)
|
||||
|
||||
STATIC int write_mem(mp_obj_t self_in, uint16_t addr, uint32_t memaddr, uint8_t addrsize, const uint8_t *buf, size_t len) {
|
||||
mp_obj_base_t *self = (mp_obj_base_t*)MP_OBJ_TO_PTR(self_in);
|
||||
mp_machine_i2c_p_t *i2c_p = (mp_machine_i2c_p_t*)mp_proto_get(self, QSTR_protocol_i2c);
|
||||
|
||||
// need some memory to create the buffer to send; try to use stack if possible
|
||||
uint8_t buf2_stack[MAX_MEMADDR_SIZE + BUF_STACK_SIZE];
|
||||
uint8_t *buf2;
|
||||
size_t buf2_alloc = 0;
|
||||
if (len <= BUF_STACK_SIZE) {
|
||||
buf2 = buf2_stack;
|
||||
} else {
|
||||
buf2_alloc = MAX_MEMADDR_SIZE + len;
|
||||
buf2 = m_new(uint8_t, buf2_alloc);
|
||||
}
|
||||
|
||||
// create the buffer to send
|
||||
size_t memaddr_len = 0;
|
||||
for (int16_t i = addrsize - 8; i >= 0; i -= 8) {
|
||||
buf2[memaddr_len++] = memaddr >> i;
|
||||
}
|
||||
memcpy(buf2 + memaddr_len, buf, len);
|
||||
|
||||
int ret = i2c_p->writeto(self, addr, buf2, memaddr_len + len, true);
|
||||
if (buf2_alloc != 0) {
|
||||
m_del(uint8_t, buf2, buf2_alloc);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
STATIC const mp_arg_t machine_i2c_mem_allowed_args[] = {
|
||||
{ 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_arg, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
|
||||
{ MP_QSTR_addrsize, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
|
||||
};
|
||||
|
||||
STATIC mp_obj_t machine_i2c_readfrom_mem(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
enum { ARG_addr, ARG_memaddr, ARG_n, ARG_addrsize };
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(machine_i2c_mem_allowed_args)];
|
||||
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args,
|
||||
MP_ARRAY_SIZE(machine_i2c_mem_allowed_args), machine_i2c_mem_allowed_args, args);
|
||||
|
||||
// create the buffer to store data into
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, mp_obj_get_int(args[ARG_n].u_obj));
|
||||
|
||||
// do the transfer
|
||||
int ret = read_mem(pos_args[0], args[ARG_addr].u_int, args[ARG_memaddr].u_int,
|
||||
args[ARG_addrsize].u_int, (uint8_t*)vstr.buf, vstr.len);
|
||||
if (ret < 0) {
|
||||
mp_raise_OSError(-ret);
|
||||
}
|
||||
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(machine_i2c_readfrom_mem_obj, 1, machine_i2c_readfrom_mem);
|
||||
|
||||
|
||||
STATIC mp_obj_t machine_i2c_readfrom_mem_into(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
enum { ARG_addr, ARG_memaddr, ARG_buf, ARG_addrsize };
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(machine_i2c_mem_allowed_args)];
|
||||
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args,
|
||||
MP_ARRAY_SIZE(machine_i2c_mem_allowed_args), machine_i2c_mem_allowed_args, args);
|
||||
|
||||
// get the buffer to store data into
|
||||
mp_buffer_info_t bufinfo;
|
||||
mp_get_buffer_raise(args[ARG_buf].u_obj, &bufinfo, MP_BUFFER_WRITE);
|
||||
|
||||
// do the transfer
|
||||
int ret = read_mem(pos_args[0], args[ARG_addr].u_int, args[ARG_memaddr].u_int,
|
||||
args[ARG_addrsize].u_int, bufinfo.buf, bufinfo.len);
|
||||
if (ret < 0) {
|
||||
mp_raise_OSError(-ret);
|
||||
}
|
||||
return mp_const_none;
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_KW(machine_i2c_readfrom_mem_into_obj, 1, machine_i2c_readfrom_mem_into);
|
||||
|
||||
STATIC mp_obj_t machine_i2c_writeto_mem(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
enum { ARG_addr, ARG_memaddr, ARG_buf, ARG_addrsize };
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(machine_i2c_mem_allowed_args)];
|
||||
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args,
|
||||
MP_ARRAY_SIZE(machine_i2c_mem_allowed_args), machine_i2c_mem_allowed_args, args);
|
||||
|
||||
// get the buffer to write the data from
|
||||
mp_buffer_info_t bufinfo;
|
||||
mp_get_buffer_raise(args[ARG_buf].u_obj, &bufinfo, MP_BUFFER_READ);
|
||||
|
||||
// do the transfer
|
||||
int ret = write_mem(pos_args[0], args[ARG_addr].u_int, args[ARG_memaddr].u_int,
|
||||
args[ARG_addrsize].u_int, bufinfo.buf, bufinfo.len);
|
||||
if (ret < 0) {
|
||||
mp_raise_OSError(-ret);
|
||||
}
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_i2c_writeto_mem_obj, 1, machine_i2c_writeto_mem);
|
||||
|
||||
STATIC const mp_rom_map_elem_t machine_i2c_locals_dict_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_i2c_init_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_scan), MP_ROM_PTR(&machine_i2c_scan_obj) },
|
||||
|
||||
// primitive I2C operations
|
||||
{ MP_ROM_QSTR(MP_QSTR_start), MP_ROM_PTR(&machine_i2c_start_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_stop), MP_ROM_PTR(&machine_i2c_stop_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&machine_i2c_readinto_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&machine_i2c_write_obj) },
|
||||
|
||||
// standard bus operations
|
||||
{ MP_ROM_QSTR(MP_QSTR_readfrom), MP_ROM_PTR(&machine_i2c_readfrom_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_readfrom_into), MP_ROM_PTR(&machine_i2c_readfrom_into_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_writeto), MP_ROM_PTR(&machine_i2c_writeto_obj) },
|
||||
|
||||
// memory operations
|
||||
{ MP_ROM_QSTR(MP_QSTR_readfrom_mem), MP_ROM_PTR(&machine_i2c_readfrom_mem_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_readfrom_mem_into), MP_ROM_PTR(&machine_i2c_readfrom_mem_into_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_writeto_mem), MP_ROM_PTR(&machine_i2c_writeto_mem_obj) },
|
||||
};
|
||||
|
||||
MP_DEFINE_CONST_DICT(mp_machine_soft_i2c_locals_dict, machine_i2c_locals_dict_table);
|
||||
|
||||
int mp_machine_soft_i2c_read(mp_obj_base_t *self_in, uint8_t *dest, size_t len, bool nack) {
|
||||
machine_i2c_obj_t *self = (machine_i2c_obj_t*)self_in;
|
||||
while (len--) {
|
||||
int ret = mp_hal_i2c_read_byte(self, dest++, nack && (len == 0));
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
return 0; // success
|
||||
}
|
||||
|
||||
int mp_machine_soft_i2c_write(mp_obj_base_t *self_in, const uint8_t *src, size_t len) {
|
||||
machine_i2c_obj_t *self = (machine_i2c_obj_t*)self_in;
|
||||
int num_acks = 0;
|
||||
while (len--) {
|
||||
int ret = mp_hal_i2c_write_byte(self, *src++);
|
||||
if (ret < 0) {
|
||||
return ret;
|
||||
} else if (ret != 0) {
|
||||
// nack received, stop sending
|
||||
break;
|
||||
}
|
||||
++num_acks;
|
||||
}
|
||||
return num_acks;
|
||||
}
|
||||
|
||||
STATIC const mp_machine_i2c_p_t mp_machine_soft_i2c_p = {
|
||||
MP_PROTO_IMPLEMENT(MP_QSTR_protocol_i2c)
|
||||
.start = (int(*)(mp_obj_base_t*))mp_hal_i2c_start,
|
||||
.stop = (int(*)(mp_obj_base_t*))mp_hal_i2c_stop,
|
||||
.read = mp_machine_soft_i2c_read,
|
||||
.write = mp_machine_soft_i2c_write,
|
||||
.readfrom = mp_machine_soft_i2c_readfrom,
|
||||
.writeto = mp_machine_soft_i2c_writeto,
|
||||
};
|
||||
|
||||
const mp_obj_type_t machine_i2c_type = {
|
||||
{ &mp_type_type },
|
||||
.name = MP_QSTR_I2C,
|
||||
.make_new = machine_i2c_make_new,
|
||||
.protocol = &mp_machine_soft_i2c_p,
|
||||
.locals_dict = (mp_obj_dict_t*)&mp_machine_soft_i2c_locals_dict,
|
||||
};
|
||||
|
||||
#endif // MICROPY_PY_MACHINE_I2C
|
@ -1,58 +0,0 @@
|
||||
/*
|
||||
* This file is part of the MicroPython project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2016 Damien P. George
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MICROPY_INCLUDED_EXTMOD_MACHINE_I2C_H
|
||||
#define MICROPY_INCLUDED_EXTMOD_MACHINE_I2C_H
|
||||
|
||||
#include "py/obj.h"
|
||||
#include "py/proto.h"
|
||||
|
||||
// I2C protocol
|
||||
// the first 4 methods can be NULL, meaning operation is not supported
|
||||
typedef struct _mp_machine_i2c_p_t {
|
||||
MP_PROTOCOL_HEAD
|
||||
int (*start)(mp_obj_base_t *obj);
|
||||
int (*stop)(mp_obj_base_t *obj);
|
||||
int (*read)(mp_obj_base_t *obj, uint8_t *dest, size_t len, bool nack);
|
||||
int (*write)(mp_obj_base_t *obj, const uint8_t *src, size_t len);
|
||||
int (*readfrom)(mp_obj_base_t *obj, uint16_t addr, uint8_t *dest, size_t len, bool stop);
|
||||
int (*writeto)(mp_obj_base_t *obj, uint16_t addr, const uint8_t *src, size_t len, bool stop);
|
||||
} mp_machine_i2c_p_t;
|
||||
|
||||
typedef struct _mp_machine_soft_i2c_obj_t {
|
||||
mp_obj_base_t base;
|
||||
uint32_t us_delay;
|
||||
uint32_t us_timeout;
|
||||
mp_hal_pin_obj_t scl;
|
||||
mp_hal_pin_obj_t sda;
|
||||
} mp_machine_soft_i2c_obj_t;
|
||||
|
||||
extern const mp_obj_type_t machine_i2c_type;
|
||||
extern const mp_obj_dict_t mp_machine_soft_i2c_locals_dict;
|
||||
|
||||
int mp_machine_soft_i2c_readfrom(mp_obj_base_t *self_in, uint16_t addr, uint8_t *dest, size_t len, bool stop);
|
||||
int mp_machine_soft_i2c_writeto(mp_obj_base_t *self_in, uint16_t addr, const uint8_t *src, size_t len, bool stop);
|
||||
|
||||
#endif // MICROPY_INCLUDED_EXTMOD_MACHINE_I2C_H
|
@ -1,286 +0,0 @@
|
||||
/*
|
||||
* This file is part of the MicroPython project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2016 Damien P. George
|
||||
*
|
||||
* 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/runtime.h"
|
||||
#include "extmod/machine_spi.h"
|
||||
|
||||
#include "supervisor/shared/translate.h"
|
||||
|
||||
#if MICROPY_PY_MACHINE_SPI
|
||||
|
||||
// if a port didn't define MSB/LSB constants then provide them
|
||||
#ifndef MICROPY_PY_MACHINE_SPI_MSB
|
||||
#define MICROPY_PY_MACHINE_SPI_MSB (0)
|
||||
#define MICROPY_PY_MACHINE_SPI_LSB (1)
|
||||
#endif
|
||||
|
||||
/******************************************************************************/
|
||||
// MicroPython bindings for generic machine.SPI
|
||||
|
||||
STATIC mp_obj_t mp_machine_soft_spi_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
||||
|
||||
mp_obj_t mp_machine_spi_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
|
||||
// check the id argument, if given
|
||||
if (n_args > 0) {
|
||||
if (args[0] != MP_OBJ_NEW_SMALL_INT(-1)) {
|
||||
#if defined(MICROPY_PY_MACHINE_SPI_MAKE_NEW)
|
||||
// dispatch to port-specific constructor
|
||||
extern mp_obj_t MICROPY_PY_MACHINE_SPI_MAKE_NEW(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
||||
return MICROPY_PY_MACHINE_SPI_MAKE_NEW(type, n_args, args, kw_args);
|
||||
#else
|
||||
mp_raise_ValueError(translate("invalid SPI peripheral"));
|
||||
#endif
|
||||
}
|
||||
--n_args;
|
||||
++args;
|
||||
}
|
||||
|
||||
// software SPI
|
||||
return mp_machine_soft_spi_make_new(type, n_args, args, kw_args);
|
||||
}
|
||||
|
||||
STATIC mp_obj_t machine_spi_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
|
||||
mp_obj_base_t *s = (mp_obj_base_t*)MP_OBJ_TO_PTR(args[0]);
|
||||
mp_machine_spi_p_t *spi_p = (mp_machine_spi_p_t*)mp_proto_get(QSTR_protocol_spi, s);
|
||||
spi_p->init(s, n_args - 1, args + 1, kw_args);
|
||||
return mp_const_none;
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_spi_init_obj, 1, machine_spi_init);
|
||||
|
||||
STATIC mp_obj_t machine_spi_deinit(mp_obj_t self) {
|
||||
mp_obj_base_t *s = (mp_obj_base_t*)MP_OBJ_TO_PTR(self);
|
||||
mp_machine_spi_p_t *spi_p = (mp_machine_spi_p_t*)mp_proto_get(QSTR_protocol_spi, s);
|
||||
if (spi_p->deinit != NULL) {
|
||||
spi_p->deinit(s);
|
||||
}
|
||||
return mp_const_none;
|
||||
}
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_spi_deinit_obj, machine_spi_deinit);
|
||||
|
||||
STATIC void mp_machine_spi_transfer(mp_obj_t self, size_t len, const void *src, void *dest) {
|
||||
mp_obj_base_t *s = (mp_obj_base_t*)MP_OBJ_TO_PTR(self);
|
||||
mp_machine_spi_p_t *spi_p = (mp_machine_spi_p_t*)mp_proto_get(QSTR_protocol_spi, s);
|
||||
spi_p->transfer(s, len, src, dest);
|
||||
}
|
||||
|
||||
STATIC mp_obj_t mp_machine_spi_read(size_t n_args, const mp_obj_t *args) {
|
||||
vstr_t vstr;
|
||||
vstr_init_len(&vstr, mp_obj_get_int(args[1]));
|
||||
memset(vstr.buf, n_args == 3 ? mp_obj_get_int(args[2]) : 0, vstr.len);
|
||||
mp_machine_spi_transfer(args[0], vstr.len, vstr.buf, vstr.buf);
|
||||
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_machine_spi_read_obj, 2, 3, mp_machine_spi_read);
|
||||
|
||||
STATIC mp_obj_t mp_machine_spi_readinto(size_t n_args, const mp_obj_t *args) {
|
||||
mp_buffer_info_t bufinfo;
|
||||
mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_WRITE);
|
||||
memset(bufinfo.buf, n_args == 3 ? mp_obj_get_int(args[2]) : 0, bufinfo.len);
|
||||
mp_machine_spi_transfer(args[0], bufinfo.len, bufinfo.buf, bufinfo.buf);
|
||||
return mp_const_none;
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_machine_spi_readinto_obj, 2, 3, mp_machine_spi_readinto);
|
||||
|
||||
STATIC mp_obj_t mp_machine_spi_write(mp_obj_t self, mp_obj_t wr_buf) {
|
||||
mp_buffer_info_t src;
|
||||
mp_get_buffer_raise(wr_buf, &src, MP_BUFFER_READ);
|
||||
mp_machine_spi_transfer(self, src.len, (const uint8_t*)src.buf, NULL);
|
||||
return mp_const_none;
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_2(mp_machine_spi_write_obj, mp_machine_spi_write);
|
||||
|
||||
STATIC mp_obj_t mp_machine_spi_write_readinto(mp_obj_t self, mp_obj_t wr_buf, mp_obj_t rd_buf) {
|
||||
mp_buffer_info_t src;
|
||||
mp_get_buffer_raise(wr_buf, &src, MP_BUFFER_READ);
|
||||
mp_buffer_info_t dest;
|
||||
mp_get_buffer_raise(rd_buf, &dest, MP_BUFFER_WRITE);
|
||||
if (src.len != dest.len) {
|
||||
mp_raise_ValueError(translate("buffers must be the same length"));
|
||||
}
|
||||
mp_machine_spi_transfer(self, src.len, src.buf, dest.buf);
|
||||
return mp_const_none;
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_3(mp_machine_spi_write_readinto_obj, mp_machine_spi_write_readinto);
|
||||
|
||||
STATIC const mp_rom_map_elem_t machine_spi_locals_dict_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_spi_init_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_spi_deinit_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_machine_spi_read_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&mp_machine_spi_readinto_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_machine_spi_write_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_write_readinto), MP_ROM_PTR(&mp_machine_spi_write_readinto_obj) },
|
||||
|
||||
{ MP_ROM_QSTR(MP_QSTR_MSB), MP_ROM_INT(MICROPY_PY_MACHINE_SPI_MSB) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_LSB), MP_ROM_INT(MICROPY_PY_MACHINE_SPI_LSB) },
|
||||
};
|
||||
|
||||
MP_DEFINE_CONST_DICT(mp_machine_spi_locals_dict, machine_spi_locals_dict_table);
|
||||
|
||||
/******************************************************************************/
|
||||
// Implementation of soft SPI
|
||||
|
||||
STATIC uint32_t baudrate_from_delay_half(uint32_t delay_half) {
|
||||
#ifdef MICROPY_HW_SOFTSPI_MIN_DELAY
|
||||
if (delay_half == MICROPY_HW_SOFTSPI_MIN_DELAY) {
|
||||
return MICROPY_HW_SOFTSPI_MAX_BAUDRATE;
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
return 500000 / delay_half;
|
||||
}
|
||||
}
|
||||
|
||||
STATIC uint32_t baudrate_to_delay_half(uint32_t baudrate) {
|
||||
#ifdef MICROPY_HW_SOFTSPI_MIN_DELAY
|
||||
if (baudrate >= MICROPY_HW_SOFTSPI_MAX_BAUDRATE) {
|
||||
return MICROPY_HW_SOFTSPI_MIN_DELAY;
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
uint32_t delay_half = 500000 / baudrate;
|
||||
// round delay_half up so that: actual_baudrate <= requested_baudrate
|
||||
if (500000 % baudrate != 0) {
|
||||
delay_half += 1;
|
||||
}
|
||||
return delay_half;
|
||||
}
|
||||
}
|
||||
|
||||
STATIC void mp_machine_soft_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
||||
mp_machine_soft_spi_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
||||
mp_printf(print, "SoftSPI(baudrate=%u, polarity=%u, phase=%u,"
|
||||
" sck=" MP_HAL_PIN_FMT ", mosi=" MP_HAL_PIN_FMT ", miso=" MP_HAL_PIN_FMT ")",
|
||||
baudrate_from_delay_half(self->spi.delay_half), self->spi.polarity, self->spi.phase,
|
||||
mp_hal_pin_name(self->spi.sck), mp_hal_pin_name(self->spi.mosi), mp_hal_pin_name(self->spi.miso));
|
||||
}
|
||||
|
||||
STATIC mp_obj_t mp_machine_soft_spi_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *all_args, mp_map_t *kw_args) {
|
||||
enum { ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits, ARG_firstbit, ARG_sck, ARG_mosi, ARG_miso };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_baudrate, MP_ARG_INT, {.u_int = 500000} },
|
||||
{ MP_QSTR_polarity, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
|
||||
{ MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
|
||||
{ MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
|
||||
{ MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = MICROPY_PY_MACHINE_SPI_MSB} },
|
||||
{ MP_QSTR_sck, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
|
||||
{ MP_QSTR_mosi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
|
||||
{ MP_QSTR_miso, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
|
||||
};
|
||||
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
||||
mp_arg_parse_all(n_args, all_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
||||
|
||||
// create new object
|
||||
mp_machine_soft_spi_obj_t *self = m_new_obj(mp_machine_soft_spi_obj_t);
|
||||
self->base.type = &mp_machine_soft_spi_type;
|
||||
|
||||
// set parameters
|
||||
self->spi.delay_half = baudrate_to_delay_half(args[ARG_baudrate].u_int);
|
||||
self->spi.polarity = args[ARG_polarity].u_int;
|
||||
self->spi.phase = args[ARG_phase].u_int;
|
||||
if (args[ARG_bits].u_int != 8) {
|
||||
mp_raise_ValueError(translate("bits must be 8"));
|
||||
}
|
||||
if (args[ARG_firstbit].u_int != MICROPY_PY_MACHINE_SPI_MSB) {
|
||||
mp_raise_ValueError(translate("firstbit must be MSB"));
|
||||
}
|
||||
if (args[ARG_sck].u_obj == MP_OBJ_NULL
|
||||
|| args[ARG_mosi].u_obj == MP_OBJ_NULL
|
||||
|| args[ARG_miso].u_obj == MP_OBJ_NULL) {
|
||||
mp_raise_ValueError(translate("must specify all of sck/mosi/miso"));
|
||||
}
|
||||
self->spi.sck = mp_hal_get_pin_obj(args[ARG_sck].u_obj);
|
||||
self->spi.mosi = mp_hal_get_pin_obj(args[ARG_mosi].u_obj);
|
||||
self->spi.miso = mp_hal_get_pin_obj(args[ARG_miso].u_obj);
|
||||
|
||||
// configure bus
|
||||
mp_soft_spi_ioctl(&self->spi, MP_SPI_IOCTL_INIT);
|
||||
|
||||
return MP_OBJ_FROM_PTR(self);
|
||||
}
|
||||
|
||||
STATIC void mp_machine_soft_spi_init(mp_obj_base_t *self_in, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
||||
mp_machine_soft_spi_obj_t *self = (mp_machine_soft_spi_obj_t*)self_in;
|
||||
|
||||
enum { ARG_baudrate, ARG_polarity, ARG_phase, ARG_sck, ARG_mosi, ARG_miso };
|
||||
static const mp_arg_t allowed_args[] = {
|
||||
{ MP_QSTR_baudrate, MP_ARG_INT, {.u_int = -1} },
|
||||
{ MP_QSTR_polarity, MP_ARG_INT, {.u_int = -1} },
|
||||
{ MP_QSTR_phase, MP_ARG_INT, {.u_int = -1} },
|
||||
{ MP_QSTR_sck, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
|
||||
{ MP_QSTR_mosi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
|
||||
{ MP_QSTR_miso, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
|
||||
};
|
||||
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);
|
||||
|
||||
if (args[ARG_baudrate].u_int != -1) {
|
||||
self->spi.delay_half = baudrate_to_delay_half(args[ARG_baudrate].u_int);
|
||||
}
|
||||
if (args[ARG_polarity].u_int != -1) {
|
||||
self->spi.polarity = args[ARG_polarity].u_int;
|
||||
}
|
||||
if (args[ARG_phase].u_int != -1) {
|
||||
self->spi.phase = args[ARG_phase].u_int;
|
||||
}
|
||||
if (args[ARG_sck].u_obj != MP_OBJ_NULL) {
|
||||
self->spi.sck = mp_hal_get_pin_obj(args[ARG_sck].u_obj);
|
||||
}
|
||||
if (args[ARG_mosi].u_obj != MP_OBJ_NULL) {
|
||||
self->spi.mosi = mp_hal_get_pin_obj(args[ARG_mosi].u_obj);
|
||||
}
|
||||
if (args[ARG_miso].u_obj != MP_OBJ_NULL) {
|
||||
self->spi.miso = mp_hal_get_pin_obj(args[ARG_miso].u_obj);
|
||||
}
|
||||
|
||||
// configure bus
|
||||
mp_soft_spi_ioctl(&self->spi, MP_SPI_IOCTL_INIT);
|
||||
}
|
||||
|
||||
STATIC void mp_machine_soft_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint8_t *src, uint8_t *dest) {
|
||||
mp_machine_soft_spi_obj_t *self = (mp_machine_soft_spi_obj_t*)self_in;
|
||||
mp_soft_spi_transfer(&self->spi, len, src, dest);
|
||||
}
|
||||
|
||||
const mp_machine_spi_p_t mp_machine_soft_spi_p = {
|
||||
MP_PROTO_IMPLEMENT(MP_QSTR_protocol_spi)
|
||||
.init = mp_machine_soft_spi_init,
|
||||
.deinit = NULL,
|
||||
.transfer = mp_machine_soft_spi_transfer,
|
||||
};
|
||||
|
||||
const mp_obj_type_t mp_machine_soft_spi_type = {
|
||||
{ &mp_type_type },
|
||||
.name = MP_QSTR_SoftSPI,
|
||||
.print = mp_machine_soft_spi_print,
|
||||
.make_new = mp_machine_spi_make_new, // delegate to master constructor
|
||||
.protocol = &mp_machine_soft_spi_p,
|
||||
.locals_dict = (mp_obj_dict_t*)&mp_machine_spi_locals_dict,
|
||||
};
|
||||
|
||||
#endif // MICROPY_PY_MACHINE_SPI
|
@ -1,58 +0,0 @@
|
||||
/*
|
||||
* This file is part of the MicroPython project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2016 Damien P. George
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MICROPY_INCLUDED_EXTMOD_MACHINE_SPI_H
|
||||
#define MICROPY_INCLUDED_EXTMOD_MACHINE_SPI_H
|
||||
|
||||
#include "py/obj.h"
|
||||
#include "py/proto.h"
|
||||
#include "py/mphal.h"
|
||||
#include "drivers/bus/spi.h"
|
||||
|
||||
// SPI protocol
|
||||
typedef struct _mp_machine_spi_p_t {
|
||||
MP_PROTOCOL_HEAD
|
||||
void (*init)(mp_obj_base_t *obj, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
|
||||
void (*deinit)(mp_obj_base_t *obj); // can be NULL
|
||||
void (*transfer)(mp_obj_base_t *obj, size_t len, const uint8_t *src, uint8_t *dest);
|
||||
} mp_machine_spi_p_t;
|
||||
|
||||
typedef struct _mp_machine_soft_spi_obj_t {
|
||||
mp_obj_base_t base;
|
||||
mp_soft_spi_obj_t spi;
|
||||
} mp_machine_soft_spi_obj_t;
|
||||
|
||||
extern const mp_machine_spi_p_t mp_machine_soft_spi_p;
|
||||
extern const mp_obj_type_t mp_machine_soft_spi_type;
|
||||
extern const mp_obj_dict_t mp_machine_spi_locals_dict;
|
||||
|
||||
mp_obj_t mp_machine_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args);
|
||||
|
||||
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_machine_spi_read_obj);
|
||||
MP_DECLARE_CONST_FUN_OBJ_VAR_BETWEEN(mp_machine_spi_readinto_obj);
|
||||
MP_DECLARE_CONST_FUN_OBJ_2(mp_machine_spi_write_obj);
|
||||
MP_DECLARE_CONST_FUN_OBJ_3(mp_machine_spi_write_readinto_obj);
|
||||
|
||||
#endif // MICROPY_INCLUDED_EXTMOD_MACHINE_SPI_H
|
@ -161,8 +161,6 @@ PY_EXTMOD_O_BASENAME += \
|
||||
extmod/machine_pinbase.o \
|
||||
extmod/machine_signal.o \
|
||||
extmod/machine_pulse.o \
|
||||
extmod/machine_i2c.o \
|
||||
extmod/machine_spi.o
|
||||
|
||||
LIB_SRC_C = $(addprefix lib/,\
|
||||
$(LIB_SRC_C_EXTRA) \
|
||||
|
Loading…
Reference in New Issue
Block a user