From d86013c90c979f21eb0b344486580f61089feaba Mon Sep 17 00:00:00 2001 From: Austin Morlan Date: Tue, 19 May 2020 18:16:30 -0700 Subject: [PATCH] Part 4: Storage --- CMakeLists.txt | 1 - README.md | 12 ++++++++++ src/CMakeLists.txt | 4 +++- src/main.c | 54 ++++++++++++++++++++++++++++++++++++++++++--- src/odroid/sdcard.c | 33 +++++++++++++++++++++++++++ src/odroid/sdcard.h | 5 +++++ 6 files changed, 104 insertions(+), 5 deletions(-) create mode 100644 src/odroid/sdcard.c create mode 100644 src/odroid/sdcard.h diff --git a/CMakeLists.txt b/CMakeLists.txt index c54e1d9..9eb2f37 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,6 @@ cmake_minimum_required(VERSION 3.5) set(EXTRA_COMPONENT_DIRS "src") set(COMPONENTS "esptool_py src") - include($ENV{IDF_PATH}/tools/cmake/project.cmake) project(game) diff --git a/README.md b/README.md index aeee955..38d852c 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,18 @@ * Get the [ESP-IDF v4.0](https://github.com/espressif/esp-idf/releases/tag/v4.0) * Follow Steps 1 through 3 of the [Documentation](https://docs.espressif.com/projects/esp-idf/en/v4.0/get-started/index.html#step-1-install-prerequisites) +* Comment out line 303 of `esp-idf/components/driver/sdspi_host.c` to enable the shared SPI bus + +```c +// Initialize SPI bus +esp_err_t ret = spi_bus_initialize((spi_host_device_t)slot, &buscfg, + slot_config->dma_channel); +if (ret != ESP_OK) { + ESP_LOGD(TAG, "spi_bus_initialize failed with rc=0x%x", ret); + //return ret; +} +``` + * Modify the `export.sh` script in this directory and fill out `IDF_PATH` and `IDF_TOOLS_PATH` * `IDF_PATH` is the extracted ESP-IDF * `IDF_TOOLS_PATH` is the path to the toolchain diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6bfbdb8..80e9f56 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -3,10 +3,12 @@ idf_component_register( "main.c" "odroid/display.c" "odroid/input.c" + "odroid/sdcard.c" INCLUDE_DIRS "." PRIV_REQUIRES - "esp_adc_cal") + "esp_adc_cal" + "fatfs") diff --git a/src/main.c b/src/main.c index b282892..6fc69c8 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,6 @@ #include "odroid/display.h" #include "odroid/input.h" +#include "odroid/sdcard.h" #include "macros.h" #include #include @@ -14,6 +15,25 @@ void app_main(void) { Odroid_InitializeInput(); Odroid_InitializeDisplay(); + Odroid_InitializeSdcard(); + + // Load sprite + uint16_t* sprite = (uint16_t*)malloc(64 * 64 * sizeof(uint16_t)); + { + FILE* spriteFile = fopen("/sdcard/key", "r"); + assert(spriteFile); + + for (int i = 0; i < 64; ++i) + { + for (int j = 0; j < 64; ++j) + { + fread(sprite, sizeof(uint16_t), 64 * 64, spriteFile); + } + } + + fclose(spriteFile); + } + ESP_LOGI(LOG_TAG, "Odroid initialization complete - entering main loop"); @@ -39,12 +59,40 @@ void app_main(void) else if (input.start) { color = SWAP_ENDIAN_16(RGB565(0, 0, 0xff)); } else if (input.select) { color = SWAP_ENDIAN_16(RGB565(0xff, 0xff, 0xff)); } - for (int row = y; row < y + 50; ++row) + int spriteRow = 0; + int spriteCol = 0; + + for (int row = y; row < y + 64; ++row) { - for (int col = x; col < x + 50; ++col) + spriteCol = 0; + + for (int col = x; col < x + 64; ++col) { - gFramebuffer[LCD_WIDTH * row + col] = color; + uint16_t pixelColor = sprite[64 * spriteRow + spriteCol]; + + if (pixelColor != 0) + { + gFramebuffer[row * LCD_WIDTH + col] = color; + } + + ++spriteCol; } + + ++spriteRow; + } + + if (input.menu) + { + const char* snapFilename = "/sdcard/framebuf"; + + ESP_LOGI(LOG_TAG, "Writing snapshot to %s", snapFilename); + + FILE* snapFile = fopen(snapFilename, "wb"); + assert(snapFile); + + fwrite(gFramebuffer, 1, LCD_WIDTH * LCD_HEIGHT * sizeof(gFramebuffer[0]), snapFile); + + fclose(snapFile); } Odroid_DrawFrame(gFramebuffer); diff --git a/src/odroid/sdcard.c b/src/odroid/sdcard.c new file mode 100644 index 0000000..8cde1ae --- /dev/null +++ b/src/odroid/sdcard.c @@ -0,0 +1,33 @@ +#include "sdcard.h" +#include +#include +#include +#include + + +static const gpio_num_t SD_PIN_MISO = GPIO_NUM_19; +static const gpio_num_t SD_PIN_MOSI = GPIO_NUM_23; +static const gpio_num_t SD_PIN_SCLK = GPIO_NUM_18; +static const gpio_num_t SD_PIN_CS = GPIO_NUM_22; + + +void Odroid_InitializeSdcard() +{ + sdmmc_host_t host = SDSPI_HOST_DEFAULT(); + host.slot = VSPI_HOST; + + sdspi_slot_config_t slot_config = SDSPI_SLOT_CONFIG_DEFAULT(); + slot_config.gpio_miso = SD_PIN_MISO; + slot_config.gpio_mosi = SD_PIN_MOSI; + slot_config.gpio_sck = SD_PIN_SCLK; + slot_config.gpio_cs = SD_PIN_CS; + + esp_vfs_fat_sdmmc_mount_config_t mount_config = {}; + mount_config.format_if_mount_failed = false; + mount_config.max_files = 5; + + sdmmc_card_t* card; + + ESP_ERROR_CHECK(esp_vfs_fat_sdmmc_mount("/sdcard", &host, &slot_config, &mount_config, &card)); +} + diff --git a/src/odroid/sdcard.h b/src/odroid/sdcard.h new file mode 100644 index 0000000..27fb78b --- /dev/null +++ b/src/odroid/sdcard.h @@ -0,0 +1,5 @@ +#pragma once + + +void Odroid_InitializeSdcard(void); +