Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions ports/psoc-edge/machine_pin.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include "py/obj.h"
#include "py/runtime.h"
#include "py/mphal.h"

#include "cy_gpio.h"

Expand All @@ -37,7 +38,7 @@
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT(msg), ret); \
}

static uint8_t pin_get_mode(const machine_pin_obj_t *self) {
uint8_t pin_get_mode(const machine_pin_obj_t *self) {
uint32_t drive_mode = Cy_GPIO_GetDrivemode(Cy_GPIO_PortToAddr(self->port), self->pin);

switch (drive_mode) {
Expand Down Expand Up @@ -291,9 +292,36 @@ static void machine_pin_print(const mp_print_t *print, mp_obj_t self_in, mp_prin
}

static mp_obj_t machine_pin_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
// TODO: Placeholder.
mp_arg_check_num(n_args, n_kw, 0, 1, false);
machine_pin_obj_t *self = MP_OBJ_TO_PTR(self_in);
if (n_args == 0) {
// get pin
return MP_OBJ_NEW_SMALL_INT(mp_hal_pin_read(self));
} else {
// set pin
mp_hal_pin_write(self, mp_obj_is_true(args[0]));
return mp_const_none;
}
}

static mp_obj_t machine_pin_value(size_t n_args, const mp_obj_t *args) {
return machine_pin_call(args[0], n_args - 1, 0, args + 1);
}
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_pin_value_obj, 1, 2, machine_pin_value);

static mp_obj_t machine_pin_off(mp_obj_t self_in) {
machine_pin_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_hal_pin_low(self);
return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_off_obj, machine_pin_off);

static mp_obj_t machine_pin_on(mp_obj_t self_in) {
machine_pin_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_hal_pin_high(self);
return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_on_obj, machine_pin_on);

static mp_uint_t pin_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, int *errcode) {
// TODO: Placeholder.
Expand Down Expand Up @@ -321,6 +349,9 @@ MP_DEFINE_CONST_OBJ_TYPE(
static const mp_rom_map_elem_t machine_pin_locals_dict_table[] = {
// Instance methods
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_pin_obj_init_obj) },
{ MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&machine_pin_value_obj) },
{ MP_ROM_QSTR(MP_QSTR_off), MP_ROM_PTR(&machine_pin_off_obj) },
{ MP_ROM_QSTR(MP_QSTR_on), MP_ROM_PTR(&machine_pin_on_obj) },

// class attributes
#if MICROPY_PY_MACHINE_PIN_BOARD_NUM_ENTRIES > 0
Expand Down
7 changes: 7 additions & 0 deletions ports/psoc-edge/mphalport.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,14 @@ void mp_hal_pin_config(mp_hal_pin_obj_t pin, uint32_t mode, uint32_t pull) {
Cy_GPIO_Pin_FastInit(Cy_GPIO_PortToAddr(pin->port), pin->pin, drive_mode, 0, HSIOM_SEL_GPIO);
}

extern uint8_t pin_get_mode(const machine_pin_obj_t *self);

uint32_t mp_hal_pin_read(mp_hal_pin_obj_t pin) {
uint8_t mode = pin_get_mode(pin);
if (mode == GPIO_MODE_OUT ||
mode == GPIO_MODE_OPEN_DRAIN) {
return Cy_GPIO_ReadOut(Cy_GPIO_PortToAddr(pin->port), pin->pin);
}
return Cy_GPIO_Read(Cy_GPIO_PortToAddr(pin->port), pin->pin);
}

Expand Down
44 changes: 44 additions & 0 deletions tests/ports/psoc-edge/board_ext_hw/single/pin.py
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could already also test here what is returned when a pin is initialized as output and then you do pin.value(). This was a feedback from the MPY owners that in our port it is "undefined" while they would have expected a "1".

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. I will add it 👍. Actually, the PDL supports that (not sure about the psoc6 ....).

Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,47 @@
pin_in = Pin.cpu.P16_1
pin_in.init(mode=Pin.IN, pull=Pin.PULL_UP)
print(pin_in)

# Validating initialization values
print("pin out initial value 1: ", pin_in.value() == 1)

pin_out = Pin(pin_out_name, mode=Pin.OUT, value=False)
print("pin out initial value 0: ", pin_in.value() == 0)

# Validation different output setting
pin_out.value(1)
print("pin out (self) value 1: ", pin_out.value() == 1)
print("pin out value 1: ", pin_in.value() == 1)

pin_out.value(0)
print("pin out (self) value 0: ", pin_out.value() == 0)
print("pin out value 0: ", pin_in.value() == 0)

pin_out.value(True)
print("pin out value True: ", pin_in.value() == True)

pin_out.value(False)
print("pin out value False: ", pin_in.value() == False)

pin_out.on()
print("pin out value on: ", pin_in.value() == 1)

pin_out.off()
print("pin out value off: ", pin_in.value() == 0)

pin_out(1)
print("pin out callable 1: ", pin_in() == 1)

pin_out(0)
print("pin out callable 0: ", pin_in() == 0)

# Validating pull resistors configurations and open drain mode
pin_out = Pin(pin_out_name, mode=Pin.OUT, pull=Pin.PULL_UP)
print("pin out with pull up initially high: ", pin_in() == 1)

# This does not work in the test, but does manually for P16_0 - P16_1
# pin_out = Pin(pin_out_name, mode=Pin.OUT, pull=Pin.PULL_DOWN)
# print("pin out with pull down initially down: ", pin_in() == 0)

pin_out = Pin(pin_out_name, mode=Pin.OPEN_DRAIN)
print("pin out with pull none initially 0: ", pin_in() == 0)
14 changes: 14 additions & 0 deletions tests/ports/psoc-edge/board_ext_hw/single/pin.py.exp
Original file line number Diff line number Diff line change
@@ -1,2 +1,16 @@
Pin(Pin.cpu.P16_0, mode=Pin.OUT)
Pin(Pin.cpu.P16_1, mode=Pin.IN, pull=Pin.PULL_UP)
pin out initial value 1: True
pin out initial value 0: True
pin out (self) value 1: True
pin out value 1: True
pin out (self) value 0: True
pin out value 0: True
pin out value True: True
pin out value False: True
pin out value on: True
pin out value off: True
pin out callable 1: True
pin out callable 0: True
pin out with pull up initially high: True
pin out with pull none initially 0: True
Loading