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

Dynamic configuration #12

Merged
merged 10 commits into from
Sep 12, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion FatFs_SPI/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ target_sources(FatFs_SPI INTERFACE
${CMAKE_CURRENT_LIST_DIR}/ff14a/source/ff.c
${CMAKE_CURRENT_LIST_DIR}/sd_driver/sd_spi.c
${CMAKE_CURRENT_LIST_DIR}/sd_driver/demo_logging.c
${CMAKE_CURRENT_LIST_DIR}/sd_driver/hw_config.c
# ${CMAKE_CURRENT_LIST_DIR}/sd_driver/hw_config.c
${CMAKE_CURRENT_LIST_DIR}/sd_driver/spi.c
${CMAKE_CURRENT_LIST_DIR}/sd_driver/sd_card.c
${CMAKE_CURRENT_LIST_DIR}/sd_driver/crc.c
Expand Down
6 changes: 1 addition & 5 deletions FatFs_SPI/ff14a/source/ffconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,7 @@
/ Drive/Volume Configurations
/---------------------------------------------------------------------------*/

#if defined N_SD_CARDS
# define FF_VOLUMES N_SD_CARDS
#else
# define FF_VOLUMES 1
#endif
# define FF_VOLUMES 2
/* Number of volumes (logical drives) to be used. (1-10) */


Expand Down
12 changes: 6 additions & 6 deletions FatFs_SPI/sd_driver/hw_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
extern "C" {
#endif

size_t const sd_get_num();
sd_card_t *const sd_get_by_num(size_t num);
sd_card_t *const sd_get_by_name(const char *const name);
size_t const spi_get_num();
spi_t *const spi_get_by_num(size_t num);
FATFS *const get_fs_by_name(const char *name);
size_t sd_get_num();
sd_card_t *sd_get_by_num(size_t num);
sd_card_t *sd_get_by_name(const char * name);
size_t spi_get_num();
spi_t *spi_get_by_num(size_t num);
FATFS *get_fs_by_name(const char *name);

#ifdef __cplusplus
}
Expand Down
10 changes: 7 additions & 3 deletions FatFs_SPI/sd_driver/sd_card.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
* ========================================
*/

// Note: The model used here is one FatFS per SD card.
// Multiple partitions on a card are not supported.

#ifndef _SD_CARD_H_
#define _SD_CARD_H_

Expand All @@ -28,11 +31,12 @@ extern "C" {
// "Class" representing SD Cards
typedef struct {
const char *pcName;
spi_t *const spi;
spi_t *spi;
// Slave select is here in sd_card_t because multiple SDs can share an SPI
uint ss_gpio; // Slave select for this SD card
const uint card_detect_gpio; // Card detect
const uint card_detected_true; // Varies with card socket
uint card_detect_gpio; // Card detect
uint card_detected_true; // Varies with card socket
// Following fields are used to keep track of the state of the card:
int m_Status; // Card status
uint64_t sectors; // Assigned dynamically
int card_type; // Assigned dynamically
Expand Down
8 changes: 4 additions & 4 deletions FatFs_SPI/sd_driver/spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
typedef struct {
// SPI HW
spi_inst_t *hw_inst;
const uint miso_gpio; // SPI MISO GPIO number (not pin number)
const uint mosi_gpio;
const uint sck_gpio;
const uint baud_rate;
uint miso_gpio; // SPI MISO GPIO number (not pin number)
uint mosi_gpio;
uint sck_gpio;
uint baud_rate;
// State variables:
uint tx_dma;
uint rx_dma;
Expand Down
44 changes: 44 additions & 0 deletions dynamic_config_example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Generated Cmake Pico project file

cmake_minimum_required(VERSION 3.13)

set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)

# initalize pico_sdk from installed location
# (note this can come from environment, CMake cache etc)
#set(PICO_SDK_PATH "/home/carlk/pi/pico/pico-sdk")

# Pull in Raspberry Pi Pico SDK (must be before project)
include(pico_sdk_import.cmake)

project(dynamic_config_example C CXX ASM)

# Initialise the Raspberry Pi Pico SDK
pico_sdk_init()

add_subdirectory(../FatFs_SPI build)

# Add executable. Default name is the project name, version 0.1
add_executable(dynamic_config_example
dynamic_config_example.cpp
hw_config.cpp
)

pico_set_program_name(dynamic_config_example "dynamic_config_example")
pico_set_program_version(dynamic_config_example "0.1")

# Choose source and destination for standard input and output:
# See 4.1. Serial input and output on Raspberry Pi Pico in Getting started with Raspberry Pi Pico (https://datasheets.raspberrypi.org/pico/getting-started-with-pico.pdf)
# and 2.7.1. Standard Input/Output (stdio) Support in Raspberry Pi Pico C/C++ SDK (https://datasheets.raspberrypi.org/pico/raspberry-pi-pico-c-sdk.pdf):
pico_enable_stdio_uart(dynamic_config_example 1)
pico_enable_stdio_usb(dynamic_config_example 1)

# Add the standard library and FatFS/SPI to the build
target_link_libraries(dynamic_config_example
pico_stdlib
FatFs_SPI
)

pico_add_extra_outputs(dynamic_config_example)

105 changes: 105 additions & 0 deletions dynamic_config_example/dynamic_config_example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/* Instead of a statically linked hw_config.c,
create configuration dynamically */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//
#include "f_util.h"
#include "ff.h"
#include "pico/stdlib.h"
#include "rtc.h"
//
#include "hw_config.h"
//
#include "diskio.h" /* Declarations of disk functions */

void add_spi(spi_t *const spi);
void add_sd_card(sd_card_t *const sd_card);

static spi_t *p_spi;
void spi0_dma_isr() { spi_irq_handler(p_spi); }

void test(sd_card_t *pSD) {
// See FatFs - Generic FAT Filesystem Module, "Application Interface",
// http://elm-chan.org/fsw/ff/00index_e.html
FRESULT fr = f_mount(&pSD->fatfs, pSD->pcName, 1);
if (FR_OK != fr) panic("f_mount error: %s (%d)\n", FRESULT_str(fr), fr);
fr = f_chdrive(pSD->pcName);
if (FR_OK != fr) panic("f_chdrive error: %s (%d)\n", FRESULT_str(fr), fr);

FIL fil;
const char *const filename = "filename.txt";
fr = f_open(&fil, filename, FA_OPEN_APPEND | FA_WRITE);
if (FR_OK != fr && FR_EXIST != fr)
panic("f_open(%s) error: %s (%d)\n", filename, FRESULT_str(fr), fr);
if (f_printf(&fil, "Hello, world!\n") < 0) {
printf("f_printf failed\n");
}
fr = f_close(&fil);
if (FR_OK != fr) {
printf("f_close error: %s (%d)\n", FRESULT_str(fr), fr);
}

f_unmount(pSD->pcName);
}

int main() {
stdio_init_all();
time_init();

puts("Hello, world!");

// Hardware Configuration of SPI "object"
p_spi = (spi_t *)malloc(sizeof(spi_t));
if (!p_spi) panic("Out of memory");
p_spi->hw_inst = spi0; // SPI component
p_spi->miso_gpio = 16; // GPIO number (not pin number)
p_spi->mosi_gpio = 19;
p_spi->sck_gpio = 18;
p_spi->baud_rate = 12500 * 1000; // The limitation here is SPI slew rate.
p_spi->dma_isr = spi0_dma_isr;
p_spi->initialized = false; // initialized flag
add_spi(p_spi);

// Hardware Configuration of the SD Card "object"
sd_card_t *p_sd_card = (sd_card_t *)malloc(sizeof(sd_card_t));
if (!p_sd_card) panic("Out of memory");
memset(p_sd_card, 0, sizeof(sd_card_t));
p_sd_card->pcName = "0:"; // Name used to mount device
p_sd_card->spi = p_spi; // Pointer to the SPI driving this card
p_sd_card->ss_gpio = 17; // The SPI slave select GPIO for this SD card
p_sd_card->card_detect_gpio = 22; // Card detect
// What the GPIO read returns when a card is
// present. Use -1 if there is no card detect.
p_sd_card->card_detected_true = 1;
// State attributes:
p_sd_card->m_Status = STA_NOINIT;
p_sd_card->sectors = 0;
p_sd_card->card_type = 0;
add_sd_card(p_sd_card);

/* Add another SD card */
p_sd_card = (sd_card_t *)malloc(sizeof(sd_card_t));
if (!p_sd_card) panic("Out of memory");
memset(p_sd_card, 0, sizeof(sd_card_t));
p_sd_card->pcName = "1:"; // Name used to mount device
p_sd_card->spi = p_spi; // Pointer to the SPI driving this card
p_sd_card->ss_gpio = 15; // The SPI slave select GPIO for this SD card
p_sd_card->card_detect_gpio = 14; // Card detect
// What the GPIO read returns when a card is
// present. Use -1 if there is no card detect.
p_sd_card->card_detected_true = 1;
// State attributes:
p_sd_card->m_Status = STA_NOINIT;
p_sd_card->sectors = 0;
p_sd_card->card_type = 0;
add_sd_card(p_sd_card);

for (size_t i = 0; i < sd_get_num(); ++i)
test(sd_get_by_num(i));

puts("Goodbye, world!");

for (;;);
}
50 changes: 50 additions & 0 deletions dynamic_config_example/hw_config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*

*/

#include <string.h>
//
#include "my_debug.h"
//
#include "hw_config.h"
//
#include "ff.h" /* Obtains integer types */
//
#include "diskio.h" /* Declarations of disk functions */
#include "vector"

static std::vector<spi_t *> spis;
static std::vector<sd_card_t *> sd_cards;

size_t sd_get_num() { return sd_cards.size(); }
sd_card_t *sd_get_by_num(size_t num) {
if (num <= sd_get_num()) {
return sd_cards[num];
} else {
return NULL;
}
}
sd_card_t *sd_get_by_name(const char *name) {
for (size_t i = 0; i < sd_get_num(); ++i)
if (0 == strcmp(sd_cards[i]->pcName, name)) return sd_cards[i];
DBG_PRINTF("%s: unknown name %s\n", __func__, name);
return NULL;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly redundant? Perhaps sd_get_by_name should be removed from hw_config.h?

size_t spi_get_num() { return spis.size(); }
spi_t *spi_get_by_num(size_t num) {
if (num <= sd_get_num()) {
return spis[num];
} else {
return NULL;
}
}
FATFS *get_fs_by_name(const char *name) {
for (size_t i = 0; i < sd_get_num(); ++i)
if (0 == strcmp(sd_cards[i]->pcName, name)) return &sd_cards[i]->fatfs;
DBG_PRINTF("%s: unknown name %s\n", __func__, name);
return NULL;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly redundant? Perhaps get_fs_by_name should be removed from hw_config.h?

void add_spi(spi_t *spi) { spis.push_back(spi); }
void add_sd_card(sd_card_t *sd_card) { sd_cards.push_back(sd_card); }

/* [] END OF FILE */
62 changes: 62 additions & 0 deletions dynamic_config_example/pico_sdk_import.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# This is a copy of <PICO_SDK_PATH>/external/pico_sdk_import.cmake

# This can be dropped into an external project to help locate this SDK
# It should be include()ed prior to project()

if (DEFINED ENV{PICO_SDK_PATH} AND (NOT PICO_SDK_PATH))
set(PICO_SDK_PATH $ENV{PICO_SDK_PATH})
message("Using PICO_SDK_PATH from environment ('${PICO_SDK_PATH}')")
endif ()

if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT} AND (NOT PICO_SDK_FETCH_FROM_GIT))
set(PICO_SDK_FETCH_FROM_GIT $ENV{PICO_SDK_FETCH_FROM_GIT})
message("Using PICO_SDK_FETCH_FROM_GIT from environment ('${PICO_SDK_FETCH_FROM_GIT}')")
endif ()

if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_PATH} AND (NOT PICO_SDK_FETCH_FROM_GIT_PATH))
set(PICO_SDK_FETCH_FROM_GIT_PATH $ENV{PICO_SDK_FETCH_FROM_GIT_PATH})
message("Using PICO_SDK_FETCH_FROM_GIT_PATH from environment ('${PICO_SDK_FETCH_FROM_GIT_PATH}')")
endif ()

set(PICO_SDK_PATH "${PICO_SDK_PATH}" CACHE PATH "Path to the Raspberry Pi Pico SDK")
set(PICO_SDK_FETCH_FROM_GIT "${PICO_SDK_FETCH_FROM_GIT}" CACHE BOOL "Set to ON to fetch copy of SDK from git if not otherwise locatable")
set(PICO_SDK_FETCH_FROM_GIT_PATH "${PICO_SDK_FETCH_FROM_GIT_PATH}" CACHE FILEPATH "location to download SDK")

if (NOT PICO_SDK_PATH)
if (PICO_SDK_FETCH_FROM_GIT)
include(FetchContent)
set(FETCHCONTENT_BASE_DIR_SAVE ${FETCHCONTENT_BASE_DIR})
if (PICO_SDK_FETCH_FROM_GIT_PATH)
get_filename_component(FETCHCONTENT_BASE_DIR "${PICO_SDK_FETCH_FROM_GIT_PATH}" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}")
endif ()
FetchContent_Declare(
pico_sdk
GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
GIT_TAG master
)
if (NOT pico_sdk)
message("Downloading Raspberry Pi Pico SDK")
FetchContent_Populate(pico_sdk)
set(PICO_SDK_PATH ${pico_sdk_SOURCE_DIR})
endif ()
set(FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR_SAVE})
else ()
message(FATAL_ERROR
"SDK location was not specified. Please set PICO_SDK_PATH or set PICO_SDK_FETCH_FROM_GIT to on to fetch from git."
)
endif ()
endif ()

