├── .gitignore ├── FatFs_SPI ├── include │ ├── rtc.h │ ├── f_util.h │ ├── my_debug.h │ ├── util.h │ └── ff_stdio.h ├── ff15 │ ├── source │ │ ├── 00readme.txt │ │ ├── diskio.h │ │ ├── diskio.c │ │ ├── ffsystem.c │ │ └── ffconf.h │ ├── documents │ │ ├── res │ │ │ ├── app1.c │ │ │ ├── app5.c │ │ │ ├── app6.c │ │ │ ├── app2.c │ │ │ ├── app3.c │ │ │ └── app4.c │ │ └── css_e.css │ └── LICENSE.txt ├── sd_driver │ ├── hw_config.h │ ├── crc.h │ ├── sd_spi.h │ ├── spi.h │ ├── sd_card.h │ ├── sd_spi.c │ ├── demo_logging.c │ ├── crc.c │ └── spi.c ├── CMakeLists.txt └── src │ ├── my_debug.c │ ├── f_util.c │ ├── rtc.c │ └── glue.c ├── dynamic_config_example ├── hw_config.cpp ├── CMakeLists.txt ├── pico_sdk_import.cmake ├── thing_plus.dynamic_config_example.cpp └── dynamic_config_example.cpp ├── simple_example ├── simple_example.cpp ├── CMakeLists.txt ├── pico_sdk_import.cmake ├── hw_config.c └── thing_plus.hw_config.c ├── example ├── CMakeLists.txt ├── maker_pi_pico.hw_config.c ├── pico_sdk_import.cmake ├── thing_plus.hw_config.c ├── hw_config.c ├── exp_mod_A.hw_config.c ├── dev_brd.hw_config.c ├── data_log_demo.c └── tests │ ├── simple.c │ ├── big_file_test.c │ └── app4-IO_module_function_checker.c └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | **/build 2 | *.html 3 | *.png 4 | *.zip 5 | *.xlsx 6 | *.bak 7 | **/.vscode 8 | -------------------------------------------------------------------------------- /FatFs_SPI/include/rtc.h: -------------------------------------------------------------------------------- 1 | /* rtc.h 2 | Copyright 2021 Carl John Kugler III 3 | 4 | Licensed under the Apache License, Version 2.0 (the License); you may not use 5 | this file except in compliance with the License. You may obtain a copy of the 6 | License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | Unless required by applicable law or agreed to in writing, software distributed 10 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 12 | specific language governing permissions and limitations under the License. 13 | */ 14 | #pragma once 15 | 16 | #ifdef __cplusplus 17 | extern "C" { 18 | #endif 19 | 20 | void time_init(); 21 | 22 | #ifdef __cplusplus 23 | } 24 | #endif 25 | -------------------------------------------------------------------------------- /dynamic_config_example/hw_config.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "sd_card.h" 3 | 4 | #include "hw_config.h" 5 | 6 | static std::vector spis; 7 | static std::vector sd_cards; 8 | 9 | size_t sd_get_num() { return sd_cards.size(); } 10 | sd_card_t *sd_get_by_num(size_t num) { 11 | if (num <= sd_get_num()) { 12 | return sd_cards[num]; 13 | } else { 14 | return NULL; 15 | } 16 | } 17 | 18 | size_t spi_get_num() { return spis.size(); } 19 | spi_t *spi_get_by_num(size_t num) { 20 | if (num <= sd_get_num()) { 21 | return spis[num]; 22 | } else { 23 | return NULL; 24 | } 25 | } 26 | void add_spi(spi_t *spi) { spis.push_back(spi); } 27 | void add_sd_card(sd_card_t *sd_card) { sd_cards.push_back(sd_card); } 28 | 29 | /* [] END OF FILE */ 30 | -------------------------------------------------------------------------------- /FatFs_SPI/ff15/source/00readme.txt: -------------------------------------------------------------------------------- 1 | FatFs Module Source Files R0.15 2 | 3 | 4 | FILES 5 | 6 | 00readme.txt This file. 7 | 00history.txt Revision history. 8 | ff.c FatFs module. 9 | ffconf.h Configuration file of FatFs module. 10 | ff.h Common include file for FatFs and application module. 11 | diskio.h Common include file for FatFs and disk I/O module. 12 | diskio.c An example of glue function to attach existing disk I/O module to FatFs. 13 | ffunicode.c Optional Unicode utility functions. 14 | ffsystem.c An example of optional O/S related functions. 15 | 16 | 17 | Low level disk I/O module is not included in this archive because the FatFs 18 | module is only a generic file system layer and it does not depend on any specific 19 | storage device. You need to provide a low level disk I/O module written to 20 | control the storage device that attached to the target system. 21 | 22 | -------------------------------------------------------------------------------- /FatFs_SPI/sd_driver/hw_config.h: -------------------------------------------------------------------------------- 1 | /* hw_config.h 2 | Copyright 2021 Carl John Kugler III 3 | 4 | Licensed under the Apache License, Version 2.0 (the License); you may not use 5 | this file except in compliance with the License. You may obtain a copy of the 6 | License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | Unless required by applicable law or agreed to in writing, software distributed 10 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 12 | specific language governing permissions and limitations under the License. 13 | */ 14 | #pragma once 15 | 16 | #include "ff.h" 17 | #include "sd_card.h" 18 | 19 | #ifdef __cplusplus 20 | extern "C" { 21 | #endif 22 | 23 | size_t sd_get_num(); 24 | sd_card_t *sd_get_by_num(size_t num); 25 | 26 | size_t spi_get_num(); 27 | spi_t *spi_get_by_num(size_t num); 28 | 29 | #ifdef __cplusplus 30 | } 31 | #endif 32 | 33 | /* [] END OF FILE */ 34 | -------------------------------------------------------------------------------- /FatFs_SPI/include/f_util.h: -------------------------------------------------------------------------------- 1 | /* f_util.h 2 | Copyright 2021 Carl John Kugler III 3 | 4 | Licensed under the Apache License, Version 2.0 (the License); you may not use 5 | this file except in compliance with the License. You may obtain a copy of the 6 | License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | Unless required by applicable law or agreed to in writing, software distributed 10 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 12 | specific language governing permissions and limitations under the License. 13 | */ 14 | #pragma once 15 | #include "ff.h" 16 | 17 | #ifdef __cplusplus 18 | extern "C" { 19 | #endif 20 | 21 | const char *FRESULT_str(FRESULT i); 22 | FRESULT delete_node ( 23 | TCHAR* path, /* Path name buffer with the sub-directory to delete */ 24 | UINT sz_buff, /* Size of path name buffer (items) */ 25 | FILINFO* fno /* Name read buffer */ 26 | ); 27 | 28 | #ifdef __cplusplus 29 | } 30 | #endif 31 | -------------------------------------------------------------------------------- /FatFs_SPI/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_library(FatFs_SPI INTERFACE) 2 | target_sources(FatFs_SPI INTERFACE 3 | ${CMAKE_CURRENT_LIST_DIR}/ff15/source/ffsystem.c 4 | ${CMAKE_CURRENT_LIST_DIR}/ff15/source/ffunicode.c 5 | ${CMAKE_CURRENT_LIST_DIR}/ff15/source/ff.c 6 | ${CMAKE_CURRENT_LIST_DIR}/sd_driver/sd_spi.c 7 | ${CMAKE_CURRENT_LIST_DIR}/sd_driver/demo_logging.c 8 | # ${CMAKE_CURRENT_LIST_DIR}/sd_driver/hw_config.c 9 | ${CMAKE_CURRENT_LIST_DIR}/sd_driver/spi.c 10 | ${CMAKE_CURRENT_LIST_DIR}/sd_driver/sd_card.c 11 | ${CMAKE_CURRENT_LIST_DIR}/sd_driver/crc.c 12 | ${CMAKE_CURRENT_LIST_DIR}/src/glue.c 13 | ${CMAKE_CURRENT_LIST_DIR}/src/f_util.c 14 | ${CMAKE_CURRENT_LIST_DIR}/src/ff_stdio.c 15 | ${CMAKE_CURRENT_LIST_DIR}/src/my_debug.c 16 | ${CMAKE_CURRENT_LIST_DIR}/src/rtc.c 17 | ) 18 | target_include_directories(FatFs_SPI INTERFACE 19 | ff15/source 20 | sd_driver 21 | include 22 | ) 23 | target_link_libraries(FatFs_SPI INTERFACE 24 | hardware_spi 25 | hardware_dma 26 | hardware_rtc 27 | pico_stdlib 28 | ) 29 | -------------------------------------------------------------------------------- /simple_example/simple_example.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | // 3 | #include "f_util.h" 4 | #include "ff.h" 5 | #include "pico/stdlib.h" 6 | #include "rtc.h" 7 | // 8 | #include "hw_config.h" 9 | 10 | int main() { 11 | stdio_init_all(); 12 | time_init(); 13 | 14 | puts("Hello, world!"); 15 | 16 | // See FatFs - Generic FAT Filesystem Module, "Application Interface", 17 | // http://elm-chan.org/fsw/ff/00index_e.html 18 | sd_card_t *pSD = sd_get_by_num(0); 19 | FRESULT fr = f_mount(&pSD->fatfs, pSD->pcName, 1); 20 | if (FR_OK != fr) panic("f_mount error: %s (%d)\n", FRESULT_str(fr), fr); 21 | FIL fil; 22 | const char* const filename = "filename.txt"; 23 | fr = f_open(&fil, filename, FA_OPEN_APPEND | FA_WRITE); 24 | if (FR_OK != fr && FR_EXIST != fr) 25 | panic("f_open(%s) error: %s (%d)\n", filename, FRESULT_str(fr), fr); 26 | if (f_printf(&fil, "Hello, world!\n") < 0) { 27 | printf("f_printf failed\n"); 28 | } 29 | fr = f_close(&fil); 30 | if (FR_OK != fr) { 31 | printf("f_close error: %s (%d)\n", FRESULT_str(fr), fr); 32 | } 33 | f_unmount(pSD->pcName); 34 | 35 | puts("Goodbye, world!"); 36 | for (;;); 37 | } 38 | -------------------------------------------------------------------------------- /FatFs_SPI/ff15/documents/res/app1.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------/ 2 | / Open or create a file in append mode 3 | / (This function was sperseded by FA_OPEN_APPEND flag at FatFs R0.12a) 4 | /------------------------------------------------------------*/ 5 | 6 | FRESULT open_append ( 7 | FIL* fp, /* [OUT] File object to create */ 8 | const char* path /* [IN] File name to be opened */ 9 | ) 10 | { 11 | FRESULT fr; 12 | 13 | /* Opens an existing file. If not exist, creates a new file. */ 14 | fr = f_open(fp, path, FA_WRITE | FA_OPEN_ALWAYS); 15 | if (fr == FR_OK) { 16 | /* Seek to end of the file to append data */ 17 | fr = f_lseek(fp, f_size(fp)); 18 | if (fr != FR_OK) 19 | f_close(fp); 20 | } 21 | return fr; 22 | } 23 | 24 | 25 | int main (void) 26 | { 27 | FRESULT fr; 28 | FATFS fs; 29 | FIL fil; 30 | 31 | /* Open or create a log file and ready to append */ 32 | f_mount(&fs, "", 0); 33 | fr = open_append(&fil, "logfile.txt"); 34 | if (fr != FR_OK) return 1; 35 | 36 | /* Append a line */ 37 | f_printf(&fil, "%02u/%02u/%u, %2u:%02u\n", Mday, Mon, Year, Hour, Min); 38 | 39 | /* Close the file */ 40 | f_close(&fil); 41 | 42 | return 0; 43 | } 44 | 45 | -------------------------------------------------------------------------------- /FatFs_SPI/include/my_debug.h: -------------------------------------------------------------------------------- 1 | /* my_debug.h 2 | Copyright 2021 Carl John Kugler III 3 | 4 | Licensed under the Apache License, Version 2.0 (the License); you may not use 5 | this file except in compliance with the License. You may obtain a copy of the 6 | License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | Unless required by applicable law or agreed to in writing, software distributed 10 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 12 | specific language governing permissions and limitations under the License. 13 | */ 14 | #pragma once 15 | 16 | #include 17 | 18 | #ifdef __cplusplus 19 | extern "C" { 20 | #endif 21 | 22 | void my_printf(const char *pcFormat, ...) __attribute__((format(__printf__, 1, 2))); 23 | 24 | void my_assert_func(const char *file, int line, const char *func, 25 | const char *pred); 26 | 27 | #ifdef __cplusplus 28 | } 29 | #endif 30 | 31 | 32 | #ifdef NDEBUG /* required by ANSI standard */ 33 | # define DBG_PRINTF(fmt, args...) {} /* Don't do anything in release builds*/ 34 | #else 35 | # define DBG_PRINTF my_printf 36 | #endif 37 | 38 | #ifdef NDEBUG /* required by ANSI standard */ 39 | # define myASSERT(__e) ((void)0) 40 | #else 41 | # define myASSERT(__e) \ 42 | ((__e) ? (void)0 : my_assert_func(__FILE__, __LINE__, __func__, #__e)) 43 | #endif -------------------------------------------------------------------------------- /FatFs_SPI/src/my_debug.c: -------------------------------------------------------------------------------- 1 | /* my_debug.c 2 | Copyright 2021 Carl John Kugler III 3 | 4 | Licensed under the Apache License, Version 2.0 (the License); you may not use 5 | this file except in compliance with the License. You may obtain a copy of the 6 | License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | Unless required by applicable law or agreed to in writing, software distributed 10 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 12 | specific language governing permissions and limitations under the License. 13 | */ 14 | #include 15 | #include 16 | #include "my_debug.h" 17 | 18 | void my_printf(const char *pcFormat, ...) { 19 | char pcBuffer[256] = {0}; 20 | va_list xArgs; 21 | va_start(xArgs, pcFormat); 22 | vsnprintf(pcBuffer, sizeof(pcBuffer), pcFormat, xArgs); 23 | va_end(xArgs); 24 | printf("%s", pcBuffer); 25 | fflush(stdout); 26 | } 27 | 28 | 29 | void my_assert_func(const char *file, int line, const char *func, 30 | const char *pred) { 31 | printf("assertion \"%s\" failed: file \"%s\", line %d, function: %s\n", 32 | pred, file, line, func); 33 | fflush(stdout); 34 | __asm volatile("cpsid i" : : : "memory"); /* Disable global interrupts. */ 35 | while (1) { 36 | __asm("bkpt #0"); 37 | }; // Stop in GUI as if at a breakpoint (if debugging, otherwise loop 38 | // forever) 39 | } 40 | -------------------------------------------------------------------------------- /simple_example/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Generated Cmake Pico project file 2 | 3 | cmake_minimum_required(VERSION 3.13) 4 | 5 | set(CMAKE_C_STANDARD 11) 6 | set(CMAKE_CXX_STANDARD 17) 7 | 8 | # initalize pico_sdk from installed location 9 | # (note this can come from environment, CMake cache etc) 10 | #set(PICO_SDK_PATH "/home/carlk/pi/pico/pico-sdk") 11 | 12 | # Pull in Raspberry Pi Pico SDK (must be before project) 13 | include(pico_sdk_import.cmake) 14 | 15 | project(simple_example C CXX ASM) 16 | 17 | # Initialise the Raspberry Pi Pico SDK 18 | pico_sdk_init() 19 | 20 | add_subdirectory(../FatFs_SPI build) 21 | 22 | # Add executable. Default name is the project name, version 0.1 23 | add_executable(simple_example 24 | simple_example.cpp 25 | hw_config.c 26 | ) 27 | 28 | # Add the standard library and FatFS/SPI to the build 29 | target_link_libraries(simple_example 30 | pico_stdlib 31 | FatFs_SPI 32 | ) 33 | 34 | pico_set_program_name(simple_example "simple_example") 35 | pico_set_program_version(simple_example "0.1") 36 | 37 | # Choose source and destination for standard input and output: 38 | # 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) 39 | # 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): 40 | pico_enable_stdio_uart(simple_example 1) 41 | pico_enable_stdio_usb(simple_example 1) 42 | 43 | pico_add_extra_outputs(simple_example) 44 | 45 | -------------------------------------------------------------------------------- /FatFs_SPI/ff15/documents/res/app5.c: -------------------------------------------------------------------------------- 1 | /*----------------------------------------------------------------------/ 2 | / Test if the file is contiguous / 3 | /----------------------------------------------------------------------*/ 4 | 5 | FRESULT test_contiguous_file ( 6 | FIL* fp, /* [IN] Open file object to be checked */ 7 | int* cont /* [OUT] 1:Contiguous, 0:Fragmented or zero-length */ 8 | ) 9 | { 10 | DWORD clst, clsz, step; 11 | FSIZE_t fsz; 12 | FRESULT fr; 13 | 14 | 15 | *cont = 0; 16 | fr = f_rewind(fp); /* Validates and prepares the file */ 17 | if (fr != FR_OK) return fr; 18 | 19 | #if FF_MAX_SS == FF_MIN_SS 20 | clsz = (DWORD)fp->obj.fs->csize * FF_MAX_SS; /* Cluster size */ 21 | #else 22 | clsz = (DWORD)fp->obj.fs->csize * fp->obj.fs->ssize; 23 | #endif 24 | fsz = f_size(fp); 25 | if (fsz > 0) { 26 | clst = fp->obj.sclust - 1; /* A cluster leading the first cluster for first test */ 27 | while (fsz) { 28 | step = (fsz >= clsz) ? clsz : (DWORD)fsz; 29 | fr = f_lseek(fp, f_tell(fp) + step); /* Advances file pointer a cluster */ 30 | if (fr != FR_OK) return fr; 31 | if (clst + 1 != fp->clust) break; /* Is not the cluster next to previous one? */ 32 | clst = fp->clust; fsz -= step; /* Get current cluster for next test */ 33 | } 34 | if (fsz == 0) *cont = 1; /* All done without fail? */ 35 | } 36 | 37 | return FR_OK; 38 | } 39 | -------------------------------------------------------------------------------- /FatFs_SPI/sd_driver/crc.h: -------------------------------------------------------------------------------- 1 | /* crc.h 2 | Copyright 2021 Carl John Kugler III 3 | 4 | Licensed under the Apache License, Version 2.0 (the License); you may not use 5 | this file except in compliance with the License. You may obtain a copy of the 6 | License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | Unless required by applicable law or agreed to in writing, software distributed 10 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 12 | specific language governing permissions and limitations under the License. 13 | */ 14 | /* Derived from: 15 | * SD/MMC File System Library 16 | * Copyright (c) 2016 Neil Thiessen 17 | * 18 | * Licensed under the Apache License, Version 2.0 (the "License"); 19 | * you may not use this file except in compliance with the License. 20 | * You may obtain a copy of the License at 21 | * 22 | * http://www.apache.org/licenses/LICENSE-2.0 23 | * 24 | * Unless required by applicable law or agreed to in writing, software 25 | * distributed under the License is distributed on an "AS IS" BASIS, 26 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 27 | * See the License for the specific language governing permissions and 28 | * limitations under the License. 29 | */ 30 | 31 | #ifndef SD_CRC_H 32 | #define SD_CRC_H 33 | 34 | #include 35 | 36 | char crc7(const char* data, int length); 37 | unsigned short crc16(const char* data, int length); 38 | void update_crc16(unsigned short *pCrc16, const char data[], size_t length); 39 | 40 | #endif 41 | 42 | /* [] END OF FILE */ 43 | -------------------------------------------------------------------------------- /dynamic_config_example/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Generated Cmake Pico project file 2 | 3 | cmake_minimum_required(VERSION 3.13) 4 | 5 | set(CMAKE_C_STANDARD 11) 6 | set(CMAKE_CXX_STANDARD 17) 7 | 8 | # initalize pico_sdk from installed location 9 | # (note this can come from environment, CMake cache etc) 10 | #set(PICO_SDK_PATH "/home/carlk/pi/pico/pico-sdk") 11 | 12 | # Pull in Raspberry Pi Pico SDK (must be before project) 13 | include(pico_sdk_import.cmake) 14 | 15 | project(dynamic_config_example C CXX ASM) 16 | 17 | # Initialise the Raspberry Pi Pico SDK 18 | pico_sdk_init() 19 | 20 | add_subdirectory(../FatFs_SPI build) 21 | 22 | # Add executable. Default name is the project name, version 0.1 23 | add_executable(dynamic_config_example 24 | dynamic_config_example.cpp 25 | hw_config.cpp 26 | ) 27 | 28 | pico_set_program_name(dynamic_config_example "dynamic_config_example") 29 | pico_set_program_version(dynamic_config_example "0.1") 30 | 31 | # Choose source and destination for standard input and output: 32 | # 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) 33 | # 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): 34 | pico_enable_stdio_uart(dynamic_config_example 1) 35 | pico_enable_stdio_usb(dynamic_config_example 1) 36 | 37 | # Add the standard library and FatFS/SPI to the build 38 | target_link_libraries(dynamic_config_example 39 | pico_stdlib 40 | FatFs_SPI 41 | ) 42 | 43 | # add_compile_definitions(CARD2) 44 | 45 | pico_add_extra_outputs(dynamic_config_example) 46 | 47 | -------------------------------------------------------------------------------- /FatFs_SPI/sd_driver/sd_spi.h: -------------------------------------------------------------------------------- 1 | /* sd_spi.h 2 | Copyright 2021 Carl John Kugler III 3 | 4 | Licensed under the Apache License, Version 2.0 (the License); you may not use 5 | this file except in compliance with the License. You may obtain a copy of the 6 | License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | Unless required by applicable law or agreed to in writing, software distributed 10 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 12 | specific language governing permissions and limitations under the License. 13 | */ 14 | 15 | #ifndef _SD_SPI_H_ 16 | #define _SD_SPI_H_ 17 | 18 | #include 19 | #include "sd_card.h" 20 | 21 | /* Transfer tx to SPI while receiving SPI to rx. 22 | tx or rx can be NULL if not important. */ 23 | bool sd_spi_transfer(sd_card_t *pSD, const uint8_t *tx, uint8_t *rx, size_t length); 24 | uint8_t sd_spi_write(sd_card_t *pSD, const uint8_t value); 25 | void sd_spi_deselect_pulse(sd_card_t *pSD); 26 | void sd_spi_acquire(sd_card_t *pSD); 27 | void sd_spi_release(sd_card_t *pSD); 28 | void sd_spi_go_low_frequency(sd_card_t *this); 29 | void sd_spi_go_high_frequency(sd_card_t *this); 30 | 31 | /* 32 | After power up, the host starts the clock and sends the initializing sequence on the CMD line. 33 | This sequence is a contiguous stream of logical ‘1’s. The sequence length is the maximum of 1msec, 34 | 74 clocks or the supply-ramp-uptime; the additional 10 clocks 35 | (over the 64 clocks after what the card should be ready for communication) is 36 | provided to eliminate power-up synchronization problems. 37 | */ 38 | void sd_spi_send_initializing_sequence(sd_card_t * pSD); 39 | 40 | #endif 41 | 42 | /* [] END OF FILE */ 43 | -------------------------------------------------------------------------------- /example/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.13) 2 | 3 | # Pull in Pico SDK (must be before project) 4 | include(pico_sdk_import.cmake) 5 | 6 | project(FatFS_SPI_example C CXX ASM) 7 | 8 | set(CMAKE_C_STANDARD 11) 9 | set(CMAKE_CXX_STANDARD 17) 10 | 11 | # Initialise the Pico SDK 12 | pico_sdk_init() 13 | 14 | add_subdirectory(../FatFs_SPI build) 15 | 16 | # Add executable. Default name is the project name, version 0.1 17 | add_executable(FatFS_SPI_example 18 | FatFS_SPI_example.cpp 19 | hw_config.c 20 | data_log_demo.c 21 | tests/simple.c 22 | tests/app4-IO_module_function_checker.c 23 | tests/big_file_test.c 24 | tests/CreateAndVerifyExampleFiles.c 25 | tests/ff_stdio_tests_with_cwd.c 26 | ) 27 | # Add the standard library to the build 28 | target_link_libraries(FatFS_SPI_example pico_stdlib) 29 | 30 | target_compile_options(FatFS_SPI_example PUBLIC -Wall -Wextra -Wno-unused-function -Wno-unused-parameter) 31 | 32 | # Use Pico's LED to show drive activity. 33 | # Ensure that PICO_DEFAULT_LED_PIN is set correctly. 34 | # Note that Pico W uses GPIO 25 for SPI communication to the CYW43439. 35 | # add_compile_definitions(USE_LED=1) 36 | 37 | pico_set_program_name(FatFS_SPI_example "FatFS_SPI_example") 38 | pico_set_program_version(FatFS_SPI_example "0.1") 39 | 40 | # 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) 41 | # 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): 42 | pico_enable_stdio_uart(FatFS_SPI_example 1) 43 | pico_enable_stdio_usb(FatFS_SPI_example 1) 44 | 45 | target_link_libraries(FatFS_SPI_example 46 | FatFs_SPI 47 | hardware_clocks 48 | hardware_adc 49 | ) 50 | 51 | pico_add_extra_outputs(FatFS_SPI_example) 52 | 53 | -------------------------------------------------------------------------------- /FatFs_SPI/ff15/LICENSE.txt: -------------------------------------------------------------------------------- 1 | FatFs License 2 | 3 | FatFs has being developped as a personal project of the author, ChaN. It is free from the code anyone else wrote at current release. Following code block shows a copy of the FatFs license document that heading the source files. 4 | 5 | /*----------------------------------------------------------------------------/ 6 | / FatFs - Generic FAT Filesystem Module Rx.xx / 7 | /-----------------------------------------------------------------------------/ 8 | / 9 | / Copyright (C) 20xx, ChaN, all right reserved. 10 | / 11 | / FatFs module is an open source software. Redistribution and use of FatFs in 12 | / source and binary forms, with or without modification, are permitted provided 13 | / that the following condition is met: 14 | / 15 | / 1. Redistributions of source code must retain the above copyright notice, 16 | / this condition and the following disclaimer. 17 | / 18 | / This software is provided by the copyright holder and contributors "AS IS" 19 | / and any warranties related to this software are DISCLAIMED. 20 | / The copyright owner or contributors be NOT LIABLE for any damages caused 21 | / by use of this software. 22 | /----------------------------------------------------------------------------*/ 23 | 24 | Therefore FatFs license is one of the BSD-style licenses, but there is a significant feature. FatFs is mainly intended for embedded systems. In order to extend the usability for commercial products, the redistributions of FatFs in binary form, such as embedded code, binary library and any forms without source code, do not need to include about FatFs in the documentations. This is equivalent to the 1-clause BSD license. Of course FatFs is compatible with the most of open source software licenses include GNU GPL. When you redistribute the FatFs source code with changes or create a fork, the license can also be changed to GNU GPL, BSD-style license or any open source software license that not conflict with FatFs license. 25 | -------------------------------------------------------------------------------- /FatFs_SPI/ff15/documents/res/app6.c: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------*/ 2 | /* Raw Read/Write Throughput Checker */ 3 | /*---------------------------------------------------------------------*/ 4 | 5 | #include 6 | #include 7 | #include "diskio.h" 8 | #include "ff.h" 9 | 10 | 11 | int test_raw_speed ( 12 | BYTE pdrv, /* Physical drive number */ 13 | DWORD lba, /* Start LBA for read/write test */ 14 | DWORD len, /* Number of bytes to read/write (must be multiple of sz_buff) */ 15 | void* buff, /* Read/write buffer */ 16 | UINT sz_buff /* Size of read/write buffer (must be multiple of FF_MAX_SS) */ 17 | ) 18 | { 19 | WORD ss; 20 | DWORD ofs, tmr; 21 | 22 | 23 | #if FF_MIN_SS != FF_MAX_SS 24 | if (disk_ioctl(pdrv, GET_SECTOR_SIZE, &ss) != RES_OK) { 25 | printf("\ndisk_ioctl() failed.\n"); 26 | return 0; 27 | } 28 | #else 29 | ss = FF_MAX_SS; 30 | #endif 31 | 32 | printf("Starting raw write test at sector %lu in %u bytes of data chunks...", lba, sz_buff); 33 | tmr = systimer(); 34 | for (ofs = 0; ofs < len / ss; ofs += sz_buff / ss) { 35 | if (disk_write(pdrv, buff, lba + ofs, sz_buff / ss) != RES_OK) { 36 | printf("\ndisk_write() failed.\n"); 37 | return 0; 38 | } 39 | } 40 | if (disk_ioctl(pdrv, CTRL_SYNC, 0) != RES_OK) { 41 | printf("\ndisk_ioctl() failed.\n"); 42 | return 0; 43 | } 44 | tmr = systimer() - tmr; 45 | printf("\n%lu bytes written and it took %lu timer ticks.\n", len, tmr); 46 | 47 | printf("Starting raw read test at sector %lu in %u bytes of data chunks...", lba, sz_buff); 48 | tmr = systimer(); 49 | for (ofs = 0; ofs < len / ss; ofs += sz_buff / ss) { 50 | if (disk_read(pdrv, buff, lba + ofs, sz_buff / ss) != RES_OK) { 51 | printf("\ndisk_read() failed.\n"); 52 | return 0; 53 | } 54 | } 55 | tmr = systimer() - tmr; 56 | printf("\n%lu bytes read and it took %lu timer ticks.\n", len, tmr); 57 | 58 | printf("Test completed.\n"); 59 | return 1; 60 | } 61 | 62 | -------------------------------------------------------------------------------- /FatFs_SPI/include/util.h: -------------------------------------------------------------------------------- 1 | /* util.h 2 | Copyright 2021 Carl John Kugler III 3 | 4 | Licensed under the Apache License, Version 2.0 (the License); you may not use 5 | this file except in compliance with the License. You may obtain a copy of the 6 | License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | Unless required by applicable law or agreed to in writing, software distributed 10 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 12 | specific language governing permissions and limitations under the License. 13 | */ 14 | #ifndef _UTIL_H_ 15 | #define _UTIL_H_ 16 | 17 | #include 18 | #include 19 | 20 | #include "hardware/structs/scb.h" 21 | 22 | // works with negative index 23 | static inline int wrap_ix(int index, int n) 24 | { 25 | return ((index % n) + n) % n; 26 | } 27 | 28 | __attribute__((always_inline)) static inline uint32_t calculate_checksum(uint32_t const *p, size_t const size){ 29 | uint32_t checksum = 0; 30 | for (uint32_t i = 0; i < (size/sizeof(uint32_t))-1; i++){ 31 | checksum ^= *p; 32 | p++; 33 | } 34 | return checksum; 35 | } 36 | 37 | 38 | // from Google Chromium's codebase: 39 | #ifndef COUNT_OF 40 | #define COUNT_OF(x) ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x]))))) 41 | #endif 42 | 43 | __attribute__((always_inline)) static inline void __DSB(void) { 44 | __asm volatile("dsb 0xF" ::: "memory"); 45 | } 46 | 47 | // Patterned after CMSIS NVIC_SystemReset 48 | __attribute__((__noreturn__)) static inline void system_reset() { 49 | __DSB(); /* Ensure all outstanding memory accesses included 50 | buffered write are completed before reset */ 51 | scb_hw->aircr = ((0x5FAUL << 16U) | (1UL << 2U)); 52 | __DSB(); /* Ensure completion of memory access */ 53 | for (;;) { 54 | __asm volatile("nop"); 55 | } 56 | } 57 | 58 | /** 59 | \brief Disable IRQ Interrupts 60 | \details Disables IRQ interrupts by setting the I-bit in the CPSR. 61 | Can only be executed in Privileged modes. 62 | */ 63 | __attribute__((always_inline)) static inline void __disable_irq(void) { 64 | __asm volatile("cpsid i" : : : "memory"); 65 | } 66 | 67 | #endif 68 | /* [] END OF FILE */ 69 | -------------------------------------------------------------------------------- /FatFs_SPI/ff15/documents/res/app2.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------/ 2 | / Delete a sub-directory even if it contains any file 3 | /-------------------------------------------------------------/ 4 | / The delete_node() function is for R0.12+. 5 | / It works regardless of FF_FS_RPATH. 6 | */ 7 | 8 | 9 | FRESULT delete_node ( 10 | TCHAR* path, /* Path name buffer with the sub-directory to delete */ 11 | UINT sz_buff, /* Size of path name buffer (items) */ 12 | FILINFO* fno /* Name read buffer */ 13 | ) 14 | { 15 | UINT i, j; 16 | FRESULT fr; 17 | DIR dir; 18 | 19 | 20 | fr = f_opendir(&dir, path); /* Open the sub-directory to make it empty */ 21 | if (fr != FR_OK) return fr; 22 | 23 | for (i = 0; path[i]; i++) ; /* Get current path length */ 24 | path[i++] = _T('/'); 25 | 26 | for (;;) { 27 | fr = f_readdir(&dir, fno); /* Get a directory item */ 28 | if (fr != FR_OK || !fno->fname[0]) break; /* End of directory? */ 29 | j = 0; 30 | do { /* Make a path name */ 31 | if (i + j >= sz_buff) { /* Buffer over flow? */ 32 | fr = 100; break; /* Fails with 100 when buffer overflow */ 33 | } 34 | path[i + j] = fno->fname[j]; 35 | } while (fno->fname[j++]); 36 | if (fno->fattrib & AM_DIR) { /* Item is a sub-directory */ 37 | fr = delete_node(path, sz_buff, fno); 38 | } else { /* Item is a file */ 39 | fr = f_unlink(path); 40 | } 41 | if (fr != FR_OK) break; 42 | } 43 | 44 | path[--i] = 0; /* Restore the path name */ 45 | f_closedir(&dir); 46 | 47 | if (fr == FR_OK) fr = f_unlink(path); /* Delete the empty sub-directory */ 48 | return fr; 49 | } 50 | 51 | 52 | 53 | 54 | int main (void) /* How to use */ 55 | { 56 | FRESULT fr; 57 | FATFS fs; 58 | TCHAR buff[256]; 59 | FILINFO fno; 60 | 61 | 62 | f_mount(&fs, _T("5:"), 0); 63 | 64 | /* Directory to be deleted */ 65 | _tcscpy(buff, _T("5:dir")); 66 | 67 | /* Delete the directory */ 68 | fr = delete_node(buff, sizeof buff / sizeof buff[0], &fno); 69 | 70 | /* Check the result */ 71 | if (fr) { 72 | _tprintf(_T("Failed to delete the directory. (%u)\n"), fr); 73 | return fr; 74 | } else { 75 | _tprintf(_T("The directory and the contents have successfully been deleted.\n"), buff); 76 | return 0; 77 | } 78 | } 79 | 80 | 81 | 82 | -------------------------------------------------------------------------------- /FatFs_SPI/include/ff_stdio.h: -------------------------------------------------------------------------------- 1 | /* ff_stdio.h 2 | Copyright 2021 Carl John Kugler III 3 | 4 | Licensed under the Apache License, Version 2.0 (the License); you may not use 5 | this file except in compliance with the License. You may obtain a copy of the 6 | License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | Unless required by applicable law or agreed to in writing, software distributed 10 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 12 | specific language governing permissions and limitations under the License. 13 | */ 14 | // For compatibility with FreeRTOS+FAT API 15 | #include 16 | #include 17 | #include 18 | #include 19 | // 20 | #include "ff.h" 21 | // 22 | #include "my_debug.h" 23 | 24 | #define BaseType_t int 25 | #define FF_FILE FIL 26 | 27 | #define ff_rewind f_rewind 28 | #define pvPortMalloc malloc 29 | #define vPortFree free 30 | #define ffconfigMAX_FILENAME 250 31 | #define configASSERT myASSERT 32 | #define FF_PRINTF printf 33 | #define pdFREERTOS_ERRNO_NONE 0 34 | #define FF_EOF (-1) 35 | #define FF_SEEK_SET 0 36 | #define FF_SEEK_CUR 1 37 | #define FF_SEEK_END 2 38 | #define pdFALSE 0 39 | #define pdTRUE 1 40 | #define ff_filelength f_size 41 | #define ff_feof f_eof 42 | 43 | typedef struct FF_STAT { 44 | uint32_t st_size; /* Size of the object in number of bytes. */ 45 | // uint16_t st_mode; /* The mode (attribute bits) of this 46 | // file or directory. */ 47 | } FF_Stat_t; 48 | 49 | typedef struct { 50 | DIR dir; 51 | FILINFO fileinfo; 52 | const char *pcFileName; 53 | uint32_t ulFileSize; 54 | //uint8_t ucAttributes; 55 | } FF_FindData_t; 56 | 57 | FF_FILE *ff_fopen(const char *pcFile, const char *pcMode); 58 | int ff_fclose(FF_FILE *pxStream); 59 | int ff_stat(const char *pcFileName, FF_Stat_t *pxStatBuffer); 60 | size_t ff_fwrite(const void *pvBuffer, size_t xSize, size_t xItems, 61 | FF_FILE *pxStream); 62 | size_t ff_fread(void *pvBuffer, size_t xSize, size_t xItems, FF_FILE *pxStream); 63 | int ff_chdir(const char *pcDirectoryName); 64 | char *ff_getcwd(char *pcBuffer, size_t xBufferLength); 65 | int ff_mkdir(const char *pcPath); 66 | int ff_fputc(int iChar, FF_FILE *pxStream); 67 | int ff_fgetc(FF_FILE *pxStream); 68 | int ff_rmdir(const char *pcDirectory); 69 | int ff_remove(const char *pcPath); 70 | long ff_ftell(FF_FILE *pxStream); 71 | int ff_fseek(FF_FILE *pxStream, int iOffset, int iWhence); 72 | int ff_findfirst(const char *pcDirectory, FF_FindData_t *pxFindData); 73 | int ff_findnext( FF_FindData_t *pxFindData ); 74 | FF_FILE *ff_truncate( const char * pcFileName, long lTruncateSize ); 75 | int ff_seteof( FF_FILE *pxStream ); 76 | int ff_rename( const char *pcOldName, const char *pcNewName, int bDeleteIfExists ); 77 | char *ff_fgets(char *pcBuffer, size_t xCount, FF_FILE *pxStream); 78 | -------------------------------------------------------------------------------- /example/maker_pi_pico.hw_config.c: -------------------------------------------------------------------------------- 1 | /* hw_config.c 2 | Copyright 2021 Carl John Kugler III 3 | 4 | Licensed under the Apache License, Version 2.0 (the License); you may not use 5 | this file except in compliance with the License. You may obtain a copy of the 6 | License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | Unless required by applicable law or agreed to in writing, software distributed 10 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 12 | specific language governing permissions and limitations under the License. 13 | */ 14 | /* 15 | 16 | This file should be tailored to match the hardware design. 17 | 18 | There should be one element of the spi[] array for each hardware SPI used. 19 | 20 | There should be one element of the sd_cards[] array for each SD card slot. 21 | The name is should correspond to the FatFs "logical drive" identifier. 22 | (See http://elm-chan.org/fsw/ff/doc/filename.html#vol) 23 | The rest of the constants will depend on the type of 24 | socket, which SPI it is driven by, and how it is wired. 25 | 26 | */ 27 | 28 | #include 29 | // 30 | #include "my_debug.h" 31 | // 32 | #include "hw_config.h" 33 | // 34 | #include "ff.h" /* Obtains integer types */ 35 | // 36 | #include "diskio.h" /* Declarations of disk functions */ 37 | 38 | // Hardware Configuration of SPI "objects" 39 | // Note: multiple SD cards can be driven by one SPI if they use different slave 40 | // selects. 41 | static spi_t spis[] = { // One for each SPI. 42 | { 43 | .hw_inst = spi1, // SPI component 44 | .miso_gpio = 12, // GPIO number (not pin number) 45 | .mosi_gpio = 11, 46 | .sck_gpio = 10, 47 | /* The choice of SD card matters! SanDisk runs at the highest speed. PNY 48 | can only mangage 5 MHz. Those are all I've tried. */ 49 | //.baud_rate = 1000 * 1000, 50 | .baud_rate = 12500 * 1000, 51 | //.baud_rate = 25 * 1000 * 1000, // Actual frequency: 20833333. 52 | }}; 53 | 54 | // Hardware Configuration of the SD Card "objects" 55 | static sd_card_t sd_cards[] = { // One for each SD card 56 | { 57 | .pcName = "0:", // Name used to mount device 58 | .spi = &spis[0], // Pointer to the SPI driving this card 59 | .ss_gpio = 15 // The SPI slave select GPIO for this SD card 60 | } 61 | }; 62 | 63 | /* ********************************************************************** */ 64 | size_t sd_get_num() { return count_of(sd_cards); } 65 | sd_card_t *sd_get_by_num(size_t num) { 66 | if (num <= sd_get_num()) { 67 | return &sd_cards[num]; 68 | } else { 69 | return NULL; 70 | } 71 | } 72 | size_t spi_get_num() { return count_of(spis); } 73 | spi_t *spi_get_by_num(size_t num) { 74 | if (num <= sd_get_num()) { 75 | return &spis[num]; 76 | } else { 77 | return NULL; 78 | } 79 | } 80 | 81 | /* [] END OF FILE */ 82 | -------------------------------------------------------------------------------- /example/pico_sdk_import.cmake: -------------------------------------------------------------------------------- 1 | # This is a copy of /external/pico_sdk_import.cmake 2 | 3 | # This can be dropped into an external project to help locate this SDK 4 | # It should be include()ed prior to project() 5 | 6 | if (DEFINED ENV{PICO_SDK_PATH} AND (NOT PICO_SDK_PATH)) 7 | set(PICO_SDK_PATH $ENV{PICO_SDK_PATH}) 8 | message("Using PICO_SDK_PATH from environment ('${PICO_SDK_PATH}')") 9 | endif () 10 | 11 | if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT} AND (NOT PICO_SDK_FETCH_FROM_GIT)) 12 | set(PICO_SDK_FETCH_FROM_GIT $ENV{PICO_SDK_FETCH_FROM_GIT}) 13 | message("Using PICO_SDK_FETCH_FROM_GIT from environment ('${PICO_SDK_FETCH_FROM_GIT}')") 14 | endif () 15 | 16 | if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_PATH} AND (NOT PICO_SDK_FETCH_FROM_GIT_PATH)) 17 | set(PICO_SDK_FETCH_FROM_GIT_PATH $ENV{PICO_SDK_FETCH_FROM_GIT_PATH}) 18 | message("Using PICO_SDK_FETCH_FROM_GIT_PATH from environment ('${PICO_SDK_FETCH_FROM_GIT_PATH}')") 19 | endif () 20 | 21 | set(PICO_SDK_PATH "${PICO_SDK_PATH}" CACHE PATH "Path to the Raspberry Pi Pico SDK") 22 | 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") 23 | set(PICO_SDK_FETCH_FROM_GIT_PATH "${PICO_SDK_FETCH_FROM_GIT_PATH}" CACHE FILEPATH "location to download SDK") 24 | 25 | if (NOT PICO_SDK_PATH) 26 | if (PICO_SDK_FETCH_FROM_GIT) 27 | include(FetchContent) 28 | set(FETCHCONTENT_BASE_DIR_SAVE ${FETCHCONTENT_BASE_DIR}) 29 | if (PICO_SDK_FETCH_FROM_GIT_PATH) 30 | get_filename_component(FETCHCONTENT_BASE_DIR "${PICO_SDK_FETCH_FROM_GIT_PATH}" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}") 31 | endif () 32 | FetchContent_Declare( 33 | pico_sdk 34 | GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk 35 | GIT_TAG master 36 | ) 37 | if (NOT pico_sdk) 38 | message("Downloading Raspberry Pi Pico SDK") 39 | FetchContent_Populate(pico_sdk) 40 | set(PICO_SDK_PATH ${pico_sdk_SOURCE_DIR}) 41 | endif () 42 | set(FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR_SAVE}) 43 | else () 44 | message(FATAL_ERROR 45 | "SDK location was not specified. Please set PICO_SDK_PATH or set PICO_SDK_FETCH_FROM_GIT to on to fetch from git." 46 | ) 47 | endif () 48 | endif () 49 | 50 | get_filename_component(PICO_SDK_PATH "${PICO_SDK_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}") 51 | if (NOT EXISTS ${PICO_SDK_PATH}) 52 | message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' not found") 53 | endif () 54 | 55 | set(PICO_SDK_INIT_CMAKE_FILE ${PICO_SDK_PATH}/pico_sdk_init.cmake) 56 | if (NOT EXISTS ${PICO_SDK_INIT_CMAKE_FILE}) 57 | message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' does not appear to contain the Raspberry Pi Pico SDK") 58 | endif () 59 | 60 | set(PICO_SDK_PATH ${PICO_SDK_PATH} CACHE PATH "Path to the Raspberry Pi Pico SDK" FORCE) 61 | 62 | include(${PICO_SDK_INIT_CMAKE_FILE}) 63 | -------------------------------------------------------------------------------- /FatFs_SPI/ff15/source/diskio.h: -------------------------------------------------------------------------------- 1 | /*-----------------------------------------------------------------------/ 2 | / Low level disk interface modlue include file (C)ChaN, 2019 / 3 | /-----------------------------------------------------------------------*/ 4 | 5 | #ifndef _DISKIO_DEFINED 6 | #define _DISKIO_DEFINED 7 | 8 | #ifdef __cplusplus 9 | extern "C" { 10 | #endif 11 | 12 | /* Status of Disk Functions */ 13 | typedef BYTE DSTATUS; 14 | 15 | /* Results of Disk Functions */ 16 | typedef enum { 17 | RES_OK = 0, /* 0: Successful */ 18 | RES_ERROR, /* 1: R/W Error */ 19 | RES_WRPRT, /* 2: Write Protected */ 20 | RES_NOTRDY, /* 3: Not Ready */ 21 | RES_PARERR /* 4: Invalid Parameter */ 22 | } DRESULT; 23 | 24 | 25 | /*---------------------------------------*/ 26 | /* Prototypes for disk control functions */ 27 | 28 | 29 | DSTATUS disk_initialize (BYTE pdrv); 30 | DSTATUS disk_status (BYTE pdrv); 31 | DRESULT disk_read (BYTE pdrv, BYTE* buff, LBA_t sector, UINT count); 32 | DRESULT disk_write (BYTE pdrv, const BYTE* buff, LBA_t sector, UINT count); 33 | DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff); 34 | 35 | 36 | /* Disk Status Bits (DSTATUS) */ 37 | 38 | #define STA_NOINIT 0x01 /* Drive not initialized */ 39 | #define STA_NODISK 0x02 /* No medium in the drive */ 40 | #define STA_PROTECT 0x04 /* Write protected */ 41 | 42 | 43 | /* Command code for disk_ioctrl fucntion */ 44 | 45 | /* Generic command (Used by FatFs) */ 46 | #define CTRL_SYNC 0 /* Complete pending write process (needed at FF_FS_READONLY == 0) */ 47 | #define GET_SECTOR_COUNT 1 /* Get media size (needed at FF_USE_MKFS == 1) */ 48 | #define GET_SECTOR_SIZE 2 /* Get sector size (needed at FF_MAX_SS != FF_MIN_SS) */ 49 | #define GET_BLOCK_SIZE 3 /* Get erase block size (needed at FF_USE_MKFS == 1) */ 50 | #define CTRL_TRIM 4 /* Inform device that the data on the block of sectors is no longer used (needed at FF_USE_TRIM == 1) */ 51 | 52 | /* Generic command (Not used by FatFs) */ 53 | #define CTRL_POWER 5 /* Get/Set power status */ 54 | #define CTRL_LOCK 6 /* Lock/Unlock media removal */ 55 | #define CTRL_EJECT 7 /* Eject media */ 56 | #define CTRL_FORMAT 8 /* Create physical format on the media */ 57 | 58 | /* MMC/SDC specific ioctl command */ 59 | #define MMC_GET_TYPE 10 /* Get card type */ 60 | #define MMC_GET_CSD 11 /* Get CSD */ 61 | #define MMC_GET_CID 12 /* Get CID */ 62 | #define MMC_GET_OCR 13 /* Get OCR */ 63 | #define MMC_GET_SDSTAT 14 /* Get SD status */ 64 | #define ISDIO_READ 55 /* Read data form SD iSDIO register */ 65 | #define ISDIO_WRITE 56 /* Write data to SD iSDIO register */ 66 | #define ISDIO_MRITE 57 /* Masked write data to SD iSDIO register */ 67 | 68 | /* ATA/CF specific ioctl command */ 69 | #define ATA_GET_REV 20 /* Get F/W revision */ 70 | #define ATA_GET_MODEL 21 /* Get model name */ 71 | #define ATA_GET_SN 22 /* Get serial number */ 72 | 73 | #ifdef __cplusplus 74 | } 75 | #endif 76 | 77 | #endif 78 | -------------------------------------------------------------------------------- /simple_example/pico_sdk_import.cmake: -------------------------------------------------------------------------------- 1 | # This is a copy of /external/pico_sdk_import.cmake 2 | 3 | # This can be dropped into an external project to help locate this SDK 4 | # It should be include()ed prior to project() 5 | 6 | if (DEFINED ENV{PICO_SDK_PATH} AND (NOT PICO_SDK_PATH)) 7 | set(PICO_SDK_PATH $ENV{PICO_SDK_PATH}) 8 | message("Using PICO_SDK_PATH from environment ('${PICO_SDK_PATH}')") 9 | endif () 10 | 11 | if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT} AND (NOT PICO_SDK_FETCH_FROM_GIT)) 12 | set(PICO_SDK_FETCH_FROM_GIT $ENV{PICO_SDK_FETCH_FROM_GIT}) 13 | message("Using PICO_SDK_FETCH_FROM_GIT from environment ('${PICO_SDK_FETCH_FROM_GIT}')") 14 | endif () 15 | 16 | if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_PATH} AND (NOT PICO_SDK_FETCH_FROM_GIT_PATH)) 17 | set(PICO_SDK_FETCH_FROM_GIT_PATH $ENV{PICO_SDK_FETCH_FROM_GIT_PATH}) 18 | message("Using PICO_SDK_FETCH_FROM_GIT_PATH from environment ('${PICO_SDK_FETCH_FROM_GIT_PATH}')") 19 | endif () 20 | 21 | set(PICO_SDK_PATH "${PICO_SDK_PATH}" CACHE PATH "Path to the Raspberry Pi Pico SDK") 22 | 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") 23 | set(PICO_SDK_FETCH_FROM_GIT_PATH "${PICO_SDK_FETCH_FROM_GIT_PATH}" CACHE FILEPATH "location to download SDK") 24 | 25 | if (NOT PICO_SDK_PATH) 26 | if (PICO_SDK_FETCH_FROM_GIT) 27 | include(FetchContent) 28 | set(FETCHCONTENT_BASE_DIR_SAVE ${FETCHCONTENT_BASE_DIR}) 29 | if (PICO_SDK_FETCH_FROM_GIT_PATH) 30 | get_filename_component(FETCHCONTENT_BASE_DIR "${PICO_SDK_FETCH_FROM_GIT_PATH}" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}") 31 | endif () 32 | FetchContent_Declare( 33 | pico_sdk 34 | GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk 35 | GIT_TAG master 36 | ) 37 | if (NOT pico_sdk) 38 | message("Downloading Raspberry Pi Pico SDK") 39 | FetchContent_Populate(pico_sdk) 40 | set(PICO_SDK_PATH ${pico_sdk_SOURCE_DIR}) 41 | endif () 42 | set(FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR_SAVE}) 43 | else () 44 | message(FATAL_ERROR 45 | "SDK location was not specified. Please set PICO_SDK_PATH or set PICO_SDK_FETCH_FROM_GIT to on to fetch from git." 46 | ) 47 | endif () 48 | endif () 49 | 50 | get_filename_component(PICO_SDK_PATH "${PICO_SDK_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}") 51 | if (NOT EXISTS ${PICO_SDK_PATH}) 52 | message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' not found") 53 | endif () 54 | 55 | set(PICO_SDK_INIT_CMAKE_FILE ${PICO_SDK_PATH}/pico_sdk_init.cmake) 56 | if (NOT EXISTS ${PICO_SDK_INIT_CMAKE_FILE}) 57 | message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' does not appear to contain the Raspberry Pi Pico SDK") 58 | endif () 59 | 60 | set(PICO_SDK_PATH ${PICO_SDK_PATH} CACHE PATH "Path to the Raspberry Pi Pico SDK" FORCE) 61 | 62 | include(${PICO_SDK_INIT_CMAKE_FILE}) 63 | -------------------------------------------------------------------------------- /dynamic_config_example/pico_sdk_import.cmake: -------------------------------------------------------------------------------- 1 | # This is a copy of /external/pico_sdk_import.cmake 2 | 3 | # This can be dropped into an external project to help locate this SDK 4 | # It should be include()ed prior to project() 5 | 6 | if (DEFINED ENV{PICO_SDK_PATH} AND (NOT PICO_SDK_PATH)) 7 | set(PICO_SDK_PATH $ENV{PICO_SDK_PATH}) 8 | message("Using PICO_SDK_PATH from environment ('${PICO_SDK_PATH}')") 9 | endif () 10 | 11 | if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT} AND (NOT PICO_SDK_FETCH_FROM_GIT)) 12 | set(PICO_SDK_FETCH_FROM_GIT $ENV{PICO_SDK_FETCH_FROM_GIT}) 13 | message("Using PICO_SDK_FETCH_FROM_GIT from environment ('${PICO_SDK_FETCH_FROM_GIT}')") 14 | endif () 15 | 16 | if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_PATH} AND (NOT PICO_SDK_FETCH_FROM_GIT_PATH)) 17 | set(PICO_SDK_FETCH_FROM_GIT_PATH $ENV{PICO_SDK_FETCH_FROM_GIT_PATH}) 18 | message("Using PICO_SDK_FETCH_FROM_GIT_PATH from environment ('${PICO_SDK_FETCH_FROM_GIT_PATH}')") 19 | endif () 20 | 21 | set(PICO_SDK_PATH "${PICO_SDK_PATH}" CACHE PATH "Path to the Raspberry Pi Pico SDK") 22 | 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") 23 | set(PICO_SDK_FETCH_FROM_GIT_PATH "${PICO_SDK_FETCH_FROM_GIT_PATH}" CACHE FILEPATH "location to download SDK") 24 | 25 | if (NOT PICO_SDK_PATH) 26 | if (PICO_SDK_FETCH_FROM_GIT) 27 | include(FetchContent) 28 | set(FETCHCONTENT_BASE_DIR_SAVE ${FETCHCONTENT_BASE_DIR}) 29 | if (PICO_SDK_FETCH_FROM_GIT_PATH) 30 | get_filename_component(FETCHCONTENT_BASE_DIR "${PICO_SDK_FETCH_FROM_GIT_PATH}" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}") 31 | endif () 32 | FetchContent_Declare( 33 | pico_sdk 34 | GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk 35 | GIT_TAG master 36 | ) 37 | if (NOT pico_sdk) 38 | message("Downloading Raspberry Pi Pico SDK") 39 | FetchContent_Populate(pico_sdk) 40 | set(PICO_SDK_PATH ${pico_sdk_SOURCE_DIR}) 41 | endif () 42 | set(FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR_SAVE}) 43 | else () 44 | message(FATAL_ERROR 45 | "SDK location was not specified. Please set PICO_SDK_PATH or set PICO_SDK_FETCH_FROM_GIT to on to fetch from git." 46 | ) 47 | endif () 48 | endif () 49 | 50 | get_filename_component(PICO_SDK_PATH "${PICO_SDK_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}") 51 | if (NOT EXISTS ${PICO_SDK_PATH}) 52 | message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' not found") 53 | endif () 54 | 55 | set(PICO_SDK_INIT_CMAKE_FILE ${PICO_SDK_PATH}/pico_sdk_init.cmake) 56 | if (NOT EXISTS ${PICO_SDK_INIT_CMAKE_FILE}) 57 | message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' does not appear to contain the Raspberry Pi Pico SDK") 58 | endif () 59 | 60 | set(PICO_SDK_PATH ${PICO_SDK_PATH} CACHE PATH "Path to the Raspberry Pi Pico SDK" FORCE) 61 | 62 | include(${PICO_SDK_INIT_CMAKE_FILE}) 63 | -------------------------------------------------------------------------------- /simple_example/hw_config.c: -------------------------------------------------------------------------------- 1 | /* hw_config.c 2 | Copyright 2021 Carl John Kugler III 3 | 4 | Licensed under the Apache License, Version 2.0 (the License); you may not use 5 | this file except in compliance with the License. You may obtain a copy of the 6 | License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | Unless required by applicable law or agreed to in writing, software distributed 10 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 12 | specific language governing permissions and limitations under the License. 13 | */ 14 | /* 15 | 16 | This file should be tailored to match the hardware design. 17 | 18 | There should be one element of the spi[] array for each hardware SPI used. 19 | 20 | There should be one element of the sd_cards[] array for each SD card slot. 21 | The name is should correspond to the FatFs "logical drive" identifier. 22 | (See http://elm-chan.org/fsw/ff/doc/filename.html#vol) 23 | The rest of the constants will depend on the type of 24 | socket, which SPI it is driven by, and how it is wired. 25 | 26 | */ 27 | 28 | #include 29 | // 30 | #include "my_debug.h" 31 | // 32 | #include "hw_config.h" 33 | // 34 | #include "ff.h" /* Obtains integer types */ 35 | // 36 | #include "diskio.h" /* Declarations of disk functions */ 37 | 38 | // Hardware Configuration of SPI "objects" 39 | // Note: multiple SD cards can be driven by one SPI if they use different slave 40 | // selects. 41 | static spi_t spis[] = { // One for each SPI. 42 | { 43 | .hw_inst = spi1, // SPI component 44 | .miso_gpio = 12, // GPIO number (not pin number) 45 | .mosi_gpio = 15, 46 | .sck_gpio = 14, 47 | .baud_rate = 12500 * 1000, 48 | //.baud_rate = 25 * 1000 * 1000, // Actual frequency: 20833333. 49 | } 50 | }; 51 | 52 | // Hardware Configuration of the SD Card "objects" 53 | static sd_card_t sd_cards[] = { // One for each SD card 54 | { 55 | .pcName = "0:", // Name used to mount device 56 | .spi = &spis[0], // Pointer to the SPI driving this card 57 | .ss_gpio = 9, // The SPI slave select GPIO for this SD card 58 | .use_card_detect = true, 59 | .card_detect_gpio = 13, // Card detect 60 | .card_detected_true = 1 // What the GPIO read returns when a card is 61 | // present. Use -1 if there is no card detect. 62 | }}; 63 | 64 | /* ********************************************************************** */ 65 | size_t sd_get_num() { return count_of(sd_cards); } 66 | sd_card_t *sd_get_by_num(size_t num) { 67 | if (num <= sd_get_num()) { 68 | return &sd_cards[num]; 69 | } else { 70 | return NULL; 71 | } 72 | } 73 | size_t spi_get_num() { return count_of(spis); } 74 | spi_t *spi_get_by_num(size_t num) { 75 | if (num <= sd_get_num()) { 76 | return &spis[num]; 77 | } else { 78 | return NULL; 79 | } 80 | } 81 | 82 | /* [] END OF FILE */ 83 | -------------------------------------------------------------------------------- /dynamic_config_example/thing_plus.dynamic_config_example.cpp: -------------------------------------------------------------------------------- 1 | /* Instead of a statically linked hw_config.c, 2 | create configuration dynamically */ 3 | 4 | #include 5 | #include 6 | // 7 | #include "f_util.h" 8 | #include "ff.h" 9 | #include "pico/stdlib.h" 10 | #include "rtc.h" 11 | // 12 | #include "hw_config.h" 13 | // 14 | #include "diskio.h" /* Declarations of disk functions */ 15 | 16 | void add_spi(spi_t *const spi); 17 | void add_sd_card(sd_card_t *const sd_card); 18 | 19 | static spi_t *p_spi; 20 | void spi_dma_isr() { spi_irq_handler(p_spi); } 21 | 22 | void test(sd_card_t *pSD) { 23 | // See FatFs - Generic FAT Filesystem Module, "Application Interface", 24 | // http://elm-chan.org/fsw/ff/00index_e.html 25 | FRESULT fr = f_mount(&pSD->fatfs, pSD->pcName, 1); 26 | if (FR_OK != fr) panic("f_mount error: %s (%d)\n", FRESULT_str(fr), fr); 27 | fr = f_chdrive(pSD->pcName); 28 | if (FR_OK != fr) panic("f_chdrive error: %s (%d)\n", FRESULT_str(fr), fr); 29 | 30 | FIL fil; 31 | const char *const filename = "filename.txt"; 32 | fr = f_open(&fil, filename, FA_OPEN_APPEND | FA_WRITE); 33 | if (FR_OK != fr && FR_EXIST != fr) 34 | panic("f_open(%s) error: %s (%d)\n", filename, FRESULT_str(fr), fr); 35 | if (f_printf(&fil, "Hello, world!\n") < 0) { 36 | printf("f_printf failed\n"); 37 | } 38 | fr = f_close(&fil); 39 | if (FR_OK != fr) { 40 | printf("f_close error: %s (%d)\n", FRESULT_str(fr), fr); 41 | } 42 | 43 | f_unmount(pSD->pcName); 44 | } 45 | 46 | int main() { 47 | stdio_init_all(); 48 | time_init(); 49 | 50 | puts("Hello, world!"); 51 | 52 | // Hardware Configuration of SPI "object" 53 | p_spi = new spi_t; 54 | memset(p_spi, 0, sizeof(spi_t)); 55 | if (!p_spi) panic("Out of memory"); 56 | p_spi->hw_inst = spi1; // SPI component 57 | p_spi->miso_gpio = 12; // GPIO number (not pin number) 58 | p_spi->mosi_gpio = 15; 59 | p_spi->sck_gpio = 14; 60 | p_spi->baud_rate = 12500 * 1000; // The limitation here is SPI slew rate. 61 | p_spi->dma_isr = spi_dma_isr; 62 | p_spi->initialized = false; // initialized flag 63 | add_spi(p_spi); 64 | 65 | // Hardware Configuration of the SD Card "object" 66 | sd_card_t *p_sd_card = new sd_card_t; 67 | if (!p_sd_card) panic("Out of memory"); 68 | memset(p_sd_card, 0, sizeof(sd_card_t)); 69 | p_sd_card->pcName = "0:"; // Name used to mount device 70 | p_sd_card->spi = p_spi; // Pointer to the SPI driving this card 71 | p_sd_card->ss_gpio = 9; // The SPI slave select GPIO for this SD card 72 | p_sd_card->use_card_detect = false; 73 | p_sd_card->card_detect_gpio = 0; // Card detect 74 | // What the GPIO read returns when a card is 75 | // present. 76 | p_sd_card->card_detected_true = 0; 77 | // State attributes: 78 | p_sd_card->m_Status = STA_NOINIT; 79 | p_sd_card->sectors = 0; 80 | p_sd_card->card_type = 0; 81 | add_sd_card(p_sd_card); 82 | 83 | for (size_t i = 0; i < sd_get_num(); ++i) 84 | test(sd_get_by_num(i)); 85 | 86 | puts("Goodbye, world!"); 87 | 88 | for (;;); 89 | } 90 | -------------------------------------------------------------------------------- /FatFs_SPI/sd_driver/spi.h: -------------------------------------------------------------------------------- 1 | /* spi.h 2 | Copyright 2021 Carl John Kugler III 3 | 4 | Licensed under the Apache License, Version 2.0 (the License); you may not use 5 | this file except in compliance with the License. You may obtain a copy of the 6 | License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | Unless required by applicable law or agreed to in writing, software distributed 10 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 12 | specific language governing permissions and limitations under the License. 13 | */ 14 | 15 | #pragma once 16 | 17 | #include 18 | // 19 | // Pico includes 20 | #include "hardware/dma.h" 21 | #include "hardware/gpio.h" 22 | #include "hardware/irq.h" 23 | #include "hardware/spi.h" 24 | #include "pico/mutex.h" 25 | #include "pico/sem.h" 26 | #include "pico/types.h" 27 | 28 | #define SPI_FILL_CHAR (0xFF) 29 | 30 | // "Class" representing SPIs 31 | typedef struct { 32 | // SPI HW 33 | spi_inst_t *hw_inst; 34 | uint miso_gpio; // SPI MISO GPIO number (not pin number) 35 | uint mosi_gpio; 36 | uint sck_gpio; 37 | uint baud_rate; 38 | uint DMA_IRQ_num; // DMA_IRQ_0 or DMA_IRQ_1 39 | 40 | // Drive strength levels for GPIO outputs. 41 | // enum gpio_drive_strength { GPIO_DRIVE_STRENGTH_2MA = 0, GPIO_DRIVE_STRENGTH_4MA = 1, GPIO_DRIVE_STRENGTH_8MA = 2, 42 | // GPIO_DRIVE_STRENGTH_12MA = 3 } 43 | bool set_drive_strength; 44 | enum gpio_drive_strength mosi_gpio_drive_strength; 45 | enum gpio_drive_strength sck_gpio_drive_strength; 46 | 47 | // State variables: 48 | uint tx_dma; 49 | uint rx_dma; 50 | dma_channel_config tx_dma_cfg; 51 | dma_channel_config rx_dma_cfg; 52 | irq_handler_t dma_isr; // Ignored: no longer used 53 | bool initialized; 54 | semaphore_t sem; 55 | mutex_t mutex; 56 | } spi_t; 57 | 58 | #ifdef __cplusplus 59 | extern "C" { 60 | #endif 61 | 62 | bool __not_in_flash_func(spi_transfer)(spi_t *pSPI, const uint8_t *tx, uint8_t *rx, size_t length); 63 | void spi_lock(spi_t *pSPI); 64 | void spi_unlock(spi_t *pSPI); 65 | bool my_spi_init(spi_t *pSPI); 66 | void set_spi_dma_irq_channel(bool useChannel1, bool shared); 67 | 68 | #ifdef __cplusplus 69 | } 70 | #endif 71 | 72 | /* 73 | This uses the Pico LED to show SD card activity. 74 | You can use it to get a rough idea of utilization. 75 | Warning: Pico W uses GPIO 25 for SPI communication to the CYW43439. 76 | 77 | You can enable this by putting something like 78 | add_compile_definitions(USE_LED=1) 79 | in CMakeLists.txt, for example. 80 | */ 81 | #if !defined(NO_PICO_LED) && defined(USE_LED) && USE_LED && defined(PICO_DEFAULT_LED_PIN) 82 | # define LED_INIT() \ 83 | { \ 84 | gpio_init(PICO_DEFAULT_LED_PIN); \ 85 | gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT); \ 86 | } 87 | # define LED_ON() gpio_put(PICO_DEFAULT_LED_PIN, 1) 88 | # define LED_OFF() gpio_put(PICO_DEFAULT_LED_PIN, 0) 89 | #else 90 | # define LED_ON() 91 | # define LED_OFF() 92 | # define LED_INIT() 93 | #endif 94 | 95 | /* [] END OF FILE */ 96 | -------------------------------------------------------------------------------- /simple_example/thing_plus.hw_config.c: -------------------------------------------------------------------------------- 1 | /* hw_config.c 2 | Copyright 2021 Carl John Kugler III 3 | 4 | Licensed under the Apache License, Version 2.0 (the License); you may not use 5 | this file except in compliance with the License. You may obtain a copy of the 6 | License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | Unless required by applicable law or agreed to in writing, software distributed 10 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 12 | specific language governing permissions and limitations under the License. 13 | */ 14 | /* 15 | 16 | This file should be tailored to match the hardware design. 17 | 18 | There should be one element of the spi[] array for each hardware SPI used. 19 | 20 | There should be one element of the sd_cards[] array for each SD card slot. 21 | The name is should correspond to the FatFs "logical drive" identifier. 22 | (See http://elm-chan.org/fsw/ff/doc/filename.html#vol) 23 | The rest of the constants will depend on the type of 24 | socket, which SPI it is driven by, and how it is wired. 25 | 26 | */ 27 | 28 | #include 29 | // 30 | #include "my_debug.h" 31 | // 32 | #include "hw_config.h" 33 | // 34 | #include "ff.h" /* Obtains integer types */ 35 | // 36 | #include "diskio.h" /* Declarations of disk functions */ 37 | 38 | void spi_dma_isr(); 39 | 40 | // Hardware Configuration of SPI "objects" 41 | // Note: multiple SD cards can be driven by one SPI if they use different slave 42 | // selects. 43 | static spi_t spis[] = { // One for each SPI. 44 | { 45 | .hw_inst = spi1, // SPI component 46 | .miso_gpio = 12, // GPIO number (not pin number) 47 | .mosi_gpio = 15, 48 | .sck_gpio = 14, 49 | 50 | /* The choice of SD card matters! SanDisk runs at the highest speed. PNY 51 | can only mangage 5 MHz. Those are all I've tried. */ 52 | //.baud_rate = 1000 * 1000, 53 | .baud_rate = 12500 * 1000, // The limitation here is SPI slew rate. 54 | //.baud_rate = 25 * 1000 * 1000, // Actual frequency: 20833333. Has 55 | // worked for me with SanDisk. 56 | 57 | .dma_isr = spi_dma_isr 58 | } 59 | }; 60 | 61 | // Hardware Configuration of the SD Card "objects" 62 | static sd_card_t sd_cards[] = { // One for each SD card 63 | { 64 | .pcName = "0:", // Name used to mount device 65 | .spi = &spis[0], // Pointer to the SPI driving this card 66 | .ss_gpio = 9, // The SPI slave select GPIO for this SD card 67 | //.use_card_detect = false, 68 | .m_Status = STA_NOINIT 69 | } 70 | }; 71 | 72 | void spi_dma_isr() { spi_irq_handler(&spis[0]); } 73 | 74 | /* ********************************************************************** */ 75 | size_t sd_get_num() { return count_of(sd_cards); } 76 | sd_card_t *sd_get_by_num(size_t num) { 77 | if (num <= sd_get_num()) { 78 | return &sd_cards[num]; 79 | } else { 80 | return NULL; 81 | } 82 | } 83 | size_t spi_get_num() { return count_of(spis); } 84 | spi_t *spi_get_by_num(size_t num) { 85 | if (num <= sd_get_num()) { 86 | return &spis[num]; 87 | } else { 88 | return NULL; 89 | } 90 | } 91 | 92 | /* [] END OF FILE */ 93 | -------------------------------------------------------------------------------- /dynamic_config_example/dynamic_config_example.cpp: -------------------------------------------------------------------------------- 1 | /* Instead of a statically linked hw_config.c, 2 | create configuration dynamically */ 3 | 4 | #include 5 | #include 6 | // 7 | #include "f_util.h" 8 | #include "ff.h" 9 | #include "pico/stdlib.h" 10 | #include "rtc.h" 11 | // 12 | #include "hw_config.h" 13 | // 14 | #include "diskio.h" /* Declarations of disk functions */ 15 | 16 | void add_spi(spi_t *const spi); 17 | void add_sd_card(sd_card_t *const sd_card); 18 | 19 | static spi_t *p_spi; 20 | 21 | void test(sd_card_t *pSD) { 22 | // See FatFs - Generic FAT Filesystem Module, "Application Interface", 23 | // http://elm-chan.org/fsw/ff/00index_e.html 24 | FRESULT fr = f_mount(&pSD->fatfs, pSD->pcName, 1); 25 | if (FR_OK != fr) panic("f_mount error: %s (%d)\n", FRESULT_str(fr), fr); 26 | fr = f_chdrive(pSD->pcName); 27 | if (FR_OK != fr) panic("f_chdrive error: %s (%d)\n", FRESULT_str(fr), fr); 28 | 29 | FIL fil; 30 | const char *const filename = "filename.txt"; 31 | fr = f_open(&fil, filename, FA_OPEN_APPEND | FA_WRITE); 32 | if (FR_OK != fr && FR_EXIST != fr) 33 | panic("f_open(%s) error: %s (%d)\n", filename, FRESULT_str(fr), fr); 34 | if (f_printf(&fil, "Hello, world!\n") < 0) { 35 | printf("f_printf failed\n"); 36 | } 37 | fr = f_close(&fil); 38 | if (FR_OK != fr) { 39 | printf("f_close error: %s (%d)\n", FRESULT_str(fr), fr); 40 | } 41 | 42 | f_unmount(pSD->pcName); 43 | } 44 | 45 | int main() { 46 | stdio_init_all(); 47 | time_init(); 48 | 49 | puts("Hello, world!"); 50 | 51 | // Hardware Configuration of SPI "object" 52 | p_spi = new spi_t; 53 | memset(p_spi, 0, sizeof(spi_t)); 54 | if (!p_spi) panic("Out of memory"); 55 | p_spi->hw_inst = spi1; // SPI component 56 | p_spi->miso_gpio = 12; // GPIO number (not pin number) 57 | p_spi->mosi_gpio = 15; 58 | p_spi->sck_gpio = 14; 59 | p_spi->baud_rate = 12500 * 1000; 60 | add_spi(p_spi); 61 | 62 | // Hardware Configuration of the SD Card "object" 63 | sd_card_t *p_sd_card = new sd_card_t; 64 | if (!p_sd_card) panic("Out of memory"); 65 | memset(p_sd_card, 0, sizeof(sd_card_t)); 66 | p_sd_card->pcName = "0:"; // Name used to mount device 67 | p_sd_card->spi = p_spi; // Pointer to the SPI driving this card 68 | p_sd_card->ss_gpio = 9; // The SPI slave select GPIO for this SD card 69 | p_sd_card->use_card_detect = true; 70 | p_sd_card->card_detect_gpio = 13; // Card detect 71 | // What the GPIO read returns when a card is 72 | // present. Use -1 if there is no card detect. 73 | p_sd_card->card_detected_true = 1; 74 | add_sd_card(p_sd_card); 75 | 76 | #ifdef CARD2 77 | /* Add another SD card */ 78 | p_sd_card = new sd_card_t; 79 | if (!p_sd_card) panic("Out of memory"); 80 | memset(p_sd_card, 0, sizeof(sd_card_t)); 81 | p_sd_card->pcName = "1:"; // Name used to mount device 82 | p_sd_card->spi = p_spi; // Pointer to the SPI driving this card 83 | p_sd_card->ss_gpio = 15; // The SPI slave select GPIO for this SD card 84 | p_sd_card->card_detect_gpio = 14; // Card detect 85 | // What the GPIO read returns when a card is 86 | // present. Use -1 if there is no card detect. 87 | p_sd_card->card_detected_true = 0; 88 | // State attributes: 89 | p_sd_card->m_Status = STA_NOINIT; 90 | p_sd_card->sectors = 0; 91 | p_sd_card->card_type = 0; 92 | add_sd_card(p_sd_card); 93 | #endif 94 | 95 | for (size_t i = 0; i < sd_get_num(); ++i) 96 | test(sd_get_by_num(i)); 97 | 98 | puts("Goodbye, world!"); 99 | 100 | for (;;); 101 | } 102 | -------------------------------------------------------------------------------- /example/thing_plus.hw_config.c: -------------------------------------------------------------------------------- 1 | /* hw_config.c 2 | Copyright 2021 Carl John Kugler III 3 | 4 | Licensed under the Apache License, Version 2.0 (the License); you may not use 5 | this file except in compliance with the License. You may obtain a copy of the 6 | License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | Unless required by applicable law or agreed to in writing, software distributed 10 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 12 | specific language governing permissions and limitations under the License. 13 | */ 14 | /* 15 | 16 | This file should be tailored to match the hardware design. 17 | 18 | There should be one element of the spi[] array for each hardware SPI used. 19 | 20 | There should be one element of the sd_cards[] array for each SD card slot. 21 | The name is should correspond to the FatFs "logical drive" identifier. 22 | (See http://elm-chan.org/fsw/ff/doc/filename.html#vol) 23 | The rest of the constants will depend on the type of 24 | socket, which SPI it is driven by, and how it is wired. 25 | 26 | */ 27 | 28 | #include 29 | // 30 | #include "my_debug.h" 31 | // 32 | #include "hw_config.h" 33 | // 34 | #include "ff.h" /* Obtains integer types */ 35 | // 36 | #include "diskio.h" /* Declarations of disk functions */ 37 | 38 | /* This configuration is for the [SparkFun Thing Plus - RP2040](https://www.sparkfun.com/products/17745), 39 | which has the following hardware configuration: 40 | 41 | | | Old | | | | | 42 | | Signal | term | spi0 | GPIO | Description | Controller/Peripheral (NEW) | 43 | | --------------------------------- | ---- | ---- | ---- | -------------------------- | ------------------------------------ | 44 | | DATA 0/CIPO (or Peripheral's SDO) | MISO | RX | 12 | Master In Slave Out (MISO) | Controller In, Peripheral Out (CIPO) | 45 | | CMD/COPI (or Peripheral's SDI) | MOSI | TX | 15 | Master Out Slave In (MOSI) | Controller Out Peripheral In (COPI) | 46 | | CLK/SCK | SCK | SCK | 14 | Serial Clock | | 47 | | DATA 3/CS | SS | CSn | 9 | Slave Select pin (SS) | Chip Select Pin (CS) | 48 | 49 | See [RP2040 Thing Plus Hookup Guide](https://learn.sparkfun.com/tutorials/rp2040-thing-plus-hookup-guide/hardware-overview). 50 | */ 51 | 52 | // Hardware Configuration of SPI "objects" 53 | // Note: multiple SD cards can be driven by one SPI if they use different slave 54 | // selects. 55 | static spi_t spis[] = { // One for each SPI. 56 | { 57 | .hw_inst = spi1, // SPI component 58 | .miso_gpio = 12, // GPIO number (not pin number) 59 | .mosi_gpio = 15, 60 | .sck_gpio = 14, 61 | 62 | // .baud_rate = 1000 * 1000 63 | //.baud_rate = 12500 * 1000 64 | .baud_rate = 25 * 1000 * 1000 // Actual frequency: 20833333. 65 | }}; 66 | 67 | // Hardware Configuration of the SD Card "objects" 68 | static sd_card_t sd_cards[] = { // One for each SD card 69 | { 70 | .pcName = "0:", // Name used to mount device 71 | .spi = &spis[0], // Pointer to the SPI driving this card 72 | .ss_gpio = 9 // The SPI slave select GPIO for this SD card 73 | 74 | /* ********************************************************************** */ 75 | size_t sd_get_num() { return count_of(sd_cards); } 76 | sd_card_t *sd_get_by_num(size_t num) { 77 | if (num <= sd_get_num()) { 78 | return &sd_cards[num]; 79 | } else { 80 | return NULL; 81 | } 82 | } 83 | size_t spi_get_num() { return count_of(spis); } 84 | spi_t *spi_get_by_num(size_t num) { 85 | if (num <= spi_get_num()) { 86 | return &spis[num]; 87 | } else { 88 | return NULL; 89 | } 90 | } 91 | 92 | /* [] END OF FILE */ 93 | -------------------------------------------------------------------------------- /FatFs_SPI/ff15/documents/css_e.css: -------------------------------------------------------------------------------- 1 | * {margin: 0; padding: 0; border-width: 0;} 2 | body {margin: 8px; background-color: #e0ffff; font-color: black; font-family: serif; line-height: 133%; max-width: 1024px;} 3 | a:link {color: blue;} 4 | a:visited {color: darkmagenta;} 5 | a:hover {background-color: #a0ffff;} 6 | a:active {color: darkmagenta; overflow: hidden; outline:none; position: relative; top: 1px; left: 1px;} 7 | abbr {border-width: 1px;} 8 | 9 | p {margin: 0 0 0.3em 1em;} 10 | i {margin: 0 0.3em 0 0;} 11 | b {margin: 0 0.1em;} 12 | em {font-style: normal; font-weight: bold; margin: 0 0.1em;} 13 | strong {} 14 | pre {border: 1px dashed gray; margin: 0.5em 1em; padding: 0.5em; line-height: 1.2em; font-size: 85%; font-family: "Consolas", "Courier New", monospace; background-color: white;} 15 | pre span.c {color: green;} 16 | pre span.k {color: blue;} 17 | pre span.e {color: red;} 18 | pre span.b {font-weight: bold;} 19 | pre span.arg {font-style: italic;} 20 | tt {margin: 0 0.2em; font-size: 0.85em; font-family: "Consolas", "Courier New", monospace; } 21 | tt.arg {font-style: italic;} 22 | ol {margin: 0.5em 2.5em;} 23 | ul {margin: 0.5em 2em;} 24 | ul ul {margin: 0 2em 0.5em 1em;} 25 | dl {margin: 0.5em 1em;} 26 | dd {margin: 0 2em;} 27 | dt {font-size: 0.85em; font-family: "Consolas", "Courier New", monospace;} 28 | dl.par dt {margin: 0.5em 0 0 0 ; font-style: italic; } 29 | dl.ret dt {margin: 0.5em 0 0 0 ; font-size: 0.85em; font-family: "Consolas", "Courier New", monospace; font-weight: bold; } 30 | hr {border-width: 1px; margin: 1em;} 31 | div.abst {font-family: sans-serif;} 32 | div.para {clear: both; font-family: serif;} 33 | div.ret a {font-size: 0.85em; font-family: "Consolas", "Courier New", monospace; } 34 | .equ {text-indent: 0; margin: 1em 2em 1em;} 35 | .indent {margin-left: 2em;} 36 | .rset {float: right; margin: 0.3em 0 0.5em 0.5em;} 37 | .lset {float: left; margin: 0.3em 0.5em 0.5em 0.5em;} 38 | ul.flat li {list-style-type: none; margin: 0;} 39 | a.imglnk img {border: 1px solid;} 40 | .iequ {white-space: nowrap; font-weight: bold;} 41 | .clr {clear: both;} 42 | .it {font-style: italic;} 43 | .mfd {font-size: 0.7em; padding: 0 1px; border: 1px solid; white-space : nowrap} 44 | .ral {text-align: right; } 45 | .lal {text-align: left; } 46 | .cal {text-align: center; } 47 | 48 | h1 {line-height: 1em; font-size: 2em; font-family: sans-serif; padding: 0.3em 0 0.3em;} 49 | h2 {font-size: 2em; font-family: sans-serif; background-color: #d8d8FF; padding: 0.5em 0.5em; margin: 0 0 0.5em;} 50 | h3 {font-size: 1.5em; font-family: sans-serif; margin: 1.5em 0 0.5em;} 51 | div.doc h3 {border-color: #b0d8d8; border-style: solid; border-width: 0px 0px 4px 12px; padding: 4px; margin-top: 3em;} 52 | h4 {font-size: 1.2em; font-family: sans-serif; margin: 2em 0 0.2em;} 53 | h5 {font-size: 1em; font-family: sans-serif; margin: 1em 0 0em;} 54 | p.hdd {float: right; text-align: right; margin-top: 0.5em;} 55 | hr.hds {clear: both; margin-bottom: 1em;} 56 | kbd {letter-spacing: 0;} 57 | small {font-size: 80%;} 58 | .indent {margin-left: 2em;} 59 | 60 | /* Tables */ 61 | table {margin: 0.5em 1em; border-collapse: collapse; border: 2px solid gray; } 62 | table caption {font-family: sans-serif; font-weight: bold;} 63 | table th {background-color: white; border-style: solid; border-width: 1px 1px 2px; border-color: gray; padding: 0 3px; vertical-align: top; white-space: nowrap;} 64 | table td {background-color: white; border: 1px solid gray; padding: 0 3px; vertical-align: top; line-height: 1.3em;} 65 | table.lst td:first-child {font-size: 0.85em; font-family: "Consolas", "Courier New", monospace; white-space: nowrap;} 66 | table.lst2 td {font-size: 0.85em; font-family: "Consolas", "Courier New", monospace; white-space: nowrap;} 67 | table.lst3 td {font-family: "Consolas", "Courier New", monospace; white-space: nowrap;} 68 | tr.lst3 td {border-width: 2px 1px 1px; } 69 | table.lst4 td {padding: 0.3em;} 70 | table.lst4 td:nth-child(2) {width: 45%;} 71 | table.lst4 td:nth-child(3) {width: 45%;} 72 | 73 | p.foot {clear: both; text-indent: 0; margin: 1em 0.5em 1em;} 74 | -------------------------------------------------------------------------------- /example/hw_config.c: -------------------------------------------------------------------------------- 1 | /* hw_config.c 2 | Copyright 2021 Carl John Kugler III 3 | 4 | Licensed under the Apache License, Version 2.0 (the License); you may not use 5 | this file except in compliance with the License. You may obtain a copy of the 6 | License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | Unless required by applicable law or agreed to in writing, software distributed 10 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 12 | specific language governing permissions and limitations under the License. 13 | */ 14 | /* 15 | 16 | This file should be tailored to match the hardware design. 17 | 18 | There should be one element of the spi[] array for each hardware SPI used. 19 | 20 | There should be one element of the sd_cards[] array for each SD card slot. 21 | The name is should correspond to the FatFs "logical drive" identifier. 22 | (See http://elm-chan.org/fsw/ff/doc/filename.html#vol) 23 | In general, this should correspond to the (zero origin) array index. 24 | The rest of the constants will depend on the type of 25 | socket, which SPI it is driven by, and how it is wired. 26 | 27 | */ 28 | 29 | #include 30 | #include 31 | // 32 | #include "my_debug.h" 33 | // 34 | #include "hw_config.h" 35 | // 36 | #include "ff.h" /* Obtains integer types */ 37 | // 38 | #include "diskio.h" /* Declarations of disk functions */ 39 | 40 | /* 41 | This example assumes the following hardware configuration: 42 | 43 | | | SPI0 | GPIO | Pin | SPI | MicroSD | Description | 44 | | ----- | ---- | ----- | --- | -------- | --------- | ---------------------- | 45 | | MISO | RX | 16 | 21 | DO | DO | Master In, Slave Out | 46 | | MOSI | TX | 19 | 25 | DI | DI | Master Out, Slave In | 47 | | SCK | SCK | 18 | 24 | SCLK | CLK | SPI clock | 48 | | CS0 | CSn | 17 | 22 | SS or CS | CS | Slave (or Chip) Select | 49 | | DET | | 22 | 29 | | CD | Card Detect | 50 | | GND | | | 18,23 | | GND | Ground | 51 | | 3v3 | | | 36 | | 3v3 | 3.3 volt power | 52 | 53 | */ 54 | 55 | // Hardware Configuration of SPI "objects" 56 | // Note: multiple SD cards can be driven by one SPI if they use different slave 57 | // selects. 58 | static spi_t spis[] = { // One for each SPI. 59 | { 60 | .hw_inst = spi0, // SPI component 61 | .miso_gpio = 16, // GPIO number (not Pico pin number) 62 | .mosi_gpio = 19, 63 | .sck_gpio = 18, 64 | 65 | // .baud_rate = 1000 * 1000 66 | .baud_rate = 12500 * 1000 67 | // .baud_rate = 25 * 1000 * 1000 // Actual frequency: 20833333. 68 | }}; 69 | 70 | // Hardware Configuration of the SD Card "objects" 71 | static sd_card_t sd_cards[] = { // One for each SD card 72 | { 73 | .pcName = "0:", // Name used to mount device 74 | .spi = &spis[0], // Pointer to the SPI driving this card 75 | .ss_gpio = 17, // The SPI slave select GPIO for this SD card 76 | .use_card_detect = true, 77 | .card_detect_gpio = 22, // Card detect 78 | .card_detected_true = 1 // What the GPIO read returns when a card is 79 | // present. 80 | }}; 81 | 82 | /* ********************************************************************** */ 83 | size_t sd_get_num() { return count_of(sd_cards); } 84 | sd_card_t *sd_get_by_num(size_t num) { 85 | assert(num <= sd_get_num()); 86 | if (num <= sd_get_num()) { 87 | return &sd_cards[num]; 88 | } else { 89 | return NULL; 90 | } 91 | } 92 | size_t spi_get_num() { return count_of(spis); } 93 | spi_t *spi_get_by_num(size_t num) { 94 | assert(num <= spi_get_num()); 95 | if (num <= spi_get_num()) { 96 | return &spis[num]; 97 | } else { 98 | return NULL; 99 | } 100 | } 101 | 102 | /* [] END OF FILE */ 103 | -------------------------------------------------------------------------------- /FatFs_SPI/src/f_util.c: -------------------------------------------------------------------------------- 1 | /* f_util.c 2 | Copyright 2021 Carl John Kugler III 3 | 4 | Licensed under the Apache License, Version 2.0 (the License); you may not use 5 | this file except in compliance with the License. You may obtain a copy of the 6 | License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | Unless required by applicable law or agreed to in writing, software distributed 10 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 12 | specific language governing permissions and limitations under the License. 13 | */ 14 | #include "ff.h" 15 | 16 | const char *FRESULT_str(FRESULT i) { 17 | switch (i) { 18 | case FR_OK: 19 | return "Succeeded"; 20 | case FR_DISK_ERR: 21 | return "A hard error occurred in the low level disk I/O layer"; 22 | case FR_INT_ERR: 23 | return "Assertion failed"; 24 | case FR_NOT_READY: 25 | return "The physical drive cannot work"; 26 | case FR_NO_FILE: 27 | return "Could not find the file"; 28 | case FR_NO_PATH: 29 | return "Could not find the path"; 30 | case FR_INVALID_NAME: 31 | return "The path name format is invalid"; 32 | case FR_DENIED: 33 | return "Access denied due to prohibited access or directory full"; 34 | case FR_EXIST: 35 | return "Access denied due to prohibited access (exists)"; 36 | case FR_INVALID_OBJECT: 37 | return "The file/directory object is invalid"; 38 | case FR_WRITE_PROTECTED: 39 | return "The physical drive is write protected"; 40 | case FR_INVALID_DRIVE: 41 | return "The logical drive number is invalid"; 42 | case FR_NOT_ENABLED: 43 | return "The volume has no work area (mount)"; 44 | case FR_NO_FILESYSTEM: 45 | return "There is no valid FAT volume"; 46 | case FR_MKFS_ABORTED: 47 | return "The f_mkfs() aborted due to any problem"; 48 | case FR_TIMEOUT: 49 | return "Could not get a grant to access the volume within defined " 50 | "period"; 51 | case FR_LOCKED: 52 | return "The operation is rejected according to the file sharing " 53 | "policy"; 54 | case FR_NOT_ENOUGH_CORE: 55 | return "LFN working buffer could not be allocated"; 56 | case FR_TOO_MANY_OPEN_FILES: 57 | return "Number of open files > FF_FS_LOCK"; 58 | case FR_INVALID_PARAMETER: 59 | return "Given parameter is invalid"; 60 | default: 61 | return "Unknown"; 62 | } 63 | } 64 | 65 | FRESULT delete_node ( 66 | TCHAR* path, /* Path name buffer with the sub-directory to delete */ 67 | UINT sz_buff, /* Size of path name buffer (items) */ 68 | FILINFO* fno /* Name read buffer */ 69 | ) 70 | { 71 | UINT i, j; 72 | FRESULT fr; 73 | DIR dir; 74 | 75 | 76 | fr = f_opendir(&dir, path); /* Open the sub-directory to make it empty */ 77 | if (fr != FR_OK) return fr; 78 | 79 | for (i = 0; path[i]; i++) ; /* Get current path length */ 80 | path[i++] = '/'; 81 | 82 | for (;;) { 83 | fr = f_readdir(&dir, fno); /* Get a directory item */ 84 | if (fr != FR_OK || !fno->fname[0]) break; /* End of directory? */ 85 | j = 0; 86 | do { /* Make a path name */ 87 | if (i + j >= sz_buff) { /* Buffer over flow? */ 88 | fr = 100; break; /* Fails with 100 when buffer overflow */ 89 | } 90 | path[i + j] = fno->fname[j]; 91 | } while (fno->fname[j++]); 92 | if (fno->fattrib & AM_DIR) { /* Item is a sub-directory */ 93 | fr = delete_node(path, sz_buff, fno); 94 | } else { /* Item is a file */ 95 | fr = f_unlink(path); 96 | } 97 | if (fr != FR_OK) break; 98 | } 99 | 100 | path[--i] = 0; /* Restore the path name */ 101 | f_closedir(&dir); 102 | 103 | if (fr == FR_OK) fr = f_unlink(path); /* Delete the empty sub-directory */ 104 | return fr; 105 | } 106 | -------------------------------------------------------------------------------- /FatFs_SPI/src/rtc.c: -------------------------------------------------------------------------------- 1 | /* rtc.c 2 | Copyright 2021 Carl John Kugler III 3 | 4 | Licensed under the Apache License, Version 2.0 (the License); you may not use 5 | this file except in compliance with the License. You may obtain a copy of the 6 | License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | Unless required by applicable law or agreed to in writing, software distributed 10 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 12 | specific language governing permissions and limitations under the License. 13 | */ 14 | #include 15 | #include 16 | // 17 | #include "hardware/rtc.h" 18 | #include "pico/stdio.h" 19 | #include "pico/stdlib.h" 20 | #include "pico/util/datetime.h" 21 | // 22 | #include "ff.h" 23 | #include "util.h" // calculate_checksum 24 | // 25 | #include "rtc.h" 26 | 27 | static time_t epochtime; 28 | 29 | // Make an attempt to save a recent time stamp across reset: 30 | typedef struct rtc_save { 31 | uint32_t signature; 32 | datetime_t datetime; 33 | uint32_t checksum; // last, not included in checksum 34 | } rtc_save_t; 35 | static rtc_save_t rtc_save __attribute__((section(".uninitialized_data"))); 36 | 37 | static void update_epochtime() { 38 | bool rc = rtc_get_datetime(&rtc_save.datetime); 39 | if (rc) { 40 | rtc_save.signature = 0xBABEBABE; 41 | struct tm timeinfo = { 42 | .tm_sec = rtc_save.datetime 43 | .sec, /* Seconds. [0-60] (1 leap second) */ 44 | .tm_min = rtc_save.datetime.min, /* Minutes. [0-59] */ 45 | .tm_hour = rtc_save.datetime.hour, /* Hours. [0-23] */ 46 | .tm_mday = rtc_save.datetime.day, /* Day. [1-31] */ 47 | .tm_mon = rtc_save.datetime.month - 1, /* Month. [0-11] */ 48 | .tm_year = rtc_save.datetime.year - 1900, /* Year - 1900. */ 49 | .tm_wday = 0, /* Day of week. [0-6] */ 50 | .tm_yday = 0, /* Days in year.[0-365] */ 51 | .tm_isdst = -1 /* DST. [-1/0/1]*/ 52 | }; 53 | rtc_save.checksum = calculate_checksum((uint32_t *)&rtc_save, 54 | offsetof(rtc_save_t, checksum)); 55 | epochtime = mktime(&timeinfo); 56 | rtc_save.datetime.dotw = timeinfo.tm_wday; 57 | // configASSERT(-1 != epochtime); 58 | } 59 | } 60 | 61 | time_t time(time_t *pxTime) { 62 | update_epochtime(); 63 | if (pxTime) { 64 | *pxTime = epochtime; 65 | } 66 | return epochtime; 67 | } 68 | 69 | void time_init() { 70 | rtc_init(); 71 | datetime_t t = {0, 0, 0, 0, 0, 0, 0}; 72 | rtc_get_datetime(&t); 73 | if (!t.year && rtc_save.datetime.year) { 74 | uint32_t xor_checksum = calculate_checksum( 75 | (uint32_t *)&rtc_save, offsetof(rtc_save_t, checksum)); 76 | if (rtc_save.signature == 0xBABEBABE && 77 | rtc_save.checksum == xor_checksum) { 78 | // Set rtc 79 | rtc_set_datetime(&rtc_save.datetime); 80 | } 81 | } 82 | } 83 | 84 | // Called by FatFs: 85 | DWORD get_fattime(void) { 86 | datetime_t t = {0, 0, 0, 0, 0, 0, 0}; 87 | bool rc = rtc_get_datetime(&t); 88 | if (!rc) return 0; 89 | 90 | DWORD fattime = 0; 91 | // bit31:25 92 | // Year origin from the 1980 (0..127, e.g. 37 for 2017) 93 | uint8_t yr = t.year - 1980; 94 | fattime |= (0b01111111 & yr) << 25; 95 | // bit24:21 96 | // Month (1..12) 97 | uint8_t mo = t.month; 98 | fattime |= (0b00001111 & mo) << 21; 99 | // bit20:16 100 | // Day of the month (1..31) 101 | uint8_t da = t.day; 102 | fattime |= (0b00011111 & da) << 16; 103 | // bit15:11 104 | // Hour (0..23) 105 | uint8_t hr = t.hour; 106 | fattime |= (0b00011111 & hr) << 11; 107 | // bit10:5 108 | // Minute (0..59) 109 | uint8_t mi = t.min; 110 | fattime |= (0b00111111 & mi) << 5; 111 | // bit4:0 112 | // Second / 2 (0..29, e.g. 25 for 50) 113 | uint8_t sd = t.sec / 2; 114 | fattime |= (0b00011111 & sd); 115 | return fattime; 116 | } 117 | -------------------------------------------------------------------------------- /FatFs_SPI/sd_driver/sd_card.h: -------------------------------------------------------------------------------- 1 | /* sd_card.h 2 | Copyright 2021 Carl John Kugler III 3 | 4 | Licensed under the Apache License, Version 2.0 (the License); you may not use 5 | this file except in compliance with the License. You may obtain a copy of the 6 | License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | Unless required by applicable law or agreed to in writing, software distributed 10 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 12 | specific language governing permissions and limitations under the License. 13 | */ 14 | 15 | // Note: The model used here is one FatFS per SD card. 16 | // Multiple partitions on a card are not supported. 17 | 18 | #pragma once 19 | 20 | #include 21 | // 22 | #include "hardware/gpio.h" 23 | #include "pico/mutex.h" 24 | // 25 | #include "ff.h" 26 | // 27 | #include "spi.h" 28 | 29 | #ifdef __cplusplus 30 | extern "C" { 31 | #endif 32 | 33 | typedef struct sd_card_t sd_card_t; 34 | 35 | // "Class" representing SD Cards 36 | struct sd_card_t { 37 | const char *pcName; 38 | spi_t *spi; 39 | // Slave select is here instead of in spi_t because multiple SDs can share an SPI. 40 | uint ss_gpio; // Slave select for this SD card 41 | bool use_card_detect; 42 | uint card_detect_gpio; // Card detect; ignored if !use_card_detect 43 | uint card_detected_true; // Varies with card socket; ignored if !use_card_detect 44 | // Drive strength levels for GPIO outputs. 45 | // enum gpio_drive_strength { GPIO_DRIVE_STRENGTH_2MA = 0, GPIO_DRIVE_STRENGTH_4MA = 1, GPIO_DRIVE_STRENGTH_8MA = 2, 46 | // GPIO_DRIVE_STRENGTH_12MA = 3 } 47 | bool set_drive_strength; 48 | enum gpio_drive_strength ss_gpio_drive_strength; 49 | 50 | // Following fields are used to keep track of the state of the card: 51 | int m_Status; // Card status 52 | uint64_t sectors; // Assigned dynamically 53 | int card_type; // Assigned dynamically 54 | mutex_t mutex; 55 | FATFS fatfs; 56 | bool mounted; 57 | 58 | int (*init)(sd_card_t *sd_card_p); 59 | int (*write_blocks)(sd_card_t *sd_card_p, const uint8_t *buffer, 60 | uint64_t ulSectorNumber, uint32_t blockCnt); 61 | int (*read_blocks)(sd_card_t *sd_card_p, uint8_t *buffer, uint64_t ulSectorNumber, 62 | uint32_t ulSectorCount); 63 | 64 | // Useful when use_card_detect is false - call periodically to check for presence of SD card 65 | // Returns true if and only if SD card was sensed on the bus 66 | bool (*sd_test_com)(sd_card_t *sd_card_p); 67 | }; 68 | 69 | #define SD_BLOCK_DEVICE_ERROR_NONE 0 70 | #define SD_BLOCK_DEVICE_ERROR_WOULD_BLOCK -5001 /*!< operation would block */ 71 | #define SD_BLOCK_DEVICE_ERROR_UNSUPPORTED -5002 /*!< unsupported operation */ 72 | #define SD_BLOCK_DEVICE_ERROR_PARAMETER -5003 /*!< invalid parameter */ 73 | #define SD_BLOCK_DEVICE_ERROR_NO_INIT -5004 /*!< uninitialized */ 74 | #define SD_BLOCK_DEVICE_ERROR_NO_DEVICE -5005 /*!< device is missing or not connected */ 75 | #define SD_BLOCK_DEVICE_ERROR_WRITE_PROTECTED -5006 /*!< write protected */ 76 | #define SD_BLOCK_DEVICE_ERROR_UNUSABLE -5007 /*!< unusable card */ 77 | #define SD_BLOCK_DEVICE_ERROR_NO_RESPONSE -5008 /*!< No response from device */ 78 | #define SD_BLOCK_DEVICE_ERROR_CRC -5009 /*!< CRC error */ 79 | #define SD_BLOCK_DEVICE_ERROR_ERASE -5010 /*!< Erase error: reset/sequence */ 80 | #define SD_BLOCK_DEVICE_ERROR_WRITE -5011 /*!< SPI Write error: !SPI_DATA_ACCEPTED */ 81 | 82 | ///* Disk Status Bits (DSTATUS) */ 83 | // See diskio.h. 84 | //enum { 85 | // STA_NOINIT = 0x01, /* Drive not initialized */ 86 | // STA_NODISK = 0x02, /* No medium in the drive */ 87 | // STA_PROTECT = 0x04 /* Write protected */ 88 | //}; 89 | 90 | bool sd_card_detect(sd_card_t *pSD); 91 | uint64_t sd_sectors(sd_card_t *pSD); 92 | 93 | bool sd_init_driver(); 94 | bool sd_card_detect(sd_card_t *sd_card_p); 95 | 96 | #ifdef __cplusplus 97 | } 98 | #endif 99 | 100 | /* [] END OF FILE */ 101 | -------------------------------------------------------------------------------- /example/exp_mod_A.hw_config.c: -------------------------------------------------------------------------------- 1 | /* hw_config.c 2 | Copyright 2021 Carl John Kugler III 3 | 4 | Licensed under the Apache License, Version 2.0 (the License); you may not use 5 | this file except in compliance with the License. You may obtain a copy of the 6 | License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | Unless required by applicable law or agreed to in writing, software distributed 10 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 12 | specific language governing permissions and limitations under the License. 13 | */ 14 | /* 15 | 16 | This file should be tailored to match the hardware design. 17 | 18 | There should be one element of the spi[] array for each hardware SPI used. 19 | 20 | There should be one element of the sd_cards[] array for each SD card slot. 21 | The name is should correspond to the FatFs "logical drive" identifier. 22 | (See http://elm-chan.org/fsw/ff/doc/filename.html#vol) 23 | In general, this should correspond to the (zero origin) array index. 24 | The rest of the constants will depend on the type of 25 | socket, which SPI it is driven by, and how it is wired. 26 | 27 | */ 28 | 29 | #include 30 | #include 31 | // 32 | #include "my_debug.h" 33 | // 34 | #include "hw_config.h" 35 | // 36 | #include "ff.h" /* Obtains integer types */ 37 | // 38 | #include "diskio.h" /* Declarations of disk functions */ 39 | 40 | /* 41 | This example assumes the following hardware configuration: 42 | 43 | | | SPI0 | GPIO | Pin | SPI | MicroSD | Description | 44 | | ----- | ---- | ----- | --- | -------- | --------- | ---------------------- | 45 | | MISO | RX | 16 | 21 | DO | DO | Master In, Slave Out | 46 | | MOSI | TX | 19 | 25 | DI | DI | Master Out, Slave In | 47 | | SCK | SCK | 18 | 24 | SCLK | CLK | SPI clock | 48 | | CS0 | CSn | 17 | 22 | SS or CS | CS | Slave (or Chip) Select | 49 | | DET | | 22 | 29 | | CD | Card Detect | 50 | | GND | | | 18,23 | | GND | Ground | 51 | | 3v3 | | | 36 | | 3v3 | 3.3 volt power | 52 | 53 | */ 54 | 55 | // Hardware Configuration of SPI "objects" 56 | // Note: multiple SD cards can be driven by one SPI if they use different slave 57 | // selects. 58 | static spi_t spis[] = { // One for each SPI. 59 | { 60 | .hw_inst = spi0, // SPI component 61 | .sck_gpio = 2, // GPIO number (not Pico pin number) 62 | .mosi_gpio = 3, 63 | .miso_gpio = 4, 64 | .set_drive_strength = true, 65 | .mosi_gpio_drive_strength = GPIO_DRIVE_STRENGTH_4MA, 66 | .sck_gpio_drive_strength = GPIO_DRIVE_STRENGTH_2MA, 67 | 68 | // .baud_rate = 25 * 1000 * 1000, // Actual frequency: 20833333. 69 | .baud_rate = 125E6 / 4, 70 | 71 | // .DMA_IRQ_num = DMA_IRQ_0 72 | } 73 | }; 74 | 75 | // Hardware Configuration of the SD Card "objects" 76 | static sd_card_t sd_cards[] = { // One for each SD card 77 | { 78 | .pcName = "0:", // Name used to mount device 79 | .spi = &spis[0], // Pointer to the SPI driving this card 80 | .ss_gpio = 7, // The SPI slave select GPIO for this SD card 81 | 82 | // .set_drive_strength = true, 83 | // .ss_gpio_drive_strength = GPIO_DRIVE_STRENGTH_4MA, 84 | 85 | // SD Card detect: 86 | .use_card_detect = true, 87 | .card_detect_gpio = 9, 88 | .card_detected_true = 0, // What the GPIO read returns when a card is 89 | // present. 90 | } 91 | }; 92 | 93 | /* ********************************************************************** */ 94 | size_t sd_get_num() { return count_of(sd_cards); } 95 | sd_card_t *sd_get_by_num(size_t num) { 96 | assert(num <= sd_get_num()); 97 | if (num <= sd_get_num()) { 98 | return &sd_cards[num]; 99 | } else { 100 | return NULL; 101 | } 102 | } 103 | size_t spi_get_num() { return count_of(spis); } 104 | spi_t *spi_get_by_num(size_t num) { 105 | assert(num <= spi_get_num()); 106 | if (num <= spi_get_num()) { 107 | return &spis[num]; 108 | } else { 109 | return NULL; 110 | } 111 | } 112 | 113 | /* [] END OF FILE */ 114 | -------------------------------------------------------------------------------- /FatFs_SPI/sd_driver/sd_spi.c: -------------------------------------------------------------------------------- 1 | /* sd_spi.c 2 | Copyright 2021 Carl John Kugler III 3 | 4 | Licensed under the Apache License, Version 2.0 (the License); you may not use 5 | this file except in compliance with the License. You may obtain a copy of the 6 | License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | Unless required by applicable law or agreed to in writing, software distributed 10 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 12 | specific language governing permissions and limitations under the License. 13 | */ 14 | 15 | /* Standard includes. */ 16 | #include 17 | #include 18 | #include 19 | // 20 | #include "hardware/gpio.h" 21 | // 22 | #include "my_debug.h" 23 | #include "sd_card.h" 24 | #include "sd_spi.h" 25 | #include "spi.h" 26 | 27 | #define TRACE_PRINTF(fmt, args...) 28 | /* #define TRACE_PRINTF printf // task_printf */ 29 | 30 | #pragma GCC diagnostic push 31 | #pragma GCC diagnostic ignored "-Wunused-variable" 32 | 33 | void sd_spi_go_high_frequency(sd_card_t *pSD) { 34 | uint actual = spi_set_baudrate(pSD->spi->hw_inst, pSD->spi->baud_rate); 35 | TRACE_PRINTF("%s: Actual frequency: %lu\n", __FUNCTION__, (long)actual); 36 | } 37 | void sd_spi_go_low_frequency(sd_card_t *pSD) { 38 | uint actual = spi_set_baudrate(pSD->spi->hw_inst, 400 * 1000); // Actual frequency: 398089 39 | TRACE_PRINTF("%s: Actual frequency: %lu\n", __FUNCTION__, (long)actual); 40 | } 41 | 42 | #pragma GCC diagnostic pop 43 | 44 | static void sd_spi_lock(sd_card_t *pSD) { 45 | spi_lock(pSD->spi); 46 | } 47 | static void sd_spi_unlock(sd_card_t *pSD) { 48 | spi_unlock(pSD->spi); 49 | } 50 | 51 | // Would do nothing if pSD->ss_gpio were set to GPIO_FUNC_SPI. 52 | static void sd_spi_select(sd_card_t *pSD) { 53 | gpio_put(pSD->ss_gpio, 0); 54 | // A fill byte seems to be necessary, sometimes: 55 | uint8_t fill = SPI_FILL_CHAR; 56 | spi_write_blocking(pSD->spi->hw_inst, &fill, 1); 57 | LED_ON(); 58 | } 59 | 60 | static void sd_spi_deselect(sd_card_t *pSD) { 61 | gpio_put(pSD->ss_gpio, 1); 62 | LED_OFF(); 63 | /* 64 | MMC/SDC enables/disables the DO output in synchronising to the SCLK. This 65 | means there is a posibility of bus conflict with MMC/SDC and another SPI 66 | slave that shares an SPI bus. Therefore to make MMC/SDC release the MISO 67 | line, the master device needs to send a byte after the CS signal is 68 | deasserted. 69 | */ 70 | uint8_t fill = SPI_FILL_CHAR; 71 | spi_write_blocking(pSD->spi->hw_inst, &fill, 1); 72 | } 73 | /* Some SD cards want to be deselected between every bus transaction */ 74 | void sd_spi_deselect_pulse(sd_card_t *pSD) { 75 | sd_spi_deselect(pSD); 76 | // tCSH Pulse duration, CS high 200 ns 77 | sd_spi_select(pSD); 78 | } 79 | void sd_spi_acquire(sd_card_t *pSD) { 80 | sd_spi_lock(pSD); 81 | sd_spi_select(pSD); 82 | } 83 | 84 | void sd_spi_release(sd_card_t *pSD) { 85 | sd_spi_deselect(pSD); 86 | sd_spi_unlock(pSD); 87 | } 88 | 89 | bool sd_spi_transfer(sd_card_t *pSD, const uint8_t *tx, uint8_t *rx, 90 | size_t length) { 91 | return spi_transfer(pSD->spi, tx, rx, length); 92 | } 93 | 94 | uint8_t sd_spi_write(sd_card_t *pSD, const uint8_t value) { 95 | // TRACE_PRINTF("%s\n", __FUNCTION__); 96 | uint8_t received = SPI_FILL_CHAR; 97 | #if 0 98 | int num = spi_write_read_blocking(pSD->spi->hw_inst, &value, &received, 1); 99 | myASSERT(1 == num); 100 | #else 101 | bool success = spi_transfer(pSD->spi, &value, &received, 1); 102 | myASSERT(success); 103 | #endif 104 | return received; 105 | } 106 | 107 | void sd_spi_send_initializing_sequence(sd_card_t * pSD) { 108 | bool old_ss = gpio_get(pSD->ss_gpio); 109 | // Set DI and CS high and apply 74 or more clock pulses to SCLK: 110 | gpio_put(pSD->ss_gpio, 1); 111 | uint8_t ones[10]; 112 | memset(ones, 0xFF, sizeof ones); 113 | absolute_time_t timeout_time = make_timeout_time_ms(1); 114 | do { 115 | sd_spi_transfer(pSD, ones, NULL, sizeof ones); 116 | } while (0 < absolute_time_diff_us(get_absolute_time(), timeout_time)); 117 | gpio_put(pSD->ss_gpio, old_ss); 118 | } 119 | 120 | void sd_spi_init_pl022(sd_card_t *pSD) { 121 | // Let the PL022 SPI handle it. 122 | // the CS line is brought high between each byte during transmission. 123 | gpio_set_function(pSD->ss_gpio, GPIO_FUNC_SPI); 124 | } 125 | 126 | /* [] END OF FILE */ 127 | -------------------------------------------------------------------------------- /FatFs_SPI/ff15/documents/res/app3.c: -------------------------------------------------------------------------------- 1 | /*----------------------------------------------------------------------/ 2 | / Allocate a contiguous area to the file 3 | /-----------------------------------------------------------------------/ 4 | / This function checks if the file is contiguous with desired size. 5 | / If not, a block of contiguous sectors is allocated to the file. 6 | / If the file has been opened without FA_WRITE flag, it only checks if 7 | / the file is contiguous and returns the resulut. 8 | /-----------------------------------------------------------------------/ 9 | / This function can work with FatFs R0.09 - R0.11a. 10 | / It is incompatible with R0.12+. Use f_expand function instead. 11 | /----------------------------------------------------------------------*/ 12 | 13 | /* Declarations of FatFs internal functions accessible from applications. 14 | / This is intended to be used for disk checking/fixing or dirty hacks :-) */ 15 | DWORD clust2sect (FATFS* fs, DWORD clst); 16 | DWORD get_fat (FATFS* fs, DWORD clst); 17 | FRESULT put_fat (FATFS* fs, DWORD clst, DWORD val); 18 | 19 | 20 | DWORD allocate_contiguous_clusters ( /* Returns the first sector in LBA (0:error or not contiguous) */ 21 | FIL* fp, /* Pointer to the open file object */ 22 | DWORD len /* Number of bytes to allocate */ 23 | ) 24 | { 25 | DWORD csz, tcl, ncl, ccl, cl; 26 | 27 | 28 | if (f_lseek(fp, 0) || !len) /* Check if the given parameters are valid */ 29 | return 0; 30 | csz = 512UL * fp->fs->csize; /* Cluster size in unit of byte (assuming 512 bytes/sector) */ 31 | tcl = (len + csz - 1) / csz; /* Total number of clusters required */ 32 | len = tcl * csz; /* Round-up file size to the cluster boundary */ 33 | 34 | /* Check if the existing cluster chain is contiguous */ 35 | if (len == fp->fsize) { 36 | ncl = 0; ccl = fp->sclust; 37 | do { 38 | cl = get_fat(fp->fs, ccl); /* Get the cluster status */ 39 | if (cl + 1 < 3) return 0; /* Hard error? */ 40 | if (cl != ccl + 1 && cl < fp->fs->n_fatent) break; /* Not contiguous? */ 41 | ccl = cl; 42 | } while (++ncl < tcl); 43 | if (ncl == tcl) /* Is the file contiguous? */ 44 | return clust2sect(fp->fs, fp->sclust); /* File is contiguous. Return the start sector */ 45 | } 46 | 47 | /* File is not contiguous */ 48 | #if _FS_READONLY 49 | return 0; /* Exit if in read-only cfg. */ 50 | #else 51 | if (!(fp->flag & FA_WRITE)) return 0; /* Exit if the file object is for read-only */ 52 | 53 | if (f_truncate(fp)) return 0; /* Remove the non-contiguous chain */ 54 | 55 | /* Find a free contiguous area */ 56 | ccl = cl = 2; ncl = 0; 57 | do { 58 | if (cl >= fp->fs->n_fatent) return 0; /* No contiguous area is found. */ 59 | if (get_fat(fp->fs, cl)) { /* Encounterd a cluster in use */ 60 | do { /* Skip the block of used clusters */ 61 | cl++; 62 | if (cl >= fp->fs->n_fatent) return 0; /* No contiguous area is found. */ 63 | } while (get_fat(fp->fs, cl)); 64 | ccl = cl; ncl = 0; 65 | } 66 | cl++; ncl++; 67 | } while (ncl < tcl); 68 | 69 | /* Create a contiguous cluster chain */ 70 | fp->fs->last_clust = ccl - 1; 71 | if (f_lseek(fp, len)) return 0; 72 | 73 | return clust2sect(fp->fs, fp->sclust); /* Return file start sector */ 74 | #endif 75 | } 76 | 77 | 78 | int main (void) 79 | { 80 | FRESULT fr; 81 | DRESULT dr; 82 | FATFS fs; 83 | FIL fil; 84 | DWORD org; 85 | 86 | 87 | /* Open or create a file to write */ 88 | f_mount(&fs, "", 0); 89 | fr = f_open(&fil, "fastrec.log", FA_READ | FA_WRITE | FA_OPEN_ALWAYS); 90 | if (fr) return 1; 91 | 92 | /* Check if the file is 256MB in size and occupies a contiguous area. 93 | / If not, a contiguous area will be re-allocated to the file. */ 94 | org = allocate_contiguous_clusters(&fil, 0x10000000); 95 | if (!org) { 96 | printf("Function failed due to any error or insufficient contiguous area.\n"); 97 | f_close(&fil); 98 | return 1; 99 | } 100 | 101 | /* Now you can read/write the file without filesystem layer. */ 102 | ... 103 | dr = disk_write(fil.fs->drv, Buff, org, 1024); /* Write 512KiB from top of the file */ 104 | ... 105 | 106 | f_close(&fil); 107 | return 0; 108 | } 109 | 110 | -------------------------------------------------------------------------------- /example/dev_brd.hw_config.c: -------------------------------------------------------------------------------- 1 | /* hw_config.c 2 | Copyright 2021 Carl John Kugler III 3 | 4 | Licensed under the Apache License, Version 2.0 (the License); you may not use 5 | this file except in compliance with the License. You may obtain a copy of the 6 | License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | Unless required by applicable law or agreed to in writing, software distributed 10 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 12 | specific language governing permissions and limitations under the License. 13 | */ 14 | /* 15 | 16 | This file should be tailored to match the hardware design. 17 | 18 | There should be one element of the spi[] array for each hardware SPI used. 19 | 20 | There should be one element of the sd_cards[] array for each SD card slot. 21 | The name is should correspond to the FatFs "logical drive" identifier. 22 | (See http://elm-chan.org/fsw/ff/doc/filename.html#vol) 23 | In general, this should correspond to the (zero origin) array index. 24 | The rest of the constants will depend on the type of 25 | socket, which SPI it is driven by, and how it is wired. 26 | 27 | */ 28 | 29 | #include 30 | // 31 | #include "my_debug.h" 32 | // 33 | #include "hw_config.h" 34 | // 35 | #include "ff.h" /* Obtains integer types */ 36 | // 37 | #include "diskio.h" /* Declarations of disk functions */ 38 | 39 | /* 40 | See https://docs.google.com/spreadsheets/d/1BrzLWTyifongf_VQCc2IpJqXWtsrjmG7KnIbSBy-CPU/edit?usp=sharing, 41 | tab "Monster", for pin assignments assumed in this configuration file. 42 | */ 43 | 44 | // Hardware Configuration of SPI "objects" 45 | // Note: multiple SD cards can be driven by one SPI if they use different slave 46 | // selects. 47 | static spi_t spis[] = { // One for each SPI. 48 | { 49 | .hw_inst = spi0, // SPI component 50 | .sck_gpio = 2, // GPIO number (not Pico pin number) 51 | .mosi_gpio = 3, 52 | .miso_gpio = 4, 53 | .set_drive_strength = true, 54 | .mosi_gpio_drive_strength = GPIO_DRIVE_STRENGTH_4MA, 55 | .sck_gpio_drive_strength = GPIO_DRIVE_STRENGTH_2MA, 56 | 57 | // .baud_rate = 25 * 1000 * 1000, // Actual frequency: 20833333. 58 | .baud_rate = 12 * 1000 * 1000, // Actual frequency: 10416666. 59 | // .baud_rate = 1 * 1000 * 1000, 60 | }, 61 | { 62 | .hw_inst = spi1, // SPI component 63 | .miso_gpio = 8, // GPIO number (not Pico pin number) 64 | .sck_gpio = 10, 65 | .mosi_gpio = 11, 66 | .set_drive_strength = true, 67 | .mosi_gpio_drive_strength = GPIO_DRIVE_STRENGTH_4MA, 68 | .sck_gpio_drive_strength = GPIO_DRIVE_STRENGTH_2MA, 69 | 70 | // .baud_rate = 25 * 1000 * 1000, // Actual frequency: 20833333. 71 | .baud_rate = 12 * 1000 * 1000, // Actual frequency: 10416666. 72 | } 73 | }; 74 | 75 | // Hardware Configuration of the SD Card "objects" 76 | static sd_card_t sd_cards[] = { // One for each SD card 77 | { // Socket sd0 over SPI 78 | .pcName = "0:", // Name used to mount device 79 | .spi = &spis[0], // Pointer to the SPI driving this card 80 | .ss_gpio = 7, // The SPI slave select GPIO for this SD card 81 | .set_drive_strength = true, 82 | .ss_gpio_drive_strength = GPIO_DRIVE_STRENGTH_4MA, 83 | }, 84 | { // Socket sd1 85 | .pcName = "1:", // Name used to mount device 86 | .spi = &spis[1], // Pointer to the SPI driving this card 87 | .ss_gpio = 12, // The SPI slave select GPIO for this SD card 88 | .set_drive_strength = true, 89 | .ss_gpio_drive_strength = GPIO_DRIVE_STRENGTH_4MA, 90 | }, 91 | { // Socket sd2 92 | .pcName = "2:", // Name used to mount device 93 | .spi = &spis[1], // Pointer to the SPI driving this card 94 | .ss_gpio = 13, // The SPI slave select GPIO for this SD card 95 | .set_drive_strength = true, 96 | .ss_gpio_drive_strength = GPIO_DRIVE_STRENGTH_4MA, 97 | }, 98 | }; 99 | 100 | /* ********************************************************************** */ 101 | size_t sd_get_num() { return count_of(sd_cards); } 102 | sd_card_t *sd_get_by_num(size_t num) { 103 | if (num <= sd_get_num()) { 104 | return &sd_cards[num]; 105 | } else { 106 | return NULL; 107 | } 108 | } 109 | size_t spi_get_num() { return count_of(spis); } 110 | spi_t *spi_get_by_num(size_t num) { 111 | if (num <= spi_get_num()) { 112 | return &spis[num]; 113 | } else { 114 | return NULL; 115 | } 116 | } 117 | 118 | /* [] END OF FILE */ 119 | -------------------------------------------------------------------------------- /example/data_log_demo.c: -------------------------------------------------------------------------------- 1 | /* data_log_demo.c 2 | Copyright 2021 Carl John Kugler III 3 | 4 | Licensed under the Apache License, Version 2.0 (the License); you may not use 5 | this file except in compliance with the License. You may obtain a copy of the 6 | License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | Unless required by applicable law or agreed to in writing, software distributed 10 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 12 | specific language governing permissions and limitations under the License. 13 | */ 14 | 15 | #include 16 | #include 17 | #include 18 | // 19 | #include "hardware/adc.h" 20 | #include "pico/stdlib.h" 21 | // 22 | #include "ff.h" 23 | // 24 | #include "f_util.h" 25 | #include "my_debug.h" 26 | 27 | #define DEVICENAME "0:" 28 | 29 | #define TRACE_PRINTF(fmt, args...) 30 | //#define TRACE_PRINTF printf 31 | 32 | static bool print_header(FIL *fp) { 33 | TRACE_PRINTF("%s\n", __func__); 34 | myASSERT(fp); 35 | FRESULT fr = f_lseek(fp, f_size(fp)); 36 | if (FR_OK != fr) { 37 | printf("f_lseek error: %s (%d)\n", FRESULT_str(fr), fr); 38 | return false; 39 | } 40 | if (0 == f_tell(fp)) { 41 | // Print header 42 | if (f_printf(fp, "Date,Time,Temperature (°C)\n") < 0) { 43 | printf("f_printf error\n"); 44 | return false; 45 | } 46 | } 47 | return true; 48 | } 49 | 50 | static bool open_file(FIL *fp) { 51 | TRACE_PRINTF("%s\n", __func__); 52 | myASSERT(fp); 53 | const time_t timer = time(NULL); 54 | struct tm tmbuf; 55 | localtime_r(&timer, &tmbuf); 56 | char filename[64]; 57 | int n = snprintf(filename, sizeof filename, "/data"); 58 | myASSERT(0 < n && n < (int)sizeof filename); 59 | FRESULT fr = f_mkdir(filename); 60 | if (FR_OK != fr && FR_EXIST != fr) { 61 | printf("f_mkdir error: %s (%d)\n", FRESULT_str(fr), fr); 62 | return false; 63 | } 64 | // tm_year int years since 1900 65 | // tm_mon int months since January 0-11 66 | // tm_mday int day of the month 1-31 67 | n += snprintf(filename + n, sizeof filename - n, "/%04d-%02d-%02d", 68 | tmbuf.tm_year + 1900, tmbuf.tm_mon + 1, tmbuf.tm_mday); 69 | myASSERT(0 < n && n < (int)sizeof filename); 70 | fr = f_mkdir(filename); 71 | if (FR_OK != fr && FR_EXIST != fr) { 72 | printf("f_mkdir error: %s (%d)\n", FRESULT_str(fr), fr); 73 | return false; 74 | } 75 | size_t nw = strftime(filename + n, sizeof filename - n, "/%H.csv", &tmbuf); 76 | myASSERT(nw); 77 | fr = f_open(fp, filename, FA_OPEN_APPEND | FA_WRITE); 78 | if (FR_OK != fr && FR_EXIST != fr) { 79 | printf("f_open(%s) error: %s (%d)\n", filename, FRESULT_str(fr), fr); 80 | return false; 81 | } 82 | if (!print_header(fp)) return false; 83 | return true; 84 | } 85 | 86 | bool process_logger() { 87 | TRACE_PRINTF("%s\n", __func__); 88 | /* It's very inefficient to open and close the file for every record, 89 | but you're less likely to lose data that way. But also see f_sync 90 | (http://elm-chan.org/fsw/ff/doc/sync.html). */ 91 | FIL fil; 92 | bool rc = open_file(&fil); 93 | if (!rc) return false; 94 | 95 | // Form date-time string 96 | char buf[128]; 97 | const time_t secs = time(NULL); 98 | struct tm tmbuf; 99 | struct tm *ptm = localtime_r(&secs, &tmbuf); 100 | size_t n = strftime(buf, sizeof buf, "%F,%T,", ptm); 101 | myASSERT(n); 102 | 103 | // The temperature sensor is on input 4: 104 | adc_select_input(4); 105 | uint16_t result = adc_read(); 106 | // 12-bit conversion, assume max value == ADC_VREF == 3.3 V 107 | const float conversion_factor = 3.3f / (1 << 12); 108 | float voltage = conversion_factor * result; 109 | TRACE_PRINTF("Raw value: 0x%03x, voltage: %f V\n", result, (double)voltage); 110 | 111 | // Temperature sensor values can be approximated in centigrade as: 112 | // T = 27 - (ADC_Voltage - 0.706)/0.001721 113 | float Tc = 27.0f - (voltage - 0.706f) / 0.001721f; 114 | TRACE_PRINTF("Temperature: %.1f °C\n", (double)Tc); 115 | int nw = snprintf(buf + n, sizeof buf - n, "%.3g\n", (double)Tc); 116 | myASSERT(0 < nw && nw < (int)sizeof buf); 117 | 118 | if (f_printf(&fil, "%s", buf) < 0) { 119 | printf("f_printf failed\n"); 120 | return false; 121 | } 122 | FRESULT fr = f_close(&fil); 123 | if (FR_OK != fr) { 124 | printf("f_close error: %s (%d)\n", FRESULT_str(fr), fr); 125 | return false; 126 | } 127 | return true; 128 | } 129 | -------------------------------------------------------------------------------- /FatFs_SPI/sd_driver/demo_logging.c: -------------------------------------------------------------------------------- 1 | /* demo_logging.c 2 | Copyright 2021 Carl John Kugler III 3 | 4 | Licensed under the Apache License, Version 2.0 (the License); you may not use 5 | this file except in compliance with the License. You may obtain a copy of the 6 | License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | Unless required by applicable law or agreed to in writing, software distributed 10 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 12 | specific language governing permissions and limitations under the License. 13 | */ 14 | /* 15 | FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd. 16 | All rights reserved 17 | 18 | VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION. 19 | 20 | This file is part of the FreeRTOS distribution. 21 | 22 | FreeRTOS is free software; you can redistribute it and/or modify it under 23 | the terms of the GNU General Public License (version 2) as published by the 24 | Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception. 25 | 26 | *************************************************************************** 27 | >>! NOTE: The modification to the GPL is included to allow you to !<< 28 | >>! distribute a combined work that includes FreeRTOS without being !<< 29 | >>! obliged to provide the source code for proprietary components !<< 30 | >>! outside of the FreeRTOS kernel. !<< 31 | *************************************************************************** 32 | 33 | FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY 34 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 35 | FOR A PARTICULAR PURPOSE. Full license text is available on the following 36 | link: http://www.freertos.org/a00114.html 37 | 38 | *************************************************************************** 39 | * * 40 | * FreeRTOS provides completely free yet professionally developed, * 41 | * robust, strictly quality controlled, supported, and cross * 42 | * platform software that is more than just the market leader, it * 43 | * is the industry's de facto standard. * 44 | * * 45 | * Help yourself get started quickly while simultaneously helping * 46 | * to support the FreeRTOS project by purchasing a FreeRTOS * 47 | * tutorial book, reference manual, or both: * 48 | * http://www.FreeRTOS.org/Documentation * 49 | * * 50 | *************************************************************************** 51 | 52 | http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading 53 | the FAQ page "My application does not run, what could be wrong?". Have you 54 | defined configASSERT()? 55 | 56 | http://www.FreeRTOS.org/support - In return for receiving this top quality 57 | embedded software for free we request you assist our global community by 58 | participating in the support forum. 59 | 60 | http://www.FreeRTOS.org/training - Investing in training allows your team to 61 | be as productive as possible as early as possible. Now you can receive 62 | FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers 63 | Ltd, and the world's leading authority on the world's leading RTOS. 64 | 65 | http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products, 66 | including FreeRTOS+Trace - an indispensable productivity tool, a DOS 67 | compatible FAT file system, and our tiny thread aware UDP/IP stack. 68 | 69 | http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate. 70 | Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS. 71 | 72 | http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High 73 | Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS 74 | licenses offer ticketed support, indemnification and commercial middleware. 75 | 76 | http://www.SafeRTOS.com - High Integrity Systems also provide a safety 77 | engineered and independently SIL3 certified version for use in safety and 78 | mission critical applications that require provable dependability. 79 | 80 | 1 tab == 4 spaces! 81 | */ 82 | 83 | #include 84 | #include 85 | 86 | void vLoggingPrintf( const char *pcFormat, ... ) 87 | { 88 | char pcBuffer[256] = {0}; 89 | va_list xArgs; 90 | va_start( xArgs, pcFormat ); 91 | vsnprintf( pcBuffer, sizeof(pcBuffer), pcFormat, xArgs ); 92 | va_end( xArgs ); 93 | printf("%s", pcBuffer); 94 | fflush(stdout); 95 | } 96 | /*-----------------------------------------------------------*/ 97 | -------------------------------------------------------------------------------- /example/tests/simple.c: -------------------------------------------------------------------------------- 1 | /* simple.c 2 | Copyright 2021 Carl John Kugler III 3 | 4 | Licensed under the Apache License, Version 2.0 (the License); you may not use 5 | this file except in compliance with the License. You may obtain a copy of the 6 | License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | Unless required by applicable law or agreed to in writing, software distributed 10 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 12 | specific language governing permissions and limitations under the License. 13 | */ 14 | // Adapted from "FATFileSystem example" 15 | // at https://os.mbed.com/docs/mbed-os/v5.15/apis/fatfilesystem.html 16 | 17 | #include 18 | #include 19 | #include 20 | // 21 | #include "f_util.h" 22 | #include "ff.h" 23 | 24 | // Maximum number of elements in buffer 25 | #define BUFFER_MAX_LEN 10 26 | 27 | #define TRACE_PRINTF(fmt, args...) 28 | //#define TRACE_PRINTF printf 29 | 30 | extern void ls(const char *dir); 31 | 32 | void simple() { 33 | printf("\nSimple Test\n"); 34 | 35 | char cwdbuf[FF_LFN_BUF - 12] = {0}; 36 | FRESULT fr = f_getcwd(cwdbuf, sizeof cwdbuf); 37 | if (FR_OK != fr) { 38 | printf("f_getcwd error: %s (%d)\n", FRESULT_str(fr), fr); 39 | return; 40 | } 41 | // Open the numbers file 42 | printf("Opening \"numbers.txt\"... "); 43 | FIL f; 44 | fr = f_open(&f, "numbers.txt", FA_READ | FA_WRITE); 45 | printf("%s\n", (FR_OK != fr ? "Fail :(" : "OK")); 46 | fflush(stdout); 47 | if (FR_OK != fr && FR_NO_FILE != fr) { 48 | printf("f_open error: %s (%d)\n", FRESULT_str(fr), fr); 49 | return; 50 | } else if (FR_NO_FILE == fr) { 51 | // Create the numbers file if it doesn't exist 52 | printf("No file found, creating a new file... "); 53 | fflush(stdout); 54 | fr = f_open(&f, "numbers.txt", FA_CREATE_ALWAYS | FA_WRITE | FA_READ); 55 | printf("%s\n", (FR_OK != fr ? "Fail :(" : "OK")); 56 | if (FR_OK != fr) printf("f_open error: %s (%d)\n", FRESULT_str(fr), fr); 57 | fflush(stdout); 58 | for (int i = 0; i < 10; i++) { 59 | printf("\rWriting numbers (%d/%d)... ", i, 10); 60 | fflush(stdout); 61 | // When the string was written successfuly, it returns number of 62 | // character encoding units written to the file. When the function 63 | // failed due to disk full or any error, a negative value will be 64 | // returned. 65 | int rc = f_printf(&f, " %d\n", i); 66 | if (rc < 0) { 67 | printf("Fail :(\n"); 68 | printf("f_printf error: %s (%d)\n", FRESULT_str(fr), fr); 69 | } 70 | } 71 | printf("\rWriting numbers (%d/%d)... OK\n", 10, 10); 72 | fflush(stdout); 73 | 74 | printf("Seeking file... "); 75 | fr = f_lseek(&f, 0); 76 | printf("%s\n", (FR_OK != fr ? "Fail :(" : "OK")); 77 | if (FR_OK != fr) 78 | printf("f_lseek error: %s (%d)\n", FRESULT_str(fr), fr); 79 | fflush(stdout); 80 | } 81 | // Go through and increment the numbers 82 | for (int i = 0; i < 10; i++) { 83 | printf("\nIncrementing numbers (%d/%d)... ", i, 10); 84 | 85 | // Get current stream position 86 | long pos = f_tell(&f); 87 | 88 | // Parse out the number and increment 89 | char buf[BUFFER_MAX_LEN]; 90 | if (!f_gets(buf, BUFFER_MAX_LEN, &f)) { 91 | printf("error: f_gets returned NULL\n"); 92 | } 93 | char *endptr; 94 | int32_t number = strtol(buf, &endptr, 10); 95 | if ((endptr == buf) || // No character was read 96 | (*endptr && *endptr != '\n') // The whole input was not converted 97 | ) { 98 | continue; 99 | } 100 | number += 1; 101 | 102 | // Seek to beginning of number 103 | f_lseek(&f, pos); 104 | 105 | // Store number 106 | f_printf(&f, " %d\n", (int)number); 107 | 108 | // Flush between write and read on same file 109 | f_sync(&f); 110 | } 111 | printf("\rIncrementing numbers (%d/%d)... OK\n", 10, 10); 112 | fflush(stdout); 113 | 114 | // Close the file which also flushes any cached writes 115 | printf("Closing \"numbers.txt\"... "); 116 | fr = f_close(&f); 117 | printf("%s\n", (FR_OK != fr ? "Fail :(" : "OK")); 118 | if (FR_OK != fr) printf("f_close error: %s (%d)\n", FRESULT_str(fr), fr); 119 | fflush(stdout); 120 | 121 | ls(""); 122 | 123 | fr = f_chdir("/"); 124 | if (FR_OK != fr) printf("chdir error: %s (%d)\n", FRESULT_str(fr), fr); 125 | 126 | ls(""); 127 | 128 | // Display the numbers file 129 | char pathbuf[FF_LFN_BUF] = {0}; 130 | snprintf(pathbuf, sizeof pathbuf, "%s/%s", cwdbuf, "numbers.txt"); 131 | printf("Opening \"%s\"... ", pathbuf); 132 | fr = f_open(&f, pathbuf, FA_READ); 133 | printf("%s\n", (FR_OK != fr ? "Fail :(" : "OK")); 134 | if (FR_OK != fr) printf("f_open error: %s (%d)\n", FRESULT_str(fr), fr); 135 | fflush(stdout); 136 | 137 | printf("numbers:\n"); 138 | while (!f_eof(&f)) { 139 | // int c = f_getc(f); 140 | char c; 141 | UINT br; 142 | fr = f_read(&f, &c, sizeof c, &br); 143 | if (FR_OK != fr) 144 | printf("f_read error: %s (%d)\n", FRESULT_str(fr), fr); 145 | else 146 | printf("%c", c); 147 | } 148 | 149 | printf("\nClosing \"%s\"... ", pathbuf); 150 | fr = f_close(&f); 151 | printf("%s\n", (FR_OK != fr ? "Fail :(" : "OK")); 152 | if (FR_OK != fr) printf("f_close error: %s (%d)\n", FRESULT_str(fr), fr); 153 | fflush(stdout); 154 | } 155 | -------------------------------------------------------------------------------- /FatFs_SPI/sd_driver/crc.c: -------------------------------------------------------------------------------- 1 | /* crc.c 2 | Copyright 2021 Carl John Kugler III 3 | 4 | Licensed under the Apache License, Version 2.0 (the License); you may not use 5 | this file except in compliance with the License. You may obtain a copy of the 6 | License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | Unless required by applicable law or agreed to in writing, software distributed 10 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 12 | specific language governing permissions and limitations under the License. 13 | */ 14 | /* Derived from: 15 | * SD/MMC File System Library 16 | * Copyright (c) 2016 Neil Thiessen 17 | * 18 | * Licensed under the Apache License, Version 2.0 (the "License"); 19 | * you may not use this file except in compliance with the License. 20 | * You may obtain a copy of the License at 21 | * 22 | * http://www.apache.org/licenses/LICENSE-2.0 23 | * 24 | * Unless required by applicable law or agreed to in writing, software 25 | * distributed under the License is distributed on an "AS IS" BASIS, 26 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 27 | * See the License for the specific language governing permissions and 28 | * limitations under the License. 29 | */ 30 | 31 | #include "crc.h" 32 | 33 | static const char m_Crc7Table[] = {0x00, 0x09, 0x12, 0x1B, 0x24, 0x2D, 0x36, 34 | 0x3F, 0x48, 0x41, 0x5A, 0x53, 0x6C, 0x65, 0x7E, 0x77, 0x19, 0x10, 0x0B, 35 | 0x02, 0x3D, 0x34, 0x2F, 0x26, 0x51, 0x58, 0x43, 0x4A, 0x75, 0x7C, 0x67, 36 | 0x6E, 0x32, 0x3B, 0x20, 0x29, 0x16, 0x1F, 0x04, 0x0D, 0x7A, 0x73, 0x68, 37 | 0x61, 0x5E, 0x57, 0x4C, 0x45, 0x2B, 0x22, 0x39, 0x30, 0x0F, 0x06, 0x1D, 38 | 0x14, 0x63, 0x6A, 0x71, 0x78, 0x47, 0x4E, 0x55, 0x5C, 0x64, 0x6D, 0x76, 39 | 0x7F, 0x40, 0x49, 0x52, 0x5B, 0x2C, 0x25, 0x3E, 0x37, 0x08, 0x01, 0x1A, 40 | 0x13, 0x7D, 0x74, 0x6F, 0x66, 0x59, 0x50, 0x4B, 0x42, 0x35, 0x3C, 0x27, 41 | 0x2E, 0x11, 0x18, 0x03, 0x0A, 0x56, 0x5F, 0x44, 0x4D, 0x72, 0x7B, 0x60, 42 | 0x69, 0x1E, 0x17, 0x0C, 0x05, 0x3A, 0x33, 0x28, 0x21, 0x4F, 0x46, 0x5D, 43 | 0x54, 0x6B, 0x62, 0x79, 0x70, 0x07, 0x0E, 0x15, 0x1C, 0x23, 0x2A, 0x31, 44 | 0x38, 0x41, 0x48, 0x53, 0x5A, 0x65, 0x6C, 0x77, 0x7E, 0x09, 0x00, 0x1B, 45 | 0x12, 0x2D, 0x24, 0x3F, 0x36, 0x58, 0x51, 0x4A, 0x43, 0x7C, 0x75, 0x6E, 46 | 0x67, 0x10, 0x19, 0x02, 0x0B, 0x34, 0x3D, 0x26, 0x2F, 0x73, 0x7A, 0x61, 47 | 0x68, 0x57, 0x5E, 0x45, 0x4C, 0x3B, 0x32, 0x29, 0x20, 0x1F, 0x16, 0x0D, 48 | 0x04, 0x6A, 0x63, 0x78, 0x71, 0x4E, 0x47, 0x5C, 0x55, 0x22, 0x2B, 0x30, 49 | 0x39, 0x06, 0x0F, 0x14, 0x1D, 0x25, 0x2C, 0x37, 0x3E, 0x01, 0x08, 0x13, 50 | 0x1A, 0x6D, 0x64, 0x7F, 0x76, 0x49, 0x40, 0x5B, 0x52, 0x3C, 0x35, 0x2E, 51 | 0x27, 0x18, 0x11, 0x0A, 0x03, 0x74, 0x7D, 0x66, 0x6F, 0x50, 0x59, 0x42, 52 | 0x4B, 0x17, 0x1E, 0x05, 0x0C, 0x33, 0x3A, 0x21, 0x28, 0x5F, 0x56, 0x4D, 53 | 0x44, 0x7B, 0x72, 0x69, 0x60, 0x0E, 0x07, 0x1C, 0x15, 0x2A, 0x23, 0x38, 54 | 0x31, 0x46, 0x4F, 0x54, 0x5D, 0x62, 0x6B, 0x70, 0x79}; 55 | 56 | static const unsigned short m_Crc16Table[256] = {0x0000, 0x1021, 0x2042, 57 | 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7, 0x8108, 0x9129, 0xA14A, 0xB16B, 58 | 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF, 0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 59 | 0x4294, 0x72F7, 0x62D6, 0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 60 | 0xF3FF, 0xE3DE, 0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 61 | 0x5485, 0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D, 62 | 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4, 0xB75B, 63 | 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC, 0x48C4, 0x58E5, 64 | 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823, 0xC9CC, 0xD9ED, 0xE98E, 65 | 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B, 0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 66 | 0x1A71, 0x0A50, 0x3A33, 0x2A12, 0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 67 | 0x8B58, 0xBB3B, 0xAB1A, 0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 68 | 0x0C60, 0x1C41, 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 69 | 0x9D49, 0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70, 70 | 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78, 0x9188, 71 | 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F, 0x1080, 0x00A1, 72 | 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067, 0x83B9, 0x9398, 0xA3FB, 73 | 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E, 0x02B1, 0x1290, 0x22F3, 0x32D2, 74 | 0x4235, 0x5214, 0x6277, 0x7256, 0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 75 | 0xE54F, 0xD52C, 0xC50D, 0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 76 | 0x5424, 0x4405, 0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 77 | 0xD73C, 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634, 78 | 0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB, 0x5844, 79 | 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3, 0xCB7D, 0xDB5C, 80 | 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A, 0x4A75, 0x5A54, 0x6A37, 81 | 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92, 0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 82 | 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9, 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 83 | 0x2C83, 0x1CE0, 0x0CC1, 0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 84 | 0x8FD9, 0x9FF8, 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 85 | 0x1EF0}; 86 | 87 | char crc7(const char* data, int length) 88 | { 89 | //Calculate the CRC7 checksum for the specified data block 90 | char crc = 0; 91 | for (int i = 0; i < length; i++) { 92 | crc = m_Crc7Table[(crc << 1) ^ data[i]]; 93 | } 94 | 95 | //Return the calculated checksum 96 | return crc; 97 | } 98 | 99 | unsigned short crc16(const char* data, int length) 100 | { 101 | //Calculate the CRC16 checksum for the specified data block 102 | unsigned short crc = 0; 103 | for (int i = 0; i < length; i++) { 104 | crc = (crc << 8) ^ m_Crc16Table[((crc >> 8) ^ data[i]) & 0x00FF]; 105 | } 106 | 107 | //Return the calculated checksum 108 | return crc; 109 | } 110 | 111 | void update_crc16(unsigned short *pCrc16, const char data[], size_t length) { 112 | for (size_t i = 0; i < length; i++) { 113 | *pCrc16 = (*pCrc16 << 8) ^ m_Crc16Table[((*pCrc16 >> 8) ^ data[i]) & 0x00FF]; 114 | } 115 | } 116 | /* [] END OF FILE */ 117 | -------------------------------------------------------------------------------- /FatFs_SPI/ff15/source/diskio.c: -------------------------------------------------------------------------------- 1 | /*-----------------------------------------------------------------------*/ 2 | /* Low level disk I/O module SKELETON for FatFs (C)ChaN, 2019 */ 3 | /*-----------------------------------------------------------------------*/ 4 | /* If a working storage control module is available, it should be */ 5 | /* attached to the FatFs via a glue function rather than modifying it. */ 6 | /* This is an example of glue functions to attach various exsisting */ 7 | /* storage control modules to the FatFs module with a defined API. */ 8 | /*-----------------------------------------------------------------------*/ 9 | 10 | #include "ff.h" /* Obtains integer types */ 11 | #include "diskio.h" /* Declarations of disk functions */ 12 | 13 | /* Definitions of physical drive number for each drive */ 14 | #define DEV_RAM 0 /* Example: Map Ramdisk to physical drive 0 */ 15 | #define DEV_MMC 1 /* Example: Map MMC/SD card to physical drive 1 */ 16 | #define DEV_USB 2 /* Example: Map USB MSD to physical drive 2 */ 17 | 18 | 19 | /*-----------------------------------------------------------------------*/ 20 | /* Get Drive Status */ 21 | /*-----------------------------------------------------------------------*/ 22 | 23 | DSTATUS disk_status ( 24 | BYTE pdrv /* Physical drive nmuber to identify the drive */ 25 | ) 26 | { 27 | DSTATUS stat; 28 | int result; 29 | 30 | switch (pdrv) { 31 | case DEV_RAM : 32 | result = RAM_disk_status(); 33 | 34 | // translate the reslut code here 35 | 36 | return stat; 37 | 38 | case DEV_MMC : 39 | result = MMC_disk_status(); 40 | 41 | // translate the reslut code here 42 | 43 | return stat; 44 | 45 | case DEV_USB : 46 | result = USB_disk_status(); 47 | 48 | // translate the reslut code here 49 | 50 | return stat; 51 | } 52 | return STA_NOINIT; 53 | } 54 | 55 | 56 | 57 | /*-----------------------------------------------------------------------*/ 58 | /* Inidialize a Drive */ 59 | /*-----------------------------------------------------------------------*/ 60 | 61 | DSTATUS disk_initialize ( 62 | BYTE pdrv /* Physical drive nmuber to identify the drive */ 63 | ) 64 | { 65 | DSTATUS stat; 66 | int result; 67 | 68 | switch (pdrv) { 69 | case DEV_RAM : 70 | result = RAM_disk_initialize(); 71 | 72 | // translate the reslut code here 73 | 74 | return stat; 75 | 76 | case DEV_MMC : 77 | result = MMC_disk_initialize(); 78 | 79 | // translate the reslut code here 80 | 81 | return stat; 82 | 83 | case DEV_USB : 84 | result = USB_disk_initialize(); 85 | 86 | // translate the reslut code here 87 | 88 | return stat; 89 | } 90 | return STA_NOINIT; 91 | } 92 | 93 | 94 | 95 | /*-----------------------------------------------------------------------*/ 96 | /* Read Sector(s) */ 97 | /*-----------------------------------------------------------------------*/ 98 | 99 | DRESULT disk_read ( 100 | BYTE pdrv, /* Physical drive nmuber to identify the drive */ 101 | BYTE *buff, /* Data buffer to store read data */ 102 | LBA_t sector, /* Start sector in LBA */ 103 | UINT count /* Number of sectors to read */ 104 | ) 105 | { 106 | DRESULT res; 107 | int result; 108 | 109 | switch (pdrv) { 110 | case DEV_RAM : 111 | // translate the arguments here 112 | 113 | result = RAM_disk_read(buff, sector, count); 114 | 115 | // translate the reslut code here 116 | 117 | return res; 118 | 119 | case DEV_MMC : 120 | // translate the arguments here 121 | 122 | result = MMC_disk_read(buff, sector, count); 123 | 124 | // translate the reslut code here 125 | 126 | return res; 127 | 128 | case DEV_USB : 129 | // translate the arguments here 130 | 131 | result = USB_disk_read(buff, sector, count); 132 | 133 | // translate the reslut code here 134 | 135 | return res; 136 | } 137 | 138 | return RES_PARERR; 139 | } 140 | 141 | 142 | 143 | /*-----------------------------------------------------------------------*/ 144 | /* Write Sector(s) */ 145 | /*-----------------------------------------------------------------------*/ 146 | 147 | #if FF_FS_READONLY == 0 148 | 149 | DRESULT disk_write ( 150 | BYTE pdrv, /* Physical drive nmuber to identify the drive */ 151 | const BYTE *buff, /* Data to be written */ 152 | LBA_t sector, /* Start sector in LBA */ 153 | UINT count /* Number of sectors to write */ 154 | ) 155 | { 156 | DRESULT res; 157 | int result; 158 | 159 | switch (pdrv) { 160 | case DEV_RAM : 161 | // translate the arguments here 162 | 163 | result = RAM_disk_write(buff, sector, count); 164 | 165 | // translate the reslut code here 166 | 167 | return res; 168 | 169 | case DEV_MMC : 170 | // translate the arguments here 171 | 172 | result = MMC_disk_write(buff, sector, count); 173 | 174 | // translate the reslut code here 175 | 176 | return res; 177 | 178 | case DEV_USB : 179 | // translate the arguments here 180 | 181 | result = USB_disk_write(buff, sector, count); 182 | 183 | // translate the reslut code here 184 | 185 | return res; 186 | } 187 | 188 | return RES_PARERR; 189 | } 190 | 191 | #endif 192 | 193 | 194 | /*-----------------------------------------------------------------------*/ 195 | /* Miscellaneous Functions */ 196 | /*-----------------------------------------------------------------------*/ 197 | 198 | DRESULT disk_ioctl ( 199 | BYTE pdrv, /* Physical drive nmuber (0..) */ 200 | BYTE cmd, /* Control code */ 201 | void *buff /* Buffer to send/receive control data */ 202 | ) 203 | { 204 | DRESULT res; 205 | int result; 206 | 207 | switch (pdrv) { 208 | case DEV_RAM : 209 | 210 | // Process of the command for the RAM drive 211 | 212 | return res; 213 | 214 | case DEV_MMC : 215 | 216 | // Process of the command for the MMC/SD card 217 | 218 | return res; 219 | 220 | case DEV_USB : 221 | 222 | // Process of the command the USB drive 223 | 224 | return res; 225 | } 226 | 227 | return RES_PARERR; 228 | } 229 | 230 | -------------------------------------------------------------------------------- /FatFs_SPI/ff15/source/ffsystem.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------*/ 2 | /* A Sample Code of User Provided OS Dependent Functions for FatFs */ 3 | /*------------------------------------------------------------------------*/ 4 | 5 | #include "ff.h" 6 | 7 | 8 | #if FF_USE_LFN == 3 /* Use dynamic memory allocation */ 9 | 10 | /*------------------------------------------------------------------------*/ 11 | /* Allocate/Free a Memory Block */ 12 | /*------------------------------------------------------------------------*/ 13 | 14 | #include /* with POSIX API */ 15 | 16 | 17 | void* ff_memalloc ( /* Returns pointer to the allocated memory block (null if not enough core) */ 18 | UINT msize /* Number of bytes to allocate */ 19 | ) 20 | { 21 | return malloc((size_t)msize); /* Allocate a new memory block */ 22 | } 23 | 24 | 25 | void ff_memfree ( 26 | void* mblock /* Pointer to the memory block to free (no effect if null) */ 27 | ) 28 | { 29 | free(mblock); /* Free the memory block */ 30 | } 31 | 32 | #endif 33 | 34 | 35 | 36 | 37 | #if FF_FS_REENTRANT /* Mutal exclusion */ 38 | /*------------------------------------------------------------------------*/ 39 | /* Definitions of Mutex */ 40 | /*------------------------------------------------------------------------*/ 41 | 42 | #define OS_TYPE 0 /* 0:Win32, 1:uITRON4.0, 2:uC/OS-II, 3:FreeRTOS, 4:CMSIS-RTOS */ 43 | 44 | 45 | #if OS_TYPE == 0 /* Win32 */ 46 | #include 47 | static HANDLE Mutex[FF_VOLUMES + 1]; /* Table of mutex handle */ 48 | 49 | #elif OS_TYPE == 1 /* uITRON */ 50 | #include "itron.h" 51 | #include "kernel.h" 52 | static mtxid Mutex[FF_VOLUMES + 1]; /* Table of mutex ID */ 53 | 54 | #elif OS_TYPE == 2 /* uc/OS-II */ 55 | #include "includes.h" 56 | static OS_EVENT *Mutex[FF_VOLUMES + 1]; /* Table of mutex pinter */ 57 | 58 | #elif OS_TYPE == 3 /* FreeRTOS */ 59 | #include "FreeRTOS.h" 60 | #include "semphr.h" 61 | static SemaphoreHandle_t Mutex[FF_VOLUMES + 1]; /* Table of mutex handle */ 62 | 63 | #elif OS_TYPE == 4 /* CMSIS-RTOS */ 64 | #include "cmsis_os.h" 65 | static osMutexId Mutex[FF_VOLUMES + 1]; /* Table of mutex ID */ 66 | 67 | #endif 68 | 69 | 70 | 71 | /*------------------------------------------------------------------------*/ 72 | /* Create a Mutex */ 73 | /*------------------------------------------------------------------------*/ 74 | /* This function is called in f_mount function to create a new mutex 75 | / or semaphore for the volume. When a 0 is returned, the f_mount function 76 | / fails with FR_INT_ERR. 77 | */ 78 | 79 | int ff_mutex_create ( /* Returns 1:Function succeeded or 0:Could not create the mutex */ 80 | int vol /* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */ 81 | ) 82 | { 83 | #if OS_TYPE == 0 /* Win32 */ 84 | Mutex[vol] = CreateMutex(NULL, FALSE, NULL); 85 | return (int)(Mutex[vol] != INVALID_HANDLE_VALUE); 86 | 87 | #elif OS_TYPE == 1 /* uITRON */ 88 | T_CMTX cmtx = {TA_TPRI,1}; 89 | 90 | Mutex[vol] = acre_mtx(&cmtx); 91 | return (int)(Mutex[vol] > 0); 92 | 93 | #elif OS_TYPE == 2 /* uC/OS-II */ 94 | OS_ERR err; 95 | 96 | Mutex[vol] = OSMutexCreate(0, &err); 97 | return (int)(err == OS_NO_ERR); 98 | 99 | #elif OS_TYPE == 3 /* FreeRTOS */ 100 | Mutex[vol] = xSemaphoreCreateMutex(); 101 | return (int)(Mutex[vol] != NULL); 102 | 103 | #elif OS_TYPE == 4 /* CMSIS-RTOS */ 104 | osMutexDef(cmsis_os_mutex); 105 | 106 | Mutex[vol] = osMutexCreate(osMutex(cmsis_os_mutex)); 107 | return (int)(Mutex[vol] != NULL); 108 | 109 | #endif 110 | } 111 | 112 | 113 | /*------------------------------------------------------------------------*/ 114 | /* Delete a Mutex */ 115 | /*------------------------------------------------------------------------*/ 116 | /* This function is called in f_mount function to delete a mutex or 117 | / semaphore of the volume created with ff_mutex_create function. 118 | */ 119 | 120 | void ff_mutex_delete ( /* Returns 1:Function succeeded or 0:Could not delete due to an error */ 121 | int vol /* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */ 122 | ) 123 | { 124 | #if OS_TYPE == 0 /* Win32 */ 125 | CloseHandle(Mutex[vol]); 126 | 127 | #elif OS_TYPE == 1 /* uITRON */ 128 | del_mtx(Mutex[vol]); 129 | 130 | #elif OS_TYPE == 2 /* uC/OS-II */ 131 | OS_ERR err; 132 | 133 | OSMutexDel(Mutex[vol], OS_DEL_ALWAYS, &err); 134 | 135 | #elif OS_TYPE == 3 /* FreeRTOS */ 136 | vSemaphoreDelete(Mutex[vol]); 137 | 138 | #elif OS_TYPE == 4 /* CMSIS-RTOS */ 139 | osMutexDelete(Mutex[vol]); 140 | 141 | #endif 142 | } 143 | 144 | 145 | /*------------------------------------------------------------------------*/ 146 | /* Request a Grant to Access the Volume */ 147 | /*------------------------------------------------------------------------*/ 148 | /* This function is called on enter file functions to lock the volume. 149 | / When a 0 is returned, the file function fails with FR_TIMEOUT. 150 | */ 151 | 152 | int ff_mutex_take ( /* Returns 1:Succeeded or 0:Timeout */ 153 | int vol /* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */ 154 | ) 155 | { 156 | #if OS_TYPE == 0 /* Win32 */ 157 | return (int)(WaitForSingleObject(Mutex[vol], FF_FS_TIMEOUT) == WAIT_OBJECT_0); 158 | 159 | #elif OS_TYPE == 1 /* uITRON */ 160 | return (int)(tloc_mtx(Mutex[vol], FF_FS_TIMEOUT) == E_OK); 161 | 162 | #elif OS_TYPE == 2 /* uC/OS-II */ 163 | OS_ERR err; 164 | 165 | OSMutexPend(Mutex[vol], FF_FS_TIMEOUT, &err)); 166 | return (int)(err == OS_NO_ERR); 167 | 168 | #elif OS_TYPE == 3 /* FreeRTOS */ 169 | return (int)(xSemaphoreTake(Mutex[vol], FF_FS_TIMEOUT) == pdTRUE); 170 | 171 | #elif OS_TYPE == 4 /* CMSIS-RTOS */ 172 | return (int)(osMutexWait(Mutex[vol], FF_FS_TIMEOUT) == osOK); 173 | 174 | #endif 175 | } 176 | 177 | 178 | 179 | /*------------------------------------------------------------------------*/ 180 | /* Release a Grant to Access the Volume */ 181 | /*------------------------------------------------------------------------*/ 182 | /* This function is called on leave file functions to unlock the volume. 183 | */ 184 | 185 | void ff_mutex_give ( 186 | int vol /* Mutex ID: Volume mutex (0 to FF_VOLUMES - 1) or system mutex (FF_VOLUMES) */ 187 | ) 188 | { 189 | #if OS_TYPE == 0 /* Win32 */ 190 | ReleaseMutex(Mutex[vol]); 191 | 192 | #elif OS_TYPE == 1 /* uITRON */ 193 | unl_mtx(Mutex[vol]); 194 | 195 | #elif OS_TYPE == 2 /* uC/OS-II */ 196 | OSMutexPost(Mutex[vol]); 197 | 198 | #elif OS_TYPE == 3 /* FreeRTOS */ 199 | xSemaphoreGive(Mutex[vol]); 200 | 201 | #elif OS_TYPE == 4 /* CMSIS-RTOS */ 202 | osMutexRelease(Mutex[vol]); 203 | 204 | #endif 205 | } 206 | 207 | #endif /* FF_FS_REENTRANT */ 208 | 209 | -------------------------------------------------------------------------------- /FatFs_SPI/src/glue.c: -------------------------------------------------------------------------------- 1 | /* glue.c 2 | Copyright 2021 Carl John Kugler III 3 | 4 | Licensed under the Apache License, Version 2.0 (the License); you may not use 5 | this file except in compliance with the License. You may obtain a copy of the 6 | License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | Unless required by applicable law or agreed to in writing, software distributed 10 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 12 | specific language governing permissions and limitations under the License. 13 | */ 14 | /*-----------------------------------------------------------------------*/ 15 | /* Low level disk I/O module SKELETON for FatFs (C)ChaN, 2019 */ 16 | /*-----------------------------------------------------------------------*/ 17 | /* If a working storage control module is available, it should be */ 18 | /* attached to the FatFs via a glue function rather than modifying it. */ 19 | /* This is an example of glue functions to attach various exsisting */ 20 | /* storage control modules to the FatFs module with a defined API. */ 21 | /*-----------------------------------------------------------------------*/ 22 | #include 23 | // 24 | #include "ff.h" /* Obtains integer types */ 25 | // 26 | #include "diskio.h" /* Declarations of disk functions */ 27 | // 28 | #include "hw_config.h" 29 | #include "my_debug.h" 30 | #include "sd_card.h" 31 | 32 | #define TRACE_PRINTF(fmt, args...) 33 | //#define TRACE_PRINTF printf // task_printf 34 | 35 | /*-----------------------------------------------------------------------*/ 36 | /* Get Drive Status */ 37 | /*-----------------------------------------------------------------------*/ 38 | 39 | DSTATUS disk_status(BYTE pdrv /* Physical drive nmuber to identify the drive */ 40 | ) { 41 | TRACE_PRINTF(">>> %s\n", __FUNCTION__); 42 | sd_card_t *p_sd = sd_get_by_num(pdrv); 43 | if (!p_sd) return RES_PARERR; 44 | sd_card_detect(p_sd); // Fast: just a GPIO read 45 | return p_sd->m_Status; // See http://elm-chan.org/fsw/ff/doc/dstat.html 46 | } 47 | 48 | /*-----------------------------------------------------------------------*/ 49 | /* Inidialize a Drive */ 50 | /*-----------------------------------------------------------------------*/ 51 | 52 | DSTATUS disk_initialize( 53 | BYTE pdrv /* Physical drive nmuber to identify the drive */ 54 | ) { 55 | TRACE_PRINTF(">>> %s\n", __FUNCTION__); 56 | 57 | bool rc = sd_init_driver(); 58 | if (!rc) return RES_NOTRDY; 59 | 60 | sd_card_t *p_sd = sd_get_by_num(pdrv); 61 | if (!p_sd) return RES_PARERR; 62 | // See http://elm-chan.org/fsw/ff/doc/dstat.html 63 | return p_sd->init(p_sd); 64 | } 65 | 66 | static int sdrc2dresult(int sd_rc) { 67 | switch (sd_rc) { 68 | case SD_BLOCK_DEVICE_ERROR_NONE: 69 | return RES_OK; 70 | case SD_BLOCK_DEVICE_ERROR_UNUSABLE: 71 | case SD_BLOCK_DEVICE_ERROR_NO_RESPONSE: 72 | case SD_BLOCK_DEVICE_ERROR_NO_INIT: 73 | case SD_BLOCK_DEVICE_ERROR_NO_DEVICE: 74 | return RES_NOTRDY; 75 | case SD_BLOCK_DEVICE_ERROR_PARAMETER: 76 | case SD_BLOCK_DEVICE_ERROR_UNSUPPORTED: 77 | return RES_PARERR; 78 | case SD_BLOCK_DEVICE_ERROR_WRITE_PROTECTED: 79 | return RES_WRPRT; 80 | case SD_BLOCK_DEVICE_ERROR_CRC: 81 | case SD_BLOCK_DEVICE_ERROR_WOULD_BLOCK: 82 | case SD_BLOCK_DEVICE_ERROR_ERASE: 83 | case SD_BLOCK_DEVICE_ERROR_WRITE: 84 | default: 85 | return RES_ERROR; 86 | } 87 | } 88 | 89 | /*-----------------------------------------------------------------------*/ 90 | /* Read Sector(s) */ 91 | /*-----------------------------------------------------------------------*/ 92 | 93 | DRESULT disk_read(BYTE pdrv, /* Physical drive nmuber to identify the drive */ 94 | BYTE *buff, /* Data buffer to store read data */ 95 | LBA_t sector, /* Start sector in LBA */ 96 | UINT count /* Number of sectors to read */ 97 | ) { 98 | TRACE_PRINTF(">>> %s\n", __FUNCTION__); 99 | sd_card_t *p_sd = sd_get_by_num(pdrv); 100 | if (!p_sd) return RES_PARERR; 101 | int rc = p_sd->read_blocks(p_sd, buff, sector, count); 102 | return sdrc2dresult(rc); 103 | } 104 | 105 | /*-----------------------------------------------------------------------*/ 106 | /* Write Sector(s) */ 107 | /*-----------------------------------------------------------------------*/ 108 | 109 | #if FF_FS_READONLY == 0 110 | 111 | DRESULT disk_write(BYTE pdrv, /* Physical drive nmuber to identify the drive */ 112 | const BYTE *buff, /* Data to be written */ 113 | LBA_t sector, /* Start sector in LBA */ 114 | UINT count /* Number of sectors to write */ 115 | ) { 116 | TRACE_PRINTF(">>> %s\n", __FUNCTION__); 117 | sd_card_t *p_sd = sd_get_by_num(pdrv); 118 | if (!p_sd) return RES_PARERR; 119 | int rc = p_sd->write_blocks(p_sd, buff, sector, count); 120 | return sdrc2dresult(rc); 121 | } 122 | 123 | #endif 124 | 125 | /*-----------------------------------------------------------------------*/ 126 | /* Miscellaneous Functions */ 127 | /*-----------------------------------------------------------------------*/ 128 | 129 | DRESULT disk_ioctl(BYTE pdrv, /* Physical drive nmuber (0..) */ 130 | BYTE cmd, /* Control code */ 131 | void *buff /* Buffer to send/receive control data */ 132 | ) { 133 | TRACE_PRINTF(">>> %s\n", __FUNCTION__); 134 | sd_card_t *p_sd = sd_get_by_num(pdrv); 135 | if (!p_sd) return RES_PARERR; 136 | switch (cmd) { 137 | case GET_SECTOR_COUNT: { // Retrieves number of available sectors, the 138 | // largest allowable LBA + 1, on the drive 139 | // into the LBA_t variable pointed by buff. 140 | // This command is used by f_mkfs and f_fdisk 141 | // function to determine the size of 142 | // volume/partition to be created. It is 143 | // required when FF_USE_MKFS == 1. 144 | static LBA_t n; 145 | n = sd_sectors(p_sd); 146 | *(LBA_t *)buff = n; 147 | if (!n) return RES_ERROR; 148 | return RES_OK; 149 | } 150 | case GET_BLOCK_SIZE: { // Retrieves erase block size of the flash 151 | // memory media in unit of sector into the DWORD 152 | // variable pointed by buff. The allowable value 153 | // is 1 to 32768 in power of 2. Return 1 if the 154 | // erase block size is unknown or non flash 155 | // memory media. This command is used by only 156 | // f_mkfs function and it attempts to align data 157 | // area on the erase block boundary. It is 158 | // required when FF_USE_MKFS == 1. 159 | static DWORD bs = 1; 160 | *(DWORD *)buff = bs; 161 | return RES_OK; 162 | } 163 | case CTRL_SYNC: 164 | return RES_OK; 165 | default: 166 | return RES_PARERR; 167 | } 168 | } 169 | -------------------------------------------------------------------------------- /example/tests/big_file_test.c: -------------------------------------------------------------------------------- 1 | /* big_file_test.c 2 | Copyright 2021 Carl John Kugler III 3 | 4 | Licensed under the Apache License, Version 2.0 (the License); you may not use 5 | this file except in compliance with the License. You may obtain a copy of the 6 | License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | Unless required by applicable law or agreed to in writing, software distributed 10 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 12 | specific language governing permissions and limitations under the License. 13 | */ 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | // 23 | #include "pico/stdlib.h" 24 | // 25 | //#include "ff_headers.h" 26 | #include "ff_stdio.h" 27 | 28 | #define FF_MAX_SS 512 29 | #define BUFFSZ 8 * 1024 30 | 31 | #undef assert 32 | #define assert configASSERT 33 | #define fopen ff_fopen 34 | #define fwrite ff_fwrite 35 | #define fread ff_fread 36 | #define fclose ff_fclose 37 | #ifndef FF_DEFINED 38 | #define errno stdioGET_ERRNO() 39 | #define free vPortFree 40 | #define malloc pvPortMalloc 41 | #endif 42 | 43 | typedef uint32_t DWORD; 44 | typedef unsigned int UINT; 45 | 46 | // Borrowed from http://elm-chan.org/fsw/ff/res/app4.c 47 | static DWORD pn(/* Pseudo random number generator */ 48 | DWORD pns /* 0:Initialize, !0:Read */ // Is that right? -- CK3 49 | ) { 50 | static DWORD lfsr; 51 | UINT n; 52 | 53 | if (pns) { 54 | lfsr = pns; 55 | for (n = 0; n < 32; n++) pn(0); 56 | } 57 | if (lfsr & 1) { 58 | lfsr >>= 1; 59 | lfsr ^= 0x80200003; 60 | } else { 61 | lfsr >>= 1; 62 | } 63 | return lfsr; 64 | } 65 | 66 | // Create a file of size "size" bytes filled with random data seeded with "seed" 67 | static bool create_big_file(const char *const pathname, size_t size, 68 | unsigned seed) { 69 | int32_t lItems; 70 | FF_FILE *pxFile; 71 | 72 | // DWORD buff[FF_MAX_SS]; /* Working buffer (4 sector in size) */ 73 | size_t bufsz = size < BUFFSZ ? size : BUFFSZ; 74 | assert(0 == size % bufsz); 75 | DWORD *buff = malloc(bufsz); 76 | assert(buff); 77 | 78 | pn(seed); // See pseudo-random number generator 79 | 80 | printf("Writing...\n"); 81 | absolute_time_t xStart = get_absolute_time(); 82 | 83 | /* Open the file, creating the file if it does not already exist. */ 84 | FF_Stat_t xStat; 85 | size_t fsz = 0; 86 | if (ff_stat(pathname, &xStat) == 0) fsz = xStat.st_size; 87 | if (0 < fsz && fsz <= size) { 88 | // This is an attempt at optimization: 89 | // rewriting the file should be faster than 90 | // writing it from scratch. 91 | pxFile = ff_fopen(pathname, "r+"); 92 | ff_rewind(pxFile); 93 | } else { 94 | pxFile = ff_fopen(pathname, "w"); 95 | } 96 | // // FF_FILE *ff_truncate( const char * pcFileName, long lTruncateSize 97 | // ); 98 | // // If the file was shorter than lTruncateSize 99 | // // then new data added to the end of the file is set to 0. 100 | // pxFile = ff_truncate(pathname, size); 101 | if (!pxFile) { 102 | printf("ff_fopen(%s): %s (%d)\n", pathname, strerror(errno), errno); 103 | return false; 104 | } 105 | assert(pxFile); 106 | 107 | size_t i; 108 | for (i = 0; i < size / bufsz; ++i) { 109 | size_t n; 110 | for (n = 0; n < bufsz / sizeof(DWORD); n++) buff[n] = pn(0); 111 | lItems = fwrite(buff, bufsz, 1, pxFile); 112 | if (lItems < 1) 113 | printf("fwrite(%s): %s (%d)\n", pathname, strerror(errno), errno); 114 | assert(lItems == 1); 115 | } 116 | free(buff); 117 | /* Close the file. */ 118 | ff_fclose(pxFile); 119 | 120 | int64_t elapsed_us = absolute_time_diff_us(xStart, get_absolute_time()); 121 | float elapsed = elapsed_us / 1E6; 122 | printf("Elapsed seconds %.3g\n", elapsed); 123 | printf("Transfer rate %.3g KiB/s\n", (double)size / elapsed / 1024); 124 | return true; 125 | } 126 | 127 | // Read a file of size "size" bytes filled with random data seeded with "seed" 128 | // and verify the data 129 | static void check_big_file(const char *const pathname, size_t size, 130 | uint32_t seed) { 131 | int32_t lItems; 132 | FF_FILE *pxFile; 133 | 134 | // DWORD buff[FF_MAX_SS]; /* Working buffer (4 sector in size) */ 135 | // assert(0 == size % sizeof(buff)); 136 | size_t bufsz = size < BUFFSZ ? size : BUFFSZ; 137 | assert(0 == size % bufsz); 138 | DWORD *buff = malloc(bufsz); 139 | assert(buff); 140 | 141 | pn(seed); 142 | 143 | /* Open the file, creating the file if it does not already exist. */ 144 | pxFile = fopen(pathname, "r"); 145 | if (!pxFile) 146 | printf("fopen(%s): %s (%d)\n", pathname, strerror(errno), -errno); 147 | assert(pxFile); 148 | 149 | printf("Reading...\n"); 150 | absolute_time_t xStart = get_absolute_time(); 151 | 152 | size_t i; 153 | for (i = 0; i < size / bufsz; ++i) { 154 | lItems = fread(buff, bufsz, 1, pxFile); 155 | if (lItems < 1) 156 | printf("fread(%s): %s (%d)\n", pathname, strerror(errno), -errno); 157 | assert(lItems == 1); 158 | 159 | /* Check the buffer is filled with the expected data. */ 160 | size_t n; 161 | for (n = 0; n < bufsz / sizeof(DWORD); n++) { 162 | unsigned int expected = pn(0); 163 | unsigned int val = buff[n]; 164 | if (val != expected) 165 | printf("Data mismatch at dword %u: expected=0x%8x val=0x%8x\n", 166 | (i * sizeof(buff)) + n, expected, val); 167 | } 168 | } 169 | free(buff); 170 | /* Close the file. */ 171 | ff_fclose(pxFile); 172 | 173 | int64_t elapsed_us = absolute_time_diff_us(xStart, get_absolute_time()); 174 | float elapsed = elapsed_us / 1E6; 175 | printf("Elapsed seconds %.3g\n", elapsed); 176 | printf("Transfer rate %.3g KiB/s\n", (double)size / elapsed / 1024); 177 | } 178 | 179 | // Create a file of size "size" bytes filled with random data seeded with "seed" 180 | // static 181 | void create_big_file_v1(const char *const pathname, size_t size, 182 | unsigned seed) { 183 | int32_t lItems; 184 | FF_FILE *pxFile; 185 | int val; 186 | 187 | assert(0 == size % sizeof(int)); 188 | 189 | srand(seed); 190 | 191 | /* Open the file, creating the file if it does not already exist. */ 192 | pxFile = ff_fopen(pathname, "w"); 193 | if (!pxFile) 194 | printf("ff_fopen(%s): %s (%d)\n", pathname, strerror(errno), errno); 195 | assert(pxFile); 196 | 197 | printf("Writing...\n"); 198 | absolute_time_t xStart = get_absolute_time(); 199 | 200 | size_t i; 201 | for (i = 0; i < size / sizeof(val); ++i) { 202 | val = rand(); 203 | lItems = ff_fwrite(&val, sizeof(val), 1, pxFile); 204 | if (lItems < 1) 205 | printf("ff_fwrite(%s): %s (%d)\n", pathname, strerror(errno), 206 | errno); 207 | assert(lItems == 1); 208 | } 209 | /* Close the file. */ 210 | ff_fclose(pxFile); 211 | 212 | int64_t elapsed_us = absolute_time_diff_us(xStart, get_absolute_time()); 213 | float elapsed = elapsed_us / 1E6; 214 | printf("Elapsed seconds %.3g\n", elapsed); 215 | } 216 | 217 | // Read a file of size "size" bytes filled with random data seeded with "seed" 218 | // and verify the data 219 | // static 220 | void check_big_file_v1(const char *const pathname, size_t size, uint32_t seed) { 221 | int32_t lItems; 222 | FF_FILE *pxFile; 223 | 224 | assert(0 == size % sizeof(int)); 225 | 226 | srand(seed); 227 | 228 | /* Open the file, creating the file if it does not already exist. */ 229 | pxFile = ff_fopen(pathname, "r"); 230 | if (!pxFile) 231 | printf("ff_fopen(%s): %s (%d)\n", pathname, strerror(errno), errno); 232 | assert(pxFile); 233 | 234 | printf("Reading...\n"); 235 | absolute_time_t xStart = get_absolute_time(); 236 | 237 | size_t i; 238 | int val; 239 | for (i = 0; i < size / sizeof(val); ++i) { 240 | lItems = ff_fread(&val, sizeof(val), 1, pxFile); 241 | if (lItems < 1) 242 | printf("ff_fread(%s): %s (%d)\n", pathname, strerror(errno), errno); 243 | assert(lItems == 1); 244 | 245 | /* Check the buffer is filled with the expected data. */ 246 | int expected = rand(); 247 | if (val != expected) 248 | printf("Data mismatch at word %zu: expected=%d val=%d\n", i, 249 | expected, val); 250 | } 251 | /* Close the file. */ 252 | ff_fclose(pxFile); 253 | 254 | int64_t elapsed_us = absolute_time_diff_us(xStart, get_absolute_time()); 255 | float elapsed = elapsed_us / 1E6; 256 | printf("Elapsed seconds %.3g\n", elapsed); 257 | } 258 | 259 | void big_file_test(const char *const pathname, size_t size, uint32_t seed) { 260 | if (create_big_file(pathname, size, seed)) 261 | check_big_file(pathname, size, seed); 262 | } 263 | 264 | /* [] END OF FILE */ 265 | -------------------------------------------------------------------------------- /FatFs_SPI/sd_driver/spi.c: -------------------------------------------------------------------------------- 1 | /* spi.c 2 | Copyright 2021 Carl John Kugler III 3 | 4 | Licensed under the Apache License, Version 2.0 (the License); you may not use 5 | this file except in compliance with the License. You may obtain a copy of the 6 | License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | Unless required by applicable law or agreed to in writing, software distributed 10 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 11 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 12 | specific language governing permissions and limitations under the License. 13 | */ 14 | 15 | #include 16 | #include 17 | // 18 | #include "pico/stdlib.h" 19 | #include "pico/mutex.h" 20 | #include "pico/sem.h" 21 | // 22 | #include "my_debug.h" 23 | #include "hw_config.h" 24 | // 25 | #include "spi.h" 26 | 27 | static bool irqChannel1 = false; 28 | static bool irqShared = true; 29 | 30 | static void in_spi_irq_handler(const uint DMA_IRQ_num, io_rw_32 *dma_hw_ints_p) { 31 | for (size_t i = 0; i < spi_get_num(); ++i) { 32 | spi_t *spi_p = spi_get_by_num(i); 33 | if (DMA_IRQ_num == spi_p->DMA_IRQ_num) { 34 | // Is the SPI's channel requesting interrupt? 35 | if (*dma_hw_ints_p & (1 << spi_p->rx_dma)) { 36 | *dma_hw_ints_p = 1 << spi_p->rx_dma; // Clear it. 37 | assert(!dma_channel_is_busy(spi_p->rx_dma)); 38 | assert(!sem_available(&spi_p->sem)); 39 | bool ok = sem_release(&spi_p->sem); 40 | assert(ok); 41 | } 42 | } 43 | } 44 | } 45 | static void __not_in_flash_func(spi_irq_handler_0)() { 46 | in_spi_irq_handler(DMA_IRQ_0, &dma_hw->ints0); 47 | } 48 | static void __not_in_flash_func(spi_irq_handler_1)() { 49 | in_spi_irq_handler(DMA_IRQ_1, &dma_hw->ints1); 50 | } 51 | 52 | void set_spi_dma_irq_channel(bool useChannel1, bool shared) { 53 | irqChannel1 = useChannel1; 54 | irqShared = shared; 55 | } 56 | 57 | // SPI Transfer: Read & Write (simultaneously) on SPI bus 58 | // If the data that will be received is not important, pass NULL as rx. 59 | // If the data that will be transmitted is not important, 60 | // pass NULL as tx and then the SPI_FILL_CHAR is sent out as each data 61 | // element. 62 | bool spi_transfer(spi_t *spi_p, const uint8_t *tx, uint8_t *rx, size_t length) { 63 | // assert(512 == length || 1 == length); 64 | assert(tx || rx); 65 | // assert(!(tx && rx)); 66 | 67 | // tx write increment is already false 68 | if (tx) { 69 | channel_config_set_read_increment(&spi_p->tx_dma_cfg, true); 70 | } else { 71 | static const uint8_t dummy = SPI_FILL_CHAR; 72 | tx = &dummy; 73 | channel_config_set_read_increment(&spi_p->tx_dma_cfg, false); 74 | } 75 | 76 | // rx read increment is already false 77 | if (rx) { 78 | channel_config_set_write_increment(&spi_p->rx_dma_cfg, true); 79 | } else { 80 | static uint8_t dummy = 0xA5; 81 | rx = &dummy; 82 | channel_config_set_write_increment(&spi_p->rx_dma_cfg, false); 83 | } 84 | 85 | dma_channel_configure(spi_p->tx_dma, &spi_p->tx_dma_cfg, 86 | &spi_get_hw(spi_p->hw_inst)->dr, // write address 87 | tx, // read address 88 | length, // element count (each element is of 89 | // size transfer_data_size) 90 | false); // start 91 | dma_channel_configure(spi_p->rx_dma, &spi_p->rx_dma_cfg, 92 | rx, // write address 93 | &spi_get_hw(spi_p->hw_inst)->dr, // read address 94 | length, // element count (each element is of 95 | // size transfer_data_size) 96 | false); // start 97 | 98 | switch (spi_p->DMA_IRQ_num) { 99 | case DMA_IRQ_0: 100 | assert(!dma_channel_get_irq0_status(spi_p->rx_dma)); 101 | break; 102 | case DMA_IRQ_1: 103 | assert(!dma_channel_get_irq1_status(spi_p->rx_dma)); 104 | break; 105 | default: 106 | assert(false); 107 | } 108 | sem_reset(&spi_p->sem, 0); 109 | 110 | // start them exactly simultaneously to avoid races (in extreme cases 111 | // the FIFO could overflow) 112 | dma_start_channel_mask((1u << spi_p->tx_dma) | (1u << spi_p->rx_dma)); 113 | 114 | /* Wait until master completes transfer or time out has occured. */ 115 | uint32_t timeOut = 1000; /* Timeout 1 sec */ 116 | bool rc = sem_acquire_timeout_ms( 117 | &spi_p->sem, timeOut); // Wait for notification from ISR 118 | if (!rc) { 119 | // If the timeout is reached the function will return false 120 | DBG_PRINTF("Notification wait timed out in %s\n", __FUNCTION__); 121 | return false; 122 | } 123 | // Shouldn't be necessary: 124 | dma_channel_wait_for_finish_blocking(spi_p->tx_dma); 125 | dma_channel_wait_for_finish_blocking(spi_p->rx_dma); 126 | 127 | assert(!sem_available(&spi_p->sem)); 128 | assert(!dma_channel_is_busy(spi_p->tx_dma)); 129 | assert(!dma_channel_is_busy(spi_p->rx_dma)); 130 | 131 | return true; 132 | } 133 | 134 | void spi_lock(spi_t *spi_p) { 135 | assert(mutex_is_initialized(&spi_p->mutex)); 136 | mutex_enter_blocking(&spi_p->mutex); 137 | } 138 | void spi_unlock(spi_t *spi_p) { 139 | assert(mutex_is_initialized(&spi_p->mutex)); 140 | mutex_exit(&spi_p->mutex); 141 | } 142 | 143 | bool my_spi_init(spi_t *spi_p) { 144 | auto_init_mutex(my_spi_init_mutex); 145 | mutex_enter_blocking(&my_spi_init_mutex); 146 | if (!spi_p->initialized) { 147 | //// The SPI may be shared (using multiple SSs); protect it 148 | //spi_p->mutex = xSemaphoreCreateRecursiveMutex(); 149 | //xSemaphoreTakeRecursive(spi_p->mutex, portMAX_DELAY); 150 | if (!mutex_is_initialized(&spi_p->mutex)) mutex_init(&spi_p->mutex); 151 | spi_lock(spi_p); 152 | 153 | // Default: 154 | if (!spi_p->baud_rate) 155 | spi_p->baud_rate = 10 * 1000 * 1000; 156 | // For the IRQ notification: 157 | sem_init(&spi_p->sem, 0, 1); 158 | 159 | /* Configure component */ 160 | // Enable SPI at 100 kHz and connect to GPIOs 161 | spi_init(spi_p->hw_inst, 100 * 1000); 162 | spi_set_format(spi_p->hw_inst, 8, SPI_CPOL_0, SPI_CPHA_0, SPI_MSB_FIRST); 163 | 164 | gpio_set_function(spi_p->miso_gpio, GPIO_FUNC_SPI); 165 | gpio_set_function(spi_p->mosi_gpio, GPIO_FUNC_SPI); 166 | gpio_set_function(spi_p->sck_gpio, GPIO_FUNC_SPI); 167 | // ss_gpio is initialized in sd_init_driver() 168 | 169 | // Slew rate limiting levels for GPIO outputs. 170 | // enum gpio_slew_rate { GPIO_SLEW_RATE_SLOW = 0, GPIO_SLEW_RATE_FAST = 1 } 171 | // void gpio_set_slew_rate (uint gpio,enum gpio_slew_rate slew) 172 | // Default appears to be GPIO_SLEW_RATE_SLOW. 173 | 174 | // Drive strength levels for GPIO outputs. 175 | // enum gpio_drive_strength { GPIO_DRIVE_STRENGTH_2MA = 0, GPIO_DRIVE_STRENGTH_4MA = 1, GPIO_DRIVE_STRENGTH_8MA = 2, 176 | // GPIO_DRIVE_STRENGTH_12MA = 3 } 177 | // enum gpio_drive_strength gpio_get_drive_strength (uint gpio) 178 | if (spi_p->set_drive_strength) { 179 | gpio_set_drive_strength(spi_p->mosi_gpio, spi_p->mosi_gpio_drive_strength); 180 | gpio_set_drive_strength(spi_p->sck_gpio, spi_p->sck_gpio_drive_strength); 181 | } 182 | 183 | // SD cards' DO MUST be pulled up. 184 | gpio_pull_up(spi_p->miso_gpio); 185 | 186 | // Grab some unused dma channels 187 | spi_p->tx_dma = dma_claim_unused_channel(true); 188 | spi_p->rx_dma = dma_claim_unused_channel(true); 189 | 190 | spi_p->tx_dma_cfg = dma_channel_get_default_config(spi_p->tx_dma); 191 | spi_p->rx_dma_cfg = dma_channel_get_default_config(spi_p->rx_dma); 192 | channel_config_set_transfer_data_size(&spi_p->tx_dma_cfg, DMA_SIZE_8); 193 | channel_config_set_transfer_data_size(&spi_p->rx_dma_cfg, DMA_SIZE_8); 194 | 195 | // We set the outbound DMA to transfer from a memory buffer to the SPI 196 | // transmit FIFO paced by the SPI TX FIFO DREQ The default is for the 197 | // read address to increment every element (in this case 1 byte - 198 | // DMA_SIZE_8) and for the write address to remain unchanged. 199 | channel_config_set_dreq(&spi_p->tx_dma_cfg, spi_get_index(spi_p->hw_inst) 200 | ? DREQ_SPI1_TX 201 | : DREQ_SPI0_TX); 202 | channel_config_set_write_increment(&spi_p->tx_dma_cfg, false); 203 | 204 | // We set the inbound DMA to transfer from the SPI receive FIFO to a 205 | // memory buffer paced by the SPI RX FIFO DREQ We coinfigure the read 206 | // address to remain unchanged for each element, but the write address 207 | // to increment (so data is written throughout the buffer) 208 | channel_config_set_dreq(&spi_p->rx_dma_cfg, spi_get_index(spi_p->hw_inst) 209 | ? DREQ_SPI1_RX 210 | : DREQ_SPI0_RX); 211 | channel_config_set_read_increment(&spi_p->rx_dma_cfg, false); 212 | 213 | /* Theory: we only need an interrupt on rx complete, 214 | since if rx is complete, tx must also be complete. */ 215 | 216 | /* Configure the processor to run dma_handler() when DMA IRQ 0/1 is asserted */ 217 | 218 | spi_p->DMA_IRQ_num = irqChannel1 ? DMA_IRQ_1 : DMA_IRQ_0; 219 | 220 | // Tell the DMA to raise IRQ line 0/1 when the channel finishes a block 221 | static void (*spi_irq_handler_p)(); 222 | switch (spi_p->DMA_IRQ_num) { 223 | case DMA_IRQ_0: 224 | spi_irq_handler_p = spi_irq_handler_0; 225 | dma_channel_set_irq0_enabled(spi_p->rx_dma, true); 226 | dma_channel_set_irq0_enabled(spi_p->tx_dma, false); 227 | break; 228 | case DMA_IRQ_1: 229 | spi_irq_handler_p = spi_irq_handler_1; 230 | dma_channel_set_irq1_enabled(spi_p->rx_dma, true); 231 | dma_channel_set_irq1_enabled(spi_p->tx_dma, false); 232 | break; 233 | default: 234 | assert(false); 235 | } 236 | if (irqShared) { 237 | irq_add_shared_handler( 238 | spi_p->DMA_IRQ_num, *spi_irq_handler_p, 239 | PICO_SHARED_IRQ_HANDLER_DEFAULT_ORDER_PRIORITY); 240 | } else { 241 | irq_set_exclusive_handler(spi_p->DMA_IRQ_num, *spi_irq_handler_p); 242 | } 243 | irq_set_enabled(spi_p->DMA_IRQ_num, true); 244 | LED_INIT(); 245 | spi_p->initialized = true; 246 | spi_unlock(spi_p); 247 | } 248 | mutex_exit(&my_spi_init_mutex); 249 | return true; 250 | } 251 | 252 | /* [] END OF FILE */ 253 | -------------------------------------------------------------------------------- /FatFs_SPI/ff15/documents/res/app4.c: -------------------------------------------------------------------------------- 1 | /*----------------------------------------------------------------------/ 2 | / Low level disk I/O module function checker / 3 | /-----------------------------------------------------------------------/ 4 | / WARNING: The data on the target drive will be lost! 5 | */ 6 | 7 | #include 8 | #include 9 | #include "ff.h" /* Declarations of sector size */ 10 | #include "diskio.h" /* Declarations of disk functions */ 11 | 12 | 13 | 14 | static DWORD pn ( /* Pseudo random number generator */ 15 | DWORD pns /* 0:Initialize, !0:Read */ 16 | ) 17 | { 18 | static DWORD lfsr; 19 | UINT n; 20 | 21 | 22 | if (pns) { 23 | lfsr = pns; 24 | for (n = 0; n < 32; n++) pn(0); 25 | } 26 | if (lfsr & 1) { 27 | lfsr >>= 1; 28 | lfsr ^= 0x80200003; 29 | } else { 30 | lfsr >>= 1; 31 | } 32 | return lfsr; 33 | } 34 | 35 | 36 | int test_diskio ( 37 | BYTE pdrv, /* Physical drive number to be checked (all data on the drive will be lost) */ 38 | UINT ncyc, /* Number of test cycles */ 39 | DWORD* buff, /* Pointer to the working buffer */ 40 | UINT sz_buff /* Size of the working buffer in unit of byte */ 41 | ) 42 | { 43 | UINT n, cc, ns; 44 | DWORD sz_drv, lba, lba2, sz_eblk, pns = 1; 45 | WORD sz_sect; 46 | BYTE *pbuff = (BYTE*)buff; 47 | DSTATUS ds; 48 | DRESULT dr; 49 | 50 | 51 | printf("test_diskio(%u, %u, 0x%08X, 0x%08X)\n", pdrv, ncyc, (UINT)buff, sz_buff); 52 | 53 | if (sz_buff < FF_MAX_SS + 8) { 54 | printf("Insufficient work area to run the program.\n"); 55 | return 1; 56 | } 57 | 58 | for (cc = 1; cc <= ncyc; cc++) { 59 | printf("**** Test cycle %u of %u start ****\n", cc, ncyc); 60 | 61 | printf(" disk_initalize(%u)", pdrv); 62 | ds = disk_initialize(pdrv); 63 | if (ds & STA_NOINIT) { 64 | printf(" - failed.\n"); 65 | return 2; 66 | } else { 67 | printf(" - ok.\n"); 68 | } 69 | 70 | printf("**** Get drive size ****\n"); 71 | printf(" disk_ioctl(%u, GET_SECTOR_COUNT, 0x%08X)", pdrv, (UINT)&sz_drv); 72 | sz_drv = 0; 73 | dr = disk_ioctl(pdrv, GET_SECTOR_COUNT, &sz_drv); 74 | if (dr == RES_OK) { 75 | printf(" - ok.\n"); 76 | } else { 77 | printf(" - failed.\n"); 78 | return 3; 79 | } 80 | if (sz_drv < 128) { 81 | printf("Failed: Insufficient drive size to test.\n"); 82 | return 4; 83 | } 84 | printf(" Number of sectors on the drive %u is %lu.\n", pdrv, sz_drv); 85 | 86 | #if FF_MAX_SS != FF_MIN_SS 87 | printf("**** Get sector size ****\n"); 88 | printf(" disk_ioctl(%u, GET_SECTOR_SIZE, 0x%X)", pdrv, (UINT)&sz_sect); 89 | sz_sect = 0; 90 | dr = disk_ioctl(pdrv, GET_SECTOR_SIZE, &sz_sect); 91 | if (dr == RES_OK) { 92 | printf(" - ok.\n"); 93 | } else { 94 | printf(" - failed.\n"); 95 | return 5; 96 | } 97 | printf(" Size of sector is %u bytes.\n", sz_sect); 98 | #else 99 | sz_sect = FF_MAX_SS; 100 | #endif 101 | 102 | printf("**** Get block size ****\n"); 103 | printf(" disk_ioctl(%u, GET_BLOCK_SIZE, 0x%X)", pdrv, (UINT)&sz_eblk); 104 | sz_eblk = 0; 105 | dr = disk_ioctl(pdrv, GET_BLOCK_SIZE, &sz_eblk); 106 | if (dr == RES_OK) { 107 | printf(" - ok.\n"); 108 | } else { 109 | printf(" - failed.\n"); 110 | } 111 | if (dr == RES_OK || sz_eblk >= 2) { 112 | printf(" Size of the erase block is %lu sectors.\n", sz_eblk); 113 | } else { 114 | printf(" Size of the erase block is unknown.\n"); 115 | } 116 | 117 | /* Single sector write test */ 118 | printf("**** Single sector write test ****\n"); 119 | lba = 0; 120 | for (n = 0, pn(pns); n < sz_sect; n++) pbuff[n] = (BYTE)pn(0); 121 | printf(" disk_write(%u, 0x%X, %lu, 1)", pdrv, (UINT)pbuff, lba); 122 | dr = disk_write(pdrv, pbuff, lba, 1); 123 | if (dr == RES_OK) { 124 | printf(" - ok.\n"); 125 | } else { 126 | printf(" - failed.\n"); 127 | return 6; 128 | } 129 | printf(" disk_ioctl(%u, CTRL_SYNC, NULL)", pdrv); 130 | dr = disk_ioctl(pdrv, CTRL_SYNC, 0); 131 | if (dr == RES_OK) { 132 | printf(" - ok.\n"); 133 | } else { 134 | printf(" - failed.\n"); 135 | return 7; 136 | } 137 | memset(pbuff, 0, sz_sect); 138 | printf(" disk_read(%u, 0x%X, %lu, 1)", pdrv, (UINT)pbuff, lba); 139 | dr = disk_read(pdrv, pbuff, lba, 1); 140 | if (dr == RES_OK) { 141 | printf(" - ok.\n"); 142 | } else { 143 | printf(" - failed.\n"); 144 | return 8; 145 | } 146 | for (n = 0, pn(pns); n < sz_sect && pbuff[n] == (BYTE)pn(0); n++) ; 147 | if (n == sz_sect) { 148 | printf(" Read data matched.\n"); 149 | } else { 150 | printf(" Read data differs from the data written.\n"); 151 | return 10; 152 | } 153 | pns++; 154 | 155 | printf("**** Multiple sector write test ****\n"); 156 | lba = 5; ns = sz_buff / sz_sect; 157 | if (ns > 4) ns = 4; 158 | if (ns > 1) { 159 | for (n = 0, pn(pns); n < (UINT)(sz_sect * ns); n++) pbuff[n] = (BYTE)pn(0); 160 | printf(" disk_write(%u, 0x%X, %lu, %u)", pdrv, (UINT)pbuff, lba, ns); 161 | dr = disk_write(pdrv, pbuff, lba, ns); 162 | if (dr == RES_OK) { 163 | printf(" - ok.\n"); 164 | } else { 165 | printf(" - failed.\n"); 166 | return 11; 167 | } 168 | printf(" disk_ioctl(%u, CTRL_SYNC, NULL)", pdrv); 169 | dr = disk_ioctl(pdrv, CTRL_SYNC, 0); 170 | if (dr == RES_OK) { 171 | printf(" - ok.\n"); 172 | } else { 173 | printf(" - failed.\n"); 174 | return 12; 175 | } 176 | memset(pbuff, 0, sz_sect * ns); 177 | printf(" disk_read(%u, 0x%X, %lu, %u)", pdrv, (UINT)pbuff, lba, ns); 178 | dr = disk_read(pdrv, pbuff, lba, ns); 179 | if (dr == RES_OK) { 180 | printf(" - ok.\n"); 181 | } else { 182 | printf(" - failed.\n"); 183 | return 13; 184 | } 185 | for (n = 0, pn(pns); n < (UINT)(sz_sect * ns) && pbuff[n] == (BYTE)pn(0); n++) ; 186 | if (n == (UINT)(sz_sect * ns)) { 187 | printf(" Read data matched.\n"); 188 | } else { 189 | printf(" Read data differs from the data written.\n"); 190 | return 14; 191 | } 192 | } else { 193 | printf(" Test skipped.\n"); 194 | } 195 | pns++; 196 | 197 | printf("**** Single sector write test (unaligned buffer address) ****\n"); 198 | lba = 5; 199 | for (n = 0, pn(pns); n < sz_sect; n++) pbuff[n+3] = (BYTE)pn(0); 200 | printf(" disk_write(%u, 0x%X, %lu, 1)", pdrv, (UINT)(pbuff+3), lba); 201 | dr = disk_write(pdrv, pbuff+3, lba, 1); 202 | if (dr == RES_OK) { 203 | printf(" - ok.\n"); 204 | } else { 205 | printf(" - failed.\n"); 206 | return 15; 207 | } 208 | printf(" disk_ioctl(%u, CTRL_SYNC, NULL)", pdrv); 209 | dr = disk_ioctl(pdrv, CTRL_SYNC, 0); 210 | if (dr == RES_OK) { 211 | printf(" - ok.\n"); 212 | } else { 213 | printf(" - failed.\n"); 214 | return 16; 215 | } 216 | memset(pbuff+5, 0, sz_sect); 217 | printf(" disk_read(%u, 0x%X, %lu, 1)", pdrv, (UINT)(pbuff+5), lba); 218 | dr = disk_read(pdrv, pbuff+5, lba, 1); 219 | if (dr == RES_OK) { 220 | printf(" - ok.\n"); 221 | } else { 222 | printf(" - failed.\n"); 223 | return 17; 224 | } 225 | for (n = 0, pn(pns); n < sz_sect && pbuff[n+5] == (BYTE)pn(0); n++) ; 226 | if (n == sz_sect) { 227 | printf(" Read data matched.\n"); 228 | } else { 229 | printf(" Read data differs from the data written.\n"); 230 | return 18; 231 | } 232 | pns++; 233 | 234 | printf("**** 4GB barrier test ****\n"); 235 | if (sz_drv >= 128 + 0x80000000 / (sz_sect / 2)) { 236 | lba = 6; lba2 = lba + 0x80000000 / (sz_sect / 2); 237 | for (n = 0, pn(pns); n < (UINT)(sz_sect * 2); n++) pbuff[n] = (BYTE)pn(0); 238 | printf(" disk_write(%u, 0x%X, %lu, 1)", pdrv, (UINT)pbuff, lba); 239 | dr = disk_write(pdrv, pbuff, lba, 1); 240 | if (dr == RES_OK) { 241 | printf(" - ok.\n"); 242 | } else { 243 | printf(" - failed.\n"); 244 | return 19; 245 | } 246 | printf(" disk_write(%u, 0x%X, %lu, 1)", pdrv, (UINT)(pbuff+sz_sect), lba2); 247 | dr = disk_write(pdrv, pbuff+sz_sect, lba2, 1); 248 | if (dr == RES_OK) { 249 | printf(" - ok.\n"); 250 | } else { 251 | printf(" - failed.\n"); 252 | return 20; 253 | } 254 | printf(" disk_ioctl(%u, CTRL_SYNC, NULL)", pdrv); 255 | dr = disk_ioctl(pdrv, CTRL_SYNC, 0); 256 | if (dr == RES_OK) { 257 | printf(" - ok.\n"); 258 | } else { 259 | printf(" - failed.\n"); 260 | return 21; 261 | } 262 | memset(pbuff, 0, sz_sect * 2); 263 | printf(" disk_read(%u, 0x%X, %lu, 1)", pdrv, (UINT)pbuff, lba); 264 | dr = disk_read(pdrv, pbuff, lba, 1); 265 | if (dr == RES_OK) { 266 | printf(" - ok.\n"); 267 | } else { 268 | printf(" - failed.\n"); 269 | return 22; 270 | } 271 | printf(" disk_read(%u, 0x%X, %lu, 1)", pdrv, (UINT)(pbuff+sz_sect), lba2); 272 | dr = disk_read(pdrv, pbuff+sz_sect, lba2, 1); 273 | if (dr == RES_OK) { 274 | printf(" - ok.\n"); 275 | } else { 276 | printf(" - failed.\n"); 277 | return 23; 278 | } 279 | for (n = 0, pn(pns); pbuff[n] == (BYTE)pn(0) && n < (UINT)(sz_sect * 2); n++) ; 280 | if (n == (UINT)(sz_sect * 2)) { 281 | printf(" Read data matched.\n"); 282 | } else { 283 | printf(" Read data differs from the data written.\n"); 284 | return 24; 285 | } 286 | } else { 287 | printf(" Test skipped.\n"); 288 | } 289 | pns++; 290 | 291 | printf("**** Test cycle %u of %u completed ****\n\n", cc, ncyc); 292 | } 293 | 294 | return 0; 295 | } 296 | 297 | 298 | 299 | int main (int argc, char* argv[]) 300 | { 301 | int rc; 302 | DWORD buff[FF_MAX_SS]; /* Working buffer (4 sector in size) */ 303 | 304 | /* Check function/compatibility of the physical drive #0 */ 305 | rc = test_diskio(0, 3, buff, sizeof buff); 306 | 307 | if (rc) { 308 | printf("Sorry the function/compatibility test failed. (rc=%d)\nFatFs will not work with this disk driver.\n", rc); 309 | } else { 310 | printf("Congratulations! The disk driver works well.\n"); 311 | } 312 | 313 | return rc; 314 | } 315 | 316 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /FatFs_SPI/ff15/source/ffconf.h: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------/ 2 | / Configurations of FatFs Module 3 | /---------------------------------------------------------------------------*/ 4 | 5 | #define FFCONF_DEF 80286 /* Revision ID */ 6 | 7 | /*---------------------------------------------------------------------------/ 8 | / Function Configurations 9 | /---------------------------------------------------------------------------*/ 10 | 11 | #define FF_FS_READONLY 0 12 | /* This option switches read-only configuration. (0:Read/Write or 1:Read-only) 13 | / Read-only configuration removes writing API functions, f_write(), f_sync(), 14 | / f_unlink(), f_mkdir(), f_chmod(), f_rename(), f_truncate(), f_getfree() 15 | / and optional writing functions as well. */ 16 | 17 | 18 | #define FF_FS_MINIMIZE 0 19 | /* This option defines minimization level to remove some basic API functions. 20 | / 21 | / 0: Basic functions are fully enabled. 22 | / 1: f_stat(), f_getfree(), f_unlink(), f_mkdir(), f_truncate() and f_rename() 23 | / are removed. 24 | / 2: f_opendir(), f_readdir() and f_closedir() are removed in addition to 1. 25 | / 3: f_lseek() function is removed in addition to 2. */ 26 | 27 | 28 | #define FF_USE_FIND 1 29 | /* This option switches filtered directory read functions, f_findfirst() and 30 | / f_findnext(). (0:Disable, 1:Enable 2:Enable with matching altname[] too) */ 31 | 32 | 33 | #define FF_USE_MKFS 1 34 | /* This option switches f_mkfs() function. (0:Disable or 1:Enable) */ 35 | 36 | 37 | #define FF_USE_FASTSEEK 1 38 | /* This option switches fast seek function. (0:Disable or 1:Enable) */ 39 | 40 | 41 | #define FF_USE_EXPAND 0 42 | /* This option switches f_expand function. (0:Disable or 1:Enable) */ 43 | 44 | 45 | #define FF_USE_CHMOD 0 46 | /* This option switches attribute manipulation functions, f_chmod() and f_utime(). 47 | / (0:Disable or 1:Enable) Also FF_FS_READONLY needs to be 0 to enable this option. */ 48 | 49 | 50 | #define FF_USE_LABEL 0 51 | /* This option switches volume label functions, f_getlabel() and f_setlabel(). 52 | / (0:Disable or 1:Enable) */ 53 | 54 | 55 | #define FF_USE_FORWARD 0 56 | /* This option switches f_forward() function. (0:Disable or 1:Enable) */ 57 | 58 | 59 | #define FF_USE_STRFUNC 1 60 | #define FF_PRINT_LLI 1 61 | #define FF_PRINT_FLOAT 1 62 | #define FF_STRF_ENCODE 3 63 | /* FF_USE_STRFUNC switches string functions, f_gets(), f_putc(), f_puts() and 64 | / f_printf(). 65 | / 66 | / 0: Disable. FF_PRINT_LLI, FF_PRINT_FLOAT and FF_STRF_ENCODE have no effect. 67 | / 1: Enable without LF-CRLF conversion. 68 | / 2: Enable with LF-CRLF conversion. 69 | / 70 | / FF_PRINT_LLI = 1 makes f_printf() support long long argument and FF_PRINT_FLOAT = 1/2 71 | / makes f_printf() support floating point argument. These features want C99 or later. 72 | / When FF_LFN_UNICODE >= 1 with LFN enabled, string functions convert the character 73 | / encoding in it. FF_STRF_ENCODE selects assumption of character encoding ON THE FILE 74 | / to be read/written via those functions. 75 | / 76 | / 0: ANSI/OEM in current CP 77 | / 1: Unicode in UTF-16LE 78 | / 2: Unicode in UTF-16BE 79 | / 3: Unicode in UTF-8 80 | */ 81 | 82 | 83 | /*---------------------------------------------------------------------------/ 84 | / Locale and Namespace Configurations 85 | /---------------------------------------------------------------------------*/ 86 | 87 | #define FF_CODE_PAGE 437 88 | /* This option specifies the OEM code page to be used on the target system. 89 | / Incorrect code page setting can cause a file open failure. 90 | / 91 | / 437 - U.S. 92 | / 720 - Arabic 93 | / 737 - Greek 94 | / 771 - KBL 95 | / 775 - Baltic 96 | / 850 - Latin 1 97 | / 852 - Latin 2 98 | / 855 - Cyrillic 99 | / 857 - Turkish 100 | / 860 - Portuguese 101 | / 861 - Icelandic 102 | / 862 - Hebrew 103 | / 863 - Canadian French 104 | / 864 - Arabic 105 | / 865 - Nordic 106 | / 866 - Russian 107 | / 869 - Greek 2 108 | / 932 - Japanese (DBCS) 109 | / 936 - Simplified Chinese (DBCS) 110 | / 949 - Korean (DBCS) 111 | / 950 - Traditional Chinese (DBCS) 112 | / 0 - Include all code pages above and configured by f_setcp() 113 | */ 114 | 115 | 116 | #define FF_USE_LFN 3 117 | #define FF_MAX_LFN 255 118 | /* The FF_USE_LFN switches the support for LFN (long file name). 119 | / 120 | / 0: Disable LFN. FF_MAX_LFN has no effect. 121 | / 1: Enable LFN with static working buffer on the BSS. Always NOT thread-safe. 122 | / 2: Enable LFN with dynamic working buffer on the STACK. 123 | / 3: Enable LFN with dynamic working buffer on the HEAP. 124 | / 125 | / To enable the LFN, ffunicode.c needs to be added to the project. The LFN function 126 | / requiers certain internal working buffer occupies (FF_MAX_LFN + 1) * 2 bytes and 127 | / additional (FF_MAX_LFN + 44) / 15 * 32 bytes when exFAT is enabled. 128 | / The FF_MAX_LFN defines size of the working buffer in UTF-16 code unit and it can 129 | / be in range of 12 to 255. It is recommended to be set it 255 to fully support LFN 130 | / specification. 131 | / When use stack for the working buffer, take care on stack overflow. When use heap 132 | / memory for the working buffer, memory management functions, ff_memalloc() and 133 | / ff_memfree() exemplified in ffsystem.c, need to be added to the project. */ 134 | 135 | 136 | #define FF_LFN_UNICODE 2 137 | /* This option switches the character encoding on the API when LFN is enabled. 138 | / 139 | / 0: ANSI/OEM in current CP (TCHAR = char) 140 | / 1: Unicode in UTF-16 (TCHAR = WCHAR) 141 | / 2: Unicode in UTF-8 (TCHAR = char) 142 | / 3: Unicode in UTF-32 (TCHAR = DWORD) 143 | / 144 | / Also behavior of string I/O functions will be affected by this option. 145 | / When LFN is not enabled, this option has no effect. */ 146 | 147 | 148 | #define FF_LFN_BUF 255 149 | #define FF_SFN_BUF 12 150 | /* This set of options defines size of file name members in the FILINFO structure 151 | / which is used to read out directory items. These values should be suffcient for 152 | / the file names to read. The maximum possible length of the read file name depends 153 | / on character encoding. When LFN is not enabled, these options have no effect. */ 154 | 155 | 156 | #define FF_FS_RPATH 2 157 | /* This option configures support for relative path. 158 | / 159 | / 0: Disable relative path and remove related functions. 160 | / 1: Enable relative path. f_chdir() and f_chdrive() are available. 161 | / 2: f_getcwd() function is available in addition to 1. 162 | */ 163 | 164 | 165 | /*---------------------------------------------------------------------------/ 166 | / Drive/Volume Configurations 167 | /---------------------------------------------------------------------------*/ 168 | 169 | # define FF_VOLUMES 2 170 | /* Number of volumes (logical drives) to be used. (1-10) */ 171 | 172 | 173 | #define FF_STR_VOLUME_ID 0 174 | #define FF_VOLUME_STRS "RAM","NAND","CF","SD","SD2","USB","USB2","USB3" 175 | /* FF_STR_VOLUME_ID switches support for volume ID in arbitrary strings. 176 | / When FF_STR_VOLUME_ID is set to 1 or 2, arbitrary strings can be used as drive 177 | / number in the path name. FF_VOLUME_STRS defines the volume ID strings for each 178 | / logical drives. Number of items must not be less than FF_VOLUMES. Valid 179 | / characters for the volume ID strings are A-Z, a-z and 0-9, however, they are 180 | / compared in case-insensitive. If FF_STR_VOLUME_ID >= 1 and FF_VOLUME_STRS is 181 | / not defined, a user defined volume string table is needed as: 182 | / 183 | / const char* VolumeStr[FF_VOLUMES] = {"ram","flash","sd","usb",... 184 | */ 185 | 186 | 187 | #define FF_MULTI_PARTITION 0 188 | /* This option switches support for multiple volumes on the physical drive. 189 | / By default (0), each logical drive number is bound to the same physical drive 190 | / number and only an FAT volume found on the physical drive will be mounted. 191 | / When this function is enabled (1), each logical drive number can be bound to 192 | / arbitrary physical drive and partition listed in the VolToPart[]. Also f_fdisk() 193 | / function will be available. */ 194 | 195 | 196 | #define FF_MIN_SS 512 197 | #define FF_MAX_SS 512 198 | /* This set of options configures the range of sector size to be supported. (512, 199 | / 1024, 2048 or 4096) Always set both 512 for most systems, generic memory card and 200 | / harddisk, but a larger value may be required for on-board flash memory and some 201 | / type of optical media. When FF_MAX_SS is larger than FF_MIN_SS, FatFs is configured 202 | / for variable sector size mode and disk_ioctl() function needs to implement 203 | / GET_SECTOR_SIZE command. */ 204 | 205 | 206 | #define FF_LBA64 1 207 | /* This option switches support for 64-bit LBA. (0:Disable or 1:Enable) 208 | / To enable the 64-bit LBA, also exFAT needs to be enabled. (FF_FS_EXFAT == 1) */ 209 | 210 | 211 | #define FF_MIN_GPT 0x10000000 212 | /* Minimum number of sectors to switch GPT as partitioning format in f_mkfs and 213 | / f_fdisk function. 0x100000000 max. This option has no effect when FF_LBA64 == 0. */ 214 | 215 | 216 | #define FF_USE_TRIM 0 217 | /* This option switches support for ATA-TRIM. (0:Disable or 1:Enable) 218 | / To enable Trim function, also CTRL_TRIM command should be implemented to the 219 | / disk_ioctl() function. */ 220 | 221 | 222 | 223 | /*---------------------------------------------------------------------------/ 224 | / System Configurations 225 | /---------------------------------------------------------------------------*/ 226 | 227 | #define FF_FS_TINY 0 228 | /* This option switches tiny buffer configuration. (0:Normal or 1:Tiny) 229 | / At the tiny configuration, size of file object (FIL) is shrinked FF_MAX_SS bytes. 230 | / Instead of private sector buffer eliminated from the file object, common sector 231 | / buffer in the filesystem object (FATFS) is used for the file data transfer. */ 232 | 233 | 234 | #define FF_FS_EXFAT 1 235 | /* This option switches support for exFAT filesystem. (0:Disable or 1:Enable) 236 | / To enable exFAT, also LFN needs to be enabled. (FF_USE_LFN >= 1) 237 | / Note that enabling exFAT discards ANSI C (C89) compatibility. */ 238 | 239 | 240 | #define FF_FS_NORTC 0 241 | #define FF_NORTC_MON 1 242 | #define FF_NORTC_MDAY 1 243 | #define FF_NORTC_YEAR 2022 244 | /* The option FF_FS_NORTC switches timestamp feature. If the system does not have 245 | / an RTC or valid timestamp is not needed, set FF_FS_NORTC = 1 to disable the 246 | / timestamp feature. Every object modified by FatFs will have a fixed timestamp 247 | / defined by FF_NORTC_MON, FF_NORTC_MDAY and FF_NORTC_YEAR in local time. 248 | / To enable timestamp function (FF_FS_NORTC = 0), get_fattime() function need to be 249 | / added to the project to read current time form real-time clock. FF_NORTC_MON, 250 | / FF_NORTC_MDAY and FF_NORTC_YEAR have no effect. 251 | / These options have no effect in read-only configuration (FF_FS_READONLY = 1). */ 252 | 253 | 254 | #define FF_FS_NOFSINFO 0 255 | /* If you need to know correct free space on the FAT32 volume, set bit 0 of this 256 | / option, and f_getfree() function at the first time after volume mount will force 257 | / a full FAT scan. Bit 1 controls the use of last allocated cluster number. 258 | / 259 | / bit0=0: Use free cluster count in the FSINFO if available. 260 | / bit0=1: Do not trust free cluster count in the FSINFO. 261 | / bit1=0: Use last allocated cluster number in the FSINFO if available. 262 | / bit1=1: Do not trust last allocated cluster number in the FSINFO. 263 | */ 264 | 265 | 266 | #define FF_FS_LOCK 16 267 | /* The option FF_FS_LOCK switches file lock function to control duplicated file open 268 | / and illegal operation to open objects. This option must be 0 when FF_FS_READONLY 269 | / is 1. 270 | / 271 | / 0: Disable file lock function. To avoid volume corruption, application program 272 | / should avoid illegal open, remove and rename to the open objects. 273 | / >0: Enable file lock function. The value defines how many files/sub-directories 274 | / can be opened simultaneously under file lock control. Note that the file 275 | / lock control is independent of re-entrancy. */ 276 | 277 | 278 | #define FF_FS_REENTRANT 0 279 | #define FF_FS_TIMEOUT 1000 280 | /* The option FF_FS_REENTRANT switches the re-entrancy (thread safe) of the FatFs 281 | / module itself. Note that regardless of this option, file access to different 282 | / volume is always re-entrant and volume control functions, f_mount(), f_mkfs() 283 | / and f_fdisk() function, are always not re-entrant. Only file/directory access 284 | / to the same volume is under control of this featuer. 285 | / 286 | / 0: Disable re-entrancy. FF_FS_TIMEOUT have no effect. 287 | / 1: Enable re-entrancy. Also user provided synchronization handlers, 288 | / ff_mutex_create(), ff_mutex_delete(), ff_mutex_take() and ff_mutex_give() 289 | / function, must be added to the project. Samples are available in ffsystem.c. 290 | / 291 | / The FF_FS_TIMEOUT defines timeout period in unit of O/S time tick. 292 | */ 293 | 294 | 295 | 296 | /*--- End of configuration options ---*/ 297 | -------------------------------------------------------------------------------- /example/tests/app4-IO_module_function_checker.c: -------------------------------------------------------------------------------- 1 | /*----------------------------------------------------------------------/ 2 | / Low level disk I/O module function checker / 3 | /-----------------------------------------------------------------------/ 4 | / WARNING: The data on the target drive will be lost! 5 | */ 6 | /* app4-IO_module_function_checker.c 7 | Originally from [Compatibility Checker for Storage Device Control Module](http://elm-chan.org/fsw/ff/res/app4.c). 8 | */ 9 | /*----------------------------------------------------------------------------/ 10 | / FatFs - Generic FAT Filesystem Module Rx.xx / 11 | /-----------------------------------------------------------------------------/ 12 | / 13 | / Copyright (C) 20xx, ChaN, all right reserved. 14 | / 15 | / FatFs module is an open source software. Redistribution and use of FatFs in 16 | / source and binary forms, with or without modification, are permitted provided 17 | / that the following condition is met: 18 | / 19 | / 1. Redistributions of source code must retain the above copyright notice, 20 | / this condition and the following disclaimer. 21 | / 22 | / This software is provided by the copyright holder and contributors "AS IS" 23 | / and any warranties related to this software are DISCLAIMED. 24 | / The copyright owner or contributors be NOT LIABLE for any damages caused 25 | / by use of this software. 26 | /----------------------------------------------------------------------------*/ 27 | /* 28 | Modifications: Copyright 2021 Carl John Kugler III 29 | 30 | Licensed under the Apache License, Version 2.0 (the License); you may not use 31 | this file except in compliance with the License. You may obtain a copy of the 32 | License at 33 | 34 | http://www.apache.org/licenses/LICENSE-2.0 35 | Unless required by applicable law or agreed to in writing, software distributed 36 | under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR 37 | CONDITIONS OF ANY KIND, either express or implied. See the License for the 38 | specific language governing permissions and limitations under the License. 39 | */ 40 | 41 | #include 42 | #include 43 | #include "ff.h" /* Declarations of sector size */ 44 | #include "diskio.h" /* Declarations of disk functions */ 45 | 46 | #include "hardware/gpio.h" // gpio_put 47 | 48 | static DWORD pn ( /* Pseudo random number generator */ 49 | DWORD pns /* 0:Initialize, !0:Read */ 50 | ) 51 | { 52 | static DWORD lfsr; 53 | UINT n; 54 | 55 | 56 | if (pns) { 57 | lfsr = pns; 58 | for (n = 0; n < 32; n++) pn(0); 59 | } 60 | if (lfsr & 1) { 61 | lfsr >>= 1; 62 | lfsr ^= 0x80200003; 63 | } else { 64 | lfsr >>= 1; 65 | } 66 | return lfsr; 67 | } 68 | 69 | 70 | int test_diskio ( 71 | BYTE pdrv, /* Physical drive number to be checked (all data on the drive will be lost) */ 72 | UINT ncyc, /* Number of test cycles */ 73 | DWORD* buff, /* Pointer to the working buffer */ 74 | UINT sz_buff /* Size of the working buffer in unit of byte */ 75 | ) 76 | { 77 | UINT n, cc, ns; 78 | DWORD sz_drv, lba, lba2, sz_eblk, pns = 1; 79 | WORD sz_sect; 80 | BYTE *pbuff = (BYTE*)buff; 81 | DSTATUS ds; 82 | DRESULT dr; 83 | 84 | printf("test_diskio(%u, %u, 0x%08X, 0x%08X)\n", pdrv, ncyc, (UINT)buff, sz_buff); 85 | 86 | if (sz_buff < FF_MAX_SS + 8) { 87 | printf("Insufficient work area to run the program.\n"); 88 | return 1; 89 | } 90 | 91 | for (cc = 1; cc <= ncyc; cc++) { 92 | printf("**** Test cycle %u of %u start ****\n", cc, ncyc); 93 | 94 | printf(" disk_initalize(%u)", pdrv); 95 | ds = disk_initialize(pdrv); 96 | if (ds & STA_NOINIT) { 97 | printf(" - failed.\n"); 98 | return 2; 99 | } else { 100 | printf(" - ok.\n"); 101 | } 102 | 103 | printf("**** Get drive size ****\n"); 104 | printf(" disk_ioctl(%u, GET_SECTOR_COUNT, 0x%08X)", pdrv, (UINT)&sz_drv); 105 | sz_drv = 0; 106 | dr = disk_ioctl(pdrv, GET_SECTOR_COUNT, &sz_drv); 107 | if (dr == RES_OK) { 108 | printf(" - ok.\n"); 109 | } else { 110 | printf(" - failed.\n"); 111 | return 3; 112 | } 113 | if (sz_drv < 128) { 114 | printf("Failed: Insufficient drive size to test.\n"); 115 | return 4; 116 | } 117 | printf(" Number of sectors on the drive %u is %lu.\n", pdrv, sz_drv); 118 | 119 | #if FF_MAX_SS != FF_MIN_SS 120 | printf("**** Get sector size ****\n"); 121 | printf(" disk_ioctl(%u, GET_SECTOR_SIZE, 0x%X)", pdrv, (UINT)&sz_sect); 122 | sz_sect = 0; 123 | dr = disk_ioctl(pdrv, GET_SECTOR_SIZE, &sz_sect); 124 | if (dr == RES_OK) { 125 | printf(" - ok.\n"); 126 | } else { 127 | printf(" - failed.\n"); 128 | return 5; 129 | } 130 | printf(" Size of sector is %u bytes.\n", sz_sect); 131 | #else 132 | sz_sect = FF_MAX_SS; 133 | #endif 134 | 135 | printf("**** Get block size ****\n"); 136 | printf(" disk_ioctl(%u, GET_BLOCK_SIZE, 0x%X)", pdrv, (UINT)&sz_eblk); 137 | sz_eblk = 0; 138 | dr = disk_ioctl(pdrv, GET_BLOCK_SIZE, &sz_eblk); 139 | if (dr == RES_OK) { 140 | printf(" - ok.\n"); 141 | } else { 142 | printf(" - failed.\n"); 143 | } 144 | if (dr == RES_OK || sz_eblk >= 2) { 145 | printf(" Size of the erase block is %lu sectors.\n", sz_eblk); 146 | } else { 147 | printf(" Size of the erase block is unknown.\n"); 148 | } 149 | 150 | /* Single sector write test */ 151 | printf("**** Single sector write test ****\n"); 152 | lba = 0; 153 | for (n = 0, pn(pns); n < sz_sect; n++) pbuff[n] = (BYTE)pn(0); 154 | printf(" disk_write(%u, 0x%X, %lu, 1)", pdrv, (UINT)pbuff, lba); 155 | dr = disk_write(pdrv, pbuff, lba, 1); 156 | if (dr == RES_OK) { 157 | printf(" - ok.\n"); 158 | } else { 159 | printf(" - failed.\n"); 160 | return 6; 161 | } 162 | printf(" disk_ioctl(%u, CTRL_SYNC, NULL)", pdrv); 163 | dr = disk_ioctl(pdrv, CTRL_SYNC, 0); 164 | if (dr == RES_OK) { 165 | printf(" - ok.\n"); 166 | } else { 167 | printf(" - failed.\n"); 168 | return 7; 169 | } 170 | memset(pbuff, 0, sz_sect); 171 | printf(" disk_read(%u, 0x%X, %lu, 1)", pdrv, (UINT)pbuff, lba); 172 | dr = disk_read(pdrv, pbuff, lba, 1); 173 | if (dr == RES_OK) { 174 | printf(" - ok.\n"); 175 | } else { 176 | printf(" - failed.\n"); 177 | return 8; 178 | } 179 | for (n = 0, pn(pns); n < sz_sect && pbuff[n] == (BYTE)pn(0); n++) ; 180 | if (n == sz_sect) { 181 | printf(" Read data matched.\n"); 182 | } else { 183 | printf(" Read data differs from the data written.\n"); 184 | return 10; 185 | } 186 | pns++; 187 | 188 | printf("**** Multiple sector write test ****\n"); 189 | lba = 5; ns = sz_buff / sz_sect; 190 | if (ns > 4) ns = 4; 191 | if (ns > 1) { 192 | for (n = 0, pn(pns); n < (UINT)(sz_sect * ns); n++) pbuff[n] = (BYTE)pn(0); 193 | printf(" disk_write(%u, 0x%X, %lu, %u)", pdrv, (UINT)pbuff, lba, ns); 194 | dr = disk_write(pdrv, pbuff, lba, ns); 195 | if (dr == RES_OK) { 196 | printf(" - ok.\n"); 197 | } else { 198 | printf(" - failed.\n"); 199 | return 11; 200 | } 201 | printf(" disk_ioctl(%u, CTRL_SYNC, NULL)", pdrv); 202 | dr = disk_ioctl(pdrv, CTRL_SYNC, 0); 203 | if (dr == RES_OK) { 204 | printf(" - ok.\n"); 205 | } else { 206 | printf(" - failed.\n"); 207 | return 12; 208 | } 209 | memset(pbuff, 0, sz_sect * ns); 210 | printf(" disk_read(%u, 0x%X, %lu, %u)", pdrv, (UINT)pbuff, lba, ns); 211 | dr = disk_read(pdrv, pbuff, lba, ns); 212 | if (dr == RES_OK) { 213 | printf(" - ok.\n"); 214 | } else { 215 | printf(" - failed.\n"); 216 | return 13; 217 | } 218 | for (n = 0, pn(pns); n < (UINT)(sz_sect * ns) && pbuff[n] == (BYTE)pn(0); n++) ; 219 | if (n == (UINT)(sz_sect * ns)) { 220 | printf(" Read data matched.\n"); 221 | } else { 222 | printf(" Read data differs from the data written.\n"); 223 | return 14; 224 | } 225 | } else { 226 | printf(" Test skipped.\n"); 227 | } 228 | pns++; 229 | 230 | printf("**** Single sector write test (unaligned buffer address) ****\n"); 231 | lba = 5; 232 | for (n = 0, pn(pns); n < sz_sect; n++) pbuff[n+3] = (BYTE)pn(0); 233 | printf(" disk_write(%u, 0x%X, %lu, 1)", pdrv, (UINT)(pbuff+3), lba); 234 | dr = disk_write(pdrv, pbuff+3, lba, 1); 235 | if (dr == RES_OK) { 236 | printf(" - ok.\n"); 237 | } else { 238 | printf(" - failed.\n"); 239 | return 15; 240 | } 241 | printf(" disk_ioctl(%u, CTRL_SYNC, NULL)", pdrv); 242 | dr = disk_ioctl(pdrv, CTRL_SYNC, 0); 243 | if (dr == RES_OK) { 244 | printf(" - ok.\n"); 245 | } else { 246 | printf(" - failed.\n"); 247 | return 16; 248 | } 249 | memset(pbuff+5, 0, sz_sect); 250 | printf(" disk_read(%u, 0x%X, %lu, 1)", pdrv, (UINT)(pbuff+5), lba); 251 | dr = disk_read(pdrv, pbuff+5, lba, 1); 252 | if (dr == RES_OK) { 253 | printf(" - ok.\n"); 254 | } else { 255 | printf(" - failed.\n"); 256 | return 17; 257 | } 258 | for (n = 0, pn(pns); n < sz_sect && pbuff[n+5] == (BYTE)pn(0); n++) ; 259 | if (n == sz_sect) { 260 | printf(" Read data matched.\n"); 261 | } else { 262 | printf(" Read data differs from the data written.\n"); 263 | return 18; 264 | } 265 | pns++; 266 | 267 | printf("**** 4GB barrier test ****\n"); 268 | if (sz_drv >= 128 + 0x80000000 / (sz_sect / 2)) { 269 | lba = 6; lba2 = lba + 0x80000000 / (sz_sect / 2); 270 | for (n = 0, pn(pns); n < (UINT)(sz_sect * 2); n++) pbuff[n] = (BYTE)pn(0); 271 | printf(" disk_write(%u, 0x%X, %lu, 1)", pdrv, (UINT)pbuff, lba); 272 | dr = disk_write(pdrv, pbuff, lba, 1); 273 | if (dr == RES_OK) { 274 | printf(" - ok.\n"); 275 | } else { 276 | printf(" - failed.\n"); 277 | return 19; 278 | } 279 | printf(" disk_write(%u, 0x%X, %lu, 1)", pdrv, (UINT)(pbuff+sz_sect), lba2); 280 | dr = disk_write(pdrv, pbuff+sz_sect, lba2, 1); 281 | if (dr == RES_OK) { 282 | printf(" - ok.\n"); 283 | } else { 284 | printf(" - failed.\n"); 285 | return 20; 286 | } 287 | printf(" disk_ioctl(%u, CTRL_SYNC, NULL)", pdrv); 288 | dr = disk_ioctl(pdrv, CTRL_SYNC, 0); 289 | if (dr == RES_OK) { 290 | printf(" - ok.\n"); 291 | } else { 292 | printf(" - failed.\n"); 293 | return 21; 294 | } 295 | memset(pbuff, 0, sz_sect * 2); 296 | printf(" disk_read(%u, 0x%X, %lu, 1)", pdrv, (UINT)pbuff, lba); 297 | dr = disk_read(pdrv, pbuff, lba, 1); 298 | if (dr == RES_OK) { 299 | printf(" - ok.\n"); 300 | } else { 301 | printf(" - failed.\n"); 302 | return 22; 303 | } 304 | printf(" disk_read(%u, 0x%X, %lu, 1)", pdrv, (UINT)(pbuff+sz_sect), lba2); 305 | dr = disk_read(pdrv, pbuff+sz_sect, lba2, 1); 306 | if (dr == RES_OK) { 307 | printf(" - ok.\n"); 308 | } else { 309 | printf(" - failed.\n"); 310 | return 23; 311 | } 312 | for (n = 0, pn(pns); pbuff[n] == (BYTE)pn(0) && n < (UINT)(sz_sect * 2); n++) ; 313 | if (n == (UINT)(sz_sect * 2)) { 314 | printf(" Read data matched.\n"); 315 | } else { 316 | printf(" Read data differs from the data written.\n"); 317 | return 24; 318 | } 319 | } else { 320 | printf(" Test skipped.\n"); 321 | } 322 | pns++; 323 | 324 | printf("**** Test cycle %u of %u completed ****\n\n", cc, ncyc); 325 | } 326 | 327 | return 0; 328 | } 329 | 330 | 331 | //int main (int argc, char* argv[]) 332 | int lliot(size_t pnum) 333 | { 334 | int rc; 335 | DWORD buff[FF_MAX_SS]; /* Working buffer (4 sector in size) */ 336 | 337 | /* Check function/compatibility of the physical drive #0 */ 338 | rc = test_diskio(pnum, 3, buff, sizeof buff); 339 | 340 | if (rc) { 341 | printf("Sorry the function/compatibility test failed. (rc=%d)\nFatFs will not work with this disk driver.\n", rc); 342 | } else { 343 | printf("Congratulations! The disk driver works well.\n"); 344 | } 345 | 346 | return rc; 347 | } 348 | 349 | --------------------------------------------------------------------------------