Support changing the usb msc state

custom
jacqueline 1 year ago
parent d4a0085753
commit 5f0e16e97a
  1. 10
      src/app_console/app_console.cpp
  2. 3
      src/drivers/include/samd.hpp
  3. 26
      src/drivers/samd.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));

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

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

Loading…
Cancel
Save