Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pico: SPI-mode SD card support #819

Merged
merged 6 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion 32blit-pico/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ target_sources(BlitHalPico INTERFACE
${CMAKE_CURRENT_LIST_DIR}/file.cpp
${CMAKE_CURRENT_LIST_DIR}/led.cpp
${CMAKE_CURRENT_LIST_DIR}/main.cpp
${CMAKE_CURRENT_LIST_DIR}/storage.cpp
${CMAKE_CURRENT_LIST_DIR}/multiplayer.cpp
${CMAKE_CURRENT_LIST_DIR}/st7789.cpp
${CMAKE_CURRENT_LIST_DIR}/usb_descriptors.c
Expand Down Expand Up @@ -99,6 +98,9 @@ endif()
if(NOT BLIT_INPUT_DRIVER)
set(BLIT_INPUT_DRIVER "none")
endif()
if(NOT BLIT_STORAGE_DRIVER)
set(BLIT_STORAGE_DRIVER "flash")
endif()
if(NOT BLIT_USB_DRIVER)
set(BLIT_USB_DRIVER "device")
endif()
Expand All @@ -124,6 +126,10 @@ if(BLIT_INPUT_DRIVER STREQUAL "usb_hid")
list(APPEND BLIT_BOARD_DEFINITIONS INPUT_USB_HID)
endif()

if(BLIT_STORAGE_DRIVER STREQUAL "sd_spi")
list(APPEND BLIT_BOARD_DEFINITIONS STORAGE_SD)
endif()

if(BLIT_USB_DRIVER STREQUAL "host")
list(APPEND BLIT_BOARD_DEFINITIONS USB_HOST)
list(APPEND BLIT_BOARD_LIBRARIES tinyusb_host)
Expand All @@ -139,12 +145,14 @@ pico_sdk_init()

# generate PIO headers (has to be after SDK init)
pico_generate_pio_header(BlitHalPico ${CMAKE_CURRENT_LIST_DIR}/st7789.pio)
pico_generate_pio_header(BlitHalPico ${CMAKE_CURRENT_LIST_DIR}/spi.pio)

# driver sources
target_sources(BlitHalPico INTERFACE
${CMAKE_CURRENT_LIST_DIR}/audio_${BLIT_AUDIO_DRIVER}.cpp
${CMAKE_CURRENT_LIST_DIR}/display_${BLIT_DISPLAY_DRIVER}.cpp
${CMAKE_CURRENT_LIST_DIR}/input_${BLIT_INPUT_DRIVER}.cpp
${CMAKE_CURRENT_LIST_DIR}/storage_${BLIT_STORAGE_DRIVER}.cpp
${CMAKE_CURRENT_LIST_DIR}/usb_${BLIT_USB_DRIVER}.cpp
)

Expand Down
1 change: 1 addition & 0 deletions 32blit-pico/board/vgaboard/config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ set(BLIT_BOARD_DEFINITIONS
blit_driver(audio i2s)
blit_driver(display scanvideo)
blit_driver(input usb_hid)
blit_driver(storage sd_spi)
blit_driver(usb host)
6 changes: 6 additions & 0 deletions 32blit-pico/board/vgaboard/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@
#ifndef ALLOW_HIRES
#define ALLOW_HIRES 0 // disable by default, mode switching isn't supported
#endif

// spi
#define SD_SCK 5
#define SD_MOSI 18
#define SD_MISO 19
#define SD_CS 22
7 changes: 6 additions & 1 deletion 32blit-pico/ffconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,14 @@
/ arbitrary physical drive and partition listed in the VolToPart[]. Also f_fdisk()
/ funciton will be available. */


#ifdef STORAGE_SD
#define FF_MIN_SS 512
#define FF_MAX_SS 512
#else
#define FF_MIN_SS 4096
#define FF_MAX_SS 4096
#endif

/* This set of options configures the range of sector size to be supported. (512,
/ 1024, 2048 or 4096) Always set both 512 for most systems, generic memory card and
/ harddisk, but a larger value may be required for on-board flash memory and some
Expand Down
9 changes: 7 additions & 2 deletions 32blit-pico/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@
#include "storage.hpp"

static FATFS fs;
static bool initialised = false;

std::vector<void *> open_files;

// fatfs io funcs
DSTATUS disk_initialize(BYTE pdrv) {
return RES_OK;
initialised = storage_init();
return initialised ? RES_OK : STA_NOINIT;
}

DSTATUS disk_status(BYTE pdrv) {
return RES_OK; // FIXME: NOINIT?
return initialised ? RES_OK : STA_NOINIT;
}

DRESULT disk_read(BYTE pdrv, BYTE *buff, LBA_t sector, UINT count) {
Expand Down Expand Up @@ -57,6 +59,8 @@ DRESULT disk_ioctl(BYTE pdrv, BYTE cmd, void* buff) {
void init_fs() {
auto res = f_mount(&fs, "", 1);

// auto-format flash, but not SD cards
#ifndef STORAGE_SD
if(res == FR_NO_FILESYSTEM) {
printf("No filesystem found, formatting...\n");

Expand All @@ -71,6 +75,7 @@ void init_fs() {

res = f_mount(&fs, "", 1);
}
#endif

if(res != FR_OK)
printf("Failed to mount filesystem! (%i)\n", res);
Expand Down
4 changes: 4 additions & 0 deletions 32blit-pico/spi.pio
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.program spi_cpha0
.side_set 1
out pins, 1 side 0
in pins, 1 side 1
2 changes: 2 additions & 0 deletions 32blit-pico/storage.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once
#include <cstdint>

bool storage_init();

void get_storage_size(uint16_t &block_size, uint32_t &num_blocks);

int32_t storage_read(uint32_t sector, uint32_t offset, void *buffer, uint32_t size_bytes);
Expand Down
6 changes: 5 additions & 1 deletion 32blit-pico/storage.cpp → 32blit-pico/storage_flash.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// raw storage interface, currently flash
// raw flash storage interface
#include "storage.hpp"

#include <cstring>
Expand All @@ -15,6 +15,10 @@ static const uint32_t storage_offset = PICO_FLASH_SIZE_BYTES - storage_size;

extern bool core1_started;

bool storage_init() {
return true;
}

void get_storage_size(uint16_t &block_size, uint32_t &num_blocks) {
block_size = FLASH_SECTOR_SIZE;
num_blocks = storage_size / FLASH_SECTOR_SIZE;
Expand Down
Loading