From 5f0e16e97a773480a3b09c5312c98742e7049f1d Mon Sep 17 00:00:00 2001 From: jacqueline Date: Fri, 22 Dec 2023 16:48:24 +1100 Subject: [PATCH] Support changing the usb msc state --- src/app_console/app_console.cpp | 10 ++++++++-- src/drivers/include/samd.hpp | 3 +++ src/drivers/samd.cpp | 26 ++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/app_console/app_console.cpp b/src/app_console/app_console.cpp index f99f7536..2d16b60b 100644 --- a/src/app_console/app_console.cpp +++ b/src/app_console/app_console.cpp @@ -39,6 +39,7 @@ #include "index.hpp" #include "lua_thread.hpp" #include "memory_resource.hpp" +#include "samd.hpp" #include "service_locator.hpp" #include "system_events.hpp" #include "track.hpp" @@ -414,14 +415,15 @@ int CmdSamd(int argc, char** argv) { std::cout << usage << std::endl; return 1; } + drivers::Samd& samd = AppConsole::sServices->samd(); std::pmr::string cmd{argv[1]}; if (cmd == "flash") { std::cout << "resetting samd..." << std::endl; vTaskDelay(pdMS_TO_TICKS(5)); - AppConsole::sServices->samd().ResetToFlashSamd(); + samd.ResetToFlashSamd(); } else if (cmd == "charge") { - auto res = AppConsole::sServices->samd().GetChargeStatus(); + auto res = samd.GetChargeStatus(); if (res) { switch (res.value()) { case drivers::Samd::ChargeStatus::kNoBattery: @@ -446,6 +448,10 @@ int CmdSamd(int argc, char** argv) { } else { std::cout << "unknown" << std::endl; } + } else if (cmd == "msc") { + bool current = samd.UsbMassStorage(); + std::cout << "toggling to: " << !current << std::endl; + samd.UsbMassStorage(!current); } else if (cmd == "off") { std::cout << "bye !!!" << std::endl; vTaskDelay(pdMS_TO_TICKS(5)); diff --git a/src/drivers/include/samd.hpp b/src/drivers/include/samd.hpp index d9f1ca48..f25f9575 100644 --- a/src/drivers/include/samd.hpp +++ b/src/drivers/include/samd.hpp @@ -54,6 +54,9 @@ class Samd { auto ResetToFlashSamd() -> void; auto PowerDown() -> void; + auto UsbMassStorage(bool en) -> void; + auto UsbMassStorage() -> bool; + // Not copyable or movable. There should usually only ever be once instance // of this class, and that instance will likely have a static lifetime. Samd(const Samd&) = delete; diff --git a/src/drivers/samd.cpp b/src/drivers/samd.cpp index f361513e..cf3b9c18 100644 --- a/src/drivers/samd.cpp +++ b/src/drivers/samd.cpp @@ -143,4 +143,30 @@ auto Samd::PowerDown() -> void { ESP_ERROR_CHECK(transaction.Execute(3)); } +auto Samd::UsbMassStorage(bool en) -> void { + I2CTransaction transaction; + transaction.start() + .write_addr(kAddress, I2C_MASTER_WRITE) + .write_ack(Registers::kUsbControl, en) + .stop(); + ESP_ERROR_CHECK(transaction.Execute(3)); +} + +auto Samd::UsbMassStorage() -> bool { + uint8_t raw_res; + I2CTransaction transaction; + transaction.start() + .write_addr(kAddress, I2C_MASTER_WRITE) + .write_ack(Registers::kUsbControl) + .start() + .write_addr(kAddress, I2C_MASTER_READ) + .read(&raw_res, I2C_MASTER_NACK) + .stop(); + esp_err_t res = transaction.Execute(1); + if (res != ESP_OK) { + return false; + } + return raw_res & 1; +} + } // namespace drivers