Skip to content

Commit 21a8c22

Browse files
committed
feat(bitscrambler): add driver support on esp32c5
1 parent 706b5e4 commit 21a8c22

File tree

18 files changed

+684
-46
lines changed

18 files changed

+684
-46
lines changed

components/esp_driver_bitscrambler/src/bitscrambler.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ static void enable_clocks(bitscrambler_t *bs)
100100
if (ref_count == 0) { //we're the first to enable the BitScrambler module
101101
bitscrambler_ll_set_bus_clock_sys_enable(1);
102102
bitscrambler_ll_reset_sys();
103+
bitscrambler_ll_mem_power_by_pmu();
103104
}
104105
if (bs->cfg.dir == BITSCRAMBLER_DIR_RX || bs->loopback) {
105106
bitscrambler_ll_set_bus_clock_rx_enable(1);
@@ -123,6 +124,7 @@ static void disable_clocks(bitscrambler_t *bs)
123124
}
124125
if (ref_count == 0) { //we're the last to disable the BitScrambler module
125126
bitscrambler_ll_set_bus_clock_sys_enable(0);
127+
bitscrambler_ll_mem_force_power_off();
126128
}
127129
}
128130
}
@@ -221,7 +223,7 @@ esp_err_t bitscrambler_load_program(bitscrambler_handle_t bs, const void *progra
221223

222224
//Set options from header
223225
bitscrambler_ll_set_lut_width(bs->hw, bs->cfg.dir, hdr.lut_width);
224-
bitscrambler_ll_set_prefetch_mode(bs->hw, bs->cfg.dir, hdr.prefetch ? BITSCRAMBLER_PREFETCH_ENABLED : BITSCRAMBLER_PREFETCH_DISABLED);
226+
bitscrambler_ll_enable_prefetch_on_reset(bs->hw, bs->cfg.dir, hdr.prefetch);
225227
bitscrambler_ll_set_eof_mode(bs->hw, bs->cfg.dir, hdr.eof_on);
226228
bitscrambler_ll_set_tailing_bits(bs->hw, bs->cfg.dir, hdr.trailing_bits);
227229
//fixed options
@@ -298,7 +300,7 @@ esp_err_t bitscrambler_reset(bitscrambler_handle_t handle)
298300
//If the halt bit is set, the Bitscrambler should (eventually) go to idle state. If it
299301
//does not, something got stuck.
300302
int timeout = BITSCRAMBLER_RESET_ITERATIONS;
301-
while ((bitscrambler_ll_current_state(handle->hw, handle->cfg.dir) != BITSCRAMBLER_STATE_IDLE) && timeout != 0) {
303+
while ((bitscrambler_ll_get_current_state(handle->hw, handle->cfg.dir) != BITSCRAMBLER_STATE_IDLE) && timeout != 0) {
302304
timeout--;
303305
}
304306
if (timeout == 0) {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
#include "soc/gdma_channel.h"
7+
#include "bitscrambler_soc_specific.h"
8+
9+
// Note: these are indexed by the values of the SOC_BITSCRAMBLER_ATTACH_ defines
10+
// in soc/bitscrambler_peri_select.h
11+
// This map is used by the bitscrambler loopback driver only.
12+
13+
const bitscrambler_periph_desc_t g_bitscrambler_periph_desc[] = {
14+
[SOC_BITSCRAMBLER_ATTACH_GPSPI2] = {GDMA_MAKE_TRIGGER(GDMA_TRIG_PERIPH_SPI, 2), SOC_GDMA_TRIG_PERIPH_SPI2_BUS},
15+
[SOC_BITSCRAMBLER_ATTACH_UHCI] = {GDMA_MAKE_TRIGGER(GDMA_TRIG_PERIPH_UHCI, 0), SOC_GDMA_TRIG_PERIPH_UHCI0_BUS},
16+
[SOC_BITSCRAMBLER_ATTACH_I2S0] = {GDMA_MAKE_TRIGGER(GDMA_TRIG_PERIPH_I2S, 0), SOC_GDMA_TRIG_PERIPH_I2S0_BUS},
17+
[SOC_BITSCRAMBLER_ATTACH_AES] = {GDMA_MAKE_TRIGGER(GDMA_TRIG_PERIPH_AES, 0), SOC_GDMA_TRIG_PERIPH_AES0_BUS},
18+
[SOC_BITSCRAMBLER_ATTACH_SHA] = {GDMA_MAKE_TRIGGER(GDMA_TRIG_PERIPH_SHA, 0), SOC_GDMA_TRIG_PERIPH_SHA0_BUS},
19+
[SOC_BITSCRAMBLER_ATTACH_ADC] = {GDMA_MAKE_TRIGGER(GDMA_TRIG_PERIPH_ADC, 0), SOC_GDMA_TRIG_PERIPH_ADC0_BUS},
20+
[SOC_BITSCRAMBLER_ATTACH_PARL_IO] = {GDMA_MAKE_TRIGGER(GDMA_TRIG_PERIPH_PARLIO, 0), SOC_GDMA_TRIG_PERIPH_PARLIO0_BUS},
21+
};

components/esp_driver_bitscrambler/src/bitscrambler_loopback.c

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@
1111
#include "esp_private/gdma_link.h"
1212
#include "esp_private/bitscrambler.h"
1313
#include "hal/dma_types.h"
14+
#include "hal/cache_hal.h"
15+
#include "hal/cache_ll.h"
1416
#include "bitscrambler_private.h"
1517
#include "bitscrambler_soc_specific.h"
1618
#include "esp_err.h"
1719
#include "esp_check.h"
1820
#include "esp_heap_caps.h"
1921
#include "esp_cache.h"
2022
#include "esp_dma_utils.h"
23+
#include "esp_memory_utils.h"
2124

2225
const static char *TAG = "bs_loop";
2326

@@ -97,7 +100,11 @@ esp_err_t bitscrambler_loopback_create(bitscrambler_handle_t *handle, int attach
97100
bs->max_transfer_sz_bytes = max_transfer_sz_bytes;
98101
int desc_ct = (max_transfer_sz_bytes + DMA_DESCRIPTOR_BUFFER_MAX_SIZE_4B_ALIGNED - 1) / DMA_DESCRIPTOR_BUFFER_MAX_SIZE_4B_ALIGNED;
99102
int bus = g_bitscrambler_periph_desc[attach_to].bus;
103+
#ifdef SOC_GDMA_BUS_AXI
100104
size_t align = (bus == SOC_GDMA_BUS_AXI) ? 8 : 4;
105+
#else
106+
size_t align = 4;
107+
#endif
101108

102109
// create DMA link list for TX and RX
103110
gdma_link_list_config_t dma_link_cfg = {
@@ -239,10 +246,16 @@ esp_err_t bitscrambler_loopback_run(bitscrambler_handle_t bs, void *buffer_in, s
239246
};
240247
gdma_link_mount_buffers(bsl->rx_link_list, 0, &out_buf_mount_config, 1, NULL);
241248

242-
//Note: we add the ESP_CACHE_MSYNC_FLAG_UNALIGNED flag for now as otherwise esp_cache_msync will complain about
243-
//the size not being aligned... we miss out on a check to see if the address is aligned this way. This needs to
244-
//be improved, but potentially needs a fix in esp_cache_msync not to check the size.
245-
esp_cache_msync(buffer_in, length_bytes_in, ESP_CACHE_MSYNC_FLAG_DIR_C2M | ESP_CACHE_MSYNC_FLAG_UNALIGNED);
249+
int int_mem_cache_line_size = cache_hal_get_cache_line_size(CACHE_LL_LEVEL_INT_MEM, CACHE_TYPE_DATA);
250+
int ext_mem_cache_line_size = cache_hal_get_cache_line_size(CACHE_LL_LEVEL_EXT_MEM, CACHE_TYPE_DATA);
251+
252+
bool need_cache_sync = esp_ptr_internal(buffer_in) ? (int_mem_cache_line_size > 0) : (ext_mem_cache_line_size > 0);
253+
if (need_cache_sync) {
254+
//Note: we add the ESP_CACHE_MSYNC_FLAG_UNALIGNED flag for now as otherwise esp_cache_msync will complain about
255+
//the size not being aligned... we miss out on a check to see if the address is aligned this way. This needs to
256+
//be improved, but potentially needs a fix in esp_cache_msync not to check the size.
257+
esp_cache_msync(buffer_in, length_bytes_in, ESP_CACHE_MSYNC_FLAG_DIR_C2M | ESP_CACHE_MSYNC_FLAG_UNALIGNED);
258+
}
246259

247260
gdma_start(bsl->rx_channel, gdma_link_get_head_addr(bsl->rx_link_list));
248261
gdma_start(bsl->tx_channel, gdma_link_get_head_addr(bsl->tx_link_list));
@@ -258,7 +271,10 @@ esp_err_t bitscrambler_loopback_run(bitscrambler_handle_t bs, void *buffer_in, s
258271
ret = ESP_ERR_TIMEOUT;
259272
}
260273

261-
esp_cache_msync(buffer_out, length_bytes_out, ESP_CACHE_MSYNC_FLAG_DIR_M2C);
274+
need_cache_sync = esp_ptr_internal(buffer_out) ? (int_mem_cache_line_size > 0) : (ext_mem_cache_line_size > 0);
275+
if (need_cache_sync) {
276+
esp_cache_msync(buffer_out, length_bytes_out, ESP_CACHE_MSYNC_FLAG_DIR_M2C);
277+
}
262278

263279
if (bytes_written) {
264280
*bytes_written = gdma_link_count_buffer_size_till_eof(bsl->rx_link_list, 0);

components/esp_driver_bitscrambler/test_apps/bitscrambler/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@ set(COMPONENTS main)
66

77
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
88
project(test_bitscrambler)
9+
10+
message(STATUS "Checking bitscrambler registers are not read-write by half-word")
11+
include($ENV{IDF_PATH}/tools/ci/check_register_rw_half_word.cmake)
12+
check_register_rw_half_word(SOC_MODULES "bitscrambler" "pcr" "hp_sys_clkrst"
13+
HAL_MODULES "bitscrambler")
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
| Supported Targets | ESP32-P4 |
2-
| ----------------- | -------- |
1+
| Supported Targets | ESP32-C5 | ESP32-P4 |
2+
| ----------------- | -------- | -------- |
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
# SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
1+
# SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
22
# SPDX-License-Identifier: CC0-1.0
33
import pytest
44
from pytest_embedded import Dut
55

66

77
@pytest.mark.esp32p4
8+
@pytest.mark.esp32c5
89
@pytest.mark.generic
910
def test_bitscrambler(dut: Dut) -> None:
1011
dut.run_all_single_board_cases()

0 commit comments

Comments
 (0)