no-OS
Loading...
Searching...
No Matches
flash_storage.h
Go to the documentation of this file.
1/*******************************************************************************
2 * @file flash_storage.h
3 * @brief Flash storage interface for PQM calibration data
4 * @author Radu Etz (radu.etz@analog.com)
5 ********************************************************************************
6 * Copyright (c) 2026 Analog Devices, Inc.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
17 *
18 * 3. Neither the name of Analog Devices, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived from this
20 * software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES, INC. "AS IS" AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
25 * EVENT SHALL ANALOG DEVICES, INC. BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
28 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
31 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *******************************************************************************/
33
34#ifndef __FLASH_STORAGE_H__
35#define __FLASH_STORAGE_H__
36
37#include <stdint.h>
38#include <stdbool.h>
39
40/*******************************************************************************
41 * Flash Memory Layout (MAX32650 Internal Flash - 3MB)
42 *
43 * Uses last two pages of internal flash for calibration storage.
44 * Page size: 8KB (8192 bytes)
45 *
46 * Address Range | Size | Content
47 * ---------------------------|--------|---------------------------
48 * 0x10000000 - 0x102FBFFF | ~3MB | Application code
49 * 0x102FC000 - 0x102FDFFF | 8KB | Calibration Data (2nd to last page)
50 * 0x102FE000 - 0x102FFFFF | 8KB | Calibration Backup (last page)
51 ******************************************************************************/
52
53/* Flash sector size (4KB for MX25U6432F) */
54#define FLASH_SECTOR_SIZE 0x1000
55
56/* Magic number for data validation */
57#define FLASH_MAGIC_CALIBRATION 0x43414C49 /* "CALI" */
58
59/* Data structure version (increment when structure changes) */
60#define FLASH_VERSION_CALIBRATION 0x0001
61
62/* Number of channels (phases) */
63#define FLASH_NUM_CHANNELS 3
64
68typedef struct {
69 /* Gain coefficients (from gain calibration) */
70 int32_t i_gain; /* Current gain coefficient */
71 int32_t v_gain; /* Voltage gain coefficient */
72 /* Offset coefficients (from offset calibration) */
73 int32_t i_rmsos; /* Current RMS offset */
74 int32_t v_rmsos; /* Voltage RMS offset */
75 int32_t if_rmsos; /* Current fundamental RMS offset */
76 int32_t vf_rmsos; /* Voltage fundamental RMS offset */
77 /* Calibration status */
78 bool gain_calibrated; /* Gain calibration done flag */
79 bool offset_calibrated; /* Offset calibration done flag */
80 /* Calibration errors (for reference) */
81 float gain_i_error; /* Current error after gain cal (%) */
82 float gain_v_error; /* Voltage error after gain cal (%) */
83 float offset_i_error; /* Current error after offset cal (%) */
84 float offset_v_error; /* Voltage error after offset cal (%) */
85 /* Reserved for future use */
86 uint8_t reserved[16];
88
92typedef struct {
93 /* Header */
94 uint32_t magic; /* Magic number for validation */
95 uint16_t version; /* Structure version */
96 uint16_t reserved_hdr; /* Reserved */
97 /* Per-channel calibration data */
99 /* Calibration input parameters used */
100 float cal_nominal_current; /* Nominal current used for gain cal */
101 float cal_nominal_voltage; /* Nominal voltage used for gain cal */
102 float cal_offset_current; /* Offset current used for offset cal */
103 float cal_offset_voltage; /* Offset voltage used for offset cal */
104 /* Timestamp of last calibration */
105 uint32_t timestamp; /* Unix timestamp of last calibration */
106 /* CRC for data integrity */
107 uint32_t crc32; /* CRC32 of all data above */
109
125
130int flash_storage_init(void);
131
136int flash_storage_remove(void);
137
143
150
157
163
169
175int flash_save_calibration_channel(uint8_t channel);
176
182
188
194const char *flash_status_to_string(FLASH_STATUS status);
195
202uint32_t flash_calculate_crc32(const void *data, uint32_t len);
203
204#endif /* __FLASH_STORAGE_H__ */
int flash_storage_init(void)
Initialize flash storage.
Definition flash_storage.c:94
bool flash_storage_is_initialized(void)
Check if flash storage is initialized.
Definition flash_storage.c:114
int flash_save_all_calibration(void)
Save all calibration data to flash.
Definition flash_storage.c:394
int flash_write_calibration(const FLASH_CALIBRATION_DATA *data)
Write calibration data to flash.
Definition flash_storage.c:153
int flash_save_calibration_channel(uint8_t channel)
Save current calibration context to flash for specified channel.
Definition flash_storage.c:349
int flash_erase_calibration(void)
Erase calibration data from flash.
Definition flash_storage.c:224
const char * flash_status_to_string(FLASH_STATUS status)
Get status string for flash status code.
Definition flash_storage.c:474
int flash_storage_remove(void)
De-initialize flash storage.
Definition flash_storage.c:108
FLASH_STATUS
Flash storage status.
Definition flash_storage.h:113
@ FLASH_STATUS_ERASE_FAILED
Definition flash_storage.h:119
@ FLASH_STATUS_INVALID_VERSION
Definition flash_storage.h:117
@ FLASH_STATUS_INIT_FAILED
Definition flash_storage.h:122
@ FLASH_STATUS_INVALID_CRC
Definition flash_storage.h:116
@ FLASH_STATUS_READ_FAILED
Definition flash_storage.h:121
@ FLASH_STATUS_NOT_INITIALIZED
Definition flash_storage.h:123
@ FLASH_STATUS_NO_DATA
Definition flash_storage.h:115
@ FLASH_STATUS_INVALID_MAGIC
Definition flash_storage.h:118
@ FLASH_STATUS_OK
Definition flash_storage.h:114
@ FLASH_STATUS_WRITE_FAILED
Definition flash_storage.h:120
int flash_read_calibration(FLASH_CALIBRATION_DATA *data)
Read calibration data from flash.
Definition flash_storage.c:119
uint32_t flash_calculate_crc32(const void *data, uint32_t len)
Calculate CRC32 for data.
Definition flash_storage.c:79
bool flash_has_valid_calibration(void)
Check if valid calibration data exists in flash.
Definition flash_storage.c:254
#define FLASH_NUM_CHANNELS
Definition flash_storage.h:63
int flash_load_and_apply_calibration(void)
Load calibration from flash and apply to AFE registers.
Definition flash_storage.c:260
Complete calibration data structure.
Definition flash_storage.h:92
float cal_nominal_voltage
Definition flash_storage.h:101
uint32_t crc32
Definition flash_storage.h:107
uint32_t timestamp
Definition flash_storage.h:105
FLASH_CHANNEL_CALIBRATION channel[FLASH_NUM_CHANNELS]
Definition flash_storage.h:98
uint32_t magic
Definition flash_storage.h:94
float cal_nominal_current
Definition flash_storage.h:100
float cal_offset_voltage
Definition flash_storage.h:103
uint16_t reserved_hdr
Definition flash_storage.h:96
uint16_t version
Definition flash_storage.h:95
float cal_offset_current
Definition flash_storage.h:102
Calibration coefficients for a single channel (phase)
Definition flash_storage.h:68
int32_t i_rmsos
Definition flash_storage.h:73
float gain_v_error
Definition flash_storage.h:82
bool gain_calibrated
Definition flash_storage.h:78
uint8_t reserved[16]
Definition flash_storage.h:86
int32_t v_gain
Definition flash_storage.h:71
int32_t v_rmsos
Definition flash_storage.h:74
float gain_i_error
Definition flash_storage.h:81
int32_t if_rmsos
Definition flash_storage.h:75
int32_t i_gain
Definition flash_storage.h:70
float offset_v_error
Definition flash_storage.h:84
int32_t vf_rmsos
Definition flash_storage.h:76
float offset_i_error
Definition flash_storage.h:83
bool offset_calibrated
Definition flash_storage.h:79