ACwing FC firmware
STM32WB55: attitude IEKF, altitude Kalman filter, flight core, protocol v2
Loading...
Searching...
No Matches
log_flash_ram.c
Go to the documentation of this file.
1
5#include "log_flash_ram.h"
6#include <string.h>
7
8typedef struct { uint8_t *b; uint32_t size; } ram_t;
9static ram_t s_ram;
10
11static int rd(void *c, uint32_t a, void *buf, uint32_t n)
12{
13 const ram_t *r = (const ram_t *)c;
14 if (a + n > r->size) return -1;
15 memcpy(buf, r->b + a, n);
16 return 0;
17}
18
19static int pg(void *c, uint32_t a, const void *buf, uint32_t n)
20{
21 const ram_t *r = (const ram_t *)c;
22 if (a + n > r->size || (a % LOG_PAGE) + n > LOG_PAGE) return -1;
23 const uint8_t *p = (const uint8_t *)buf;
24 for (uint32_t k = 0; k < n; ++k) r->b[a + k] &= p[k]; /* NOR: 1 -> 0 only */
25 return 0;
26}
27
28static int er(void *c, uint32_t a)
29{
30 const ram_t *r = (const ram_t *)c;
31 if (a % LOG_SECTOR || a + LOG_SECTOR > r->size) return -1;
32 memset(r->b + a, 0xFF, LOG_SECTOR);
33 return 0;
34}
35
36void log_flash_ram_init(log_flash_t *f, uint8_t *buf, uint32_t size)
37{
38 s_ram.b = buf; s_ram.size = size - size % LOG_SECTOR;
39 f->ctx = &s_ram; f->size = s_ram.size;
40 f->read = rd; f->prog = pg; f->erase = er;
41}
uint32_t n
Definition calib_mgr.c:35
static ram_t s_ram
static int pg(void *c, uint32_t a, const void *buf, uint32_t n)
static int rd(void *c, uint32_t a, void *buf, uint32_t n)
void log_flash_ram_init(log_flash_t *f, uint8_t *buf, uint32_t size)
buf: size bytes (rounded down to whole sectors)
static int er(void *c, uint32_t a)
log_flash_t on a RAM buffer with NOR semantics (erase sets 0xFF, program can only clear bits).
#define LOG_PAGE
Definition log_format.h:31
#define LOG_SECTOR
Definition log_format.h:30
flash backend; addresses in bytes from the start of the log area
Definition log_store.h:28
int(* erase)(void *ctx, uint32_t addr)
one 4 KB sector, blocking
Definition log_store.h:33
void * ctx
Definition log_store.h:29
uint32_t size
multiple of LOG_SECTOR
Definition log_store.h:30
int(* read)(void *ctx, uint32_t addr, void *buf, uint32_t n)
Definition log_store.h:31
int(* prog)(void *ctx, uint32_t addr, const void *buf, uint32_t n)
inside one 256-byte page
Definition log_store.h:32
uint32_t size
uint8_t * b