mirror of
https://github.com/cesanta/mongoose.git
synced 2026-12-03 21:48:22 +00:00
48 lines
2.3 KiB
C
48 lines
2.3 KiB
C
#pragma once
|
|
|
|
#include "arch.h"
|
|
#include "periph.h"
|
|
|
|
struct mg_flash {
|
|
void *start; // Address at which flash starts
|
|
size_t size; // Flash size
|
|
size_t secsz; // Sector size
|
|
size_t align; // Write alignment
|
|
bool (*read_fn)(void *, void *, size_t); // Read function
|
|
bool (*write_fn)(void *, const void *, size_t); // Write function
|
|
bool (*swap_fn)(void); // Swap partitions
|
|
bool (*init_fn)(struct mg_flash *); // Optional, fills runtime attributes
|
|
};
|
|
|
|
extern struct mg_flash *mg_flash;
|
|
|
|
// Reads memory-mapped (XIP) flash. Use as read_fn for such flash
|
|
bool mg_flash_read_xip(void *addr, void *buf, size_t len);
|
|
|
|
// JEDEC SPI NOR flash: W25Q, P25Q, MX25 and similar, up to 16 MB, 3-byte
|
|
// addresses, single-line SPI. MG_SPI_FLASH() wraps these into a struct mg_flash
|
|
bool mg_spi_flash_init(struct mg_spi *spi, size_t *size);
|
|
bool mg_spi_flash_read(struct mg_spi *spi, size_t ofs, void *buf, size_t len);
|
|
bool mg_spi_flash_write(struct mg_spi *spi, size_t ofs, const void *buf,
|
|
size_t len);
|
|
|
|
// Defines struct mg_flash name for a JEDEC SPI NOR chip on the struct mg_spi
|
|
// pointer spi. Start is 0, so addresses are flash offsets. The size comes
|
|
// from the chip on first use, and stays 0 if there is no chip. For example:
|
|
// static struct mg_spi s_spi = {NULL, my_begin, my_end, my_txn};
|
|
// MG_SPI_FLASH(my_flash, &s_spi);
|
|
// #define MG_LFS_FLASH (&my_flash) // In mongoose_config.h
|
|
#define MG_SPI_FLASH(name, spi) \
|
|
static bool name##_init(struct mg_flash *f) { \
|
|
if (f->size == 0) mg_spi_flash_init(spi, &f->size); \
|
|
return f->size > 0; \
|
|
} \
|
|
static bool name##_read(void *a, void *b, size_t n) { \
|
|
return mg_spi_flash_read(spi, (size_t) a, b, n); \
|
|
} \
|
|
static bool name##_write(void *a, const void *b, size_t n) { \
|
|
return mg_spi_flash_write(spi, (size_t) a, b, n); \
|
|
} \
|
|
struct mg_flash name = {NULL, 0, 4096, 1, name##_read, \
|
|
name##_write, NULL, name##_init}
|