nrf5/i2c: Adopting use of extmod/machine_i2c module as base for port's machine i2c module.

This commit is contained in:
Glenn Ruben Bakke 2017-01-30 00:31:00 +01:00
parent bb9cd36314
commit 61c026e51f
3 changed files with 52 additions and 51 deletions

View File

@ -30,22 +30,25 @@
#include "py/nlr.h" #include "py/nlr.h"
#include "py/runtime.h" #include "py/runtime.h"
#include "py/mphal.h" #include "py/mphal.h"
#include "extmod/machine_i2c.h"
#include "i2c.h" #include "i2c.h"
#include "hal_twi.h" #include "hal_twi.h"
#if MICROPY_PY_MACHINE_HW_I2C #if MICROPY_PY_MACHINE_I2C
typedef struct _machine_i2c_obj_t { STATIC const mp_obj_type_t machine_hard_i2c_type;
typedef struct _machine_hard_i2c_obj_t {
mp_obj_base_t base; mp_obj_base_t base;
TWI_HandleTypeDef *i2c; TWI_HandleTypeDef *i2c;
} machine_i2c_obj_t; } machine_hard_i2c_obj_t;
TWI_HandleTypeDef I2CHandle0 = {.instance = NULL, .init.id = 0}; TWI_HandleTypeDef I2CHandle0 = {.instance = NULL, .init.id = 0};
TWI_HandleTypeDef I2CHandle1 = {.instance = NULL, .init.id = 1}; TWI_HandleTypeDef I2CHandle1 = {.instance = NULL, .init.id = 1};
STATIC const machine_i2c_obj_t machine_i2c_obj[] = { STATIC const machine_hard_i2c_obj_t machine_hard_i2c_obj[] = {
{{&machine_i2c_type}, &I2CHandle0}, {{&machine_hard_i2c_type}, &I2CHandle0},
{{&machine_i2c_type}, &I2CHandle1}, {{&machine_hard_i2c_type}, &I2CHandle1},
}; };
void i2c_init0(void) { void i2c_init0(void) {
@ -59,16 +62,16 @@ void i2c_init0(void) {
STATIC int i2c_find(mp_obj_t id) { STATIC int i2c_find(mp_obj_t id) {
// given an integer id // given an integer id
int i2c_id = mp_obj_get_int(id); int i2c_id = mp_obj_get_int(id);
if (i2c_id >= 0 && i2c_id <= MP_ARRAY_SIZE(machine_i2c_obj) if (i2c_id >= 0 && i2c_id <= MP_ARRAY_SIZE(machine_hard_i2c_obj)
&& machine_i2c_obj[i2c_id].i2c != NULL) { && machine_hard_i2c_obj[i2c_id].i2c != NULL) {
return i2c_id; return i2c_id;
} }
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
"I2C(%d) does not exist", i2c_id)); "I2C(%d) does not exist", i2c_id));
} }
STATIC void i2c_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind) { STATIC void machine_hard_i2c_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind) {
machine_i2c_obj_t *self = o; machine_hard_i2c_obj_t *self = o;
mp_printf(print, "I2C(%u, scl=(port=%u, pin=%u), sda=(port=%u, pin=%u))", mp_printf(print, "I2C(%u, scl=(port=%u, pin=%u), sda=(port=%u, pin=%u))",
self->i2c->init.id, self->i2c->init.id,
self->i2c->init.scl_pin->port, self->i2c->init.scl_pin->port,
@ -82,16 +85,18 @@ STATIC void i2c_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind)
// for make_new // for make_new
enum { enum {
ARG_NEW_ID, ARG_NEW_id,
ARG_NEW_SCL, ARG_NEW_scl,
ARG_NEW_SDA, ARG_NEW_sda,
ARG_NEW_freq,
ARG_NEW_timeout,
}; };
STATIC mp_obj_t machine_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { 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) {
static const mp_arg_t allowed_args[] = { static const mp_arg_t allowed_args[] = {
{ MP_QSTR_ID, MP_ARG_OBJ, {.u_obj = MP_OBJ_NEW_SMALL_INT(-1)} }, { ARG_NEW_id, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ ARG_NEW_SCL, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, { ARG_NEW_scl, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ ARG_NEW_SDA, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, { ARG_NEW_sda, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
}; };
// parse args // parse args
@ -99,18 +104,18 @@ STATIC mp_obj_t machine_i2c_make_new(const mp_obj_type_t *type, size_t n_args, s
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
// get static peripheral object // get static peripheral object
int i2c_id = i2c_find(args[ARG_NEW_ID].u_obj); int i2c_id = i2c_find(args[ARG_NEW_id].u_obj);
const machine_i2c_obj_t *self = &machine_i2c_obj[i2c_id]; const machine_hard_i2c_obj_t *self = &machine_hard_i2c_obj[i2c_id];
if (args[ARG_NEW_SCL].u_obj != MP_OBJ_NULL) { if (args[ARG_NEW_scl].u_obj != MP_OBJ_NULL) {
self->i2c->init.scl_pin = args[ARG_NEW_SCL].u_obj; self->i2c->init.scl_pin = args[ARG_NEW_scl].u_obj;
} else { } else {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
"I2C SCL Pin not set")); "I2C SCL Pin not set"));
} }
if (args[ARG_NEW_SDA].u_obj != MP_OBJ_NULL) { if (args[ARG_NEW_sda].u_obj != MP_OBJ_NULL) {
self->i2c->init.sda_pin = args[ARG_NEW_SDA].u_obj; self->i2c->init.sda_pin = args[ARG_NEW_sda].u_obj;
} else { } else {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
"I2C SDA Pin not set")); "I2C SDA Pin not set"));
@ -121,29 +126,30 @@ STATIC mp_obj_t machine_i2c_make_new(const mp_obj_type_t *type, size_t n_args, s
return MP_OBJ_FROM_PTR(self); return MP_OBJ_FROM_PTR(self);
} }
STATIC const mp_map_elem_t pyb_i2c_locals_dict_table[] = { #include <stdio.h>
// instance methods
#if 0 int machine_hard_i2c_readfrom(mp_obj_base_t *self_in, uint16_t addr, uint8_t *dest, size_t len, bool stop) {
{ MP_OBJ_NEW_QSTR(MP_QSTR_scan), (mp_obj_t)&machine_i2c_scan_obj }, printf("machine_hard_i2c_readfrom called\n");
{ MP_OBJ_NEW_QSTR(MP_QSTR_readfrom), (mp_obj_t)&machine_i2c_readfrom_obj }, return 0;
{ MP_OBJ_NEW_QSTR(MP_QSTR_readfrom_into), (mp_obj_t)&machine_i2c_readfrom_into_obj }, }
{ MP_OBJ_NEW_QSTR(MP_QSTR_writeto), (mp_obj_t)&machine_i2c_writeto_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_readfrom_mem), (mp_obj_t)&machine_i2c_readfrom_mem_obj }, int machine_hard_i2c_writeto(mp_obj_base_t *self_in, uint16_t addr, const uint8_t *src, size_t len, bool stop) {
{ MP_OBJ_NEW_QSTR(MP_QSTR_readfrom_mem_into), (mp_obj_t)&machine_i2c_readfrom_mem_into_obj }, printf("machine_hard_i2c_writeto called\n");
{ MP_OBJ_NEW_QSTR(MP_QSTR_writeto_mem), (mp_obj_t)&machine_i2c_writeto_mem_obj }, return 0;
#endif }
STATIC const mp_machine_i2c_p_t machine_hard_i2c_p = {
.readfrom = machine_hard_i2c_readfrom,
.writeto = machine_hard_i2c_writeto,
}; };
STATIC MP_DEFINE_CONST_DICT(pyb_i2c_locals_dict, pyb_i2c_locals_dict_table); STATIC const mp_obj_type_t machine_hard_i2c_type = {
const mp_obj_type_t machine_i2c_type = {
{ &mp_type_type }, { &mp_type_type },
.name = MP_QSTR_I2C, .name = MP_QSTR_I2C,
.print = i2c_print, .print = machine_hard_i2c_print,
.make_new = machine_i2c_make_new, .make_new = machine_hard_i2c_make_new,
#if 0 .protocol = &machine_hard_i2c_p,
.locals_dict = (mp_obj_t)&machine_i2c_locals_dict .locals_dict = (mp_obj_dict_t*)&mp_machine_soft_i2c_locals_dict,
#endif
}; };
#endif // MICROPY_PY_MACHINE_HW_I2C #endif // MICROPY_PY_MACHINE_I2C

