Skip to content

Commit 4b3a2b2

Browse files
committed
fix(parlio): fix rempty interrupt during resetting fifo
Move the fifo reset to after disabling the tx core clock. And add external non-free running clock src test.
1 parent 1f84180 commit 4b3a2b2

File tree

7 files changed

+142
-5
lines changed

7 files changed

+142
-5
lines changed

components/esp_driver_parlio/src/parlio_tx.c

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -62,6 +62,7 @@ typedef struct parlio_tx_unit_t {
6262
#endif
6363
portMUX_TYPE spinlock; // prevent resource accessing by user and interrupt concurrently
6464
uint32_t out_clk_freq_hz; // output clock frequency
65+
parlio_clock_source_t clk_src; // Parallel IO internal clock source
6566
size_t max_transfer_bits; // maximum transfer size in bits
6667
size_t queue_depth; // size of transaction queue
6768
size_t num_trans_inflight; // indicates the number of transactions that are undergoing but not recycled to ready_queue
@@ -290,6 +291,7 @@ static esp_err_t parlio_select_periph_clock(parlio_tx_unit_t *tx_unit, const par
290291
if (tx_unit->out_clk_freq_hz != config->output_clk_freq_hz) {
291292
ESP_LOGW(TAG, "precision loss, real output frequency: %"PRIu32, tx_unit->out_clk_freq_hz);
292293
}
294+
tx_unit->clk_src = clk_src;
293295

294296
return ESP_OK;
295297
}
@@ -465,6 +467,18 @@ static void IRAM_ATTR parlio_tx_do_transaction(parlio_tx_unit_t *tx_unit, parlio
465467

466468
tx_unit->cur_trans = t;
467469

470+
// If the external clock is a non-free-running clock, it needs to be switched to the internal free-running clock first.
471+
// And then switched back to the actual clock after the reset is completed.
472+
bool switch_clk = tx_unit->clk_src == PARLIO_CLK_SRC_EXTERNAL ? true : false;
473+
if (switch_clk) {
474+
PARLIO_CLOCK_SRC_ATOMIC() {
475+
parlio_ll_tx_set_clock_source(hal->regs, PARLIO_CLK_SRC_XTAL);
476+
}
477+
}
478+
PARLIO_RCC_ATOMIC() {
479+
parlio_ll_tx_reset_clock(hal->regs);
480+
}
481+
468482
// DMA transfer data based on bytes not bits, so convert the bit length to bytes, round up
469483
gdma_buffer_mount_config_t mount_config = {
470484
.buffer = (void *)t->payload,
@@ -474,14 +488,21 @@ static void IRAM_ATTR parlio_tx_do_transaction(parlio_tx_unit_t *tx_unit, parlio
474488
.mark_final = true, // singly link list, mark final descriptor
475489
}
476490
};
491+
// Since the threshold of the clock divider counter is not updated simultaneously with the clock source switching.
492+
// The update of the threshold relies on the moment when the counter reaches the threshold each time.
493+
// We place gdma_link_mount_buffers between reset clock and disable clock to ensure enough time for updating the threshold of the clock divider counter.
477494
gdma_link_mount_buffers(tx_unit->dma_link, 0, &mount_config, 1, NULL);
478-
parlio_ll_tx_reset_fifo(hal->regs);
479-
PARLIO_RCC_ATOMIC() {
480-
parlio_ll_tx_reset_clock(hal->regs);
495+
496+
if (switch_clk) {
497+
PARLIO_CLOCK_SRC_ATOMIC() {
498+
parlio_ll_tx_set_clock_source(hal->regs, PARLIO_CLK_SRC_EXTERNAL);
499+
}
481500
}
482501
PARLIO_CLOCK_SRC_ATOMIC() {
483502
parlio_ll_tx_enable_clock(hal->regs, false);
484503
}
504+
// reset tx fifo after disabling tx core clk to avoid unexpected rempty interrupt
505+
parlio_ll_tx_reset_fifo(hal->regs);
485506
parlio_ll_tx_set_idle_data_value(hal->regs, t->idle_value);
486507
parlio_ll_tx_set_trans_bit_len(hal->regs, t->payload_bits);
487508

components/esp_driver_parlio/test_apps/parlio/main/test_board.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ extern "C" {
2222

2323
#if CONFIG_IDF_TARGET_ESP32C6
2424
#define TEST_CLK_GPIO 10
25+
#define TEST_EXT_CLK_GPIO 12
2526
#define TEST_VALID_GPIO 11
2627
#define TEST_DATA0_GPIO 0
2728
#define TEST_DATA1_GPIO 1
@@ -33,6 +34,7 @@ extern "C" {
3334
#define TEST_DATA7_GPIO 7
3435
#elif CONFIG_IDF_TARGET_ESP32C5
3536
#define TEST_CLK_GPIO 25
37+
#define TEST_EXT_CLK_GPIO 10
3638
#define TEST_VALID_GPIO 26
3739
#define TEST_DATA0_GPIO 0
3840
#define TEST_DATA1_GPIO 1
@@ -44,6 +46,7 @@ extern "C" {
4446
#define TEST_DATA7_GPIO 7
4547
#elif CONFIG_IDF_TARGET_ESP32H2
4648
#define TEST_VALID_GPIO 2
49+
#define TEST_EXT_CLK_GPIO 4
4750
#define TEST_CLK_GPIO 3
4851
#define TEST_DATA0_GPIO 8
4952
#define TEST_DATA1_GPIO 5
@@ -55,6 +58,7 @@ extern "C" {
5558
#define TEST_DATA7_GPIO 12
5659
#elif CONFIG_IDF_TARGET_ESP32P4
5760
#define TEST_CLK_GPIO 33
61+
#define TEST_EXT_CLK_GPIO 34
5862
#define TEST_VALID_GPIO 32
5963
#define TEST_DATA0_GPIO 24
6064
#define TEST_DATA1_GPIO 25

components/esp_driver_parlio/test_apps/parlio/main/test_parlio_tx.c

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -11,9 +11,11 @@
1111
#include "unity.h"
1212
#include "driver/parlio_tx.h"
1313
#include "driver/gpio.h"
14+
#include "hal/parlio_ll.h"
1415
#include "soc/soc_caps.h"
1516
#include "esp_attr.h"
1617
#include "test_board.h"
18+
#include "soc/parl_io_struct.h"
1719

1820
TEST_CASE("parallel_tx_unit_install_uninstall", "[parlio_tx]")
1921
{
@@ -338,3 +340,109 @@ TEST_CASE("parlio can transmit PSRAM buffer", "[parlio_tx]")
338340
free(buffer);
339341
}
340342
#endif // SOC_PSRAM_DMA_CAPABLE
343+
344+
static void test_gpio_simulate_rising_edge(int gpio_sig, size_t times)
345+
{
346+
while (times--) {
347+
gpio_set_level(gpio_sig, 0);
348+
gpio_set_level(gpio_sig, 1);
349+
gpio_set_level(gpio_sig, 0);
350+
}
351+
}
352+
353+
static uint8_t test_gpio_get_output_data(gpio_num_t* gpio, size_t gpio_num)
354+
{
355+
uint8_t result = 0;
356+
for (size_t i = 0; i < gpio_num; i++) {
357+
int level = gpio_get_level(gpio[i]);
358+
result |= level << i;
359+
}
360+
return result;
361+
}
362+
363+
static void test_use_external_non_free_running_clock(parlio_tx_unit_handle_t tx_unit, parlio_tx_unit_config_t config, int test_round)
364+
{
365+
uint32_t clock_div = config.input_clk_src_freq_hz / config.output_clk_freq_hz;
366+
TEST_ESP_OK(parlio_new_tx_unit(&config, &tx_unit));
367+
TEST_ESP_OK(parlio_tx_unit_enable(tx_unit));
368+
// let core clock running for a while to update the clock divider threshold
369+
esp_rom_delay_us(100);
370+
parlio_transmit_config_t transmit_config = {
371+
.idle_value = 0xAA,
372+
};
373+
__attribute__((aligned(64))) uint8_t payload[256] = {0};
374+
for (int i = 0; i < 256; i++) {
375+
payload[i] = i;
376+
}
377+
378+
for (int round = 0; round < test_round; round++) {
379+
TEST_ESP_OK(parlio_tx_unit_transmit(tx_unit, payload, 256 * sizeof(uint8_t) * 8, &transmit_config));
380+
for (int i = 0; i < 256; i++) {
381+
// After "clock_div" times external pulses pass through the internal frequency divider, the parlio core clock generates a single pulse.
382+
test_gpio_simulate_rising_edge(TEST_EXT_CLK_GPIO, clock_div);
383+
TEST_ASSERT_EQUAL(i, test_gpio_get_output_data(config.data_gpio_nums, config.data_width));
384+
}
385+
// In order to update the idle value, an additional rising edge is required
386+
test_gpio_simulate_rising_edge(TEST_EXT_CLK_GPIO, clock_div);
387+
TEST_ASSERT_EQUAL(transmit_config.idle_value, test_gpio_get_output_data(config.data_gpio_nums, config.data_width));
388+
TEST_ESP_OK(parlio_tx_unit_wait_all_done(tx_unit, 100));
389+
}
390+
TEST_ESP_OK(parlio_tx_unit_disable(tx_unit));
391+
TEST_ESP_OK(parlio_del_tx_unit(tx_unit));
392+
}
393+
394+
TEST_CASE("parallel tx unit use external non-free running clock", "[parlio_tx]")
395+
{
396+
printf("use gpio as external clock source\r\n");
397+
// configure the data gpio for loopback test
398+
gpio_config_t gpio_conf = {
399+
.mode = GPIO_MODE_INPUT,
400+
.pin_bit_mask = BIT64(TEST_DATA0_GPIO) | BIT64(TEST_DATA1_GPIO) | BIT64(TEST_DATA2_GPIO) | BIT64(TEST_DATA3_GPIO) |
401+
BIT64(TEST_DATA4_GPIO) | BIT64(TEST_DATA5_GPIO) | BIT64(TEST_DATA6_GPIO) | BIT64(TEST_DATA7_GPIO),
402+
};
403+
TEST_ESP_OK(gpio_config(&gpio_conf));
404+
// configure the external clock output gpio
405+
gpio_conf.mode = GPIO_MODE_OUTPUT;
406+
gpio_conf.pin_bit_mask = BIT64(TEST_EXT_CLK_GPIO);
407+
TEST_ESP_OK(gpio_config(&gpio_conf));
408+
409+
printf("install parlio tx unit\r\n");
410+
parlio_tx_unit_handle_t tx_unit = NULL;
411+
parlio_tx_unit_config_t config = {
412+
.clk_src = PARLIO_CLK_SRC_DEFAULT,
413+
.data_width = 8,
414+
.clk_in_gpio_num = TEST_EXT_CLK_GPIO,
415+
.input_clk_src_freq_hz = 80 * 1000 * 1000, // Note that this is not the real input frequency, we just use it to calculate the clock divider
416+
.valid_gpio_num = -1, // don't generate valid signal
417+
.clk_out_gpio_num = TEST_CLK_GPIO,
418+
.data_gpio_nums = {
419+
TEST_DATA0_GPIO,
420+
TEST_DATA1_GPIO,
421+
TEST_DATA2_GPIO,
422+
TEST_DATA3_GPIO,
423+
TEST_DATA4_GPIO,
424+
TEST_DATA5_GPIO,
425+
TEST_DATA6_GPIO,
426+
TEST_DATA7_GPIO,
427+
},
428+
.output_clk_freq_hz = 1 * 1000 * 1000, // For the same reason, this is not the real output frequency
429+
.trans_queue_depth = 8,
430+
.max_transfer_size = 256,
431+
.bit_pack_order = PARLIO_BIT_PACK_ORDER_LSB,
432+
.sample_edge = PARLIO_SAMPLE_EDGE_POS,
433+
};
434+
435+
uint8_t test_round = 50;
436+
printf("test input clk freq is greater than output clk freq\r\n");
437+
test_use_external_non_free_running_clock(tx_unit, config, test_round);
438+
439+
// changes input clk freq
440+
config.input_clk_src_freq_hz = 1 * 1000 * 1000;
441+
printf("test special condition, input clk freq equals to output clk freq\r\n");
442+
test_use_external_non_free_running_clock(tx_unit, config, test_round);
443+
444+
TEST_ESP_OK(gpio_reset_pin(TEST_EXT_CLK_GPIO));
445+
for (int i = 0; i < 8; i++) {
446+
TEST_ESP_OK(gpio_reset_pin(config.data_gpio_nums[i]));
447+
}
448+
};

components/hal/esp32c5/include/hal/parlio_ll.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ static inline uint32_t parlio_ll_rx_get_fifo_cycle_cnt(parl_io_dev_t *dev)
397397
* @param dev Parallel IO register base address
398398
* @param src Clock source
399399
*/
400+
__attribute__((always_inline))
400401
static inline void parlio_ll_tx_set_clock_source(parl_io_dev_t *dev, parlio_clock_source_t src)
401402
{
402403
(void)dev;

components/hal/esp32c6/include/hal/parlio_ll.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ static inline void parlio_ll_rx_update_config(parl_io_dev_t *dev)
373373
* @param dev Parallel IO register base address
374374
* @param src Clock source
375375
*/
376+
__attribute__((always_inline))
376377
static inline void parlio_ll_tx_set_clock_source(parl_io_dev_t *dev, parlio_clock_source_t src)
377378
{
378379
(void)dev;

components/hal/esp32h2/include/hal/parlio_ll.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ static inline uint32_t parlio_ll_rx_get_fifo_cycle_cnt(parl_io_dev_t *dev)
398398
* @param dev Parallel IO register base address
399399
* @param src Clock source
400400
*/
401+
__attribute__((always_inline))
401402
static inline void parlio_ll_tx_set_clock_source(parl_io_dev_t *dev, parlio_clock_source_t src)
402403
{
403404
(void)dev;

components/hal/esp32p4/include/hal/parlio_ll.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ static inline uint32_t parlio_ll_rx_get_fifo_cycle_cnt(parl_io_dev_t *dev)
427427
* @param dev Parallel IO register base address
428428
* @param src Clock source
429429
*/
430+
__attribute__((always_inline))
430431
static inline void _parlio_ll_tx_set_clock_source(parl_io_dev_t *dev, parlio_clock_source_t src)
431432
{
432433
(void)dev;

0 commit comments

Comments
 (0)