Skip to content

Commit c376517

Browse files
committed
Add trace option to spi
1 parent 9e2915f commit c376517

5 files changed

Lines changed: 25 additions & 7 deletions

File tree

app/main.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ new_flash_command(const std::string& name, const std::string& desc) {
3939
auto cmd = std::make_unique<argparse::ArgumentParser>(name);
4040
cmd->add_description(desc);
4141
cmd->add_argument("--interface", "-i").help("One of [spi, i2c, gpio]");
42+
cmd->add_argument("--traces", "-t").help("Enable ftdi traces").default_value(false).implicit_value(true);
4243
cmd->add_argument("--ftdi")
4344
.default_value(std::string("FT4222"))
4445
.help("Filter ftdi chips connected to USB");
@@ -48,13 +49,15 @@ new_flash_command(const std::string& name, const std::string& desc) {
4849
static std::optional<ftdi::SpiHost>
4950
handle_flash_command(std::unique_ptr<argparse::ArgumentParser>& cmd) {
5051
auto ftdi = cmd->get<std::string>("--ftdi");
52+
auto traces = cmd->get<bool>("--traces");
5153
auto devices = scan();
5254
auto filtered = ftdi::DeviceInfo::filter_by_description(devices, ftdi);
5355
if (filtered.empty()) {
5456
std::print("No supported ftdi found.\n");
5557
exit(0);
5658
}
5759
if (auto opt = ftdi::SpiHost::from_device_info(filtered[0])) {
60+
opt->with_traces(traces);
5861
return opt;
5962
}
6063
std::println("Can't open spi.");

lib/flash/flash.hh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ class Generic {
8383
}
8484

8585
Option<Sfdp> sfdp() {
86-
std::array<uint8_t, 5 + sizeof(Sfdp)> cmd = {Opcode::ReadSfdp, 0x00, 0x00, 0x00, 0x00};
86+
std::array<uint8_t, 5 + sizeof(Sfdp)> cmd = {Opcode::ReadSfdp, 0x00, 0x00, 0x00,
87+
0xDB}; // DB=DummyByte
8788

8889
std::span<uint8_t> ret = TRY_OPT(spih.transfer(cmd));
8990
return Sfdp::try_from(ret.subspan(5));

lib/ftdi/log.hh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ struct std::formatter<std::array<uint8_t, N>> : std::formatter<std::string_view>
1414

1515
auto format(const std::array<uint8_t, N>& arr, format_context& ctx) const {
1616
auto out = ctx.out();
17-
out = std::format_to(out, "Array<uint8_t, {} >: {{", N);
17+
out = std::format_to(out, "Array<{}>: {{", N);
1818

1919
for (std::size_t i = 0; i < N; ++i) {
2020
out = std::format_to(out, "{:02x}", arr[i]);
@@ -32,7 +32,7 @@ struct std::formatter<std::span<uint8_t, N>> : std::formatter<std::string_view>
3232

3333
auto format(const std::span<uint8_t, N>& arr, format_context& ctx) const {
3434
auto out = ctx.out();
35-
out = std::format_to(out, "Span<uint8_t, {} >: {{", N);
35+
out = std::format_to(out, "Span<{}>: {{", N);
3636

3737
for (std::size_t i = 0; i < N; ++i) {
3838
out = std::format_to(out, "{:02x}", arr[i]);
@@ -50,7 +50,7 @@ struct std::formatter<std::span<uint8_t>> : std::formatter<std::string_view> {
5050

5151
auto format(const std::span<uint8_t>& arr, format_context& ctx) const {
5252
auto out = ctx.out();
53-
out = std::format_to(out, "Span<uint8_t, {}> = {{", arr.size());
53+
out = std::format_to(out, "Span<{}> = {{", arr.size());
5454

5555
for (std::size_t i = 0; i < arr.size(); ++i) {
5656
out = std::format_to(out, "{:02x}", arr[i]);
@@ -68,7 +68,7 @@ struct std::formatter<std::vector<uint8_t>> : std::formatter<std::string_view> {
6868

6969
auto format(const std::vector<uint8_t>& arr, format_context& ctx) const {
7070
auto out = ctx.out();
71-
out = std::format_to(out, "Vector<uint8_t, {}> = {{", arr.size());
71+
out = std::format_to(out, "Vector<{}> = {{", arr.size());
7272

7373
for (std::size_t i = 0; i < arr.size(); ++i) {
7474
out = std::format_to(out, "{:02x}", arr[i]);

lib/ftdi/spi_host.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
namespace ftdi {
1818
embeddedpp::Result<std::span<uint8_t>> SpiHost::transfer(std::span<uint8_t> payload) {
1919
uint16_t received = 0;
20+
log("SPI -->> {}", payload);
2021

2122
FT4222_SPIMaster_SetLines(handle, SPI_IO_SINGLE);
2223

@@ -32,7 +33,7 @@ embeddedpp::Result<std::span<uint8_t>> SpiHost::transfer(std::span<uint8_t> payl
3233
std::cerr << std::format("Wrote only {}/{}\n", received, payload.size());
3334
return embeddedpp::Code::Generic;
3435
}
35-
36+
log("SPI <<-- {}", payload);
3637
return payload;
3738
}
3839

@@ -96,13 +97,15 @@ embeddedpp::Status SpiHost::transaction(embeddedpp::Transfers transfers) {
9697
return embeddedpp::Code::Generic;
9798
}
9899

100+
log("SPI -->> {}", payload);
99101
status =
100102
FT4222_SPIMaster_MultiReadWrite(handle, rd_buffer.data(), payload.data(), single_bytes_wr,
101103
multi_bytes_wr, multi_bytes_rd, &received);
102104
if (FT4222_OK != status) {
103105
std::cerr << std::format("MultiReadWrite:{}\n", status);
104106
return embeddedpp::Code::Generic;
105107
}
108+
log("SPI <<-- {}", rd_buffer);
106109

107110
return embeddedpp::Code::Ok;
108111
};

lib/ftdi/spi_host.hh

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ using Result = std::optional<std::span<uint8_t>>;
2121

2222
class SpiHost {
2323
FT_HANDLE handle;
24+
bool traces = false;
2425

2526
public:
26-
explicit SpiHost(FT_HANDLE handle) noexcept : handle(handle) {}
27+
explicit SpiHost(FT_HANDLE handle) noexcept : handle(handle), traces(false) {}
2728
~SpiHost() { FT_Close(handle); }
2829

2930
embeddedpp::Result<std::span<uint8_t>> transfer(std::span<uint8_t> payload);
@@ -34,6 +35,16 @@ class SpiHost {
3435

3536
Result read(uint32_t size, bool deassert_cs = true);
3637

38+
void with_traces(bool enable) { traces = enable; };
39+
3740
static std::optional<SpiHost> from_device_info(DeviceInfo& device);
41+
42+
private:
43+
template <typename... Args>
44+
void log(std::format_string<Args...> fmt, Args&&... args) {
45+
if (this->traces) {
46+
std::println(fmt, std::forward<Args>(args)...);
47+
}
48+
}
3849
};
3950
} // namespace ftdi

0 commit comments

Comments
 (0)