Skip to content

Commit

Permalink
feat: WIP USB and hardware config
Browse files Browse the repository at this point in the history
* Added usb support for USB MSC wired up to uSD card for mouting to computer.
* Updated main settings screen to have a USB MSC button for enabling and disabling USB MSC. Deault is disabled on boot so that programming / logging can be done.
* Refactor box-emu-hal some to have kconfig for selecting hardware version and configuring the input driver based on that.
* Added support for Aw9523 input driver (V1 hardware which changes compared to V0)
* Update espp submodule
* Update file system to expose sdcard for use by usb subsystem.
related / WIP #45 #46
  • Loading branch information
finger563 committed Dec 23, 2023
1 parent 2af6a66 commit 974a613
Show file tree
Hide file tree
Showing 21 changed files with 1,584 additions and 61 deletions.
2 changes: 1 addition & 1 deletion components/box-emu-hal/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
idf_component_register(
INCLUDE_DIRS "include"
SRC_DIRS "src"
REQUIRES "driver" "heap" "fatfs" "esp_lcd" "esp_psram" "spi_flash" "nvs_flash" "codec" "display" "display_drivers" "mcp23x17" "input_drivers" "tt21100" "gt911" "drv2605" "event_manager" "i2c" "task" "timer"
REQUIRES "driver" "heap" "fatfs" "esp_lcd" "esp_psram" "esp_tinyusb" "spi_flash" "nvs_flash" "codec" "aw9523" "display" "display_drivers" "mcp23x17" "input_drivers" "tt21100" "gt911" "drv2605" "event_manager" "i2c" "task" "timer"
)
31 changes: 24 additions & 7 deletions components/box-emu-hal/Kconfig.projbuild
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
menu "BOX Emulator HAL Configuration"
menu "BOX Emulator Configuration"

choice
prompt "Hardware Configuration"
prompt "Hardware Revision"
default HARDWARE_V1
help
Select the version of the hardware you're using.
config HARDWARE_V0
bool "Hardware V0"
help
This is the hardware version 0 of the BOX. It uses custom 3d printed button plastics and tactile switches.
It only supports the ESP32-S3-BOX.
config HARDWARE_V1
bool "Hardware V1"
help
This is the hardware version 1 of the BOX. It uses gameboy color button plastics and membranes
and supports both the ESP32-S3-BOX and the ESP32-S3-BOX-3. It also adds support for physical volume
buttons, battery measurement, and a battery charging LED.
endchoice

choice
prompt "Module Configuration"
default HARDWARE_BOX
help
Select the dev-kit / hardware you're using.
Select which display + SoC module you're using.
config HARDWARE_BOX
bool "ESP BOX"
bool "ESP32-S3-BOX"
config HARDWARE_BOX_3
bool "ESP BOX 3"
config HARDWARE_TDECK
bool "LILYGO T DECK"
depends on HARDWARE_V1
bool "ESP32-S3-BOX-3"
endchoice

endmenu
4 changes: 4 additions & 0 deletions components/box-emu-hal/idf_component.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## IDF Component Manager Manifest File
dependencies:
espressif/esp_tinyusb: "^1.4.2"
idf: "^5.1"
17 changes: 17 additions & 0 deletions components/box-emu-hal/include/box_emu_hal.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#if CONFIG_HARDWARE_BOX
#include "box.hpp"
#elif CONFIG_HARDWARE_BOX_3
#include "box_3.hpp"
#else
#error "Invalid module selection"
#endif

#if CONFIG_HARDWARE_V0
#include "emu_v0.hpp"
#elif CONFIG_HARDWARE_V1
#include "emu_v1.hpp"
#else
#error "Invalid hardware version"
#endif
22 changes: 22 additions & 0 deletions components/box-emu-hal/include/emu_v0.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#if CONFIG_HARDWARE_V0
#include "mcp23x17.hpp"
using InputDriver = espp::Mcp23x17;
static constexpr uint16_t START_PIN = (1<<0) << 0; // start pin is on port a of the MCP23x17
static constexpr uint16_t SELECT_PIN = (1<<1) << 0; // select pin is on port a of the MCP23x17
static constexpr uint16_t UP_PIN = (1<<0) << 8; // up pin is on port b of the MCP23x17
static constexpr uint16_t DOWN_PIN = (1<<1) << 8; // down pin is on port b of the MCP23x17
static constexpr uint16_t LEFT_PIN = (1<<2) << 8; // left pin is on port b of the MCP23x17
static constexpr uint16_t RIGHT_PIN = (1<<3) << 8; // right pin is on port b of the MCP23x17
static constexpr uint16_t A_PIN = (1<<4) << 8; // a pin is on port b of the MCP23x17
static constexpr uint16_t B_PIN = (1<<5) << 8; // b pin is on port b of the MCP23x17
static constexpr uint16_t X_PIN = (1<<6) << 8; // x pin is on port b of the MCP23x17
static constexpr uint16_t Y_PIN = (1<<7) << 8; // y pin is on port b of the MCP23x17
static constexpr uint16_t DIRECTION_MASK = (UP_PIN | DOWN_PIN | LEFT_PIN | RIGHT_PIN | A_PIN | B_PIN | X_PIN | Y_PIN | START_PIN | SELECT_PIN);
static constexpr uint16_t INTERRUPT_MASK = (START_PIN | SELECT_PIN);
static constexpr uint8_t PORT_0_DIRECTION_MASK = DIRECTION_MASK & 0xFF;
static constexpr uint8_t PORT_1_DIRECTION_MASK = (DIRECTION_MASK >> 8) & 0xFF;
static constexpr uint8_t PORT_0_INTERRUPT_MASK = INTERRUPT_MASK & 0xFF;
static constexpr uint8_t PORT_1_INTERRUPT_MASK = (INTERRUPT_MASK >> 8) & 0xFF;
#endif
27 changes: 27 additions & 0 deletions components/box-emu-hal/include/emu_v1.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#if CONFIG_HARDWARE_V1
#include "aw9523.hpp"
using InputDriver = espp::Aw9523;
static constexpr gpio_num_t VBAT_SENSE_PIN = GPIO_NUM_14; // battery sense pin is on GPIO 14
static constexpr gpio_num_t AW9523_INT_PIN = GPIO_NUM_21; // interrupt pin is on GPIO 21
static constexpr uint16_t UP_PIN = (1<<0) << 0; // up pin is on port 0 of the AW9523
static constexpr uint16_t DOWN_PIN = (1<<1) << 0; // down pin is on port 0 of the AW9523
static constexpr uint16_t LEFT_PIN = (1<<2) << 0; // left pin is on port 0 of the AW9523
static constexpr uint16_t RIGHT_PIN = (1<<3) << 0; // right pin is on port 0 of the AW9523
static constexpr uint16_t A_PIN = (1<<4) << 0; // a pin is on port 0 of the AW9523
static constexpr uint16_t B_PIN = (1<<5) << 0; // b pin is on port 0 of the AW9523
static constexpr uint16_t X_PIN = (1<<6) << 0; // x pin is on port 0 of the AW9523
static constexpr uint16_t Y_PIN = (1<<7) << 0; // y pin is on port 0 of the AW9523
static constexpr uint16_t START_PIN = (1<<0) << 8; // start pin is on port 1 of the AW9523
static constexpr uint16_t SELECT_PIN = (1<<1) << 8; // select pin is on port 1 of the AW9523
static constexpr uint16_t BAT_ALERT_PIN = (1<<3) << 8; // battery alert pin is on port 1 of the AW9523
static constexpr uint16_t VOL_UP_PIN = (1<<4) << 8; // volume up pin is on port 1 of the AW9523
static constexpr uint16_t VOL_DOWN_PIN = (1<<5) << 8; // volume down pin is on port 1 of the AW9523
static constexpr uint16_t DIRECTION_MASK = (UP_PIN | DOWN_PIN | LEFT_PIN | RIGHT_PIN | A_PIN | B_PIN | X_PIN | Y_PIN | START_PIN | SELECT_PIN | BAT_ALERT_PIN | VOL_UP_PIN | VOL_DOWN_PIN);
static constexpr uint16_t INTERRUPT_MASK = (BAT_ALERT_PIN);
static constexpr uint8_t PORT_0_DIRECTION_MASK = DIRECTION_MASK & 0xFF;
static constexpr uint8_t PORT_1_DIRECTION_MASK = (DIRECTION_MASK >> 8) & 0xFF;
static constexpr uint8_t PORT_0_INTERRUPT_MASK = INTERRUPT_MASK & 0xFF;
static constexpr uint8_t PORT_1_INTERRUPT_MASK = (INTERRUPT_MASK >> 8) & 0xFF;
#endif
7 changes: 5 additions & 2 deletions components/box-emu-hal/include/fs_init.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
#include <sys/stat.h>
#include <errno.h>

