-
Notifications
You must be signed in to change notification settings - Fork 47
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
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2260521
Move hw_config.c out of library
carlk3 9c76d0e
Dynamic configuration
carlk3 118fce2
Editing
carlk3 dd21920
Enhance example to show configuring two SD cards
carlk3 7193543
Cleanup
carlk3 3d0f454
Move sd_get_by_name and get_fs_by_name out of hw_config
carlk3 dd0b085
Clean up #includes
carlk3 330098e
Updated README
carlk3 1c586ec
Updated README
carlk3 5bae2c9
Unconditionally print "No SD card detected!" error message
carlk3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 (;;); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
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; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?