mirror of
https://github.com/RT-Thread/rt-thread.git
synced 2026-11-16 01:05:15 +08:00
[bsp][esp32-c6] address review: chunk SPI xfers to 64B limit, BSP-owned gdma.c, early width check
This commit is contained in:
1 parent
1f747c967c
commit
338eb83718
4 files changed
+934
-154
No files matched your search
@@ -162,107 +162,6 @@ def _match_idf_spi_lock_stub_to_spi_config():
|
||||
|
||||
_match_idf_spi_lock_stub_to_spi_config()
|
||||
|
||||
def _adapt_idf_gdma_to_c6_ll():
|
||||
"""idf_port/SConscript compiles the package's components/esp_hw_support/
|
||||
gdma.c when BSP_USING_SPI is on, because spi_common.c's
|
||||
SOC_GDMA_SUPPORTED section calls gdma_new_channel() and friends. That gdma.c
|
||||
is written against the C3 generation of hal/gdma_ll.h and this package's
|
||||
components/hal/esp32c6/include/hal/gdma_ll.h is a different one:
|
||||
|
||||
- gdma_ll_enable_m2m_mode() (gdma.c:259) does not exist on C6. C6 folds
|
||||
the same thing into the two connect helpers: gdma_ll_rx_connect_to_periph
|
||||
(gdma_ll.h:297) writes in_conf0.mem_trans_en = (periph ==
|
||||
GDMA_TRIG_PERIPH_M2M) itself, and gdma_ll_rx_disconnect_from_periph
|
||||
(gdma_ll.h:306) clears it. So the call goes away rather than being
|
||||
renamed to something C6 does not do.
|
||||
- gdma_ll_tx/rx_connect_to_periph take four arguments on C6 (gdma_ll.h:520
|
||||
and :297) -- dev, channel, periph, periph_id -- while gdma.c:264/:269
|
||||
pass three. gdma_trigger_t (esp_hw_support/include/esp_private/
|
||||
gdma.h:130) keeps .periph and .instance_id apart, and .instance_id is
|
||||
what the C3 header wrote into the same select field, so the fix is to
|
||||
hand C6 the periph it also asks for and keep that value as periph_id.
|
||||
- gdma.c:289/:291 disconnect by feeding GDMA_INVALID_PERIPH_TRIG back into
|
||||
connect. C6 has no such sentinel path: it has
|
||||
gdma_ll_tx_disconnect_from_periph (gdma_ll.h:529) and
|
||||
gdma_ll_rx_disconnect_from_periph, which write GDMA_LL_INVALID_PERIPH_ID
|
||||
and clear mem_trans_en.
|
||||
- the RX connect is left inside its `periph != GDMA_TRIG_PERIPH_M2M`
|
||||
guard, which on C6 also skips the only writer of mem_trans_en; it is
|
||||
unguarded here and the TX guard is left alone. Rewritten below, after
|
||||
these four.
|
||||
|
||||
The rewrite is here, in a tracked file, and it is a whole call expression
|
||||
swapped for a whole call expression, so a second run finds nothing left to
|
||||
match and cannot stack a second copy. packages/ is not in the repository.
|
||||
"""
|
||||
import re
|
||||
from env_package import find_package_path
|
||||
|
||||
gdma = os.path.join(os.path.abspath(
|
||||
find_package_path(SDK_ROOT, 'ESP-IDF-latest')),
|
||||
'components', 'esp_hw_support', 'gdma.c')
|
||||
if not os.path.isfile(gdma):
|
||||
return
|
||||
with open(gdma, 'rb') as f:
|
||||
text = f.read()
|
||||
|
||||
fixed = text
|
||||
# C6 has no m2m setter: its rx connect sets mem_trans_en, its rx disconnect
|
||||
# clears it. Drop the call, keep the comment above it, which still says what
|
||||
# the connect below now does.
|
||||
fixed = re.sub(rb'^[ \t]*gdma_ll_enable_m2m_mode\([^\r\n]*\r?\n', b'',
|
||||
fixed, flags=re.M)
|
||||
for old, new in [
|
||||
(b'gdma_ll_tx_connect_to_periph(group->hal.dev, pair->pair_id, '
|
||||
b'trig_periph.instance_id)',
|
||||
b'gdma_ll_tx_connect_to_periph(group->hal.dev, pair->pair_id, '
|
||||
b'trig_periph.periph, trig_periph.instance_id)'),
|
||||
(b'gdma_ll_rx_connect_to_periph(group->hal.dev, pair->pair_id, '
|
||||
b'trig_periph.instance_id)',
|
||||
b'gdma_ll_rx_connect_to_periph(group->hal.dev, pair->pair_id, '
|
||||
b'trig_periph.periph, trig_periph.instance_id)'),
|
||||
(b'gdma_ll_tx_connect_to_periph(group->hal.dev, pair->pair_id, '
|
||||
b'GDMA_INVALID_PERIPH_TRIG)',
|
||||
b'gdma_ll_tx_disconnect_from_periph(group->hal.dev, pair->pair_id)'),
|
||||
(b'gdma_ll_rx_connect_to_periph(group->hal.dev, pair->pair_id, '
|
||||
b'GDMA_INVALID_PERIPH_TRIG)',
|
||||
b'gdma_ll_rx_disconnect_from_periph(group->hal.dev, pair->pair_id)'),
|
||||
]:
|
||||
fixed = fixed.replace(old, new)
|
||||
|
||||
# The RX connect stays inside the same `periph != M2M` guard gdma.c uses for
|
||||
# TX, and on C6 that guard is exactly what decides mem_trans_en: the only
|
||||
# writer of that bit is gdma_ll_rx_connect_to_periph (gdma_ll.h:297-301), and
|
||||
# it writes mem_trans_en = (periph == GDMA_TRIG_PERIPH_M2M) while doing the
|
||||
# selection. Guarded, a memory-to-memory channel never reaches the call that
|
||||
# would set its own mode bit, so mem_trans_en stays 0 forever. C3's
|
||||
# gdma_ll_enable_m2m_mode -- the call removed above -- wrote that one RX bit
|
||||
# and nothing else, so the guard replaces a call that used to be made
|
||||
# unconditionally. TX keeps its guard: gdma_ll_tx_connect_to_periph
|
||||
# (gdma_ll.h:520-523) throws `periph` away and writes only peri_out_sel, so
|
||||
# for a TX channel skipping the call loses nothing.
|
||||
rx_guarded = (
|
||||
b' if (trig_periph.periph != GDMA_TRIG_PERIPH_M2M) {\n'
|
||||
b' gdma_ll_rx_connect_to_periph(group->hal.dev, pair->pair_id,'
|
||||
b' trig_periph.periph, trig_periph.instance_id);\n'
|
||||
b' }\n')
|
||||
rx_unguarded = (
|
||||
b' gdma_ll_rx_connect_to_periph(group->hal.dev, pair->pair_id,'
|
||||
b' trig_periph.periph, trig_periph.instance_id);\n')
|
||||
for old, new in ((rx_guarded, rx_unguarded),
|
||||
(rx_guarded.replace(b'\n', b'\r\n'),
|
||||
rx_unguarded.replace(b'\n', b'\r\n'))):
|
||||
if old in fixed:
|
||||
fixed = fixed.replace(old, new, 1)
|
||||
break
|
||||
|
||||
if fixed != text:
|
||||
with open(gdma, 'wb') as f:
|
||||
f.write(fixed)
|
||||
|
||||
_adapt_idf_gdma_to_c6_ll()
|
||||
|
||||
|
||||
objs = PrepareBuilding(env, RTT_ROOT, remove_components = ['libc'])
|
||||
|
||||
objs.extend(SConscript(os.path.join(libraries_path_prefix, 'drivers', 'SConscript'), variant_dir='build/libraries/drivers', duplicate=0))
|
||||
|
||||
@@ -48,11 +48,11 @@ if GetDepend(['BSP_USING_SPI']):
|
||||
# compiled into the object no matter what the third argument of
|
||||
# spi_bus_initialize() is. The package drops every path containing
|
||||
# 'gdma' from its C6 source list, so the driver and its hal wrapper are
|
||||
# compiled here instead. That gdma.c is written against the C3
|
||||
# generation of hal/gdma_ll.h; SConstruct rewrites its gdma_ll_* call
|
||||
# sites to the prototypes in this package's C6 header before anything is
|
||||
# compiled.
|
||||
'../%s/components/esp_hw_support/gdma.c' % idf,
|
||||
# compiled here instead. The package's gdma.c is written against the C3
|
||||
# generation of hal/gdma_ll.h and that tree is shared with the C3 BSP,
|
||||
# so this BSP carries its own C6-correct copy (src/gdma.c) instead of
|
||||
# rewriting the package file at build time.
|
||||
'src/gdma.c',
|
||||
'../%s/components/hal/gdma_hal.c' % idf,
|
||||
# gdma.c reads gdma_periph_signals (gdma.c:541 and on) to map a
|
||||
# GDMA_TRIG_PERIPH_* onto the signal slots of a channel; the one object
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -84,6 +84,20 @@ static rt_uint32_t spi_bits_per_word = 8;
|
||||
#define RT_ESP_SPI_MAX_RX_BYTES 4092
|
||||
static rt_uint32_t spi_rx_units[(RT_ESP_SPI_MAX_RX_BYTES + 3) / 4] __attribute__((aligned(4)));
|
||||
|
||||
/*
|
||||
* Largest number of bytes one spi_transaction_t may carry. With DMA enabled
|
||||
* this is buscfg.max_transfer_sz; with SPI_DMA_DISABLED (C6) spi_common.c sets
|
||||
* bus_attr->max_transfer_sz to SOC_SPI_MAXIMUM_BUFFER_SIZE (64 on C6) and
|
||||
* spi_master.c rejects a longer transaction with ESP_ERR_INVALID_ARG, so
|
||||
* spixfer() splits longer messages into chunks and keeps CS asserted between
|
||||
* them.
|
||||
*/
|
||||
#ifdef SOC_ESP32_C6
|
||||
#define RT_ESP_SPI_MAX_CHUNK_BYTES SOC_SPI_MAXIMUM_BUFFER_SIZE
|
||||
#else
|
||||
#define RT_ESP_SPI_MAX_CHUNK_BYTES RT_ESP_SPI_MAX_RX_BYTES
|
||||
#endif
|
||||
|
||||
static struct esp32_spi spi_bus_obj[] = {
|
||||
#ifdef BSP_USING_SPI2
|
||||
{
|
||||
@@ -112,6 +126,19 @@ static rt_err_t spi_configure(struct rt_spi_device* device,
|
||||
return -RT_EINVAL;
|
||||
}
|
||||
|
||||
/* Validate the word width before touching the hardware. An unsupported
|
||||
* width must not leave an initialized SPI bus behind: a caller that asks
|
||||
* for 16 bits and then retries with 8 would otherwise hit
|
||||
* spi_bus_initialize()'s "bus already initialized" abort. */
|
||||
if (configuration->data_width != 8)
|
||||
{
|
||||
LOG_E("data width %u is not supported\n", (rt_uint32_t)configuration->data_width);
|
||||
return -RT_ENOSYS;
|
||||
}
|
||||
/* One RT-Thread byte is 8 bits of transaction: this is what length and
|
||||
* rxlength are counted in. */
|
||||
spi_bits_per_word = 8;
|
||||
|
||||
static spi_bus_config_t buscfg =
|
||||
{
|
||||
.miso_io_num=SPI2_IOMUX_PIN_NUM_MISO, /*MISO*/
|
||||
@@ -126,13 +153,13 @@ static rt_err_t spi_configure(struct rt_spi_device* device,
|
||||
|
||||
#ifdef SOC_ESP32_C6
|
||||
/* SPI_DMA_DISABLED: this transfer does not go through GDMA, even though GDMA
|
||||
is in the image -- BSP_USING_SPI makes idf_port/SConscript compile the
|
||||
package's components/esp_hw_support/gdma.c, because spi_common.c's
|
||||
SOC_GDMA_SUPPORTED section calls it. The received bytes are instead read
|
||||
out by spi_ll_read_buffer() (hal/esp32c6/include/hal/spi_ll.h:425), which
|
||||
memcpys the low byte of each 32-bit data_buf word to the start of the
|
||||
buffer -- its length is (len + 7) / 8, which is 1 for an 8-bit word -- and
|
||||
that is what fills the staging array above. */
|
||||
is in the image -- BSP_USING_SPI makes idf_port/SConscript compile this
|
||||
BSP's own src/gdma.c, because spi_common.c's SOC_GDMA_SUPPORTED section
|
||||
calls it. The received bytes are instead read out by spi_ll_read_buffer()
|
||||
(hal/esp32c6/include/hal/spi_ll.h:425), which memcpys the low byte of each
|
||||
32-bit data_buf word to the start of the buffer -- its length is
|
||||
(len + 7) / 8, which is 1 for an 8-bit word -- and that is what fills the
|
||||
staging array above. */
|
||||
err = spi_bus_initialize(SPI2_HOST, &buscfg, SPI_DMA_DISABLED);
|
||||
#else
|
||||
err = spi_bus_initialize(SPI2_HOST, &buscfg, SPI_DMA_CH_AUTO);
|
||||
@@ -140,17 +167,6 @@ static rt_err_t spi_configure(struct rt_spi_device* device,
|
||||
ESP_ERROR_CHECK(err);
|
||||
|
||||
static spi_device_interface_config_t devcfg;
|
||||
if(configuration->data_width == 8)
|
||||
{
|
||||
/* One RT-Thread byte is 8 bits of transaction: this branch only sets
|
||||
* spi_bits_per_word, which is what length and rxlength are counted in. */
|
||||
spi_bits_per_word = 8;
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_E("data width %u is not supported\n", (rt_uint32_t)configuration->data_width);
|
||||
return -RT_ENOSYS;
|
||||
}
|
||||
|
||||
LOG_W("configuration->max_hz = %d \n",configuration->max_hz);
|
||||
if(configuration->max_hz >= SPI_MASTER_FREQ_80M)
|
||||
@@ -241,43 +257,76 @@ static rt_ssize_t spixfer(struct rt_spi_device* device, struct rt_spi_message* m
|
||||
RT_ASSERT(device != NULL);
|
||||
RT_ASSERT(message != NULL);
|
||||
|
||||
/* Zeroed per transfer: this transaction is reused, and cmd/addr/flags/user
|
||||
left over from the previous one would be sent out again. */
|
||||
static spi_transaction_t trans;
|
||||
rt_memset(&trans, 0, sizeof(trans));
|
||||
|
||||
trans.length = (message->length) * spi_bits_per_word;
|
||||
trans.tx_buffer = message->send_buf;
|
||||
|
||||
if (message->recv_buf != RT_NULL)
|
||||
if (message->recv_buf != RT_NULL && message->length > sizeof(spi_rx_units))
|
||||
{
|
||||
if (message->length > sizeof(spi_rx_units))
|
||||
{
|
||||
LOG_E("rx length %u is larger than %u\n",
|
||||
(rt_uint32_t)message->length, (rt_uint32_t)RT_ESP_SPI_MAX_RX_BYTES);
|
||||
return -RT_EINVAL;
|
||||
}
|
||||
/* The driver writes the received units into this 4-byte aligned array,
|
||||
not into the caller's buffer. */
|
||||
trans.rx_buffer = spi_rx_units;
|
||||
trans.rxlength = trans.length;
|
||||
LOG_E("rx length %u is larger than %u\n",
|
||||
(rt_uint32_t)message->length, (rt_uint32_t)RT_ESP_SPI_MAX_RX_BYTES);
|
||||
return -RT_EINVAL;
|
||||
}
|
||||
|
||||
rt_size_t total = message->length;
|
||||
rt_size_t done = 0;
|
||||
static spi_transaction_t trans;
|
||||
|
||||
spi_device_acquire_bus(spi, portMAX_DELAY);
|
||||
esp_err_t err = spi_device_polling_transmit(spi, &trans);
|
||||
|
||||
while (done < total)
|
||||
{
|
||||
rt_size_t chunk = total - done;
|
||||
if (chunk > RT_ESP_SPI_MAX_CHUNK_BYTES)
|
||||
{
|
||||
chunk = RT_ESP_SPI_MAX_CHUNK_BYTES;
|
||||
}
|
||||
|
||||
/* Zeroed per chunk: this transaction is reused, and cmd/addr/flags/user
|
||||
left over from the previous one would be sent out again. */
|
||||
rt_memset(&trans, 0, sizeof(trans));
|
||||
|
||||
trans.length = chunk * spi_bits_per_word;
|
||||
if (message->send_buf != RT_NULL)
|
||||
{
|
||||
trans.tx_buffer = (const rt_uint8_t *)message->send_buf + done;
|
||||
}
|
||||
|
||||
if (message->recv_buf != RT_NULL)
|
||||
{
|
||||
/* The driver writes the received units into this 4-byte aligned
|
||||
array, not into the caller's buffer. */
|
||||
trans.rx_buffer = spi_rx_units;
|
||||
trans.rxlength = trans.length;
|
||||
}
|
||||
|
||||
/* Hold CS across chunk boundaries; the last chunk lets the driver
|
||||
release it. The bus is acquired above, which is what allows the
|
||||
KEEP_ACTIVE flag in a polling transaction. */
|
||||
if (done + chunk < total)
|
||||
{
|
||||
trans.flags |= SPI_TRANS_CS_KEEP_ACTIVE;
|
||||
}
|
||||
|
||||
esp_err_t err = spi_device_polling_transmit(spi, &trans);
|
||||
if (err != ESP_OK)
|
||||
{
|
||||
LOG_E("spi transfer failed at offset %u: %d\n",
|
||||
(rt_uint32_t)done, err);
|
||||
spi_device_release_bus(spi);
|
||||
return -RT_EIO;
|
||||
}
|
||||
|
||||
if (message->recv_buf != RT_NULL)
|
||||
{
|
||||
/* Verbatim: the aligned array holds the received bytes in the order
|
||||
they came off the bus, and only the count the caller asked for is
|
||||
copied. */
|
||||
rt_memcpy((rt_uint8_t *)message->recv_buf + done, spi_rx_units, chunk);
|
||||
}
|
||||
|
||||
done += chunk;
|
||||
}
|
||||
|
||||
spi_device_release_bus(spi);
|
||||
|
||||
ESP_ERROR_CHECK(err);
|
||||
|
||||
if (message->recv_buf != RT_NULL)
|
||||
{
|
||||
/* Verbatim: the aligned array holds the received bytes in the order they
|
||||
came off the bus, and only the count the caller asked for is copied. */
|
||||
rt_memcpy(message->recv_buf, spi_rx_units, message->length);
|
||||
}
|
||||
|
||||
return message->length;
|
||||
return total;
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user