From f7dc335456df05bb574bcb3011c4f2e469284f01 Mon Sep 17 00:00:00 2001 From: lxowalle Date: Mon, 1 Dec 2025 12:12:28 +0800 Subject: [PATCH] * fix spi dma bug --- .../src/tof100_lib/tof_adapter.hpp | 2 + .../peripheral/port/linux_common/maix_spi.cpp | 51 ++++++++++++++++++- examples/ext_dev_tof_tof100/main/src/main.cpp | 48 +++++++++++------ examples/peripheral_spi/main/src/main.cpp | 18 +++++++ 4 files changed, 101 insertions(+), 18 deletions(-) diff --git a/components/ext_devs/ext_dev_tof100/src/tof100_lib/tof_adapter.hpp b/components/ext_devs/ext_dev_tof100/src/tof100_lib/tof_adapter.hpp index e5a1f7cd..4c0af571 100644 --- a/components/ext_devs/ext_dev_tof100/src/tof100_lib/tof_adapter.hpp +++ b/components/ext_devs/ext_dev_tof100/src/tof100_lib/tof_adapter.hpp @@ -98,6 +98,7 @@ int spi_transmit_receive(SPI* dev, const void *send_buf, void *recv_buf, uint32_ clone_with_rearrange_rx(rx_b->data, length, reinterpret_cast(recv_buf)); #if 0 + u8_len = u8_len > 50 ? 50 : u8_len; maix::log::info0("spi_transmit_receive [RW]Tx: "); for (int i = 0; i < u8_len; ++i) { printf("[%02x]", tx_buff[i]); @@ -123,6 +124,7 @@ int spi_transmit(SPI* dev, void *buffer, uint32_t size, uint8_t type) maix::Bytes tx_b(tx_buff.get(), u8_len, false, false); #if 0 + u8_len = u8_len > 50 ? 50 : u8_len; maix::log::info0("spi_transmit [W]Tx: "); for (int i = 0; i < u8_len; ++i) { printf("[%02x]", tx_buff[i]); diff --git a/components/peripheral/port/linux_common/maix_spi.cpp b/components/peripheral/port/linux_common/maix_spi.cpp index 139ed9e0..079002ad 100755 --- a/components/peripheral/port/linux_common/maix_spi.cpp +++ b/components/peripheral/port/linux_common/maix_spi.cpp @@ -57,8 +57,55 @@ namespace maix::peripheral::spi transfer.cs_change = used_soft_cs; // log::info("[__spi_transfer] cs_change = %d", transfer.cs_change); - if (::ioctl(fd, SPI_IOC_MESSAGE(1), &transfer) < 1) - return -1; + if (txbuf) { + auto data_size = len; + auto p_data = (uint8_t *)txbuf; + uint8_t tmp_data = 0; + if (data_size > 50) { + if (data_size % 4 == 0) { + for (size_t i = 0; i < data_size; i += 4) { + for (size_t j = i; j < i + 2; j++) { + tmp_data = p_data[j]; + p_data[j] = p_data[i + i + 4 - j - 1]; + p_data[i + i + 4 - j - 1] = tmp_data; + } + } + } else if (data_size % 2 == 0) { + for (size_t i = 0; i < data_size; i += 2) { + tmp_data = p_data[i]; + p_data[i] = p_data[i + 1]; + p_data[i + 1] = tmp_data; + } + } + } + } + int res = ::ioctl(fd, SPI_IOC_MESSAGE(1), &transfer); + if (res < 1) { + log::error("[__spi_transfer] run ioctl failed! res:%d", res); + return res; + } + if (rxbuf) { + auto data_size = len; + auto p_data = (uint8_t *)rxbuf; + uint8_t tmp_data = 0; + if (data_size > 50) { + if (data_size % 4 == 0) { + for (size_t i = 0; i < data_size; i += 4) { + for (size_t j = i; j < i + 2; j++) { + tmp_data = p_data[j]; + p_data[j] = p_data[i + i + 4 - j - 1]; + p_data[i + i + 4 - j - 1] = tmp_data; + } + } + } else if (data_size % 2 == 0) { + for (size_t i = 0; i < data_size; i += 2) { + tmp_data = p_data[i]; + p_data[i] = p_data[i + 1]; + p_data[i + 1] = tmp_data; + } + } + } + } return 0; } diff --git a/examples/ext_dev_tof_tof100/main/src/main.cpp b/examples/ext_dev_tof_tof100/main/src/main.cpp index 4151b574..f9f22d06 100644 --- a/examples/ext_dev_tof_tof100/main/src/main.cpp +++ b/examples/ext_dev_tof_tof100/main/src/main.cpp @@ -1,7 +1,7 @@ #include "maix_basic.hpp" #include "main.h" - +#include "maix_sys.hpp" #include "maix_display.hpp" #include "maix_tof100.hpp" #include "maix_pinmap.hpp" @@ -12,27 +12,43 @@ using namespace maix::ext_dev::tof100; using namespace maix::peripheral::pinmap; using namespace maix::ext_dev::cmap; -static const std::vector> pins = { - {"A24", "SPI4_CS"}, - {"A23", "SPI4_MISO"}, - {"A25", "SPI4_MOSI"}, - {"A22", "SPI4_SCK"}, -}; - int _main(int argc, char* argv[]) { + auto disp = Display(); + int id = 4; + if (sys::device_id() == "maixcam") { + id = 4; + const std::vector> pins = { + {"A24", "SPI4_CS"}, + {"A23", "SPI4_MISO"}, + {"A25", "SPI4_MOSI"}, + {"A22", "SPI4_SCK"}, + }; - for (auto& i : pins) { - if (set_pin_function(i.first, i.second) != maix::err::Err::ERR_NONE) { - log::info("%s --> %s failed!", i.first.c_str(), i.second.c_str()); - return -1; + for (auto& i : pins) { + if (set_pin_function(i.first, i.second) != maix::err::Err::ERR_NONE) { + log::info("%s --> %s failed!", i.first.c_str(), i.second.c_str()); + return -1; + } + } + } else { + id = 2; + const std::vector> pins = { + {"B21", "SPI2_CS1"}, + {"B19", "SPI2_MISO"}, + {"B18", "SPI2_MOSI"}, + {"B20", "SPI2_SCK"}, + }; + + for (auto& i : pins) { + if (set_pin_function(i.first, i.second) != maix::err::Err::ERR_NONE) { + log::info("%s --> %s failed!", i.first.c_str(), i.second.c_str()); + return -1; + } } } - auto disp = Display(); - - auto tof = Tof100(4, Resolution::RES_100x100, Cmap::JET, 40, 1000); - + auto tof = Tof100(id, Resolution::RES_100x100, Cmap::JET, 40, 1000); while (!app::need_exit()) { auto img = tof.image(); if (img == nullptr) diff --git a/examples/peripheral_spi/main/src/main.cpp b/examples/peripheral_spi/main/src/main.cpp index 07cccb6b..9d7b0738 100755 --- a/examples/peripheral_spi/main/src/main.cpp +++ b/examples/peripheral_spi/main/src/main.cpp @@ -1,6 +1,7 @@ #include "maix_basic.hpp" #include "main.h" #include "maix_spi.hpp" +#include "maix_sys.hpp" #include "maix_pinmap.hpp" #include @@ -50,6 +51,7 @@ int _main(int argc, char* argv[]) log::info("Init spi4 end"); #endif #ifdef USE_SPI2 + if (sys::device_id() == "maixcam") { const std::map pin_functions{ {"P18", "SPI2_CS"}, {"P21", "SPI2_MISO"}, @@ -65,7 +67,23 @@ int _main(int argc, char* argv[]) return -1; } } + } else { + const std::map pin_functions{ + {"B21", "SPI2_CS1"}, + {"B19", "SPI2_MISO"}, + {"B18", "SPI2_MOSI"}, + {"B20", "SPI2_SCK"}, + }; + log::info("Use pinmap to configure SPI2."); + for (const auto& item : pin_functions) { + if (err::ERR_NONE != pinmap::set_pin_function(item.first, item.second)) { + log::error("Set pin{%s} to function{%s} failed!", + item.first.c_str(), item.second.c_str()); + return -1; + } + } + } log::info("Init spi2"); spi::SPI dev(2, spi::Mode::MASTER, 400000); log::info("Init spi2 end");