#ifndef FLASH_RING_H
#define FLASH_RING_H
#include "stm32f1xx_hal.h"

//#define FLASH_PAGE_SIZE      1024
#define NUM_OF_PAGE_EEPROM   2
#define FLASH_START_ADDR     0x08000000
#define FLASH_SIZE           (64 * 1024)  // для STM32F103C8
#define LAST_PAGE_ADDR       (FLASH_START_ADDR + FLASH_SIZE - NUM_OF_PAGE_EEPROM*FLASH_PAGE_SIZE)
#define RECORD_SIZE          255
#define RECORDS_PER_PAGE     NUM_OF_PAGE_EEPROM*(FLASH_PAGE_SIZE / RECORD_SIZE)  // 10 записей


#pragma pack(push, 1)
typedef struct {
    uint32_t timestamp;
    uint8_t data[RECORD_SIZE-4];  // 200 - 4 байта timestamp
} FlashRecord_t;
#pragma pack(pop)

typedef struct {
    uint32_t write_index;  // индекс следующей записи (0-9)
    uint8_t initialized;   // флаг инициализации
} BufferState_t;
BufferState_t buffer_init(void);
HAL_StatusTypeDef buffer_write_record(FlashRecord_t* record, BufferState_t* state);
HAL_StatusTypeDef erase_flash_page(void) ;
HAL_StatusTypeDef write_flash_record(uint32_t address, FlashRecord_t* record);
FlashRecord_t* buffer_read_record(uint32_t index);
void buffer_get_all_records(FlashRecord_t* records[], uint32_t* count);
#endif // FLASH_RING_H