Tracing extmod\machine_signal.c 通过不成熟的实验,发现Pin对象还是能够传入到Signal对象的,现在没有绑定到硬件上而已。 on和off函数是通过mp_virtual_pin_write()函数实现的:
STATIC mp_obj_t signal_on(mp_obj_t self_in) { mp_virtual_pin_write(self_in, 1); return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_1(signal_on_obj, signal_on);
STATIC mp_obj_t signal_off(mp_obj_t self_in) { mp_virtual_pin_write(self_in, 0); return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_1(signal_off_obj, signal_off);
mp_virtual_pin_write() 函数的实现在 extmod/virpin.c 文件
void mp_virtual_pin_write(mp_obj_t pin, int value) {
mp_obj_base_t *s = (mp_obj_base_t *)MP_OBJ_TO_PTR(pin);
mp_pin_p_t *pin_p = (mp_pin_p_t *)s->type->protocol;
pin_p->ioctl(pin, MP_PIN_WRITE, value, NULL);
}
这里调用了mp_pin_p_t中的ioctl函数,它是在virpin.h中声明的,这是哪里定义的呢? 在代码仓库里搜索“mp_pin_p_t”,发现esp系的port中,machine_pin.c中有相关的引用。
STATIC mp_uint_t pin_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, int *errcode) { (void)errcode; machine_pin_obj_t *self = self_in;
switch (request) { case MP_PIN_READ: { return gpio_get_level(self->id); } case MP_PIN_WRITE: { gpio_set_level(self->id, arg); return 0; } } return -1; }
STATIC MP_DEFINE_CONST_DICT(machine_pin_locals_dict, machine_pin_locals_dict_table);
STATIC const mp_pin_p_t pin_pin_p = { .ioctl = pin_ioctl, };
const mp_obj_type_t machine_pin_type = { { &mp_type_type }, .name = MP_QSTR_Pin, .print = machine_pin_print, .make_new = mp_pin_make_new, .call = machine_pin_call, .protocol = &pin_pin_p, .locals_dict = (mp_obj_t)&machine_pin_locals_dict, };
特别注意,machine_pin_type中的一个字段:“ .protocol = &pin_pin_p,”
但是在mm32的machine_pin_type的实现中就没有这个字段。原来如此 !!!
这也解释了为啥代码能编通,但是执行不对,因为这个protocol的字段是有的,只是默认为空而已。编译不会有问题,运行时不报hardfault就谢天谢地了。
在mm32的machine_pin.c中添加并更新代码如下:
/* to support virpin. */
STATIC mp_uint_t pin_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, int *errcode) {
(void)errcode;
machine_pin_obj_t *self = self_in;
switch (request)
{
case MP_PIN_READ:
{
//return gpio_get_level(self->id);
return GPIO_ReadInputDataBit(self->gpio_port, 1u << self->gpio_pin) ? 1u: 0u;
}
case MP_PIN_WRITE:
{
//gpio_set_level(self->id, arg);
GPIO_WriteBit(self->gpio_port, 1u << self->gpio_pin, arg);
return 0;
}
}
return -1;
}
STATIC const mp_pin_p_t pin_pin_p =
{
.ioctl = pin_ioctl,
};
const mp_obj_type_t machine_pin_type =
{
{ &mp_type_type },
.name = MP_QSTR_Pin,
.print = machine_pin_obj_print, /* __repr__(), which would be called by print(<ClassName>). */
.call = machine_pin_obj_call, /* __call__(), which can be called as <ClassName>(). */
.make_new = machine_pin_obj_make_new, /* create new class instance. */
.protocol = &pin_pin_p, /* to support virpin. */
.locals_dict = (mp_obj_dict_t *)&machine_pin_locals_dict,
};
记得要在machine_pin.c中包含extmod/virpin.h 编译通过。
|