#include "esp_vfs_fat.h"
#include "sdmmc_cmd.h"
#include <esp_vfs_fat.h>
#include <sdmmc_cmd.h>

#define MOUNT_POINT "/sdcard"

#include "hal.hpp"
#include "format.hpp"

void fs_init();
sdmmc_card_t *get_sdcard();
10 changes: 10 additions & 0 deletions components/box-emu-hal/include/usb.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#include <tinyusb.h>
#include <tusb_msc_storage.h>

#include "fs_init.hpp"

bool usb_is_enabled();
void usb_init();
void usb_deinit();
15 changes: 9 additions & 6 deletions components/box-emu-hal/src/fs_init.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#include "fs_init.hpp"

#include "format.hpp"

using namespace box_hal;
static sdmmc_card_t *sdcard = nullptr;

static void sdcard_init() {
esp_err_t ret;
Expand All @@ -15,7 +14,6 @@ static void sdcard_init() {
.max_files = 5,
.allocation_unit_size = 16 * 1024
};
sdmmc_card_t *card;
const char mount_point[] = "/sdcard";
fmt::print("Initializing SD card\n");

Expand Down Expand Up @@ -54,24 +52,29 @@ static void sdcard_init() {
slot_config.host_id = host_id;

fmt::print("Mounting filesystem\n");
ret = esp_vfs_fat_sdspi_mount(mount_point, &host, &slot_config, &mount_config, &card);
ret = esp_vfs_fat_sdspi_mount(mount_point, &host, &slot_config, &mount_config, &sdcard);

if (ret != ESP_OK) {
if (ret == ESP_FAIL) {
fmt::print("Failed to mount filesystem. "
"If you want the card to be formatted, set the CONFIG_EXAMPLE_FORMAT_IF_MOUNT_FAILED menuconfig option.\n");
} else {
fmt::print("Failed to initialize the card (%s). "
fmt::print("Failed to initialize the card ({}). "
"Make sure SD card lines have pull-up resistors in place.\n", esp_err_to_name(ret));
}
return;
}
fmt::print("Filesystem mounted\n");

// Card has been initialized, print its properties
sdmmc_card_print_info(stdout, card);
sdmmc_card_print_info(stdout, sdcard);
}

void fs_init() {
if (sdcard) return;
sdcard_init();
}

sdmmc_card_t *get_sdcard() {
return sdcard;
}
70 changes: 35 additions & 35 deletions components/box-emu-hal/src/input.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#include <mutex>

#include "input.h"

#include "hal_i2c.hpp"
#include <driver/gpio.h>

#include "mcp23x17.hpp"
#include "timer.hpp"
#include "touchpad_input.hpp"
#include "keypad_input.hpp"

#include "input.h"
#include "box_emu_hal.hpp"
#include "hal_i2c.hpp"

using namespace std::chrono_literals;
using namespace box_hal;

Expand All @@ -19,7 +20,7 @@ struct TouchpadData {
uint8_t btn_state = 0;
};

static std::shared_ptr<espp::Mcp23x17> mcp23x17;
static std::shared_ptr<InputDriver> input_driver;
static std::shared_ptr<TouchDriver> touch_driver;
static std::shared_ptr<espp::TouchpadInput> touchpad;
static std::shared_ptr<espp::KeypadInput> keypad;
Expand Down Expand Up @@ -77,44 +78,43 @@ void update_touchpad_input() {
}

void update_gamepad_input() {
bool is_a_pressed = false;
bool is_b_pressed = false;
bool is_x_pressed = false;
bool is_y_pressed = false;
static bool can_read_input = true;
bool is_select_pressed = false;
bool is_start_pressed = false;
bool is_up_pressed = false;
bool is_down_pressed = false;
bool is_left_pressed = false;
bool is_right_pressed = false;
if (!mcp23x17) {
fmt::print("cannot get input state: mcp23x17 not initialized properly!\n");
bool is_a_pressed = false;
bool is_b_pressed = false;
bool is_x_pressed = false;
bool is_y_pressed = false;
if (!input_driver) {
fmt::print("cannot get input state: input driver not initialized properly!\n");
return;
}
if (!can_read_input) {
return;
}
// pins are active low
// start, select = A0, A1
std::error_code ec;
auto a_pins = mcp23x17->get_pins(espp::Mcp23x17::Port::A, ec);
if (ec) {
fmt::print("error getting pins from mcp23x17: {}\n", ec.message());
return;
}
// d-pad, abxy = B0-B3, B4-B7
auto b_pins = mcp23x17->get_pins(espp::Mcp23x17::Port::B, ec);
auto pins = input_driver->get_pins(ec);
if (ec) {
fmt::print("error getting pins from mcp23x17: {}\n", ec.message());
fmt::print("error getting pins: {}\n", ec.message());
can_read_input = false;
return;
}
is_a_pressed = !(b_pins & 1<<4);
is_b_pressed = !(b_pins & 1<<5);
is_x_pressed = !(b_pins & 1<<6);
is_y_pressed = !(b_pins & 1<<7);
is_start_pressed = !(a_pins & 1<<0);
is_select_pressed = !(a_pins & 1<<1);
is_up_pressed = !(b_pins & 1<<0);
is_down_pressed = !(b_pins & 1<<1);
is_left_pressed = !(b_pins & 1<<2);
is_right_pressed = !(b_pins & 1<<3);
is_start_pressed = !(pins & START_PIN);
is_select_pressed = !(pins & SELECT_PIN);
is_up_pressed = !(pins & UP_PIN);
is_down_pressed = !(pins & DOWN_PIN);
is_left_pressed = !(pins & LEFT_PIN);
is_right_pressed = !(pins & RIGHT_PIN);
is_a_pressed = !(pins & A_PIN);
is_b_pressed = !(pins & B_PIN);
is_x_pressed = !(pins & X_PIN);
is_y_pressed = !(pins & Y_PIN);
{
std::lock_guard<std::mutex> lock(gamepad_state_mutex);
gamepad_state.a = is_a_pressed;
Expand Down Expand Up @@ -162,12 +162,12 @@ void init_input() {
.log_level = espp::Logger::Verbosity::WARN
});

fmt::print("initializing MCP23X17\n");
mcp23x17 = std::make_shared<espp::Mcp23x17>(espp::Mcp23x17::Config{
.port_a_direction_mask = 0x03, // Start on A0, Select on A1
.port_a_interrupt_mask = 0x00,
.port_b_direction_mask = 0xFF, // D-pad B0-B3, ABXY B4-B7
.port_b_interrupt_mask = 0x00,
fmt::print("initializing input driver\n");
input_driver = std::make_shared<InputDriver>(InputDriver::Config{
.port_0_direction_mask = PORT_0_DIRECTION_MASK,
.port_0_interrupt_mask = PORT_0_INTERRUPT_MASK,
.port_1_direction_mask = PORT_1_DIRECTION_MASK,
.port_1_interrupt_mask = PORT_1_INTERRUPT_MASK,
.write = std::bind(&espp::I2c::write, external_i2c.get(), std::placeholders::_1, std::placeholders::_2, std::placeholders::_3),
.read = std::bind(&espp::I2c::read_at_register, external_i2c.get(), std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4),
.log_level = espp::Logger::Verbosity::WARN
Expand Down
Loading

0 comments on commit 974a613

Please sign in to comment.