Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hil tests pdm pcm #195

Merged
merged 18 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/ports_psoc6.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ jobs:
source tools/ci.sh && ci_psoc6_flash_multiple_devices ${{ matrix.board }} firmware.hex tools/psoc6/${{ runner.name }}-devs.yml
- name: Run psoc6 tests
timeout-minutes: 12
run: |
run: |
./tests/ports/psoc6/run_psoc6_tests.sh --test-suite ci-tests --board ${{ matrix.board }} --hil ${{ runner.name }}
#./tests/ports/psoc6/run_psoc6_tests.sh --test-suite pdm_pcm --board ${{ matrix.board }} --hil ${{ runner.name }}

- name: Container teardown
if: failure() || success()
run: |
Expand Down
13 changes: 1 addition & 12 deletions docs/psoc6/quickref.rst
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,7 @@ Methods
^^^^^^^^

.. method:: PDM_PCM.init()

Starts the PDM_PCM hardware block and conversion operation.

.. note::
Expand Down Expand Up @@ -695,18 +696,6 @@ Constants

for initialising the PDM_PCM ``bits`` to 16

.. data:: PDM_PCM.BITS_18

for initialising the PDM_PCM ``bits`` to 18

.. data:: PDM_PCM.BITS_20

for initialising the PDM_PCM ``bits`` to 20

.. data:: PDM_PCM.BITS_24

for initialising the PDM_PCM ``bits`` to 24