get_filename_component(PICO_SDK_PATH "${PICO_SDK_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}")
if (NOT EXISTS ${PICO_SDK_PATH})
message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' not found")
endif ()

set(PICO_SDK_INIT_CMAKE_FILE ${PICO_SDK_PATH}/pico_sdk_init.cmake)
if (NOT EXISTS ${PICO_SDK_INIT_CMAKE_FILE})
message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' does not appear to contain the Raspberry Pi Pico SDK")
endif ()

set(PICO_SDK_PATH ${PICO_SDK_PATH} CACHE PATH "Path to the Raspberry Pi Pico SDK" FORCE)

include(${PICO_SDK_INIT_CMAKE_FILE})
7 changes: 2 additions & 5 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ add_subdirectory(../FatFs_SPI build)
# Add executable. Default name is the project name, version 0.1
add_executable(FatFS_SPI_example
FatFS_SPI_example.cpp
hw_config.c
data_log_demo.c
tests/simple.c
tests/app4-IO_module_function_checker.c
Expand All @@ -27,18 +28,14 @@ add_executable(FatFS_SPI_example
target_link_libraries(FatFS_SPI_example pico_stdlib)

target_compile_options(FatFS_SPI_example PUBLIC -Wall -Wextra -Wno-unused-function -Wno-unused-parameter)
IF (NOT DEFINED N_SD_CARDS)
SET(N_SD_CARDS 1)
ENDIF()
target_compile_definitions(FatFS_SPI_example PUBLIC DEBUG N_SD_CARDS=${N_SD_CARDS})

pico_set_program_name(FatFS_SPI_example "FatFS_SPI_example")
pico_set_program_version(FatFS_SPI_example "0.1")

# See 4.1. Serial input and output on Raspberry Pi Pico in Getting started with Raspberry Pi Pico (https://datasheets.raspberrypi.org/pico/getting-started-with-pico.pdf)
# and 2.7.1. Standard Input/Output (stdio) Support in Raspberry Pi Pico C/C++ SDK (https://datasheets.raspberrypi.org/pico/raspberry-pi-pico-c-sdk.pdf):
pico_enable_stdio_uart(FatFS_SPI_example 1)
pico_enable_stdio_usb(FatFS_SPI_example 0)
pico_enable_stdio_usb(FatFS_SPI_example 1)

target_link_libraries(FatFS_SPI_example
FatFs_SPI
Expand Down
Loading