/**
 * @file flash_param.h
 * @brief Flash parameter storage module for TCP2UART
 * 
 * Stores configuration parameters in STM32F103 internal Flash.
 * Uses the last page of Flash (1KB) for parameter storage.
 */

#ifndef __FLASH_PARAM_H__
#define __FLASH_PARAM_H__

#include <stdint.h>
#include <stdbool.h>

#ifdef __cplusplus
extern "C" {
#endif

/* Flash configuration for STM32F103R8 (64KB Flash) */
#define FLASH_PARAM_PAGE_SIZE       1024        /* 1KB per page for STM32F103 */
#define FLASH_PARAM_START_ADDR      0x0800FC00  /* Last 1KB of 64KB Flash */
#define FLASH_PARAM_END_ADDR        0x08010000  /* End of Flash */

/* For STM32F103RC (256KB), use: 0x0803FC00 */
/* For STM32F103RB (128KB), use: 0x0801FC00 */

/**
 * @brief Initialize Flash parameter storage
 * @return 0 on success, negative on error
 */
int flash_param_init(void);

/**
 * @brief Read parameters from Flash
 * @param data Output buffer
 * @param len Length to read
 * @return 0 on success, negative on error
 */
int flash_param_read(void *data, uint32_t len);

/**
 * @brief Write parameters to Flash
 * @param data Data to write
 * @param len Length to write
 * @return 0 on success, negative on error
 * 
 * Note: This function will erase the Flash page before writing.
 */
int flash_param_write(const void *data, uint32_t len);

/**
 * @brief Erase parameter storage area
 * @return 0 on success, negative on error
 */
int flash_param_erase(void);

/**
 * @brief Calculate CRC32 for data
 * @param data Data buffer
 * @param len Data length
 * @return CRC32 value
 */
uint32_t flash_param_crc32(const void *data, uint32_t len);

/**
 * @brief Verify parameter storage integrity
 * @return 0 if valid, negative if invalid or corrupted
 */
int flash_param_verify(void);

#ifdef __cplusplus
}
#endif

#endif /* __FLASH_PARAM_H__ */
