2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
27 #include "common/crc.h"
28 #include "common/maths.h"
32 const pgRegistry_t
* pgFind(pgn_t pgn
)
35 if (pgN(reg
) == pgn
) {
42 static uint8_t *pgOffset(const pgRegistry_t
* reg
)
47 void pgResetInstance(const pgRegistry_t
*reg
, uint8_t *base
)
49 const uint16_t regSize
= pgSize(reg
);
51 memset(base
, 0, regSize
);
52 if (reg
->reset
.ptr
>= (void*)__pg_resetdata_start
&& reg
->reset
.ptr
< (void*)__pg_resetdata_end
) {
53 // pointer points to resetdata section, to it is data template
54 memcpy(base
, reg
->reset
.ptr
, regSize
);
55 } else if (reg
->reset
.fn
) {
56 // reset function, call it
61 void pgReset(const pgRegistry_t
* reg
)
63 pgResetInstance(reg
, pgOffset(reg
));
66 bool pgResetCopy(void *copy
, pgn_t pgn
)
68 const pgRegistry_t
*reg
= pgFind(pgn
);
70 pgResetInstance(reg
, copy
);
76 bool pgLoad(const pgRegistry_t
* reg
, const void *from
, int size
, int version
)
78 pgResetInstance(reg
, pgOffset(reg
));
79 // restore only matching version, keep defaults otherwise
80 if (version
== pgVersion(reg
)) {
81 const int take
= MIN(size
, pgSize(reg
));
82 memcpy(pgOffset(reg
), from
, take
);
84 *reg
->fnv_hash
= fnv_update(FNV_OFFSET_BASIS
, from
, take
);
92 int pgStore(const pgRegistry_t
* reg
, void *to
, int size
)
94 const int take
= MIN(size
, pgSize(reg
));
95 memcpy(to
, pgOffset(reg
), take
);