View File

@ -172,7 +172,7 @@ STATIC const mp_map_elem_t machine_module_globals_table[] = {
#if MICROPY_PY_MACHINE_HW_SPI #if MICROPY_PY_MACHINE_HW_SPI
{ MP_OBJ_NEW_QSTR(MP_QSTR_SPI), (mp_obj_t)&machine_hard_spi_type }, { MP_OBJ_NEW_QSTR(MP_QSTR_SPI), (mp_obj_t)&machine_hard_spi_type },
#endif #endif
#if MICROPY_PY_MACHINE_HW_I2C #if MICROPY_PY_MACHINE_I2C
{ MP_OBJ_NEW_QSTR(MP_QSTR_I2C), (mp_obj_t)&machine_i2c_type }, { MP_OBJ_NEW_QSTR(MP_QSTR_I2C), (mp_obj_t)&machine_i2c_type },
#endif #endif
#if MICROPY_PY_MACHINE_ADC #if MICROPY_PY_MACHINE_ADC

View File

@ -30,8 +30,6 @@
#include <mpconfigboard.h> #include <mpconfigboard.h>
// options to control how Micro Python is built // options to control how Micro Python is built
// options to control how Micro Python is built
#define MICROPY_ALLOC_PATH_MAX (512) #define MICROPY_ALLOC_PATH_MAX (512)
#define MICROPY_PERSISTENT_CODE_LOAD (0) #define MICROPY_PERSISTENT_CODE_LOAD (0)
#define MICROPY_EMIT_THUMB (0) #define MICROPY_EMIT_THUMB (0)
@ -103,7 +101,8 @@
#define MICROPY_PY_UTIME_MP_HAL (1) #define MICROPY_PY_UTIME_MP_HAL (1)
#define MICROPY_PY_MACHINE (1) #define MICROPY_PY_MACHINE (1)
#define MICROPY_PY_MACHINE_PULSE (0) #define MICROPY_PY_MACHINE_PULSE (0)
#define MICROPY_PY_MACHINE_I2C (0) #define MICROPY_PY_MACHINE_I2C (1)
#define MICROPY_PY_MACHINE_I2C_MAKE_NEW machine_hard_i2c_make_new
#define MICROPY_PY_MACHINE_SPI (0) #define MICROPY_PY_MACHINE_SPI (0)
#define MICROPY_PY_MACHINE_SPI_MIN_DELAY (0) #define MICROPY_PY_MACHINE_SPI_MIN_DELAY (0)
#define MICROPY_PY_FRAMEBUF (0) #define MICROPY_PY_FRAMEBUF (0)
@ -112,10 +111,6 @@
#define MICROPY_PY_MACHINE_ADC (0) #define MICROPY_PY_MACHINE_ADC (0)
#endif #endif
#ifndef MICROPY_PY_MACHINE_HW_I2C
#define MICROPY_PY_MACHINE_HW_I2C (0)
#endif
#ifndef MICROPY_PY_MACHINE_HW_SPI #ifndef MICROPY_PY_MACHINE_HW_SPI
#define MICROPY_PY_MACHINE_HW_SPI (1) #define MICROPY_PY_MACHINE_HW_SPI (1)
#endif #endif