diff --git a/.gitignore b/.gitignore index c71be5a..22d5313 100644 --- a/.gitignore +++ b/.gitignore @@ -32,3 +32,5 @@ creaturedex.wiki/ *.zip *.lck schematics/.history/ + +wokwi/wokwi-api.h diff --git a/build.rs b/build.rs index 926a012..e90d624 100644 --- a/build.rs +++ b/build.rs @@ -1,5 +1,6 @@ fn main() { generate_filter_snippet(); + compile_custom_wokwi_components(); linker_be_nice(); // make sure linkall.x is the last linker script (otherwise might cause problems with flip-link) println!("cargo:rustc-link-arg=-Tlinkall.x"); @@ -70,6 +71,28 @@ fn linker_be_nice() { ); } +fn compile_custom_wokwi_components() { + use std::process::Command; + + if std::env::var("CARGO_FEATURE_WOKWI").is_ok() { + println!("cargo:rerun-if-changed=wokwi/ch1115.chip.c"); + println!("cargo:rerun-if-changed=wokwi/ch1115.chip.json"); + println!("cargo:rerun-if-changed=wokwi/Makefile"); + + let status = Command::new("make") + .current_dir("wokwi") + .status() + .expect("Failed to execute make command. Is 'make' installed?"); + + if !status.success() { + panic!( + "Wokwi custom chip compilation failed with status: {}", + status + ); + } + } +} + // Taken over from esp_println because impossible to just extend their existing env logger. // https://github.com/esp-rs/esp-hal/blob/8fa6d7e985e724d6ad998e34307429b0920da370/esp-println/build.rs fn generate_filter_snippet() { diff --git a/diagram.json b/diagram.json index 4959541..9d7e4cc 100644 --- a/diagram.json +++ b/diagram.json @@ -59,19 +59,23 @@ "top": -291.2, "left": -167.2, "attrs": { "pins": "i2c" } - } + }, + { "type": "chip-ch1115", "id": "chip1", "top": 509.82, "left": -4.8, "attrs": {} }, + { "type": "chip-ch1115", "id": "chip2", "top": 625.02, "left": -4.8, "attrs": {} }, + { "type": "chip-ch1115", "id": "chip3", "top": 394.62, "left": -4.8, "attrs": {} }, + { "type": "wokwi-vcc", "id": "vcc1", "top": 39.16, "left": 316.8, "attrs": {} } ], "connections": [ [ "esp1:TX", "$serialMonitor:RX", "", [] ], [ "esp1:RX", "$serialMonitor:TX", "", [] ], [ "lcd1:VCC", "esp1:3V3", "red", [ "v19.2", "h-57.6", "v-105.6", "h-124.95" ] ], [ "lcd1:GND", "esp1:GND.2", "black", [ "v28.8", "h-76.8", "v-76.8" ] ], - [ "lcd1:CS", "esp1:11", "orange", [ "v0" ] ], - [ "lcd1:D/C", "esp1:12", "gold", [ "v0" ] ], + [ "lcd1:CS", "esp1:11", "orange", [ "v259.2", "h-729.6", "v-95.82" ] ], + [ "lcd1:D/C", "esp1:12", "gold", [ "v278.4", "h-729.6", "v-105.42" ] ], [ "lcd1:RESET", "esp1:10", "brown", [ "v0" ] ], - [ "lcd1:MOSI", "esp1:35", "violet", [ "v105.6", "h-124.8", "v-144" ] ], + [ "lcd1:MOSI", "esp1:35", "violet", [ "v124.8", "h-124.8", "v0.18" ] ], [ "lcd1:SCK", "esp1:36", "white", [ "v115.2", "h-144", "v-86.4" ] ], - [ "esp1:37", "lcd1:MISO", "magenta", [ "h57.6", "v105.6", "h172.8" ] ], + [ "esp1:37", "lcd1:MISO", "magenta", [ "h57.6", "v-0.18", "h594.94" ] ], [ "btn-up:2.l", "esp1:38", "purple", [ "h-9.6", "v19.4" ] ], [ "btn-up:1.l", "esp1:GND.2", "black", [ "h0" ] ], [ "btn-down:1.l", "esp1:GND.2", "black", [ "h0" ] ], @@ -87,7 +91,31 @@ [ "lcd2:GND", "esp1:GND.2", "black", [ "h0" ] ], [ "lcd2:VCC", "esp1:3V3.1", "red", [ "h0" ] ], [ "lcd2:SDA", "esp1:17", "green", [ "h-134.4", "v413" ] ], - [ "lcd2:SCL", "esp1:18", "green", [ "h-124.8", "v38.7" ] ] + [ "lcd2:SCL", "esp1:18", "green", [ "h-124.8", "v38.7" ] ], + [ "chip2:RES", "chip1:RES", "gray", [ "h-28.8", "v-115.2" ] ], + [ "chip1:RES", "chip3:RES", "gray", [ "h-28.8", "v-67.2" ] ], + [ "chip2:GND", "chip1:GND", "black", [ "h-48", "v-115.2" ] ], + [ "chip1:GND", "chip3:GND", "black", [ "h-48", "v-115.2" ] ], + [ "chip2:VCC", "chip1:VCC", "red", [ "h-38.4", "v-115.2" ] ], + [ "chip3:VCC", "chip1:VCC", "red", [ "h-38.4", "v115.2" ] ], + [ "chip2:DC", "chip1:DC", "gold", [ "h46.84", "v-115.2" ] ], + [ "chip1:DC", "chip3:DC", "gold", [ "h46.84", "v-115.2" ] ], + [ "chip2:SCL", "chip1:SCL", "white", [ "h56.44", "v-115.2" ] ], + [ "chip1:SCL", "chip3:SCL", "white", [ "h56.44", "v-115.2" ] ], + [ "chip3:SDA", "chip1:SDA", "violet", [ "h-1.16", "h67.2", "v57.6" ] ], + [ "chip1:SDA", "chip2:SDA", "violet", [ "h66.04", "v115.2" ] ], + [ "esp1:35", "chip3:SDA", "violet", [ "h364.53", "v153.42" ] ], + [ "chip3:SCL", "esp1:36", "white", [ "h56.44", "v-172.62" ] ], + [ "esp1:12", "chip3:DC", "gold", [ "h-29.12", "v105.42", "h460.8", "v67.2" ] ], + [ "esp1:GND.4", "chip3:GND", "black", [ "h18.93", "v19.02", "h105.6", "v48" ] ], + [ "esp1:3V3.1", "chip3:VCC", "red", [ "h-57.92", "v259.02" ] ], + [ "lcd1:VCC", "vcc1:VCC", "red", [ "v67.2", "h-48" ] ], + [ "lcd1:LED", "vcc1:VCC", "red", [ "v67.2", "h-144.01" ] ], + [ "lcd1:RST", "esp1:10", "gray", [ "v268.8", "h-729.6", "v-115.02" ] ], + [ "esp1:47", "chip3:RES", "gray", [ "h28.53", "v182.22" ] ], + [ "chip3:CS", "esp1:13", "blue", [ "h-278.4", "v-153.42" ] ], + [ "esp1:14", "chip1:CS", "magenta", [ "h-9.92", "v259.02" ] ], + [ "esp1:21", "chip2:CS", "#8f4814", [ "h95.73", "v316.62" ] ] ], "dependencies": {} } \ No newline at end of file diff --git a/src/bin/main.rs b/src/bin/main.rs index f65da9a..f001eee 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -11,6 +11,7 @@ use alloc::boxed::Box; use creaturedex::background_tasks; use creaturedex::drivers::i2c_bus::I2cBusPinConfiguration; use creaturedex::drivers::nfc_pn532::NfcPn532Driver; +use creaturedex::drivers::primary_lcd::PrimaryDisplay; use creaturedex::drivers::secondary_oled::SecondaryDisplay; use creaturedex::drivers::spi_bus; use creaturedex::drivers::tertiary_lcd::{ @@ -71,7 +72,7 @@ async fn main(spawner: Spawner) { let timg0 = TimerGroup::new(esp_peripherals.TIMG0); esp_rtos::start(timg0.timer0, sw_interrupt.software_interrupt0); - let (primary, mut secondaries) = retry( + let (mut primary, mut secondaries) = retry( 5, spi_bus::DisplayPinConfiguration { spi_peripheral: esp_peripherals.SPI2, @@ -160,6 +161,7 @@ async fn main(spawner: Spawner) { display_shit(&mut secondaries[0], "foo 1"); display_shit(&mut secondaries[1], "bar 1"); display_shit(&mut secondaries[2], "baz 1"); + display_shit_prim(&mut primary, "Hello World"); let outputs = { let [sec_1, sec_2, sec_3] = secondaries; @@ -191,6 +193,16 @@ fn display_shit(oled: &mut SecondaryDisplay<'static>, text: &str) { oled.flush().unwrap(); } +fn display_shit_prim(main: &mut PrimaryDisplay<'static>, text: &str) { + use embedded_graphics::pixelcolor::Rgb565; + main.clear(Rgb565::BLACK).unwrap(); + + let text_style = MonoTextStyle::new(&FONT_6X10, Rgb565::WHITE); + Text::new(text, Point::new(10, 20), text_style) + .draw(main) + .unwrap(); +} + fn retry Result>(repetitions: u32, input: T, mut action: F) -> U { let mut initial_state = input; for iteration in 0..repetitions { diff --git a/src/drivers/spi_bus.rs b/src/drivers/spi_bus.rs index 89ec939..cb39249 100644 --- a/src/drivers/spi_bus.rs +++ b/src/drivers/spi_bus.rs @@ -158,14 +158,7 @@ where OutputConfig::default(), )); - let primary = PrimaryDisplayPinConfiguration { - spi_bus: bus_static, - cs: low_active(self.cs_primary), - reset: low_active(self.reset_primary), - dc: dc.clone(), - } - .build(); - + // Secondary displays must come first or they don't work in wokwi for some reason. let secondaries = SecondaryDisplayPinConfiguration { spi_bus: bus_static, cs_pins: [ @@ -174,6 +167,14 @@ where low_active(self.cs_secondary_3), ], res: reset_secondary, + dc: dc.clone(), + } + .build(); + + let primary = PrimaryDisplayPinConfiguration { + spi_bus: bus_static, + cs: low_active(self.cs_primary), + reset: low_active(self.reset_primary), dc, } .build(); diff --git a/wokwi.toml b/wokwi.toml index 4445166..c6e133d 100644 --- a/wokwi.toml +++ b/wokwi.toml @@ -3,3 +3,7 @@ version = 1 gdbServerPort = 3333 elf = "target/xtensa-esp32s3-none-elf/debug/creaturedex" firmware = "target/xtensa-esp32s3-none-elf/debug/creaturedex" + +[[chip]] +name = 'ch1115' +binary = 'wokwi/ch1115.chip.wasm' \ No newline at end of file diff --git a/wokwi/Makefile b/wokwi/Makefile new file mode 100644 index 0000000..d19a525 --- /dev/null +++ b/wokwi/Makefile @@ -0,0 +1,36 @@ +.PHONY: all auto native docker clean + +# Configuration +SRC = ch1115.chip.c +OUT = ch1115.chip.wasm + +# Check if wokwi-cli exists in PATH +WOKWI_CLI := $(shell command -v wokwi-cli 2> /dev/null) + +HOST_UID := $(shell id -u) +HOST_GID := $(shell id -g) + +# Default target runs the auto-detection +all: auto + +auto: +ifeq ($(WOKWI_CLI),) + @echo "wokwi-cli not found in PATH. Falling back to Docker build..." + $(MAKE) docker +else + @echo "Found wokwi-cli at $(WOKWI_CLI). Using native build..." + $(MAKE) native +endif + +native: + wokwi-cli chip compile $(SRC) -o $(OUT) + +docker: + docker run --rm -v $(CURDIR):/workspace -w /workspace ubuntu:latest \ + bash -c "apt-get update && apt-get install -y --no-install-recommends curl ca-certificates && \ + curl -L https://wokwi.com/ci/install.sh | sh && \ + ~/.wokwi/bin/wokwi-cli chip compile $(SRC) -o $(OUT) && \ + chown $(HOST_UID):$(HOST_GID) $(OUT)" + +clean: + rm -f $(OUT) \ No newline at end of file diff --git a/wokwi/ch1115.chip.c b/wokwi/ch1115.chip.c new file mode 100644 index 0000000..1673d4f --- /dev/null +++ b/wokwi/ch1115.chip.c @@ -0,0 +1,86 @@ +#include "wokwi-api.h" +#include +#include + +typedef struct { + pin_t cs, dc, res; + spi_dev_t spi; + buffer_t fb; + uint32_t width, height; + uint8_t spi_buffer[1]; + uint8_t page; + uint8_t col; +} chip_state_t; + +// Forward declarations +static void on_cs_change(void *user_data, pin_t pin, uint32_t value); +static void chip_spi_done(void *user_data, uint8_t *buffer, uint32_t count); + +void chip_init(void) { + printf("Initing CH1115 chip\n"); + fflush(stdout); + + chip_state_t *chip = malloc(sizeof(chip_state_t)); + chip->fb = framebuffer_init(&chip->width, &chip->height); + + chip->cs = pin_init("CS", INPUT_PULLUP); + chip->dc = pin_init("DC", INPUT); + chip->res = pin_init("RES", INPUT_PULLUP); + + const spi_config_t spi_cfg = { + .sck = pin_init("SCL", INPUT), + .mosi = pin_init("SDA", INPUT), + .miso = NO_PIN, + .mode = 0, + .done = chip_spi_done, + .user_data = chip, + }; + chip->spi = spi_init(&spi_cfg); + + const pin_watch_config_t cs_watch = { + .edge = BOTH, + .pin_change = on_cs_change, + .user_data = chip, + }; + pin_watch(chip->cs, &cs_watch); +} + +static void on_cs_change(void *user_data, pin_t pin, uint32_t value) { + chip_state_t *chip = (chip_state_t*)user_data; + if (value == LOW) { + // Start receiving 1 byte at a time + spi_start(chip->spi, chip->spi_buffer, 1); + } else { + spi_stop(chip->spi); + } +} + +static void chip_spi_done(void *user_data, uint8_t *buffer, uint32_t count) { + chip_state_t *chip = (chip_state_t*)user_data; + if (!count) return; + + uint8_t val = buffer[0]; + bool is_data = pin_read(chip->dc) == HIGH; + + if (is_data) { + // Write 8 vertical pixels to the Wokwi RGBA Framebuffer + for (int i = 0; i < 8; i++) { + // 0xFFFFFFFF = White, 0x000000FF = Black + uint32_t color = (val & (1 << i)) ? 0xFFFFFFFF : 0x000000FF; + uint32_t y = (chip->page * 8) + i; + uint32_t offset = (y * chip->width + chip->col) * 4; + buffer_write(chip->fb, offset, &color, 4); + } + chip->col++; // Auto-increment column + } else { + // CH1115 Core Commands + if ((val & 0xB0) == 0xB0) chip->page = val & 0x0F; // Set Page Start + else if ((val & 0xF0) == 0x00) chip->col = (chip->col & 0xF0) | (val & 0x0F); // Lower Col + else if ((val & 0xF0) == 0x10) chip->col = (chip->col & 0x0F) | ((val & 0x0F) << 4); // Upper Col + } + + // Continue receiving if CS is still active + if (pin_read(chip->cs) == LOW) { + spi_start(chip->spi, chip->spi_buffer, 1); + } +} \ No newline at end of file diff --git a/wokwi/ch1115.chip.json b/wokwi/ch1115.chip.json new file mode 100644 index 0000000..4fa1d84 --- /dev/null +++ b/wokwi/ch1115.chip.json @@ -0,0 +1,9 @@ +{ + "name": "CH1115 OLED", + "author": "ede1998", + "pins": ["VCC", "GND", "CS", "RES", "DC", "SCL", "SDA"], + "display": { + "width": 128, + "height": 64 + } +} \ No newline at end of file diff --git a/wokwi/ch1115.chip.wasm b/wokwi/ch1115.chip.wasm new file mode 100755 index 0000000..0783263 Binary files /dev/null and b/wokwi/ch1115.chip.wasm differ