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 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_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/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])); + } + } +} 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 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)