Skip to content

Commit

Permalink
ports/psoc6: SPI refactoring and tests.
Browse files Browse the repository at this point in the history
Signed-off-by: NikhitaR-IFX <Nikhita.Rajasekhar@infineon.com>
  • Loading branch information
NikhitaR-IFX committed May 7, 2024
1 parent 236f1dd commit a321ecb
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 36 deletions.
14 changes: 13 additions & 1 deletion ports/psoc6/boards/make-pins-csv.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from __future__ import print_function
from itertools import islice
import argparse
import sys
import csv
Expand Down Expand Up @@ -50,7 +51,11 @@ def generate_pins_csv(pin_package_filename, pins_csv_filename):
with open("./" + pins_csv_filename, "w", newline="") as csv_file:
csv_writer = csv.writer(csv_file)
csv_writer.writerows(
[[value, value] for value in enum_values if value.startswith("P")]
[
[value, value]
for value in enum_values
if value.startswith("P") or value.startswith("N")
]
)
print("// pins.csv generated successfully")
else:
Expand All @@ -75,11 +80,18 @@ def generate_af_pins_csv(pin_package_filename, pins_af_csv_filename):

# Extract enum values using regex
pin_name = re.findall(r"\b(?!NC\b)(\w+)\s*=", enum_content)
# pin_name = re.findall(r"\b(\w+)\s*=", enum_content)
pin_def = re.findall(r"=\s*(.*?\))", enum_content)
# pin_def.insert(0, "255")
# print(pin_def)

# Write enum values to a CSV file
with open("./" + pins_af_csv_filename, "w", newline="") as csv_file:
NC_pin = ["NC", 255, "CYHAL_GET_GPIO(CYHAL_PORT_31, 7)"] # non-existent port
csv_writer = csv.writer(csv_file)
csv_writer.writerow(NC_pin)
for pname, pdef in zip(pin_name, pin_def):
# for pname, pdef in zip(islice(pin_name, 1, None), pin_def):
# if pin_name[pname].startswith('P'):
val = get_pin_addr_helper(pdef)
csv_writer.writerow([pname, val, pdef.strip('"')])
Expand Down
33 changes: 19 additions & 14 deletions ports/psoc6/machine_spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#define DEFAULT_SPI_PHASE (0)
#define DEFAULT_SPI_BITS (8)
#define DEFAULT_SPI_FIRSTBIT (0) // msb
#define DEFAULT_SPI_SSEL_PIN (NC)


#define spi_assert_raise_val(msg, ret) if (ret != CY_RSLT_SUCCESS) { \
Expand Down Expand Up @@ -142,7 +143,9 @@ static inline void spi_miso_alloc(machine_spi_obj_t *spi_obj, mp_obj_t pin_name)
static inline void spi_sck_free(machine_spi_obj_t *spi_obj) {
pin_phy_free(spi_obj->sck);
}

