From ff29f699f210f1f641a01a07ff06f5945955ee4f Mon Sep 17 00:00:00 2001 From: enriquezgarc Date: Thu, 12 Oct 2023 16:19:17 +0200 Subject: [PATCH 1/5] ports/psoc6/../machine_pwm.c: Added deinit and pin phy refactor. Signed-off-by: enriquezgarc --- ports/psoc6/main.c | 2 + ports/psoc6/modules/machine/machine_pwm.c | 124 +++++++++++++--------- 2 files changed, 75 insertions(+), 51 deletions(-) diff --git a/ports/psoc6/main.c b/ports/psoc6/main.c index da9916081d54..661c8bd82de2 100644 --- a/ports/psoc6/main.c +++ b/ports/psoc6/main.c @@ -59,6 +59,7 @@ extern void network_deinit(void); extern void mod_pin_deinit(void); extern void mod_adc_block_deinit(void); extern void mod_i2c_deinit(void); +extern void mod_pwm_deinit(void); void mpy_task(void *arg); @@ -181,6 +182,7 @@ void mpy_task(void *arg) { mod_pin_deinit(); mod_adc_block_deinit(); mod_i2c_deinit(); + mod_pwm_deinit(); mod_pin_phy_deinit(); #if MICROPY_PY_NETWORK mod_network_deinit(); diff --git a/ports/psoc6/modules/machine/machine_pwm.c b/ports/psoc6/modules/machine/machine_pwm.c index b649a940a455..5f3f4a5a311c 100644 --- a/ports/psoc6/modules/machine/machine_pwm.c +++ b/ports/psoc6/modules/machine/machine_pwm.c @@ -3,21 +3,59 @@ #include "modmachine.h" // port-specific includes +#include "machine_pin_phy.h" #include "mplogger.h" -extern mp_hal_pin_obj_t mp_hal_get_pin_obj(mp_obj_t obj); +#define MAX_PWM_OBJS 10 // TODO: Derive this from BSP typedef struct _machine_pwm_obj_t { mp_obj_base_t base; cyhal_pwm_t pwm_obj; - bool active; - uint8_t pin; + machine_pin_phy_obj_t *pin; uint32_t fz; uint8_t duty_type; mp_float_t duty; bool invert; } machine_pwm_obj_t; +static machine_pwm_obj_t *pwm_obj[MAX_PWM_OBJS] = { NULL }; + +STATIC inline machine_pwm_obj_t *pwm_obj_alloc() { + for (uint8_t i = 0; i < MAX_PWM_OBJS; i++) + { + if (pwm_obj[i] == NULL) { + pwm_obj[i] = mp_obj_malloc(machine_pwm_obj_t, &machine_pwm_type); + return pwm_obj[i]; + } + } + + return NULL; +} + +STATIC inline void pwm_obj_free(machine_pwm_obj_t *pwm_obj_ptr) { + for (uint8_t i = 0; i < MAX_PWM_OBJS; i++) + { + if (pwm_obj[i] == pwm_obj_ptr) { + pwm_obj[i] = NULL; + } + } +} + +STATIC inline void pwm_pin_alloc(machine_pwm_obj_t *pwm_obj, mp_obj_t pin_name) { + machine_pin_phy_obj_t *pin = pin_phy_realloc(pin_name, PIN_PHY_FUNC_PWM); + + if (pin == NULL) { + size_t slen; + mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("PWM pin (%s) not found !"), mp_obj_str_get_data(pin_name, &slen)); + } + + pwm_obj->pin = pin; +} + +STATIC inline void pwm_pin_free(machine_pwm_obj_t *pwm_obj) { + pin_phy_free(pwm_obj->pin); +} + enum { VALUE_NOT_SET = -1, DUTY_NOT_SET = 0, @@ -25,33 +63,20 @@ enum { DUTY_NS }; -STATIC void mp_machine_pwm_freq_set(machine_pwm_obj_t *self, mp_int_t freq); -STATIC void mp_machine_pwm_duty_set_u16(machine_pwm_obj_t *self, mp_float_t duty_u16); -STATIC void mp_machine_pwm_duty_set_ns(machine_pwm_obj_t *self, mp_float_t duty_ns); - -STATIC cy_rslt_t pwm_freq_duty_set(cyhal_pwm_t *pwm_obj, uint32_t fz, float duty_cycle) { +STATIC inline cy_rslt_t pwm_freq_duty_set(cyhal_pwm_t *pwm_obj, uint32_t fz, float duty_cycle) { return cyhal_pwm_set_duty_cycle(pwm_obj, duty_cycle * 100, fz); // duty_cycle in percentage } -STATIC cy_rslt_t pwm_duty_set_ns(cyhal_pwm_t *pwm_obj, uint32_t fz, uint32_t pulse_width) { +STATIC inline cy_rslt_t pwm_duty_set_ns(cyhal_pwm_t *pwm_obj, uint32_t fz, uint32_t pulse_width) { return cyhal_pwm_set_period(pwm_obj, 1000000 / fz, pulse_width * 1000); } -STATIC cy_rslt_t pwm_advanced_init(machine_pwm_obj_t *machine_pwm_obj) { - return cyhal_pwm_init_adv(&machine_pwm_obj->pwm_obj, machine_pwm_obj->pin, NC, CYHAL_PWM_LEFT_ALIGN, true, 0, true, NULL); // complimentary pin set as not connected -} - - -// To check whether the PWM is active -STATIC void pwm_is_active(machine_pwm_obj_t *self) { - if (self->active == 0) { - mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("PWM inactive")); - } +STATIC inline cy_rslt_t pwm_advanced_init(machine_pwm_obj_t *machine_pwm_obj) { + return cyhal_pwm_init_adv(&machine_pwm_obj->pwm_obj, machine_pwm_obj->pin->addr, NC, CYHAL_PWM_LEFT_ALIGN, true, 0, true, NULL); // complimentary pin set as not connected } STATIC void mp_machine_pwm_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { machine_pwm_obj_t *self = MP_OBJ_TO_PTR(self_in); - pwm_is_active(self_in); mp_printf(print, "frequency=%u duty_cycle=%f invert=%u", self->fz, (double)self->duty, self->invert); } @@ -69,7 +94,7 @@ STATIC void mp_machine_pwm_init_helper(machine_pwm_obj_t *self, 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); - self->active = 1; + // self->active = 1; if ((args[ARG_freq].u_int != VALUE_NOT_SET)) { pwm_freq_duty_set(&self->pwm_obj, args[ARG_freq].u_int, self->duty); @@ -108,20 +133,15 @@ STATIC mp_obj_t mp_machine_pwm_make_new(const mp_obj_type_t *type, size_t n_args // Check number of arguments mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true); - // Get GPIO to connect to PWM. - int pin = mp_hal_get_pin_obj(all_args[0]); - // Get static peripheral object. - machine_pwm_obj_t *self = mp_obj_malloc(machine_pwm_obj_t, &machine_pwm_type); - self->base.type = &machine_pwm_type; - self->pin = pin; - self->active = 0; + machine_pwm_obj_t *self = pwm_obj_alloc(); + pwm_pin_alloc(self, all_args[0]); self->duty_type = DUTY_NOT_SET; self->fz = -1; self->invert = -1; // Initialize PWM - cy_rslt_t result = cyhal_pwm_init(&self->pwm_obj, self->pin, NULL); + cy_rslt_t result = cyhal_pwm_init(&self->pwm_obj, self->pin->addr, NULL); // To check whether PWM init is successful if (result != CY_RSLT_SUCCESS) { @@ -138,28 +158,11 @@ STATIC mp_obj_t mp_machine_pwm_make_new(const mp_obj_type_t *type, size_t n_args STATIC void mp_machine_pwm_deinit(machine_pwm_obj_t *self) { cyhal_pwm_free(&self->pwm_obj); - self->active = 0; -} - - -STATIC mp_obj_t mp_machine_pwm_freq_get(machine_pwm_obj_t *self) { - pwm_is_active(self); - return MP_OBJ_NEW_SMALL_INT(self->fz); - -} - -STATIC void mp_machine_pwm_freq_set(machine_pwm_obj_t *self, mp_int_t freq) { - pwm_is_active(self); - self->fz = freq; - pwm_freq_duty_set(&self->pwm_obj, freq, self->duty); - if (self->duty_type == DUTY_NS) { - self->duty = ((self->duty) * (self->fz) * 65535) / 1000000000; - mp_machine_pwm_duty_set_ns(self, self->duty); - } + pwm_pin_free(self); + pwm_obj_free(self); } STATIC mp_obj_t mp_machine_pwm_duty_get_u16(machine_pwm_obj_t *self) { - pwm_is_active(self); if (self->duty_type == DUTY_NS) { // duty_cycle = pulsewidth(ns)*freq(hz); return mp_obj_new_float(((self->duty) * (self->fz) * 65535) / 1000000000); @@ -170,7 +173,6 @@ STATIC mp_obj_t mp_machine_pwm_duty_get_u16(machine_pwm_obj_t *self) { // sets the duty cycle as a ratio duty_u16 / 65535. STATIC void mp_machine_pwm_duty_set_u16(machine_pwm_obj_t *self, mp_float_t duty_u16) { - pwm_is_active(self); // Check the value is more than the max value self->duty = duty_u16 > 65535 ? 65535 : duty_u16; self->duty_type = DUTY_U16; @@ -178,7 +180,6 @@ STATIC void mp_machine_pwm_duty_set_u16(machine_pwm_obj_t *self, mp_float_t duty } STATIC mp_obj_t mp_machine_pwm_duty_get_ns(machine_pwm_obj_t *self) { - pwm_is_active(self); if (self->duty_type == DUTY_U16) { return mp_obj_new_float(((self->duty) * 1000000000) / ((self->fz) * 65535)); // pw (ns) = duty_cycle*10^9/fz } else { @@ -188,8 +189,29 @@ STATIC mp_obj_t mp_machine_pwm_duty_get_ns(machine_pwm_obj_t *self) { // sets the pulse width in nanoseconds STATIC void mp_machine_pwm_duty_set_ns(machine_pwm_obj_t *self, mp_float_t duty_ns) { - pwm_is_active(self); self->duty = duty_ns; self->duty_type = DUTY_NS; pwm_freq_duty_set(&self->pwm_obj, self->fz, duty_ns); } + +STATIC mp_obj_t mp_machine_pwm_freq_get(machine_pwm_obj_t *self) { + return MP_OBJ_NEW_SMALL_INT(self->fz); + +} + +STATIC void mp_machine_pwm_freq_set(machine_pwm_obj_t *self, mp_int_t freq) { + self->fz = freq; + pwm_freq_duty_set(&self->pwm_obj, freq, self->duty); + if (self->duty_type == DUTY_NS) { + self->duty = ((self->duty) * (self->fz) * 65535) / 1000000000; + mp_machine_pwm_duty_set_ns(self, self->duty); + } +} + +void mod_pwm_deinit() { + for (uint8_t i = 0; i < MAX_PWM_OBJS; i++) { + if (pwm_obj[i] != NULL) { + mp_machine_pwm_deinit(pwm_obj[i]); + } + } +} From 26e0325627b4e4f20acb6b9d3c513821e4a918fa Mon Sep 17 00:00:00 2001 From: enriquezgarc Date: Fri, 13 Oct 2023 09:04:00 +0200 Subject: [PATCH 2/5] ports/psoc6/../machine_spi.c: Added deinit and pin phy refactor. Signed-off-by: enriquezgarc --- ports/psoc6/main.c | 2 + ports/psoc6/modules/machine/machine_spi.c | 133 +++++++++++++++------- 2 files changed, 97 insertions(+), 38 deletions(-) diff --git a/ports/psoc6/main.c b/ports/psoc6/main.c index 661c8bd82de2..734bbfa4c931 100644 --- a/ports/psoc6/main.c +++ b/ports/psoc6/main.c @@ -60,6 +60,7 @@ extern void mod_pin_deinit(void); extern void mod_adc_block_deinit(void); extern void mod_i2c_deinit(void); extern void mod_pwm_deinit(void); +extern void mod_spi_deinit(void); void mpy_task(void *arg); @@ -183,6 +184,7 @@ void mpy_task(void *arg) { mod_adc_block_deinit(); mod_i2c_deinit(); mod_pwm_deinit(); + mod_spi_deinit(); mod_pin_phy_deinit(); #if MICROPY_PY_NETWORK mod_network_deinit(); diff --git a/ports/psoc6/modules/machine/machine_spi.c b/ports/psoc6/modules/machine/machine_spi.c index 9a601c0fadc0..a129dd060c50 100644 --- a/ports/psoc6/modules/machine/machine_spi.c +++ b/ports/psoc6/modules/machine/machine_spi.c @@ -16,12 +16,10 @@ // port-specific includes #include "modmachine.h" +#include "machine_pin_phy.h" #include "mplogger.h" -#define MICROPY_HW_SPI_SCK P9_2 -#define MICROPY_HW_SPI_MOSI P9_0 -#define MICROPY_HW_SPI_MISO P9_1 - +#define MAX_SPI 10 // TODO: Change as per bsp? #define DEFAULT_SPI_BAUDRATE (1000000) #define DEFAULT_SPI_POLARITY (0) @@ -38,12 +36,80 @@ typedef struct _machine_spi_obj_t { uint8_t phase; uint8_t bits; uint8_t firstbit; - uint8_t sck; - uint8_t mosi; - uint8_t miso; + machine_pin_phy_obj_t *sck; + machine_pin_phy_obj_t *mosi; + machine_pin_phy_obj_t *miso; uint32_t baudrate; } machine_spi_obj_t; +machine_spi_obj_t *spi_obj[MAX_SPI] = { NULL }; + +static inline machine_spi_obj_t *spi_obj_alloc() { + for (uint8_t i = 0; i < MAX_SPI; i++) + { + if (spi_obj[i] == NULL) { + spi_obj[i] = mp_obj_malloc(machine_spi_obj_t, &machine_spi_type); + return spi_obj[i]; + } + } + + return NULL; +} + +static inline void spi_obj_free(machine_spi_obj_t *spi_obj_ptr) { + for (uint8_t i = 0; i < MAX_SPI; i++) + { + if (spi_obj[i] == spi_obj_ptr) { + spi_obj[i] = NULL; + } + } +} + +STATIC inline void spi_sck_alloc(machine_spi_obj_t *spi_obj, mp_obj_t pin_name) { + machine_pin_phy_obj_t *sck = pin_phy_realloc(pin_name, PIN_PHY_FUNC_SPI); + + if (sck == NULL) { + size_t slen; + mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("SCK pin (%s) not found !"), mp_obj_str_get_data(pin_name, &slen)); + } + + spi_obj->sck = sck; +} + +STATIC inline void spi_sck_free(machine_spi_obj_t *spi_obj) { + pin_phy_free(spi_obj->sck); +} + +STATIC inline void spi_mosi_alloc(machine_spi_obj_t *spi_obj, mp_obj_t pin_name) { + machine_pin_phy_obj_t *mosi = pin_phy_realloc(pin_name, PIN_PHY_FUNC_SPI); + + if (mosi == NULL) { + size_t slen; + mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("MOSI pin (%s) not found !"), mp_obj_str_get_data(pin_name, &slen)); + } + + spi_obj->mosi = mosi; +} + +STATIC inline void spi_mosi_free(machine_spi_obj_t *spi_obj) { + pin_phy_free(spi_obj->mosi); +} + +STATIC inline void spi_miso_alloc(machine_spi_obj_t *spi_obj, mp_obj_t pin_name) { + machine_pin_phy_obj_t *miso = pin_phy_realloc(pin_name, PIN_PHY_FUNC_SPI); + + if (miso == NULL) { + size_t slen; + mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("MISO pin (%s) not found !"), mp_obj_str_get_data(pin_name, &slen)); + } + + spi_obj->miso = miso; +} + +STATIC inline void spi_miso_free(machine_spi_obj_t *spi_obj) { + pin_phy_free(spi_obj->miso); +} + // Function to select the mode STATIC cyhal_spi_mode_t mode_select(uint8_t firstbit, uint8_t polarity, uint8_t phase) { @@ -85,10 +151,9 @@ STATIC void machine_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_prin mp_printf(print, "SPI(id=%u, baudrate=%u, polarity=%u, phase=%u, bits=%u, firstbit=%u, sck=%d, mosi=%d, miso=%d)", self->spi_id, self->baudrate, self->polarity, self->phase, self->bits, self->firstbit, - self->sck, self->mosi, self->miso); + self->sck->addr, self->mosi->addr, self->miso->addr); } - mp_obj_t machine_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { enum { ARG_id, ARG_baudrate, ARG_polarity, ARG_phase, ARG_bits, ARG_firstbit, ARG_sck, ARG_mosi, ARG_miso }; static const mp_arg_t allowed_args[] = { @@ -116,7 +181,7 @@ mp_obj_t machine_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n } // Get static peripheral object. - machine_spi_obj_t *self = mp_obj_malloc(machine_spi_obj_t, &machine_spi_type); + machine_spi_obj_t *self = spi_obj_alloc(); // set baudrate if provided if (args[ARG_baudrate].u_int != -1) { @@ -145,40 +210,21 @@ mp_obj_t machine_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n // Set SCK/MOSI/MISO pins if configured. if (args[ARG_sck].u_obj != mp_const_none) { - int sck = mp_hal_get_pin_obj(args[ARG_sck].u_obj); - - if (self->sck == -1) { - size_t slen; - mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("SCK pin (%s) not found !"), mp_obj_str_get_data(args[ARG_sck].u_obj, &slen)); - } - self->sck = sck; + spi_sck_alloc(self, args[ARG_sck].u_obj); } else { - self->sck = MICROPY_HW_SPI_SCK; + mp_raise_TypeError(MP_ERROR_TEXT("SCK pin must be provided")); } if (args[ARG_mosi].u_obj != mp_const_none) { - int mosi = mp_hal_get_pin_obj(args[ARG_mosi].u_obj); - - if (self->mosi == -1) { - size_t slen; - mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("MOSI pin (%s) not found !"), mp_obj_str_get_data(args[ARG_mosi].u_obj, &slen)); - } - self->mosi = mosi; + spi_mosi_alloc(self, args[ARG_mosi].u_obj); } else { - self->mosi = MICROPY_HW_SPI_MOSI; + mp_raise_TypeError(MP_ERROR_TEXT("MOSI pin must be provided")); } if (args[ARG_miso].u_obj != mp_const_none) { - int miso = mp_hal_get_pin_obj(args[ARG_miso].u_obj); - - if (self->miso == -1) { - size_t slen; - mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("MISO pin (%s) not found !"), mp_obj_str_get_data(args[ARG_miso].u_obj, &slen)); - } - self->miso = miso; + spi_miso_alloc(self, args[ARG_miso].u_obj); } else { - self->miso = MICROPY_HW_SPI_MISO; - + mp_raise_TypeError(MP_ERROR_TEXT("MISO pin must be provided")); } if (n_args > 1 || n_kw > 0) { @@ -186,13 +232,12 @@ mp_obj_t machine_spi_make_new(const mp_obj_type_t *type, size_t n_args, size_t n // set the baudrate cyhal_spi_set_frequency(&self->spi_obj, self->baudrate); // Initialise the SPI peripheral if any arguments given, or it was not initialised previously. - cy_rslt_t result = cyhal_spi_init(&self->spi_obj, self->mosi, self->miso, self->sck, NC, NULL, self->bits, mode, false); + cy_rslt_t result = cyhal_spi_init(&self->spi_obj, self->mosi->addr, self->miso->addr, self->sck->addr, NC, NULL, self->bits, mode, false); if (result != CY_RSLT_SUCCESS) { mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("SPI initialisation failed with return code %lx !"), result); } } return MP_OBJ_FROM_PTR(self); - } STATIC void machine_spi_init(mp_obj_base_t *self_in, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { @@ -237,7 +282,7 @@ STATIC void machine_spi_init(mp_obj_base_t *self_in, size_t n_args, const mp_obj // since it's reinitialising, first it should be deinitialised & initilased with new parameters cyhal_spi_free(&self->spi_obj); - cy_rslt_t result = cyhal_spi_init(&self->spi_obj, self->mosi, self->miso, self->sck, NC, NULL, self->bits, mode, false); + cy_rslt_t result = cyhal_spi_init(&self->spi_obj, self->mosi->addr, self->miso->addr, self->sck->addr, NC, NULL, self->bits, mode, false); if (result != CY_RSLT_SUCCESS) { mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("SPI initialisation failed with return code %lx !"), result); } @@ -275,6 +320,10 @@ STATIC void machine_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint8 STATIC void machine_spi_deinit(mp_obj_base_t *self_in) { machine_spi_obj_t *self = (machine_spi_obj_t *)self_in; cyhal_spi_free(&self->spi_obj); + spi_sck_free(self); + spi_mosi_free(self); + spi_miso_free(self); + spi_obj_free(self); } STATIC const mp_machine_spi_p_t machine_spi_p = { @@ -292,3 +341,11 @@ MP_DEFINE_CONST_OBJ_TYPE( protocol, &machine_spi_p, locals_dict, &mp_machine_spi_locals_dict ); + +void mod_spi_deinit() { + for (uint8_t i = 0; i < MAX_SPI; i++) { + if (spi_obj[i] != NULL) { + machine_spi_deinit((mp_obj_base_t *)(spi_obj[i])); + } + } +} From 1d2f84b0104f8b4f3b8660f596e44428fbce114f Mon Sep 17 00:00:00 2001 From: enriquezgarc Date: Fri, 13 Oct 2023 09:04:59 +0200 Subject: [PATCH 3/5] tests/psoc6/spi_hard.c: Added pins as args. Signed-off-by: enriquezgarc --- tests/psoc6/spi_hard.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/psoc6/spi_hard.py b/tests/psoc6/spi_hard.py index 87a1d6d91d0f..ed27e30c29ae 100644 --- a/tests/psoc6/spi_hard.py +++ b/tests/psoc6/spi_hard.py @@ -1,4 +1,4 @@ from machine import SPI -spi = SPI(0) +spi = SPI(0, sck="P9_2", mosi="P9_0", miso="P9_1") print(spi) From 97559dfd247bb96663c656f449cf0904f6fbf3a7 Mon Sep 17 00:00:00 2001 From: enriquezgarc Date: Fri, 13 Oct 2023 10:54:02 +0200 Subject: [PATCH 4/5] ports/psoc6: Cleanup of previous machine_pin implementation. Signed-off-by: enriquezgarc --- ports/psoc6/modules/machine/machine_adc.h | 1 + ports/psoc6/modules/machine/machine_adcblock.h | 2 -- ports/psoc6/modules/machine/machine_pin.c | 8 ++++---- ports/psoc6/modules/machine/machine_pin.h | 9 --------- ports/psoc6/mphalport.c | 2 +- ports/psoc6/mphalport.h | 1 + 6 files changed, 7 insertions(+), 16 deletions(-) delete mode 100644 ports/psoc6/modules/machine/machine_pin.h diff --git a/ports/psoc6/modules/machine/machine_adc.h b/ports/psoc6/modules/machine/machine_adc.h index 4df04cfff6a1..4d37d70fdb2f 100644 --- a/ports/psoc6/modules/machine/machine_adc.h +++ b/ports/psoc6/modules/machine/machine_adc.h @@ -2,6 +2,7 @@ #define MICROPY_INCLUDED_MACHINE_ADC_H #include "machine_adcblock.h" +#include "machine_pin_phy.h" typedef struct _machine_adc_obj_t { mp_obj_base_t base; diff --git a/ports/psoc6/modules/machine/machine_adcblock.h b/ports/psoc6/modules/machine/machine_adcblock.h index 9e5c6727d9d9..1d65ec441e67 100644 --- a/ports/psoc6/modules/machine/machine_adcblock.h +++ b/ports/psoc6/modules/machine/machine_adcblock.h @@ -11,8 +11,6 @@ #define MAX_BLOCKS (1) #define MAX_CHANNELS (6) -#include "machine_pin.h" - typedef struct _machine_adc_obj_t machine_adc_obj_t; /* Forward declaration of adc_obj */ typedef struct _machine_adcblock_obj_t { diff --git a/ports/psoc6/modules/machine/machine_pin.c b/ports/psoc6/modules/machine/machine_pin.c index 01938fb1d68f..8b1f8b08557b 100644 --- a/ports/psoc6/modules/machine/machine_pin.c +++ b/ports/psoc6/modules/machine/machine_pin.c @@ -4,7 +4,7 @@ // port-specific includes #include "modmachine.h" -#include "machine_pin.h" +#include "machine_pin_phy.h" #include "extmod/virtpin.h" #include "mplogger.h" #include "cyhal.h" @@ -178,7 +178,7 @@ static bool machine_pin_is_inited(machine_pin_io_obj_t *self) { STATIC mp_obj_t machine_pin_obj_init_helper(machine_pin_io_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { mplogger_print("init helper function called\n"); - enum {ARG_mode, ARG_pull, ARG_value, ARG_drive}; // , ARG_alt}; + enum {ARG_mode, ARG_pull, ARG_value}; 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}}, @@ -223,7 +223,7 @@ STATIC mp_obj_t machine_pin_obj_init_helper(machine_pin_io_obj_t *self, size_t n } // Machine Pin methods - port-specific definitions -// Pin constructor(id,mode,pull,value=value,drive=drive,alt=alt) +// Pin constructor(id,mode,pull,value=value) 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) { mplogger_print("%q constructor invoked\n", MP_QSTR_Pin); @@ -272,7 +272,7 @@ STATIC mp_obj_t machine_pin_value(size_t n_args, const mp_obj_t *args) { STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_pin_value_obj, 1, 2, machine_pin_value); // instantiates obj of Pin class -// Pin.init(mode,pull,value=value,drive=drive,alt=alt) +// Pin.init(mode,pull,value=value) STATIC mp_obj_t machine_pin_obj_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) { return machine_pin_obj_init_helper(args[0], n_args - 1, args + 1, kw_args); } diff --git a/ports/psoc6/modules/machine/machine_pin.h b/ports/psoc6/modules/machine/machine_pin.h deleted file mode 100644 index 4fd411532646..000000000000 --- a/ports/psoc6/modules/machine/machine_pin.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef MICROPY_INCLUDED_MACHINE_PIN_H -#define MICROPY_INCLUDED_MACHINE_PIN_H - -#include -#include "machine_pin_phy.h" - -void mod_pin_deinit(); - -#endif // MICROPY_INCLUDED_MACHINE_PIN_H diff --git a/ports/psoc6/mphalport.c b/ports/psoc6/mphalport.c index 5acc2e20ef15..b13096b06d0d 100644 --- a/ports/psoc6/mphalport.c +++ b/ports/psoc6/mphalport.c @@ -16,7 +16,7 @@ // port-specific includes -#include "modules/machine/machine_pin.h" +#include "modules/machine/machine_pin_phy.h" extern cyhal_rtc_t psoc6_rtc; extern cyhal_timer_t psoc6_timer; diff --git a/ports/psoc6/mphalport.h b/ports/psoc6/mphalport.h index a09eeaa9960e..e266a7b2727f 100644 --- a/ports/psoc6/mphalport.h +++ b/ports/psoc6/mphalport.h @@ -15,6 +15,7 @@ // port-specific includes + #define MP_HAL_PIN_FMT "%u" #define mp_hal_pin_obj_t uint From 305f33fe7a77d40e5eb456dcb5477aed5bd04447 Mon Sep 17 00:00:00 2001 From: enriquezgarc Date: Fri, 13 Oct 2023 11:11:18 +0200 Subject: [PATCH 5/5] docs/psoc6: Modificatons in machine module. Signed-off-by: enriquezgarc --- docs/psoc6/feature_list.rst | 60 +++++++++++++-------------- docs/psoc6/quickref.rst | 82 ++++++++++++++++++++++--------------- 2 files changed, 80 insertions(+), 62 deletions(-) diff --git a/docs/psoc6/feature_list.rst b/docs/psoc6/feature_list.rst index d4ec7023f59b..ad8fa06e5893 100644 --- a/docs/psoc6/feature_list.rst +++ b/docs/psoc6/feature_list.rst @@ -10,25 +10,25 @@ Enabled modules * cmath * gc * math - * uarray - * uasyncio - * ubinascii - * ucollections - * uerrno - * uhashlib - * uheapq - * uio - * ujson - * uos - * urandom - * ure - * uselect - * usocket - * ussl - * ustruct - * usys - * utime - * uzlib + * array + * asyncio + * binascii + * collections + * errno + * hashlib + * heapq + * io + * json + * os + * random + * re + * select + * socket + * ssl + * struct + * sys + * time + * zlib * Micropython specific modules and libraries @@ -46,7 +46,7 @@ Enabled modules * ADCBlock * micropython - * ucryptolib + * cryptolib * uctypes * network @@ -62,7 +62,7 @@ Not yet enabled * Micropython specific modules and libraries * btree - * ubluetooth + * bluetooth Table :ref:`configuration details ` below lists specific settings deviating from the configuration as per config level as well as functionality not yet implemented: @@ -74,9 +74,9 @@ Table :ref:`configuration details ` below lists specifi +=================+======================================================================================================================+ | gc | Option ``MICROPY_ENABLE_GC`` enabled. | +-----------------+----------------------------------------------------------------------------------------------------------------------+ -| uhashlib | Options ``MICROPY_PY_UHASHLIB_MD5``, ``MICROPY_PY_UHASHLIB_SHA1``, ``MICROPY_PY_UHASHLIB_SHA256`` enabled. | +| hashlib | Options ``MICROPY_PY_UHASHLIB_MD5``, ``MICROPY_PY_UHASHLIB_SHA1``, ``MICROPY_PY_UHASHLIB_SHA256`` enabled. | +-----------------+----------------------------------------------------------------------------------------------------------------------+ -| uos | Support for LFS2 and FAT, LFS2 enabled by default. FS mounted on external flash at "/flash". | +| os | Support for LFS2 and FAT, LFS2 enabled by default. FS mounted on external flash at "/flash". | | | | | | Options ``MICROPY_PY_OS_DUPTERM``, ``MICROPY_PY_UOS_GETENV_PUTENV_UNSETENV``, ``MICROPY_PY_UOS_INCLUDEFILE``, | | | ``MICROPY_PY_UOS_SYSTEM``, ``MICROPY_PY_UOS_UNAME``, ``MICROPY_VFS_LFS2`` enabled. | @@ -84,19 +84,19 @@ Table :ref:`configuration details ` below lists specifi | | Function *urandom()* not yet implemented. Requires implementing *mp_uos_urandom()* and setting option | | | ``MICROPY_PY_UOS_URANDOM``. | +-----------------+----------------------------------------------------------------------------------------------------------------------+ -| urandom | Function *seed()* not yet implemented. | +| random | Function *seed()* not yet implemented. | +-----------------+----------------------------------------------------------------------------------------------------------------------+ -| ure | Options ``MICROPY_PY_URE_DEBUG``, ``MICROPY_PY_URE_MATCH_GROUPS``, ``MICROPY_PY_URE_MATCH_SPAN_START_END`` enabled. | +| re | Options ``MICROPY_PY_URE_DEBUG``, ``MICROPY_PY_URE_MATCH_GROUPS``, ``MICROPY_PY_URE_MATCH_SPAN_START_END`` enabled. | +-----------------+----------------------------------------------------------------------------------------------------------------------+ -| usocket | Options ``MICROPY_PY_USOCKET`` enabled. | +| socket | Options ``MICROPY_PY_USOCKET`` enabled. | +-----------------+----------------------------------------------------------------------------------------------------------------------+ -| ussl | Options ``MICROPY_PY_USSL`` enabled. Has 2 failing tests. | +| ssl | Options ``MICROPY_PY_USSL`` enabled. Has 2 failing tests. | +-----------------+----------------------------------------------------------------------------------------------------------------------+ -| usys | Options ``MICROPY_PY_SYS_EXC_INFO`` enabled. | +| sys | Options ``MICROPY_PY_SYS_EXC_INFO`` enabled. | +-----------------+----------------------------------------------------------------------------------------------------------------------+ -| utime | Enabled through HAL functions based on machine.RTC module. Option ``MICROPY_PY_UTIME_MP_HAL`` enabled. | +| time | Enabled through HAL functions based on machine.RTC module. Option ``MICROPY_PY_UTIME_MP_HAL`` enabled. | +-----------------+----------------------------------------------------------------------------------------------------------------------+ -| ucryptolib | Options ``MICROPY_PY_UCRYPTOLIB``, ``MICROPY_PY_UCRYPTOLIB_CTR``, ``MICROPY_PY_UCRYPTOLIB_CONSTS`` enabled. | +| cryptolib | Options ``MICROPY_PY_UCRYPTOLIB``, ``MICROPY_PY_UCRYPTOLIB_CTR``, ``MICROPY_PY_UCRYPTOLIB_CONSTS`` enabled. | +-----------------+----------------------------------------------------------------------------------------------------------------------+ | machine | Functions not yet implemented: *lightsleep()*, *deepsleep()*, *wake_reason()*, *time_pulse_us()*, *rng()*. | | | | diff --git a/docs/psoc6/quickref.rst b/docs/psoc6/quickref.rst index eba20f130135..7db0f60d3952 100644 --- a/docs/psoc6/quickref.rst +++ b/docs/psoc6/quickref.rst @@ -87,9 +87,9 @@ An instance of the :mod:`machine.Pin` class can be created by invoking the const from machine import Pin - p0 = Pin('P13_7', Pin.OUT, Pin.PULL_DOWN, value=Pin.STATE_LOW) # create output pin on pin P13_7, - # with pull-down resistor enabled, - # with initial value 0 (STATE_LOW) + p0 = Pin('P13_7', Pin.OUT, Pin.PULL_DOWN, value=0) # create output pin on pin P13_7, + # with pull-down resistor enabled, + # with initial value 0 (low) Additionally, with any combination of parameters (except the Pin number or ``id`` which should be passed mandatorily), a :mod:`machine.Pin` object with various configuration levels can be instantiated. In these cases, the :meth:`Pin.init` function has to be called proactively to set the other necessary configurations, as needed. @@ -115,19 +115,22 @@ Similar to CPython, the parameters can be passed in any order if keywords are us from machine import Pin - p0 = Pin(id='P13_7', value=Pin.STATE_LOW, pull=Pin.PULL_DOWN, mode=Pin.OUT) # create output pin on pin P13_7, - # with pull-down resistor enabled, - # with initial value 0 (STATE_LOW) + p0 = Pin(id='P13_7', value=0, pull=Pin.PULL_DOWN, mode=Pin.OUT) # create output pin on pin P13_7, + # with pull-down resistor enabled, + # with initial value 0 (low) - p1 = Pin('P0_0', Pin.OUT, None, value=Pin.STATE_HIGH) # create output pin on pin P0_0, - # with pull as NONE, - # with initial value 1 (STATE_HIGH) + p1 = Pin('P0_0', Pin.OUT, None, value=1) # create output pin on pin P0_0, + # with pull as NONE, + # with initial value 1 (high) Note that the parameters such as ``value`` can only be passed as keyword arguments. -.. - TODO: add ``drive`` and ``alt`` when implemented +.. note:: + + The following constructor arguments are NOT supported in this port: + * ``drive``. This configuration is automatically handled by the constructor and abstracted to the user. + * ``alt``. Alternative functionality is directly handled by the respective machine peripherals classes. Methods ^^^^^^^ @@ -137,16 +140,6 @@ Methods Set pin value to its complement. -Constants -^^^^^^^^^ -The following constants are used to configure the pin objects in addition to the ones mentioned in the :mod:`machine.Pin` class. - -.. data:: Pin.STATE_LOW - Pin.STATE_HIGH - - Selects the pin value. - - There's a higher-level abstraction :ref:`machine.Signal ` which can be used to invert a pin. Useful for illuminating active-low LEDs using ``on()`` or ``value(1)``. @@ -180,6 +173,14 @@ scl P6_0 P9_0 sda P6_1 P9_1 ===== =========== ============ +.. + + TODO: This is only applicable to the CY8CPROTO-062-4343W. This does not belong here. + TODO: Define approach on how the user gets to know the pinout diagram, alternate function of each board + - From board manual? + - From datasheet? + - To create a pinout diagram? + The driver is accessed via :ref:`machine.I2C ` @@ -192,19 +193,19 @@ initialized and configured to work in master mode. The maximum supported frequen :: from machine import I2C - i2c = I2C(0,scl='P6_0',sda='P6_1',freq=400000) + i2c = I2C(0, scl='P6_0', sda='P6_1',freq=400000) -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. The ``scl`` and ``sda`` pins are also required. :: from machine import I2C - i2c = I2C(0) #I2C is initialized & configured with default scl, sda pin & frequency + i2c = I2C(0, scl='P6_0', sda='P6_1') #I2C is initialized & configured with default frequency :: from machine import I2C - i2c = I2C(0,scl='P9_0',sda='P9_1',freq=400000) #I2C is initialised & configured with given scl,sda pins & frequency + i2c = I2C(0, scl='P9_0', sda='P9_1', freq=400000) #I2C is initialised & configured with given scl,sda pins & frequency Methods ^^^^^^^ @@ -411,8 +412,17 @@ MISO P9_1 P6_1 P10_1 SCK P9_2 P6_2 P10_2 ===== =========== ============ ============ +.. + + TODO: This is only applicable to the CY8CPROTO-062-4343W. This does not belong here. + TODO: Define approach on how the user gets to know the pinout diagram, alternate function of each board + - From board manual? + - From datasheet? + - To create a pinout diagram? + Refer `PSoC 6 MCU: CY8C62x8, CY8C62xA Datasheet `_ -for additional details regarding all the SPI capable pins. +for additional details regarding all the SPI capable pins. The pins ``sck``, ``mosi`` and ``miso`` *must* be specified when +initialising Software SPI. The driver is accessed via :ref:`machine.SPI ` @@ -425,7 +435,7 @@ SPI object is created with default settings or settings of previous initializati :: from machine import SPI - spi = SPI(0) # Default assignment: id=0, sck=P9_2 ,MOSI=P9_0, MISO=P9_1, baudrate=1000000, Polarity=0, Phase=0, bits=8, firstbit=SPI.MSB + spi = SPI(0, sck='P11_2', mosi='P11_0', miso='P11_1') # Default assignment: id=0, SCK=P11_2 ,MOSI=P11_0, MISO=P11_1 spi.init() Management of a CS signal should happen in user code (via machine.Pin class). @@ -441,7 +451,7 @@ If the constructor is called with any additional parameters then SPI object is c :: - spi = SPI(0, baudrate=2000000) #object is created & initialised with baudrate=2000000 & default parameters + spi = SPI(0, sck='P11_2', mosi='P11_0', miso='P11_1', baudrate=2000000) #object is created & initialised with baudrate=2000000 & default parameters spi = SPI(0, baudrate=1500000, polarity=1, phase=1, bits=8, firstbit=SPI.LSB, sck='P11_2', mosi='P11_0', miso='P11_1') Methods @@ -473,9 +483,9 @@ following pins : "P10_0" - "P10_5". Use the :ref:`machine.ADC ` class:: - from machine import ADC, Pin + from machine import ADC - adc = ADC(Pin("P10_0")) # create an ADC object on ADC pin + adc = ADC("P10_0") # create an ADC object on ADC pin val = adc.read_u16() # read a raw analog value in the range 0-65535 val = adc.read_uv() # read an analog value in micro volts @@ -499,6 +509,14 @@ PSoC6 supports only 1 12-bit SAR ADC with the following channel to pin mapping a | 5 | P10_5 | +---------+-------+ +.. + + TODO: This is only applicable to the CY8CPROTO-062-4343W. This does not belong here. + TODO: Define approach on how the user gets to know the pinout diagram, alternate function of each board + - From board manual? + - From datasheet? + - To create a pinout diagram? + .. note:: Arbitrary connection of ADC channels to GPIO is not supported. Specifying a pin that is not connected to this block, or specifying a mismatched channel and pin, will raise an exception. @@ -506,9 +524,9 @@ PSoC6 supports only 1 12-bit SAR ADC with the following channel to pin mapping a To use the APIs: :: - from machine import ADCBlock, Pin + from machine import ADCBlock adcBlock = ADCBlock(0, bits=12) # create an ADCBlock 0 object - adc = adcBlock.connect(0, Pin("P10_0")) # connect channel 0 to pin P10_0 + adc = adcBlock.connect(0, "P10_0") # connect channel 0 to pin P10_0 val = adc.read_uv() # read an analog value in micro volts