Skip to content

Commit

Permalink
ports/psoc6: Fix pin passing for consistency with Neopixel module.
Browse files Browse the repository at this point in the history
Signed-off-by: IFX-Anusha <Anusha.TR@infineon.com>
  • Loading branch information
IFX-Anusha committed Feb 29, 2024
1 parent b41a86e commit 1382a36
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 15 deletions.
37 changes: 24 additions & 13 deletions ports/psoc6/machine_bitstream.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,45 @@
#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) {

GPIO_PRT_Type *base = CYHAL_GET_PORTADDR(pin);
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)
{
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++)
Expand All @@ -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;
}
}
Expand All @@ -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
6 changes: 6 additions & 0 deletions ports/psoc6/machine_pin.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions ports/psoc6/machine_pin_phy.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
2 changes: 2 additions & 0 deletions ports/psoc6/machine_pin_phy.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions ports/psoc6/modmachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 1382a36

Please sign in to comment.