UART
----
Expand Down
87 changes: 5 additions & 82 deletions ports/psoc6/machine_pdm_pcm.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ size_t ringbuf_available_space(ring_buf_t *rbuf) {
static uint32_t fill_appbuf_from_ringbuf(machine_pdm_pcm_obj_t *self, mp_buffer_info_t *appbuf) {
uint32_t num_bytes_copied_to_appbuf = 0;
uint8_t *app_p = (uint8_t *)appbuf->buf;
uint8_t appbuf_sample_size_in_bytes = (self->bits == 16? 2 : 4) * (self->format == STEREO ? 2: 1);
uint8_t appbuf_sample_size_in_bytes = (self->bits == 16? 2 : 3) * (self->format == STEREO ? 2: 1);
uint32_t num_bytes_needed_from_ringbuf = appbuf->len * (PDM_PCM_RX_FRAME_SIZE_IN_BYTES / appbuf_sample_size_in_bytes);
uint8_t discard_byte;
while (num_bytes_needed_from_ringbuf) {
Expand Down Expand Up @@ -123,7 +123,7 @@ static void fill_appbuf_from_ringbuf_non_blocking(machine_pdm_pcm_obj_t *self) {
uint32_t num_bytes_copied_to_appbuf = 0;
uint8_t *app_p = &(((uint8_t *)self->non_blocking_descriptor.appbuf.buf)[self->non_blocking_descriptor.index]);

uint8_t appbuf_sample_size_in_bytes = (self->bits == 16? 2 : 4) * (self->format == STEREO ? 2: 1);
uint8_t appbuf_sample_size_in_bytes = (self->bits == 16? 2 : 3) * (self->format == STEREO ? 2: 1);
uint32_t num_bytes_remaining_to_copy_to_appbuf = self->non_blocking_descriptor.appbuf.len - self->non_blocking_descriptor.index;
uint32_t num_bytes_remaining_to_copy_from_ring_buffer = num_bytes_remaining_to_copy_to_appbuf *
(PDM_PCM_RX_FRAME_SIZE_IN_BYTES / appbuf_sample_size_in_bytes);
Expand Down Expand Up @@ -330,7 +330,7 @@ uint8_t pdm_pcm_get_word_byte_size(machine_pdm_pcm_obj_t *self) {
if (res_bits == 16) {
return 2;
} else {
return 4;
return 3;
}
}

Expand All @@ -352,50 +352,6 @@ static void pdm_pcm_init(machine_pdm_pcm_obj_t *self, cyhal_clock_t *clock) {
pdm_pcm_assert_raise_val("PDM_PCM initialisation failed with return code %lx !", result);
}

// Initialize audio clock
void pdm_pcm_audio_clock_init(uint32_t audio_clock_freq_hz) {
mplogger_print("pdm_pcm_audio_clock_init \r\n");
cyhal_clock_t pll_clock;
cy_rslt_t result;

static bool clock_set = false;

result = cyhal_clock_reserve(&pll_clock, &CYHAL_CLOCK_PLL[0]);
pdm_pcm_assert_raise_val("PLL clock reserve failed with error code: %lx", result);

uint32_t pll_source_clock_freq_hz = cyhal_clock_get_frequency(&pll_clock);

if (audio_clock_freq_hz != pll_source_clock_freq_hz) {
mp_printf(&mp_plat_print, "machine.PDM_PCM: PLL0 freq is changed to %lu. This will affect all resources clock freq sourced by PLL0.\n", audio_clock_freq_hz);
clock_set = false;
pll_source_clock_freq_hz = audio_clock_freq_hz;
}

if (!clock_set) {
result = cyhal_clock_set_frequency(&pll_clock, pll_source_clock_freq_hz, NULL);
pdm_pcm_assert_raise_val("Set PLL clock frequency failed with error code: %lx", result);
if (!cyhal_clock_is_enabled(&pll_clock)) {
result = cyhal_clock_set_enabled(&pll_clock, true, true);
pdm_pcm_assert_raise_val("PLL clock enable failed with error code: %lx", result);
}

result = cyhal_clock_reserve(&pdm_pcm_audio_clock, &CYHAL_CLOCK_HF[1]);
pdm_pcm_assert_raise_val("HF1 clock reserve failed with error code: %lx", result);
result = cyhal_clock_set_source(&pdm_pcm_audio_clock, &pll_clock);
pdm_pcm_assert_raise_val("HF1 clock sourcing failed with error code: %lx", result);

result = cyhal_clock_set_enabled(&pdm_pcm_audio_clock, true, true);
pdm_pcm_assert_raise_val("HF1 clock enable failed with error code: %lx", result);

cyhal_clock_free(&pdm_pcm_audio_clock); // Check the impact while read function

clock_set = true;
}

cyhal_clock_free(&pll_clock);
cyhal_system_delay_ms(1);
}

// Set PDM_PCM async mode to DMA
static void pdm_pcm_set_async_mode_dma(machine_pdm_pcm_obj_t *self) {
mplogger_print("pdm_pcm_set_async_mode_dma \r\n");
Expand Down Expand Up @@ -427,7 +383,6 @@ static void pdm_pcm_irq_handler(void *arg, cyhal_pdm_pcm_event_t event) {
_dma_swap_active_dmabuf(self);
pdm_pcm_read_rxbuf(self);
_dma_copy_from_dmabuf_to_ringbuf(self);

if ((self->io_mode == NON_BLOCKING) && (self->non_blocking_descriptor.copy_in_progress)) {
fill_appbuf_from_ringbuf_non_blocking(self);
}
Expand All @@ -445,16 +400,13 @@ int8_t get_frame_mapping_index(int8_t bits, format_t format) {
if ((format == MONO_LEFT) | (format == MONO_RIGHT)) {
if (bits == 16) {
return 0;
} else { // >16 bits
return 1;
}
} else { // STEREO
if (bits == 16) {
return 2;
} else { // >16 bits
return 3;
}
}
return -1;
}

// =======================================================================================
Expand Down Expand Up @@ -511,21 +463,7 @@ static void mp_machine_pdm_pcm_init_helper(machine_pdm_pcm_obj_t *self, mp_arg_v
self->sample_rate = args[ARG_sample_rate].u_int;
self->decimation_rate = args[ARG_decimation_rate].u_int;

// Set clock values
uint32_t audio_clock_freq_hz;
uint32_t rate = args[ARG_sample_rate].u_int;
if (rate == 8000 ||
rate == 16000 ||
rate == 48000) {
audio_clock_freq_hz = AUDIO_SYS_CLOCK_24_576_000_HZ;
} else if (rate == 22050 ||
rate == 44100) {
audio_clock_freq_hz = AUDIO_SYS_CLOCK_22_579_000_HZ;
} else {
mp_raise_ValueError(MP_ERROR_TEXT("rate not supported"));
}

int32_t ring_buffer_len = 20000; // 2048
int32_t ring_buffer_len = 20000;
if (ring_buffer_len < 0) {
mp_raise_ValueError(MP_ERROR_TEXT("invalid ibuf"));
}
Expand All @@ -534,7 +472,6 @@ static void mp_machine_pdm_pcm_init_helper(machine_pdm_pcm_obj_t *self, mp_arg_v
self->io_mode = BLOCKING;

ringbuf_init(&self->ring_buffer, ring_buffer_len);
pdm_pcm_audio_clock_init(audio_clock_freq_hz);
pdm_pcm_init(self, &pdm_pcm_audio_clock);
pdm_pcm_irq_configure(self);
pdm_pcm_set_async_mode_dma(self);
Expand Down Expand Up @@ -570,17 +507,6 @@ static void mp_machine_pdm_pcm_irq_update(machine_pdm_pcm_obj_t *self) {

// =======================================================================================
// Implementation for stream protocol (ports/psoc6)
void read_appbuf_value(uint8_t value, mp_buffer_info_t appbuf) {
if (value == 1) {
mp_printf(&mp_plat_print, "\nAppbuf Before : ");
} else {
mp_printf(&mp_plat_print, "\nAppbuf After : ");
}
for (uint32_t i = 0; i < appbuf.len; i++) {
mp_printf(&mp_plat_print, "0x%x ", ((uint8_t *)appbuf.buf)[i]);
}
}

static mp_uint_t machine_pdm_pcm_stream_read(mp_obj_t self_in, void *buf_in, mp_uint_t size, int *errcode) {
machine_pdm_pcm_obj_t *self = MP_OBJ_TO_PTR(self_in);

Expand Down Expand Up @@ -638,9 +564,6 @@ static const mp_rom_map_elem_t machine_pdm_pcm_locals_dict_table[] = {
// Constants
// Word lengths
{ MP_ROM_QSTR(MP_QSTR_BITS_16), MP_ROM_INT(BITS_16) },
{ MP_ROM_QSTR(MP_QSTR_BITS_18), MP_ROM_INT(BITS_18) },
{ MP_ROM_QSTR(MP_QSTR_BITS_20), MP_ROM_INT(BITS_20) },
{ MP_ROM_QSTR(MP_QSTR_BITS_24), MP_ROM_INT(BITS_24) },

// Modes
{ MP_ROM_QSTR(MP_QSTR_STEREO), MP_ROM_INT(STEREO) },
Expand Down
2 changes: 1 addition & 1 deletion ports/psoc6/machine_pdm_pcm.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ static void mp_machine_pdm_pcm_irq_update(machine_pdm_pcm_obj_t *self);

int8_t get_frame_mapping_index(int8_t bits, format_t format);

// Should change to MONO_LEFT and MONO_RIGHT when right mic works
// Only 16 bit mono and stereo modes tested
static const int8_t pdm_pcm_frame_map[4][PDM_PCM_RX_FRAME_SIZE_IN_BYTES] = {
{ 0, 1, -1, -1, -1, -1, -1, -1 }, // Mono, 16-bits
{ 0, 1, 2, -1, -1, -1, -1, -1 }, // Mono, >16-bits
Expand Down
1 change: 0 additions & 1 deletion ports/psoc6/modmachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,6 @@ void audio_pdm_set_frequency(uint32_t freq) {
if (freq != 24576000 && freq != 22579000) {
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("Invalid PLL0 frequency %lu"), freq);
}
cyhal_clock_t pdm_pcm_audio_clock;
cyhal_clock_t pll_clock;
cy_rslt_t result;

Expand Down
1 change: 1 addition & 0 deletions ports/psoc6/modmachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ mp_obj_t system_reset_cause(void);

extern void audio_i2s_set_frequency(uint32_t audio_clock_freq_hz);
extern cyhal_clock_t audio_clock;
extern cyhal_clock_t pdm_pcm_audio_clock;
extern bool clock_set_i2s;
extern bool clock_set_pdm;

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 0 additions & 28 deletions tests/ports/psoc6/board_ext_hw/multi/pdm_pcm.py

This file was deleted.

1 change: 0 additions & 1 deletion tests/ports/psoc6/board_ext_hw/multi/pdm_pcm.py.exp

This file was deleted.

123 changes: 123 additions & 0 deletions tests/ports/psoc6/board_ext_hw/multi/pdm_pcm_rx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import os
import machine
from machine import PDM_PCM, Pin
import time

# Allocate pin based on board
board = os.uname().machine
if "CY8CPROTO-062-4343W with PSoC62" in board:
clk_pin = "P10_4"
data_pin = "P10_5"
send_signal_to_tx_pin = "P10_3"
elif "CY8CPROTO-063-BLE" in board:
print("SKIP")
raise SystemExit
elif "CY8CKIT-062S2-AI" in board:
print("SKIP")
raise SystemExit

print("*** PDM_PCM tests - RX ***")

send_signal = Pin(send_signal_to_tx_pin, mode=Pin.OUT, pull=Pin.PULL_DOWN, value=False)
send_signal.value(0)


def generate_exp_seq(data):
exp_seq = bytearray(data * 64)
return exp_seq


print("1. blocking read implementation ")

_sampling_rate = [8000, 16000, 32000, 48000, 22050, 44100]
_mode = [PDM_PCM.MONO_LEFT, PDM_PCM.MONO_RIGHT, PDM_PCM.STEREO]
_bits = 16
exp_data = [[0x00], [0xFF]]
iterations = 100
rounds = 2

for m in range(rounds):
exp_seq = generate_exp_seq(exp_data[m])
if m == 0:
print("*** Test for data high ***")
else:
print("*** Test for data low ***")

for i in range(len(_sampling_rate)): # Check for all sampling rates
set_sampling_rate = _sampling_rate[i]
if _sampling_rate[i] == 22050 or _sampling_rate[i] == 44100:
machine.freq(machine.AUDIO_PDM_22_579_000_HZ)
else:
machine.freq(machine.AUDIO_PDM_24_576_000_HZ)
for j in range(len(_mode)): # Check for all modes
set_mode = _mode[j]
if set_mode == PDM_PCM.STEREO:
iterations = 200
pdm_pcm = PDM_PCM(
0,
sck=clk_pin,
data=data_pin,
sample_rate=set_sampling_rate,
decimation_rate=64,
bits=PDM_PCM.BITS_16,
format=set_mode,
left_gain=0,
right_gain=0,
)
pdm_pcm.init() # Start

for k in range(iterations):
rx_buf = bytearray([1] * 64)
num_read = pdm_pcm.readinto(rx_buf)
# print("buf :", ''.join(f'{x:02x} ' for x in rx_buf))
if rx_buf[:3] == bytearray([exp_data[m][0], exp_data[m][0], exp_data[m][0]]):
is_seq_received = rx_buf == exp_seq
if is_seq_received:
print(
f"data received for mode = {set_mode}, bits = PDM_PCM.BITS_16, rate = {set_sampling_rate} : {is_seq_received}"
)
break
pdm_pcm.deinit()
send_signal.high()
time.sleep(1) # Wait to sync

send_signal.deinit()

###############################################################################
print("\n2. irq non-blocking read implementation ")

rx_done = False


def rx_complete_irq(obj):
global rx_done
rx_done = True


machine.freq(machine.AUDIO_PDM_24_576_000_HZ)
pdm_pcm = PDM_PCM(
0,
sck=clk_pin,
data=data_pin,
sample_rate=set_sampling_rate,
decimation_rate=64,
bits=PDM_PCM.BITS_16,
format=set_mode,
left_gain=0,
right_gain=0,
)

pdm_pcm.init() # Start

rx_buf = bytearray([0] * 64)
pdm_pcm.irq(rx_complete_irq)
num_read = pdm_pcm.readinto(rx_buf)

while not rx_done:
pass

# if we get pass this rx_done flag has been
# modified by the interrupt

print("rx blocking done")
pdm_pcm.deinit()
Loading
Loading