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>

 tests/psoc6: Bitstream test.

Signed-off-by: IFX-Anusha <Anusha.TR@infineon.com>
  • Loading branch information
IFX-Anusha committed Mar 9, 2024
1 parent 1e354ac commit 0a5ac3e
Show file tree
Hide file tree
Showing 7 changed files with 120 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 @@ -303,4 +303,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
54 changes: 54 additions & 0 deletions tests/psoc6/multi/bitstream.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import sys
import time
from machine import Pin
from machine import bitstream
import os

# Allocate pin based on board
machine = os.uname().machine
if "CY8CPROTO-062-4343W" in machine:
gpio_pin = "P13_6"
pin_in = "P12_0"

elif "CY8CPROTO-063-BLE" in machine:
gpio_pin = "P5_2"
pin_in = "P5_3"

timing = [50000, 10000, 80000, 75000]
buf = bytearray([0xAB])
device = '/dev/ttyACM0'
file_location = "./psoc6/test_inputs/bitstream_listen.py"
remote_directory_path = "/"
mpr_connect = f"../tools/mpremote/mpremote.py connect {device}"
mpr_file_cp = f"{mpr_connect} cp {file_location} :{remote_directory_path}"
mpr_execute = f"../tools/mpremote/mpremote.py exec {remote_directory_path}bitstream_listen.py"
mpr_ls = f"{mpr_connect} fs ls /"
mpr_rm = f"{mpr_connect} fs rm "

def exec(cmd, op_file_path="null"):
if cmd == mpr_rm:
# Check if file is present already
status = os.system(f"{mpr_ls} | grep {op_file_path}")
# If the file is present, remove it
if status == 0:
os.system(f"{cmd} {op_file_path}")
else:
with open(op_file_path, "a") as file:
status = os.system(cmd)
if status != 0:
print("error")


#bistream generation
def instance0():
exec(mpr_rm, "bitstream_listen.py") # Remove file if it exists
exec(mpr_file_cp) # Copy the file to the remote device
exec(mpr_execute) # Execute the script remotely



#bitstream capture
def instance1():
print("bitstream")
p0 = Pin('P13_6', Pin.OUT, value=0)
bitstream(p0, 0, timing, buf)
30 changes: 30 additions & 0 deletions tests/psoc6/test_inputs/bitstream_listen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import os
import time
from machine import Pin
from machine import bitstream

# Allocate pin based on board
machine = os.uname().machine
if "CY8CPROTO-062-4343W" in machine:
pin_in = "P12_0"

elif "CY8CPROTO-063-BLE" in machine:
pin_in = "P5_3"

print("bitstream capture")
input_pin = Pin(pin_in, Pin.IN)
value = input_pin.value()
periods = []
last_value = 0
for i in range(100):
start_time = time.ticks_us()
while input_pin.value() == last_value:
pass

time_period = time.ticks_diff(time.ticks_us(), start_time)
periods.append((time_period, last_value))
last_value = input_pin.value()




0 comments on commit 0a5ac3e

Please sign in to comment.