Browse Source

esp8266: Implement Pin.__call__() and Pin.OPEN_DRAIN mode.

OPEN_DRAIN is of course synthesised.  All pin modes are tested and
working.
pull/1867/head
Damien George 9 years ago
committed by Paul Sokolovsky
parent
commit
cdad2b6f4d
  1. 103
      esp8266/modpybpin.c
  2. 4
      esp8266/qstrdefsport.h

103
esp8266/modpybpin.c

@ -38,6 +38,7 @@
#define GPIO_MODE_INPUT (0)
#define GPIO_MODE_OUTPUT (1)
#define GPIO_MODE_OPEN_DRAIN (2) // synthesised
#define GPIO_PULL_NONE (0)
#define GPIO_PULL_UP (1)
// Removed in SDK 1.1.0
@ -66,6 +67,42 @@ STATIC const pyb_pin_obj_t pyb_pin_obj[] = {
{{&pyb_pin_type}, 15, 15, PERIPHS_IO_MUX_MTDO_U, FUNC_GPIO15},
};
STATIC uint8_t pin_mode[16];
STATIC void pin_set(uint pin, uint mode, int value) {
uint32_t enable = 0;
uint32_t disable = 0;
switch (mode) {
case GPIO_MODE_INPUT:
value = -1;
disable = 1;
break;
case GPIO_MODE_OUTPUT:
enable = 1;
break;
case GPIO_MODE_OPEN_DRAIN:
if (value == -1) {
return;
} else if (value == 0) {
enable = 1;
} else {
value = -1;
disable = 1;
}
break;
}
enable <<= pin;
disable <<= pin;
if (value == -1) {
gpio_output_set(0, 0, enable, disable);
} else {
gpio_output_set(value << pin, (1 - value) << pin, enable, disable);
}
}
STATIC void pyb_pin_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
pyb_pin_obj_t *self = self_in;
@ -77,18 +114,33 @@ STATIC void pyb_pin_print(const mp_print_t *print, mp_obj_t self_in, mp_print_ki
STATIC mp_obj_t pyb_pin_obj_init_helper(pyb_pin_obj_t *self, mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_mode, MP_ARG_REQUIRED | MP_ARG_INT },
{ MP_QSTR_pull, MP_ARG_INT, {.u_int = GPIO_PULL_NONE}},
{ MP_QSTR_pull, MP_ARG_INT, {.u_int = GPIO_PULL_NONE}},
{ MP_QSTR_value, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL}},
};
// parse args
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);
struct {
mp_arg_val_t mode, pull, value;
} args;
mp_arg_parse_all(n_args, pos_args, kw_args,
MP_ARRAY_SIZE(allowed_args), allowed_args, (mp_arg_val_t*)&args);
// get io mode
uint mode = args[0].u_int;
uint mode = args.mode.u_int;
// get pull mode
uint pull = args[1].u_int;
uint pull = args.pull.u_int;
// get initial value
int value;
if (args.value.u_obj == MP_OBJ_NULL) {
value = -1;
} else {
value = mp_obj_is_true(args.value.u_obj);
}
// save the mode
pin_mode[self->phys_port] = mode;
// configure the GPIO as requested
PIN_FUNC_SELECT(self->periph, self->func);
@ -110,10 +162,7 @@ STATIC mp_obj_t pyb_pin_obj_init_helper(pyb_pin_obj_t *self, mp_uint_t n_args, c
PIN_PULLUP_EN(self->periph);
}
// TODO input mode is not working...
if ((mode & GPIO_MODE_OUTPUT) == 0) {
GPIO_DIS_OUTPUT(self->phys_port);
}
pin_set(self->phys_port, mode, value);
return mp_const_none;
}
@ -145,6 +194,20 @@ STATIC mp_obj_t pyb_pin_make_new(const mp_obj_type_t *type, mp_uint_t n_args, mp
return (mp_obj_t)pin;
}
// fast method for getting/setting pin value
STATIC mp_obj_t pyb_pin_call(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
mp_arg_check_num(n_args, n_kw, 0, 1, false);
pyb_pin_obj_t *self = self_in;
if (n_args == 0) {
// get pin
return MP_OBJ_NEW_SMALL_INT(GPIO_INPUT_GET(self->phys_port));
} else {
// set pin
pin_set(self->phys_port, pin_mode[self->phys_port], mp_obj_is_true(args[0]));
return mp_const_none;
}
}
// pin.init(mode, pull)
STATIC mp_obj_t pyb_pin_obj_init(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
return pyb_pin_obj_init_helper(args[0], n_args - 1, args + 1, kw_args);
@ -153,26 +216,14 @@ MP_DEFINE_CONST_FUN_OBJ_KW(pyb_pin_init_obj, 1, pyb_pin_obj_init);
// pin.value([value])
STATIC mp_obj_t pyb_pin_value(mp_uint_t n_args, const mp_obj_t *args) {
pyb_pin_obj_t *self = args[0];
if (n_args == 1) {
// get pin
return MP_OBJ_NEW_SMALL_INT(GPIO_INPUT_GET(self->phys_port));
} else {
// set pin
if (mp_obj_is_true(args[1])) {
GPIO_OUTPUT_SET(self->phys_port, 1);
} else {
GPIO_OUTPUT_SET(self->phys_port, 0);
}
return mp_const_none;
}
return pyb_pin_call(args[0], n_args - 1, 0, args + 1);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_pin_value_obj, 1, 2, pyb_pin_value);
// pin.low()
STATIC mp_obj_t pyb_pin_low(mp_obj_t self_in) {
pyb_pin_obj_t *self = self_in;
GPIO_OUTPUT_SET(self->phys_port, 0);
pin_set(self->phys_port, pin_mode[self->phys_port], 0);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_pin_low_obj, pyb_pin_low);
@ -180,7 +231,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_pin_low_obj, pyb_pin_low);
// pin.high()
STATIC mp_obj_t pyb_pin_high(mp_obj_t self_in) {
pyb_pin_obj_t *self = self_in;
GPIO_OUTPUT_SET(self->phys_port, 1);
pin_set(self->phys_port, pin_mode[self->phys_port], 1);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_pin_high_obj, pyb_pin_high);
@ -194,7 +245,8 @@ STATIC const mp_map_elem_t pyb_pin_locals_dict_table[] = {
// class constants
{ MP_OBJ_NEW_QSTR(MP_QSTR_IN), MP_OBJ_NEW_SMALL_INT(GPIO_MODE_INPUT) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_OUT_PP), MP_OBJ_NEW_SMALL_INT(GPIO_MODE_OUTPUT) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_OUT), MP_OBJ_NEW_SMALL_INT(GPIO_MODE_OUTPUT) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_OPEN_DRAIN), MP_OBJ_NEW_SMALL_INT(GPIO_MODE_OPEN_DRAIN) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_PULL_NONE), MP_OBJ_NEW_SMALL_INT(GPIO_PULL_NONE) },
{ MP_OBJ_NEW_QSTR(MP_QSTR_PULL_UP), MP_OBJ_NEW_SMALL_INT(GPIO_PULL_UP) },
//{ MP_OBJ_NEW_QSTR(MP_QSTR_PULL_DOWN), MP_OBJ_NEW_SMALL_INT(GPIO_PULL_DOWN) },
@ -207,5 +259,6 @@ const mp_obj_type_t pyb_pin_type = {
.name = MP_QSTR_Pin,
.print = pyb_pin_print,
.make_new = pyb_pin_make_new,
.call = pyb_pin_call,
.locals_dict = (mp_obj_t)&pyb_pin_locals_dict,
};

4
esp8266/qstrdefsport.h

@ -120,8 +120,8 @@ Q(value)
Q(low)
Q(high)
Q(IN)
Q(OUT_PP)
Q(OUT_OD)
Q(OUT)
Q(OPEN_DRAIN)
Q(PULL_NONE)
Q(PULL_UP)
Q(PULL_DOWN)

Loading…
Cancel
Save