/*
 * eeprom.h
 *
 *  Created on: Dec 7, 2025
 *      Author: user
 */

#ifndef __EEPROM_H
#define __EEPROM_H

#ifdef __cplusplus
extern "C" {
#endif

#include "stm32f1xx_hal.h"

/*
 * Simple EEPROM emulation for STM32F103C8T6 (medium density).
 * - Uses 2 Flash pages (1 kB each) at the end of Flash.
 * - Stores variables as 16-bit values identified by 16-bit "virtual addresses".
 *
 * You must define the list of virtual addresses in eeprom.c: EE_VirtAddrs[].
 */

typedef enum
{
    EE_STATUS_OK = 0,
    EE_STATUS_ERROR,
    EE_STATUS_NOT_FOUND,
    EE_STATUS_NO_SPACE
} EE_Status;

/* For compatibility with ST style uint16_t return codes */
#define EE_OK        ((uint16_t)EE_STATUS_OK)
#define EE_ERROR     ((uint16_t)EE_STATUS_ERROR)
#define EE_NOT_FOUND ((uint16_t)EE_STATUS_NOT_FOUND)
#define EE_NO_SPACE  ((uint16_t)EE_STATUS_NO_SPACE)

/* Flash parameters for STM32F103C8T6 */
#define EE_FLASH_BASE_ADDR     0x08000000U
#define EE_PAGE_SIZE           0x400U      /* 1 kB pages */

/*
 * Here we assume a 64 kB Flash device (STM32F103C8T6):
 * Flash range: 0x0800 0000 - 0x0800 FFFF
 * Pages: 0..63 (64 pages)
 * We use the last 2 pages for EEPROM:
 *  - Page 62: 0x0800 F800
 *  - Page 63: 0x0800 FC00
 */
#define EE_PAGE0_BASE          (EE_FLASH_BASE_ADDR + (62U * EE_PAGE_SIZE))
#define EE_PAGE1_BASE          (EE_FLASH_BASE_ADDR + (63U * EE_PAGE_SIZE))

/* Page status markers (stored in the first halfword of each page) */
#define EE_PAGE_STATUS_ERASED  0xFFFFU
#define EE_PAGE_STATUS_VALID   0xAAAAU
#define EE_PAGE_STATUS_RECEIVE 0x5555U

/*
 * Configure how many virtual variables you have.
 * Example: bytes, words, and array elements mapped to 16-bit variables.
 * Set EE_NUM_VIRTUAL_ADDR and define EE_VirtAddrs[] in eeprom.c.
 */
/* 32 virtual variables, sequential addresses */
#define EE_NUM_VIRTUAL_ADDR    64U
#define EEW_ADDR(i)            ((uint16_t)(0x0001U + (uint16_t)(i)))   // i = 0..63


#define M1PWMAP	0	//m1pwmap
#define M1PWMCH	1	//m1pwmch
#define M2PWMAP	2	//m2pwmap
#define M2PWMCH	3	//m2pwmch
#define M3PWMAP	4	//m3pwmap
#define M3PWMCH	5	//m3pwmch
#define M4PWMAP	6	//m4pwmap
#define M4PWMCH	7	//m4pwmch
#define M1RAMPSTART 8 //m1rampstart
#define M2RAMPSTART 9 //m2rampstart
#define M3RAMPSTART 10 //m3rampstart
#define M4RAMPSTART 11 //m4rampstart
#define T1AP 12 //t1ap
#define T2AP 13 //t2ap
#define T3AP 14 //t3ap
#define T4AP 15 //t4ap
#define TWAP 16 //twap
#define T1CH 17 //t1ch
#define T2CH 18 //t2ch
#define T3CH 19 //t3ch
#define T4CH 20 //t4ch
#define TWCH 21 //twch

#define TRAMP1   25  //tramp1
#define M1PWMMAN 26 //m1pwmMan
#define M2PWMMAN 27 //m2pwmMan
#define M3PWMMAN 28 //m3pwmMan
#define M4PWMMAN 29 //m4pwmMan
#define TRAMPMAN 30 //trampman
#define TRAMP 31 //tramp


/* Virtual address table (defined in eeprom.c, can be customized) */
extern const uint16_t EE_VirtAddrs[EE_NUM_VIRTUAL_ADDR];

/* Public API – now using uint16_t like ST examples */
uint16_t EE_Init(void);
uint16_t EE_ReadVariable(uint16_t VirtAddress, uint16_t *Data);
uint16_t EE_WriteVariable(uint16_t VirtAddress, uint16_t Data);
void loadEE(void);
/* Pseudo-array accessor EEW[idx] */
static inline uint16_t EEW_Read(uint8_t idx, uint16_t *value)
{
    return EE_ReadVariable(EEW_ADDR(idx), value);
}

static inline uint16_t EEW_Write(uint8_t idx, uint16_t value)
{
    return EE_WriteVariable(EEW_ADDR(idx), value);
}

#ifdef __cplusplus
}
#endif

#endif /* __EEPROM_H */