static inline void spi_ssel_free(machine_spi_obj_t *spi_obj) {
pin_phy_free(spi_obj->ssel);
}
static inline void spi_mosi_free(machine_spi_obj_t *spi_obj) {
pin_phy_free(spi_obj->mosi);
}
Expand Down Expand Up @@ -184,7 +187,7 @@ mp_obj_t machine_spi_init_helper(machine_spi_obj_t *self, int spi_mode, size_t n
{ MP_QSTR_phase, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = DEFAULT_SPI_PHASE} },
{ MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = DEFAULT_SPI_BITS} },
{ MP_QSTR_firstbit, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = DEFAULT_SPI_FIRSTBIT} },
{ MP_QSTR_ssel, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
{ MP_QSTR_ssel, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE}},
{ MP_QSTR_sck, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
{ MP_QSTR_mosi, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
{ MP_QSTR_miso, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
Expand Down Expand Up @@ -286,20 +289,21 @@ static void machine_spi_transfer(mp_obj_base_t *self_in, size_t len, const uint8
uint8_t rx_temp_buf[len];
uint8_t write_fill = 0xFF;

// Case 1: rx is NULL - (write operation)
// write
if (rx == NULL) {
tx_buf = tx;
memset(rx_temp_buf, 0x01, len * sizeof(uint8_t));
rx_buf = rx_temp_buf;
}
// Case 2: tx and rx equal --> read(), readinto() and write_readinto() with tx and rx same buffers
else {
} else {
// read(), readinto() and write_readinto() with tx and rx same buffers
if (tx == rx || tx == NULL) {
memcpy(tx_temp_buf, tx, len * sizeof(uint8_t));
tx_buf = tx_temp_buf;
rx_buf = rx;
write_fill = tx_temp_buf[0];
} else {
}
// write_readinto() with tx and rx different buffers
else {
tx_buf = tx;
rx_buf = rx;
}
Expand All @@ -313,6 +317,7 @@ 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_ssel_free(self);
spi_mosi_free(self);
spi_miso_free(self);
spi_obj_free(self);
Expand Down Expand Up @@ -391,13 +396,13 @@ static MP_DEFINE_CONST_FUN_OBJ_3(machine_spi_slave_write_readinto_obj, machine_s

static const mp_rom_map_elem_t machine_spi_slave_locals_dict_table[] = {
// Functions
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&machine_spi_slave_read_obj) },
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&machine_spi_slave_write_obj) },
{ MP_ROM_QSTR(MP_QSTR_write_readinto), MP_ROM_PTR(&machine_spi_slave_write_readinto_obj) },
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_spi_slave_deinit_obj) },

{ MP_ROM_QSTR(MP_QSTR_MSB), MP_ROM_INT(MICROPY_PY_MACHINE_SPISLAVE_MSB) },
{ MP_ROM_QSTR(MP_QSTR_LSB), MP_ROM_INT(MICROPY_PY_MACHINE_SPISLAVE_LSB) },
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&machine_spi_slave_read_obj) },
{ MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&machine_spi_slave_write_obj) },
{ MP_ROM_QSTR(MP_QSTR_write_readinto), MP_ROM_PTR(&machine_spi_slave_write_readinto_obj) },
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_spi_slave_deinit_obj) },
// constants
{ MP_ROM_QSTR(MP_QSTR_MSB), MP_ROM_INT(MICROPY_PY_MACHINE_SPISLAVE_MSB) },
{ MP_ROM_QSTR(MP_QSTR_LSB), MP_ROM_INT(MICROPY_PY_MACHINE_SPISLAVE_LSB) },
};
static MP_DEFINE_CONST_DICT(machine_spi_slave_locals_dict, machine_spi_slave_locals_dict_table);

Expand Down
12 changes: 4 additions & 8 deletions tests/psoc6/hw_ext/spi_master.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
import os

try:
from machine import Pin, SPI, SPISlave
from machine import Pin, SPI, SPISlave, SoftSPI
except ImportError:
print("SKIP")
raise SystemExit

machine = os.uname().machine
if "CY8CPROTO-062-4343W" in machine:
master_write_notify_pin = "P12_4" # Sends signals when master wants to write data
master_read_notify_pin = "P12_3" # Polls pin to check if slave is writing data
master_read_notify_pin = "P12_3" # interrupt pin to check if slave is writing data
# Allocate pin based on board
sck_master_pin = "P6_2"
mosi_master_pin = "P6_0"
Expand Down Expand Up @@ -76,7 +76,6 @@ def spi_half_duplex_communication(spi_obj, tx, rx):
_wait_for_slave_signal()
rx = spi_obj.read(8)
print("Slave returned (rx): \t", rx)
# print("Status: ", spi_obj.read(8))
print("Test case successful : \t", rx == tx)

print("\nTest Case 2: slave-->write and master-->read using readinto()")
Expand All @@ -89,11 +88,10 @@ def spi_half_duplex_communication(spi_obj, tx, rx):
print("Test case successful : \t", rx == exp)

print(
"\nTest Case 3: slave-->write and master-->read using readinto by sending out a write_byte=0x12"
"\nTest Case 3: slave-->write and master-->read using readinto by sending out a write_byte=5"
)
exp = b"\x08\x06\x04\x02\x08\x06\x04\x02"
spi_obj.readinto(rx, 5)
# _wait_for_slave_signal()
print("Slave wrote (rx): \t", rx)
print("Master expects (exp): \t", exp)
print("Test case successful : \t", rx == exp)
Expand All @@ -104,8 +102,6 @@ def spi_full_duplex_communication(spi_obj, tx, rx):
exp_rx = b"\x06\x06\x05\x05\x06\x06\x05\x05"
print("1) master-->write and slave-->read continuously")
spi_obj.write_readinto(tx, rx)
print(rx)
# _verify_test("read value is same as expected", rx, exp_rx)


