* fix spi dma bug

This commit is contained in:
lxowalle
2025-12-01 12:12:28 +08:00
parent 36acb82c6e
commit f7dc335456
4 changed files with 101 additions and 18 deletions

View File

@@ -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<uint32_t*>(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]);

View File

@@ -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;
}

View File

@@ -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<std::pair<std::string, std::string>> 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<std::pair<std::string, std::string>> 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<std::pair<std::string, std::string>> 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)

View File

@@ -1,6 +1,7 @@
#include "maix_basic.hpp"
#include "main.h"
#include "maix_spi.hpp"
#include "maix_sys.hpp"
#include "maix_pinmap.hpp"
#include <map>
@@ -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<std::string, std::string> pin_functions{
{"P18", "SPI2_CS"},
{"P21", "SPI2_MISO"},
@@ -65,7 +67,23 @@ int _main(int argc, char* argv[])
return -1;
}
}
} else {
const std::map<std::string, std::string> 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");