Lightweight partitioned BaseFlash registry

This forum is about you. Feel free to discuss anything is related to embedded and electronics, your awesome projects, your ideas, your announcements, not necessarily related to ChibiOS but to embedded in general. This forum is NOT for support.
faisal
Posts: 374
Joined: Wed Jul 19, 2017 12:44 am
Has thanked: 44 times
Been thanked: 60 times

Lightweight partitioned BaseFlash registry

Postby faisal » Mon Oct 31, 2022 6:06 pm

This is a lightweight layer to register BaseFlash devices along with partitions for the application layer. This allows the application to address/access a particular partition of a particular device with path like "sda1/config". This is useful, and probably all most embedded applications need - as opposed to a full filesystem with directory hierarchy and so forth. It fills a niche between raw flash access, and something like FatFS or LittleFs. It's different than MFS - which is feature rich but confines you to use it in a certain way.

You can define a device with various partitions like this:

Code: Select all

static struct smtd_partition snorparts[] = {
  {
    .name = "config"
    .offset = 0,
    .size = 1024,
  },
  {
    .name = "histograms"
    .offset = 1024,
    .size = 1024,
  },
  {
    .name = "diags"
    .offset = 2048,
    .size = 1024,
  },
};

static struct smtd_info snor1 = {
    .name = "sda",
    .drv = (BaseFlash *)&SNORDriver,
    .erase_range = NULL,
    .partitions = &snorparts,
    .npart = sizeof(snorparts)/sizeof(snorparts[0]),
};


And you can have multiple devices with partitions - just define another struct smtd_info for each.

Register the struct smtd_info with the system like so:

Code: Select all

smtd_device_register(&snor1);
smtd_device_register(&snor2);
smtd_device_register(&internal_flash);
smtd_device_register(&fram);
etc ...


The application can get a context (or call it a handle) to a specific partition by just giving a path "<device>/<partition>".
The context stores a current file position as well. So you can use typical read/write/fseek/ftell like APIs. The offsets/position/sector_index of the partition are relative to the start of the partition. That's really the bulk of what this layer is doing - constraining and address translation between the partition base address and the actual memory location in the device itself.

Code: Select all

smtd_ctx_t * fp = smtd_get_ctx("sda/histograms");

flash_offset_t cur_pos = smtd_ftell(fp);

smtd_fseek(fp, cur_pos + 100, SMTD_SEEK_CUR);

smtd_write(fp, buf, sizeof(buf));


If anyone's gonna use this - it needs to be updated to use the acquire_exclusive/release_exclusive BaseFlash APIs (it's just a few lines). When I get around to it, I'll update this post.
Attachments
smtd.zip
(3.72 KiB) Downloaded 206 times

Return to “User Projects”

Who is online

Users browsing this forum: No registered users and 7 guests