Add support for boot/disc storage on SD card
Uses the no-OS-FatFS-SD-SPI-RPi-Pico library, and on startup opens the fs to search for 'umac0.img' or 'umac0ro.img'. If the former is found, it's used as a RW image; the latter is RO. If no images are found, the code falls back to an in-flash disc image; you can set up a default that's used if no SD card is inserted, for example. The wiring defaults to GPIOs 2/3/4/5 for SCK/TX/RX/CS respectively, which matches the upper-left SPI0 on the Pico board. The wiring can be overridden by defining SD_TX, SD_RX, SD_SCK, SD_CS on the `cmake` build commandline. The SPI speed defaults to a very conservative 5MHz (at least one of my cards will only cope with this). Again, this can be overridden by defining SD_MHZ on the commandline (to an integer in units of MHz). Note this also disables the RTC portion of FatFS when building for RP2350 (which doesn't have an RTC, or APIs for it).
This commit is contained in:
parent
cd92bf8ffb
commit
67cb1741c6
3 changed files with 215 additions and 3 deletions
|
|
@ -29,6 +29,13 @@ cmake_minimum_required(VERSION 3.13)
|
||||||
# directory with cmake, e.g. "cmake .. -DOPTION=true":
|
# directory with cmake, e.g. "cmake .. -DOPTION=true":
|
||||||
#
|
#
|
||||||
|
|
||||||
|
option(USE_SD "Build in SD support" OFF)
|
||||||
|
set(SD_TX 3 CACHE STRING "SD SPI TX pin")
|
||||||
|
set(SD_RX 4 CACHE STRING "SD SPI RX pin")
|
||||||
|
set(SD_SCK 2 CACHE STRING "SD SPI SCK pin")
|
||||||
|
set(SD_CS 5 CACHE STRING "SD SPI CS pin")
|
||||||
|
set(SD_MHZ 5 CACHE STRING "SD SPI speed in MHz")
|
||||||
|
|
||||||
# See below, -DMEMSIZE=<size in KB> will configure umac's memory size,
|
# See below, -DMEMSIZE=<size in KB> will configure umac's memory size,
|
||||||
# overriding defaults.
|
# overriding defaults.
|
||||||
|
|
||||||
|
|
@ -68,12 +75,22 @@ set(UMAC_SOURCES
|
||||||
set(MEMSIZE 128 CACHE STRING "Memory size, in KB")
|
set(MEMSIZE 128 CACHE STRING "Memory size, in KB")
|
||||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 -DPICO -DMUSASHI_CNF=\\\"../include/m68kconf.h\\\" -DUMAC_MEMSIZE=${MEMSIZE}")
|
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 -DPICO -DMUSASHI_CNF=\\\"../include/m68kconf.h\\\" -DUMAC_MEMSIZE=${MEMSIZE}")
|
||||||
|
|
||||||
|
if (USE_SD)
|
||||||
|
add_compile_definitions(USE_SD=1)
|
||||||
|
set(FF_DISABLE_RTC ${PICO_RP2350}) # RP2350 doesn't have RTC, so disable it
|
||||||
|
add_subdirectory(external/no-OS-FatFS-SD-SPI-RPi-Pico/FatFs_SPI build)
|
||||||
|
set(EXTRA_SD_SRC src/sd_hw_config.c)
|
||||||
|
set(EXTRA_SD_LIB FatFs_SPI)
|
||||||
|
add_compile_definitions(SD_TX=${SD_TX} SD_RX=${SD_RX} SD_SCK=${SD_SCK} SD_CS=${SD_CS} SD_MHZ=${SD_MHZ})
|
||||||
|
endif()
|
||||||
|
|
||||||
if (TARGET tinyusb_device)
|
if (TARGET tinyusb_device)
|
||||||
add_executable(firmware
|
add_executable(firmware
|
||||||
src/main.c
|
src/main.c
|
||||||
src/video.c
|
src/video.c
|
||||||
src/kbd.c
|
src/kbd.c
|
||||||
src/hid.c
|
src/hid.c
|
||||||
|
${EXTRA_SD_SRC}
|
||||||
|
|
||||||
${UMAC_SOURCES}
|
${UMAC_SOURCES}
|
||||||
)
|
)
|
||||||
|
|
@ -96,6 +113,7 @@ if (TARGET tinyusb_device)
|
||||||
hardware_dma
|
hardware_dma
|
||||||
hardware_pio
|
hardware_pio
|
||||||
hardware_sync
|
hardware_sync
|
||||||
|
${EXTRA_SD_LIB}
|
||||||
)
|
)
|
||||||
|
|
||||||
target_include_directories(firmware PRIVATE
|
target_include_directories(firmware PRIVATE
|
||||||
|
|
|
||||||
111
src/main.c
111
src/main.c
|
|
@ -45,6 +45,13 @@
|
||||||
|
|
||||||
#include "umac.h"
|
#include "umac.h"
|
||||||
|
|
||||||
|
#if USE_SD
|
||||||
|
#include "f_util.h"
|
||||||
|
#include "ff.h"
|
||||||
|
#include "rtc.h"
|
||||||
|
#include "hw_config.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
// Imports and data
|
// Imports and data
|
||||||
|
|
||||||
|
|
@ -138,14 +145,110 @@ static void poll_umac()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void core1_main()
|
#if USE_SD
|
||||||
|
static int disc_do_read(void *ctx, uint8_t *data, unsigned int offset, unsigned int len)
|
||||||
{
|
{
|
||||||
printf("Core 1 started\n");
|
FIL *fp = (FIL *)ctx;
|
||||||
|
f_lseek(fp, offset);
|
||||||
|
unsigned int did_read = 0;
|
||||||
|
FRESULT fr = f_read(fp, data, len, &did_read);
|
||||||
|
if (fr != FR_OK || len != did_read) {
|
||||||
|
printf("disc: f_read returned %d, read %u (of %u)\n", fr, did_read, len);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
disc_descr_t discs[DISC_NUM_DRIVES] = {0};
|
static int disc_do_write(void *ctx, uint8_t *data, unsigned int offset, unsigned int len)
|
||||||
|
{
|
||||||
|
FIL *fp = (FIL *)ctx;
|
||||||
|
f_lseek(fp, offset);
|
||||||
|
unsigned int did_write = 0;
|
||||||
|
FRESULT fr = f_write(fp, data, len, &did_write);
|
||||||
|
if (fr != FR_OK || len != did_write) {
|
||||||
|
printf("disc: f_write returned %d, read %u (of %u)\n", fr, did_write, len);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static FIL discfp;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static void disc_setup(disc_descr_t discs[DISC_NUM_DRIVES])
|
||||||
|
{
|
||||||
|
#if USE_SD
|
||||||
|
char *disc0_name;
|
||||||
|
const char *disc0_ro_name = "umac0ro.img";
|
||||||
|
const char *disc0_pattern = "umac0*.img";
|
||||||
|
|
||||||
|
/* Mount SD filesystem */
|
||||||
|
printf("Starting SPI/FatFS:\n");
|
||||||
|
set_spi_dma_irq_channel(true, false);
|
||||||
|
sd_card_t *pSD = sd_get_by_num(0);
|
||||||
|
FRESULT fr = f_mount(&pSD->fatfs, pSD->pcName, 1);
|
||||||
|
printf(" mount: %d\n", fr);
|
||||||
|
if (fr != FR_OK) {
|
||||||
|
printf(" error mounting disc: %s (%d)\n", FRESULT_str(fr), fr);
|
||||||
|
goto no_sd;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Look for a disc image */
|
||||||
|
DIR di = {0};
|
||||||
|
FILINFO fi = {0};
|
||||||
|
fr = f_findfirst(&di, &fi, "/", disc0_pattern);
|
||||||
|
if (fr != FR_OK) {
|
||||||
|
printf(" Can't find images %s: %s (%d)\n", disc0_pattern, FRESULT_str(fr), fr);
|
||||||
|
goto no_sd;
|
||||||
|
}
|
||||||
|
disc0_name = fi.fname;
|
||||||
|
f_closedir(&di);
|
||||||
|
|
||||||
|
int read_only = !strcmp(disc0_name, disc0_ro_name);
|
||||||
|
printf(" Opening %s (R%c)\n", disc0_name, read_only ? 'O' : 'W');
|
||||||
|
|
||||||
|
/* Open image, set up disc info: */
|
||||||
|
fr = f_open(&discfp, disc0_name, FA_OPEN_EXISTING | FA_READ | FA_WRITE);
|
||||||
|
if (fr != FR_OK && fr != FR_EXIST) {
|
||||||
|
printf(" *** Can't open %s: %s (%d)!\n", disc0_name, FRESULT_str(fr), fr);
|
||||||
|
goto no_sd;
|
||||||
|
} else {
|
||||||
|
printf(" Opened, size 0x%x\n", f_size(&discfp));
|
||||||
|
if (read_only)
|
||||||
|
printf(" (disc is read-only)\n");
|
||||||
|
discs[0].base = 0; // Means use R/W ops
|
||||||
|
discs[0].read_only = read_only;
|
||||||
|
discs[0].size = f_size(&discfp);
|
||||||
|
discs[0].op_ctx = &discfp;
|
||||||
|
discs[0].op_read = disc_do_read;
|
||||||
|
discs[0].op_write = disc_do_write;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* FIXME: Other files can be stored on SD too, such as logging
|
||||||
|
* and NVRAM storage.
|
||||||
|
*
|
||||||
|
* We could also implement a menu here to select an image,
|
||||||
|
* writing text to the framebuffer and checking kbd_queue_*()
|
||||||
|
* for user input.
|
||||||
|
*/
|
||||||
|
return;
|
||||||
|
|
||||||
|
no_sd:
|
||||||
|
#endif
|
||||||
|
/* If we don't find (or look for) an SD-based image, attempt
|
||||||
|
* to use in-flash disc image:
|
||||||
|
*/
|
||||||
discs[0].base = (void *)umac_disc;
|
discs[0].base = (void *)umac_disc;
|
||||||
discs[0].read_only = 1;
|
discs[0].read_only = 1;
|
||||||
discs[0].size = sizeof(umac_disc);
|
discs[0].size = sizeof(umac_disc);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void core1_main()
|
||||||
|
{
|
||||||
|
disc_descr_t discs[DISC_NUM_DRIVES] = {0};
|
||||||
|
|
||||||
|
printf("Core 1 started\n");
|
||||||
|
disc_setup(discs);
|
||||||
|
|
||||||
umac_init(umac_ram, (void *)umac_rom, discs);
|
umac_init(umac_ram, (void *)umac_rom, discs);
|
||||||
/* Video runs on core 1, i.e. IRQs/DMA are unaffected by
|
/* Video runs on core 1, i.e. IRQs/DMA are unaffected by
|
||||||
|
|
@ -153,6 +256,8 @@ static void core1_main()
|
||||||
*/
|
*/
|
||||||
video_init((uint32_t *)(umac_ram + umac_get_fb_offset()));
|
video_init((uint32_t *)(umac_ram + umac_get_fb_offset()));
|
||||||
|
|
||||||
|
printf("Enjoyable Mac times now begin:\n\n");
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
poll_umac();
|
poll_umac();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
89
src/sd_hw_config.c
Executable file
89
src/sd_hw_config.c
Executable file
|
|
@ -0,0 +1,89 @@
|
||||||
|
/* hw_config.c
|
||||||
|
Copyright 2021 Carl John Kugler III
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the License); you may not use
|
||||||
|
this file except in compliance with the License. You may obtain a copy of the
|
||||||
|
License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
Unless required by applicable law or agreed to in writing, software distributed
|
||||||
|
under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR
|
||||||
|
CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||||
|
specific language governing permissions and limitations under the License.
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
|
||||||
|
This file should be tailored to match the hardware design.
|
||||||
|
|
||||||
|
There should be one element of the spi[] array for each hardware SPI used.
|
||||||
|
|
||||||
|
There should be one element of the sd_cards[] array for each SD card slot.
|
||||||
|
The name is should correspond to the FatFs "logical drive" identifier.
|
||||||
|
(See http://elm-chan.org/fsw/ff/doc/filename.html#vol)
|
||||||
|
The rest of the constants will depend on the type of
|
||||||
|
socket, which SPI it is driven by, and how it is wired.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
//
|
||||||
|
#include "my_debug.h"
|
||||||
|
//
|
||||||
|
#include "hw_config.h"
|
||||||
|
//
|
||||||
|
#include "ff.h" /* Obtains integer types */
|
||||||
|
//
|
||||||
|
#include "diskio.h" /* Declarations of disk functions */
|
||||||
|
|
||||||
|
// Hardware Configuration of SPI "objects"
|
||||||
|
// Note: multiple SD cards can be driven by one SPI if they use different slave
|
||||||
|
// selects.
|
||||||
|
static spi_t spis[] = { // One for each SPI.
|
||||||
|
{
|
||||||
|
.hw_inst = spi0, // SPI component
|
||||||
|
.miso_gpio = SD_RX, // GPIO number (not pin number)
|
||||||
|
.mosi_gpio = SD_TX,
|
||||||
|
.sck_gpio = SD_SCK,
|
||||||
|
|
||||||
|
.set_drive_strength = true,
|
||||||
|
.mosi_gpio_drive_strength = GPIO_DRIVE_STRENGTH_8MA,
|
||||||
|
.sck_gpio_drive_strength = GPIO_DRIVE_STRENGTH_8MA,
|
||||||
|
|
||||||
|
// One of my cards doesn't seem to work beyond 5MHz :(
|
||||||
|
.baud_rate = 5 * 1000 * 1000,
|
||||||
|
//.baud_rate = 25 * 1000 * 1000, // Actual frequency: 20833333.
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Hardware Configuration of the SD Card "objects"
|
||||||
|
static sd_card_t sd_cards[] = { // One for each SD card
|
||||||
|
{
|
||||||
|
.pcName = "0:", // Name used to mount device
|
||||||
|
.spi = &spis[0], // Pointer to the SPI driving this card
|
||||||
|
.ss_gpio = SD_CS, // The SPI slave select GPIO for this SD card
|
||||||
|
.set_drive_strength = true,
|
||||||
|
.ss_gpio_drive_strength = GPIO_DRIVE_STRENGTH_8MA,
|
||||||
|
.use_card_detect = false,
|
||||||
|
.card_detected_true = -1 // What the GPIO read returns when a card is
|
||||||
|
// present. Use -1 if there is no card detect.
|
||||||
|
}};
|
||||||
|
|
||||||
|
/* ********************************************************************** */
|
||||||
|
size_t sd_get_num() { return count_of(sd_cards); }
|
||||||
|
sd_card_t *sd_get_by_num(size_t num) {
|
||||||
|
if (num <= sd_get_num()) {
|
||||||
|
return &sd_cards[num];
|
||||||
|
} else {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
size_t spi_get_num() { return count_of(spis); }
|
||||||
|
spi_t *spi_get_by_num(size_t num) {
|
||||||
|
if (num <= sd_get_num()) {
|
||||||
|
return &spis[num];
|
||||||
|
} else {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* [] END OF FILE */
|
||||||
Loading…
Reference in a new issue