Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

IAR ARM BusFault exception when mounting lfs (NO_MALLOC, NO_ASSERT/DEBUG/ERROR/WARN) #1032

Open
s1syph0s opened this issue Oct 18, 2024 · 2 comments

Comments

@s1syph0s
Copy link

Hi,

I want to play around with littlefs for our project for an XMC4500 board. I connected an external NOR FLASH (S25FL) and wrote the driver to interact with the flash. I also created a wrapper function for the lfs to call the read/prog/erase/sync functions, and I allocated a static buffers for the read/prog/lookahead/file.

#define CACHE_SIZE 2048
#define LOOKAHEAD_SIZE 2048

static uint8_t ReadBuf[CACHE_SIZE];
static uint8_t ProgBuf[CACHE_SIZE];
static uint8_t FileBuf[CACHE_SIZE];
static uint8_t LABuf[LOOKAHEAD_SIZE];

const struct lfs_config cfg = {
    .read = my_lfs_read,
    .prog = my_lfs_prog,
    .erase = my_lfs_erase,
    .sync = my_lfs_sync,

    // Device conf
    .read_size = 256,
    .prog_size = 256,
    .block_size = 4096,
    .block_count = 2048,
    .block_cycles = 250,
    .cache_size = CACHE_SIZE,
    .lookahead_size = LOOKAHEAD_SIZE,

    // buffers
    .read_buffer = ReadBuf,
    .prog_buffer = ProgBuf,
    .lookahead_buffer = LABuf,
};

const struct lfs_file_config file_cfg = {
    .buffer = FileBuf,
    .attr_count = 0,
};

lfs_t lfs;
lfs_file_t file;

And during my setup function, lfs_mount is called:

int err = lfs_mount(&lfs, &cfg);
if (err) {
    lfs_format(&lfs, &cfg);
    lfs_mount(&lfs, &cfg);
}

When the lfs_mount function is called, I received a BusFault Exception:

A precise data access error has occurred (CFSR.PRECISERR, BFAR)
At data address 0xf0116801.

An imprecise data access error has occurred (CFSR.IMPRECISERR, BFAR)
At data address 0xf0116801.

While debugging the problem, I found out that the exception happens in my driver, when I send the read opcode through the SPI.

    BYTE cmd = (BYTE) S25FL_READ;
    S25FL_ASSERT(spi_hal_send_data(m_uFlashChannels[idx], &cmd, sizeof(cmd), SPI_ACTION_KEEP_CS_AFTER_TRANSFER));
    
    // m_uFlashChannels is an array of Flash channels; In my current case it has only one element

This only happens if the wrapper my_lfs_read is called from lfs; If I called the function independently without lfs, the exception will not happen and my nor flash is read correctly.

From what I'm reading up until now, this can happen if there's a buffer overflow. Did I configure littlefs incorrectly?

@s1syph0s
Copy link
Author

Mounting is now possible. I changed the CACHE_SIZE and LOOKAHEAD_SIZE to 256, and I increased the stack size of my RTOS task to 8192, and now it is possible to read. However, my program hangs again when it tries to call lfs_file_write:

    for (;;) {
        while (spi_device_open(ch_flash, 10) < 0) {
            if (sc_miscErrnoGet() != ETIMEDOUT) {
                kprintf(0, "ERROR %d\n", sc_miscErrnoGet());
                FREEZE;
            }
        }
        // read current count
        uint64_t counter = 0;
        lfs_file_opencfg(&lfs, &file, "counter", LFS_O_RDWR | LFS_O_CREAT, &file_cfg);
        lfs_file_read(&lfs, &file, &counter, sizeof(counter));

        kprintf(0, "Counter: %d\n", counter);

        // update counter
        counter += 1;
        lfs_file_rewind(&lfs, &file);
        lfs_file_write(&lfs, &file, &counter, sizeof(counter));

        lfs_file_close(&lfs, &file);

        if (spi_device_close(ch_flash) < 0) {
            kprintf(0, "ERROR %d\n", sc_miscErrnoGet());
            FREEZE;
        }
        sc_sleep(sc_tickMs2Tick(1000));
    }

The same error like last time happened here. My program crashes in lfs_bd_prog, when it tried to do memcpy lfs.c:242.

@s1syph0s
Copy link
Author

I noticed now, that my littlefs is not working properly..

I thought that mounting lfs was now working, but in my code it is actually having an unexpected behavior.

    s25fl_EraseChip(ch_flash); /* Testing to see if the chip will be formatted or not when trying to mount */

    int err = lfs_mount(&lfs, &cfg);
    if (err) {
        lfs_format(&lfs, &cfg);
        lfs_mount(&lfs, &cfg);
    }

I erased the chip before mounting LFS. I expected that lfs_mount call will fail because the chip is erased completely, and should return an error, but I noticed that it doesn't even go inside the if block where the chip will be formatted to LFS format. @geky did I create my configuration correctly?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant