feat: Power-Cycle-Zähler für den Werksreset
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
#include "config/nl_reset_counter.h"
|
||||
|
||||
#include "config/nl_flash.h"
|
||||
|
||||
#define REC_TAG 0x5A000000u
|
||||
#define REC_MASK 0xFF000000u
|
||||
#define REC_FREE 0xFFFFFFFFu
|
||||
#define REC_SLOTS (NL_FLASH_SECTOR_SIZE / 4u)
|
||||
|
||||
static uint32_t s_sector;
|
||||
static uint32_t s_next;
|
||||
static uint8_t s_count;
|
||||
|
||||
void nl_resetcnt_init(uint32_t sector)
|
||||
{
|
||||
uint32_t w;
|
||||
|
||||
s_sector = sector;
|
||||
s_next = 0;
|
||||
s_count = 0;
|
||||
for (uint32_t i = 0; i < REC_SLOTS; i++) {
|
||||
if (nl_flash_read(sector + i * 4u, &w, 4) != 0 || w == REC_FREE)
|
||||
continue;
|
||||
s_next = i + 1;
|
||||
s_count = ((w & REC_MASK) == REC_TAG) ? (uint8_t)(w & 0xFFu) : 0;
|
||||
}
|
||||
}
|
||||
|
||||
static int put(uint8_t count)
|
||||
{
|
||||
uint32_t w = REC_TAG | count;
|
||||
int r;
|
||||
|
||||
if (s_next >= REC_SLOTS) {
|
||||
if (nl_flash_erase_sector(s_sector) != 0)
|
||||
return -1;
|
||||
s_next = 0;
|
||||
}
|
||||
r = nl_flash_write(s_sector + s_next * 4u, &w, 4);
|
||||
s_next++;
|
||||
if (r == 0)
|
||||
s_count = count;
|
||||
return r;
|
||||
}
|
||||
|
||||
uint8_t nl_resetcnt_boot(void)
|
||||
{
|
||||
uint8_t c = s_count < 255 ? (uint8_t)(s_count + 1) : 255;
|
||||
put(c);
|
||||
return c;
|
||||
}
|
||||
|
||||
int nl_resetcnt_clear(void)
|
||||
{
|
||||
if (s_count == 0)
|
||||
return 0;
|
||||
return put(0);
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
#ifndef NL_RESET_COUNTER_H
|
||||
#define NL_RESET_COUNTER_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define NL_FACTORY_RESET_BOOTS 5u
|
||||
#define NL_RESETCNT_CLEAR_MS 5000u
|
||||
|
||||
void nl_resetcnt_init(uint32_t sector);
|
||||
uint8_t nl_resetcnt_boot(void);
|
||||
int nl_resetcnt_clear(void);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user