From 0a970d28744e46622cfcef156ec8d9871f23dce3 Mon Sep 17 00:00:00 2001 From: boramonideep Date: Fri, 27 Jan 2023 12:36:55 +0100 Subject: [PATCH 1/3] ports/psoc6:added check in pin_find(), added some docu changes, added some changes to test scripts --- docs/psoc6/quickref.rst | 20 +++++++++++--------- ports/psoc6/modules/machine/machine_pin.c | 5 +---- ports/psoc6/modules/machine/pins.c | 5 +++++ ports/psoc6/modules/machine/pins.h | 3 +++ tests/psoc6/i2c_hard.py | 2 -- tests/psoc6/i2c_soft.py | 2 -- 6 files changed, 20 insertions(+), 17 deletions(-) diff --git a/docs/psoc6/quickref.rst b/docs/psoc6/quickref.rst index fe4c9993f83c..79f87fda535c 100644 --- a/docs/psoc6/quickref.rst +++ b/docs/psoc6/quickref.rst @@ -174,30 +174,32 @@ The driver is accessed via :ref:`machine.I2C ` The constructor ^^^^^^^^^^^^^^^ An instance of the :mod:`machine.I2C` class can be created by invoking the constructor with all the -necessary parameters to fully configure the ``I2C``. By invoking the constructor I2C peripheral is +necessary parameters to fully configure the ``I2C``. By invoking the constructor, I2C peripheral is initialized and configured to work in master mode. -i2c = I2C(0,scl='P6_0',sda='P6_1',freq=4000000) +:: + from machine import I2C + i2c = I2C(0,scl='P6_0',sda='P6_1',freq=4000000) -Here id=0 should be passed mandatorily which selects the master mode operation. +Here, ``id=0`` should be passed mandatorily which selects the ``master`` mode operation. :: - from machine import Pin,I2C - i2c = I2C(0) #I2C is initialized & configured with default scl, sda pin & frequency + from machine import I2C + i2c = I2C(0) #I2C is initialized & configured with default scl, sda pin & frequency :: - from machine import Pin,I2C - i2c = I2C(0,scl='P9_0',sda='P9_1',freq=4000000) #I2C is initialised & configured with given scl,sda pins & frequency + from machine import I2C + i2c = I2C(0,scl='P9_0',sda='P9_1',freq=4000000) #I2C is initialised & configured with given scl,sda pins & frequency Methods ^^^^^^^ -All the methods(functions) given in :ref:`machine.I2C ` class:: have been implemented in this port except +All the methods(functions) given in :ref:`machine.I2C ` class have been implemented in this port except: .. method:: I2C.init() -All the initialization & configurations are handled by the constructor.Hence init() is not required +All the initialization & configurations are handled by the constructor. Hence ``init()`` is not required. Real time clock (RTC) diff --git a/ports/psoc6/modules/machine/machine_pin.c b/ports/psoc6/modules/machine/machine_pin.c index 7ae7648249ce..55b340b1ef9d 100644 --- a/ports/psoc6/modules/machine/machine_pin.c +++ b/ports/psoc6/modules/machine/machine_pin.c @@ -220,12 +220,9 @@ mp_obj_t mp_pin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, // object ptr for the class obj instantiated const machine_pin_obj_t *self = NULL; - // get the wanted pin object + // get the wanted pin index int wanted_pin = pin_find(args[0]); - if (!(0 <= wanted_pin && wanted_pin < MP_ARRAY_SIZE(machine_pin_obj))) { - mp_raise_ValueError(MP_ERROR_TEXT("invalid pin: Pin not defined!")); - } // Note: we have different init args based on the type of pin. so Pin("LED", Pin.OUT) may not always make sense // assign machine_pin obj to obj pointer self = &machine_pin_obj[wanted_pin]; diff --git a/ports/psoc6/modules/machine/pins.c b/ports/psoc6/modules/machine/pins.c index 1675648c2f29..ba84f4f17bb1 100644 --- a/ports/psoc6/modules/machine/pins.c +++ b/ports/psoc6/modules/machine/pins.c @@ -21,6 +21,11 @@ int pin_find(mp_obj_t pin) { } } } + + if (!(0 <= wanted_pin && wanted_pin < MP_ARRAY_SIZE(machine_pin_obj))) { + mp_raise_ValueError(MP_ERROR_TEXT("invalid pin: Pin not defined!")); + } + return wanted_pin; } diff --git a/ports/psoc6/modules/machine/pins.h b/ports/psoc6/modules/machine/pins.h index dd7281e65a56..e12c649bc1c4 100644 --- a/ports/psoc6/modules/machine/pins.h +++ b/ports/psoc6/modules/machine/pins.h @@ -8,6 +8,9 @@ // cy includes #include "cyhal.h" +// mpy include +#include "py/runtime.h" + /* Include Pin definitions to make it visible at py side*/ // Generated manually w.r.t cyhal_gpio_psoc6_02_124_bga_t enum for the package present in the CYPROTO-062-4343 target // TODO: automate this generation using a python script, same as other ports. diff --git a/tests/psoc6/i2c_hard.py b/tests/psoc6/i2c_hard.py index 3cba63750b78..f4f6a2d64e55 100644 --- a/tests/psoc6/i2c_hard.py +++ b/tests/psoc6/i2c_hard.py @@ -1,6 +1,4 @@ ### -import machine - from machine import I2C i2c = I2C(id=0, scl="P6_0", sda="P6_1", freq=400000) diff --git a/tests/psoc6/i2c_soft.py b/tests/psoc6/i2c_soft.py index faf6fcbbc19a..d36fb499458e 100644 --- a/tests/psoc6/i2c_soft.py +++ b/tests/psoc6/i2c_soft.py @@ -1,6 +1,4 @@ #### SoftI2C -import machine - from machine import SoftI2C si2c = SoftI2C(scl="P6_0", sda="P6_1", freq=400000) From 0b53be425629a6ed71c2d587f9cc36365e7e4024 Mon Sep 17 00:00:00 2001 From: Jens Bargfrede Date: Mon, 30 Jan 2023 11:21:32 +0100 Subject: [PATCH 2/3] i2c: Updates based on review comments. --- ports/psoc6/Makefile | 1 - ports/psoc6/boards/CY8CPROTO-062-4343W/Makefile | 1 - ports/psoc6/modules/machine/machine_pin.c | 2 +- ports/psoc6/mphalport.c | 6 ------ tests/psoc6/i2c_hard.py | 4 +--- tests/psoc6/i2c_soft.py | 2 -- 6 files changed, 2 insertions(+), 14 deletions(-) diff --git a/ports/psoc6/Makefile b/ports/psoc6/Makefile index 966acc773cd1..5ebde6c6faee 100644 --- a/ports/psoc6/Makefile +++ b/ports/psoc6/Makefile @@ -55,7 +55,6 @@ CFLAGS_CORTEX_M4 = -mthumb -mtune=cortex-m4 -mcpu=cortex-m4 -msoft-float -fsingl # c11 : provides "static_assert" (not available in c99) # -D_XOPEN_SOURCE=700 : makes sure the setenv/unsetenv headers are included -#CFLAGS = $(INC) -Wall $(CFLAGS_CORTEX_M4) $(COPT) -std=c11 -D_XOPEN_SOURCE=700 CFLAGS = $(INC) -Wall -Werror $(CFLAGS_CORTEX_M4) $(COPT) -std=c11 -D_XOPEN_SOURCE=700 -Wno-error=double-promotion -Wno-error=overflow #CFLAGS = $(INC) -Wall -Werror -std=c99 $(CFLAGS_CORTEX_M4) $(COPT) diff --git a/ports/psoc6/boards/CY8CPROTO-062-4343W/Makefile b/ports/psoc6/boards/CY8CPROTO-062-4343W/Makefile index bfff2253ea10..909ce9d36c70 100755 --- a/ports/psoc6/boards/CY8CPROTO-062-4343W/Makefile +++ b/ports/psoc6/boards/CY8CPROTO-062-4343W/Makefile @@ -48,7 +48,6 @@ TARGET=APP_CY8CPROTO-062-4343W # # If APPNAME / LIBNAME is edited, ensure to update or regenerate launch # configurations for your IDE. -#APPNAME=mtb LIBNAME=libmtb # Name of toolchain to use. Options include: diff --git a/ports/psoc6/modules/machine/machine_pin.c b/ports/psoc6/modules/machine/machine_pin.c index 7ae7648249ce..6118c5471bf7 100644 --- a/ports/psoc6/modules/machine/machine_pin.c +++ b/ports/psoc6/modules/machine/machine_pin.c @@ -29,7 +29,7 @@ enum {ARG_mode, ARG_pull, ARG_value, ARG_drive, ARG_alt}; static const mp_arg_t allowed_args[] = { {MP_QSTR_mode, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE}}, {MP_QSTR_pull, MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE}}, - {MP_QSTR_valu, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE}}, + {MP_QSTR_value, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE}}, {MP_QSTR_drive, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE}}, {MP_QSTR_alt, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = HSIOM_GPIO_FUNC}}, // default value of HSIOM set to GPIO mode of pin. }; diff --git a/ports/psoc6/mphalport.c b/ports/psoc6/mphalport.c index 24922572dbb6..7a1c0a736a92 100644 --- a/ports/psoc6/mphalport.c +++ b/ports/psoc6/mphalport.c @@ -93,12 +93,6 @@ int mp_hal_pin_read(mp_hal_pin_obj_t pin) { void mp_hal_pin_open_drain(mp_hal_pin_obj_t pin) { cyhal_gpio_configure(pin, CYHAL_GPIO_DIR_INPUT, CYHAL_GPIO_DRIVE_OPENDRAINDRIVESLOW); - - // printf("2 SDA is CYHAL_GPIO_DRIVE_OPENDRAINDRIVESLOW ? %ld\n", gpio_get_drive(CYBSP_I2C_SDA)); - // printf("2 SCL is CYHAL_GPIO_DRIVE_OPENDRAINDRIVESLOW ? %ld\n", gpio_get_drive(CYBSP_I2C_SCL)); - - // printf("SDA is CYHAL_GPIO_DRIVE_OPENDRAINDRIVESLOW ? %d\n", (gpio_get_drive(CYBSP_I2C_SDA) & 0xFFFFFFF7) == CYHAL_GPIO_DRIVE_OPENDRAINDRIVESLOW); - // printf("SCL is CYHAL_GPIO_DRIVE_OPENDRAINDRIVESLOW ? %d\n", (gpio_get_drive(CYBSP_I2C_SCL) & 0xFFFFFFF7) == CYHAL_GPIO_DRIVE_OPENDRAINDRIVESLOW); } diff --git a/tests/psoc6/i2c_hard.py b/tests/psoc6/i2c_hard.py index 3cba63750b78..07219fbb7bda 100644 --- a/tests/psoc6/i2c_hard.py +++ b/tests/psoc6/i2c_hard.py @@ -1,6 +1,4 @@ -### -import machine - +### I2C from machine import I2C i2c = I2C(id=0, scl="P6_0", sda="P6_1", freq=400000) diff --git a/tests/psoc6/i2c_soft.py b/tests/psoc6/i2c_soft.py index faf6fcbbc19a..d36fb499458e 100644 --- a/tests/psoc6/i2c_soft.py +++ b/tests/psoc6/i2c_soft.py @@ -1,6 +1,4 @@ #### SoftI2C -import machine - from machine import SoftI2C si2c = SoftI2C(scl="P6_0", sda="P6_1", freq=400000) From cf7f06b5e7fa414bd87af3bb0be2458987ca7632 Mon Sep 17 00:00:00 2001 From: Jens Bargfrede Date: Mon, 30 Jan 2023 16:04:08 +0100 Subject: [PATCH 3/3] i2c: Fix changes lost. --- ports/psoc6/Makefile | 1 - 1 file changed, 1 deletion(-) diff --git a/ports/psoc6/Makefile b/ports/psoc6/Makefile index 419c6d2a792c..62089862189d 100644 --- a/ports/psoc6/Makefile +++ b/ports/psoc6/Makefile @@ -57,7 +57,6 @@ CFLAGS_CORTEX_M4 = -mthumb -mtune=cortex-m4 -mcpu=cortex-m4 -msoft-float -fsingl # -D_XOPEN_SOURCE=700 : makes sure the setenv/unsetenv headers are included CFLAGS = $(INC) -Wall -Werror $(CFLAGS_CORTEX_M4) $(COPT) -std=c11 -D_XOPEN_SOURCE=700 -Wno-error=double-promotion -Wno-error=overflow -#CFLAGS = $(INC) -Wall -Werror -std=c99 $(CFLAGS_CORTEX_M4) $(COPT) LDFLAGS = -Wl,--cref -Wl,--gc-sections