Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
carlk3 committed Apr 22, 2021
1 parent 3075e9d commit bec6354
Show file tree
Hide file tree
Showing 11 changed files with 168 additions and 62 deletions.
1 change: 0 additions & 1 deletion FatFs_SPI/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ target_sources(FatFs_SPI INTERFACE
${CMAKE_CURRENT_LIST_DIR}/sd_driver/spi.c
${CMAKE_CURRENT_LIST_DIR}/sd_driver/sd_card.c
${CMAKE_CURRENT_LIST_DIR}/sd_driver/crc.c
${CMAKE_CURRENT_LIST_DIR}/sd_driver/my_debug.c
${CMAKE_CURRENT_LIST_DIR}/src/glue.c
${CMAKE_CURRENT_LIST_DIR}/src/f_util.c
${CMAKE_CURRENT_LIST_DIR}/src/ff_stdio.c
Expand Down
2 changes: 1 addition & 1 deletion FatFs_SPI/ff14a/source/ffconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@
*/


#define FF_FS_LOCK 0 // Note: 0 since we're not bothering with f_closedir
#define FF_FS_LOCK 16
/* The option FF_FS_LOCK switches file lock function to control duplicated file open
/ and illegal operation to open objects. This option must be 0 when FF_FS_READONLY
/ is 1.
Expand Down
7 changes: 6 additions & 1 deletion FatFs_SPI/include/f_util.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#pragma once
#include "ff.h"
const char *FRESULT_str(FRESULT i);
const char *FRESULT_str(FRESULT i);
FRESULT delete_node (
TCHAR* path, /* Path name buffer with the sub-directory to delete */
UINT sz_buff, /* Size of path name buffer (items) */
FILINFO* fno /* Name read buffer */
);
File renamed without changes.
13 changes: 0 additions & 13 deletions FatFs_SPI/sd_driver/my_debug.c

This file was deleted.

42 changes: 42 additions & 0 deletions FatFs_SPI/src/f_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,45 @@ const char *FRESULT_str(FRESULT i) {
return "Unknown";
}
}

FRESULT delete_node (
TCHAR* path, /* Path name buffer with the sub-directory to delete */
UINT sz_buff, /* Size of path name buffer (items) */
FILINFO* fno /* Name read buffer */
)
{
UINT i, j;
FRESULT fr;
DIR dir;


fr = f_opendir(&dir, path); /* Open the sub-directory to make it empty */
if (fr != FR_OK) return fr;

for (i = 0; path[i]; i++) ; /* Get current path length */
path[i++] = '/';

for (;;) {
fr = f_readdir(&dir, fno); /* Get a directory item */
if (fr != FR_OK || !fno->fname[0]) break; /* End of directory? */
j = 0;
do { /* Make a path name */
if (i + j >= sz_buff) { /* Buffer over flow? */
fr = 100; break; /* Fails with 100 when buffer overflow */
}
path[i + j] = fno->fname[j];
} while (fno->fname[j++]);
if (fno->fattrib & AM_DIR) { /* Item is a sub-directory */
fr = delete_node(path, sz_buff, fno);
} else { /* Item is a file */
fr = f_unlink(path);
}
if (fr != FR_OK) break;
}

path[--i] = 0; /* Restore the path name */
f_closedir(&dir);

if (fr == FR_OK) fr = f_unlink(path); /* Delete the empty sub-directory */
return fr;
}
79 changes: 46 additions & 33 deletions FatFs_SPI/src/ff_stdio.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
#include "f_util.h"
#include "ff_stdio.h"

bool tracing = true;

#define TRACE_PRINTF(fmt, args...)
#define TRACE_PRINTF(fmt, args...) {}
//#define TRACE_PRINTF printf

