diff --git a/ports/psoc6/machine_bitstream.c b/ports/psoc6/machine_bitstream.c index 8363baab3bba9..36bbf18d79f1b 100644 --- a/ports/psoc6/machine_bitstream.c +++ b/ports/psoc6/machine_bitstream.c @@ -2,11 +2,14 @@ #include "py/mphal.h" #include "FreeRTOS.h" #include "task.h" +#include "py/runtime.h" +#include "py/mphal.h" +#include "extmod/modmachine.h" #if MICROPY_PY_MACHINE_BITSTREAM -#define NS_TICKS_OVERHEAD (0) // set overhead as required to adjust the level -uint32_t range = 0; // range<1000 ns +const uint32_t timing_sequence_1[] = {500, 1125, 800, 750}; +uint32_t range = 0; void machine_bitstream_high_low(mp_hal_pin_obj_t pin, uint32_t *timing_ns, const uint8_t *buf, size_t len) { @@ -14,21 +17,22 @@ void machine_bitstream_high_low(mp_hal_pin_obj_t pin, uint32_t *timing_ns, const uint32_t pinNum = CYHAL_GET_PIN(pin); uint32_t fcpu_mhz = mp_hal_get_cpu_freq() / 1000000; - cyhal_gpio_init(pin, CYHAL_GPIO_DIR_OUTPUT, - CYHAL_GPIO_DRIVE_STRONG, 1); + if (memcmp(timing_ns, timing_sequence_1, 4 * sizeof(uint32_t)) == 0) { + range = 0; + } else { + for (size_t i = 0; i < 4; ++i) { + if ((timing_ns[i]) <= 150) { + mp_raise_ValueError(MP_ERROR_TEXT("Timing is not supported")); + } + } + range = 1; + } // Convert ns to cycles [high_time_0, low_time_0, high_time_1, low_time_1]. for (size_t i = 0; i < 4; ++i) { timing_ns[i] = fcpu_mhz * timing_ns[i] / 1000; } - // loop to check the timing values range - for (size_t i = 0; i < 4; ++i) { - if ((timing_ns[i]) >= 100) { - range = 1; - } - } - taskENTER_CRITICAL(); // FreeRTOS function for critical section switch (range) @@ -36,6 +40,7 @@ void machine_bitstream_high_low(mp_hal_pin_obj_t pin, uint32_t *timing_ns, const case 0: for (size_t i = 0; i < len; i++) { + uint8_t b = buf[i]; // Send each bit of the byte for (size_t j = 0; j < 8; j++) @@ -49,14 +54,21 @@ void machine_bitstream_high_low(mp_hal_pin_obj_t pin, uint32_t *timing_ns, const // sendOneBit(); Cy_GPIO_Write(base, pinNum, 1); Cy_GPIO_Write(base, pinNum, 1); + Cy_GPIO_Write(base, pinNum, 1); + Cy_GPIO_Write(base, pinNum, 1); + Cy_GPIO_Write(base, pinNum, 0); Cy_GPIO_Write(base, pinNum, 0); break; // Send a 0-bit case 0: // sendZeroBit(); + Cy_GPIO_Write(base, pinNum, 1); + Cy_GPIO_Write(base, pinNum, 1); + Cy_GPIO_Write(base, pinNum, 0); + Cy_GPIO_Write(base, pinNum, 0); + Cy_GPIO_Write(base, pinNum, 0); Cy_GPIO_Write(base, pinNum, 0); Cy_GPIO_Write(base, pinNum, 0); - Cy_GPIO_Write(base, pinNum, 1); break; } } @@ -78,7 +90,6 @@ void machine_bitstream_high_low(mp_hal_pin_obj_t pin, uint32_t *timing_ns, const break; } taskEXIT_CRITICAL(); - } #endif // MICROPY_PY_MACHINE_BITSTREAM diff --git a/ports/psoc6/machine_pin.c b/ports/psoc6/machine_pin.c index 109b863ea1357..d96a41004bcf5 100644 --- a/ports/psoc6/machine_pin.c +++ b/ports/psoc6/machine_pin.c @@ -29,6 +29,12 @@ typedef struct _machine_pin_io_obj_t { machine_pin_io_obj_t *pin_io[MAX_IO_PINS] = {NULL}; +// helper function used by mphalport +int pin_fetch_address(mp_obj_t pin) { + machine_pin_io_obj_t *self = MP_OBJ_TO_PTR(pin); + return self->pin_phy->addr; +} + static inline machine_pin_io_obj_t *pin_io_allocate(mp_obj_t pin_name) { machine_pin_phy_obj_t *pin_phy = pin_phy_realloc(pin_name, PIN_PHY_FUNC_DIO); uint16_t i; diff --git a/ports/psoc6/machine_pin_phy.c b/ports/psoc6/machine_pin_phy.c index 6502b54b4193c..b5e161f76ed2b 100644 --- a/ports/psoc6/machine_pin_phy.c +++ b/ports/psoc6/machine_pin_phy.c @@ -43,12 +43,13 @@ mp_obj_t pin_name_by_addr(mp_obj_t pin) { } } -// helper function to translate pin_name(string) into machine_pin_io_obj_t->pin_addr +// helper function to translate pin_name(string) into machine_pin_io_obj_t->pin_addr or pin_obj to machine_pin_io_obj_t->pin_addr int pin_addr_by_name(mp_obj_t pin) { if (mp_obj_is_str(pin)) { return machine_pin_phy_obj[pin_find(pin)].addr; + printf("pinphybit %lu\n\r", machine_pin_phy_obj[pin_find(pin)].addr); } else { - return -1; // expecting a str as input + return pin_fetch_address(pin); } } diff --git a/ports/psoc6/machine_pin_phy.h b/ports/psoc6/machine_pin_phy.h index bd46e8a57edd2..f34149edaec6d 100644 --- a/ports/psoc6/machine_pin_phy.h +++ b/ports/psoc6/machine_pin_phy.h @@ -31,6 +31,8 @@ void pin_phy_free(machine_pin_phy_obj_t *obj); void mod_pin_phy_deinit(void); +int pin_fetch_address(mp_obj_t pin); // function to support the bitstream class (obj to pin address) + // Function Prototypes to support interaction between c<->py int pin_find(mp_obj_t obj); mp_obj_t pin_name_by_addr(mp_obj_t pin); diff --git a/ports/psoc6/modmachine.c b/ports/psoc6/modmachine.c index 5bd931cafe714..7c8dbf5749aff 100644 --- a/ports/psoc6/modmachine.c +++ b/ports/psoc6/modmachine.c @@ -302,4 +302,5 @@ STATIC void mp_machine_idle(void) { { MP_ROM_QSTR(MP_QSTR_ADCBlock), MP_ROM_PTR(&machine_adcblock_type) }, \ { MP_ROM_QSTR(MP_QSTR_I2S), MP_ROM_PTR(&machine_i2s_type) }, \ + #endif // MICROPY_PY_MACHINE