diff --git a/lib/micropython/meson.build b/lib/micropython/meson.build index dbd9c8fdbbb09316a37f2905fa83381af6f52421..46893df2563c0e0a596e081cb77de5b6ce78befa 100644 --- a/lib/micropython/meson.build +++ b/lib/micropython/meson.build @@ -194,4 +194,5 @@ micropython_extmod_sources = files( 'micropython/extmod/modure.c', 'micropython/extmod/moduselect.c', 'micropython/extmod/modutimeq.c', + 'micropython/extmod/machine_i2c.c', ) diff --git a/pycardium/meson.build b/pycardium/meson.build index 40fabde700ce03eff4d117b7e5e87c024b5f35f5..e5e14b17877059b6b441c62dd763893aaf0753e9 100644 --- a/pycardium/meson.build +++ b/pycardium/meson.build @@ -13,6 +13,8 @@ modsrc = files( 'modules/sys_display.c', 'modules/utime.c', 'modules/vibra.c', + 'modules/modmachine.c', + 'modules/machine_i2c.c', ) ################################# diff --git a/pycardium/modules/machine_i2c.c b/pycardium/modules/machine_i2c.c new file mode 100644 index 0000000000000000000000000000000000000000..2713d852f7f65b009b4cec94b9ba2ac651559eb8 --- /dev/null +++ b/pycardium/modules/machine_i2c.c @@ -0,0 +1,82 @@ +#include <string.h> + +#include "py/obj.h" +#include "py/objlist.h" +#include "py/runtime.h" +#include "py/mphal.h" + +#include "extmod/machine_i2c.h" +#include "i2c.h" + +#define MICROPY_HW_I2C1_NAME "X" +#define MICROPY_HW_I2C2_NAME "Y" + +STATIC const mp_obj_type_t machine_hard_i2c_type; + +typedef struct _machine_hard_i2c_obj_t { + mp_obj_base_t base; + mxc_i2c_regs_t *i2c; +} machine_hard_i2c_obj_t; + +STATIC const machine_hard_i2c_obj_t machine_hard_i2c_obj[2] = { + [0] = {{&machine_hard_i2c_type}, MXC_I2C1_BUS0}, + [1] = {{&machine_hard_i2c_type}, MXC_I2C1_BUS1}, +}; + + +int machine_hard_i2c_transfer(mp_obj_base_t *self_in, uint16_t addr, size_t n, mp_machine_i2c_buf_t *bufs, unsigned int flags) { + machine_hard_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in); + if (flags & MP_MACHINE_I2C_FLAG_READ) { + return I2C_MasterRead(self->i2c, addr, bufs->buf, bufs->len, !(flags & MP_MACHINE_I2C_FLAG_STOP)); + } else { + return I2C_MasterRead(self->i2c, addr, bufs->buf, bufs->len, !(flags & MP_MACHINE_I2C_FLAG_STOP)); + } +} + + +/******************************************************************************/ +/* MicroPython bindings for machine API */ + +mp_obj_t machine_hard_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { + // parse args + enum { ARG_id }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_OBJ }, + }; + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + // work out i2c bus + int i2c_id = 0; + if (!mp_obj_is_str(args[ARG_id].u_obj)) { + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, + "Provide I2C bus as string")); + } + + const char *port = mp_obj_str_get_str(args[ARG_id].u_obj); + if (strcmp(port, MICROPY_HW_I2C1_NAME) == 0) { + i2c_id = 1; + } else if (strcmp(port, MICROPY_HW_I2C2_NAME) == 0) { + i2c_id = 2; + } else { + nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, + "I2C(%s) doesn't exist", port)); + } + + // get static peripheral object + machine_hard_i2c_obj_t *self = (machine_hard_i2c_obj_t*)&machine_hard_i2c_obj[i2c_id - 1]; + return MP_OBJ_FROM_PTR(self); +} + +STATIC const mp_machine_i2c_p_t machine_hard_i2c_p = { + .transfer = machine_hard_i2c_transfer, +}; + +STATIC const mp_obj_type_t machine_hard_i2c_type = { + { &mp_type_type }, + .name = MP_QSTR_I2C, + .make_new = machine_hard_i2c_make_new, + .protocol = &machine_hard_i2c_p, + .locals_dict = (mp_obj_dict_t*)&mp_machine_soft_i2c_locals_dict, +}; + diff --git a/pycardium/modules/modmachine.c b/pycardium/modules/modmachine.c new file mode 100644 index 0000000000000000000000000000000000000000..3f05e7aca6a198becd1c64db646e4aae41cd41b3 --- /dev/null +++ b/pycardium/modules/modmachine.c @@ -0,0 +1,50 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2013-2015 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/gc.h" +#include "py/runtime.h" +#include "py/objstr.h" +#include "py/mperrno.h" +#include "py/mphal.h" +#include "extmod/machine_i2c.h" +#include "i2c.h" + +STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { + { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_umachine) }, + { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_i2c_type) }, +}; + +STATIC MP_DEFINE_CONST_DICT(machine_module_globals, machine_module_globals_table); + +const mp_obj_module_t machine_module = { + .base = { &mp_type_module }, + .globals = (mp_obj_dict_t*)&machine_module_globals, +}; + +MP_REGISTER_MODULE(MP_QSTR_umachine, machine_module, MODULE_UMACHINE_ENABLED); diff --git a/pycardium/modules/qstrdefs.h b/pycardium/modules/qstrdefs.h index 9e3f3979af79ebbfb16f821b7127424cadb563ff..e54d9cc8de17548d5d7ed19a02948b68dc74951e 100644 --- a/pycardium/modules/qstrdefs.h +++ b/pycardium/modules/qstrdefs.h @@ -127,3 +127,6 @@ Q(NO_CONTACT) Q(CHAOS) Q(COMMUNICATION) Q(CAMP) + +/* umachine and i2c */ +Q(umachine) diff --git a/pycardium/mpconfigport.h b/pycardium/mpconfigport.h index a1c88a7476f0b63111a4e6049aaac33f32b7fa0c..3b753e626c7c328249be0b7660de8b3e0a926756 100644 --- a/pycardium/mpconfigport.h +++ b/pycardium/mpconfigport.h @@ -44,6 +44,16 @@ int mp_hal_trng_read_int(void); #define MICROPY_PY_IO_FILEIO (1) #define MICROPY_PY_UERRNO (1) +/* I2C */ +#define mp_hal_delay_us_fast(us) mp_hal_delay_us(us) +typedef void *mp_obj_t; +int mp_virtual_pin_read(mp_obj_t pin); +#define mp_hal_pin_open_drain(x) +#define mp_hal_pin_od_low(x) +#define mp_hal_pin_od_high(x) +#define MICROPY_PY_MACHINE_I2C (1) +#define MICROPY_PY_MACHINE_I2C_MAKE_NEW machine_hard_i2c_make_new + /* Modules */ #define MODULE_BUTTONS_ENABLED (1) #define MODULE_DISPLAY_ENABLED (1) @@ -55,6 +65,7 @@ int mp_hal_trng_read_int(void); #define MODULE_PERSONAL_STATE_ENABLED (1) #define MODULE_UTIME_ENABLED (1) #define MODULE_VIBRA_ENABLED (1) +#define MODULE_UMACHINE_ENABLED (1) /* * This port is intended to be 32-bit, but unfortunately, int32_t for diff --git a/pycardium/mphalport.c b/pycardium/mphalport.c index 60cd9df0cdf1e08a01ffd3df84772d9c6276e7ac..e3ced4c5779374062e59b44e3ccaec3936693134 100644 --- a/pycardium/mphalport.c +++ b/pycardium/mphalport.c @@ -163,3 +163,11 @@ int mp_hal_trng_read_int(void) epic_trng_read((uint8_t *)&result, sizeof(result)); return result; } + +/****************************************************************************** + * Dummy implementations + */ +int mp_virtual_pin_read(mp_obj_t pin) +{ + return 0; +}