static BYTE posix2mode(const char *pcMode) {
Expand Down Expand Up @@ -91,8 +89,7 @@ FF_FILE *ff_fopen(const char *pcFile, const char *pcMode) {
FRESULT fr = f_open(fp, pcFile, posix2mode(pcMode));
errno = fresult2errno(fr);
if (FR_OK != fr) {
if (tracing)
printf("%s error: %s (%d)\n", __func__, FRESULT_str(fr), fr);
TRACE_PRINTF("%s error: %s (%d)\n", __func__, FRESULT_str(fr), fr);
free(fp);
fp = 0;
}
Expand All @@ -103,10 +100,9 @@ int ff_fclose(FF_FILE *pxStream) {
// FRESULT f_close (
// FIL* fp /* [IN] Pointer to the file object */
//);
myASSERT(pxStream);
FRESULT fr = f_close(pxStream);
if (tracing && FR_OK != fr)
printf("%s error: %s (%d)\n", __func__, FRESULT_str(fr), fr);
if (FR_OK != fr)
TRACE_PRINTF("%s error: %s (%d)\n", __func__, FRESULT_str(fr), fr);
errno = fresult2errno(fr);
free(pxStream);
if (FR_OK == fr)
Expand All @@ -125,8 +121,8 @@ int ff_stat(const char *pcFileName, FF_Stat_t *pxStatBuffer) {
FILINFO filinfo;
FRESULT fr = f_stat(pcFileName, &filinfo);
pxStatBuffer->st_size = filinfo.fsize;
if (tracing && FR_OK != fr)
printf("%s error: %s (%d)\n", __func__, FRESULT_str(fr), fr);
if (FR_OK != fr)
TRACE_PRINTF("%s error: %s (%d)\n", __func__, FRESULT_str(fr), fr);
errno = fresult2errno(fr);
if (FR_OK == fr)
return 0;
Expand All @@ -145,8 +141,8 @@ size_t ff_fwrite(const void *pvBuffer, size_t xSize, size_t xItems,
//);
UINT bw = 0;
FRESULT fr = f_write(pxStream, pvBuffer, xSize * xItems, &bw);
if (tracing && FR_OK != fr)
printf("%s error: %s (%d)\n", __func__, FRESULT_str(fr), fr);
if (FR_OK != fr)
TRACE_PRINTF("%s error: %s (%d)\n", __func__, FRESULT_str(fr), fr);
errno = fresult2errno(fr);
return bw / xSize;
}
Expand All @@ -161,8 +157,8 @@ size_t ff_fread(void *pvBuffer, size_t xSize, size_t xItems,
//);
UINT br = 0;
FRESULT fr = f_read(pxStream, pvBuffer, xSize * xItems, &br);
if (tracing && FR_OK != fr)
printf("%s error: %s (%d)\n", __func__, FRESULT_str(fr), fr);
if (FR_OK != fr)
TRACE_PRINTF("%s error: %s (%d)\n", __func__, FRESULT_str(fr), fr);
errno = fresult2errno(fr);
return br / xSize;
}
Expand All @@ -172,8 +168,8 @@ int ff_chdir(const char *pcDirectoryName) {
// const TCHAR* path /* [IN] Path name */
//);
FRESULT fr = f_chdir(pcDirectoryName);
if (tracing && FR_OK != fr)
printf("%s error: %s (%d)\n", __func__, FRESULT_str(fr), fr);
if (FR_OK != fr)
TRACE_PRINTF("%s error: %s (%d)\n", __func__, FRESULT_str(fr), fr);
errno = fresult2errno(fr);
if (FR_OK == fr)
return 0;
Expand All @@ -188,15 +184,19 @@ char *ff_getcwd(char *pcBuffer, size_t xBufferLength) {
//);
char buf[xBufferLength];
FRESULT fr = f_getcwd(buf, xBufferLength);
if (tracing && FR_OK != fr)
printf("%s error: %s (%d)\n", __func__, FRESULT_str(fr), fr);
if (FR_OK != fr)
TRACE_PRINTF("%s error: %s (%d)\n", __func__, FRESULT_str(fr), fr);
errno = fresult2errno(fr);
// If the current working directory name was successfully written to
// pcBuffer then pcBuffer is returned. Otherwise NULL is returned.
if (FR_OK == fr) {
if ('/' != buf[0]) {
char *p = strchr(buf, '/');
if (!p) p = buf;
// Strip off drive prefix:
char *p = strchr(buf, ':');
if (p)
++p;
else
p = buf;
strncpy(pcBuffer, p, xBufferLength);
}
return pcBuffer;
Expand All @@ -207,8 +207,8 @@ char *ff_getcwd(char *pcBuffer, size_t xBufferLength) {
int ff_mkdir(const char *pcDirectoryName) {
TRACE_PRINTF("%s(pxStream=%s)\n", __func__, pcDirectoryName);
FRESULT fr = f_mkdir(pcDirectoryName);
if (tracing && FR_OK != fr && FR_EXIST != fr)
printf("%s error: %s (%d)\n", __func__, FRESULT_str(fr), fr);
if (FR_OK != fr && FR_EXIST != fr)
TRACE_PRINTF("%s error: %s (%d)\n", __func__, FRESULT_str(fr), fr);
errno = fresult2errno(fr);
if (FR_OK == fr || FR_EXIST == fr)
return 0;
Expand All @@ -228,8 +228,8 @@ int ff_fputc(int iChar, FF_FILE *pxStream) {
uint8_t buff[1];
buff[0] = iChar;
FRESULT fr = f_write(pxStream, buff, 1, &bw);
if (tracing && FR_OK != fr)
printf("%s error: %s (%d)\n", __func__, FRESULT_str(fr), fr);
if (FR_OK != fr)
TRACE_PRINTF("%s error: %s (%d)\n", __func__, FRESULT_str(fr), fr);
errno = fresult2errno(fr);
// On success the byte written to the file is returned. If any other value
// is returned then the byte was not written to the file and the task's
Expand All @@ -251,8 +251,8 @@ int ff_fgetc(FF_FILE *pxStream) {
uint8_t buff[1] = {0};
UINT br;
FRESULT fr = f_read(pxStream, buff, 1, &br);
if (tracing && FR_OK != fr)
printf("%s error: %s (%d)\n", __func__, FRESULT_str(fr), fr);
if (FR_OK != fr)
TRACE_PRINTF("%s error: %s (%d)\n", __func__, FRESULT_str(fr), fr);
errno = fresult2errno(fr);
// On success the byte read from the file system is returned. If a byte
// could not be read from the file because the read position is already at
Expand Down Expand Up @@ -300,12 +300,15 @@ int ff_fseek(FF_FILE *pxStream, int iOffset, int iWhence) {
FRESULT fr = -1;
switch (iWhence) {
case FF_SEEK_CUR: // The current file position.
if ((int)f_tell(pxStream) + iOffset < 0) return -1;
fr = f_lseek(pxStream, f_tell(pxStream) + iOffset);
break;
case FF_SEEK_END: // The end of the file.
if ((int)f_size(pxStream) + iOffset < 0) return -1;
fr = f_lseek(pxStream, f_size(pxStream) + iOffset);
break;
case FF_SEEK_SET: // The beginning of the file.
if (iOffset < 0) return -1;
fr = f_lseek(pxStream, iOffset);
break;
default:
Expand Down Expand Up @@ -342,7 +345,7 @@ int ff_findfirst(const char *pcDirectory, FF_FindData_t *pxFindData) {
errno = fresult2errno(fr);
pxFindData->pcFileName = pxFindData->fileinfo.fname;
pxFindData->ulFileSize = pxFindData->fileinfo.fsize;
if (tracing) printf("%s: fname=%s\n", __func__, pxFindData->fileinfo.fname);
TRACE_PRINTF("%s: fname=%s\n", __func__, pxFindData->fileinfo.fname);
if (pcDirectory[0]) {
FRESULT fr2 = f_chdir(buf1);
errno = fresult2errno(fr2);
Expand All @@ -364,7 +367,7 @@ int ff_findnext(FF_FindData_t *pxFindData) {
errno = fresult2errno(fr);
pxFindData->pcFileName = pxFindData->fileinfo.fname;
pxFindData->ulFileSize = pxFindData->fileinfo.fsize;
if (tracing) printf("%s: fname=%s\n", __func__, pxFindData->fileinfo.fname);
TRACE_PRINTF("%s: fname=%s\n", __func__, pxFindData->fileinfo.fname);
if (FR_OK == fr && pxFindData->fileinfo.fname[0]) {
return 0;
} else {
Expand All @@ -378,19 +381,29 @@ FF_FILE *ff_truncate(const char *pcFileName, long lTruncateSize) {
errno = ENOMEM;
return NULL;
}
FRESULT fr = f_open(fp, pcFileName, FA_CREATE_ALWAYS | FA_WRITE);
if (tracing && FR_OK != fr)
FRESULT fr = f_open(fp, pcFileName, FA_OPEN_APPEND | FA_WRITE);
if (FR_OK != fr)
printf("%s: f_open error: %s (%d)\n", __func__, FRESULT_str(fr), fr);
errno = fresult2errno(fr);
if (FR_OK != fr) return NULL;
while (f_tell(fp) < (FSIZE_t)lTruncateSize) {
UINT bw = 0;
char c = 0;
fr = f_write(fp, &c, 1, &bw);
if (FR_OK != fr)
TRACE_PRINTF("%s error: %s (%d)\n", __func__, FRESULT_str(fr), fr);
errno = fresult2errno(fr);
if (1 != bw) return NULL;
}
fr = f_lseek(fp, lTruncateSize);
errno = fresult2errno(fr);
if (tracing && FR_OK != fr)
if (FR_OK != fr)
printf("%s: f_lseek error: %s (%d)\n", __func__, FRESULT_str(fr), fr);
if (FR_OK != fr) return NULL;
fr = f_truncate(fp);
if (tracing && FR_OK != fr)
printf("%s: f_truncate error: %s (%d)\n", __func__, FRESULT_str(fr), fr);
if (FR_OK != fr)
printf("%s: f_truncate error: %s (%d)\n", __func__, FRESULT_str(fr),
fr);
errno = fresult2errno(fr);
if (FR_OK == fr)
return fp;
Expand Down
13 changes: 13 additions & 0 deletions FatFs_SPI/src/my_debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,16 @@ void my_printf(const char *pcFormat, ...) {
printf("%s", pcBuffer);
fflush(stdout);
}


void my_assert_func(const char *file, int line, const char *func,
const char *pred) {
printf("assertion \"%s\" failed: file \"%s\", line %d, function: %s\n",
pred, file, line, func);
fflush(stdout);
__asm volatile("cpsid i" : : : "memory"); /* Disable global interrupts. */
while (1) {
__asm("bkpt #0");
}; // Stop in GUI as if at a breakpoint (if debugging, otherwise loop
// forever)
}
3 changes: 2 additions & 1 deletion example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ set(CMAKE_CXX_STANDARD 17)
# Initialise the Pico SDK
pico_sdk_init()

add_subdirectory(../FatFs_SPI build)

# Add executable. Default name is the project name, version 0.1
add_executable(FatFS_SPI_example
FatFS_SPI_example.c
Expand All @@ -21,7 +23,6 @@ add_executable(FatFS_SPI_example
tests/CreateAndVerifyExampleFiles.c
tests/ff_stdio_tests_with_cwd.c
)
add_subdirectory(../FatFs_SPI build)
# Add the standard library to the build
target_link_libraries(FatFS_SPI_example pico_stdlib)

Expand Down
Loading

0 comments on commit bec6354

Please sign in to comment.