Skip to content

Commit 0a88b44

Browse files
committed
zephyr/machine_pin: Implement pin protocol for machine.Signal support.
1 parent e05cb41 commit 0a88b44

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

zephyr/machine_pin.c

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ STATIC mp_obj_t machine_pin_obj_init_helper(machine_pin_obj_t *self, mp_uint_t n
8181
}
8282

8383
// constructor(drv_name, pin, ...)
84-
STATIC mp_obj_t machine_pin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
84+
mp_obj_t mp_pin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
8585
mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
8686

8787
// get the wanted port
@@ -154,6 +154,24 @@ STATIC mp_obj_t machine_pin_high(mp_obj_t self_in) {
154154
}
155155
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_high_obj, machine_pin_high);
156156

157+
STATIC mp_uint_t machine_pin_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, int *errcode) {
158+
(void)errcode;
159+
machine_pin_obj_t *self = self_in;
160+
161+
switch (request) {
162+
case MP_PIN_READ: {
163+
uint32_t pin_val;
164+
gpio_pin_read(self->port, self->pin, &pin_val);
165+
return pin_val;
166+
}
167+
case MP_PIN_WRITE: {
168+
gpio_pin_write(self->port, self->pin, arg);
169+
return 0;
170+
}
171+
}
172+
return -1;
173+
}
174+
157175
STATIC const mp_map_elem_t machine_pin_locals_dict_table[] = {
158176
// instance methods
159177
{ MP_OBJ_NEW_QSTR(MP_QSTR_init), (mp_obj_t)&machine_pin_init_obj },
@@ -170,11 +188,16 @@ STATIC const mp_map_elem_t machine_pin_locals_dict_table[] = {
170188

171189
STATIC MP_DEFINE_CONST_DICT(machine_pin_locals_dict, machine_pin_locals_dict_table);
172190

191+
STATIC const mp_pin_p_t machine_pin_pin_p = {
192+
.ioctl = machine_pin_ioctl,
193+
};
194+
173195
const mp_obj_type_t machine_pin_type = {
174196
{ &mp_type_type },
175197
.name = MP_QSTR_Pin,
176198
.print = machine_pin_print,
177-
.make_new = machine_pin_make_new,
199+
.make_new = mp_pin_make_new,
178200
.call = machine_pin_call,
201+
.protocol = &machine_pin_pin_p,
179202
.locals_dict = (mp_obj_t)&machine_pin_locals_dict,
180203
};

zephyr/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
#define MICROPY_PY_IO (0)
5959
#define MICROPY_PY_MICROPYTHON_MEM_INFO (1)
6060
#define MICROPY_PY_MACHINE (1)
61+
#define MICROPY_PY_MACHINE_PIN_MAKE_NEW mp_pin_make_new
6162
#define MICROPY_MODULE_WEAK_LINKS (1)
6263
#define MICROPY_PY_STRUCT (0)
6364
#ifdef CONFIG_NETWORKING

0 commit comments

Comments
 (0)