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/maths.h"
31 const pgRegistry_t
* pgFind(pgn_t pgn
)
34 if (pgN(reg
) == pgn
) {
41 static uint8_t *pgOffset(const pgRegistry_t
* reg
)
46 void pgResetInstance(const pgRegistry_t
*reg
, uint8_t *base
)
48 const uint16_t regSize
= pgSize(reg
);
50 memset(base
, 0, regSize
);
51 if (reg
->reset
.ptr
>= (void*)__pg_resetdata_start
&& reg
->reset
.ptr
< (void*)__pg_resetdata_end
) {
52 // pointer points to resetdata section, to it is data template
53 memcpy(base
, reg
->reset
.ptr
, regSize
);
54 } else if (reg
->reset
.fn
) {
55 // reset function, call it
60 void pgReset(const pgRegistry_t
* reg
)
62 pgResetInstance(reg
, pgOffset(reg
));
65 bool pgResetCopy(void *copy
, pgn_t pgn
)
67 const pgRegistry_t
*reg
= pgFind(pgn
);
69 pgResetInstance(reg
, copy
);
75 bool pgLoad(const pgRegistry_t
* reg
, const void *from
, int size
, int version
)
77 pgResetInstance(reg
, pgOffset(reg
));
78 // restore only matching version, keep defaults otherwise
79 if (version
== pgVersion(reg
)) {
80 const int take
= MIN(size
, pgSize(reg
));
81 memcpy(pgOffset(reg
), from
, take
);
89 int pgStore(const pgRegistry_t
* reg
, void *to
, int size
)
91 const int take
= MIN(size
, pgSize(reg
));
92 memcpy(to
, pgOffset(reg
), take
);