print("\n*** SPI MASTER INSTANCE ***")
Expand All @@ -119,4 +115,4 @@ def spi_full_duplex_communication(spi_obj, tx, rx):

tx_buf = b"\x08\x06\x04\x02\x07\x05\x03\x01"
rx_buf = bytearray(8)
spi_full_duplex_communication(spi_obj, tx_buf, rx_buf)
# spi_full_duplex_communication(spi_obj, tx_buf, rx_buf)
18 changes: 18 additions & 0 deletions tests/psoc6/hw_ext/spi_master.py.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
*** SPI MASTER INSTANCE ***

*** Half duplex communication ***

Test Case 1: master-->write and slave-->read
Master wrote (tx): b'\x08\x06\x04\x02\x07\x05\x03\x01'
Slave returned (rx): b'\x08\x06\x04\x02\x07\x05\x03\x01'
Test case successful : True

Test Case 2: slave-->write and master-->read using readinto()
Slave wrote (rx): bytearray(b'\x01\x03\x05\x07\x02\x04\x06\x08')
Master expects (exp): b'\x01\x03\x05\x07\x02\x04\x06\x08'
Test case successful : True

Test Case 3: slave-->write and master-->read using readinto by sending out a write_byte=5
Slave wrote (rx): bytearray(b'\x08\x06\x04\x02\x08\x06\x04\x02')
Master expects (exp): b'\x08\x06\x04\x02\x08\x06\x04\x02'
Test case successful : True
30 changes: 17 additions & 13 deletions tests/psoc6/hw_ext/spi_slave.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
machine = os.uname().machine
if "CY8CPROTO-062-4343W" in machine:
slave_write_notify_pin = "P12_3" # Sends signals when master wants to write data
slave_read_notify_pin = "P12_4" # Polls pin to check if slave is writing data
slave_read_notify_pin = "P12_4" # interrupt pin to check if slave is writing data
sck_slave_pin = "P13_2"
mosi_slave_pin = "P13_0"
miso_slave_pin = "P13_1"
Expand Down Expand Up @@ -56,6 +56,17 @@ def spi_slave_configure():
mosi=mosi_slave_pin,
miso=miso_slave_pin,
)

"""spi_obj = SPISlave(
baudrate=1000000,
polarity=0,
phase=0,
bits=8,
firstbit=SPISlave.MSB,
sck=sck_slave_pin,
mosi=mosi_slave_pin,
miso=miso_slave_pin,
)"""
return spi_obj


Expand All @@ -76,8 +87,7 @@ def spi_half_duplex_communication(spi_obj, tx, rx):
# print("\n1) master-->write and slave-->read")
_wait_for_master_signal()
spi_obj.read(rx)

# print("rx: ", rx)
print("rx: ", rx)
# print("slave read successful : ", rx==tx)
_slave_ready_to_write()
spi_obj.write(rx)
Expand All @@ -89,25 +99,19 @@ def spi_half_duplex_communication(spi_obj, tx, rx):
spi_obj.write(tx_buf)

# 3) slave-->write and master-->read by sending a write_byte=0x12"
print("\n3) slave-->write and master-->read using readinto by sending out a write_byte=0x12")
# print("\n3) slave-->write and master-->read using readinto by sending out a write_byte=0x12")
tx = b"\x08\x06\x04\x02\x08\x06\x04\x02"
spi_obj.read(write_byte)
print("write_byte: ", write_byte)
spi_obj.write(tx)
print("OUT")

# spi_obj.read(write_byte)
# print("write_byte: ", write_byte)
# _slave_ready_to_write()


def spi_full_duplex_communication(spi_obj, tx, rx):
print("*** Full duplex communication ***")
exp_rx = b"\x08\x06\x04\x02\x07\x05\x03\x01"
print("\n1) master-->write and slave-->read continuously")
spi_obj.write(tx)
# spi_obj.write_readinto(tx, rx)
spi_obj.read(rx)
print(rx)
# _verify_test("")
spi_obj.write_readinto(tx, rx)


print("\n*** SPI SLAVE INSTANCE ***")
Expand Down

0 comments on commit a321ecb

Please sign in to comment.