Commit 0f3c325a authored by koalo's avatar koalo
Browse files

WIP Support for I2C in MicroPython

Just the basic files created - compiles but no meaningful content is read.
parent e02671f1
......@@ -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',
)
......@@ -13,6 +13,8 @@ modsrc = files(
'modules/sys_display.c',
'modules/utime.c',
'modules/vibra.c',
'modules/modmachine.c',
'modules/machine_i2c.c',
)
#################################
......
#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,
};
/*
* 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);
......@@ -127,3 +127,6 @@ Q(NO_CONTACT)
Q(CHAOS)
Q(COMMUNICATION)
Q(CAMP)
/* umachine and i2c */
Q(umachine)
......@@ -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
......
......@@ -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;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment